cleanup hook

This commit is contained in:
Jarred Sumner
2022-05-05 00:36:24 -07:00
parent c80e048ab3
commit d6ce585ef3
4 changed files with 111 additions and 5 deletions

View File

@@ -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),

View File

@@ -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();