From 0215e295b5c71be1885ee870dccc7323355c5514 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Tue, 9 Jun 2026 14:47:41 +0200 Subject: [PATCH] Working with generics --- crates/codegen/src/class_decl.rs | 46 ++++++++++++++++------------- mist-test/src/items/class_test.mist | 5 ++-- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index e32a894..e6027ba 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -25,6 +25,23 @@ pub fn class_decl( let self_ty = get_type_from_path(&self_path); + let methods = items + .clone() + .into_iter() + .filter_map(|item| match item { + ClassItem::ImplDecl(_) => None, + ClassItem::Method(method) => Some(method), + }) + .collect::>(); + + let v_table = methods + .iter() + .filter_map(|method| match method.item.visibility { + Visibility::Public => Some((method.item.name.clone(), method.item.is_override.clone())), + _ => None, + }) + .collect::>(); + // Struct decl cg.addln(&format!( "{}struct {}{} {{", @@ -35,8 +52,8 @@ pub fn class_decl( cg.indent += 1; cg.add_indentedln(&format!( - "pub _m_oop: (&'static [*const std::ffi::c_void; {}::__V_COUNT], *mut std::ffi::c_void),", - self_path.get_rust() + "pub _m_oop: (&'static [*const std::ffi::c_void; {}], *mut std::ffi::c_void),", + v_table.len() )); if let Some(inherits) = inherits { @@ -61,23 +78,6 @@ pub fn class_decl( )); cg.indent += 1; - let methods = items - .clone() - .into_iter() - .filter_map(|item| match item { - ClassItem::ImplDecl(_) => None, - ClassItem::Method(method) => Some(method), - }) - .collect::>(); - - let v_table = methods - .iter() - .filter_map(|method| match method.item.visibility { - Visibility::Public => Some((method.item.name.clone(), method.item.is_override.clone())), - _ => None, - }) - .collect::>(); - // V TABLE { cg.add_indentedln(&format!("pub const __V_COUNT: usize = {};", v_table.len())); @@ -89,7 +89,10 @@ pub fn class_decl( )); } - cg.add_indentedln("pub const __V_TABLE: [*const std::ffi::c_void; Self::__V_COUNT] = ["); + cg.add_indentedln(&format!( + "pub const __V_TABLE: [*const std::ffi::c_void; {}] = [", + v_table.len() + )); cg.indent += 1; for (method_name, _) in &v_table { @@ -120,9 +123,10 @@ pub fn class_decl( for (name, is_override) in v_table { if is_override.is_some() { cg.add_indentedln(&format!( - "table[{}::__FN_{}] = Self::__m_{} as *const std::ffi::c_void;", + "table[{}::__FN_{}] = {}::__m_{} as *const std::ffi::c_void;", inherits.get_rust(), name.0.to_uppercase(), + self_path.get_rust(), name.get_rust() )); } diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index ad6c555..ae85717 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -1,5 +1,6 @@ -pub class Animal { +pub class Animal { pub name String; + pub val T; constructor() { self.name = "Rex".to_string(); @@ -14,7 +15,7 @@ pub class Animal { } } -pub class Dog : Animal { +pub class Dog : Animal { constructor() { unsafe { std::ptr::write(&mut super, Super::new());