diff options
-rw-r--r-- | integration/bunjs-only-snippets/text-encoder.test.js | 208 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/BunBuiltinNames.h | 1 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.cpp | 26 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 27 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/exports.zig | 7 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-handwritten.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.h | 495 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.zig | 2 | ||||
-rw-r--r-- | src/javascript/jsc/webcore.zig | 2 | ||||
-rw-r--r-- | src/javascript/jsc/webcore/encoding.zig | 628 | ||||
-rw-r--r-- | src/jsc.zig | 2 |
12 files changed, 1044 insertions, 358 deletions
diff --git a/integration/bunjs-only-snippets/text-encoder.test.js b/integration/bunjs-only-snippets/text-encoder.test.js index d0dcc2b1b..3b0c1f971 100644 --- a/integration/bunjs-only-snippets/text-encoder.test.js +++ b/integration/bunjs-only-snippets/text-encoder.test.js @@ -1,5 +1,61 @@ import { expect, it, describe } from "bun:test"; +const getByteLength = (str) => { + // returns the byte length of an utf8 string + var s = str.length; + for (var i = str.length - 1; i >= 0; i--) { + var code = str.charCodeAt(i); + if (code > 0x7f && code <= 0x7ff) s++; + else if (code > 0x7ff && code <= 0xffff) s += 2; + if (code >= 0xdc00 && code <= 0xdfff) i--; //trail surrogate + } + return s; +}; + +describe("TextDecoder", () => { + it("should decode ascii text", () => { + const decoder = new TextDecoder("latin1"); + expect(decoder.encoding).toBe("windows-1252"); + expect(decoder.decode(new Uint8Array([0x41, 0x42, 0x43]))).toBe("ABC"); + const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; + expect(decoder.decode(Uint8Array.from(result))).toBe( + String.fromCharCode(...result) + ); + }); + + it("should decode unicode text", () => { + const decoder = new TextDecoder(); + + var text = `❤️ Red Heart`; + + const bytes = [ + 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116, + ]; + const decoded = decoder.decode(Uint8Array.from(bytes)); + expect(decoder.encoding).toBe("utf-8"); + + for (let i = 0; i < text.length; i++) { + expect(decoded.charCodeAt(i)).toBe(text.charCodeAt(i)); + } + expect(decoded).toHaveLength(text.length); + }); + + it("should decode unicode text with multiple consecutive emoji", () => { + const decoder = new TextDecoder(); + const encoder = new TextEncoder(); + + var text = `❤️❤️❤️❤️❤️❤️ Red Heart`; + + text += ` ✨ Sparkles 🔥 Fire 😀 😃 😄 😁 😆 😅 😂 🤣 🥲 ☺️ 😊 😇 🙂 🙃 😉 😌 😍 🥰 😘 😗 😙 😚 😋 😛 😝 😜 🤪 🤨 🧐 🤓 😎 🥸 🤩 🥳 😏 😒 😞 😔 😟 😕 🙁 ☹️ 😣 😖 😫 😩 🥺 😢 😭 😤 😠 😡 🤬 🤯 😳 🥵 🥶 😱 😨 😰`; + + expect(decoder.decode(encoder.encode(text))).toBe(text); + + const bytes = new Uint8Array(getByteLength(text) * 8); + const amount = encoder.encodeInto(text, bytes); + expect(decoder.decode(bytes.subarray(0, amount.written))).toBe(text); + }); +}); + describe("TextEncoder", () => { it("should encode latin1 text", () => { const text = "Hello World!"; @@ -19,39 +75,85 @@ describe("TextEncoder", () => { text += "World!"; const encoder = new TextEncoder(); const encoded = encoder.encode(text); + const into = new Uint8Array(100); + const out = encoder.encodeInto(text, into); + expect(out.read).toBe(text.length); + expect(out.written).toBe(encoded.length); + expect(encoded instanceof Uint8Array).toBe(true); const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]; for (let i = 0; i < result.length; i++) { expect(encoded[i]).toBe(result[i]); + expect(encoded[i]).toBe(into[i]); } - expect(result.length).toBe(encoded.length); + expect(encoded.length).toBe(getByteLength(text)); }); it("should encode utf-16 text", () => { - const text = `❤️ Red Heart - ✨ Sparkles - 🔥 Fire + var text = `❤️ Red Heart + ✨ Sparkles + 🔥 Fire `; - const encoder = new TextEncoder(); - const encoded = encoder.encode(text); - expect(encoded instanceof Uint8Array).toBe(true); - const result = [ - 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116, - 10, 32, 32, 32, 32, 226, 156, 168, 32, 83, 112, 97, 114, 107, 108, 101, - 115, 10, 32, 32, 32, 32, 240, 159, 148, 165, 32, 70, 105, 114, 101, 10, - 32, 32, 32, 32, + var encoder = new TextEncoder(); + var decoder = new TextDecoder(); + expect(decoder.decode(encoder.encode(text))).toBe(text); + }); + + // this test is from a web platform test in WebKit + describe("should use a unicode replacement character for invalid surrogate pairs", () => { + var bad = [ + { + encoding: "utf-16le", + input: [0x00, 0xd8], + expected: "\uFFFD", + name: "lone surrogate lead", + }, + { + encoding: "utf-16le", + input: [0x00, 0xdc], + expected: "\uFFFD", + name: "lone surrogate trail", + }, + { + encoding: "utf-16le", + input: [0x00, 0xd8, 0x00, 0x00], + expected: "\uFFFD\u0000", + name: "unmatched surrogate lead", + }, + { + encoding: "utf-16le", + input: [0x00, 0xdc, 0x00, 0x00], + expected: "\uFFFD\u0000", + name: "unmatched surrogate trail", + }, + { + encoding: "utf-16le", + input: [0x00, 0xdc, 0x00, 0xd8], + expected: "\uFFFD\uFFFD", + name: "swapped surrogate pair", + }, ]; - expect(encoded.length).toBe(result.length); - for (let i = 0; i < result.length; i++) { - expect(encoded[i]).toBe(result[i]); - } + bad.forEach(function (t) { + it(t.encoding + " - " + t.name, () => { + expect( + new TextDecoder(t.encoding).decode(new Uint8Array(t.input)) + ).toBe(t.expected); + }); + // test(function () { + // assert_throws_js(TypeError, function () { + // new TextDecoder(t.encoding, { fatal: true }).decode( + // new Uint8Array(t.input) + // ); + // }); + // }, t.encoding + " - " + t.name + " (fatal flag set)"); + }); }); it("should encode utf-16 rope text", () => { var textReal = `❤️ Red Heart - ✨ Sparkles - 🔥 Fire + ✨ Sparkles + 🔥 Fire `; var a = textReal.split(""); var text = ""; @@ -59,76 +161,22 @@ describe("TextEncoder", () => { text += j; } - const text2 = `❤️ Red Heart - ✨ Sparkles - 🔥 Fire - `; - // expect(text2).toBe(text); - // console.log(text2 === text); + var encoder = new TextEncoder(); - const encoder = new TextEncoder(); - - const encoded = encoder.encode(text); - console.log(text); - console.log(textReal); + var encoded = encoder.encode(text); expect(encoded instanceof Uint8Array).toBe(true); const result = [ 226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116, - 10, 32, 32, 32, 32, 226, 156, 168, 32, 83, 112, 97, 114, 107, 108, 101, - 115, 10, 32, 32, 32, 32, 240, 159, 148, 165, 32, 70, 105, 114, 101, 10, - 32, 32, 32, 32, + 10, 32, 32, 32, 32, 32, 32, 32, 32, 226, 156, 168, 32, 83, 112, 97, 114, + 107, 108, 101, 115, 10, 32, 32, 32, 32, 32, 32, 32, 32, 240, 159, 148, + 165, 32, 70, 105, 114, 101, 10, 32, 32, 32, 32, ]; - const len = Math.min(result.length, encoded.length); + var len = Math.min(result.length, encoded.length); + for (let i = 0; i < len; i++) { expect(encoded[i]).toBe(result[i]); } - - expect(encoded.length).toBe(result.length); + expect(encoded.length).toBe(getByteLength(textReal)); }); - - // it("should use a unicode replacement character for invalid surrogate pairs", () => { - // var bad = [ - // { - // input: "\uD800", - // expected: "\uFFFD", - // name: "lone surrogate lead", - // }, - // { - // input: "\uDC00", - // expected: "\uFFFD", - // name: "lone surrogate trail", - // }, - // { - // input: "\uD800\u0000", - // expected: "\uFFFD\u0000", - // name: "unmatched surrogate lead", - // }, - // { - // input: "\uDC00\u0000", - // expected: "\uFFFD\u0000", - // name: "unmatched surrogate trail", - // }, - // { - // input: "\uDC00\uD800", - // expected: "\uFFFD\uFFFD", - // name: "swapped surrogate pair", - // }, - // { - // input: "\uD834\uDD1E", - // expected: "\uD834\uDD1E", - // name: "properly encoded MUSICAL SYMBOL G CLEF (U+1D11E)", - // }, - // ]; - - // const encoder = new TextEncoder(); - // for (var i = 0; i < bad.length; i++) { - // const input = encoder.encode(bad[i].input); - // const output = encoder.encode(bad[i].expected); - // for (let j = 0; j < input.length; j++) { - // expect(input[j]).toBe(output[j]); - // console.log(input[j], output[j]); - // } - // } - // }); }); diff --git a/src/javascript/jsc/bindings/BunBuiltinNames.h b/src/javascript/jsc/bindings/BunBuiltinNames.h index 86b5a8675..5600f9675 100644 --- a/src/javascript/jsc/bindings/BunBuiltinNames.h +++ b/src/javascript/jsc/bindings/BunBuiltinNames.h @@ -70,6 +70,7 @@ using namespace JSC; macro(sep) \ macro(delimiter) \ macro(toNamespacedPath) \ + macro(isWindows) \ BUN_ADDITIONAL_PRIVATE_IDENTIFIERS(macro) \ class BunBuiltinNames { diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp index 4a0b14737..589a19293 100644 --- a/src/javascript/jsc/bindings/bindings.cpp +++ b/src/javascript/jsc/bindings/bindings.cpp @@ -732,6 +732,32 @@ JSC__JSValue ZigString__to16BitValue(const ZigString* arg0, JSC__JSGlobalObject* return JSC::JSValue::encode(JSC::JSValue(JSC::jsString(arg1->vm(), str))); } +void free_global_string(WTF::ExternalStringImpl* str, void* ptr, unsigned len) +{ + ZigString__free_global(reinterpret_cast<const unsigned char*>(ptr), len); +} + +JSC__JSValue ZigString__toExternalU16(const uint16_t* arg0, size_t len, JSC__JSGlobalObject* global) +{ + return JSC::JSValue::encode(JSC::JSValue(JSC::jsOwnedString( + global->vm(), + ExternalStringImpl::create(reinterpret_cast<const UChar*>(arg0), len, free_global_string)))); +} +// This must be a globally allocated string +JSC__JSValue ZigString__toExternalValue(const ZigString* arg0, JSC__JSGlobalObject* arg1) +{ + ZigString str = *arg0; + if (Zig::isTaggedUTF16Ptr(str.ptr)) { + return JSC::JSValue::encode(JSC::JSValue(JSC::jsOwnedString( + arg1->vm(), + ExternalStringImpl::create(reinterpret_cast<const UChar*>(Zig::untag(str.ptr)), str.len, free_global_string)))); + } + + return JSC::JSValue::encode(JSC::JSValue(JSC::jsOwnedString( + arg1->vm(), + ExternalStringImpl::create(Zig::untag(str.ptr), str.len, free_global_string)))); +} + JSC__JSValue ZigString__toValueGC(const ZigString* arg0, JSC__JSGlobalObject* arg1) { return JSC::JSValue::encode(JSC::JSValue(JSC::jsString(arg1->vm(), Zig::toStringCopy(*arg0)))); diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 3efb23323..8687352fc 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -101,6 +101,10 @@ pub const ZigString = extern struct { return this.ptr[0..this.len]; } + pub fn mut(this: Slice) []u8 { + return @intToPtr([*]u8, @ptrToInt(this.ptr))[0..this.len]; + } + pub fn deinit(this: *Slice) void { if (!this.allocated) { return; @@ -162,18 +166,23 @@ pub const ZigString = extern struct { } pub fn detectEncoding(this: *ZigString) void { - for (this.slice()) |char| { - if (char > 127) { - this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 63)); - return; - } + if (!strings.isAllASCII(this.slice())) { + this.markUTF16(); } } + pub fn toExternalU16(ptr: [*]const u16, len: usize, global: *JSGlobalObject) JSValue { + return shim.cppFn("toExternalU16", .{ ptr, len, global }); + } + pub fn markUTF8(this: *ZigString) void { this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 61)); } + pub fn markUTF16(this: *ZigString) void { + this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 63)); + } + pub fn setOutputEncoding(this: *ZigString) void { if (!this.is16Bit()) this.detectEncoding(); if (this.is16Bit()) this.markUTF8(); @@ -262,6 +271,10 @@ pub const ZigString = extern struct { return shim.cppFn("toValue", .{ this, global }); } + pub fn toExternalValue(this: *const ZigString, global: *JSGlobalObject) JSValue { + return shim.cppFn("toExternalValue", .{ this, global }); + } + pub fn to16BitValue(this: *const ZigString, global: *JSGlobalObject) JSValue { return shim.cppFn("to16BitValue", .{ this, global }); } @@ -293,9 +306,11 @@ pub const ZigString = extern struct { pub const Extern = [_][]const u8{ "toValue", + "toExternalValue", "to16BitValue", "toValueGC", "toErrorInstance", + "toExternalU16", }; }; @@ -1662,7 +1677,7 @@ pub const JSValue = enum(i64) { c_uint, u32 => if (number < std.math.maxInt(i32)) jsNumberFromInt32(@intCast(i32, number)) else - jsNumberFromUint64(@as(u64, number)), + jsNumberFromUint64(@intCast(u64, number)), else => @compileError("Type transformation missing for number of type: " ++ @typeName(Number)), }; diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index c79ca68d8..6179ad203 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -236,6 +236,11 @@ export fn ZigString__free(ptr: [*]const u8, len: usize, allocator_: ?*anyopaque) allocator.free(str); } +export fn ZigString__free_global(ptr: [*]const u8, len: usize) void { + var str = ptr[0..len]; + default_allocator.free(str); +} + pub const JSErrorCode = enum(u8) { Error = 0, EvalError = 1, @@ -1854,5 +1859,7 @@ comptime { std.testing.refAllDecls(Bun.Timer); std.testing.refAllDecls(NodeWritableStream); std.testing.refAllDecls(NodePath); + _ = ZigString__free; + _ = ZigString__free_global; } } diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index 3a22c10bb..f99e629e7 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1645447009 +//-- AUTOGENERATED FILE -- 1645667010 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers-handwritten.h b/src/javascript/jsc/bindings/headers-handwritten.h index 26eee4407..6a92f1666 100644 --- a/src/javascript/jsc/bindings/headers-handwritten.h +++ b/src/javascript/jsc/bindings/headers-handwritten.h @@ -192,4 +192,6 @@ extern "C" const char* Bun__versions_boringssl; extern "C" const char* Bun__versions_zlib; extern "C" const char* Bun__versions_zig; +extern "C" void ZigString__free_global(const unsigned char* ptr, size_t len); + #endif diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index a742dba7d..c7d4cf21b 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,17 +1,17 @@ // clang-format: off -//-- AUTOGENERATED FILE -- 1645447009 +//-- AUTOGENERATED FILE -- 1645667010 #pragma once -#include <stdbool.h> #include <stddef.h> #include <stdint.h> +#include <stdbool.h> #ifdef __cplusplus -#define AUTO_EXTERN_C extern "C" -#define AUTO_EXTERN_C_ZIG extern "C" __attribute__((weak)) + #define AUTO_EXTERN_C extern "C" + #define AUTO_EXTERN_C_ZIG extern "C" __attribute__((weak)) #else -#define AUTO_EXTERN_C -#define AUTO_EXTERN_C_ZIG __attribute__((weak)) + #define AUTO_EXTERN_C + #define AUTO_EXTERN_C_ZIG __attribute__((weak)) #endif #define ZIG_DECL AUTO_EXTERN_C_ZIG #define CPP_DECL AUTO_EXTERN_C @@ -26,282 +26,235 @@ typedef void* JSClassRef; #include <JavaScriptCore/JSClassRef.h> #endif #include "headers-handwritten.h" -typedef struct bJSC__JSModuleRecord { - unsigned char bytes[216]; -} bJSC__JSModuleRecord; -typedef char* bJSC__JSModuleRecord_buf; -typedef struct bJSC__ThrowScope { - unsigned char bytes[8]; -} bJSC__ThrowScope; -typedef char* bJSC__ThrowScope_buf; -typedef struct bJSC__CallFrame { - unsigned char bytes[8]; -} bJSC__CallFrame; -typedef char* bJSC__CallFrame_buf; -typedef struct bJSC__PropertyName { - unsigned char bytes[8]; -} bJSC__PropertyName; -typedef char* bJSC__PropertyName_buf; -typedef struct bJSC__CatchScope { - unsigned char bytes[8]; -} bJSC__CatchScope; -typedef char* bJSC__CatchScope_buf; -typedef struct bWTF__String { - unsigned char bytes[8]; -} bWTF__String; -typedef char* bWTF__String_buf; -typedef struct bWTF__StringView { - unsigned char bytes[16]; -} bWTF__StringView; -typedef char* bWTF__StringView_buf; -typedef struct bJSC__JSModuleLoader { - unsigned char bytes[16]; -} bJSC__JSModuleLoader; -typedef char* bJSC__JSModuleLoader_buf; -typedef struct bJSC__Exception { - unsigned char bytes[40]; -} bJSC__Exception; -typedef char* bJSC__Exception_buf; -typedef struct bJSC__VM { - unsigned char bytes[48824]; -} bJSC__VM; -typedef char* bJSC__VM_buf; -typedef struct bJSC__JSString { - unsigned char bytes[16]; -} bJSC__JSString; -typedef char* bJSC__JSString_buf; -typedef struct bJSC__SourceOrigin { - unsigned char bytes[48]; -} bJSC__SourceOrigin; -typedef char* bJSC__SourceOrigin_buf; -typedef struct bWTF__ExternalStringImpl { - unsigned char bytes[32]; -} bWTF__ExternalStringImpl; -typedef char* bWTF__ExternalStringImpl_buf; -typedef struct bWTF__StringImpl { - unsigned char bytes[24]; -} bWTF__StringImpl; -typedef char* bWTF__StringImpl_buf; -typedef struct bJSC__SourceCode { - unsigned char bytes[24]; -} bJSC__SourceCode; -typedef char* bJSC__SourceCode_buf; -typedef struct bJSC__JSPromise { - unsigned char bytes[32]; -} bJSC__JSPromise; -typedef char* bJSC__JSPromise_buf; -typedef struct bWTF__URL { - unsigned char bytes[40]; -} bWTF__URL; -typedef char* bWTF__URL_buf; -typedef struct bJSC__JSFunction { - unsigned char bytes[32]; -} bJSC__JSFunction; -typedef char* bJSC__JSFunction_buf; -typedef struct bJSC__JSGlobalObject { - unsigned char bytes[2400]; -} bJSC__JSGlobalObject; -typedef char* bJSC__JSGlobalObject_buf; -typedef struct bJSC__JSCell { - unsigned char bytes[8]; -} bJSC__JSCell; -typedef char* bJSC__JSCell_buf; -typedef struct bJSC__JSLock { - unsigned char bytes[40]; -} bJSC__JSLock; -typedef char* bJSC__JSLock_buf; -typedef struct bInspector__ScriptArguments { - unsigned char bytes[32]; -} bInspector__ScriptArguments; -typedef char* bInspector__ScriptArguments_buf; -typedef struct bJSC__JSInternalPromise { - unsigned char bytes[32]; -} bJSC__JSInternalPromise; -typedef char* bJSC__JSInternalPromise_buf; -typedef struct bJSC__JSObject { - unsigned char bytes[16]; -} bJSC__JSObject; -typedef char* bJSC__JSObject_buf; -typedef struct bJSC__Identifier { - unsigned char bytes[8]; -} bJSC__Identifier; -typedef char* bJSC__Identifier_buf; + typedef struct bJSC__JSModuleRecord { unsigned char bytes[216]; } bJSC__JSModuleRecord; + typedef char* bJSC__JSModuleRecord_buf; + typedef struct bJSC__ThrowScope { unsigned char bytes[8]; } bJSC__ThrowScope; + typedef char* bJSC__ThrowScope_buf; + typedef struct bJSC__CallFrame { unsigned char bytes[8]; } bJSC__CallFrame; + typedef char* bJSC__CallFrame_buf; + typedef struct bJSC__PropertyName { unsigned char bytes[8]; } bJSC__PropertyName; + typedef char* bJSC__PropertyName_buf; + typedef struct bJSC__CatchScope { unsigned char bytes[8]; } bJSC__CatchScope; + typedef char* bJSC__CatchScope_buf; + typedef struct bWTF__String { unsigned char bytes[8]; } bWTF__String; + typedef char* bWTF__String_buf; + typedef struct bWTF__StringView { unsigned char bytes[16]; } bWTF__StringView; + typedef char* bWTF__StringView_buf; + typedef struct bJSC__JSModuleLoader { unsigned char bytes[16]; } bJSC__JSModuleLoader; + typedef char* bJSC__JSModuleLoader_buf; + typedef struct bJSC__Exception { unsigned char bytes[40]; } bJSC__Exception; + typedef char* bJSC__Exception_buf; + typedef struct bJSC__VM { unsigned char bytes[48824]; } bJSC__VM; + typedef char* bJSC__VM_buf; + typedef struct bJSC__JSString { unsigned char bytes[16]; } bJSC__JSString; + typedef char* bJSC__JSString_buf; + typedef struct bJSC__SourceOrigin { unsigned char bytes[48]; } bJSC__SourceOrigin; + typedef char* bJSC__SourceOrigin_buf; + typedef struct bWTF__ExternalStringImpl { unsigned char bytes[32]; } bWTF__ExternalStringImpl; + typedef char* bWTF__ExternalStringImpl_buf; + typedef struct bWTF__StringImpl { unsigned char bytes[24]; } bWTF__StringImpl; + typedef char* bWTF__StringImpl_buf; + typedef struct bJSC__SourceCode { unsigned char bytes[24]; } bJSC__SourceCode; + typedef char* bJSC__SourceCode_buf; + typedef struct bJSC__JSPromise { unsigned char bytes[32]; } bJSC__JSPromise; + typedef char* bJSC__JSPromise_buf; + typedef struct bWTF__URL { unsigned char bytes[40]; } bWTF__URL; + typedef char* bWTF__URL_buf; + typedef struct bJSC__JSFunction { unsigned char bytes[32]; } bJSC__JSFunction; + typedef char* bJSC__JSFunction_buf; + typedef struct bJSC__JSGlobalObject { unsigned char bytes[2400]; } bJSC__JSGlobalObject; + typedef char* bJSC__JSGlobalObject_buf; + typedef struct bJSC__JSCell { unsigned char bytes[8]; } bJSC__JSCell; + typedef char* bJSC__JSCell_buf; + typedef struct bJSC__JSLock { unsigned char bytes[40]; } bJSC__JSLock; + typedef char* bJSC__JSLock_buf; + typedef struct bInspector__ScriptArguments { unsigned char bytes[32]; } bInspector__ScriptArguments; + typedef char* bInspector__ScriptArguments_buf; + typedef struct bJSC__JSInternalPromise { unsigned char bytes[32]; } bJSC__JSInternalPromise; + typedef char* bJSC__JSInternalPromise_buf; + typedef struct bJSC__JSObject { unsigned char bytes[16]; } bJSC__JSObject; + typedef char* bJSC__JSObject_buf; + typedef struct bJSC__Identifier { unsigned char bytes[8]; } bJSC__Identifier; + typedef char* bJSC__Identifier_buf; #ifndef __cplusplus -typedef bJSC__CatchScope JSC__CatchScope; // JSC::CatchScope -typedef struct JSC__GeneratorPrototype JSC__GeneratorPrototype; // JSC::GeneratorPrototype -typedef struct JSC__ArrayIteratorPrototype JSC__ArrayIteratorPrototype; // JSC::ArrayIteratorPrototype -typedef ErrorableResolvedSource ErrorableResolvedSource; -typedef struct JSC__JSPromisePrototype JSC__JSPromisePrototype; // JSC::JSPromisePrototype -typedef ErrorableZigString ErrorableZigString; -typedef bJSC__PropertyName JSC__PropertyName; // JSC::PropertyName -typedef bJSC__JSObject JSC__JSObject; // JSC::JSObject -typedef bWTF__ExternalStringImpl WTF__ExternalStringImpl; // WTF::ExternalStringImpl -typedef struct JSC__AsyncIteratorPrototype JSC__AsyncIteratorPrototype; // JSC::AsyncIteratorPrototype -typedef bJSC__JSLock JSC__JSLock; // JSC::JSLock -typedef bJSC__JSModuleLoader JSC__JSModuleLoader; // JSC::JSModuleLoader -typedef struct JSC__AsyncGeneratorPrototype JSC__AsyncGeneratorPrototype; // JSC::AsyncGeneratorPrototype -typedef struct JSC__AsyncGeneratorFunctionPrototype JSC__AsyncGeneratorFunctionPrototype; // JSC::AsyncGeneratorFunctionPrototype -typedef bJSC__Identifier JSC__Identifier; // JSC::Identifier -typedef struct JSC__ArrayPrototype JSC__ArrayPrototype; // JSC::ArrayPrototype -typedef struct Zig__JSMicrotaskCallback Zig__JSMicrotaskCallback; // Zig::JSMicrotaskCallback -typedef bJSC__JSPromise JSC__JSPromise; // JSC::JSPromise -typedef struct JSC__SetIteratorPrototype JSC__SetIteratorPrototype; // JSC::SetIteratorPrototype -typedef SystemError SystemError; -typedef bJSC__JSCell JSC__JSCell; // JSC::JSCell -typedef bJSC__SourceOrigin JSC__SourceOrigin; // JSC::SourceOrigin -typedef bJSC__JSModuleRecord JSC__JSModuleRecord; // JSC::JSModuleRecord -typedef bWTF__String WTF__String; // WTF::String -typedef bWTF__URL WTF__URL; // WTF::URL -typedef struct JSC__IteratorPrototype JSC__IteratorPrototype; // JSC::IteratorPrototype -typedef Bun__Readable Bun__Readable; -typedef bJSC__JSInternalPromise JSC__JSInternalPromise; // JSC::JSInternalPromise -typedef Bun__Writable Bun__Writable; -typedef struct JSC__RegExpPrototype JSC__RegExpPrototype; // JSC::RegExpPrototype -typedef bJSC__CallFrame JSC__CallFrame; // JSC::CallFrame -typedef struct JSC__MapIteratorPrototype JSC__MapIteratorPrototype; // JSC::MapIteratorPrototype -typedef bWTF__StringView WTF__StringView; // WTF::StringView -typedef bJSC__ThrowScope JSC__ThrowScope; // JSC::ThrowScope -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 -typedef ZigException ZigException; -typedef bJSC__SourceCode JSC__SourceCode; // JSC::SourceCode -typedef struct JSC__BigIntPrototype JSC__BigIntPrototype; // JSC::BigIntPrototype -typedef struct JSC__GeneratorFunctionPrototype JSC__GeneratorFunctionPrototype; // JSC::GeneratorFunctionPrototype -typedef ZigString ZigString; -typedef int64_t JSC__JSValue; -typedef struct JSC__FunctionPrototype JSC__FunctionPrototype; // JSC::FunctionPrototype -typedef bInspector__ScriptArguments Inspector__ScriptArguments; // Inspector::ScriptArguments -typedef bJSC__Exception JSC__Exception; // JSC::Exception -typedef bJSC__JSString JSC__JSString; // JSC::JSString -typedef struct JSC__ObjectPrototype JSC__ObjectPrototype; // JSC::ObjectPrototype -typedef struct JSC__StringPrototype JSC__StringPrototype; // JSC::StringPrototype + typedef bJSC__CatchScope JSC__CatchScope; // JSC::CatchScope + typedef struct JSC__GeneratorPrototype JSC__GeneratorPrototype; // JSC::GeneratorPrototype + typedef struct JSC__ArrayIteratorPrototype JSC__ArrayIteratorPrototype; // JSC::ArrayIteratorPrototype + typedef ErrorableResolvedSource ErrorableResolvedSource; + typedef struct JSC__JSPromisePrototype JSC__JSPromisePrototype; // JSC::JSPromisePrototype + typedef ErrorableZigString ErrorableZigString; + typedef bJSC__PropertyName JSC__PropertyName; // JSC::PropertyName + typedef bJSC__JSObject JSC__JSObject; // JSC::JSObject + typedef bWTF__ExternalStringImpl WTF__ExternalStringImpl; // WTF::ExternalStringImpl + typedef struct JSC__AsyncIteratorPrototype JSC__AsyncIteratorPrototype; // JSC::AsyncIteratorPrototype + typedef bJSC__JSLock JSC__JSLock; // JSC::JSLock + typedef bJSC__JSModuleLoader JSC__JSModuleLoader; // JSC::JSModuleLoader + typedef struct JSC__AsyncGeneratorPrototype JSC__AsyncGeneratorPrototype; // JSC::AsyncGeneratorPrototype + typedef struct JSC__AsyncGeneratorFunctionPrototype JSC__AsyncGeneratorFunctionPrototype; // JSC::AsyncGeneratorFunctionPrototype + typedef bJSC__Identifier JSC__Identifier; // JSC::Identifier + typedef struct JSC__ArrayPrototype JSC__ArrayPrototype; // JSC::ArrayPrototype + typedef struct Zig__JSMicrotaskCallback Zig__JSMicrotaskCallback; // Zig::JSMicrotaskCallback + typedef bJSC__JSPromise JSC__JSPromise; // JSC::JSPromise + typedef struct JSC__SetIteratorPrototype JSC__SetIteratorPrototype; // JSC::SetIteratorPrototype + typedef SystemError SystemError; + typedef bJSC__JSCell JSC__JSCell; // JSC::JSCell + typedef bJSC__SourceOrigin JSC__SourceOrigin; // JSC::SourceOrigin + typedef bJSC__JSModuleRecord JSC__JSModuleRecord; // JSC::JSModuleRecord + typedef bWTF__String WTF__String; // WTF::String + typedef bWTF__URL WTF__URL; // WTF::URL + typedef struct JSC__IteratorPrototype JSC__IteratorPrototype; // JSC::IteratorPrototype + typedef Bun__Readable Bun__Readable; + typedef bJSC__JSInternalPromise JSC__JSInternalPromise; // JSC::JSInternalPromise + typedef Bun__Writable Bun__Writable; + typedef struct JSC__RegExpPrototype JSC__RegExpPrototype; // JSC::RegExpPrototype + typedef bJSC__CallFrame JSC__CallFrame; // JSC::CallFrame + typedef struct JSC__MapIteratorPrototype JSC__MapIteratorPrototype; // JSC::MapIteratorPrototype + typedef bWTF__StringView WTF__StringView; // WTF::StringView + typedef bJSC__ThrowScope JSC__ThrowScope; // JSC::ThrowScope + 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 + typedef ZigException ZigException; + typedef bJSC__SourceCode JSC__SourceCode; // JSC::SourceCode + typedef struct JSC__BigIntPrototype JSC__BigIntPrototype; // JSC::BigIntPrototype + typedef struct JSC__GeneratorFunctionPrototype JSC__GeneratorFunctionPrototype; // JSC::GeneratorFunctionPrototype + typedef ZigString ZigString; + typedef int64_t JSC__JSValue; + typedef struct JSC__FunctionPrototype JSC__FunctionPrototype; // JSC::FunctionPrototype + typedef bInspector__ScriptArguments Inspector__ScriptArguments; // Inspector::ScriptArguments + typedef bJSC__Exception JSC__Exception; // JSC::Exception + typedef bJSC__JSString JSC__JSString; // JSC::JSString + typedef struct JSC__ObjectPrototype JSC__ObjectPrototype; // JSC::ObjectPrototype + typedef struct JSC__StringPrototype JSC__StringPrototype; // JSC::StringPrototype #endif #ifdef __cplusplus -namespace JSC { -class JSCell; -class Exception; -class JSPromisePrototype; -class StringPrototype; -class GeneratorFunctionPrototype; -class ArrayPrototype; -class JSString; -class JSObject; -class AsyncIteratorPrototype; -class AsyncGeneratorFunctionPrototype; -class Identifier; -class JSPromise; -class RegExpPrototype; -class AsyncFunctionPrototype; -class CatchScope; -class VM; -class BigIntPrototype; -class SourceOrigin; -class ThrowScope; -class SetIteratorPrototype; -class AsyncGeneratorPrototype; -class PropertyName; -class MapIteratorPrototype; -class JSModuleRecord; -class JSInternalPromise; -class ArrayIteratorPrototype; -class JSFunction; -class JSModuleLoader; -class GeneratorPrototype; -class JSGlobalObject; -class SourceCode; -class JSLock; -class FunctionPrototype; -class IteratorPrototype; -class CallFrame; -class ObjectPrototype; -} -namespace WTF { -class URL; -class StringImpl; -class String; -class StringView; -class ExternalStringImpl; -} -namespace Zig { -class JSMicrotaskCallback; -} -namespace Inspector { -class ScriptArguments; -} - -typedef ErrorableResolvedSource ErrorableResolvedSource; -typedef ErrorableZigString ErrorableZigString; -typedef SystemError SystemError; -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; -using JSC__JSCell = JSC::JSCell; -using JSC__Exception = JSC::Exception; -using JSC__JSPromisePrototype = JSC::JSPromisePrototype; -using JSC__StringPrototype = JSC::StringPrototype; -using JSC__GeneratorFunctionPrototype = JSC::GeneratorFunctionPrototype; -using JSC__ArrayPrototype = JSC::ArrayPrototype; -using JSC__JSString = JSC::JSString; -using JSC__JSObject = JSC::JSObject; -using JSC__AsyncIteratorPrototype = JSC::AsyncIteratorPrototype; -using JSC__AsyncGeneratorFunctionPrototype = JSC::AsyncGeneratorFunctionPrototype; -using JSC__Identifier = JSC::Identifier; -using JSC__JSPromise = JSC::JSPromise; -using JSC__RegExpPrototype = JSC::RegExpPrototype; -using JSC__AsyncFunctionPrototype = JSC::AsyncFunctionPrototype; -using JSC__CatchScope = JSC::CatchScope; -using JSC__VM = JSC::VM; -using JSC__BigIntPrototype = JSC::BigIntPrototype; -using JSC__SourceOrigin = JSC::SourceOrigin; -using JSC__ThrowScope = JSC::ThrowScope; -using JSC__SetIteratorPrototype = JSC::SetIteratorPrototype; -using JSC__AsyncGeneratorPrototype = JSC::AsyncGeneratorPrototype; -using JSC__PropertyName = JSC::PropertyName; -using JSC__MapIteratorPrototype = JSC::MapIteratorPrototype; -using JSC__JSModuleRecord = JSC::JSModuleRecord; -using JSC__JSInternalPromise = JSC::JSInternalPromise; -using JSC__ArrayIteratorPrototype = JSC::ArrayIteratorPrototype; -using JSC__JSFunction = JSC::JSFunction; -using JSC__JSModuleLoader = JSC::JSModuleLoader; -using JSC__GeneratorPrototype = JSC::GeneratorPrototype; -using JSC__JSGlobalObject = JSC::JSGlobalObject; -using JSC__SourceCode = JSC::SourceCode; -using JSC__JSLock = JSC::JSLock; -using JSC__FunctionPrototype = JSC::FunctionPrototype; -using JSC__IteratorPrototype = JSC::IteratorPrototype; -using JSC__CallFrame = JSC::CallFrame; -using JSC__ObjectPrototype = JSC::ObjectPrototype; -using WTF__URL = WTF::URL; -using WTF__StringImpl = WTF::StringImpl; -using WTF__String = WTF::String; -using WTF__StringView = WTF::StringView; -using WTF__ExternalStringImpl = WTF::ExternalStringImpl; -using Zig__JSMicrotaskCallback = Zig::JSMicrotaskCallback; -using Inspector__ScriptArguments = Inspector::ScriptArguments; + namespace JSC { + class JSCell; + class Exception; + class JSPromisePrototype; + class StringPrototype; + class GeneratorFunctionPrototype; + class ArrayPrototype; + class JSString; + class JSObject; + class AsyncIteratorPrototype; + class AsyncGeneratorFunctionPrototype; + class Identifier; + class JSPromise; + class RegExpPrototype; + class AsyncFunctionPrototype; + class CatchScope; + class VM; + class BigIntPrototype; + class SourceOrigin; + class ThrowScope; + class SetIteratorPrototype; + class AsyncGeneratorPrototype; + class PropertyName; + class MapIteratorPrototype; + class JSModuleRecord; + class JSInternalPromise; + class ArrayIteratorPrototype; + class JSFunction; + class JSModuleLoader; + class GeneratorPrototype; + class JSGlobalObject; + class SourceCode; + class JSLock; + class FunctionPrototype; + class IteratorPrototype; + class CallFrame; + class ObjectPrototype; + } + namespace WTF { + class URL; + class StringImpl; + class String; + class StringView; + class ExternalStringImpl; + } + namespace Zig { + class JSMicrotaskCallback; + } + namespace Inspector { + class ScriptArguments; + } + + typedef ErrorableResolvedSource ErrorableResolvedSource; + typedef ErrorableZigString ErrorableZigString; + typedef SystemError SystemError; + 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; + using JSC__JSCell = JSC::JSCell; + using JSC__Exception = JSC::Exception; + using JSC__JSPromisePrototype = JSC::JSPromisePrototype; + using JSC__StringPrototype = JSC::StringPrototype; + using JSC__GeneratorFunctionPrototype = JSC::GeneratorFunctionPrototype; + using JSC__ArrayPrototype = JSC::ArrayPrototype; + using JSC__JSString = JSC::JSString; + using JSC__JSObject = JSC::JSObject; + using JSC__AsyncIteratorPrototype = JSC::AsyncIteratorPrototype; + using JSC__AsyncGeneratorFunctionPrototype = JSC::AsyncGeneratorFunctionPrototype; + using JSC__Identifier = JSC::Identifier; + using JSC__JSPromise = JSC::JSPromise; + using JSC__RegExpPrototype = JSC::RegExpPrototype; + using JSC__AsyncFunctionPrototype = JSC::AsyncFunctionPrototype; + using JSC__CatchScope = JSC::CatchScope; + using JSC__VM = JSC::VM; + using JSC__BigIntPrototype = JSC::BigIntPrototype; + using JSC__SourceOrigin = JSC::SourceOrigin; + using JSC__ThrowScope = JSC::ThrowScope; + using JSC__SetIteratorPrototype = JSC::SetIteratorPrototype; + using JSC__AsyncGeneratorPrototype = JSC::AsyncGeneratorPrototype; + using JSC__PropertyName = JSC::PropertyName; + using JSC__MapIteratorPrototype = JSC::MapIteratorPrototype; + using JSC__JSModuleRecord = JSC::JSModuleRecord; + using JSC__JSInternalPromise = JSC::JSInternalPromise; + using JSC__ArrayIteratorPrototype = JSC::ArrayIteratorPrototype; + using JSC__JSFunction = JSC::JSFunction; + using JSC__JSModuleLoader = JSC::JSModuleLoader; + using JSC__GeneratorPrototype = JSC::GeneratorPrototype; + using JSC__JSGlobalObject = JSC::JSGlobalObject; + using JSC__SourceCode = JSC::SourceCode; + using JSC__JSLock = JSC::JSLock; + using JSC__FunctionPrototype = JSC::FunctionPrototype; + using JSC__IteratorPrototype = JSC::IteratorPrototype; + using JSC__CallFrame = JSC::CallFrame; + using JSC__ObjectPrototype = JSC::ObjectPrototype; + using WTF__URL = WTF::URL; + using WTF__StringImpl = WTF::StringImpl; + using WTF__String = WTF::String; + using WTF__StringView = WTF::StringView; + using WTF__ExternalStringImpl = WTF::ExternalStringImpl; + using Zig__JSMicrotaskCallback = Zig::JSMicrotaskCallback; + using Inspector__ScriptArguments = Inspector::ScriptArguments; #endif + #pragma mark - JSC::JSObject -CPP_DECL JSC__JSValue JSC__JSObject__create(JSC__JSGlobalObject* arg0, size_t arg1, void* arg2, void (*ArgFn3)(void* arg0, JSC__JSObject* arg1, JSC__JSGlobalObject* arg2)); +CPP_DECL JSC__JSValue JSC__JSObject__create(JSC__JSGlobalObject* arg0, size_t arg1, void* arg2, void (* ArgFn3)(void* arg0, JSC__JSObject* arg1, JSC__JSGlobalObject* arg2)); CPP_DECL size_t JSC__JSObject__getArrayLength(JSC__JSObject* arg0); CPP_DECL JSC__JSValue JSC__JSObject__getDirect(JSC__JSObject* arg0, JSC__JSGlobalObject* arg1, const ZigString* arg2); CPP_DECL JSC__JSValue JSC__JSObject__getIndex(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, uint32_t arg2); CPP_DECL void JSC__JSObject__putRecord(JSC__JSObject* arg0, JSC__JSGlobalObject* arg1, ZigString* arg2, ZigString* arg3, size_t arg4); CPP_DECL JSC__JSValue ZigString__to16BitValue(const ZigString* arg0, JSC__JSGlobalObject* arg1); CPP_DECL JSC__JSValue ZigString__toErrorInstance(const ZigString* arg0, JSC__JSGlobalObject* arg1); +CPP_DECL JSC__JSValue ZigString__toExternalU16(const uint16_t* arg0, size_t arg1, JSC__JSGlobalObject* arg2); +CPP_DECL JSC__JSValue ZigString__toExternalValue(const ZigString* arg0, JSC__JSGlobalObject* arg1); CPP_DECL JSC__JSValue ZigString__toValue(const ZigString* arg0, JSC__JSGlobalObject* arg1); CPP_DECL JSC__JSValue ZigString__toValueGC(const ZigString* arg0, JSC__JSGlobalObject* arg1); CPP_DECL JSC__JSValue SystemError__toErrorInstance(const SystemError* arg0, JSC__JSGlobalObject* arg1); @@ -392,7 +345,7 @@ CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArguments(JSC__JSValue JSVal CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArgumentsAndNewTarget(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2, JSC__JSValue* arg3, size_t arg4, JSC__Exception** arg5, const unsigned char* arg6); CPP_DECL JSC__JSValue JSC__JSFunction__constructWithNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2, JSC__Exception** arg3, const unsigned char* arg4); CPP_DECL JSC__JSValue JSC__JSFunction__constructWithoutAnyArgumentsOrNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__Exception** arg2, const unsigned char* arg3); -CPP_DECL JSC__JSFunction* JSC__JSFunction__createFromNative(JSC__JSGlobalObject* arg0, uint16_t arg1, const WTF__String* arg2, void* arg3, JSC__JSValue (*ArgFn4)(void* arg0, JSC__JSGlobalObject* arg1, JSC__CallFrame* arg2)); +CPP_DECL JSC__JSFunction* JSC__JSFunction__createFromNative(JSC__JSGlobalObject* arg0, uint16_t arg1, const WTF__String* arg2, void* arg3, JSC__JSValue (* ArgFn4)(void* arg0, JSC__JSGlobalObject* arg1, JSC__CallFrame* arg2)); CPP_DECL bWTF__String JSC__JSFunction__displayName(JSC__JSFunction* arg0, JSC__VM* arg1); CPP_DECL bWTF__String JSC__JSFunction__getName(JSC__JSFunction* arg0, JSC__VM* arg1); @@ -491,7 +444,7 @@ CPP_DECL JSC__JSValue JSC__JSValue__createStringArray(JSC__JSGlobalObject* arg0, CPP_DECL JSC__JSValue JSC__JSValue__createTypeError(const ZigString* arg0, const ZigString* arg1, JSC__JSGlobalObject* arg2); CPP_DECL bool JSC__JSValue__eqlCell(JSC__JSValue JSValue0, JSC__JSCell* arg1); CPP_DECL bool JSC__JSValue__eqlValue(JSC__JSValue JSValue0, JSC__JSValue JSValue1); -CPP_DECL void JSC__JSValue__forEach(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void (*ArgFn2)(JSC__VM* arg0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2)); +CPP_DECL void JSC__JSValue__forEach(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void (* ArgFn2)(JSC__VM* arg0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2)); CPP_DECL JSC__JSValue JSC__JSValue__fromEntries(JSC__JSGlobalObject* arg0, ZigString* arg1, ZigString* arg2, size_t arg3, bool arg4); CPP_DECL void JSC__JSValue__getClassName(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2); CPP_DECL JSC__JSValue JSC__JSValue__getErrorsProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); @@ -576,7 +529,7 @@ CPP_DECL void JSC__VM__deinit(JSC__VM* arg0, JSC__JSGlobalObject* arg1); CPP_DECL void JSC__VM__deleteAllCode(JSC__VM* arg0, JSC__JSGlobalObject* arg1); CPP_DECL void JSC__VM__drainMicrotasks(JSC__VM* arg0); CPP_DECL bool JSC__VM__executionForbidden(JSC__VM* arg0); -CPP_DECL void JSC__VM__holdAPILock(JSC__VM* arg0, void* arg1, void (*ArgFn2)(void* arg0)); +CPP_DECL void JSC__VM__holdAPILock(JSC__VM* arg0, void* arg1, void (* ArgFn2)(void* arg0)); CPP_DECL bool JSC__VM__isEntered(JSC__VM* arg0); CPP_DECL bool JSC__VM__isJITEnabled(); CPP_DECL JSC__JSValue JSC__VM__runGC(JSC__VM* arg0, bool arg1); @@ -584,7 +537,7 @@ CPP_DECL void JSC__VM__setExecutionForbidden(JSC__VM* arg0, bool arg1); CPP_DECL void JSC__VM__setExecutionTimeLimit(JSC__VM* arg0, double arg1); CPP_DECL void JSC__VM__shrinkFootprint(JSC__VM* arg0); CPP_DECL bool JSC__VM__throwError(JSC__VM* arg0, JSC__JSGlobalObject* arg1, JSC__ThrowScope* arg2, const unsigned char* arg3, size_t arg4); -CPP_DECL void JSC__VM__whenIdle(JSC__VM* arg0, void (*ArgFn1)()); +CPP_DECL void JSC__VM__whenIdle(JSC__VM* arg0, void (* ArgFn1)()); #pragma mark - JSC::ThrowScope @@ -642,7 +595,7 @@ CPP_DECL size_t WTF__StringImpl__length(const WTF__StringImpl* arg0); CPP_DECL const uint16_t* WTF__ExternalStringImpl__characters16(const WTF__ExternalStringImpl* arg0); CPP_DECL const unsigned char* WTF__ExternalStringImpl__characters8(const WTF__ExternalStringImpl* arg0); -CPP_DECL bWTF__ExternalStringImpl WTF__ExternalStringImpl__create(const unsigned char* arg0, size_t arg1, void (*ArgFn2)(void* arg0, unsigned char* arg1, size_t arg2)); +CPP_DECL bWTF__ExternalStringImpl WTF__ExternalStringImpl__create(const unsigned char* arg0, size_t arg1, void (* ArgFn2)(void* arg0, unsigned char* arg1, size_t arg2)); CPP_DECL bool WTF__ExternalStringImpl__is16Bit(const WTF__ExternalStringImpl* arg0); CPP_DECL bool WTF__ExternalStringImpl__is8Bit(const WTF__ExternalStringImpl* arg0); CPP_DECL bool WTF__ExternalStringImpl__isEmpty(const WTF__ExternalStringImpl* arg0); @@ -753,6 +706,7 @@ CPP_DECL ZigException ZigException__fromException(JSC__Exception* arg0); #pragma mark - Zig::ConsoleClient + #ifdef __cplusplus ZIG_DECL void Zig__ConsoleClient__count(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3); @@ -773,6 +727,7 @@ ZIG_DECL void Zig__ConsoleClient__timeStamp(void* arg0, JSC__JSGlobalObject* arg #pragma mark - Bun__Timer + #ifdef __cplusplus ZIG_DECL JSC__JSValue Bun__Timer__clearInterval(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1); diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig index 1ceff624b..6b235bbdb 100644 --- a/src/javascript/jsc/bindings/headers.zig +++ b/src/javascript/jsc/bindings/headers.zig @@ -139,6 +139,8 @@ pub extern fn JSC__JSObject__getIndex(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSG pub extern fn JSC__JSObject__putRecord(arg0: [*c]JSC__JSObject, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigString, arg3: [*c]ZigString, arg4: usize) void; pub extern fn ZigString__to16BitValue(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn ZigString__toErrorInstance(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; +pub extern fn ZigString__toExternalU16(arg0: [*c]const u16, arg1: usize, arg2: [*c]JSC__JSGlobalObject) JSC__JSValue; +pub extern fn ZigString__toExternalValue(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn ZigString__toValue(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn ZigString__toValueGC(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn SystemError__toErrorInstance(arg0: [*c]const SystemError, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; diff --git a/src/javascript/jsc/webcore.zig b/src/javascript/jsc/webcore.zig new file mode 100644 index 000000000..488bcae0e --- /dev/null +++ b/src/javascript/jsc/webcore.zig @@ -0,0 +1,2 @@ +pub usingnamespace @import("./webcore/response.zig"); +pub usingnamespace @import("./webcore/encoding.zig"); diff --git a/src/javascript/jsc/webcore/encoding.zig b/src/javascript/jsc/webcore/encoding.zig new file mode 100644 index 000000000..79c3125f7 --- /dev/null +++ b/src/javascript/jsc/webcore/encoding.zig @@ -0,0 +1,628 @@ +const std = @import("std"); +const Api = @import("../../../api/schema.zig").Api; +const RequestContext = @import("../../../http.zig").RequestContext; +const MimeType = @import("../../../http.zig").MimeType; +const ZigURL = @import("../../../query_string_map.zig").URL; +const HTTPClient = @import("http"); +const NetworkThread = HTTPClient.NetworkThread; + +const JSC = @import("../../../jsc.zig"); +const js = JSC.C; + +const Method = @import("../../../http/method.zig").Method; + +const ObjectPool = @import("../../../pool.zig").ObjectPool; + +const Output = @import("../../../global.zig").Output; +const MutableString = @import("../../../global.zig").MutableString; +const strings = @import("../../../global.zig").strings; +const string = @import("../../../global.zig").string; +const default_allocator = @import("../../../global.zig").default_allocator; +const FeatureFlags = @import("../../../global.zig").FeatureFlags; +const ArrayBuffer = @import("../base.zig").ArrayBuffer; +const Properties = @import("../base.zig").Properties; +const NewClass = @import("../base.zig").NewClass; +const d = @import("../base.zig").d; +const castObj = @import("../base.zig").castObj; +const getAllocator = @import("../base.zig").getAllocator; +const JSPrivateDataPtr = @import("../base.zig").JSPrivateDataPtr; +const GetJSPrivateData = @import("../base.zig").GetJSPrivateData; +const Environment = @import("../../../env.zig"); +const ZigString = JSC.ZigString; +const JSInternalPromise = JSC.JSInternalPromise; +const JSPromise = JSC.JSPromise; +const JSValue = JSC.JSValue; +const JSError = JSC.JSError; +const JSGlobalObject = JSC.JSGlobalObject; + +const VirtualMachine = @import("../javascript.zig").VirtualMachine; +const Task = @import("../javascript.zig").Task; + +const picohttp = @import("picohttp"); + +pub const TextEncoder = struct { + filler: u32 = 0, + var text_encoder: TextEncoder = TextEncoder{}; + + pub const Class = NewClass( + TextEncoder, + .{ + .name = "TextEncoder", + }, + .{ + .encode = .{ + .rfn = encode, + }, + .encodeInto = .{ + .rfn = encodeInto, + }, + }, + .{ + .encoding = .{ + .get = getEncoding, + .readOnly = true, + }, + }, + ); + + const utf8_string: string = "utf-8"; + pub fn getEncoding( + _: *TextEncoder, + ctx: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, + ) js.JSValueRef { + return ZigString.init(utf8_string).toValue(ctx.ptr()).asObjectRef(); + } + + pub fn encode( + _: *TextEncoder, + ctx: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + args: []const js.JSValueRef, + exception: js.ExceptionRef, + ) js.JSValueRef { + var arguments: []const JSC.JSValue = @ptrCast([*]const JSC.JSValue, args.ptr)[0..args.len]; + + if (arguments.len < 1) { + return JSC.C.JSObjectMakeTypedArray(ctx, JSC.C.JSTypedArrayType.kJSTypedArrayTypeUint8Array, 0, exception); + } + + const value = arguments[0]; + + var zig_str = value.getZigString(ctx.ptr()); + + var array_buffer: ArrayBuffer = undefined; + if (zig_str.is16Bit()) { + var bytes = strings.toUTF8AllocWithType( + default_allocator, + @TypeOf(zig_str.utf16Slice()), + zig_str.utf16Slice(), + ) catch { + JSC.throwInvalidArguments("Out of memory", .{}, ctx, exception); + return null; + }; + array_buffer = ArrayBuffer.fromBytes(bytes, .Uint8Array); + } else { + var bytes = strings.allocateLatin1IntoUTF8(default_allocator, []const u8, zig_str.slice()) catch { + JSC.throwInvalidArguments("Out of memory", .{}, ctx, exception); + return null; + }; + + array_buffer = ArrayBuffer.fromBytes(bytes, .Uint8Array); + } + + return array_buffer.toJS(ctx, exception).asObjectRef(); + } + + const read_key = ZigString.init("read"); + const written_key = ZigString.init("written"); + + pub fn encodeInto( + _: *TextEncoder, + ctx: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + args: []const js.JSValueRef, + exception: js.ExceptionRef, + ) js.JSValueRef { + var arguments: []const JSC.JSValue = @ptrCast([*]const JSC.JSValue, args.ptr)[0..args.len]; + + if (arguments.len < 2) { + JSC.throwInvalidArguments("TextEncoder.encodeInto expects (string, Uint8Array)", .{}, ctx, exception); + return null; + } + + const value = arguments[0]; + + const array_buffer = arguments[1].asArrayBuffer(ctx.ptr()) orelse { + JSC.throwInvalidArguments("TextEncoder.encodeInto expects a Uint8Array", .{}, ctx, exception); + return null; + }; + + var output = array_buffer.slice(); + const input = value.getZigString(ctx.ptr()); + var result: strings.EncodeIntoResult = strings.EncodeIntoResult{ .read = 0, .written = 0 }; + if (input.is16Bit()) { + const utf16_slice = input.utf16Slice(); + result = strings.copyUTF16IntoUTF8(output, @TypeOf(utf16_slice), utf16_slice); + } else { + result = strings.copyLatin1IntoUTF8(output, @TypeOf(input.slice()), input.slice()); + } + return JSC.JSValue.createObject2(ctx.ptr(), &read_key, &written_key, JSValue.jsNumber(result.read), JSValue.jsNumber(result.written)).asObjectRef(); + } + + pub const Constructor = struct { + pub const Class = NewClass( + void, + .{ + .name = "TextEncoder", + }, + .{ + .constructor = constructor, + }, + .{}, + ); + + pub fn constructor( + ctx: js.JSContextRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, + ) js.JSObjectRef { + return TextEncoder.Class.make(ctx, &text_encoder); + } + }; +}; + +/// https://encoding.spec.whatwg.org/encodings.json +pub const EncodingLabel = enum { + @"UTF-8", + @"IBM866", + @"ISO-8859-2", + @"ISO-8859-3", + @"ISO-8859-4", + @"ISO-8859-5", + @"ISO-8859-6", + @"ISO-8859-7", + @"ISO-8859-8", + @"ISO-8859-8-I", + @"ISO-8859-10", + @"ISO-8859-13", + @"ISO-8859-14", + @"ISO-8859-15", + @"ISO-8859-16", + @"KOI8-R", + @"KOI8-U", + @"macintosh", + @"windows-874", + @"windows-1250", + @"windows-1251", + /// Also known as + /// - ASCII + /// - latin1 + @"windows-1252", + @"windows-1253", + @"windows-1254", + @"windows-1255", + @"windows-1256", + @"windows-1257", + @"windows-1258", + @"x-mac-cyrillic", + @"Big5", + @"EUC-JP", + @"ISO-2022-JP", + @"Shift_JIS", + @"EUC-KR", + @"UTF-16BE", + @"UTF-16LE", + @"x-user-defined", + + pub const Map = std.enums.EnumMap(EncodingLabel, string); + pub const label: Map = brk: { + var map = Map.initFull(""); + map.put(EncodingLabel.@"UTF-8", "utf-8"); + map.put(EncodingLabel.@"UTF-16LE", "utf-16le"); + map.put(EncodingLabel.@"windows-1252", "windows-1252"); + break :brk map; + }; + + const utf16_names = [_]string{ + "ucs-2", + "utf-16", + "unicode", + "utf-16le", + "csunicode", + "unicodefeff", + "iso-10646-ucs-2", + }; + + const utf8_names = [_]string{ + "utf8", + "utf-8", + "unicode11utf8", + "unicode20utf8", + "x-unicode20utf8", + "unicode-1-1-utf-8", + }; + + const latin1_names = [_]string{ + "l1", + "ascii", + "cp819", + "cp1252", + "ibm819", + "latin1", + "iso88591", + "us-ascii", + "x-cp1252", + "iso8859-1", + "iso_8859-1", + "iso-8859-1", + "iso-ir-100", + "csisolatin1", + "windows-1252", + "ansi_x3.4-1968", + "iso_8859-1:1987", + }; + + pub const latin1 = EncodingLabel.@"windows-1252"; + + pub fn which(input_: string) ?EncodingLabel { + const input = strings.trim(input_, " \t\r\n"); + const ExactMatcher = strings.ExactSizeMatcher; + const Eight = ExactMatcher(8); + const Sixteen = ExactMatcher(16); + return switch (input.len) { + 1, 0 => null, + 2...8 => switch (Eight.matchLower(input)) { + Eight.case("l1"), + Eight.case("ascii"), + Eight.case("cp819"), + Eight.case("cp1252"), + Eight.case("ibm819"), + Eight.case("latin1"), + Eight.case("iso88591"), + Eight.case("us-ascii"), + Eight.case("x-cp1252"), + => EncodingLabel.latin1, + + Eight.case("ucs-2"), + Eight.case("utf-16"), + Eight.case("unicode"), + Eight.case("utf-16le"), + => EncodingLabel.@"UTF-16LE", + + Eight.case("utf8"), Eight.case("utf-8") => EncodingLabel.@"UTF-8", + else => null, + }, + + 9...16 => switch (Sixteen.matchLower(input)) { + Sixteen.case("iso8859-1"), + Sixteen.case("iso_8859-1"), + Sixteen.case("iso-8859-1"), + Sixteen.case("iso-ir-100"), + Sixteen.case("csisolatin1"), + Sixteen.case("windows-1252"), + Sixteen.case("ansi_x3.4-1968"), + Sixteen.case("iso_8859-1:1987"), + => EncodingLabel.latin1, + + Sixteen.case("unicode11utf8"), + Sixteen.case("unicode20utf8"), + Sixteen.case("x-unicode20utf8"), + => EncodingLabel.@"UTF-8", + + Sixteen.case("csunicode"), + Sixteen.case("unicodefeff"), + Sixteen.case("iso-10646-ucs-2"), + => EncodingLabel.@"UTF-16LE", + + else => null, + }, + else => if (strings.eqlCaseInsensitiveASCII(input, "unicode-1-1-utf-8", true)) + EncodingLabel.@"UTF-8" + else + null, + }; + } +}; + +pub const TextDecoder = struct { + scratch_memory: []u8 = &[_]u8{}, + ignore_bom: bool = false, + fatal: bool = false, + encoding: EncodingLabel = EncodingLabel.utf8, + + pub const Class = NewClass( + TextDecoder, + .{ + .name = "TextDecoder", + }, + .{ + .decode = .{ + .rfn = decode, + }, + }, + .{ + .encoding = .{ + .get = getEncoding, + .readOnly = true, + }, + .ignoreBOM = .{ + .get = getIgnoreBOM, + .set = setIgnoreBOM, + }, + .fatal = .{ + .get = getFatal, + .set = setFatal, + }, + }, + ); + + pub fn getIgnoreBOM( + this: *TextDecoder, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, + ) js.JSValueRef { + return JSC.JSValue.jsBoolean(this.ignore_bom).asObjectRef(); + } + pub fn setIgnoreBOM( + this: *TextDecoder, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + value: JSC.C.JSValueRef, + _: js.ExceptionRef, + ) bool { + this.ignore_bom = JSValue.fromRef(value).toBoolean(); + return true; + } + pub fn setFatal( + this: *TextDecoder, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + value: JSC.C.JSValueRef, + _: js.ExceptionRef, + ) bool { + this.fatal = JSValue.fromRef(value).toBoolean(); + return true; + } + pub fn getFatal( + this: *TextDecoder, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, + ) js.JSValueRef { + return JSC.JSValue.jsBoolean(this.fatal).asObjectRef(); + } + + const utf8_string: string = "utf-8"; + pub fn getEncoding( + this: *TextDecoder, + ctx: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, + ) js.JSValueRef { + return ZigString.init(EncodingLabel.label.get(this.encoding).?).toValue(ctx.ptr()).asObjectRef(); + } + const Vector16 = std.meta.Vector(16, u16); + const max_16_ascii: Vector16 = @splat(16, @as(u16, 127)); + pub fn decode( + this: *TextDecoder, + ctx: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + args: []const js.JSValueRef, + exception: js.ExceptionRef, + ) js.JSValueRef { + var arguments: []const JSC.JSValue = @ptrCast([*]const JSC.JSValue, args.ptr)[0..args.len]; + + if (arguments.len < 1 or arguments[0].isUndefined()) { + return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); + } + + var array_buffer = arguments[0].asArrayBuffer(ctx.ptr()) orelse { + JSC.throwInvalidArguments("TextDecoder.decode expects an ArrayBuffer or TypedArray", .{}, ctx, exception); + return null; + }; + + if (array_buffer.len == 0) { + return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); + } + + switch (this.encoding) { + EncodingLabel.@"latin1" => { + var str = ZigString.init(array_buffer.slice()); + return str.toValueGC(ctx.ptr()).asObjectRef(); + }, + EncodingLabel.@"UTF-8" => { + const buffer_slice = array_buffer.slice(); + var str = ZigString.init(buffer_slice); + + if (this.fatal) { + if (strings.toUTF16Alloc(default_allocator, buffer_slice, true)) |result_| { + if (result_) |result| { + return ZigString.toExternalU16(result.ptr, result.len, ctx.ptr()).asObjectRef(); + } + } else |err| { + switch (err) { + error.InvalidByteSequence => { + JSC.JSError(default_allocator, "Invalid byte sequence", .{}, ctx, exception); + return null; + }, + error.OutOfMemory => { + JSC.JSError(default_allocator, "Out of memory", .{}, ctx, exception); + return null; + }, + else => { + JSC.JSError(default_allocator, "Unknown error", .{}, ctx, exception); + return null; + }, + } + } + } else { + if (strings.toUTF16Alloc(default_allocator, buffer_slice, false)) |result_| { + if (result_) |result| { + return ZigString.toExternalU16(result.ptr, result.len, ctx.ptr()).asObjectRef(); + } + } else |err| { + switch (err) { + error.OutOfMemory => { + JSC.JSError(default_allocator, "Out of memory", .{}, ctx, exception); + return null; + }, + else => { + JSC.JSError(default_allocator, "Unknown error", .{}, ctx, exception); + return null; + }, + } + } + } + + return str.toValueGC(ctx.ptr()).asObjectRef(); + }, + + EncodingLabel.@"UTF-16LE" => { + var slice = array_buffer.asU16(); + var i: usize = 0; + + while (i < slice.len) { + while (i + 16 <= slice.len) { + const vec: Vector16 = slice[i..][0..16].*; + if ((@reduce(.Or, vec > max_16_ascii))) { + break; + } + i += 16; + } + while (i < slice.len and slice[i] <= 127) { + i += 1; + } + break; + } + + // is this actually a UTF-16 string that is just ascii? + // we can still allocate as UTF-16 and just copy the bytes + if (i == slice.len) { + return JSC.C.JSValueMakeString(ctx, JSC.C.JSStringCreateWithCharacters(slice.ptr, slice.len)); + } + + var buffer = std.ArrayListAlignedUnmanaged(u16, 1){}; + buffer.ensureTotalCapacity(default_allocator, slice.len) catch unreachable; + buffer.items.len = i; + defer buffer.deinit( + default_allocator, + ); + + for (slice[0..i]) |char, j| { + buffer.items[j] = char; + } + + const first_high_surrogate = 0xD800; + const last_high_surrogate = 0xDBFF; + const first_low_surrogate = 0xDC00; + const last_low_surrogate = 0xDFFF; + + var remainder = slice[i..]; + while (remainder.len > 0) { + switch (remainder[0]) { + 0...127 => { + var count: usize = 1; + while (remainder.len > count and remainder[count] <= 127) : (count += 1) {} + buffer.ensureUnusedCapacity(default_allocator, count) catch unreachable; + const prev = buffer.items.len; + buffer.items.len += count; + for (remainder[0..count]) |char, j| { + buffer.items[prev + j] = char; + } + remainder = remainder[count..]; + }, + first_high_surrogate...last_high_surrogate => |first| { + if (remainder.len > 1) { + if (remainder[1] >= first_low_surrogate and remainder[1] <= last_low_surrogate) { + buffer.ensureUnusedCapacity(default_allocator, 2) catch unreachable; + buffer.items.ptr[buffer.items.len] = first; + buffer.items.ptr[buffer.items.len + 1] = remainder[1]; + buffer.items.len += 2; + remainder = remainder[2..]; + continue; + } + } + buffer.ensureUnusedCapacity(default_allocator, 1) catch unreachable; + buffer.items.ptr[buffer.items.len] = strings.unicode_replacement; + buffer.items.len += 1; + remainder = remainder[1..]; + continue; + }, + + // Is this an unpaired low surrogate or four-digit hex escape? + else => { + buffer.ensureUnusedCapacity(default_allocator, 1) catch unreachable; + buffer.items.ptr[buffer.items.len] = strings.unicode_replacement; + buffer.items.len += 1; + remainder = remainder[1..]; + }, + } + } + + var out = ZigString.init(""); + out.ptr = @ptrCast([*]u8, buffer.items.ptr); + out.len = buffer.items.len; + out.markUTF16(); + return out.toValueGC(ctx.ptr()).asObjectRef(); + }, + else => { + JSC.throwInvalidArguments("TextDecoder.decode set to unsupported encoding", .{}, ctx, exception); + return null; + }, + } + } + + pub const Constructor = struct { + pub const Class = NewClass( + void, + .{ + .name = "TextDecoder", + }, + .{ + .constructor = constructor, + }, + .{}, + ); + + pub fn constructor( + ctx: js.JSContextRef, + _: js.JSObjectRef, + args_: []const js.JSValueRef, + exception: js.ExceptionRef, + ) js.JSObjectRef { + var arguments: []const JSC.JSValue = @ptrCast([*]const JSC.JSValue, args_.ptr)[0..args_.len]; + var encoding = EncodingLabel.@"UTF-8"; + if (arguments.len > 0) { + if (!arguments[0].isString()) { + JSC.throwInvalidArguments("TextDecoder(encoding) label is invalid", .{}, ctx, exception); + return null; + } + + var str = arguments[0].toSlice(ctx.ptr(), default_allocator); + defer if (str.allocated) str.deinit(); + encoding = EncodingLabel.which(str.slice()) orelse { + JSC.throwInvalidArguments("Unsupported encoding label \"{s}\"", .{str.slice()}, ctx, exception); + return null; + }; + } + var decoder = getAllocator(ctx).create(TextDecoder) catch unreachable; + decoder.* = TextDecoder{ .encoding = encoding }; + return TextDecoder.Class.make(ctx, decoder); + } + }; +}; + +test "Vec" {} diff --git a/src/jsc.zig b/src/jsc.zig index 22e5011a7..e44eecf96 100644 --- a/src/jsc.zig +++ b/src/jsc.zig @@ -4,7 +4,7 @@ pub usingnamespace @import("./javascript/jsc/bindings/bindings.zig"); pub usingnamespace @import("./javascript/jsc/base.zig"); pub usingnamespace @import("./javascript/jsc/javascript.zig"); pub const C = @import("./javascript/jsc/javascript_core_c_api.zig"); -pub const WebCore = @import("./javascript/jsc/webcore/response.zig"); +pub const WebCore = @import("./javascript/jsc/webcore.zig"); pub const Jest = @import("./javascript/jsc/test/jest.zig"); pub const Node = struct { pub usingnamespace @import("./javascript/jsc/node/types.zig"); |