Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/perry-runtime/src/object/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +1208 to +1213

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Use an own-index check here, not array_spec_has_index.

array_spec_has_index also returns true for inherited numeric properties on a custom prototype, Array.prototype, or Object.prototype. Object.assign must copy only own enumerable properties, so this will incorrectly copy prototype-filled holes from sparse arrays. Please switch this guard to an own-index predicate instead of the full array lookup helper.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry-runtime/src/object/alloc.rs` around lines 1208 - 1213, The
sparse-array guard in the Object.assign path is using array_spec_has_index,
which can match inherited numeric properties and wrongly copy prototype-filled
holes. Update the check in the alloc logic to use an own-index predicate in the
Object.assign loop so only own enumerable indices are copied, and keep the
surrounding sparse-array skip behavior unchanged.

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);
Expand Down
26 changes: 26 additions & 0 deletions crates/perry-runtime/src/object/global_this/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.<name>` static at the global function of the same name so
Expand Down
15 changes: 13 additions & 2 deletions crates/perry-runtime/src/object/object_ops/define_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
};
Comment on lines +230 to +243

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Re-check tortoise after advancing before treating a cycle as safe.

If proto already sits in a cyclic chain that also reaches obj, tortoise can land on obj_bits on Lines 235-243 and then immediately satisfy the hare == tortoise break on Line 247. That exits as “safe” before the next iteration re-runs the tortoise == obj_bits check, so Object.setPrototypeOf can miss a real Cyclic __proto__ value case.

Proposed fix
             tortoise = advance(tortoise);
             hare = {
                 let h1 = advance(hare);
                 if h1 == TAG_NULL_U64 {
                     TAG_NULL_U64
                 } else {
                     advance(h1)
                 }
             };
+            if tortoise == obj_bits {
+                throw_object_type_error(b"Cyclic __proto__ value");
+            }
             // 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.
             if hare == tortoise {
                 break;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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)
}
};
// 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 = {
let h1 = advance(hare);
if h1 == TAG_NULL_U64 {
TAG_NULL_U64
} else {
advance(h1)
}
};
if tortoise == obj_bits {
throw_object_type_error(b"Cyclic __proto__ value");
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry-runtime/src/object/object_ops/define_properties.rs` around lines
230 - 243, The cycle check in the prototype-walk inside `define_properties.rs`
can incorrectly treat a cycle as safe if `tortoise` advances onto `obj_bits` and
then immediately matches `hare` before the next loop iteration re-checks it.
Update the Floyd-style loop around `advance`, `tortoise`, and `hare` so that
after each `tortoise = advance(tortoise)` you explicitly verify whether
`tortoise == obj_bits` before allowing the `hare == tortoise` break to succeed;
this keeps `Object.setPrototypeOf` from missing the `Cyclic __proto__ value`
case.

// 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.
Expand Down
Loading