aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-30 01:08:53 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-09-30 01:08:53 -0700
commit18310cfd7cb7ffb39a5cd96e5c5c020414894528 (patch)
tree46df44e0208c476f9018b9600ef87b18058c1ed0 /src
parent9046767da4e1a349b6ebc10680d81c07ebaa95a8 (diff)
downloadbun-18310cfd7cb7ffb39a5cd96e5c5c020414894528.tar.gz
bun-18310cfd7cb7ffb39a5cd96e5c5c020414894528.tar.zst
bun-18310cfd7cb7ffb39a5cd96e5c5c020414894528.zip
Fix unnecessary "Buffer is detached" error
Diffstat (limited to '')
-rw-r--r--src/bun.js/base.zig42
1 files changed, 34 insertions, 8 deletions
diff --git a/src/bun.js/base.zig b/src/bun.js/base.zig
index faae6d566..0770c77a4 100644
--- a/src/bun.js/base.zig
+++ b/src/bun.js/base.zig
@@ -2402,8 +2402,8 @@ pub const ArrayBuffer = extern struct {
};
}
- extern "C" fn Bun__createUint8ArrayForCopy(*JSC.JSGlobalObject, ptr: *const anyopaque, len: usize) JSValue;
- extern "C" fn Bun__createArrayBufferForCopy(*JSC.JSGlobalObject, ptr: *const anyopaque, len: usize) JSValue;
+ extern "C" fn Bun__createUint8ArrayForCopy(*JSC.JSGlobalObject, ptr: ?*const anyopaque, len: usize) JSValue;
+ extern "C" fn Bun__createArrayBufferForCopy(*JSC.JSGlobalObject, ptr: ?*const anyopaque, len: usize) JSValue;
pub fn fromTypedArray(ctx: JSC.C.JSContextRef, value: JSC.JSValue, _: JSC.C.ExceptionRef) ArrayBuffer {
var out = std.mem.zeroes(ArrayBuffer);
@@ -2417,6 +2417,24 @@ pub const ArrayBuffer = extern struct {
}
pub fn toJSUnchecked(this: ArrayBuffer, ctx: JSC.C.JSContextRef, exception: JSC.C.ExceptionRef) JSC.JSValue {
+
+ // The reason for this is
+ // JSC C API returns a detached arraybuffer
+ // if you pass it a zero-length TypedArray
+ // we don't ever want to send the user a detached arraybuffer
+ // that's just silly.
+ if (this.byte_len == 0) {
+ if (this.typed_array_type == .ArrayBuffer) {
+ return create(ctx, "", .ArrayBuffer);
+ }
+
+ if (this.typed_array_type == .Uint8Array) {
+ return create(ctx, "", .Uint8Array);
+ }
+
+ // TODO: others
+ }
+
if (this.typed_array_type == .ArrayBuffer) {
return JSC.JSValue.fromRef(JSC.C.JSObjectMakeArrayBufferWithBytesNoCopy(
ctx,
@@ -2445,7 +2463,7 @@ pub const ArrayBuffer = extern struct {
}
// If it's not a mimalloc heap buffer, we're not going to call a deallocator
- if (!bun.Global.Mimalloc.mi_is_in_heap_region(this.ptr)) {
+ if (this.len > 0 and !bun.Global.Mimalloc.mi_is_in_heap_region(this.ptr)) {
if (this.typed_array_type == .ArrayBuffer) {
return JSC.JSValue.fromRef(JSC.C.JSObjectMakeArrayBufferWithBytesNoCopy(
ctx,
@@ -2506,14 +2524,22 @@ pub const ArrayBuffer = extern struct {
pub const fromArrayBuffer = fromTypedArray;
- pub inline fn slice(this: *const @This()) []u8 {
- return this.ptr[this.offset .. this.offset + this.len];
- }
-
+ /// The equivalent of
+ ///
+ /// ```js
+ /// new ArrayBuffer(view.buffer, view.byteOffset, view.byteLength)
+ /// ```
pub inline fn byteSlice(this: *const @This()) []u8 {
return this.ptr[this.offset .. this.offset + this.byte_len];
}
+ /// The equivalent of
+ ///
+ /// ```js
+ /// new ArrayBuffer(view.buffer, view.byteOffset, view.byteLength)
+ /// ```
+ pub const slice = byteSlice;
+
pub inline fn asU16(this: *const @This()) []u16 {
return std.mem.bytesAsSlice(u16, @alignCast(@alignOf([*]u16), this.ptr[this.offset..this.byte_len]));
}
@@ -2573,7 +2599,7 @@ pub const MarkedArrayBuffer = struct {
};
pub inline fn slice(this: *const @This()) []u8 {
- return this.buffer.slice();
+ return this.buffer.byteSlice();
}
pub fn destroy(this: *MarkedArrayBuffer) void {