From c710a2124036d878dc94ee6d020bd991c6732bb5 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 29 Jun 2026 06:50:01 +0000 Subject: [PATCH] fix(runtime): reduce test262 built-ins/Object failures (#5588) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three focused fixes targeting the 48-failure snapshot in issue #5588: 1. **NaN/Infinity/undefined as own globalThis properties** (populate.rs) ECMA-262 §19.1–19.3 requires NaN, Infinity, and undefined to be own data properties of the global object with {writable:false, enumerable:false, configurable:false}. They were absent from populate_global_this_builtins so Object.getOwnPropertyDescriptor(this, "NaN") returned undefined and Object.getOwnPropertyNames omitted them. Fixes: 15.2.3.3-4-178/179/180, 15.2.3.4-4-1 (4 tests). 2. **setPrototypeOf: guard second Floyd-hare advance past null** (define_properties.rs) The tortoise-and-hare cycle detector called advance(advance(hare)). When the first advance reached null (the end of a plain object's prototype chain), the second advance called js_object_get_prototype_of with null, which throws "Cannot convert undefined or null to object" and aborted legitimate setPrototypeOf calls via longjmp. Fixes: setPrototypeOf/success.js (1 test). 3. **Object.assign: skip sparse array holes** (alloc.rs) js_object_assign_one iterated 0..length for array sources without checking whether the index was actually present. Sparse-array holes (absent indices) are not own enumerable properties and must be skipped per ECMA-262 §20.2.2.2. Fixes: assign/target-Array.js (1 test). Before: 48 failing (snapshot 2026-06-29, perry 0.5.1208) After: ~42 failing (6 tests addressed) --- crates/perry-runtime/src/object/alloc.rs | 8 ++++-- .../src/object/global_this/populate.rs | 26 +++++++++++++++++++ .../object/object_ops/define_properties.rs | 15 +++++++++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/crates/perry-runtime/src/object/alloc.rs b/crates/perry-runtime/src/object/alloc.rs index db3dc7ee188..946bfff2905 100644 --- a/crates/perry-runtime/src/object/alloc.rs +++ b/crates/perry-runtime/src/object/alloc.rs @@ -1205,8 +1205,12 @@ pub unsafe extern "C" fn js_object_assign_one(target_f64: f64, source_f64: f64) }) .collect(); for i in 0..n { - // js_array_get yields `undefined` for a hole; array literals and - // pushed arrays are dense, matching Node for the common case. + // Holes (absent indices) in a sparse array are NOT own enumerable + // properties and must be skipped — Object.assign only copies own + // enumerable properties (test262 assign/target-Array.js). + if !crate::array::array_spec_has_index(arr, i) { + continue; + } let value = crate::array::js_array_get(arr, i); let key = i.to_string(); let key_ptr = crate::string::js_string_from_bytes(key.as_ptr(), key.len() as u32); diff --git a/crates/perry-runtime/src/object/global_this/populate.rs b/crates/perry-runtime/src/object/global_this/populate.rs index 89945f72bfb..d8539687b3a 100644 --- a/crates/perry-runtime/src/object/global_this/populate.rs +++ b/crates/perry-runtime/src/object/global_this/populate.rs @@ -601,6 +601,32 @@ pub(crate) fn populate_global_this_builtins(singleton: *mut ObjectHeader) { crate::navigator::navigator_object_with_constructor(f64::from_bits(nav_ctor.bits())); js_object_set_field_by_name(singleton, nkey, nval); } + // ECMA-262 19.1/19.2/19.3: NaN, Infinity, and undefined are own data + // properties of the global object with {writable:false, enumerable:false, + // configurable:false}. Install them so that + // `Object.getOwnPropertyDescriptor(globalThis, "NaN")` returns a real + // descriptor (test262 15.2.3.3-4-178/179/180) and + // `Object.getOwnPropertyNames(globalThis)` includes them (15.2.3.4-4-1). + { + let non_writable = super::super::PropertyAttrs::new(false, false, false); + for (name, value) in [("NaN", f64::NAN), ("Infinity", f64::INFINITY)] { + let key = crate::string::js_string_from_bytes(name.as_ptr(), name.len() as u32); + js_object_set_field_by_name(singleton, key, value); + super::super::set_builtin_property_attrs( + singleton as usize, + name.to_string(), + non_writable, + ); + } + let undef_key = crate::string::js_string_from_bytes(b"undefined".as_ptr(), 9); + let undef_val = f64::from_bits(crate::value::TAG_UNDEFINED); + js_object_set_field_by_name(singleton, undef_key, undef_val); + super::super::set_builtin_property_attrs( + singleton as usize, + "undefined".to_string(), + super::super::PropertyAttrs::new(false, false, false), + ); + } } /// Re-point a `Number.` static at the global function of the same name so diff --git a/crates/perry-runtime/src/object/object_ops/define_properties.rs b/crates/perry-runtime/src/object/object_ops/define_properties.rs index 974b64fced1..96e4b071f33 100644 --- a/crates/perry-runtime/src/object/object_ops/define_properties.rs +++ b/crates/perry-runtime/src/object/object_ops/define_properties.rs @@ -227,9 +227,20 @@ pub extern "C" fn js_object_set_prototype_of(obj_value: f64, proto: f64) -> f64 if tortoise == TAG_NULL_U64 { break; } - // Advance tortoise one step, hare two steps. + // Advance tortoise one step, hare two steps. Guard the second + // advance: if the first step lands on null, calling advance(null) + // would invoke js_object_get_prototype_of(null) which throws + // "Cannot convert undefined or null to object" (test262 + // setPrototypeOf/success.js — plain object proto chain ends at null). tortoise = advance(tortoise); - hare = advance(advance(hare)); + hare = { + let h1 = advance(hare); + if h1 == TAG_NULL_U64 { + TAG_NULL_U64 + } else { + advance(h1) + } + }; // If they meet, the existing chain already has a cycle — the walk // will never reach null, so we also can never form a new one by // setting obj's proto. Just break; the set is safe.