mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
refactor: 函数 strict 标志入 IR/编译/缓存链(零行为地基)
This commit is contained in:
@@ -26,6 +26,10 @@ impl Emitter {
|
||||
pub(crate) fn emit_class_with_binding(
|
||||
&self, class: &Class, ctx: &mut CompileCtx, binding_reg: Option<u32>,
|
||||
) -> Result<u32, String> {
|
||||
// 类定义体是严格模式代码:字段初始化/静态块/extends 表达式内嵌套函数以
|
||||
// 本 ctx 为父 ctx,类编译期间强制 strict(函数末尾恢复外层标志)。
|
||||
let saved_strict = ctx.is_strict;
|
||||
ctx.is_strict = true;
|
||||
let ctor_name = class.id.as_ref().map(|id| id.name.to_string());
|
||||
// 类声明由调用方提供外层绑定槽(binding_reg=Some),类表达式无外部槽
|
||||
// (None,类名是类体内独立 const 绑定)。真实名 cell 仅声明路径指向
|
||||
@@ -264,6 +268,7 @@ impl Emitter {
|
||||
is_derived,
|
||||
&field_value_exprs,
|
||||
&extra_upvalue_names,
|
||||
true,
|
||||
)?
|
||||
} else {
|
||||
let mut module = self.compile_function_body_with_field_hooks(
|
||||
@@ -277,6 +282,7 @@ impl Emitter {
|
||||
is_derived,
|
||||
&field_value_exprs,
|
||||
&extra_upvalue_names,
|
||||
true,
|
||||
)?;
|
||||
if is_derived {
|
||||
module.insts.clear();
|
||||
@@ -288,6 +294,8 @@ impl Emitter {
|
||||
module.insts.push(Inst::super_call_spread(Operand::Reg(0), &[0x8000_0000 | 1]));
|
||||
// 字段初始化直接重发:构造器 upvalue/内置槽引用须与首轮编译产物对齐。
|
||||
let mut field_ctx = CompileCtx::new();
|
||||
// 字段表达式运行于构造器帧,构造器恒 strict,字段 ctx 同步置位。
|
||||
field_ctx.is_strict = true;
|
||||
field_ctx.scopes.private_name_map =
|
||||
private_names.iter().map(|(n, id, _, _)| (n.clone(), *id)).collect();
|
||||
field_ctx.scopes.builtin_reg_map = module.builtin_reg_map.clone();
|
||||
@@ -427,6 +435,8 @@ impl Emitter {
|
||||
ctx.scopes.private_name_map = saved_private_names;
|
||||
ctx.scopes.private_element_kinds = saved_private_kinds;
|
||||
ctx.scopes.private_brand_id = saved_brand_id;
|
||||
// 类编译结束恢复外层 strict 标志:类在 sloppy 上下文时不得泄漏 strict 到类后代码。
|
||||
ctx.is_strict = saved_strict;
|
||||
Ok(ctor_reg)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,6 +128,7 @@ impl Emitter {
|
||||
&extra_uv,
|
||||
method_value.generator,
|
||||
method_value.r#async,
|
||||
true,
|
||||
)?;
|
||||
ctx.in_instance_method = saved_instance;
|
||||
ctx.in_static_method = saved_static;
|
||||
|
||||
@@ -172,6 +172,9 @@ pub struct CompileCtx {
|
||||
pub(crate) is_generator: bool,
|
||||
/// 本函数是否为异步函数体(`async function` / async 箭头),`assemble_ir` 回写到 IR。
|
||||
pub(crate) is_async: bool,
|
||||
/// 本函数是否为严格模式函数体:自身 directive ‖ 外层严格继承(编译入口设置 +
|
||||
/// 嵌套继承,见 compile_function_body_with_field_hooks_gen),`assemble_ir` 回写到 IR。
|
||||
pub(crate) is_strict: bool,
|
||||
/// 是否为脚本顶层模块(emit_program 的根上下文)。顶层 var/function 声明需
|
||||
/// 同步写全局对象属性(脚本环境记录的 var 可经 globalThis 反射);函数体为 false。
|
||||
pub(crate) is_global_scope: bool,
|
||||
@@ -306,6 +309,7 @@ impl CompileCtx {
|
||||
in_static_method: false,
|
||||
is_generator: false,
|
||||
is_async: false,
|
||||
is_strict: false,
|
||||
is_global_scope: false,
|
||||
static_block_this_reg: None,
|
||||
field_buffer: None,
|
||||
@@ -701,6 +705,7 @@ impl CompileCtx {
|
||||
needs_home_object: false,
|
||||
is_generator: self.is_generator,
|
||||
is_async: self.is_async,
|
||||
is_strict: self.is_strict,
|
||||
captured_this_const_idx: 0,
|
||||
function_name: None,
|
||||
function_length: self.function_length,
|
||||
@@ -874,7 +879,7 @@ impl Emitter {
|
||||
/// 继承外层 super,普通函数重置 super 作用域。
|
||||
pub(crate) fn compile_function_body<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, is_arrow: bool,
|
||||
is_expression_body: bool, is_arrow: bool, own_strict: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_flags(
|
||||
param_specs,
|
||||
@@ -884,30 +889,40 @@ impl Emitter {
|
||||
is_arrow,
|
||||
false,
|
||||
false,
|
||||
own_strict,
|
||||
)
|
||||
}
|
||||
|
||||
/// 编译函数体并显式指定生成器标志(`function*` 走此入口)。
|
||||
pub(crate) fn compile_generator_body<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, own_strict: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_flags(param_specs, body_stmts, parent_ctx, false, false, true, false)
|
||||
self.compile_function_body_with_flags(
|
||||
param_specs,
|
||||
body_stmts,
|
||||
parent_ctx,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
own_strict,
|
||||
)
|
||||
}
|
||||
|
||||
/// 编译异步生成器函数体(`async function*` / async 生成器表达式走此入口):
|
||||
/// 同时标记 `is_generator` 与 `is_async`,VM 调用时按异步生成器协议执行
|
||||
/// (next 返回 Promise,yield 挂起与 await 挂起共存)。
|
||||
pub(crate) fn compile_async_generator_body<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx, own_strict: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_flags(param_specs, body_stmts, parent_ctx, false, false, true, true)
|
||||
self.compile_function_body_with_flags(param_specs, body_stmts, parent_ctx, false, false, true, true, own_strict)
|
||||
}
|
||||
|
||||
/// 编译异步函数体(`async function` / async 箭头走此入口):`is_async` 使
|
||||
/// `assemble_ir` 标记模块,VM 调用时按异步函数协议执行。
|
||||
pub(crate) fn compile_async_body<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, is_arrow: bool,
|
||||
is_expression_body: bool, is_arrow: bool, own_strict: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_flags(
|
||||
param_specs,
|
||||
@@ -917,13 +932,14 @@ impl Emitter {
|
||||
is_arrow,
|
||||
false,
|
||||
true,
|
||||
own_strict,
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn compile_function_body_with_flags<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, is_arrow: bool, is_generator: bool, is_async: bool,
|
||||
is_expression_body: bool, is_arrow: bool, is_generator: bool, is_async: bool, own_strict: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
let body_context = if is_arrow {
|
||||
FunctionBodyContext::Arrow
|
||||
@@ -939,6 +955,7 @@ impl Emitter {
|
||||
body_context,
|
||||
is_generator,
|
||||
is_async,
|
||||
own_strict,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -946,7 +963,7 @@ impl Emitter {
|
||||
pub(crate) fn compile_function_body_with_bindings_gen<'a>(
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, extra_bindings: &[(&str, u32)], body_context: FunctionBodyContext,
|
||||
is_generator: bool, is_async: bool,
|
||||
is_generator: bool, is_async: bool, own_strict: bool,
|
||||
) -> Result<IRFunction, String> {
|
||||
self.compile_function_body_with_field_hooks_gen(
|
||||
param_specs,
|
||||
@@ -961,6 +978,7 @@ impl Emitter {
|
||||
&[],
|
||||
is_generator,
|
||||
is_async,
|
||||
own_strict,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -976,7 +994,7 @@ impl Emitter {
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, extra_bindings: &[(&str, u32)], body_context: FunctionBodyContext,
|
||||
emit_fields: Option<E>, fields_after_super: bool, extra_capture_exprs: &[&'a Expression<'a>],
|
||||
extra_upvalue_names: &[(&str, u8)],
|
||||
extra_upvalue_names: &[(&str, u8)], own_strict: bool,
|
||||
) -> Result<IRFunction, String>
|
||||
where
|
||||
E: FnMut(&Emitter, &mut CompileCtx) -> Result<(), String>,
|
||||
@@ -994,6 +1012,7 @@ impl Emitter {
|
||||
extra_upvalue_names,
|
||||
false,
|
||||
false,
|
||||
own_strict,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1002,7 +1021,7 @@ impl Emitter {
|
||||
&self, param_specs: &[ParamSpec<'a>], body_stmts: &[Statement<'a>], parent_ctx: &CompileCtx,
|
||||
is_expression_body: bool, extra_bindings: &[(&str, u32)], body_context: FunctionBodyContext,
|
||||
mut emit_fields: Option<E>, fields_after_super: bool, extra_capture_exprs: &[&'a Expression<'a>],
|
||||
extra_upvalue_names: &[(&str, u8)], is_generator: bool, is_async: bool,
|
||||
extra_upvalue_names: &[(&str, u8)], is_generator: bool, is_async: bool, own_strict: bool,
|
||||
) -> Result<IRFunction, String>
|
||||
where
|
||||
E: FnMut(&Emitter, &mut CompileCtx) -> Result<(), String>,
|
||||
@@ -1010,6 +1029,9 @@ impl Emitter {
|
||||
let mut ctx = CompileCtx::new();
|
||||
ctx.is_generator = is_generator;
|
||||
ctx.is_async = is_async;
|
||||
// 函数 strict = 自身 directive ‖ 外层严格(规范不可解除);
|
||||
// 类元素方法/构造器恒 strict(ClassBody 是严格模式代码)。
|
||||
ctx.is_strict = own_strict || matches!(body_context, FunctionBodyContext::ClassElement) || parent_ctx.is_strict;
|
||||
|
||||
// length = 第一个带默认值形参之前的形参数(解构默认与标识符默认同规则);
|
||||
// rest 参数不计入 length(以 0 结尾即止)。
|
||||
@@ -1475,6 +1497,8 @@ impl Emitter {
|
||||
let mut ctx = CompileCtx::new();
|
||||
// 脚本顶层:var/function 声明需落到全局对象,let/const/class 不进全局。
|
||||
ctx.is_global_scope = true;
|
||||
// 脚本顶层严格模式由源码 "use strict" directive 决定(嵌套函数经父 ctx 继承)。
|
||||
ctx.is_strict = program.has_use_strict_directive();
|
||||
self.predeclare_function_declarations(&program.body, &mut ctx);
|
||||
|
||||
// 预注册 builtin 引用(先于任何临时寄存器),builtin 槽不进入临时寄存器池。
|
||||
|
||||
@@ -40,9 +40,23 @@ impl Emitter {
|
||||
let is_expr_body = arrow.expression;
|
||||
|
||||
let mut sub_module = if arrow.r#async {
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, is_expr_body, true)?
|
||||
self.compile_async_body(
|
||||
¶m_names,
|
||||
body_stmts,
|
||||
ctx,
|
||||
is_expr_body,
|
||||
true,
|
||||
arrow.has_use_strict_directive(),
|
||||
)?
|
||||
} else {
|
||||
self.compile_function_body(¶m_names, body_stmts, ctx, is_expr_body, true)?
|
||||
self.compile_function_body(
|
||||
¶m_names,
|
||||
body_stmts,
|
||||
ctx,
|
||||
is_expr_body,
|
||||
true,
|
||||
arrow.has_use_strict_directive(),
|
||||
)?
|
||||
};
|
||||
sub_module.is_arrow = true;
|
||||
|
||||
@@ -83,14 +97,15 @@ impl Emitter {
|
||||
|
||||
let body_stmts: &[Statement] = if let Some(body) = &fe.body { &body.statements } else { &[] };
|
||||
|
||||
let own_strict = fe.has_use_strict_directive();
|
||||
let mut sub_module = if fe.generator && fe.r#async {
|
||||
self.compile_async_generator_body(¶m_names, body_stmts, ctx)?
|
||||
self.compile_async_generator_body(¶m_names, body_stmts, ctx, own_strict)?
|
||||
} else if fe.generator {
|
||||
self.compile_generator_body(¶m_names, body_stmts, ctx)?
|
||||
self.compile_generator_body(¶m_names, body_stmts, ctx, own_strict)?
|
||||
} else if fe.r#async {
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, false, false)?
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, false, false, own_strict)?
|
||||
} else {
|
||||
self.compile_function_body(¶m_names, body_stmts, ctx, false, false)?
|
||||
self.compile_function_body(¶m_names, body_stmts, ctx, false, false, own_strict)?
|
||||
};
|
||||
if let Some(id) = &fe.id {
|
||||
sub_module.function_name = Some(id.name.to_string());
|
||||
|
||||
@@ -136,6 +136,8 @@ impl Emitter {
|
||||
let mut ctx = CompileCtx::new();
|
||||
// 模块顶层 var/function 不写全局对象(模块作用域绑定)。
|
||||
ctx.is_global_scope = false;
|
||||
// ES module 顶层恒严格模式(模块代码是严格模式代码,嵌套函数经父 ctx 继承)。
|
||||
ctx.is_strict = true;
|
||||
let mut path_stack = vec![module_path.to_string()];
|
||||
self.emit_module_into_ctx(program, module_path, loader, &mut path_stack, &mut ctx)?;
|
||||
Ok(ctx.assemble_ir(ParamLayout { base: 0, count: 0 }, None))
|
||||
@@ -387,6 +389,8 @@ impl Emitter {
|
||||
})?;
|
||||
let mut ctx = CompileCtx::new();
|
||||
ctx.is_global_scope = false;
|
||||
// 依赖模块经 parse_module 解析,顶层恒严格模式。
|
||||
ctx.is_strict = true;
|
||||
self.emit_module_into_ctx(&program, &resolved.path, loader, path_stack, &mut ctx)?;
|
||||
Ok(ctx.assemble_ir(ParamLayout { base: 0, count: 0 }, None))
|
||||
}
|
||||
|
||||
@@ -43,14 +43,15 @@ impl Emitter {
|
||||
self.push_rest_param(&rest.rest.argument, &mut param_names)?;
|
||||
}
|
||||
let body_stmts: &[Statement] = if let Some(body) = &fd.body { &body.statements } else { &[] };
|
||||
let own_strict = fd.has_use_strict_directive();
|
||||
let mut sub_module = if fd.generator && fd.r#async {
|
||||
self.compile_async_generator_body(¶m_names, body_stmts, ctx)?
|
||||
self.compile_async_generator_body(¶m_names, body_stmts, ctx, own_strict)?
|
||||
} else if fd.generator {
|
||||
self.compile_generator_body(¶m_names, body_stmts, ctx)?
|
||||
self.compile_generator_body(¶m_names, body_stmts, ctx, own_strict)?
|
||||
} else if fd.r#async {
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, false, false)?
|
||||
self.compile_async_body(¶m_names, body_stmts, ctx, false, false, own_strict)?
|
||||
} else {
|
||||
self.compile_function_body(¶m_names, body_stmts, ctx, false, false)?
|
||||
self.compile_function_body(¶m_names, body_stmts, ctx, false, false, own_strict)?
|
||||
};
|
||||
sub_module.function_name = Some(name.clone());
|
||||
ctx.nested.push(sub_module);
|
||||
|
||||
Reference in New Issue
Block a user