diff options
Diffstat (limited to 'src/javascript')
-rw-r--r-- | src/javascript/jsc/base.zig | 72 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.cpp | 36 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 30 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-handwritten.h | 8 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-replacements.zig | 1 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.h | 5 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.zig | 2 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 2 | ||||
-rw-r--r-- | src/javascript/jsc/node/node_fs.zig | 2 | ||||
-rw-r--r-- | src/javascript/jsc/webcore/response.zig | 106 |
11 files changed, 161 insertions, 105 deletions
diff --git a/src/javascript/jsc/base.zig b/src/javascript/jsc/base.zig index 4d61fb33a..b77baa550 100644 --- a/src/javascript/jsc/base.zig +++ b/src/javascript/jsc/base.zig @@ -1665,50 +1665,44 @@ pub fn getAllocator(_: js.JSContextRef) std.mem.Allocator { pub const JSStringList = std.ArrayList(js.JSStringRef); -pub const ArrayBuffer = struct { +pub const ArrayBuffer = extern struct { ptr: [*]u8 = undefined, offset: u32, - // for the array type, len: u32, - byte_len: u32, + typed_array_type: JSC.JSValue.JSType, - typed_array_type: js.JSTypedArrayType, - - encoding: JSC.Node.Encoding = JSC.Node.Encoding.utf8, - + pub const name = "Bun__ArrayBuffer"; pub const Stream = std.io.FixedBufferStream([]u8); pub inline fn stream(this: ArrayBuffer) Stream { return Stream{ .pos = 0, .buf = this.slice() }; } - pub fn fromTypedArray(ctx: JSC.C.JSContextRef, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ArrayBuffer { - return ArrayBuffer{ - .byte_len = @truncate(u32, JSC.C.JSObjectGetTypedArrayByteLength(ctx, value.asObjectRef(), exception)), - .offset = @truncate(u32, JSC.C.JSObjectGetTypedArrayByteOffset(ctx, value.asObjectRef(), exception)), - .ptr = @ptrCast([*]u8, JSC.C.JSObjectGetArrayBufferBytesPtr(ctx, value.asObjectRef(), exception) orelse - JSC.C.JSObjectGetTypedArrayBytesPtr(ctx, value.asObjectRef(), exception).?), - // TODO - .typed_array_type = js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, - .len = @truncate(u32, JSC.C.JSObjectGetTypedArrayLength(ctx, value.asObjectRef(), exception)), - }; + pub fn fromTypedArray(ctx: JSC.C.JSContextRef, value: JSC.JSValue, _: JSC.C.ExceptionRef) ArrayBuffer { + var out = std.mem.zeroes(ArrayBuffer); + std.debug.assert(value.asArrayBuffer_(ctx.ptr(), &out)); + return out; } - pub fn fromArrayBuffer(ctx: JSC.C.JSContextRef, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ArrayBuffer { - var buffer = ArrayBuffer{ - .byte_len = @truncate(u32, JSC.C.JSObjectGetArrayBufferByteLength(ctx, value.asObjectRef(), exception)), - .ptr = @ptrCast([*]u8, JSC.C.JSObjectGetArrayBufferBytesPtr(ctx, value.asObjectRef(), exception) orelse - JSC.C.JSObjectGetTypedArrayBytesPtr(ctx, value.asObjectRef(), exception).?), - // TODO - .typed_array_type = js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, - .len = 0, - .offset = 0, - }; - buffer.len = buffer.byte_len; - return buffer; + pub fn fromBytes(bytes: []u8, typed_array_type: JSC.JSValue.JSType) ArrayBuffer { + return ArrayBuffer{ .offset = 0, .len = @intCast(u32, bytes.len), .byte_len = @intCast(u32, bytes.len), .typed_array_type = typed_array_type, .ptr = bytes.ptr }; + } + + pub fn toJS(this: ArrayBuffer, ctx: JSC.C.JSContextRef, exception: JSC.C.ExceptionRef) JSC.JSValue { + return JSC.JSValue.fromRef(JSC.C.JSObjectMakeTypedArrayWithBytesNoCopy( + ctx, + this.typed_array_type.toC(), + this.ptr, + this.byte_len, + MarkedArrayBuffer_deallocator, + &_global.default_allocator, + exception, + )); } + pub const fromArrayBuffer = fromTypedArray; + pub inline fn slice(this: *const @This()) []u8 { return this.ptr[this.offset .. this.offset + this.byte_len]; } @@ -1739,7 +1733,7 @@ pub const MarkedArrayBuffer = struct { pub fn fromString(str: []const u8, allocator: std.mem.Allocator) !MarkedArrayBuffer { var buf = try allocator.dupe(u8, str); - return MarkedArrayBuffer.fromBytes(buf, allocator, js.JSTypedArrayType.kJSTypedArrayTypeUint8Array); + return MarkedArrayBuffer.fromBytes(buf, allocator, JSC.JSValue.JSType.Uint8Array); } pub fn fromJS(global: *JSC.JSGlobalObject, value: JSC.JSValue, exception: JSC.C.ExceptionRef) ?MarkedArrayBuffer { @@ -1750,9 +1744,9 @@ pub const MarkedArrayBuffer = struct { }; } - pub fn fromBytes(bytes: []u8, allocator: std.mem.Allocator, typed_array_type: js.JSTypedArrayType) MarkedArrayBuffer { + pub fn fromBytes(bytes: []u8, allocator: std.mem.Allocator, typed_array_type: JSC.JSValue.JSType) MarkedArrayBuffer { return MarkedArrayBuffer{ - .buffer = ArrayBuffer{ .offset = 0, .len = @intCast(u32, bytes.len), .byte_len = @intCast(u32, bytes.len), .typed_array_type = typed_array_type, .ptr = bytes.ptr }, + .buffer = ArrayBuffer.fromBytes(bytes, typed_array_type), .allocator = allocator, }; } @@ -1764,6 +1758,7 @@ pub const MarkedArrayBuffer = struct { pub fn destroy(this: *MarkedArrayBuffer) void { const content = this.*; if (this.allocator) |allocator| { + this.allocator = null; allocator.free(content.buffer.slice()); allocator.destroy(this); } @@ -1776,8 +1771,17 @@ pub const MarkedArrayBuffer = struct { return container; } - pub fn toJSObjectRef(this: *const MarkedArrayBuffer, ctx: js.JSContextRef, exception: js.ExceptionRef) js.JSObjectRef { - return js.JSObjectMakeTypedArrayWithBytesNoCopy(ctx, this.buffer.typed_array_type, this.buffer.ptr, this.buffer.byte_len, MarkedArrayBuffer_deallocator, @intToPtr([*]u8, @ptrToInt(this)), exception); + pub fn toJSObjectRef(this: MarkedArrayBuffer, ctx: js.JSContextRef, exception: js.ExceptionRef) js.JSObjectRef { + return js.JSObjectMakeTypedArrayWithBytesNoCopy( + ctx, + this.buffer.typed_array_type.toC(), + this.buffer.ptr, + + this.buffer.byte_len, + MarkedArrayBuffer_deallocator, + this.buffer.ptr, + exception, + ); } pub const toJS = toJSObjectRef; diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp index 7af7d3a25..c73cb8099 100644 --- a/src/javascript/jsc/bindings/bindings.cpp +++ b/src/javascript/jsc/bindings/bindings.cpp @@ -14,6 +14,7 @@ #include <JavaScriptCore/Identifier.h> #include <JavaScriptCore/IteratorOperations.h> #include <JavaScriptCore/JSArray.h> +#include <JavaScriptCore/JSArrayBuffer.h> #include <JavaScriptCore/JSArrayInlines.h> #include <JavaScriptCore/JSCInlines.h> #include <JavaScriptCore/JSCallbackObject.h> @@ -540,6 +541,41 @@ JSC__JSValue JSC__JSValue__fromEntries(JSC__JSGlobalObject *globalObject, ZigStr return JSC::JSValue::encode(object); } + +bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject *arg1, + Bun__ArrayBuffer *arg2) { + JSC::VM &vm = arg1->vm(); + + JSC::JSValue value = JSC::JSValue::decode(JSValue0); + if (!value.isObject()) { return false; } + + JSC::JSObject *object = value.getObject(); + + if (JSC::JSArrayBufferView *typedArray = + JSC::jsDynamicCast<JSC::JSArrayBufferView *>(vm, object)) { + if (JSC::ArrayBuffer *buffer = typedArray->possiblySharedBuffer()) { + buffer->pinAndLock(); + arg2->ptr = reinterpret_cast<char *>(buffer->data()); + arg2->len = typedArray->length(); + arg2->byte_len = buffer->byteLength(); + arg2->offset = typedArray->byteOffset(); + arg2->cell_type = typedArray->type(); + return true; + } + } + + if (JSC::ArrayBuffer *buffer = JSC::toPossiblySharedArrayBuffer(vm, value)) { + buffer->pinAndLock(); + arg2->ptr = reinterpret_cast<char *>(buffer->data()); + arg2->len = buffer->byteLength(); + arg2->byte_len = buffer->byteLength(); + arg2->offset = 0; + arg2->cell_type = 40; + return true; + } + + return false; +} JSC__JSValue JSC__JSValue__createStringArray(JSC__JSGlobalObject *globalObject, ZigString *arg1, size_t arg2, bool clone) { JSC::VM &vm = globalObject->vm(); diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 4dad9a926..77f2bc20c 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -13,7 +13,7 @@ const ErrorableResolvedSource = Exports.ErrorableResolvedSource; const ZigException = Exports.ZigException; const ZigStackTrace = Exports.ZigStackTrace; const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false; - +const ArrayBuffer = @import("../base.zig").ArrayBuffer; pub const JSObject = extern struct { pub const shim = Shimmer("JSC", "JSObject", @This()); bytes: shim.Bytes, @@ -1429,6 +1429,22 @@ pub const JSValue = enum(i64) { MaxJS = 0b11111111, _, + pub fn toC(this: JSType) C_API.JSTypedArrayType { + return switch (this) { + .Int8Array => .kJSTypedArrayTypeInt8Array, + .Int16Array => .kJSTypedArrayTypeInt16Array, + .Int32Array => .kJSTypedArrayTypeInt32Array, + .Uint8Array => .kJSTypedArrayTypeUint8Array, + .Uint8ClampedArray => .kJSTypedArrayTypeUint8ClampedArray, + .Uint16Array => .kJSTypedArrayTypeUint16Array, + .Uint32Array => .kJSTypedArrayTypeUint32Array, + .Float32Array => .kJSTypedArrayTypeFloat32Array, + .Float64Array => .kJSTypedArrayTypeFloat64Array, + .ArrayBuffer => .kJSTypedArrayTypeArrayBuffer, + else => .kJSTypedArrayTypeNone, + }; + } + pub fn isHidden(this: JSType) bool { return switch (this) { .APIValueWrapper, @@ -1708,6 +1724,16 @@ pub const JSValue = enum(i64) { return cppFn("toZigString", .{ this, out, global }); } + pub fn asArrayBuffer_(this: JSValue, global: *JSGlobalObject, out: *ArrayBuffer) bool { + return cppFn("asArrayBuffer_", .{ this, global, out }); + } + + pub fn asArrayBuffer(this: JSValue, global: *JSGlobalObject) ?ArrayBuffer { + var out: ArrayBuffer = undefined; + if (this.asArrayBuffer_(global, &out)) return out; + return null; + } + pub inline fn getZigString(this: JSValue, global: *JSGlobalObject) ZigString { var str = ZigString.init(""); this.toZigString(&str, global); @@ -1857,7 +1883,7 @@ pub const JSValue = enum(i64) { return @intToPtr(*anyopaque, @bitCast(u64, @enumToInt(this))); } - pub const Extern = [_][]const u8{ "getReadableStreamState", "getWritableStreamState", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" }; + pub const Extern = [_][]const u8{ "asArrayBuffer_", "getReadableStreamState", "getWritableStreamState", "fromEntries", "createTypeError", "createRangeError", "createObject2", "getIfPropertyExistsImpl", "jsType", "jsonStringify", "kind_", "isTerminationException", "isSameValue", "getLengthOfArray", "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" }; }; extern "c" fn Microtask__run(*Microtask, *JSGlobalObject) void; diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index 470feff87..4ea2d08a9 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1642473926 +//-- AUTOGENERATED FILE -- 1642723405 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers-handwritten.h b/src/javascript/jsc/bindings/headers-handwritten.h index cf7c87e4f..ee038999f 100644 --- a/src/javascript/jsc/bindings/headers-handwritten.h +++ b/src/javascript/jsc/bindings/headers-handwritten.h @@ -165,6 +165,14 @@ typedef struct { bool emit_close; } Bun__Writable; +typedef struct { + char *ptr; + uint32_t offset; + uint32_t len; + uint32_t byte_len; + uint8_t cell_type; +} Bun__ArrayBuffer; + #ifdef __cplusplus extern "C" ZigErrorCode Zig_ErrorCodeParserError; diff --git a/src/javascript/jsc/bindings/headers-replacements.zig b/src/javascript/jsc/bindings/headers-replacements.zig index 96c058d56..5f453b64f 100644 --- a/src/javascript/jsc/bindings/headers-replacements.zig +++ b/src/javascript/jsc/bindings/headers-replacements.zig @@ -58,3 +58,4 @@ const JSClassRef = bindings.C.JSClassRef; pub const JSC__CatchScope = bindings.CatchScope; pub const Bun__Readable = bindings.NodeReadableStream; pub const Bun__Writable = bindings.NodeWritableStream; +pub const Bun__ArrayBuffer = bindings.ArrayBuffer; diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index 9db733c02..73979e4dd 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format: off -//-- AUTOGENERATED FILE -- 1642473926 +//-- AUTOGENERATED FILE -- 1642723405 #pragma once #include <stddef.h> @@ -115,6 +115,7 @@ typedef void* JSClassRef; typedef bWTF__StringImpl WTF__StringImpl; // WTF::StringImpl typedef bJSC__VM JSC__VM; // JSC::VM typedef JSClassRef JSClassRef; + typedef Bun__ArrayBuffer Bun__ArrayBuffer; typedef bJSC__JSGlobalObject JSC__JSGlobalObject; // JSC::JSGlobalObject typedef bJSC__JSFunction JSC__JSFunction; // JSC::JSFunction typedef struct JSC__AsyncFunctionPrototype JSC__AsyncFunctionPrototype; // JSC::AsyncFunctionPrototype @@ -192,6 +193,7 @@ typedef void* JSClassRef; typedef Bun__Readable Bun__Readable; typedef Bun__Writable Bun__Writable; typedef JSClassRef JSClassRef; + typedef Bun__ArrayBuffer Bun__ArrayBuffer; typedef ZigException ZigException; typedef ZigString ZigString; typedef int64_t JSC__JSValue; @@ -425,6 +427,7 @@ CPP_DECL size_t WTF__String__length(WTF__String* arg0); #pragma mark - JSC::JSValue +CPP_DECL bool JSC__JSValue__asArrayBuffer_(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, Bun__ArrayBuffer* arg2); CPP_DECL JSC__JSCell* JSC__JSValue__asCell(JSC__JSValue JSValue0); CPP_DECL double JSC__JSValue__asNumber(JSC__JSValue JSValue0); CPP_DECL bJSC__JSObject JSC__JSValue__asObject(JSC__JSValue JSValue0); diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig index 061fe3d17..7ce45a4cc 100644 --- a/src/javascript/jsc/bindings/headers.zig +++ b/src/javascript/jsc/bindings/headers.zig @@ -58,6 +58,7 @@ const JSClassRef = bindings.C.JSClassRef; pub const JSC__CatchScope = bindings.CatchScope; pub const Bun__Readable = bindings.NodeReadableStream; pub const Bun__Writable = bindings.NodeWritableStream; +pub const Bun__ArrayBuffer = bindings.ArrayBuffer; // GENERATED CODE - DO NOT MODIFY BY HAND pub const ptrdiff_t = c_long; @@ -270,6 +271,7 @@ pub extern fn WTF__String__isEmpty(arg0: [*c]WTF__String) bool; pub extern fn WTF__String__isExternal(arg0: [*c]WTF__String) bool; pub extern fn WTF__String__isStatic(arg0: [*c]WTF__String) bool; pub extern fn WTF__String__length(arg0: [*c]WTF__String) usize; +pub extern fn JSC__JSValue__asArrayBuffer_(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]Bun__ArrayBuffer) bool; pub extern fn JSC__JSValue__asCell(JSValue0: JSC__JSValue) [*c]JSC__JSCell; pub extern fn JSC__JSValue__asNumber(JSValue0: JSC__JSValue) f64; pub extern fn JSC__JSValue__asObject(JSValue0: JSC__JSValue) bJSC__JSObject; diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 2299e9339..581d184fa 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -431,7 +431,7 @@ pub const Bun = struct { marked_array_buffer.* = MarkedArrayBuffer.fromBytes( contents_buf[0..contents_len], VirtualMachine.vm.allocator, - js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, + .Uint8Array, ); return marked_array_buffer.toJSObjectRef(ctx, exception); diff --git a/src/javascript/jsc/node/node_fs.zig b/src/javascript/jsc/node/node_fs.zig index a89eb190f..444480d2c 100644 --- a/src/javascript/jsc/node/node_fs.zig +++ b/src/javascript/jsc/node/node_fs.zig @@ -3320,7 +3320,7 @@ pub const NodeFS = struct { return switch (args.encoding) { .buffer => .{ .result = .{ - .buffer = Buffer.fromBytes(buf.items, _global.default_allocator, JSC.C.JSTypedArrayType.kJSTypedArrayTypeUint8Array), + .buffer = Buffer.fromBytes(buf.items, _global.default_allocator, .Uint8Array), }, }, else => .{ diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig index 1ff43bc98..00b78f41c 100644 --- a/src/javascript/jsc/webcore/response.zig +++ b/src/javascript/jsc/webcore/response.zig @@ -238,31 +238,22 @@ pub const Response = struct { switch (this.body.value) { .Unconsumed => { if (this.body.ptr) |_ptr| { - break :brk js.JSObjectMakeTypedArrayWithBytesNoCopy( - ctx, - js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, - _ptr, - this.body.len, - null, - null, - exception, - ); + break :brk JSC.MarkedArrayBuffer.fromBytes(_ptr[0..this.body.len], default_allocator, .ArrayBuffer).toJSObjectRef(ctx, exception); } - break :brk js.JSObjectMakeTypedArray( ctx, - js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, + js.JSTypedArrayType.kJSTypedArrayTypeArrayBuffer, 0, exception, ); }, .Empty => { - break :brk js.JSObjectMakeTypedArray(ctx, js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, 0, exception); + break :brk js.JSObjectMakeTypedArray(ctx, js.JSTypedArrayType.kJSTypedArrayTypeArrayBuffer, 0, exception); }, .String => |str| { break :brk js.JSObjectMakeTypedArrayWithBytesNoCopy( ctx, - js.JSTypedArrayType.kJSTypedArrayTypeUint8Array, + js.JSTypedArrayType.kJSTypedArrayTypeArrayBuffer, @intToPtr([*]u8, @ptrToInt(str.ptr)), str.len, null, @@ -273,7 +264,7 @@ pub const Response = struct { .ArrayBuffer => |buffer| { break :brk js.JSObjectMakeTypedArrayWithBytesNoCopy( ctx, - buffer.typed_array_type, + js.JSTypedArrayType.kJSTypedArrayTypeArrayBuffer, buffer.ptr, buffer.byte_len, null, @@ -1439,9 +1430,12 @@ pub const Body = struct { exception: js.ExceptionRef, ) Body { var body = Body{ .init = Init{ .headers = null, .status_code = 200 }, .value = .{ .Empty = 0 } }; - - switch (js.JSValueGetType(ctx, body_ref)) { - .kJSTypeString => { + const value = JSC.JSValue.fromRef(body_ref); + switch (value.jsType()) { + JSC.JSValue.JSType.String, + JSC.JSValue.JSType.StringObject, + JSC.JSValue.JSType.DerivedStringObject, + => { var allocator = getAllocator(ctx); if (comptime has_init) { @@ -1451,68 +1445,50 @@ pub const Body = struct { } } else |_| {} } + var zig_str = JSC.ZigString.init(""); + value.toZigString(&zig_str, ctx.ptr()); - var wtf_string = JSValue.fromRef(body_ref).toWTFString(ctx.ptr()); - - if (wtf_string.isEmpty()) { - body.value = .{ .String = "" }; - return body; - } - - if (!wtf_string.is8Bit()) { - var js_string = js.JSValueToStringCopy(ctx, body_ref, exception); - defer js.JSStringRelease(js_string); - body.ptr_allocator = default_allocator; - const len = js.JSStringGetLength(js_string); - var body_string = default_allocator.alloc(u8, len + 1) catch unreachable; - body.ptr = body_string.ptr; - body.len = body_string.len; - body.value = .{ .String = body_string.ptr[0..js.JSStringGetUTF8CString(js_string, body_string.ptr, body_string.len)] }; - return body; - } - - var slice = wtf_string.characters8()[0..wtf_string.length()]; - - if (slice.len == 0) { + if (zig_str.len == 0) { body.value = .{ .String = "" }; return body; } body.value = Value{ - .String = slice, + .String = std.fmt.allocPrint(default_allocator, "{}", .{zig_str}) catch unreachable, }; // body.ptr = body. // body.len = body.value.String.len;str.characters8()[0..len] }; return body; }, - .kJSTypeObject => { - const typed_array = js.JSValueGetTypedArrayType(ctx, body_ref, exception); - switch (typed_array) { - js.JSTypedArrayType.kJSTypedArrayTypeNone => {}, - else => { - const buffer = ArrayBuffer{ - .ptr = @ptrCast([*]u8, js.JSObjectGetTypedArrayBytesPtr(ctx, body_ref.?, exception).?), - .offset = @truncate(u32, js.JSObjectGetTypedArrayByteOffset(ctx, body_ref.?, exception)), - .len = @truncate(u32, js.JSObjectGetTypedArrayLength(ctx, body_ref.?, exception)), - .byte_len = @truncate(u32, js.JSObjectGetTypedArrayLength(ctx, body_ref.?, exception)), - .typed_array_type = typed_array, - }; - var allocator = getAllocator(ctx); - - if (comptime has_init) { - if (Init.init(allocator, ctx, init_ref.?)) |maybeInit| { - if (maybeInit) |init_| { - body.init = init_; - } - } else |_| {} + + JSC.JSValue.JSType.ArrayBuffer, + JSC.JSValue.JSType.Int8Array, + JSC.JSValue.JSType.Uint8Array, + JSC.JSValue.JSType.Uint8ClampedArray, + JSC.JSValue.JSType.Int16Array, + JSC.JSValue.JSType.Uint16Array, + JSC.JSValue.JSType.Int32Array, + JSC.JSValue.JSType.Uint32Array, + JSC.JSValue.JSType.Float32Array, + JSC.JSValue.JSType.Float64Array, + JSC.JSValue.JSType.BigInt64Array, + JSC.JSValue.JSType.BigUint64Array, + JSC.JSValue.JSType.DataView, + => { + var allocator = getAllocator(ctx); + + if (comptime has_init) { + if (Init.init(allocator, ctx, init_ref.?)) |maybeInit| { + if (maybeInit) |init_| { + body.init = init_; } - body.value = Value{ .ArrayBuffer = buffer }; - body.ptr = buffer.ptr[buffer.offset..buffer.byte_len].ptr; - body.len = buffer.ptr[buffer.offset..buffer.byte_len].len; - return body; - }, + } else |_| {} } + body.value = Value{ .ArrayBuffer = value.asArrayBuffer(ctx.ptr()).? }; + body.ptr = body.value.ArrayBuffer.ptr[body.value.ArrayBuffer.offset..body.value.ArrayBuffer.byte_len].ptr; + body.len = body.value.ArrayBuffer.ptr[body.value.ArrayBuffer.offset..body.value.ArrayBuffer.byte_len].len; + return body; }, else => {}, } |