Truly independent web browser
LibJS: Give an inlined frame its own closure’s environments
sideshowbarker14fe27960f32
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rsindex fcb357a4863..bc352729705 100644--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs@@ -1506,21 +1506,21 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {/// linked to the running frame like the interpreter's inline calls do,/// and returns the constant ones for `emit_constant_fields()`. Its `this`/// value and slots are left to the caller. Clobbers `function_register`- /// and `temp`.+ /// and `temp`. The fields come from snapshot executable `executable`,+ /// the entry of the frame's own function: closures that share one+ /// executable each have an entry, with their own environments.fn emit_frame_header(&mut self,+ executable: u32,new_frame: Gpr,function_register: Gpr,temp: Gpr,header: &FrameHeader,) -> Vec<ConstantField> {let offsets = self.runtime.offsets;- let snapshot = self- .executables- .iter()- .find(|executable| executable.cell == header.executable);- let function_fields = snapshot.and_then(|executable| executable.function_fields);- let builtin = snapshot.is_some_and(|executable| executable.builtin);+ let snapshot = &self.executables[executable as usize];+ let function_fields = snapshot.function_fields;+ let builtin = snapshot.builtin;let mut constants = Vec::new();if builtin {// NB: Frames of builtins written in JavaScript have the@@ -1736,7 +1736,7 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {self.emit_function_fields(new_frame, closure, temp);self.frame_header_field_constants(&header)}- None => self.emit_frame_header(new_frame, function_register, temp, &header),+ None => self.emit_frame_header(executable, new_frame, function_register, temp, &header),};if construct {for field in &mut constants {diff --git a/Tests/LibJS/Runtime/jit/regress/inlined-closures-sharing-an-executable.js b/Tests/LibJS/Runtime/jit/regress/inlined-closures-sharing-an-executable.jsnew file mode 100644index 00000000000..7be3f913fb1--- /dev/null+++ b/Tests/LibJS/Runtime/jit/regress/inlined-closures-sharing-an-executable.js@@ -0,0 +1,25 @@+// A function created in an inlined call of a closure must get that closure's environment, even where another closure+// of the same function is inlined in the same compiled code.++function make(prefix, suffix) {+ return function (value) {+ const join = suffix ? rest => `${prefix}/${suffix}/${rest}` : null;+ return join ? join(value) : `${prefix}/-/${value}`;+ };+}++test("closures of one function inlined in one caller", () => {+ const callees = { plain: make("plain"), withSuffix: make("with", "suffix") };+ function caller(value) {+ if (value === undefined) return callees.plain("x");+ return callees.withSuffix(value);+ }+ jit.prepare(caller);+ for (let i = 0; i < 10; ++i) {+ expect(caller()).toBe("plain/-/x");+ expect(caller("y")).toBe("with/suffix/y");+ }+ jit.compile(caller);+ expect(caller()).toBe("plain/-/x");+ expect(caller("y")).toBe("with/suffix/y");+});