fix(vm): 函数 name 属性与 harness 重复 Test262Error 定义

This commit is contained in:
爱特每羊队
2026-08-07 12:22:59 +08:00
parent be9b96dfae
commit c8979660cb
3 changed files with 12 additions and 14 deletions
@@ -24,9 +24,11 @@ impl Emitter {
let val_reg = self.emit_expression(init, ctx)?; let val_reg = self.emit_expression(init, ctx)?;
self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?; self.emit_binding_pattern(&d.id, val_reg, decl.kind, is_const, ctx)?;
if let BindingPattern::BindingIdentifier(bi) = &d.id { if let BindingPattern::BindingIdentifier(bi) = &d.id {
if matches!(*init, Expression::ArrowFunctionExpression(_)) { if matches!(*init, Expression::ArrowFunctionExpression(_) | Expression::FunctionExpression(_)) {
if let Some(sub_mod) = ctx.nested.last_mut() { if let Some(sub_mod) = ctx.nested.last_mut() {
sub_mod.function_name = Some(bi.name.to_string()); if sub_mod.function_name.is_none() {
sub_mod.function_name = Some(bi.name.to_string());
}
} }
} }
} }
+2 -12
View File
@@ -246,17 +246,6 @@ fn is_blacklisted_harness(name: &str) -> bool {
} }
/// 生成 `Test262Error` 的 JS prelude(供测试脚本 `assert` 失败时抛出)。 /// 生成 `Test262Error` 的 JS prelude(供测试脚本 `assert` 失败时抛出)。
fn test262_error_prelude() -> &'static str {
r#"
function Test262Error(message) {
this.message = message;
this.name = "Test262Error";
}
Test262Error.prototype = new Error();
Test262Error.prototype.constructor = Test262Error;
"#
}
/// 向拼接源码追加一段带注释标记的 harness 代码块。 /// 向拼接源码追加一段带注释标记的 harness 代码块。
fn append_source_chunk(out: &mut String, name: &str, source: &str) { fn append_source_chunk(out: &mut String, name: &str, source: &str) {
out.push_str("\n// ---- test262 harness: "); out.push_str("\n// ---- test262 harness: ");
@@ -282,7 +271,8 @@ fn build_harness_source(meta: &TestMeta, harness: &HarnessSources) -> Result<Str
if meta.flags.iter().any(|f| f == "onlyStrict") { if meta.flags.iter().any(|f| f == "onlyStrict") {
append_source_chunk(&mut source, "use strict", "\"use strict\";"); append_source_chunk(&mut source, "use strict", "\"use strict\";");
} }
append_source_chunk(&mut source, "Test262Error prelude", test262_error_prelude()); // Test262Error 由 sta.js 提供;此处不再重复定义(重复函数声明会引发
// 引擎 prototype 语义错乱,导致 assert.throws 的 constructor 比对失败)。
append_source_chunk( append_source_chunk(
&mut source, &mut source,
"sta.js", "sta.js",
+6
View File
@@ -102,6 +102,7 @@ impl Vm {
let is_derived_constructor = sub.is_derived_constructor; let is_derived_constructor = sub.is_derived_constructor;
let needs_home_object = sub.needs_home_object; let needs_home_object = sub.needs_home_object;
let upvalue_captures = sub.upvalue_captures.clone(); let upvalue_captures = sub.upvalue_captures.clone();
let function_name = sub.function_name.clone();
let result = self.create_function_object( let result = self.create_function_object(
sub_idx, sub_idx,
is_arrow, is_arrow,
@@ -109,6 +110,11 @@ impl Vm {
is_derived_constructor, is_derived_constructor,
needs_home_object, needs_home_object,
); );
// 函数名推断:emit 端在变量声明/对象属性赋值点设置 function_name。
let func_obj = unsafe { &mut *result.as_js_object_ptr() };
let name_si = self.kernel_core.perm_interner().intern("name").0;
let name_val = function_name.as_deref().map(|n| self.new_string(n)).unwrap_or_else(|| self.new_string(""));
self.set_or_create_prop_value(func_obj, name_si, name_val);
if !upvalue_captures.is_empty() { if !upvalue_captures.is_empty() {
if let Some(current_cells) = self.cell_stack.last() { if let Some(current_cells) = self.cell_stack.last() {
let mut upvals = Box::new(Vec::with_capacity(upvalue_captures.len())); let mut upvals = Box::new(Vec::with_capacity(upvalue_captures.len()));