feat: gc (does compile, but WIP)

This commit is contained in:
2025-05-27 21:08:59 +08:00
parent 319c12c1f4
commit c3ace28af1
20 changed files with 696 additions and 575 deletions

View File

@@ -1,9 +1,10 @@
use std::ops::Deref;
use std::rc::Rc;
use hashbrown::{HashMap, HashSet};
use derive_more::Constructor;
use itertools::Itertools;
use gc_arena::{Collect, Gc, Mutation};
use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use crate::env::VmEnv;
use crate::error::Result;
@@ -13,25 +14,25 @@ use super::super::public as p;
use super::Value;
#[repr(transparent)]
#[derive(Constructor, Clone, PartialEq)]
#[derive(Constructor, Clone, PartialEq, Collect)]
#[collect(no_drop)]
pub struct AttrSet<'gc> {
data: HashMap<usize, Value<'gc>>,
}
unsafe impl<'jit: 'vm, 'vm, 'gc> Collect for AttrSet<'gc> {
fn trace(&self, cc: &gc_arena::Collection) {
for (_, v) in self.data.iter() {
v.trace(cc);
}
}
}
impl<'gc> From<HashMap<usize, Value<'gc>>> for AttrSet<'gc> {
fn from(data: HashMap<usize, Value<'gc>>) -> Self {
Self { data }
}
}
impl<'gc> Deref for AttrSet<'gc> {
type Target = HashMap<usize, Value<'gc>>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
pub fn with_capacity(cap: usize) -> Self {
AttrSet {
@@ -61,7 +62,7 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
pub fn capture(&mut self, env: Gc<'gc, VmEnv<'gc>>, mc: &Mutation<'gc>) {
self.data.iter().for_each(|(_, v)| {
if let Value::Thunk(ref thunk) = v.clone() {
thunk.capture(env, mc);
thunk.capture_env(env, mc);
}
})
}
@@ -85,12 +86,13 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
}
pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> {
let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect();
todo!()
/* let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect();
for (_, v) in map.iter_mut() {
v.force_deep(vm, mc)?;
}
self.data = map.into_iter().collect();
Ok(())
Ok(()) */
}
pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool {
@@ -102,7 +104,7 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
.all(|((_, v1), (_, v2))| v1.eq_impl(v2))
}
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
pub fn to_public(&self, vm: &VM<'gc>, seen: &mut HashSet<Value<'gc>>) -> p::Value {
p::Value::AttrSet(p::AttrSet::new(
self.data
.iter()