diff options
Diffstat (limited to 'src/bun.js/bindings')
-rw-r--r-- | src/bun.js/bindings/ModuleLoader.cpp | 38 | ||||
-rw-r--r-- | src/bun.js/bindings/ModuleLoader.h | 8 | ||||
-rw-r--r-- | src/bun.js/bindings/ZigGlobalObject.cpp | 27 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 120 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.zig | 66 | ||||
-rw-r--r-- | src/bun.js/bindings/exports.zig | 7 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-handwritten.h | 48 | ||||
-rw-r--r-- | src/bun.js/bindings/headers-replacements.zig | 1 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.h | 8 | ||||
-rw-r--r-- | src/bun.js/bindings/headers.zig | 1 | ||||
-rw-r--r-- | src/bun.js/bindings/helpers.h | 22 |
12 files changed, 284 insertions, 64 deletions
diff --git a/src/bun.js/bindings/ModuleLoader.cpp b/src/bun.js/bindings/ModuleLoader.cpp index 45f3cc46b..763b18c65 100644 --- a/src/bun.js/bindings/ModuleLoader.cpp +++ b/src/bun.js/bindings/ModuleLoader.cpp @@ -39,7 +39,7 @@ namespace Bun { using namespace Zig; using namespace WebCore; -extern "C" BunLoaderType Bun__getDefaultLoader(JSC::JSGlobalObject*, ZigString* specifier); +extern "C" BunLoaderType Bun__getDefaultLoader(JSC::JSGlobalObject*, BunString* specifier); static JSC::JSInternalPromise* rejectedInternalPromise(JSC::JSGlobalObject* globalObject, JSC::JSValue value) { @@ -129,7 +129,7 @@ PendingVirtualModuleResult* PendingVirtualModuleResult::create(JSC::JSGlobalObje return virtualModule; } -OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::JSValue objectValue, ZigString* specifier) +OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::JSValue objectValue, BunString* specifier) { OnLoadResult result = {}; result.type = OnLoadResultTypeError; @@ -210,7 +210,7 @@ OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC:: return result; } -static OnLoadResult handleOnLoadResult(Zig::GlobalObject* globalObject, JSC::JSValue objectValue, ZigString* specifier) +static OnLoadResult handleOnLoadResult(Zig::GlobalObject* globalObject, JSC::JSValue objectValue, BunString* specifier) { if (JSC::JSPromise* promise = JSC::jsDynamicCast<JSC::JSPromise*>(objectValue)) { OnLoadResult result = {}; @@ -227,8 +227,8 @@ static JSValue handleVirtualModuleResult( Zig::GlobalObject* globalObject, JSValue virtualModuleResult, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer) + BunString* specifier, + BunString* referrer) { auto onLoadResult = handleOnLoadResult(globalObject, virtualModuleResult, specifier); JSC::VM& vm = globalObject->vm(); @@ -295,7 +295,7 @@ static JSValue handleVirtualModuleResult( object); auto source = JSC::SourceCode( JSC::SyntheticSourceProvider::create(WTFMove(function), - JSC::SourceOrigin(), Zig::toString(*specifier))); + JSC::SourceOrigin(), Bun::toWTFString(*specifier))); JSC::ensureStillAliveHere(object); return rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))); } @@ -305,8 +305,8 @@ static JSValue handleVirtualModuleResult( JSFunction* performPromiseThenFunction = globalObject->performPromiseThenFunction(); auto callData = JSC::getCallData(performPromiseThenFunction); ASSERT(callData.type != CallData::Type::None); - auto specifierString = Zig::toString(*specifier); - auto referrerString = Zig::toString(*referrer); + auto specifierString = Bun::toWTFString(*specifier); + auto referrerString = Bun::toWTFString(*referrer); PendingVirtualModuleResult* pendingModule = PendingVirtualModuleResult::create(globalObject, specifierString, referrerString); JSC::JSInternalPromise* internalPromise = pendingModule->internalPromise(); MarkedArgumentBuffer arguments; @@ -328,8 +328,8 @@ static JSValue handleVirtualModuleResult( extern "C" void Bun__onFulfillAsyncModule( EncodedJSValue promiseValue, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer) + BunString* specifier, + BunString* referrer) { JSC::JSValue value = JSValue::decode(promiseValue); JSC::JSInternalPromise* promise = jsCast<JSC::JSInternalPromise*>(value); @@ -352,8 +352,8 @@ template<bool allowPromise> static JSValue fetchSourceCode( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer) + BunString* specifier, + BunString* referrer) { void* bunVM = globalObject->bunVM(); auto& vm = globalObject->vm(); @@ -401,7 +401,7 @@ static JSValue fetchSourceCode( return reject(exception); } - auto moduleKey = Zig::toString(*specifier); + auto moduleKey = Bun::toWTFString(*specifier); switch (res->result.value.tag) { case SyntheticModuleType::Module: { @@ -491,8 +491,8 @@ extern "C" JSC::EncodedJSValue jsFunctionOnLoadObjectResultResolve(JSC::JSGlobal pendingModule->internalField(1).set(vm, pendingModule, JSC::jsUndefined()); JSC::JSInternalPromise* promise = pendingModule->internalPromise(); - ZigString specifier = Zig::toZigString(specifierString, globalObject); - ZigString referrer = Zig::toZigString(referrerString, globalObject); + BunString specifier = Bun::fromJS(globalObject, specifierString); + BunString referrer = Bun::fromJS(globalObject, referrerString); auto scope = DECLARE_THROW_SCOPE(vm); JSC::JSValue result = handleVirtualModuleResult<false>(reinterpret_cast<Zig::GlobalObject*>(globalObject), objectResult, &res, &specifier, &referrer); if (res.success) { @@ -536,8 +536,8 @@ extern "C" JSC::EncodedJSValue jsFunctionOnLoadObjectResultReject(JSC::JSGlobalO JSValue fetchSourceCodeSync( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer) + BunString* specifier, + BunString* referrer) { return fetchSourceCode<false>(globalObject, res, specifier, referrer); } @@ -545,8 +545,8 @@ JSValue fetchSourceCodeSync( JSValue fetchSourceCodeAsync( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer) + BunString* specifier, + BunString* referrer) { return fetchSourceCode<true>(globalObject, res, specifier, referrer); } diff --git a/src/bun.js/bindings/ModuleLoader.h b/src/bun.js/bindings/ModuleLoader.h index 98f8b7dbb..31af41e6e 100644 --- a/src/bun.js/bindings/ModuleLoader.h +++ b/src/bun.js/bindings/ModuleLoader.h @@ -82,13 +82,13 @@ OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC:: JSValue fetchSourceCodeSync( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer); + BunString* specifier, + BunString* referrer); JSValue fetchSourceCodeAsync( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer); + BunString* specifier, + BunString* referrer); } // namespace Bun
\ No newline at end of file diff --git a/src/bun.js/bindings/ZigGlobalObject.cpp b/src/bun.js/bindings/ZigGlobalObject.cpp index be88430bd..04e79d303 100644 --- a/src/bun.js/bindings/ZigGlobalObject.cpp +++ b/src/bun.js/bindings/ZigGlobalObject.cpp @@ -3455,8 +3455,15 @@ JSC::Identifier GlobalObject::moduleLoaderResolve(JSGlobalObject* globalObject, { ErrorableZigString res; res.success = false; - ZigString keyZ = toZigString(key, globalObject); - ZigString referrerZ = referrer && !referrer.isUndefinedOrNull() && referrer.isString() ? toZigString(referrer, globalObject) : ZigStringEmpty; + BunString keyZ = Bun::fromJS(globalObject, key); + BunString referrerZ; + + if (referrer && !referrer.isUndefinedOrNull() && referrer.isString()) { + referrerZ = Bun::fromJS(globalObject, referrer); + } else { + referrerZ = BunString { BunStringTag::Empty }; + } + Zig__GlobalObject__resolve(&res, globalObject, &keyZ, &referrerZ); if (res.success) { @@ -3482,8 +3489,12 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderImportModule(JSGlobalObject* g auto sourceURL = sourceOrigin.url(); ErrorableZigString resolved; - auto moduleNameZ = toZigString(moduleNameValue, globalObject); - auto sourceOriginZ = sourceURL.isEmpty() ? ZigStringCwd : toZigString(sourceURL.fileSystemPath()); + auto moduleNameZ = Bun::fromJS(globalObject, moduleNameValue); + WTF::String fsPath; + if (!sourceURL.isEmpty()) { + fsPath = sourceURL.fileSystemPath(); + } + auto sourceOriginZ = sourceURL.isEmpty() ? BunString { BunStringTag::StaticZigString, { .zig = ZigStringCwd } } : Bun::fromString(fsPath); resolved.success = false; Zig__GlobalObject__resolve(&resolved, globalObject, &moduleNameZ, &sourceOriginZ); if (!resolved.success) { @@ -3514,7 +3525,7 @@ static JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync, return JSValue::encode(JSC::jsUndefined()); } - auto specifier = Zig::toZigString(moduleKey); + auto specifier = Bun::fromString(moduleKey); ErrorableResolvedSource res; res.success = false; res.result.err.code = 0; @@ -3559,8 +3570,8 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalOb return rejectedInternalPromise(globalObject, createTypeError(globalObject, "To load Node-API modules, use require() or process.dlopen instead of import."_s)); } - auto moduleKeyZig = toZigString(moduleKey); - auto source = Zig::toZigString(value1, globalObject); + auto moduleKeyZig = Bun::fromString(moduleKey); + auto source = Bun::fromJS(globalObject, value1); ErrorableResolvedSource res; res.success = false; res.result.err.code = 0; @@ -3612,8 +3623,6 @@ JSC::JSValue GlobalObject::moduleLoaderEvaluate(JSGlobalObject* globalObject, return result; } - - #include "ZigGeneratedClasses+lazyStructureImpl.h" } // namespace Zig diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 955971ec0..dd26a88ba 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -1382,7 +1382,6 @@ void JSC__JSString__toZigString(JSC__JSString* arg0, JSC__JSGlobalObject* arg1, *arg2 = Zig::toZigString(arg0->value(arg1)); } - bool JSC__JSString__eql(const JSC__JSString* arg0, JSC__JSGlobalObject* obj, JSC__JSString* arg2) { return arg0->equal(obj, arg2); @@ -1394,8 +1393,6 @@ JSC__JSObject* JSC__JSString__toObject(JSC__JSString* arg0, JSC__JSGlobalObject* return arg0->toObject(arg1); } - - #pragma mark - JSC::JSModuleLoader // JSC__JSValue @@ -1414,7 +1411,6 @@ void Microtask__run_default(void* microtask, void* global) reinterpret_cast<Zig::JSMicrotaskCallbackDefaultGlobal*>(microtask)->call(reinterpret_cast<Zig::GlobalObject*>(global)); } - JSC__JSValue JSC__JSModuleLoader__evaluate(JSC__JSGlobalObject* globalObject, const unsigned char* arg1, size_t arg2, const unsigned char* originUrlPtr, size_t originURLLen, const unsigned char* referrerUrlPtr, size_t referrerUrlLen, JSC__JSValue JSValue5, JSC__JSValue* arg6) @@ -2037,8 +2033,6 @@ bool JSC__JSInternalPromise__isHandled(const JSC__JSInternalPromise* arg0, JSC__ return arg0->isHandled(reinterpret_cast<JSC::VM&>(arg1)); } - - #pragma mark - JSC::JSGlobalObject JSC__JSValue JSC__JSGlobalObject__generateHeapSnapshot(JSC__JSGlobalObject* globalObject) @@ -2550,7 +2544,6 @@ JSC__JSObject* JSC__JSValue__toObject(JSC__JSValue JSValue0, JSC__JSGlobalObject return value.toObject(arg1); } - JSC__JSString* JSC__JSValue__toString(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1) { JSC::JSValue value = JSC::JSValue::decode(JSValue0); @@ -2562,7 +2555,6 @@ JSC__JSString* JSC__JSValue__toStringOrNull(JSC__JSValue JSValue0, JSC__JSGlobal return value.toStringOrNull(arg1); } - static void populateStackFrameMetadata(JSC::VM& vm, const JSC::StackFrame* stackFrame, ZigStackFrame* frame) { frame->source_url = Zig::toZigString(stackFrame->sourceURL(vm)); @@ -3058,7 +3050,6 @@ void JSC__Exception__getStackTrace(JSC__Exception* arg0, ZigStackTrace* trace) populateStackTrace(arg0->vm(), arg0->stack(), trace); } - #pragma mark - JSC::VM JSC__JSValue JSC__VM__runGC(JSC__VM* vm, bool sync) @@ -3193,9 +3184,6 @@ bJSC__CatchScope JSC__CatchScope__declare(JSC__VM* arg0, unsigned char* arg1, un } JSC__Exception* JSC__CatchScope__exception(JSC__CatchScope* arg0) { return arg0->exception(); } - - - JSC__JSValue JSC__JSPromise__rejectedPromiseValue(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1) { @@ -3445,3 +3433,111 @@ restart: return; } } + +extern "C" void Bun__WTFStringImpl__deref(WTF::StringImpl* impl) +{ + impl->deref(); +} +extern "C" void Bun__WTFStringImpl__ref(WTF::StringImpl* impl) +{ + impl->ref(); +} + +extern "C" bool BunString__fromJS(JSC::JSGlobalObject* globalObject, JSC::EncodedJSValue encodedValue, BunString* bunString) +{ + JSC::JSValue value = JSC::JSValue::decode(encodedValue); + *bunString = Bun::fromJS(globalObject, value); + return bunString->tag != BunStringTag::Dead; +} + +namespace Bun { +JSC::JSValue toJS(JSC::JSGlobalObject* globalObject, BunString bunString) +{ + if (bunString.tag == BunStringTag::Empty || bunString.tag == BunStringTag::Dead) { + return JSValue(JSC::jsEmptyString(globalObject->vm())); + } + if (bunString.tag == BunStringTag::WTFStringImpl) { + return JSValue(jsString(globalObject->vm(), String(bunString.impl.wtf))); + } + + if (bunString.tag == BunStringTag::StaticZigString) { + return JSValue(jsOwnedString(globalObject->vm(), Zig::toStringStatic(bunString.impl.zig))); + } + + return JSValue(Zig::toJSStringGC(bunString.impl.zig, globalObject)); +} + +WTF::String toWTFString(BunString& bunString) +{ + if (bunString.tag == BunStringTag::ZigString) { + if (Zig::isTaggedUTF8Ptr(bunString.impl.zig.ptr)) { + return Zig::toStringCopy(bunString.impl.zig); + } else { + return Zig::toString(bunString.impl.zig); + } + + } else if (bunString.tag == BunStringTag::StaticZigString) { + return Zig::toStringStatic(bunString.impl.zig); + } + + if (bunString.tag == BunStringTag::WTFStringImpl) { + return WTF::String(bunString.impl.wtf); + } + + return WTF::String(); +} + +BunString fromJS(JSC::JSGlobalObject* globalObject, JSValue value) +{ + JSC::JSString* str = value.toStringOrNull(globalObject); + if (UNLIKELY(!str)) { + return { BunStringTag::Dead }; + } + + if (str->length() == 0) { + return { BunStringTag::Empty }; + } + + auto wtfString = str->value(globalObject); + + return { BunStringTag::WTFStringImpl, { .wtf = wtfString.impl() } }; +} + +BunString fromString(WTF::String& wtfString) +{ + if (wtfString.length() == 0) + return { BunStringTag::Empty }; + + return { BunStringTag::WTFStringImpl, { .wtf = wtfString.impl() } }; +} + +BunString fromString(WTF::StringImpl* wtfString) +{ + if (wtfString->length() == 0) + return { BunStringTag::Empty }; + + return { BunStringTag::WTFStringImpl, { .wtf = wtfString } }; +} + +} + +extern "C" JSC::EncodedJSValue BunString__toJS(JSC::JSGlobalObject* globalObject, BunString* bunString) +{ + return JSValue::encode(Bun::toJS(globalObject, *bunString)); +} + +extern "C" void BunString__toWTFString(BunString* bunString) +{ + if (bunString->tag == BunStringTag::ZigString) { + if (Zig::isTaggedUTF8Ptr(bunString->impl.zig.ptr)) { + bunString->impl.wtf = Zig::toStringCopy(bunString->impl.zig).impl(); + } else { + bunString->impl.wtf = Zig::toString(bunString->impl.zig).impl(); + } + + bunString->tag = BunStringTag::WTFStringImpl; + } else if (bunString->tag == BunStringTag::StaticZigString) { + bunString->impl.wtf = Zig::toStringStatic(bunString->impl.zig).impl(); + bunString->tag = BunStringTag::WTFStringImpl; + } +} diff --git a/src/bun.js/bindings/bindings.zig b/src/bun.js/bindings/bindings.zig index 15e15b032..b029c3ffe 100644 --- a/src/bun.js/bindings/bindings.zig +++ b/src/bun.js/bindings/bindings.zig @@ -17,6 +17,7 @@ const JSC = @import("bun").JSC; const Shimmer = JSC.Shimmer; const FFI = @import("./FFI.zig"); const NullableAllocator = @import("../../nullable_allocator.zig").NullableAllocator; +const String = bun.String; pub const JSObject = extern struct { pub const shim = Shimmer("JSC", "JSObject", @This()); @@ -91,6 +92,14 @@ pub const ZigString = extern struct { ptr: [*]const u8, len: usize, + pub fn byteSlice(this: ZigString) []const u8 { + if (this.is16Bit()) { + return std.mem.sliceAsBytes(this.utf16SliceAligned()); + } + + return this.slice(); + } + pub fn clone(this: ZigString, allocator: std.mem.Allocator) !ZigString { var sliced = this.toSlice(allocator); if (!sliced.isAllocated()) { @@ -198,6 +207,10 @@ pub const ZigString = extern struct { pub const shim = Shimmer("", "ZigString", @This()); + pub inline fn length(this: ZigString) usize { + return this.len; + } + pub const Slice = struct { allocator: NullableAllocator = .{}, ptr: [*]const u8 = undefined, @@ -211,6 +224,24 @@ pub const ZigString = extern struct { }; } + pub fn init(allocator: std.mem.Allocator, input: []const u8) Slice { + return .{ + .ptr = input.ptr, + .len = @truncate(u32, input.len), + .allocator = NullableAllocator.init(allocator), + }; + } + + pub fn toZigString(this: Slice) ZigString { + if (this.isAllocated()) + return ZigString.initUTF8(this.ptr[0..this.len]); + return ZigString.init(this.slice()); + } + + pub inline fn length(this: Slice) usize { + return this.len; + } + pub const empty = Slice{ .ptr = undefined, .len = 0 }; pub inline fn isAllocated(this: Slice) bool { @@ -282,6 +313,12 @@ pub const ZigString = extern struct { /// Does nothing if the slice is not allocated pub fn deinit(this: *const Slice) void { if (this.allocator.get()) |allocator| { + if (bun.String.isWTFAllocator(allocator)) { + // workaround for https://github.com/ziglang/zig/issues/4298 + bun.String.StringImplAllocator.free(allocator.ptr, bun.constStrToU8(this.slice()), 0, 0); + return; + } + allocator.free(this.slice()); } } @@ -339,10 +376,16 @@ pub const ZigString = extern struct { return out; } + pub fn fromBytes(slice_: []const u8) ZigString { + if (!strings.isAllASCII(slice_)) + return fromUTF8(slice_); + + return init(slice_); + } + pub fn fromUTF8(slice_: []const u8) ZigString { var out = init(slice_); - if (strings.isAllASCII(slice_)) - out.markUTF8(); + out.markUTF8(); return out; } @@ -409,6 +452,14 @@ pub const ZigString = extern struct { this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 61)); } + pub fn markStatic(this: *ZigString) void { + this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 60)); + } + + pub fn isStatic(this: *const ZigString) bool { + return @ptrToInt(this.ptr) & (1 << 60) != 0; + } + pub fn markUTF16(this: *ZigString) void { this.ptr = @intToPtr([*]const u8, @ptrToInt(this.ptr) | (1 << 63)); } @@ -1245,14 +1296,14 @@ pub fn NewGlobalObject(comptime Type: type) type { } return ErrorableZigString.err(error.ImportFailed, ZigString.init(importNotImpl).toErrorInstance(global).asVoid()); } - pub fn resolve(res: *ErrorableZigString, global: *JSGlobalObject, specifier: *ZigString, source: *ZigString) callconv(.C) void { + pub fn resolve(res: *ErrorableString, global: *JSGlobalObject, specifier: *String, source: *String) callconv(.C) void { if (comptime @hasDecl(Type, "resolve")) { @call(.{ .modifier = .always_inline }, Type.resolve, .{ res, global, specifier.*, source.* }); return; } - res.* = ErrorableZigString.err(error.ResolveFailed, ZigString.init(resolveNotImpl).toErrorInstance(global).asVoid()); + res.* = ErrorableString.err(error.ResolveFailed, ZigString.init(resolveNotImpl).toErrorInstance(global).asVoid()); } - pub fn fetch(ret: *ErrorableResolvedSource, global: *JSGlobalObject, specifier: *ZigString, source: *ZigString) callconv(.C) void { + pub fn fetch(ret: *ErrorableResolvedSource, global: *JSGlobalObject, specifier: *String, source: *String) callconv(.C) void { if (comptime @hasDecl(Type, "fetch")) { @call(.{ .modifier = .always_inline }, Type.fetch, .{ ret, global, specifier.*, source.* }); return; @@ -2401,6 +2452,10 @@ pub const JSValue = enum(JSValueReprInt) { return callWithThis(this, globalThis, JSC.JSValue.jsUndefined(), args); } + pub fn toBunString(this: JSValue, globalObject: *JSC.JSGlobalObject) bun.String { + return bun.String.fromJS(this, globalObject); + } + pub fn callWithThis(this: JSValue, globalThis: *JSGlobalObject, thisValue: JSC.JSValue, args: []const JSC.JSValue) JSC.JSValue { JSC.markBinding(@src()); return JSC.C.JSObjectCallAsFunctionReturnValue( @@ -3844,3 +3899,4 @@ pub const DOMCalls = .{ @import("../api/bun.zig").FFI.Reader, @import("../webcore.zig").Crypto, }; +const ErrorableString = bun.JSC.ErrorableString; diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index 2387122dd..8995cff08 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -60,13 +60,13 @@ pub const ZigGlobalObject = extern struct { return @call(.{ .modifier = .always_inline }, Interface.import, .{ global, specifier, source }); } - pub fn resolve(res: *ErrorableZigString, global: *JSGlobalObject, specifier: *ZigString, source: *ZigString) callconv(.C) void { + pub fn resolve(res: *ErrorableZigString, global: *JSGlobalObject, specifier: *bun.String, source: *bun.String) callconv(.C) void { if (comptime is_bindgen) { unreachable; } @call(.{ .modifier = .always_inline }, Interface.resolve, .{ res, global, specifier, source }); } - pub fn fetch(ret: *ErrorableResolvedSource, global: *JSGlobalObject, specifier: *ZigString, source: *ZigString) callconv(.C) void { + pub fn fetch(ret: *ErrorableResolvedSource, global: *JSGlobalObject, specifier: *bun.String, source: *bun.String) callconv(.C) void { if (comptime is_bindgen) { unreachable; } @@ -214,7 +214,7 @@ pub const ResolvedSource = extern struct { pub const name = "ResolvedSource"; pub const namespace = shim.namespace; - specifier: ZigString, + specifier: bun.String, source_code: ZigString, source_url: ZigString, hash: u32, @@ -857,6 +857,7 @@ pub const ZigException = extern struct { pub const ErrorableResolvedSource = Errorable(ResolvedSource); pub const ErrorableZigString = Errorable(ZigString); +pub const ErrorableString = Errorable(bun.String); pub const ErrorableJSValue = Errorable(JSValue); pub const ZigConsoleClient = struct { diff --git a/src/bun.js/bindings/headers-cpp.h b/src/bun.js/bindings/headers-cpp.h index e35afe0f0..8229fd247 100644 --- a/src/bun.js/bindings/headers-cpp.h +++ b/src/bun.js/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1669793662 +//-- AUTOGENERATED FILE -- 1669812013 // clang-format off #pragma once diff --git a/src/bun.js/bindings/headers-handwritten.h b/src/bun.js/bindings/headers-handwritten.h index 9018615fc..026d2ea1c 100644 --- a/src/bun.js/bindings/headers-handwritten.h +++ b/src/bun.js/bindings/headers-handwritten.h @@ -182,6 +182,7 @@ typedef void WebSocketClientTLS; #ifndef __cplusplus typedef struct Bun__ArrayBuffer Bun__ArrayBuffer; typedef struct Uint8Array_alias Uint8Array_alias; +typedef struct BunString BunString; #endif #ifdef __cplusplus @@ -209,6 +210,39 @@ enum SyntheticModuleType : uint64_t { TTY = 1029, }; +enum class BunStringTag : uint8_t { + Dead = 0, + WTFStringImpl = 1, + ZigString = 2, + StaticZigString = 3, + Empty = 4, +}; + +typedef union BunStringImpl { + ZigString zig; + WTF::StringImpl* wtf; +} BunStringImpl; + +typedef struct BunString { + BunStringTag tag; + BunStringImpl impl; +} BunString; + +extern "C" void Bun__WTFStringImpl__deref(WTF::StringImpl* impl); +extern "C" void Bun__WTFStringImpl__ref(WTF::StringImpl* impl); +extern "C" bool BunString__fromJS(JSC::JSGlobalObject*, JSC::EncodedJSValue, BunString*); +extern "C" JSC::EncodedJSValue BunString__toJS(JSC::JSGlobalObject*, BunString*); +extern "C" void BunString__toWTFString(BunString*); + +namespace Bun { +JSC::JSValue toJS(JSC::JSGlobalObject*, BunString); +BunString fromJS(JSC::JSGlobalObject* globalObject, JSC::JSValue value); +WTF::String toWTFString(BunString& bunString); +BunString fromString(WTF::String& wtfString); +BunString fromString(const WTF::String& wtfString); +BunString fromString(WTF::StringImpl* wtfString); +} + extern "C" const char* Bun__userAgent; extern "C" ZigErrorCode Zig_ErrorCodeParserError; @@ -219,21 +253,21 @@ extern "C" void Microtask__run_default(void* ptr, void* global); extern "C" bool Bun__transpileVirtualModule( JSC::JSGlobalObject* global, - ZigString* specifier, - ZigString* referrer, + BunString* specifier, + BunString* referrer, ZigString* sourceCode, BunLoaderType loader, ErrorableResolvedSource* result); extern "C" JSC::EncodedJSValue Bun__runVirtualModule( JSC::JSGlobalObject* global, - ZigString* specifier); + BunString* specifier); extern "C" void* Bun__transpileFile( void* bunVM, JSC::JSGlobalObject* global, - ZigString* specifier, - ZigString* referrer, + BunString* specifier, + BunString* referrer, ErrorableResolvedSource* result, bool allowPromise); extern "C" JSC::EncodedJSValue CallbackJob__onResolve(JSC::JSGlobalObject*, JSC::CallFrame*); @@ -242,8 +276,8 @@ extern "C" JSC::EncodedJSValue CallbackJob__onReject(JSC::JSGlobalObject*, JSC:: extern "C" bool Bun__fetchBuiltinModule( void* bunVM, JSC::JSGlobalObject* global, - ZigString* specifier, - ZigString* referrer, + BunString* specifier, + BunString* referrer, ErrorableResolvedSource* result); // Used in process.version diff --git a/src/bun.js/bindings/headers-replacements.zig b/src/bun.js/bindings/headers-replacements.zig index f586bd028..cd27fac98 100644 --- a/src/bun.js/bindings/headers-replacements.zig +++ b/src/bun.js/bindings/headers-replacements.zig @@ -16,6 +16,7 @@ pub const struct_JSC__AsyncGeneratorPrototype = bindings.AsyncGeneratorPrototype pub const struct_JSC__AsyncGeneratorFunctionPrototype = bindings.AsyncGeneratorFunctionPrototype; pub const struct_JSC__AsyncFunctionPrototype = bindings.AsyncFunctionPrototype; pub const struct_JSC__ArrayPrototype = bindings.ArrayPrototype; +pub const BunString = @import("bun").String; pub const struct_JSC__ArrayIteratorPrototype = bindings.ArrayIteratorPrototype; pub const bWTF__URL = bindings.URL; diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h index 399fc54a6..1962658cb 100644 --- a/src/bun.js/bindings/headers.h +++ b/src/bun.js/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format off -//-- AUTOGENERATED FILE -- 1669793662 +//-- AUTOGENERATED FILE -- 1669812013 #pragma once #include <stddef.h> @@ -50,6 +50,7 @@ typedef void* JSClassRef; #ifndef __cplusplus typedef bJSC__CatchScope JSC__CatchScope; // JSC::CatchScope typedef ErrorableResolvedSource ErrorableResolvedSource; + typedef BunString BunString; typedef bJSC__ThrowScope JSC__ThrowScope; // JSC::ThrowScope typedef ErrorableZigString ErrorableZigString; typedef bJSC__JSObject JSC__JSObject; // JSC::JSObject @@ -97,6 +98,7 @@ typedef void* JSClassRef; } typedef ErrorableResolvedSource ErrorableResolvedSource; + typedef BunString BunString; typedef ErrorableZigString ErrorableZigString; typedef WebSocketClient WebSocketClient; typedef WebSocketHTTPSClient WebSocketHTTPSClient; @@ -500,12 +502,12 @@ CPP_DECL bool Zig__GlobalObject__resetModuleRegistryMap(JSC__JSGlobalObject* arg #ifdef __cplusplus -ZIG_DECL void Zig__GlobalObject__fetch(ErrorableResolvedSource* arg0, JSC__JSGlobalObject* arg1, ZigString* arg2, ZigString* arg3); +ZIG_DECL void Zig__GlobalObject__fetch(ErrorableResolvedSource* arg0, JSC__JSGlobalObject* arg1, BunString* arg2, BunString* arg3); ZIG_DECL ErrorableZigString Zig__GlobalObject__import(JSC__JSGlobalObject* arg0, ZigString* arg1, ZigString* arg2); ZIG_DECL void Zig__GlobalObject__onCrash(); ZIG_DECL JSC__JSValue Zig__GlobalObject__promiseRejectionTracker(JSC__JSGlobalObject* arg0, JSC__JSPromise* arg1, uint32_t JSPromiseRejectionOperation2); ZIG_DECL JSC__JSValue Zig__GlobalObject__reportUncaughtException(JSC__JSGlobalObject* arg0, JSC__Exception* arg1); -ZIG_DECL void Zig__GlobalObject__resolve(ErrorableZigString* arg0, JSC__JSGlobalObject* arg1, ZigString* arg2, ZigString* arg3); +ZIG_DECL void Zig__GlobalObject__resolve(ErrorableZigString* arg0, JSC__JSGlobalObject* arg1, BunString* arg2, BunString* arg3); #endif diff --git a/src/bun.js/bindings/headers.zig b/src/bun.js/bindings/headers.zig index e99db7ef0..3127f6598 100644 --- a/src/bun.js/bindings/headers.zig +++ b/src/bun.js/bindings/headers.zig @@ -18,6 +18,7 @@ pub const struct_JSC__AsyncGeneratorPrototype = bindings.AsyncGeneratorPrototype pub const struct_JSC__AsyncGeneratorFunctionPrototype = bindings.AsyncGeneratorFunctionPrototype; pub const struct_JSC__AsyncFunctionPrototype = bindings.AsyncFunctionPrototype; pub const struct_JSC__ArrayPrototype = bindings.ArrayPrototype; +pub const BunString = @import("bun").String; pub const struct_JSC__ArrayIteratorPrototype = bindings.ArrayIteratorPrototype; pub const bWTF__URL = bindings.URL; diff --git a/src/bun.js/bindings/helpers.h b/src/bun.js/bindings/helpers.h index da97b4797..c52c17997 100644 --- a/src/bun.js/bindings/helpers.h +++ b/src/bun.js/bindings/helpers.h @@ -78,7 +78,7 @@ namespace Zig { static const unsigned char* untag(const unsigned char* ptr) { return reinterpret_cast<const unsigned char*>( - ((reinterpret_cast<uintptr_t>(ptr) & ~(static_cast<uint64_t>(1) << 63) & ~(static_cast<uint64_t>(1) << 62)) & ~(static_cast<uint64_t>(1) << 61))); + (((reinterpret_cast<uintptr_t>(ptr) & ~(static_cast<uint64_t>(1) << 63) & ~(static_cast<uint64_t>(1) << 62)) & ~(static_cast<uint64_t>(1) << 61)) & ~(static_cast<uint64_t>(1) << 60))); } static void* untagVoid(const unsigned char* ptr) @@ -148,6 +148,26 @@ static const WTF::String toString(ZigString str) reinterpret_cast<const UChar*>(untag(str.ptr)), str.len)); } +static const WTF::String toStringStatic(ZigString str) +{ + if (str.len == 0 || str.ptr == nullptr) { + return WTF::String(); + } + if (UNLIKELY(isTaggedUTF8Ptr(str.ptr))) { + abort(); + } + + if (isTaggedUTF16Ptr(str.ptr)) { + return WTF::String(WTF::ExternalStringImpl::createStatic(untag(str.ptr), str.len)); + } + + + return WTF::String(WTF::ExternalStringImpl::createStatic( + reinterpret_cast<const UChar*>(untag(str.ptr)), str.len)); + + +} + static WTF::AtomString toAtomString(ZigString str) { |