feat: 实现 Object/Array 的 toString 与 valueOf

This commit is contained in:
爱特每羊队
2026-06-27 13:22:45 +08:00
parent 040e2ee6b1
commit fe2042864c
2 changed files with 18 additions and 0 deletions
+6
View File
@@ -342,6 +342,12 @@ pub fn array_join<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
NativeResult::Ok(result_val) NativeResult::Ok(result_val)
} }
pub fn array_to_string<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
// Array.prototype.toString() delegates to join with the default "," separator,
// ignoring its own arguments per spec.
array_join(vm, &[args[0]])
}
pub fn array_index_of<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult { pub fn array_index_of<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
builtins_debug!("Array.prototype.indexOf called with {} args", args.len()); builtins_debug!("Array.prototype.indexOf called with {} args", args.len());
let arr_ptr = array_ptr!(vm, args); let arr_ptr = array_ptr!(vm, args);
+12
View File
@@ -459,6 +459,18 @@ pub fn object_has_own<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
NativeResult::Ok(JsValue::bool(vm.get_own_property_slot(obj, key_si).is_some())) NativeResult::Ok(JsValue::bool(vm.get_own_property_slot(obj, key_si).is_some()))
} }
pub fn object_proto_value_of<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
// Object.prototype.valueOf returns the `this` object unchanged; OrdinaryToPrimitive
// then falls through to toString since the result is not primitive.
NativeResult::Ok(vm.reg(args[0]))
}
pub fn object_proto_to_string<H: VmHost>(vm: &mut H, _args: &[u8]) -> NativeResult {
// ponytail: minimal [[Class]] string — always "[object Object]". Type-specific
// tags ("[object Array]" etc.) and Symbol.toStringTag are a later refinement.
NativeResult::Ok(vm.new_string("[object Object]"))
}
pub fn object_proto_has_own_property<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult { pub fn object_proto_has_own_property<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
if args.len() < 2 { if args.len() < 2 {
return NativeResult::Ok(JsValue::bool(false)); return NativeResult::Ok(JsValue::bool(false));