Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yhara committed Dec 10, 2023
1 parent 3d6ec19 commit 58bdad6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/skc_rustlib/src/builtin/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::builtin::{SkInt, SkObj};
use crate::sk_methods::meta_array_new;
use shiika_ffi_macro::shiika_method;
use crate::builtin::{SkInt, SkObj, SkClass, SkStr};
use shiika_ffi_macro::{shiika_const_ref, shiika_method_ref, shiika_method};

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 @@ -14,10 +20,21 @@ struct ShiikaArray<T> {
vec: *mut Vec<T>,
}

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

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
4 changes: 4 additions & 0 deletions lib/skc_rustlib/src/builtin/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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

0 comments on commit 58bdad6

Please sign in to comment.