From b47469aa56422098f54d56919417e2c1906546b8 Mon Sep 17 00:00:00 2001 From: Ralph Date: Fri, 26 Jun 2026 14:37:42 -0700 Subject: [PATCH 1/2] =?UTF-8?q?fix(codegen):=20#5703=20=E2=80=94=20pad=20u?= =?UTF-8?q?nder-supplied=20class-EXPRESSION=20static-method=20calls=20with?= =?UTF-8?q?=20undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A static method of a class EXPRESSION (`var C = class { static m(...) {...} }`) called with fewer arguments than declared dropped its default-parameter / array-destructuring prologue. `C.m()` on a class-expression value reaches the fused get-static-method+call path in `lower_call/property_get/static_dispatch.rs`, whereas class DECLARATIONS lower `C.m()` to a `StaticMethodCall` (which already pads missing slots with `undefined` — #235). The dispatch path forwarded only the supplied args to a fixed-arity LLVM function, so missing slots arrived as an uninitialized `0.0` register rather than `undefined`. The callee's default-param prologue (`if (p === undefined) p = …`) never fired, and array destructuring of the missing slot (`GetIterator(p)`) threw `TypeError: is not iterable`. This broke ~61 `language/expressions/class` test262 cases (static methods of class expressions with default params / destructuring), dominated by static async-generator methods with an array-destructuring default param. The maintainer's bisection window pointed at #5662, but that commit only adds a `RegisterClassCaptures` snapshot under `if !captured_args.is_empty()`; the bug reproduces with captures disabled, so it is pre-existing in the static-dispatch path, not a #5662 regression. Fix: pad the missing positional slots with `undefined` in the static dispatch path, mirroring the `StaticMethodCall` path — in both the plain-rest and no-rest branches. Also fix the synthesized-`arguments` branch, which pushed ONLY the arguments object: a leading real param before the synth `arguments` slot (`static method(x, _ = 0) { … arguments … }`) received the (empty) arguments array instead of its argument / `undefined`. It now lowers each arg once (no double side effects), feeds the leading params their positional values (undefined-padded), and builds the `arguments` object from all passed args. Resolves the dominant array-destructuring/default-param cases plus the `params-dflt-*-meth-static-args-unmapped` and `-ref-arguments` cases; verified 0 newly-broken across `language/expressions/class`. The remaining residuals (object-rest destructuring with getters/non-enumerable, static setters) are separate pre-existing issues outside the default-param family. Regression test issue_5703_class_expr_static_dflt_params (5 cases, byte-for-byte against node --experimental-strip-types). Co-Authored-By: Claude Opus 4.8 (1M context) --- .../property_get/static_dispatch.rs | 52 ++++- ...ssue_5703_class_expr_static_dflt_params.rs | 178 ++++++++++++++++++ 2 files changed, 226 insertions(+), 4 deletions(-) create mode 100644 crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs diff --git a/crates/perry-codegen/src/lower_call/property_get/static_dispatch.rs b/crates/perry-codegen/src/lower_call/property_get/static_dispatch.rs index 9fa2dfa009e..73a81344675 100644 --- a/crates/perry-codegen/src/lower_call/property_get/static_dispatch.rs +++ b/crates/perry-codegen/src/lower_call/property_get/static_dispatch.rs @@ -143,12 +143,33 @@ pub(crate) fn try_lower_static_dispatch( // then bundle the trailing args into an Array. let mut lowered: Vec = Vec::with_capacity(args.len()); if has_rest && is_synth_args { - let cap = (args.len() as u32).to_string(); - let mut current = ctx.block().call(I64, "js_array_alloc", &[(I32, &cap)]); + // Lower each call arg exactly ONCE (a value may have side + // effects), then reuse the SSA registers both for the leading + // real params and for the synthesized `arguments` object. + let mut vals: Vec = Vec::with_capacity(args.len()); for a in args { - let v = lower_expr(ctx, a)?; + vals.push(lower_expr(ctx, a)?); + } + // #5703: the leading real params BEFORE the synth `arguments` + // slot (`static method(x, _ = 0) { … arguments … }` → + // params `[x, _, ]`) must receive their positional + // values, padded with `undefined` when under-supplied — exactly + // as the class-DECLARATION (StaticMethodCall) path does. + // Previously this branch pushed ONLY the arguments object, so a + // leading param like `x` received the (empty) arguments array + // instead of its argument / `undefined` (test262 + // `params-dflt-meth-static-args-unmapped`). + let undef = double_literal(f64::from_bits(crate::nanbox::TAG_UNDEFINED)); + let fixed_count = declared.saturating_sub(1); + for i in 0..fixed_count { + lowered.push(vals.get(i).cloned().unwrap_or_else(|| undef.clone())); + } + // The synthesized `arguments` object holds ALL passed args. + let cap = (vals.len() as u32).to_string(); + let mut current = ctx.block().call(I64, "js_array_alloc", &[(I32, &cap)]); + for v in &vals { let blk = ctx.block(); - current = blk.call(I64, "js_array_push_f64", &[(I64, ¤t), (DOUBLE, &v)]); + current = blk.call(I64, "js_array_push_f64", &[(I64, ¤t), (DOUBLE, v)]); } current = ctx.block() @@ -160,6 +181,15 @@ pub(crate) fn try_lower_static_dispatch( for a in args.iter().take(fixed_count) { lowered.push(lower_expr(ctx, a)?); } + // #5703 (mirrors #235 in the StaticMethodCall path): when the + // caller under-supplies the fixed leading params, pad the + // missing slots with `undefined` BEFORE the rest array, so the + // callee's default-param prologue / destructuring fires instead + // of reading an uninitialized (0.0) parameter register. + let undef = double_literal(f64::from_bits(crate::nanbox::TAG_UNDEFINED)); + while lowered.len() < fixed_count { + lowered.push(undef.clone()); + } let rest_count = args.len().saturating_sub(fixed_count); let cap = (rest_count as u32).to_string(); let mut current = ctx.block().call(I64, "js_array_alloc", &[(I32, &cap)]); @@ -174,6 +204,20 @@ pub(crate) fn try_lower_static_dispatch( for a in args { lowered.push(lower_expr(ctx, a)?); } + // #5703: a static method of a class EXPRESSION called with fewer + // args than declared (`C.m()` for `static m(a = 1)` or + // `static m([x, y] = […])`) reaches this fused get-static-method + // +call path rather than the `StaticMethodCall` path used by + // class DECLARATIONS. That path pads missing slots with + // `undefined` (#235); this one did not, so the callee read an + // uninitialized (0.0) register — its default-param prologue + // (`if (p === undefined) p = …`) and array destructuring + // (`GetIterator(p)` → "is not iterable") never fired. Pad here + // too so both paths behave identically. + let undef = double_literal(f64::from_bits(crate::nanbox::TAG_UNDEFINED)); + while lowered.len() < declared { + lowered.push(undef.clone()); + } } let prev_this = ctx.block() diff --git a/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs b/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs new file mode 100644 index 00000000000..822efe2e75c --- /dev/null +++ b/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs @@ -0,0 +1,178 @@ +//! Regression test for #5703 — a STATIC method of a class EXPRESSION +//! (`var C = class { static m(...) {...} }`) called with fewer arguments than +//! declared dropped its default-parameter / array-destructuring prologue. +//! +//! Root cause: `C.m()` on a class-expression value reaches the fused +//! get-static-method+call path in +//! `lower_call/property_get/static_dispatch.rs` (class DECLARATIONS instead +//! lower `C.m()` to a `StaticMethodCall`, which already pads — #235). That +//! path forwarded only the supplied args to a fixed-arity LLVM function, so +//! missing slots arrived as an uninitialized `0.0` register rather than +//! `undefined`. The callee's default-param prologue (`if (p === undefined) p = +//! …`) never fired, and array destructuring of the missing slot +//! (`GetIterator(p)`) threw `TypeError: is not iterable`. This broke ~61 +//! `language/expressions/class` test262 cases (static methods of class +//! expressions with default params / destructuring), dominated by static +//! async-generator methods with an array-destructuring default param. +//! +//! Fix: pad the missing positional slots with `undefined` in the static +//! dispatch path, mirroring the `StaticMethodCall` path. Expected outputs are +//! byte-for-byte what `node --experimental-strip-types` prints. + +use std::path::PathBuf; +use std::process::Command; + +fn perry_bin() -> PathBuf { + PathBuf::from(env!("CARGO_BIN_EXE_perry")) +} + +fn compile_and_run(dir: &std::path::Path, source: &str) -> String { + let entry = dir.join("main.ts"); + let output = dir.join("main_bin"); + std::fs::write(&entry, source).expect("write entry"); + + let compile = Command::new(perry_bin()) + .current_dir(dir) + .arg("compile") + .arg(&entry) + .arg("-o") + .arg(&output) + .arg("--no-cache") + .output() + .expect("run perry compile"); + assert!( + compile.status.success(), + "perry compile failed\nstdout:\n{}\nstderr:\n{}", + String::from_utf8_lossy(&compile.stdout), + String::from_utf8_lossy(&compile.stderr) + ); + + let run = Command::new(&output) + .current_dir(dir) + .output() + .expect("run compiled binary"); + assert!( + run.status.success(), + "compiled binary failed (exit {:?})\nstdout:\n{}\nstderr:\n{}", + run.status.code(), + String::from_utf8_lossy(&run.stdout), + String::from_utf8_lossy(&run.stderr) + ); + String::from_utf8_lossy(&run.stdout).into_owned() +} + +/// Scalar default param on a class-expression static method, called with no +/// args. Pre-fix this printed `0` (uninitialized register), then `9`. +#[test] +fn class_expr_static_scalar_default_param() { + let dir = tempfile::tempdir().expect("tempdir"); + let stdout = compile_and_run( + dir.path(), + r#" +var C = class { + static m(x = 5) { return x; } +}; +console.log(C.m()); +console.log(C.m(9)); +"#, + ); + assert_eq!( + stdout, "5\n9\n", + "missing arg must default to 5 (undefined-padded), supplied arg wins" + ); +} + +/// Array-destructuring default param — the dominant #5703 signature. Pre-fix +/// `C.m()` threw `TypeError: is not iterable` (the missing slot was `0.0`, not +/// the `undefined` that triggers the default). +#[test] +fn class_expr_static_destructuring_default_param() { + let dir = tempfile::tempdir().expect("tempdir"); + let stdout = compile_and_run( + dir.path(), + r#" +var C = class { + static m([x, y] = [3, 4]) { return x + y; } +}; +console.log(C.m()); +console.log(C.m([1, 2])); +"#, + ); + assert_eq!(stdout, "7\n3\n", "default array destructured when no arg supplied"); +} + +/// A leading non-default param BEFORE the synthesized `arguments` slot +/// (`static method(x, _ = 0) { … arguments … }`) on a class expression. The +/// synth-`arguments` dispatch branch previously pushed only the arguments +/// object, so `x` received the (empty) arguments array — printing `x=` instead +/// of `x=undefined` (test262 `params-dflt-meth-static-args-unmapped`). The +/// `arguments` object must hold all passed args, independent of the (unmapped) +/// named params. +#[test] +fn class_expr_static_leading_param_with_arguments() { + let dir = tempfile::tempdir().expect("tempdir"); + let stdout = compile_and_run( + dir.path(), + r#" +var C = class { + static method(x, _ = 0) { + console.log("x=" + x + " _=" + _); + console.log( + "a0=" + arguments[0] + " a1=" + arguments[1] + " len=" + arguments.length + ); + } +}; +C.method(); +C.method(7, 8, 9); +"#, + ); + assert_eq!( + stdout, + "x=undefined _=0\na0=undefined a1=undefined len=0\nx=7 _=8\na0=7 a1=8 len=3\n", + "leading param + synth arguments must match node (byte-for-byte)" + ); +} + +/// A class-expression static method whose ONLY parameter is the synthesized +/// `arguments` (effect-style `static pipe() { … arguments.length … }`) must be +/// unaffected by the leading-param fix above — guards the fixed_count == 0 path. +#[test] +fn class_expr_static_arguments_only_unregressed() { + let dir = tempfile::tempdir().expect("tempdir"); + let stdout = compile_and_run( + dir.path(), + r#" +var C = class { + static pipe() { return arguments.length; } +}; +console.log(C.pipe()); +console.log(C.pipe(1, 2, 3)); +"#, + ); + assert_eq!(stdout, "0\n3\n", "arguments-only static method counts all args"); +} + +/// Static async-generator method of a class expression with an +/// array-destructuring default param — the exact shape of the ~42 +/// `async-gen-meth-static-dflt-ary-*` test262 cases. +#[test] +fn class_expr_static_async_gen_destructuring_default() { + let dir = tempfile::tempdir().expect("tempdir"); + let stdout = compile_and_run( + dir.path(), + r#" +var C = class { + static async *agen([a, b] = [10, 20]) { + yield a; + yield b; + } +}; +(async () => { + const out: number[] = []; + for await (const v of C.agen()) out.push(v); + console.log(out.join(",")); +})(); +"#, + ); + assert_eq!(stdout, "10,20\n", "default applies inside a static async generator"); +} From c370c63baed2b3ebc110743c59147b3d1d88f7c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ralph=20K=C3=BCpper?= Date: Sat, 27 Jun 2026 07:30:42 +0200 Subject: [PATCH 2/2] style: cargo fmt issue_5703_class_expr_static_dflt_params test --- ...issue_5703_class_expr_static_dflt_params.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs b/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs index 822efe2e75c..262256a80eb 100644 --- a/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs +++ b/crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs @@ -98,7 +98,10 @@ console.log(C.m()); console.log(C.m([1, 2])); "#, ); - assert_eq!(stdout, "7\n3\n", "default array destructured when no arg supplied"); + assert_eq!( + stdout, "7\n3\n", + "default array destructured when no arg supplied" + ); } /// A leading non-default param BEFORE the synthesized `arguments` slot @@ -127,8 +130,7 @@ C.method(7, 8, 9); "#, ); assert_eq!( - stdout, - "x=undefined _=0\na0=undefined a1=undefined len=0\nx=7 _=8\na0=7 a1=8 len=3\n", + stdout, "x=undefined _=0\na0=undefined a1=undefined len=0\nx=7 _=8\na0=7 a1=8 len=3\n", "leading param + synth arguments must match node (byte-for-byte)" ); } @@ -149,7 +151,10 @@ console.log(C.pipe()); console.log(C.pipe(1, 2, 3)); "#, ); - assert_eq!(stdout, "0\n3\n", "arguments-only static method counts all args"); + assert_eq!( + stdout, "0\n3\n", + "arguments-only static method counts all args" + ); } /// Static async-generator method of a class expression with an @@ -174,5 +179,8 @@ var C = class { })(); "#, ); - assert_eq!(stdout, "10,20\n", "default applies inside a static async generator"); + assert_eq!( + stdout, "10,20\n", + "default applies inside a static async generator" + ); }