Skip to content

Commit 3d1308c

Browse files
committed
self method binding working
1 parent 50e874e commit 3d1308c

File tree

2 files changed

+53
-49
lines changed

2 files changed

+53
-49
lines changed

example/test.gila

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ Vec type
55
x: u32
66
end
77

8+
9+
10+
11+
812
foo fn(self: Vec)
913
std.io.print(self)
1014
end
1115

12-
v = Vec(1)
1316

14-
v.foo(v)
17+
18+
v=Vec(1)
19+
v.foo()
20+

src/execution.rs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,13 +1047,13 @@ impl<'a> ExecutionEngine<'a> {
10471047
return Err(obj.err().unwrap());
10481048
}
10491049
if let GCRefData::DYNAMIC_OBJECT(o) = obj.unwrap() {
1050-
// bind the function to the object here
1051-
let mut bounded_fn = f.clone();
1052-
bounded_fn.bounded_object = Some(g.clone());
1053-
// set the function bounded ref
1054-
self.shared_execution_context
1055-
.heap
1056-
.set(gc_ref, GCRefData::FN(bounded_fn));
1050+
// // bind the function to the object here
1051+
// let mut bounded_fn = f.clone();
1052+
// bounded_fn.bounded_object = Some(g.clone());
1053+
// // set the function bounded ref
1054+
// self.shared_execution_context
1055+
// .heap
1056+
// .set(gc_ref, GCRefData::FN(bounded_fn));
10571057

10581058
// update the object in the heap
10591059
let mut cloned_obj = o.clone();
@@ -1121,8 +1121,8 @@ impl<'a> ExecutionEngine<'a> {
11211121
let obj = stack_access!(self, instr.arg_0);
11221122
// fixme this is horrible nesting
11231123
match obj {
1124-
Object::GC_REF(gc_ref) => {
1125-
let result = self.shared_execution_context.heap.deref(gc_ref);
1124+
Object::GC_REF(obj_gc_ref) => {
1125+
let result = self.shared_execution_context.heap.deref(obj_gc_ref);
11261126
if result.is_err() {
11271127
return Err(result.err().unwrap());
11281128
}
@@ -1180,44 +1180,42 @@ impl<'a> ExecutionEngine<'a> {
11801180
return Err(RuntimeError::INVALID_ACCESS);
11811181
}
11821182

1183-
// todo what we can do is, on an access here, allocate a new BoundedMethod heap object and call it?
1184-
// we should store a binded method cache for this like cpython
1185-
// alternatively, we can bind the obj onto the FunctionObject itself and not create a new data structure,
1186-
// may be easier for now
1187-
1188-
// // if result is a function and its bound, we should bind it!
1189-
// match result.unwrap() {
1190-
// Object::GC_REF(gc_ref) => {
1191-
// let res = self
1192-
// .shared_execution_context
1193-
// .heap
1194-
// .deref(gc_ref);
1195-
// if res.is_err() {
1196-
// return Err(res.err().unwrap());
1197-
// }
1198-
// match res.unwrap() {
1199-
// GCRefData::FN(f) => {
1200-
// let mut fnn = f.clone();
1201-
// fnn.bounded_object = Some(gc_ref.clone());
1202-
// stack_set!(
1203-
// self,
1204-
// instr.arg_2,
1205-
1206-
// ),
1207-
// }
1208-
// _ => stack_set!(
1209-
// self,
1210-
// instr.arg_2,
1211-
// result.unwrap().clone()
1212-
// ),
1213-
// }
1214-
// }
1215-
// _ => stack_set!(
1216-
// self,
1217-
// instr.arg_2,
1218-
// result.unwrap().clone()
1219-
// ),
1220-
// }
1183+
// BINDING HAPPENS HERE
1184+
match result {
1185+
Object::GC_REF(method_to_bind_gc_ref) => {
1186+
let deref = self
1187+
.shared_execution_context
1188+
.heap
1189+
.deref(method_to_bind_gc_ref);
1190+
if deref.is_err() {
1191+
return Err(deref.err().unwrap());
1192+
}
1193+
match deref.unwrap() {
1194+
GCRefData::FN(mut f) => {
1195+
if f.requires_method_binding {
1196+
f.bounded_object =
1197+
Some(obj_gc_ref.clone());
1198+
1199+
// todo we should probably not set the actual object? and instead return another?
1200+
// because now its forever bound?
1201+
let res = self
1202+
.shared_execution_context
1203+
.heap
1204+
.set(
1205+
method_to_bind_gc_ref,
1206+
GCRefData::FN(f.clone()),
1207+
);
1208+
if res.is_err() {
1209+
return Err(res.err().unwrap());
1210+
}
1211+
}
1212+
}
1213+
_ => {}
1214+
}
1215+
}
1216+
_ => {}
1217+
}
1218+
12211219
stack_set!(self, instr.arg_2, result.clone());
12221220
increment_ip!(self);
12231221
}

0 commit comments

Comments
 (0)