fix: 装箱 Symbol 强转恒返 Symbol 并修正迭代器 name

This commit is contained in:
2026-09-25 06:52:46 +00:00
parent 01195ffb32
commit ae330e2b34
4 changed files with 21 additions and 34 deletions
+4 -2
View File
@@ -137,12 +137,14 @@ pub fn string_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let v = oxide_runtime_api::to_string_for_string_constructor(v, vm);
match v {
Ok(s) => vm.new_string_owned(s),
Err(_) => {
Err(msg) => {
// ToString on an object may throw via toString/valueOf; propagate the original exception.
if let Some(exc) = vm.take_uncaught_value() {
return NativeResult::Err(exc);
}
return NativeResult::Err(crate::error::create_type_error(vm, "Cannot convert value to a string"));
// 无在途异常时按格式化文本恢复 kind 与消息(装箱 Symbol 走 ToString
// 抛 TypeError,消息须与原始异常一致)。
return NativeResult::Err(crate::error::create_from_text(vm, &msg));
}
}
} else {
+3 -26
View File
@@ -95,41 +95,18 @@ pub fn symbol_to_string<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
NativeResult::Ok(vm.new_string_owned(result))
}
/// `Symbol.prototype[Symbol.toPrimitive](hint)`:thisSymbolValue 校验后按
/// hint 返回 Symbol 或其描述串。
/// `Symbol.prototype[Symbol.toPrimitive](hint)`:thisSymbolValue 校验后忽略
/// hint,恒返回 Symbol 本体(规范 ThisSymbolValue,不转描述串、不触发 hint 转换)。
///
/// # 步骤
/// 1. this 非 Symbol 或 Symbol 包装对象 → TypeError。
/// 2. hint 缺省 → 返回 Symbol。
/// 3. hint 做完整 ToString(异常传播);结果串为 "string" → 返回
/// `Symbol(description)` 描述串,其余值(含 "number")→ 返回 Symbol。
/// 2. 返回 Symbol(hint 缺省与否均忽略)。
pub fn symbol_to_primitive<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let this_val = vm.reg(args[0]);
let sym = match this_symbol_value(vm, this_val) {
Ok(v) => v,
Err(e) => return NativeResult::Err(e),
};
if args.len() <= 1 {
return NativeResult::Ok(sym);
}
let hint = vm.reg(args[1]);
if hint.is_undefined() {
return NativeResult::Ok(sym);
}
let hint_str = match oxide_runtime_api::to_string_full(hint, vm) {
Ok(s) => s,
Err(_) => {
if let Some(exc) = vm.take_uncaught_value() {
return NativeResult::Err(exc);
}
return NativeResult::Err(crate::error::create_type_error(vm, "Cannot convert value to a string"));
}
};
if hint_str == "string" {
let idx = sym.as_symbol_index();
let desc = vm.symbol_description(idx).unwrap_or("").to_string();
return NativeResult::Ok(vm.new_string_owned(format!("Symbol({})", desc)));
}
NativeResult::Ok(sym)
}