feat(core): Phase 2 Metadata scope lift (no body ${})

Inject bound frontmatter keys into the entry env as ordinary variables.
Reject body ${env…}; bytecode entry prologue seeds the same locals.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-24 20:05:25 +08:00
1 parent 81a14f9b38
commit 46d78c6e15
10 files changed
+132 -23

No files matched your search

+31 -2
View File
@@ -79,7 +79,12 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
let path_buf = path.map(|p| p.to_path_buf());
let mut functions = Vec::new();
for i in 0..flat.len() {
functions.push(compile_function(i, &flat, path_buf.as_deref())?);
let meta = if i == entry {
Some(module.metadata.as_slice())
} else {
None
};
functions.push(compile_function(i, &flat, path_buf.as_deref(), meta)?);
}
let parents: Vec<Option<usize>> = flat.iter().map(|f| f.parent).collect();
@@ -175,7 +180,12 @@ struct FnCompiler<'a> {
stmt_span: Span,
}
fn compile_function(fn_id: usize, flat: &[FlatFun], path: Option<&Path>) -> Result<FnChunk> {
fn compile_function(
fn_id: usize,
flat: &[FlatFun],
path: Option<&Path>,
metadata: Option<&[(String, Value)]>,
) -> Result<FnChunk> {
let fun = &flat[fn_id];
let mut locals = HashMap::new();
for (i, p) in fun.params.iter().enumerate() {
@@ -193,6 +203,15 @@ fn compile_function(fn_id: usize, flat: &[FlatFun], path: Option<&Path>) -> Resu
}
}
}
// Phase 2: reserve locals for entry metadata keys before body compile.
if let Some(meta) = metadata {
for (k, _) in meta {
if !locals.contains_key(k) {
let s = locals.len() as u8;
locals.insert(k.clone(), s);
}
}
}
let mut c = FnCompiler {
path,
flat,
@@ -208,6 +227,16 @@ fn compile_function(fn_id: usize, flat: &[FlatFun], path: Option<&Path>) -> Resu
locals,
stmt_span: fun.span,
};
// Prologue: seed entry locals from bound metadata.
if let Some(meta) = metadata {
for (k, v) in meta {
let slot = *c.locals.get(k).expect("metadata local reserved");
let ci = c.add_const(v.clone());
c.emit(Op::Constant(ci));
c.emit(Op::SetLocal(slot));
c.emit(Op::Pop);
}
}
for stmt in &fun.body {
c.compile_stmt(stmt)?;
}