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:
@@ -5,7 +5,7 @@
|
||||
//! 支持提升语义。
|
||||
|
||||
use crate::{CompileCtx, Emitter};
|
||||
use oxide_parser::{BindingPattern, Expression, Statement, VariableDeclarationKind};
|
||||
use oxide_parser::{BindingPattern, Declaration, ExportDefaultDeclarationKind, Expression, Statement, VariableDeclarationKind};
|
||||
|
||||
impl Emitter {
|
||||
/// 在临时寄存器池之前分配 builtin 槽位。
|
||||
@@ -117,6 +117,20 @@ impl Emitter {
|
||||
self.pre_scan_builtin_expr(super_class, ctx);
|
||||
}
|
||||
}
|
||||
Statement::ExportNamedDeclaration(exp) => {
|
||||
if let Some(Declaration::VariableDeclaration(vd)) = &exp.declaration {
|
||||
for d in &vd.declarations {
|
||||
if let Some(init) = &d.init {
|
||||
self.pre_scan_builtin_expr(init, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::ExportDefaultDeclaration(exp) => {
|
||||
if let Some(e) = exp.declaration.as_expression() {
|
||||
self.pre_scan_builtin_expr(e, ctx);
|
||||
}
|
||||
}
|
||||
Statement::FunctionDeclaration(_) | Statement::BreakStatement(_) | Statement::ContinueStatement(_) => {}
|
||||
_ => {}
|
||||
}
|
||||
@@ -283,8 +297,15 @@ impl Emitter {
|
||||
|
||||
pub(crate) fn predeclare_function_declarations(&self, statements: &[Statement], ctx: &mut CompileCtx) {
|
||||
for statement in statements {
|
||||
let Statement::FunctionDeclaration(function) = statement else {
|
||||
continue;
|
||||
let function = match statement {
|
||||
Statement::FunctionDeclaration(f) => f,
|
||||
Statement::ExportNamedDeclaration(exp) => match &exp.declaration {
|
||||
Some(Declaration::FunctionDeclaration(f)) => f,
|
||||
_ => continue,
|
||||
},
|
||||
// export default function foo(){}:foo 绑定由 lexical predeclare 以
|
||||
// Const 预声明(emit 侧 emit_bind_target(Const) 消费预登记槽)。
|
||||
_ => continue,
|
||||
};
|
||||
let Some(identifier) = &function.id else {
|
||||
continue;
|
||||
@@ -361,6 +382,24 @@ impl Emitter {
|
||||
Statement::WithStatement(ws) => {
|
||||
self.predeclare_var_declarations(std::slice::from_ref(&ws.body), ctx);
|
||||
}
|
||||
Statement::ExportNamedDeclaration(exp) => {
|
||||
if let Some(Declaration::VariableDeclaration(decl)) = &exp.declaration {
|
||||
if !matches!(decl.kind, VariableDeclarationKind::Var) {
|
||||
continue;
|
||||
}
|
||||
for d in &decl.declarations {
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(
|
||||
bi.name.as_str(),
|
||||
reg,
|
||||
VariableDeclarationKind::Var,
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -400,6 +439,60 @@ impl Emitter {
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::ExportNamedDeclaration(exp) => {
|
||||
if let Some(decl) = &exp.declaration {
|
||||
match decl {
|
||||
Declaration::VariableDeclaration(vd) => {
|
||||
if matches!(vd.kind, VariableDeclarationKind::Var) {
|
||||
continue;
|
||||
}
|
||||
let is_const = matches!(vd.kind, VariableDeclarationKind::Const);
|
||||
for d in &vd.declarations {
|
||||
self.predeclare_lexical_pattern(&d.id, is_const, ctx);
|
||||
}
|
||||
}
|
||||
Declaration::ClassDeclaration(cd) => {
|
||||
if let Some(id) = &cd.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_predeclared(
|
||||
id.name.as_str(),
|
||||
reg,
|
||||
VariableDeclarationKind::Const,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::ExportDefaultDeclaration(exp) => {
|
||||
match &exp.declaration {
|
||||
ExportDefaultDeclarationKind::ClassDeclaration(cd) => {
|
||||
if let Some(id) = &cd.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_predeclared(
|
||||
id.name.as_str(),
|
||||
reg,
|
||||
VariableDeclarationKind::Const,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
ExportDefaultDeclarationKind::FunctionDeclaration(fd) => {
|
||||
if let Some(id) = &fd.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_predeclared(
|
||||
id.name.as_str(),
|
||||
reg,
|
||||
VariableDeclarationKind::Const,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user