Skip to content

Commit 3d4815c

Browse files
proggeramlugRalph Küpper
andauthored
fix(runtime): #5893 — class setter .length ignores default params (#5939)
A setter with a defaulted formal param (`set m(x = 42)`) must report `.length === 0` (defaults do not count toward a function's length), but `class_accessor_function_value` hardcoded the reflected setter value's length to 1, ignoring defaults. Reading `Object.getOwnPropertyDescriptor(C.prototype, 'm').set.length` therefore returned 1 instead of 0. Fix: codegen now registers each setter's default-aware spec length (`spec_function_length`) against its accessor func_ptr via `js_register_closure_length`, and `class_accessor_function_value` consults `lookup_closure_length` for the raw setter pointer, falling back to the fixed 1/0 when no registration exists (native / cross-module accessors). A plain `set m(x)` still reports 1; getters stay at 0. Fixes test262 language/{statements,expressions}/class/setter-length-dflt. Co-authored-by: Ralph Küpper <ralph@skelpo.com>
1 parent 82929c0 commit 3d4815c

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

‎crates/perry-codegen/src/codegen/string_pool.rs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::strings::StringPool;
88
use crate::types::{DOUBLE, I32, I64, PTR, VOID};
99

1010
use super::helpers::{sanitize, sanitize_member, scoped_static_method_name};
11+
use super::spec_function_length;
1112

1213
/// Emits a long sequence of INDEPENDENT init operations (string allocation,
1314
/// closure/class/function registration) into a series of small chunk functions
@@ -963,7 +964,15 @@ pub(super) fn emit_string_pool(
963964
// getter-pairs loop above; emission mangling matches the
964965
// setter-method-emission path at codegen.rs:2041 (renamed.name =
965966
// "__set_<prop>" → LLVM symbol perry_method_<mp>__<class>____set_<f.name>).
966-
let mut setter_pairs: Vec<(u32, String, String, bool)> = Vec::new();
967+
// (cid, prop, llvm_name, is_static, spec_length). `spec_length` is the
968+
// ECMAScript-visible `.length` of the setter function value read via
969+
// `Object.getOwnPropertyDescriptor(proto, prop).set` — leading formal
970+
// params before the first default/rest. A setter always has one formal
971+
// param, but `set m(x = 42)` has `.length === 0` (test262
972+
// class/setter-length-dflt): without a per-func-ptr length registration
973+
// the runtime fell back to the setter's ABI arity (1), over-counting the
974+
// defaulted param.
975+
let mut setter_pairs: Vec<(u32, String, String, bool, u32)> = Vec::new();
967976
for (class_name, class) in classes.iter() {
968977
if *class_name != class.name {
969978
continue;
@@ -999,11 +1008,12 @@ pub(super) fn emit_string_pool(
9991008
sanitize_member(&inner),
10001009
)
10011010
};
1002-
setter_pairs.push((cid, prop.clone(), llvm_name, is_static));
1011+
let spec_length = spec_function_length(&setter_fn.params) as u32;
1012+
setter_pairs.push((cid, prop.clone(), llvm_name, is_static, spec_length));
10031013
}
10041014
}
10051015
setter_pairs.sort_unstable();
1006-
for (cid, prop_name, llvm_name, is_static) in setter_pairs {
1016+
for (cid, prop_name, llvm_name, is_static, spec_length) in setter_pairs {
10071017
chunker.roll_if_full();
10081018
let blk = chunker.current_block();
10091019
let entry = match strings.iter().find(|e| e.value == prop_name) {
@@ -1015,6 +1025,15 @@ pub(super) fn emit_string_pool(
10151025
let func_ref = format!("@{}", llvm_name);
10161026
let func_i64 = blk.ptrtoint(&func_ref, I64);
10171027
let bytes_i64 = blk.ptrtoint(&bytes_global, I64);
1028+
// Register the setter's spec `.length` keyed by its func_ptr so
1029+
// `Object.getOwnPropertyDescriptor(proto, prop).set.length` reports
1030+
// the default-aware count instead of the raw ABI arity. Static
1031+
// accessors are emitted under a no-`this` `perry_static_…` symbol, so
1032+
// the same func_ptr is what a value-read of `.set` binds.
1033+
blk.call_void(
1034+
"js_register_closure_length",
1035+
&[(PTR, &func_ref), (I32, &spec_length.to_string())],
1036+
);
10181037
let register_fn = if is_static {
10191038
"js_register_class_static_setter"
10201039
} else {

‎crates/perry-runtime/src/object/class_registry/registration.rs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,16 @@ pub(crate) fn class_accessor_function_value(
161161
return f64::from_bits(crate::value::TAG_UNDEFINED);
162162
}
163163
unsafe { crate::closure::js_closure_set_capture_ptr(closure, 0, raw_ptr as i64) };
164-
super::super::native_module::set_builtin_closure_length(
165-
closure as usize,
166-
if is_setter { 1 } else { 0 },
167-
);
164+
// Spec `.length`: params before the first default/rest. A getter takes no
165+
// params (0); a setter takes exactly one formal param — but `set m(x = 42)`
166+
// has `.length === 0` (defaults don't count). Codegen registers the raw
167+
// accessor func_ptr's default-aware spec length via
168+
// `js_register_closure_length`; consult it so a defaulted setter reports 0.
169+
// Fall back to the fixed 1/0 when no registration exists (e.g. native or
170+
// cross-module accessors whose length wasn't emitted).
171+
let spec_length = crate::closure::lookup_closure_length(raw_ptr as *const u8)
172+
.unwrap_or(if is_setter { 1 } else { 0 });
173+
super::super::native_module::set_builtin_closure_length(closure as usize, spec_length);
168174
super::super::native_module::set_builtin_closure_non_constructable(closure as usize);
169175
// Spec `.name` = "get <key>" / "set <key>" with attributes
170176
// { writable: false, enumerable: false, configurable: true } (mirrors the

0 commit comments

Comments
 (0)