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
20 changes: 20 additions & 0 deletions crates/perry-runtime/src/array/iter_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ unsafe fn collection_iter_obj_for_receiver(arr: *const ArrayHeader, kind: u8) ->
_ => crate::collection_iter_object::js_set_values_iter_obj(s),
});
}
// Native URLSearchParams (ordinary heap object, `_entries`-leading shape —
// NOT a fetch-band handle): the #597 any-typed fold lands here too, e.g.
// `const sp = new URL(u).searchParams; sp.entries()` where `sp`'s static
// type erased to Any. Reading the params object as an `ArrayHeader`
// yielded an EMPTY iterator. Materialize the requested view as an eager
// array and drive it with the standard array iterator object.
{
let obj = raw as *mut crate::object::ObjectHeader;
if crate::url::search_params::shape_is_url_search_params(obj) {
let boxed = match kind {
1 => crate::url::js_url_search_params_keys_arr(obj),
2 => crate::url::js_url_search_params_entries_arr(obj),
_ => crate::url::js_url_search_params_values_arr(obj),
};
let ptr = (boxed.to_bits() & 0x0000_FFFF_FFFF_FFFF) as *const ArrayHeader;
if !ptr.is_null() {
return Some(array_iter_obj_raw(ptr, KIND_VALUES));
}
}
}
// `class X extends Map|Set` instance — probe the hidden backing field via
// the reconstructed NaN-boxed pointer value.
let boxed = f64::from_bits(JSValue::pointer(arr as *const u8).bits());
Expand Down
13 changes: 12 additions & 1 deletion crates/perry-runtime/src/object/native_call_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,18 @@ pub unsafe extern "C" fn js_native_call_method(
// before the generic field-scan misses and throws "is not a function".
if matches!(
method_name,
"append" | "set" | "get" | "has" | "delete" | "toString"
"append"
| "set"
| "get"
| "has"
| "delete"
| "toString"
| "entries"
| "keys"
| "values"
| "getAll"
| "sort"
| "forEach"
Comment on lines +865 to +876

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 | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Locate the perry-hir `#597` Any-typed catch-all fold for keys/entries/values
# to confirm whether it intercepts a direct `.entries()`/`.keys()`/`.values()`
# call syntax before it reaches js_native_call_method.
rg -n -C5 'Array(Keys|Entries|Values)' --type=rust crates/perry-hir crates/perry-codegen 2>/dev/null
rg -n -C5 '`#597`' --type=rust

Repository: PerryTS/perry

Length of output: 41394


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the lowering logic that may intercept .entries()/.keys()/.values()
# before native_call_method.rs sees them.
sed -n '740,860p' crates/perry-hir/src/lower/expr_call/local_array_methods.rs
printf '\n---\n'
sed -n '780,840p' crates/perry-hir/src/lower/expr_call/array_only_methods.rs
printf '\n---\n'
sed -n '1500,1685p' crates/perry-hir/src/ir/expr.rs

# Inspect the new test case mentioned in the review.
printf '\n=== test file ===\n'
sed -n '1,260p' test-files/test_gap_url_searchparams_iter.ts

Repository: PerryTS/perry

Length of output: 20715


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show the full control flow around the entries/keys/values lowering branch.
nl -ba crates/perry-hir/src/lower/expr_call/local_array_methods.rs | sed -n '730,860p'

printf '\n=== any/class receiver detection ===\n'
rg -n -C3 'recv_is_class|lookup_local_type|Any-typed|`#597`|entries" \| "keys" \| "values"' crates/perry-hir/src/lower/expr_call/local_array_methods.rs

printf '\n=== call sites in the new test ===\n'
rg -n 'entries\(\)|keys\(\)|values\(\)|forEach\(' test-files/test_gap_url_searchparams_iter.ts

Repository: PerryTS/perry

Length of output: 191


Add a non-direct call case for entries/keys/values in test-files/test_gap_url_searchparams_iter.ts
erased.entries() / .keys() / .values() on an any receiver is lowered to Expr::ArrayEntries / ArrayKeys / ArrayValues, so this test still misses the new js_native_call_method dispatch arms; add a computed or bound-method call instead.

🤖 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 865 -
876, Add a computed or bound-method invocation covering entries, keys, and
values in test_gap_url_searchparams_iter.ts, using an any-typed receiver so the
calls exercise js_native_call_method dispatch rather than ArrayEntries,
ArrayKeys, or ArrayValues lowering. Preserve the existing direct-call coverage.

) {
let recv_ptr = (object.to_bits() & 0x0000_FFFF_FFFF_FFFF) as *mut ObjectHeader;
if crate::url::search_params::shape_is_url_search_params(recv_ptr) {
Expand Down
20 changes: 20 additions & 0 deletions crates/perry-runtime/src/symbol/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,26 @@ pub extern "C" fn js_get_iterator(val_f64: f64) -> f64 {
// drive `.next()`. Matches the builtins' default iterator. Skipped when the
// subclass overrides `[Symbol.iterator]`, so we fall through to the generic
// symbol lookup below (which resolves the user's `@@iterator` method).
//
// A URLSearchParams object (shape-detected: `_entries` + `_owner` fields)
// has no symbol-table `[Symbol.iterator]` entry — its default iterator
// yields `[key, value]` pairs (`%URLSearchParamsIteratorPrototype%`).
// Without this branch the generic lookup below finds nothing and returns
// the params object as its own "iterator"; the lazy for-of then calls
// `.next()` on it → "next is not a function" (mysql2's `parseUrl` does
// `for (const [key, value] of url.searchParams)`, so `createPool` with a
// `uri:` option died on this).
{
let jsv = crate::value::JSValue::from_bits(val_f64.to_bits());
if jsv.is_pointer() {
let obj =
jsv.as_pointer::<crate::object::ObjectHeader>() as *mut crate::object::ObjectHeader;
if crate::url::search_params::shape_is_url_search_params(obj) {
let entries = crate::url::js_url_search_params_entries_arr(obj);
return crate::array::array_values_iter(entries);
}
}
}
match crate::object::map_set_subclass::subclass_backing_for_default_iteration(val_f64) {
Some(crate::object::map_set_subclass::CollectionBacking::Map(m)) => {
return crate::value::js_nanbox_pointer(
Expand Down
37 changes: 35 additions & 2 deletions crates/perry-runtime/src/url/search_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -948,14 +948,25 @@ pub(crate) fn url_search_params_dynamic_call(
}
}
"has" => {
if js_url_search_params_has(params, arg(0)) != 0.0 {
// Node 19+: `has(name, value)` matches on BOTH when a second
// argument is present.
let hit = if args_len >= 2 && arg(1).to_bits() != crate::value::TAG_UNDEFINED {
js_url_search_params_has2(params, arg(0), arg(1)) != 0.0
} else {
js_url_search_params_has(params, arg(0)) != 0.0
};
if hit {
f64::from_bits(crate::value::TAG_TRUE)
} else {
f64::from_bits(crate::value::TAG_FALSE)
}
}
"delete" => {
js_url_search_params_delete(params, arg(0));
if args_len >= 2 && arg(1).to_bits() != crate::value::TAG_UNDEFINED {
js_url_search_params_delete2(params, arg(0), arg(1));
} else {
js_url_search_params_delete(params, arg(0));
}
undefined
}
"toString" => {
Expand All @@ -967,6 +978,28 @@ pub(crate) fn url_search_params_dynamic_call(
.join("&");
create_string_f64(&joined)
}
// Iteration helpers surface as eager arrays, matching the convention
// the static lowering uses (`js_get_iterator` wraps an eager array in
// the runtime array iterator when driven through for-of). mysql2's
// `parseUrl` iterates `url.searchParams` and Node code commonly calls
// `.entries()` / `.getAll()` on dynamic receivers.
"entries" => js_url_search_params_entries_arr(params),
"keys" => js_url_search_params_keys_arr(params),
"values" => js_url_search_params_values_arr(params),
"getAll" => {
// The FFI helper returns RAW pointer bits (the static lowering
// boxes them); re-box for the dynamic call path.
let raw = js_url_search_params_get_all(params, arg(0));
crate::value::js_nanbox_pointer(f64::to_bits(raw).cast_signed())
}
"sort" => {
js_url_search_params_sort(params);
undefined
}
"forEach" => {
js_url_search_params_for_each(params, arg(0), arg(1));
undefined
}
_ => return None,
})
}
Expand Down
42 changes: 42 additions & 0 deletions test-files/test_gap_url_searchparams_iter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// URLSearchParams iteration parity (mysql2 parseUrl pattern + dynamic surface).
// 1. for-of over a URL-adopted searchParams (default @@iterator → [key, value] pairs)
const u = new URL('mysql://user:pw@dbhost:3306/mydb?connectionLimit=5&x=1');
for (const [k, v] of u.searchParams) console.log('adopted', k, v);

// 2. for-of over an EMPTY adopted searchParams (mysql2 hits this for query-less URIs)
const bare = new URL('mysql://user:pw@dbhost:3306/mydb');
let empty = 0;
for (const _pair of bare.searchParams) empty++;
console.log('empty count', empty);

// 3. builtin held in a variable (minified-bundle shape)
const R = URL;
const u2 = new R('https://h/p?a=1&b=2');
for (const [k, v] of u2.searchParams) console.log('via-var', k, v);

// 4. free-standing URLSearchParams
const sp = new URLSearchParams('q=1&w=2&q=3');
for (const [k, v] of sp) console.log('free', k, v);

// 5. type-erased receiver: methods must dispatch dynamically
const erased: any = u2.searchParams;
console.log('get', erased.get('a'));
console.log('getAll', sp.getAll('q'));
for (const p of erased.entries()) console.log('entries', p[0], p[1]);
for (const k of erased.keys()) console.log('key', k);
for (const v of erased.values()) console.log('value', v);
erased.forEach((v: any, k: any) => console.log('fe', k, v));
Comment on lines +21 to +28

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 | 🟡 Minor | ⚡ Quick win

getAll isn't actually exercised through the dynamic receiver here.

Line 24 calls sp.getAll('q'), but sp (declared line 18) is statically typed as URLSearchParams, not any — unlike erased used on the surrounding lines. This call routes through the static/typed lowering, not through the new dynamic getAll dispatch arm this PR adds (url_search_params_dynamic_call in crates/perry-runtime/src/url/search_params.rs / the gate in crates/perry-runtime/src/object/native_call_method.rs). The "type-erased receiver" comment block implies all these calls test dynamic dispatch, but this one doesn't.

✅ Proposed fix
-console.log('getAll', sp.getAll('q'));
+console.log('getAll', erased.getAll('q'));
📝 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
// 5. type-erased receiver: methods must dispatch dynamically
const erased: any = u2.searchParams;
console.log('get', erased.get('a'));
console.log('getAll', sp.getAll('q'));
for (const p of erased.entries()) console.log('entries', p[0], p[1]);
for (const k of erased.keys()) console.log('key', k);
for (const v of erased.values()) console.log('value', v);
erased.forEach((v: any, k: any) => console.log('fe', k, v));
// 5. type-erased receiver: methods must dispatch dynamically
const erased: any = u2.searchParams;
console.log('get', erased.get('a'));
console.log('getAll', erased.getAll('q'));
for (const p of erased.entries()) console.log('entries', p[0], p[1]);
for (const k of erased.keys()) console.log('key', k);
for (const v of erased.values()) console.log('value', v);
erased.forEach((v: any, k: any) => console.log('fe', k, v));
🤖 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 `@test-files/test_gap_url_searchparams_iter.ts` around lines 21 - 28, Change
the getAll call in the type-erased receiver test to invoke getAll on erased
rather than sp. Keep the surrounding dynamic-dispatch calls and assertions
unchanged so the new url_search_params_dynamic_call path is exercised.


// 6. has/delete with the Node 19+ two-arg forms through a dynamic receiver
const dsp: any = new URLSearchParams('m=1&m=2&n=3');
console.log('has2', dsp.has('m', '2'), dsp.has('m', '9'));
dsp.delete('m', '1');
console.log('after delete2', dsp.toString());

// 7. sort through a dynamic receiver
const ssp: any = new URLSearchParams('z=26&a=1&k=11');
ssp.sort();
console.log('sorted', ssp.toString());

// 8. spread still works (regression guard for the #1668 path)
console.log('spread', JSON.stringify([...sp]));
Loading