feat: less gc (WIP)

This commit is contained in:
2025-06-02 14:18:59 +08:00
parent d3442e87e7
commit 51f8df9cca
11 changed files with 125 additions and 179 deletions

View File

@@ -1,10 +1,12 @@
use std::rc::Rc;
use gc_arena::{Gc, Mutation};
use hashbrown::HashMap;
use crate::env::VmEnv;
use crate::ir::{DowngradeContext, Ir};
use crate::ty::common::Const;
use crate::ty::internal::{AttrSet, CoW, PrimOp, Value};
use crate::ty::internal::{AttrSet, PrimOp, Value};
use crate::vm::VM;
pub fn ir_env(ctx: &mut DowngradeContext) -> HashMap<usize, Ir> {
@@ -21,7 +23,7 @@ pub fn vm_env<'gc>(vm: &VM, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
let Ok([mut first, second]): Result<[Value; 2], _> = args.try_into() else {
unreachable!()
};
first.add(second, mc);
first.add(second);
first.ok()
}),
PrimOp::new("sub", 2, |args, _, mc| {
@@ -29,7 +31,7 @@ pub fn vm_env<'gc>(vm: &VM, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
unreachable!()
};
second.neg();
first.add(second, mc);
first.add(second);
first.ok()
}),
PrimOp::new("mul", 2, |args, _, _| {
@@ -75,15 +77,15 @@ pub fn vm_env<'gc>(vm: &VM, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
let mut map = HashMap::new();
for primop in primops {
let primop = Gc::new(mc, primop);
let primop = Rc::new(primop);
env_map.insert(
vm.new_sym(format!("__{}", primop.name)),
Value::PrimOp(primop),
Value::PrimOp(primop.clone()),
);
map.insert(vm.new_sym(primop.name), Value::PrimOp(primop));
}
let sym = vm.new_sym("builtins");
let mut attrs = CoW::new(AttrSet::new(map), mc);
/* let mut attrs = CoW::new(AttrSet::new(map), mc);
unsafe {
attrs.make_cyclic(|attrs, this| {
let _ = attrs.push_attr(sym, Value::AttrSet(this));
@@ -91,7 +93,7 @@ pub fn vm_env<'gc>(vm: &VM, mc: &Mutation<'gc>) -> Gc<'gc, VmEnv<'gc>> {
}
let builtins = Value::AttrSet(attrs);
env_map.insert(sym, builtins);
env_map.insert(sym, builtins); */
// TODO:
// VmEnv::new(Gc::new(mc, env_map), mc)
VmEnv::new(Vec::new(), mc)