diff options
Diffstat (limited to 'src/javascript')
-rw-r--r-- | src/javascript/jsc/javascript.zig | 10 | ||||
-rw-r--r-- | src/javascript/jsc/rare_data.zig | 49 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 06f0c47a1..f6147a1dd 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -584,6 +584,16 @@ pub const VirtualMachine = struct { return this.event_loop; } + pub fn onExit(this: *VirtualMachine) void { + var rare_data = this.rare_data orelse return; + var hook = rare_data.cleanup_hook orelse return; + hook.execute(); + while (hook.next) |next| { + next.execute(); + hook = next; + } + } + pub const EventLoop = struct { ready_tasks_count: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0), pending_tasks_count: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0), diff --git a/src/javascript/jsc/rare_data.zig b/src/javascript/jsc/rare_data.zig index b6559826c..90b8861d4 100644 --- a/src/javascript/jsc/rare_data.zig +++ b/src/javascript/jsc/rare_data.zig @@ -14,6 +14,55 @@ stderr_store: ?*Blob.Store = null, stdin_store: ?*Blob.Store = null, stdout_store: ?*Blob.Store = null, +// TODO: make this per JSGlobalObject instead of global +// This does not handle ShadowRealm correctly! +tail_cleanup_hook: ?*CleanupHook = null, +cleanup_hook: ?*CleanupHook = null, + +pub const CleanupHook = struct { + next: ?*CleanupHook = null, + ctx: ?*anyopaque, + func: Function, + globalThis: *JSC.JSGlobalObject, + + pub fn eql(self: CleanupHook, other: CleanupHook) bool { + return self.ctx == other.ctx and self.func == other.func and self.globalThis == other.globalThis; + } + + pub fn from( + globalThis: *JSC.JSGlobalObject, + ctx: ?*anyopaque, + func: CleanupHook.Function, + ) CleanupHook { + return .{ + .next = null, + .ctx = ctx, + .func = func, + .globalThis = globalThis, + }; + } + + pub const Function = fn (?*anyopaque) callconv(.C) void; +}; + +pub fn pushCleanupHook( + this: *RareData, + globalThis: *JSC.JSGlobalObject, + ctx: ?*anyopaque, + func: CleanupHook.Function, +) void { + var hook = JSC.VirtualMachine.vm.allocator.create(CleanupHook) catch unreachable; + hook.* = CleanupHook.from(globalThis, ctx, func); + if (this.cleanup_hook == null) { + this.cleanup_hook = hook; + this.tail_cleanup_hook = hook; + } else { + this.cleanup_hook.?.next = hook; + } + + return hook; +} + pub fn boringEngine(rare: *RareData) *BoringSSL.ENGINE { return rare.boring_ssl_engine orelse brk: { rare.boring_ssl_engine = BoringSSL.ENGINE_new(); |