feat(env): Rc

This commit is contained in:
2025-05-17 19:10:10 +08:00
parent ff9afd0cc1
commit fb14027845
6 changed files with 44 additions and 65 deletions

View File

@@ -1,5 +1,4 @@
use std::collections::HashSet;
use std::rc::Rc;
use derive_more::Constructor;
use itertools::Itertools;
@@ -43,7 +42,7 @@ impl<'vm> AttrSet<'vm> {
self.data.get(&sym).is_some()
}
pub fn capture(&mut self, env: &Rc<Env<'vm>>) {
pub fn capture(&mut self, env: &Env<'vm>) {
self.data.iter().for_each(|(_, v)| match v.clone() {
Value::Thunk(ref thunk) => {
thunk.capture(env.clone());

View File

@@ -1,6 +1,3 @@
use std::collections::HashMap;
use std::rc::Rc;
use derive_more::Constructor;
use itertools::Itertools;
@@ -46,7 +43,7 @@ pub type JITFunc<'vm> =
#[derive(Debug, Clone, Constructor)]
pub struct Func<'vm> {
pub func: &'vm BFunc,
pub env: Rc<Env<'vm>>,
pub env: Env<'vm>,
pub compiled: Option<JITFunc<'vm>>,
}
@@ -54,10 +51,10 @@ impl<'vm> Func<'vm> {
pub fn call(&self, vm: &'vm VM<'_>, arg: Value<'vm>) -> Result<Value<'vm>> {
use Param::*;
let env = Rc::new(self.env.as_ref().clone());
let mut env = self.env.clone();
match self.func.param.clone() {
Ident(ident) => env.enter([(ident.into(), arg)].into_iter()),
Ident(ident) => env = env.enter([(ident.into(), arg)].into_iter()),
Formals {
formals,
ellipsis,
@@ -88,7 +85,7 @@ impl<'vm> Func<'vm> {
if let Some(alias) = alias {
new.push((alias.clone().into(), Value::AttrSet(arg)));
}
env.enter(new.into_iter());
env = env.enter(new.into_iter());
}
}

View File

@@ -466,7 +466,7 @@ pub struct Thunk<'vm> {
#[derive(Debug, IsVariant, Unwrap, Clone)]
pub enum _Thunk<'vm> {
Code(&'vm OpCodes, OnceCell<Rc<Env<'vm>>>),
Code(&'vm OpCodes, OnceCell<Env<'vm>>),
SuspendedFrom(*const Thunk<'vm>),
Value(Value<'vm>),
}
@@ -478,7 +478,7 @@ impl<'vm> Thunk<'vm> {
}
}
pub fn capture(&self, env: Rc<Env<'vm>>) {
pub fn capture(&self, env: Env<'vm>) {
if let _Thunk::Code(_, envcell) = &*self.thunk.borrow() {
envcell.get_or_init(|| env);
}