Skip to content

Truly independent web browser

LibJS: Do not free the JIT code of a constructor while it runs

Tim Ledbetter54450aaa1b03
diff --git a/Libraries/LibJS/Rust/src/jit/calls.rs b/Libraries/LibJS/Rust/src/jit/calls.rs
index 0319bb4e8da..18d6b4c181c 100644
--- a/Libraries/LibJS/Rust/src/jit/calls.rs
+++ b/Libraries/LibJS/Rust/src/jit/calls.rs
@@ -1113,7 +1113,9 @@ pub unsafe extern "C" fn libjs_jit_prepare_construct(
};
match push_construct_frame(vm_ref, &site, function, new_target, &storage.values()) {
Ok(pushed) => {
- frame_of(pushed.callee_frame).returns_to_native_caller.set(true);
+ let callee_frame = frame_of(pushed.callee_frame);
+ callee_frame.returns_to_native_caller.set(true);
+ callee_frame.runs_jit_code.set(true);
PreparedConstruct {
callee_frame: pushed.callee_frame.as_ptr() as u64,
value: pushed.this_argument.map_or(Value::EMPTY, Value::from_object).0,
diff --git a/Tests/LibJS/Runtime/jit/invalidation.js b/Tests/LibJS/Runtime/jit/invalidation.js
index e2c25555bbe..ff867f14c48 100644
--- a/Tests/LibJS/Runtime/jit/invalidation.js
+++ b/Tests/LibJS/Runtime/jit/invalidation.js
@@ -8,6 +8,8 @@
const iterations = 200;
let neverAssignedBefore = 1;
+let neverAssignedBeforeConstruct = 1;
+let neverAssignedBeforeSuperCall = 1;
describe("Invalidation of JIT code", () => {
test("global bindings assigned for the first time while the code runs", () => {
@@ -24,6 +26,45 @@ describe("Invalidation of JIT code", () => {
for (let i = 0; i < iterations; ++i) expect(read()).toBe(2);
});
+ test("global bindings assigned for the first time while a constructor runs", () => {
+ const state = { action: index => index };
+ function call(index) {
+ debugger;
+ return state.action(index);
+ }
+ class Base {
+ field = 1;
+ constructor(index) {
+ this.before = index + neverAssignedBeforeConstruct + neverAssignedBeforeSuperCall;
+ this.value = call(index);
+ this.after = index + neverAssignedBeforeConstruct + neverAssignedBeforeSuperCall;
+ }
+ }
+ class Derived extends Base {}
+ const construct = (C, index) => new C(index);
+ for (const f of [call, Base, Derived, construct]) jit.prepare(f);
+ for (let i = 0; i < 20; ++i) {
+ expect(construct(Base, i).value).toBe(i);
+ expect(construct(Derived, i).value).toBe(i);
+ }
+ const assignments = [
+ [Base, () => ++neverAssignedBeforeConstruct],
+ [Derived, () => ++neverAssignedBeforeSuperCall],
+ ];
+ for (const [C, assign] of assignments) {
+ for (const f of [call, Base, Derived, construct]) jit.compile(f);
+ state.action = () => {
+ assign();
+ return 0;
+ };
+ const object = construct(C, 5);
+ expect(object).toBeInstanceOf(C);
+ expect(object.field).toBe(1);
+ expect(object.value).toBe(0);
+ expect(object.after).toBe(object.before + 1);
+ }
+ });
+
test("prototype chains that change between calls", () => {
class Base {
method() {