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
3 changes: 3 additions & 0 deletions changelog.d/8972-array-subclass-fill-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- `fill(value, start, end)` on a `class X extends Array` instance ignored `start` and `end` — the instance-installed stub had arity 1, so `sub.fill(8, 1)` overwrote the whole array instead of the tail from index 1.
29 changes: 23 additions & 6 deletions crates/perry-runtime/src/node_stream_constructors/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ pub extern "C" fn js_array_subclass_init(this: f64, n: f64) -> f64 {
// divergence tracked in #8953, unchanged by the elements store.
let this = this_root.get_nanbox_f64();
let obj = raw_ptr_from_value(this) as *mut ObjectHeader;
crate::closure::js_register_closure_arity(ns_array_fill as *const u8, 1);
let methods: [(&str, StubFn); 1] = [("fill", super::cast1(ns_array_fill))];
crate::closure::js_register_closure_arity(ns_array_fill as *const u8, 3);
let methods: [(&str, StubFn); 1] = [("fill", super::cast3(ns_array_fill))];
install_methods_on_existing_object(obj, this, &methods, &[]);
return this_root.get_nanbox_f64();
}
let length_key = crate::string::js_string_from_bytes(b"length".as_ptr(), 6);
js_object_set_field_by_name(obj, length_key, len);
crate::closure::js_register_closure_arity(ns_array_fill as *const u8, 1);
let methods: [(&str, StubFn); 1] = [("fill", super::cast1(ns_array_fill))];
crate::closure::js_register_closure_arity(ns_array_fill as *const u8, 3);
let methods: [(&str, StubFn); 1] = [("fill", super::cast3(ns_array_fill))];
install_methods_on_existing_object(obj, this, &methods, &[]);
this
}
Expand Down Expand Up @@ -261,8 +261,25 @@ pub unsafe extern "C" fn js_array_subclass_init_args(
/// `Array.prototype.fill`-equivalent installed on an Array-subclass instance:
/// fills the receiver's own indexed slots `0..length` with `value`. Delegates
/// to the generic array-like fill (which reads `length` off the receiver).
pub(super) extern "C" fn ns_array_fill(closure: *const ClosureHeader, value: f64) -> f64 {
crate::array::js_array_fill_generic(super::this_value(closure), value, 0, 0.0, 0, 0.0)
pub(super) extern "C" fn ns_array_fill(
closure: *const ClosureHeader,
value: f64,
start: f64,
end: f64,
) -> f64 {
// `fill(value, start?, end?)`. An omitted argument arrives as `undefined`
// and selects the spec default (`0` / `length`); before this the stub had
// arity 1, so `sub.fill(8, 1)` filled the WHOLE array instead of the tail
// from index 1 (node: `7|8|8`, perry: `8|8|8`).
let present = |v: f64| i32::from(!JSValue::from_bits(v.to_bits()).is_undefined());
crate::array::js_array_fill_generic(
super::this_value(closure),
value,
present(start),
start,
present(end),
end,
)
}

#[no_mangle]
Expand Down
62 changes: 62 additions & 0 deletions crates/perry/tests/array_subclass_fill_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! `sub.fill(value, start?, end?)` on a `class X extends Array` instance.
//!
//! `js_array_subclass_init` installs `fill` on the instance (node inherits it
//! from `Array.prototype`; perry has no such prototype object for these), and
//! that stub had arity 1 — so every `start`/`end` argument was dropped and the
//! whole array was overwritten.
use std::path::PathBuf;
use std::process::{Command, Output};

fn perry_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_perry"))
}

fn compile_and_run(source: &str) -> Output {
let dir = tempfile::tempdir().expect("tempdir");
let entry = dir.path().join("main.js");
let output = dir.path().join("main_bin");
std::fs::write(&entry, source).expect("write entry");
let compile = Command::new(perry_bin())
.current_dir(dir.path())
.arg("compile")
.arg(&entry)
.arg("-o")
.arg(&output)
.arg("--no-auto-optimize")
.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)
);
Command::new(&output).output().expect("run compiled binary")
}

/// Every `fill` form node accepts, on a subclass instance: value only, a start,
/// a start and end, and a negative start.
#[test]
fn array_subclass_fill_honours_start_and_end() {
let run = compile_and_run(
r#"
class A extends Array {}
const out = [];
const a = new A(); a.push(1, 2, 3, 4); out.push("all=" + a.fill(9).join("|"));
const b = new A(); b.push(1, 2, 3, 4); out.push("from1=" + b.fill(9, 1).join("|"));
const c = new A(); c.push(1, 2, 3, 4); out.push("1to3=" + c.fill(9, 1, 3).join("|"));
const d = new A(); d.push(1, 2, 3, 4); out.push("neg=" + d.fill(9, -2).join("|"));
console.log(out.join(" "));
"#,
);
assert!(
run.status.success(),
"the program must exit cleanly\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&run.stdout),
String::from_utf8_lossy(&run.stderr)
);
assert_eq!(
String::from_utf8_lossy(&run.stdout),
"all=9|9|9|9 from1=1|9|9|9 1to3=1|9|9|4 neg=1|2|9|9\n"
);
}
Loading