Working inheritance #68
This commit is contained in:
@@ -142,7 +142,9 @@ pub fn class_decl(
|
|||||||
for mut method in methods {
|
for mut method in methods {
|
||||||
match method.item.visibility {
|
match method.item.visibility {
|
||||||
Visibility::Public => {
|
Visibility::Public => {
|
||||||
|
if !method.item.is_override {
|
||||||
gen_method_point(&method.item, ctx, cg);
|
gen_method_point(&method.item, ctx, cg);
|
||||||
|
}
|
||||||
|
|
||||||
method.item.name.0.insert_str(0, "__m_");
|
method.item.name.0.insert_str(0, "__m_");
|
||||||
}
|
}
|
||||||
@@ -271,7 +273,9 @@ impl GenRust
|
|||||||
cg.addln(");");
|
cg.addln(");");
|
||||||
|
|
||||||
if self.2 {
|
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");
|
cg.add_indentedln("this");
|
||||||
|
|||||||
@@ -1,62 +1,29 @@
|
|||||||
pub class Counter {
|
pub class Animal {
|
||||||
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 name String;
|
pub name String;
|
||||||
|
|
||||||
pub constructor(start i32, name String) {
|
constructor() {
|
||||||
self._super = Counter::new(start);
|
self.name = "Rex".to_string();
|
||||||
|
|
||||||
self.name = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_name(*self) String {
|
pub fn speak(*self) {
|
||||||
self.name.clone()
|
println!("Unknown");
|
||||||
}
|
|
||||||
|
|
||||||
pub override fn increment(*mut self) {
|
|
||||||
println!("OK!!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test_class_inherit() {
|
pub class Dog : Animal {
|
||||||
let nc = NamedCounter::new(10, "test".to_string());
|
constructor() {
|
||||||
assert_eq!(nc.get_name(), "test");
|
unsafe {
|
||||||
assert_eq!(nc.count, 10);
|
std::ptr::write(&mut self._super, Animal::new());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub override fn speak(*self) {
|
||||||
|
println!("Woof!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_tests() {
|
pub fn run_tests() {
|
||||||
test_class_create();
|
let dawg = Dog::new();
|
||||||
test_class_method();
|
|
||||||
test_class_field_access();
|
dawg.speak();
|
||||||
test_class_inherit();
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user