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
@@ -10,6 +10,13 @@ impl Emitter {
let Statement::ClassDeclaration(class) = stmt else {
return Err("ClassDeclaration without name".into());
};
self.emit_class_declaration(class, ctx)
}
/// 类声明本体:供 export 声明复用(导出声明先按普通声明 emit,再就地注册导出值)。
pub(crate) fn emit_class_declaration(
&self, class: &oxide_parser::Class, ctx: &mut CompileCtx,
) -> Result<Option<u32>, String> {
let name = class
.id
.as_ref()
@@ -13,6 +13,13 @@ impl Emitter {
let Statement::FunctionDeclaration(fd) = stmt else {
return Err("FunctionDeclaration without name".into());
};
self.emit_function_declaration(fd, ctx)
}
/// 函数声明本体:供 export 声明复用(导出声明先按普通声明 emit,再就地注册导出值)。
pub(crate) fn emit_function_declaration(
&self, fd: &oxide_parser::Function, ctx: &mut CompileCtx,
) -> Result<Option<u32>, String> {
let name = if let Some(id) = &fd.id {
id.name.to_string()
} else {
@@ -5,7 +5,7 @@ use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
use oxide_parser::{BindingPattern, Statement, VariableDeclarationKind};
use oxide_parser::{BindingPattern, Statement, VariableDeclaration, VariableDeclarationKind};
impl Emitter {
pub(crate) fn emit_variable_declaration_statement(
@@ -14,6 +14,13 @@ impl Emitter {
let Statement::VariableDeclaration(decl) = stmt else {
return Ok(None);
};
self.emit_variable_declaration(decl, ctx)
}
/// 变量声明本体:供 export 声明复用(导出声明需先按普通声明 emit,再就地注册导出值)。
pub(crate) fn emit_variable_declaration(
&self, decl: &VariableDeclaration, ctx: &mut CompileCtx,
) -> Result<Option<u32>, String> {
let mut r = None;
for d in &decl.declarations {
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);