Skip to content

Commit 2408221

Browse files
committed
second test fails idk why
1 parent 1c1c982 commit 2408221

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

zenlang/src/value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum Value {
2525
String(String),
2626
Boolean(bool),
2727
FunctionRef(u64, u64),
28-
FunctionRefEnv(u64, u64, Rc<RefCell<Environment>>),
28+
FunctionRefEnv(u64, u64, Weak<RefCell<Environment>>),
2929
Object(Rc<RefCell<Object>>),
3030
Null(),
3131
}

zenlang/src/vm/opcodes/vm_call.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::strong_u64::U64BitsControl;
22
use crate::value::*;
33
use crate::vm::VM;
44
use alloc::format;
5+
//use alloc::rc::Rc;
56
use alloc::string::*;
67

78
impl VM {
@@ -40,7 +41,11 @@ impl VM {
4041
self.check_stack_overflow();
4142
self.pc = addr;
4243

43-
self.environs.push(env);
44+
if let Some(env) = env.upgrade() {
45+
self.environs.push(env);
46+
} else {
47+
//self.push_environment();
48+
}
4449

4550
let this_name = &String::from("self");
4651
let environ = self.environs.last_mut().unwrap();

zenlang/src/vm/opcodes/vm_closure.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::strong_u64::U64BitsControl;
22
use crate::value::Value;
33
use crate::vm::VM;
4+
use alloc::rc::*;
45

56
impl VM {
67
pub fn op_closure(&mut self, skip: u32, args: u64) {
78
let value = Value::FunctionRefEnv(
89
self.pc.get_low() as u64,
910
args,
10-
self.environs.last().unwrap().clone(),
11+
Rc::downgrade(self.environs.last().unwrap()),
1112
);
1213

1314
self.pc.add_low(skip);

zenlang/src/vm/vm.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,25 +184,24 @@ impl VM {
184184
if self.environs.len() == 0 {
185185
panic!("pop_environment: environs is empty");
186186
}
187-
// Fuck reference counted ptrs
188-
// Why do I have to count references, MANUALLY
189-
// (there is probably a better way to do this, but time's ticking)
190-
let environ = self.environs.last().unwrap();
191-
let mut refs: usize = 0;
192-
for e in self.environs.iter() {
193-
if Rc::ptr_eq(e, environ) {
194-
refs += 1;
187+
188+
let mut in_use = false;
189+
let env = self.environs.last().unwrap();
190+
for environ in self.environs.iter() {
191+
for var in (&*environ.borrow()).vars.iter() {
192+
if let Value::FunctionRefEnv(_, _, e) = &var.1 {
193+
if let Some(e) = &e.upgrade() {
194+
if Rc::ptr_eq(env, e) {
195+
in_use = true;
196+
}
197+
}
198+
}
195199
}
196200
}
197201

198-
// If refs != 1, the vars are prolly used somewhere else so don't clear them
199-
// TODO: This doesn't work and doesn't pass the fourth test in vm_closure.rs, instead we
200-
// should try using weak ptr's
201-
if refs == 1 {
202-
(&mut *environ.borrow_mut()).vars.clear();
202+
if !in_use {
203+
//self.environs.pop();
203204
}
204-
205-
self.environs.pop();
206205
}
207206

208207
pub fn get_function_name_from_pc(&mut self, pc: u64) -> Option<String> {

zenlang/tests/vm_closure.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ fn closure array {
126126
127127
fn main {
128128
let arr = [];
129-
let f =closure(arr);
130129
131-
return f();
130+
return closure(arr)();
132131
}
133132
"#
134133
.into(),

0 commit comments

Comments
 (0)