diff options
author | 2022-05-04 21:11:38 -0700 | |
---|---|---|
committer | 2022-05-05 21:35:08 -0700 | |
commit | b534c4d661845297410d557f2150a54af7a315a1 (patch) | |
tree | d12d5c03e9c88c3a14eaa50a6ad3b590bbfbaf19 /src/javascript | |
parent | 170d09b6bd1a941c72a9b468afcf4979791e7908 (diff) | |
download | bun-b534c4d661845297410d557f2150a54af7a315a1.tar.gz bun-b534c4d661845297410d557f2150a54af7a315a1.tar.zst bun-b534c4d661845297410d557f2150a54af7a315a1.zip |
More
Diffstat (limited to 'src/javascript')
-rw-r--r-- | src/javascript/jsc/base.zig | 35 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 11 |
2 files changed, 46 insertions, 0 deletions
diff --git a/src/javascript/jsc/base.zig b/src/javascript/jsc/base.zig index 2e0a723ea..ac2d05106 100644 --- a/src/javascript/jsc/base.zig +++ b/src/javascript/jsc/base.zig @@ -2511,6 +2511,41 @@ comptime { std.testing.refAllDecls(RefString); } +// TODO: remove this abstraction and make it work directly with +pub const ExternalBuffer = struct { + global: *JSC.JSGlobalObject, + ctx: ?*anyopaque = null, + function: JSC.napi.napi_finalize = null, + allocator: std.mem.Allocator, + buf: []u8 = &[_]u8{}, + + pub fn create(ctx: ?*anyopaque, buf: []u8, global: *JSC.JSGlobalObject, function: JSC.napi.napi_finalize, allocator: std.mem.Allocator) !*ExternalBuffer { + var container = try allocator.create(ExternalBuffer); + container.* = .{ + .ctx = ctx, + .global = global, + .allocator = allocator, + .function = function, + .buf = buf, + }; + return container; + } + + pub fn toJS(this: *ExternalBuffer, ctx: *JSC.JSGlobalObject) JSC.JSValue { + return JSC.JSValue.createBufferWithCtx(ctx, this.buf, this.ctx, ExternalBuffer_deallocator); + } + + pub fn toArrayBuffer(this: *ExternalBuffer, ctx: *JSC.JSGlobalObject) JSC.JSValue { + return JSValue.c(JSC.C.JSObjectMakeArrayBufferWithBytesNoCopy(ctx.ref(), this.buf.ptr, this.buf.len, ExternalBuffer_deallocator, this, null)); + } +}; +pub export fn ExternalBuffer_deallocator(bytes_: *anyopaque, ctx: *anyopaque) callconv(.C) void { + var external: *ExternalBuffer = bun.cast(*ExternalBuffer, ctx); + external.function.?(external.global, external.ctx, bytes_); + const allocator = external.allocator; + allocator.destroy(external); +} + pub export fn MarkedArrayBuffer_deallocator(bytes_: *anyopaque, _: *anyopaque) void { const mimalloc = @import("../../allocators/mimalloc.zig"); // zig's memory allocator interface won't work here diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index eb46837a1..7732a27e3 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -2324,6 +2324,11 @@ pub const JSValue = enum(u64) { return JSC.GetJSPrivateData(ZigType, value.asObjectRef()); } + extern fn JSBuffer__isBuffer(*JSGlobalObject, JSValue) bool; + pub fn isBuffer(value: JSValue, global: *JSGlobalObject) bool { + return JSBuffer__isBuffer(value, global); + } + pub fn asCheckLoaded(value: JSValue, comptime ZigType: type) ?*ZigType { if (!ZigType.Class.isLoaded() or value.isUndefinedOrNull()) return null; @@ -2355,6 +2360,12 @@ pub const JSValue = enum(u64) { } } + pub fn createBufferWithCtx(globalObject: *JSGlobalObject, slice: []u8, ptr: *anyopaque, func: JSC.C.JSTypedArrayBytesDeallocator) JSValue { + if (comptime JSC.is_bindgen) unreachable; + @setRuntimeSafety(false); + return JSBuffer__bufferFromPointerAndLengthAndDeinit(globalObject, slice.ptr, slice.len, ptr, func); + } + extern fn JSBuffer__bufferFromPointerAndLengthAndDeinit(*JSGlobalObject, [*]u8, usize, ?*anyopaque, JSC.C.JSTypedArrayBytesDeallocator) JSValue; pub fn jsNumberWithType(comptime Number: type, number: Number) JSValue { |