Working inheritance #68

This commit is contained in:
2026-06-07 01:57:09 +02:00
parent 1977bd59be
commit e70e813497
2 changed files with 24 additions and 53 deletions
+5 -1
View File
@@ -142,7 +142,9 @@ pub fn class_decl(
for mut method in methods {
match method.item.visibility {
Visibility::Public => {
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");
+18 -51
View File
@@ -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();
}