diff options
Diffstat (limited to 'src/bun.js/bindings/bindings.zig')
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 56 |
1 files changed, 23 insertions, 33 deletions
diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 46f76d21d..64958cb3e 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -2508,6 +2508,10 @@ pub const JSGlobalObject = extern struct { return JSGlobalObject__setTimeZone(this, timeZone); } + pub inline fn toJS(globalThis: *JSGlobalObject) JSValue { + return @enumFromInt(@as(JSValue.Type, @bitCast(@intFromPtr(globalThis)))); + } + pub fn throwInvalidArguments( this: *JSGlobalObject, comptime fmt: string, @@ -2759,7 +2763,6 @@ pub const JSGlobalObject = extern struct { function, if (args.len > 0) args[0] else .zero, if (args.len > 1) args[1] else .zero, - if (args.len > 2) args[2] else .zero, ); } @@ -2768,14 +2771,12 @@ pub const JSGlobalObject = extern struct { function: JSValue, first: JSValue, second: JSValue, - third: JSValue, ) void { shim.cppFn("queueMicrotaskJob", .{ this, function, first, second, - third, }); } @@ -3141,6 +3142,8 @@ pub const JSValue = enum(JSValueReprInt) { DerivedStringObject, // End StringObject s. + InternalFieldTuple, + MaxJS = 0b11111111, Event = 0b11101111, DOMWrapper = 0b11101110, @@ -3472,8 +3475,8 @@ pub const JSValue = enum(JSValueReprInt) { JSC.markBinding(@src()); return JSC.C.JSObjectCallAsFunctionReturnValue( globalThis, - this.asObjectRef(), - @as(JSC.C.JSValueRef, @ptrCast(globalThis)), + this, + globalThis.toJS(), args.len, @as(?[*]const JSC.C.JSValueRef, @ptrCast(args.ptr)), ); @@ -3483,8 +3486,8 @@ pub const JSValue = enum(JSValueReprInt) { JSC.markBinding(@src()); return JSC.C.JSObjectCallAsFunctionReturnValue( globalThis, - this.asObjectRef(), - @as(JSC.C.JSValueRef, @ptrCast(thisValue.asNullableVoid())), + this, + thisValue, args.len, @as(?[*]const JSC.C.JSValueRef, @ptrCast(args.ptr)), ); @@ -4874,8 +4877,21 @@ pub const JSValue = enum(JSValueReprInt) { "jestStrictDeepEquals", "jestDeepMatch", }; + + // For any callback JSValue created in JS that you will not call *immediatly*, you must wrap it + // in an AsyncContextFrame with this function. This allows AsyncLocalStorage to work by + // snapshotting it's state and restoring it when called. + // - If there is no current context, this returns the callback as-is. + // - It is safe to run .call() on the resulting JSValue. This includes automatic unwrapping. + // - Do not pass the callback as-is to JS; The wrapped object is NOT a function. + // - If passed to C++, call it with AsyncContextFrame::call() instead of JSC::call() + pub inline fn withAsyncContextIfNeeded(this: JSValue, global: *JSGlobalObject) JSValue { + return AsyncContextFrame__withAsyncContextIfNeeded(global, this); + } }; +extern "c" fn AsyncContextFrame__withAsyncContextIfNeeded(global: *JSGlobalObject, callback: JSValue) JSValue; + extern "c" fn Microtask__run(*Microtask, *JSGlobalObject) void; extern "c" fn Microtask__run_default(*MicrotaskForDefaultGlobalObject, *JSGlobalObject) void; @@ -4982,32 +4998,6 @@ pub const VM = extern struct { return cppFn("deleteAllCode", .{ vm, global_object }); } - extern fn Bun__setOnEachMicrotaskTick(vm: *VM, ptr: ?*anyopaque, callback: ?*const fn (*anyopaque) callconv(.C) void) void; - - pub fn onEachMicrotask(vm: *VM, comptime Ptr: type, ptr: *Ptr, comptime callback: *const fn (*Ptr) void) void { - if (comptime is_bindgen) { - return; - } - - const callback_ = callback; - const Wrapper = struct { - pub fn run(ptr_: *anyopaque) callconv(.C) void { - var ptr__ = @as(*Ptr, @ptrCast(@alignCast(ptr_))); - callback_(ptr__); - } - }; - - Bun__setOnEachMicrotaskTick(vm, ptr, Wrapper.run); - } - - pub fn clearMicrotaskCallback(vm: *VM) void { - if (comptime is_bindgen) { - return; - } - - Bun__setOnEachMicrotaskTick(vm, null, null); - } - pub fn whenIdle( vm: *VM, callback: *const fn (...) callconv(.C) void, |