[bun:ffi] it works

This commit is contained in:
Jarred Sumner
2022-04-29 23:21:14 -07:00
parent d49ba50289
commit 516b54578d
15 changed files with 1278 additions and 222 deletions

View File

@@ -2882,6 +2882,9 @@ pub fn wrapWithHasContainer(
};
args[i] = val;
},
?JSValue => {
args[i] = iter.protectEatNext();
},
else => @compileError("Unexpected Type " ++ @typeName(ArgType)),
}
}
@@ -2921,3 +2924,51 @@ pub fn wrapWithHasContainer(
}
}.callback;
}
pub fn cachedBoundFunction(comptime name: [:0]const u8, comptime callback: anytype) (fn (
_: void,
ctx: js.JSContextRef,
_: js.JSValueRef,
_: js.JSStringRef,
_: js.ExceptionRef,
) js.JSValueRef) {
return struct {
const name_ = name;
pub fn call(
arg2: js.JSContextRef,
arg3: js.JSObjectRef,
arg4: js.JSObjectRef,
arg5: usize,
arg6: [*c]const js.JSValueRef,
arg7: js.ExceptionRef,
) callconv(.C) js.JSObjectRef {
return callback(
{},
arg2,
arg3,
arg4,
arg6[0..arg5],
arg7,
);
}
pub fn getter(
_: void,
ctx: js.JSContextRef,
_: js.JSValueRef,
_: js.JSStringRef,
_: js.ExceptionRef,
) js.JSValueRef {
const name_slice = std.mem.span(name_);
var existing = ctx.ptr().getCachedObject(&ZigString.init(name_slice));
if (existing.isEmpty()) {
return ctx.ptr().putCachedObject(
&ZigString.init(name_slice),
JSValue.fromRef(JSC.C.JSObjectMakeFunctionWithCallback(ctx, JSC.C.JSStringCreateStatic(name_slice.ptr, name_slice.len), call)),
).asObjectRef();
}
return existing.asObjectRef();
}
}.getter;
}