New declosure impl

This commit is contained in:
UditDey
2026-09-13 10:31:13 +00:00
parent d026165e7a
commit 61c4971fa6
5 changed files with 217 additions and 15 deletions
+8 -7
View File
@@ -6,17 +6,17 @@ pub struct FlatFn {
pub arg_count: usize,
pub var_count: usize,
pub cpool: Vec<JsValue>,
pub captures: Vec<ClosureVar>,
pub code: Vec<Instr>,
}
pub fn flatten(bin: QuickJSBinary) -> (Vec<FlatFn>, Vec<Atom>) {
pub fn flatten(bin: QuickJSBinary) -> (Vec<FlatFn>, Vec<Vec<ClosureVar>>, Vec<Atom>) {
let mut fns = vec![];
walk(&mut fns, bin.root_mod.root_fn);
(fns, bin.atoms)
let mut captures = vec![];
walk(bin.root_mod.root_fn, &mut fns, &mut captures);
(fns, captures, bin.atoms)
}
fn walk(flat_fns: &mut Vec<FlatFn>, func: Function) -> usize {
fn walk(func: Function, flat_fns: &mut Vec<FlatFn>, captures: &mut Vec<Vec<ClosureVar>>) -> usize {
let id = flat_fns.len();
flat_fns.push(FlatFn {
@@ -24,15 +24,16 @@ fn walk(flat_fns: &mut Vec<FlatFn>, func: Function) -> usize {
arg_count: func.arg_count,
var_count: func.var_count,
cpool: vec![],
captures: func.closure_vars,
code: func.byte_code,
});
captures.push(func.closure_vars);
let cpool = func
.cpool
.into_iter()
.map(|c| match c {
JsValue::Function(child) => JsValue::FnById(walk(flat_fns, child)),
JsValue::Function(child) => JsValue::FnById(walk(child, flat_fns, captures)),
other => other,
})
.collect();