Skip to content

Commit

Permalink
fix: SkAry::new should set the class object
Browse files Browse the repository at this point in the history
  • Loading branch information
yhara committed Dec 14, 2023
1 parent 001b13c commit d8f726e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
18 changes: 13 additions & 5 deletions lib/skc_rustlib/src/builtin/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use crate::builtin::{SkInt, SkObj};
use crate::sk_methods::meta_array_new;
use shiika_ffi_macro::shiika_method;
use crate::builtin::{SkClass, SkInt, SkObj};
use crate::sk_cls::SkCls;
use shiika_ffi_macro::{shiika_const_ref, shiika_method, shiika_method_ref};

shiika_const_ref!("::Array", SkClass, "sk_Array");
shiika_method_ref!(
"Meta:Array#new",
fn(receiver: SkClass) -> SkAry<SkObj>,
"meta_array_new"
);

#[repr(C)]
#[derive(Debug)]
Expand All @@ -16,8 +23,9 @@ struct ShiikaArray<T> {

impl<T> SkAry<T> {
/// Call `Array.new`.
pub fn new<U>() -> SkAry<U> {
let sk_ary = meta_array_new(std::ptr::null());
pub fn new<U: SkCls>() -> SkAry<U> {
let spe_cls = sk_Array().specialize(vec![U::get_class_object()]);
let sk_ary = meta_array_new(spe_cls);
// Force cast because external function (Meta_Array_new)
// cannot have type a parameter.
SkAry(sk_ary.0 as *mut ShiikaArray<U>)
Expand Down
14 changes: 12 additions & 2 deletions lib/skc_rustlib/src/builtin/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
mod witness_table;
use crate::builtin::class::witness_table::WitnessTable;
use crate::builtin::{SkAry, SkInt, SkStr};
use crate::sk_methods::meta_class_new;
use shiika_ffi_macro::shiika_method;
use shiika_ffi_macro::{shiika_method, shiika_method_ref};
use std::collections::HashMap;

shiika_method_ref!(
"Meta:Class#new",
fn(receiver: *const u8) -> SkClass,
"meta_class_new"
);

#[repr(C)]
#[derive(Debug)]
pub struct SkClass(*mut ShiikaClass);
Expand All @@ -31,6 +37,10 @@ impl SkClass {
unsafe { &(*self.0).name }
}

pub fn specialize(self, tyargs: Vec<SkClass>) -> SkClass {
class_specialize(self, tyargs)
}

fn specialized_classes(&mut self) -> &mut HashMap<String, *mut ShiikaClass> {
unsafe { (*self.0).specialized_classes.as_mut().unwrap() }
}
Expand Down
12 changes: 10 additions & 2 deletions lib/skc_rustlib/src/builtin/string.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
//! Instance of `::String`
use crate::builtin::object::ShiikaObject;
use crate::builtin::{SkAry, SkInt, SkObj, SkPtr};
use shiika_ffi_macro::shiika_method;
use crate::builtin::{SkAry, SkClass, SkInt, SkObj, SkPtr};
use crate::sk_cls::SkCls;
use shiika_ffi_macro::{shiika_const_ref, shiika_method};
use std::ffi::CString;
use unicode_segmentation::UnicodeSegmentation;

shiika_const_ref!("::String", SkClass, "sk_String");
impl SkCls for SkStr {
fn get_class_object() -> SkClass {
sk_String()
}
}

extern "C" {
// TODO: better name
fn gen_literal_string(p: *const u8, bytesize: i64) -> SkStr;
Expand Down

0 comments on commit d8f726e

Please sign in to comment.