feat: emit 模块编译(快照式链接:依赖子模块闭包 + import/export 域)

- emit_program_module:模块顶层编译(is_global_scope=false,顶层 this undefined 由 VM 按标志绑定)
- emit_module_into_ctx:预声明/闭包分析识别 export 包裹声明与 import 绑定
- 依赖收集:import/re-export/star 的 source 去重、循环检测、json/text 数据模块
- 导入绑定初始化:__moduleLinkGet 链接期检查;self-import 别名语义(export 处回写)
- export 域:声明/重导出/export */export default 就地注册命名空间
- re-export 链接检查移至 prelude(先于 body 求值)
- emit_module_call 参数打包连续寄存器(CALL_NATIVE 契约)
- CompileCtx 增 module_ns_reg/module_dep_ns_regs/self-import 映射
- 拆分 declaration helper(export 声明复用);function/class 表达式 emitter 提升可见性
This commit is contained in:
爱特每羊队
2026-08-12 17:37:15 +08:00
parent 3f45f38f51
commit 6da248872b
11 changed files with 806 additions and 8 deletions
+106
View File
@@ -86,6 +86,60 @@ impl Emitter {
out.insert(id.name.to_string());
}
}
// import 绑定是模块作用域 const 绑定,须计入 own_bindings 供
// 嵌套函数 cell 捕获。
Statement::ImportDeclaration(imp) => {
if let Some(specifiers) = &imp.specifiers {
for sp in specifiers {
match sp {
oxide_parser::ImportDeclarationSpecifier::ImportSpecifier(s) => {
out.insert(s.local.name.to_string());
}
oxide_parser::ImportDeclarationSpecifier::ImportDefaultSpecifier(s) => {
out.insert(s.local.name.to_string());
}
oxide_parser::ImportDeclarationSpecifier::ImportNamespaceSpecifier(s) => {
out.insert(s.local.name.to_string());
}
}
}
}
}
Statement::ExportNamedDeclaration(exp) => {
if let Some(decl) = &exp.declaration {
match decl {
oxide_parser::Declaration::VariableDeclaration(vd) => {
for d in &vd.declarations {
self.collect_binding_pattern_names(&d.id, out);
}
}
oxide_parser::Declaration::FunctionDeclaration(fd) => {
if let Some(id) = &fd.id {
out.insert(id.name.to_string());
}
}
oxide_parser::Declaration::ClassDeclaration(cd) => {
if let Some(id) = &cd.id {
out.insert(id.name.to_string());
}
}
_ => {}
}
}
}
Statement::ExportDefaultDeclaration(exp) => match &exp.declaration {
oxide_parser::ExportDefaultDeclarationKind::FunctionDeclaration(fd) => {
if let Some(id) = &fd.id {
out.insert(id.name.to_string());
}
}
oxide_parser::ExportDefaultDeclarationKind::ClassDeclaration(cd) => {
if let Some(id) = &cd.id {
out.insert(id.name.to_string());
}
}
_ => {}
},
Statement::BlockStatement(b) => self.collect_decl_names_stmt(&b.body, out),
Statement::IfStatement(is) => {
self.collect_decl_names_stmt(std::slice::from_ref(&is.consequent), out);
@@ -259,6 +313,58 @@ impl Emitter {
}
}
}
Statement::ExportNamedDeclaration(exp) => {
if let Some(decl) = &exp.declaration {
match decl {
oxide_parser::Declaration::VariableDeclaration(vd) => {
for d in &vd.declarations {
if let Some(init) = &d.init {
self.collect_capture_names_expr(init, ref_set, shadow, out);
}
}
}
oxide_parser::Declaration::FunctionDeclaration(fd) => {
let body: &[Statement] =
fd.body.as_ref().map(|b| &b.statements[..]).unwrap_or(&[]);
let mut inner = shadow.clone();
inner.extend(self.collect_fn_param_names(&fd.params));
inner.extend(self.collect_own_binding_names(&[], body));
self.collect_fn_default_names(&fd.params, ref_set, shadow, out);
self.collect_capture_names_shadowed(body, ref_set, &inner, out);
}
oxide_parser::Declaration::ClassDeclaration(cd) => {
let mut class_shadow = shadow.clone();
if let Some(id) = &cd.id {
class_shadow.insert(id.name.as_str().to_string());
}
self.collect_class_capture_names(&cd.body, ref_set, &class_shadow, out);
}
_ => {}
}
}
}
Statement::ExportDefaultDeclaration(exp) => match &exp.declaration {
oxide_parser::ExportDefaultDeclarationKind::FunctionDeclaration(fd) => {
let body: &[Statement] = fd.body.as_ref().map(|b| &b.statements[..]).unwrap_or(&[]);
let mut inner = shadow.clone();
inner.extend(self.collect_fn_param_names(&fd.params));
inner.extend(self.collect_own_binding_names(&[], body));
self.collect_fn_default_names(&fd.params, ref_set, shadow, out);
self.collect_capture_names_shadowed(body, ref_set, &inner, out);
}
oxide_parser::ExportDefaultDeclarationKind::ClassDeclaration(cd) => {
let mut class_shadow = shadow.clone();
if let Some(id) = &cd.id {
class_shadow.insert(id.name.as_str().to_string());
}
self.collect_class_capture_names(&cd.body, ref_set, &class_shadow, out);
}
other => {
if let Some(e) = other.as_expression() {
self.collect_capture_names_expr(e, ref_set, shadow, out);
}
}
},
Statement::ThrowStatement(ts) => self.collect_capture_names_expr(&ts.argument, ref_set, shadow, out),
Statement::SwitchStatement(sw) => {
self.collect_capture_names_expr(&sw.discriminant, ref_set, shadow, out);