Skip to content
Merged
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
25 changes: 25 additions & 0 deletions crates/perry-runtime/src/object/native_call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,31 @@ pub unsafe extern "C" fn js_native_call_method(
}
}

// Exotic receivers (RegExp / Date / Error) with a user-assigned own
// property that is a callable: `var r = /x/; r.f = function(){...}; r.f()`
// and `String.prototype.toLowerCase.call`-style borrows like
// `reg.toLowerCase = String.prototype.toLowerCase; reg.toLowerCase()`
// (test262 String/prototype/{toLowerCase,toUpperCase,...}/*_A1_T14). These
// objects store dynamic props in the exotic-expando side table, not the
// ObjectHeader field map, so the field/vtable/prototype dispatch above
// never sees them. Look the name up there; if it is a callable, invoke it
// with the receiver bound as `this` (via IMPLICIT_THIS, matching the
// closure-field dispatch path above).
if jsval.is_pointer() {
if let Some((addr, kind)) = super::exotic_expando::exotic_expando_kind_of_value(object) {
if let Some(bits) = super::exotic_expando::value_lookup(kind, addr, method_name) {
Comment on lines +1559 to +1561

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

Don’t gate the expando lookup on jsval.is_pointer().

Line 1559 makes the helper’s raw-I64 branch unreachable here, so any exotic receiver encoded that way still falls through to TypeError despite exotic_expando_kind_of_value explicitly supporting it.

Proposed fix
-    if jsval.is_pointer() {
-        if let Some((addr, kind)) = super::exotic_expando::exotic_expando_kind_of_value(object) {
+    if let Some((addr, kind)) = super::exotic_expando::exotic_expando_kind_of_value(object) {
             if let Some(bits) = super::exotic_expando::value_lookup(kind, addr, method_name) {
                 let candidate = f64::from_bits(bits);
                 if crate::collection_iter::is_callable(candidate) {
                     let prev_this = IMPLICIT_THIS.with(|c| c.replace(object.to_bits()));
                     let result =
                         crate::closure::js_native_call_value(candidate, args_ptr, args_len);
                     IMPLICIT_THIS.with(|c| c.set(prev_this));
                     return result;
                 }
             }
-        }
     }
📝 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
if jsval.is_pointer() {
if let Some((addr, kind)) = super::exotic_expando::exotic_expando_kind_of_value(object) {
if let Some(bits) = super::exotic_expando::value_lookup(kind, addr, method_name) {
if let Some((addr, kind)) = super::exotic_expando::exotic_expando_kind_of_value(object) {
if let Some(bits) = super::exotic_expando::value_lookup(kind, addr, method_name) {
🤖 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/native_call_method.rs` around lines 1559 -
1561, The expando lookup in native_call_method::invoke is incorrectly gated by
jsval.is_pointer(), which prevents the raw-I64 exotic receiver path from ever
reaching exotic_expando_kind_of_value. Remove that pointer-only guard and let
the existing exotic_expando_kind_of_value(object) and value_lookup(kind, addr,
method_name) flow run for all eligible receivers so raw-I64 encoded exotic
values can resolve before falling back to TypeError.

let candidate = f64::from_bits(bits);
if crate::collection_iter::is_callable(candidate) {
let prev_this = IMPLICIT_THIS.with(|c| c.replace(object.to_bits()));
let result =
crate::closure::js_native_call_value(candidate, args_ptr, args_len);
IMPLICIT_THIS.with(|c| c.set(prev_this));
return result;
Comment on lines +1562 to +1568

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 exact callable check before returning.

collection_iter::is_callable is too broad for arbitrary expando values: it treats pointer-valued values as callable. If reg.f is a string/object, reg.f() can return from this block instead of reaching the required TypeError.

Proposed direction
                 let candidate = f64::from_bits(bits);
-                if crate::collection_iter::is_callable(candidate) {
+                let raw = (crate::value::js_nanbox_get_pointer(candidate) & !0x7) as usize;
+                let callable = (crate::value::addr_class::is_above_handle_band(raw)
+                    && crate::closure::is_closure_ptr(raw))
+                    || crate::proxy::js_proxy_is_proxy(candidate) == 1;
+                if callable {
                     let prev_this = IMPLICIT_THIS.with(|c| c.replace(object.to_bits()));
                     let result =
                         crate::closure::js_native_call_value(candidate, args_ptr, args_len);
📝 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
let candidate = f64::from_bits(bits);
if crate::collection_iter::is_callable(candidate) {
let prev_this = IMPLICIT_THIS.with(|c| c.replace(object.to_bits()));
let result =
crate::closure::js_native_call_value(candidate, args_ptr, args_len);
IMPLICIT_THIS.with(|c| c.set(prev_this));
return result;
let candidate = f64::from_bits(bits);
let raw = (crate::value::js_nanbox_get_pointer(candidate) & !0x7) as usize;
let callable = (crate::value::addr_class::is_above_handle_band(raw)
&& crate::closure::is_closure_ptr(raw))
|| crate::proxy::js_proxy_is_proxy(candidate) == 1;
if callable {
let prev_this = IMPLICIT_THIS.with(|c| c.replace(object.to_bits()));
let result =
crate::closure::js_native_call_value(candidate, args_ptr, args_len);
IMPLICIT_THIS.with(|c| c.set(prev_this));
return result;
🤖 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/native_call_method.rs` around lines 1562 -
1568, The callable path in native_call_method::native_call_method is using
collection_iter::is_callable, which is too permissive for expando values and can
incorrectly treat non-functions as callable. Replace that guard with an exact
callable check on the retrieved value before invoking
crate::closure::js_native_call_value, so only real function values can enter
this block and non-callables still fall through to the TypeError path.

}
}
}
}

crate::object::class_registry::report_dispatch_miss(
"call-method (no method/field/proto match)",
object,
Expand Down
Loading