diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index 27e86d3..c8d9fbe 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -142,7 +142,9 @@ pub fn class_decl( for mut method in methods { match method.item.visibility { Visibility::Public => { - gen_method_point(&method.item, ctx, cg); + if !method.item.is_override { + gen_method_point(&method.item, ctx, cg); + } method.item.name.0.insert_str(0, "__m_"); } @@ -271,7 +273,9 @@ impl GenRust cg.addln(");"); if self.2 { - cg.add("this._super._m_oop.0 = &Self::__SUPER_V_TABLE as *const *const std::ffi::c_void;"); + cg.add_indentedln( + "this._super._m_oop.0 = &Self::__SUPER_V_TABLE as *const *const std::ffi::c_void;", + ); } cg.add_indentedln("this"); diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index 9ca0f16..71cbfcf 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -1,62 +1,29 @@ -pub class Counter { - pub count i32; - - pub constructor(start i32) { - self.count = start; - } - - pub fn increment(*mut self) { - self.count++; - } - - pub fn get(*self) i32 { - self.count - } -} - -pub fn test_class_create() { - let c = Counter::new(0); - assert_eq!(c.get(), 0); -} - -pub fn test_class_method() { - let mut c = Counter::new(5); - c.increment(); - assert_eq!(c.get(), 6); -} - -pub fn test_class_field_access() { - let c = Counter::new(42); - assert_eq!(c.count, 42); -} - -class NamedCounter : Counter { +pub class Animal { pub name String; - pub constructor(start i32, name String) { - self._super = Counter::new(start); - - self.name = name; + constructor() { + self.name = "Rex".to_string(); } - pub fn get_name(*self) String { - self.name.clone() - } - - pub override fn increment(*mut self) { - println!("OK!!"); + pub fn speak(*self) { + println!("Unknown"); } } -pub fn test_class_inherit() { - let nc = NamedCounter::new(10, "test".to_string()); - assert_eq!(nc.get_name(), "test"); - assert_eq!(nc.count, 10); +pub class Dog : Animal { + constructor() { + unsafe { + std::ptr::write(&mut self._super, Animal::new()); + } + } + + pub override fn speak(*self) { + println!("Woof!"); + } } pub fn run_tests() { - test_class_create(); - test_class_method(); - test_class_field_access(); - test_class_inherit(); + let dawg = Dog::new(); + + dawg.speak(); }