Scripts: keep nested apply operands alive during collection

This commit is contained in:
dotCooCoo
2026-11-26 06:40:33 -08:00
parent 5d7e2af8a0
commit 75fd52fca6
2 changed files with 24 additions and 1 deletions
+4 -1
View File
@@ -2830,9 +2830,12 @@ impl Interp {
return Err(self.type_error("apply was called on a value that is not a function"));
}
let list = args.get(1).cloned().unwrap_or(Value::Undefined);
let mut roots = vec![f.clone(), this.clone()];
roots.extend(args.iter().cloned());
let expanded = self.rooted(&roots, |i| i.list_from_array_like(&list))?;
f = this;
this = args.first().cloned().unwrap_or(Value::Undefined);
args = self.list_from_array_like(&list)?;
args = expanded;
}
Func::Bound { target, this: bound_this, args: bound_args } => {
self.use_fuel()?;
+20
View File
@@ -613,6 +613,26 @@ fn apply_keeps_its_arguments_alive_while_a_native_function_runs() {
assert_eq!(realm.run(src), Ok(JsValue::Number(42.0)));
}
#[test]
fn successive_apply_expansions_keep_getter_receivers_alive() {
for queued in [false, true] {
let mut realm = Realm::with_options(Options { es2015: true, ..Options::default() }, Limits { heap_bytes: 1 << 20, ..Limits::default() });
let src = "var result; function callback(a) { result = this.kept + a; } \
var inner = {length: 1, get 0() { for (var i = 0; i < 20000; i++) ({i: i}); return 2; }}; \
var outer = {length: 2, get 0() { return {kept: 40}; }, get 1() { return inner; }};";
realm.run(src).unwrap();
let collections = realm.interp().heap.collections;
let call = if queued {
"Promise.resolve(outer).then(Function.prototype.apply.bind(Function.prototype.apply, callback));"
} else {
"Function.prototype.apply.apply(callback, outer);"
};
realm.run(call).unwrap();
assert!(realm.interp().heap.collections > collections);
assert_eq!(realm.run("result"), Ok(JsValue::Number(42.0)), "queued: {queued}");
}
}
#[test]
fn to_number_skips_the_white_space_the_lexer_skips() {
let mut realm = small(Limits::default());