diff options
Diffstat (limited to 'src/javascript')
-rw-r--r-- | src/javascript/jsc/api/router.zig | 111 | ||||
-rw-r--r-- | src/javascript/jsc/base.zig | 124 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/ZigGlobalObject.cpp | 185 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings-generator.zig | 4 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 43 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/exports.zig | 205 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/header-gen.zig | 27 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-replacements.zig | 5 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.h | 9 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.zig | 85 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/shared.zig | 9 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/shimmer.zig | 19 | ||||
-rw-r--r-- | src/javascript/jsc/config.zig | 15 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 340 | ||||
-rw-r--r-- | src/javascript/jsc/javascript_core_c_api.zig (renamed from src/javascript/jsc/JavascriptCore.zig) | 50 | ||||
-rw-r--r-- | src/javascript/jsc/typescript.zig | 5 | ||||
-rw-r--r-- | src/javascript/jsc/webcore/response.zig | 324 |
18 files changed, 896 insertions, 666 deletions
diff --git a/src/javascript/jsc/api/router.zig b/src/javascript/jsc/api/router.zig index 59d26a82a..66aca8349 100644 --- a/src/javascript/jsc/api/router.zig +++ b/src/javascript/jsc/api/router.zig @@ -1,4 +1,3 @@ -usingnamespace @import("../base.zig"); const std = @import("std"); const Api = @import("../../../api/schema.zig").Api; const FilesystemRouter = @import("../../../router.zig"); @@ -6,13 +5,29 @@ const http = @import("../../../http.zig"); const JavaScript = @import("../javascript.zig"); const QueryStringMap = @import("../../../query_string_map.zig").QueryStringMap; const CombinedScanner = @import("../../../query_string_map.zig").CombinedScanner; -usingnamespace @import("../bindings/bindings.zig"); -usingnamespace @import("../webcore/response.zig"); +const _global = @import("../../../global.zig"); +const string = _global.string; +const JSC = @import("javascript_core"); +const js = JSC.C; +const WebCore = @import("../webcore/response.zig"); const Router = @This(); const Bundler = @import("../../../bundler.zig"); const VirtualMachine = JavaScript.VirtualMachine; const ScriptSrcStream = std.io.FixedBufferStream([]u8); +const ZigString = JSC.ZigString; const Fs = @import("../../../fs.zig"); +const Base = @import("../base.zig"); +const getAllocator = Base.getAllocator; +const JSObject = JSC.JSObject; +const JSError = Base.JSError; +const JSValue = JSC.JSValue; +const JSGlobalObject = JSC.JSGlobalObject; +const strings = @import("strings"); +const NewClass = Base.NewClass; +const To = Base.To; +const Request = WebCore.Request; +const d = Base.d; +const FetchEvent = WebCore.FetchEvent; route: *const FilesystemRouter.Match, query_string_map: ?QueryStringMap = null, @@ -24,13 +39,13 @@ script_src_buf_writer: ScriptSrcStream = undefined, pub fn importRoute( this: *Router, - ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSObjectRef { - const prom = JSModuleLoader.loadAndEvaluateModule(VirtualMachine.vm.global, &ZigString.init(this.route.file_path)); + const prom = JSC.JSModuleLoader.loadAndEvaluateModule(VirtualMachine.vm.global, &ZigString.init(this.route.file_path)); VirtualMachine.vm.tick(); @@ -38,10 +53,10 @@ pub fn importRoute( } pub fn match( - obj: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSObjectRef { @@ -74,15 +89,15 @@ fn matchRequest( } fn matchPathNameString( - ctx: js.JSContextRef, - pathname: string, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: string, + _: js.ExceptionRef, ) js.JSObjectRef {} fn matchPathName( - ctx: js.JSContextRef, - pathname: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSObjectRef { return null; } @@ -95,7 +110,7 @@ fn matchFetchEvent( return createRouteObject(ctx, fetch_event.request_context, exception); } -fn createRouteObject(ctx: js.JSContextRef, req: *const http.RequestContext, exception: js.ExceptionRef) js.JSValueRef { +fn createRouteObject(ctx: js.JSContextRef, req: *const http.RequestContext, _: js.ExceptionRef) js.JSValueRef { const route = &(req.matched_route orelse { return js.JSValueMakeNull(ctx); }); @@ -259,10 +274,10 @@ pub const Instance = NewClass( pub fn getFilePath( this: *Router, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(this.route.file_path).toValue(VirtualMachine.vm.global).asRef(); } @@ -277,20 +292,20 @@ pub fn finalize( pub fn getPathname( this: *Router, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(this.route.pathname).toValue(VirtualMachine.vm.global).asRef(); } pub fn getRoute( this: *Router, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(this.route.name).toValue(VirtualMachine.vm.global).asRef(); } @@ -317,17 +332,17 @@ const KindEnum = struct { pub fn getKind( this: *Router, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return KindEnum.init(this.route.name).toValue(VirtualMachine.vm.global).asRef(); } threadlocal var query_string_values_buf: [256]string = undefined; threadlocal var query_string_value_refs_buf: [256]ZigString = undefined; -pub fn createQueryObject(ctx: js.JSContextRef, map: *QueryStringMap, exception: js.ExceptionRef) callconv(.C) js.JSValueRef { +pub fn createQueryObject(_: js.JSContextRef, map: *QueryStringMap, _: js.ExceptionRef) callconv(.C) js.JSValueRef { const QueryObjectCreator = struct { query: *QueryStringMap, pub fn create(this: *@This(), obj: *JSObject, global: *JSGlobalObject) void { @@ -342,11 +357,11 @@ pub fn createQueryObject(ctx: js.JSContextRef, map: *QueryStringMap, exception: for (entry.values) |value, i| { values[i] = ZigString.init(value); } - obj.putRecord(VirtualMachine.vm.global, &str, values.ptr, values.len); + obj.putRecord(global, &str, values.ptr, values.len); } else { query_string_value_refs_buf[0] = ZigString.init(entry.values[0]); - obj.putRecord(VirtualMachine.vm.global, &str, &query_string_value_refs_buf, 1); + obj.putRecord(global, &str, &query_string_value_refs_buf, 1); } } } @@ -386,9 +401,9 @@ pub fn getScriptSrcString( pub fn getScriptSrc( this: *Router, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { const src = this.script_src orelse brk: { getScriptSrcString(ScriptSrcStream.Writer, this.script_src_buf_writer.writer(), this.route.file_path, this.route.client_framework_enabled); @@ -403,8 +418,8 @@ pub fn getScriptSrc( pub fn getParams( this: *Router, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, + _: js.JSObjectRef, + _: js.JSStringRef, exception: js.ExceptionRef, ) js.JSValueRef { if (this.param_map == null) { @@ -416,7 +431,7 @@ pub fn getParams( this.route.params, ))) |map| { this.param_map = map; - } else |err| {} + } else |_| {} } } @@ -431,8 +446,8 @@ pub fn getParams( pub fn getQuery( this: *Router, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, + _: js.JSObjectRef, + _: js.JSStringRef, exception: js.ExceptionRef, ) js.JSValueRef { if (this.query_string_map == null) { @@ -445,11 +460,11 @@ pub fn getQuery( this.route.params, ))) |map| { this.query_string_map = map; - } else |err| {} + } else |_| {} } else if (this.route.query_string.len > 0) { if (QueryStringMap.init(getAllocator(ctx), this.route.query_string)) |map| { this.query_string_map = map; - } else |err| {} + } else |_| {} } } diff --git a/src/javascript/jsc/base.zig b/src/javascript/jsc/base.zig index 64a0f15a3..f51eb86d9 100644 --- a/src/javascript/jsc/base.zig +++ b/src/javascript/jsc/base.zig @@ -1,10 +1,26 @@ -pub const js = @import("./JavascriptCore.zig"); +pub const js = @import("../../jsc.zig").C; const std = @import("std"); -pub usingnamespace @import("../../global.zig"); -usingnamespace @import("./javascript.zig"); -usingnamespace @import("./webcore/response.zig"); +const _global = @import("../../global.zig"); +const string = _global.string; +const Output = _global.Output; +const Global = _global.Global; +const Environment = _global.Environment; +const strings = _global.strings; +const MutableString = _global.MutableString; +const stringZ = _global.stringZ; +const default_allocator = _global.default_allocator; +const C = _global.C; +const JavaScript = @import("./javascript.zig"); +const ResolveError = JavaScript.ResolveError; +const BuildError = JavaScript.BuildError; +const WebCore = @import("./webcore/response.zig"); +const Fetch = WebCore.Fetch; +const Response = WebCore.Response; +const Request = WebCore.Request; const Router = @import("./api/router.zig"); - +const FetchEvent = WebCore.FetchEvent; +const Headers = WebCore.Headers; +const Body = WebCore.Body; const TaggedPointerTypes = @import("../../tagged_pointer.zig"); const TaggedPointerUnion = TaggedPointerTypes.TaggedPointerUnion; @@ -18,7 +34,7 @@ fn ObjectPtrType(comptime Type: type) type { pub const To = struct { pub const JS = struct { - pub inline fn str(ref: anytype, val: anytype) js.JSStringRef { + pub inline fn str(_: anytype, val: anytype) js.JSStringRef { return js.JSStringCreateWithUTF8CString(val[0.. :0]); } @@ -132,9 +148,7 @@ pub const To = struct { arguments: [*c]const js.JSValueRef, exception: js.ExceptionRef, ) callconv(.C) js.JSValueRef { - var object_ptr: *c_void = undefined; - - if (comptime ZigContextType == c_void) { + if (comptime ZigContextType == anyopaque) { return ctxfn( js.JSObjectGetPrivate(function) or js.jsObjectGetPrivate(thisObject), ctx, @@ -291,7 +305,7 @@ pub const Properties = struct { @field(UTF8, name).len, ); - if (comptime isDebug) { + if (comptime Environment.isDebug) { std.debug.assert( js.JSStringIsEqualToString(@field(Refs, name), @field(UTF8, name).ptr, @field(UTF8, name).len), ); @@ -607,7 +621,6 @@ pub const d = struct { } const qualifier = if (!klass.default_export) "export " else ""; - var stmt: string = qualifier; if (klass.interface) { buf = buf ++ printIndented("export interface {s} {{\n", .{klass.name}, indent); @@ -618,7 +631,7 @@ pub const d = struct { indent += indent_level; var did_print_constructor = false; - for (klass.functions) |func, i| { + for (klass.functions) |func| { if (!strings.eqlComptime(func.name, "constructor")) continue; did_print_constructor = true; buf = buf ++ printInstanceFunction( @@ -781,7 +794,7 @@ pub fn NewClass( pub fn rfn( ctx: js.JSContextRef, function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, argumentCount: usize, arguments: [*c]const js.JSValueRef, exception: js.ExceptionRef, @@ -790,17 +803,17 @@ pub fn NewClass( } }; - pub fn throwInvalidConstructorError(ctx: js.JSContextRef, obj: js.JSObjectRef, c: usize, a: [*c]const js.JSValueRef, exception: js.ExceptionRef) callconv(.C) js.JSObjectRef { + pub fn throwInvalidConstructorError(ctx: js.JSContextRef, _: js.JSObjectRef, _: usize, _: [*c]const js.JSValueRef, exception: js.ExceptionRef) callconv(.C) js.JSObjectRef { JSError(getAllocator(ctx), "" ++ name ++ " is not a constructor", .{}, ctx, exception); return null; } pub fn throwInvalidFunctionError( ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - argumentCount: usize, - arguments: [*c]const js.JSValueRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: usize, + _: [*c]const js.JSValueRef, exception: js.ExceptionRef, ) callconv(.C) js.JSValueRef { JSError(getAllocator(ctx), "" ++ name ++ " is not a function", .{}, ctx, exception); @@ -823,28 +836,13 @@ pub fn NewClass( return &ref; } - pub fn RawGetter(comptime ReceiverType: type) type { - const ClassGetter = struct { - pub fn getter( - ctx: js.JSContextRef, - obj: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, - ) callconv(.C) js.JSValueRef { - return js.JSObjectMake(ctx, get().*, null); - } - }; - - return ClassGetter; - } - - pub fn customHasInstance(ctx: js.JSContextRef, obj: js.JSObjectRef, value: js.JSValueRef, exception: js.ExceptionRef) callconv(.C) bool { + pub fn customHasInstance(ctx: js.JSContextRef, _: js.JSObjectRef, value: js.JSValueRef, _: js.ExceptionRef) callconv(.C) bool { return js.JSValueIsObjectOfClass(ctx, value, get().*); } pub fn make(ctx: js.JSContextRef, ptr: *ZigType) js.JSObjectRef { var real_ptr = JSPrivateDataPtr.init(ptr).ptr(); - if (comptime isDebug) { + if (comptime Environment.allow_assert) { std.debug.assert(JSPrivateDataPtr.isValidPtr(real_ptr)); std.debug.assert(JSPrivateDataPtr.from(real_ptr).get(ZigType).? == ptr); } @@ -855,7 +853,7 @@ pub fn NewClass( real_ptr, ); - if (comptime isDebug) { + if (comptime Environment.allow_assert) { std.debug.assert(JSPrivateDataPtr.from(js.JSObjectGetPrivate(result)).ptr() == real_ptr); } @@ -873,10 +871,10 @@ pub fn NewClass( pub const ts = typescriptDeclaration(); pub fn rfn( - receiver: *ReceiverType, + _: *ReceiverType, ctx: js.JSContextRef, - obj: js.JSObjectRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSObjectMake(ctx, get().*, null); } @@ -894,26 +892,24 @@ pub fn NewClass( var pointer = GetJSPrivateData(ZigType, obj) orelse return js.JSValueMakeUndefined(ctx); if (singleton) { - inline for (function_names) |propname, i| { + inline for (function_names) |_, i| { if (js.JSStringIsEqual(prop, function_name_refs[i])) { return instance_functions[i]; } } - if (comptime std.meta.trait.hasFn("onMissingProperty")(ZigType)) { - return ptr.onMissingProperty(ctx, obj, prop, exception); - } + unreachable; } else { inline for (property_names) |propname, i| { if (js.JSStringIsEqual(prop, property_name_refs[i])) { return @field( properties, propname, - )(ptr, ctx, obj, exception); + )(pointer, ctx, obj, exception); } } if (comptime std.meta.trait.hasFn("onMissingProperty")(ZigType)) { - return ptr.onMissingProperty(ctx, obj, prop, exception); + return pointer.onMissingProperty(ctx, obj, prop, exception); } } @@ -930,7 +926,6 @@ pub fn NewClass( ) callconv(.C) js.JSValueRef { var this: ObjectPtrType(ZigType) = if (comptime ZigType == void) void{} else GetJSPrivateData(ZigType, obj) orelse return js.JSValueMakeUndefined(ctx); - var exc: js.JSValueRef = null; const Field = @TypeOf(@field( properties, property_names[id], @@ -965,13 +960,6 @@ pub fn NewClass( js.ExceptionRef, ) js.JSValueRef; - const WithoutPropFn = fn ( - ObjectPtrType(ZigType), - js.JSContextRef, - js.JSObjectRef, - js.ExceptionRef, - ) js.JSValueRef; - if (Func.Fn.args.len == @typeInfo(WithPropFn).Fn.args.len) { return func( this, @@ -1002,8 +990,6 @@ pub fn NewClass( ) callconv(.C) bool { var this = GetJSPrivateData(ZigType, obj) orelse return js.JSValueMakeUndefined(ctx); - var exc: js.ExceptionRef = null; - switch (comptime @typeInfo(@TypeOf(@field( properties, property_names[id], @@ -1040,7 +1026,7 @@ pub fn NewClass( if (static_functions.len > 0) { var count: usize = 0; - inline for (function_name_literals) |function_name, i| { + inline for (function_name_literals) |_, i| { const func = @field(staticFunctions, function_names[i]); const Func = @TypeOf(func); @@ -1062,7 +1048,7 @@ pub fn NewClass( class.functions = std.mem.span(&funcs); var func_i: usize = 0; - inline for (function_name_literals) |function_name, i| { + inline for (function_name_literals) |_, i| { const func = @field(staticFunctions, function_names[i]); const Func = @TypeOf(func); @@ -1104,7 +1090,7 @@ pub fn NewClass( var count: usize = 0; var class_count: usize = 0; - inline for (property_names) |property_name, i| { + inline for (property_names) |_, i| { const field = @field(properties, property_names[i]); const Field = @TypeOf(field); @@ -1209,7 +1195,7 @@ pub fn NewClass( if (static_functions.len > 0) { var count: usize = 0; - inline for (function_name_literals) |function_name, i| { + inline for (function_name_literals) |_, i| { const func = @field(staticFunctions, function_names[i]); const Func = @TypeOf(func); @@ -1231,7 +1217,7 @@ pub fn NewClass( class.functions = std.mem.span(&funcs); var func_i: usize = 0; - inline for (function_name_literals) |function_name, i| { + inline for (function_name_literals) |_, i| { const func = @field(staticFunctions, function_names[i]); const Func = @TypeOf(func); @@ -1275,7 +1261,7 @@ pub fn NewClass( if (property_names.len > 0) { var count: usize = 0; - inline for (property_names) |property_name, i| { + inline for (property_names) |_, i| { const field = @field(properties, property_names[i]); if (hasTypeScript(@TypeOf(field))) { @@ -1339,7 +1325,7 @@ pub fn NewClass( if (static_functions.len > 0) { std.mem.set(js.JSStaticFunction, &static_functions, std.mem.zeroes(js.JSStaticFunction)); var count: usize = 0; - inline for (function_name_literals) |function_name, i| { + inline for (function_name_literals) |_, i| { switch (comptime @typeInfo(@TypeOf(@field(staticFunctions, function_names[i])))) { .Struct => { if (comptime strings.eqlComptime(function_names[i], "constructor")) { @@ -1466,7 +1452,7 @@ pub fn NewClass( threadlocal var error_args: [1]js.JSValueRef = undefined; pub fn JSError( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, comptime fmt: string, args: anytype, ctx: js.JSContextRef, @@ -1489,7 +1475,7 @@ pub fn JSError( } } -pub fn getAllocator(ctx: js.JSContextRef) *std.mem.Allocator { +pub fn getAllocator(_: js.JSContextRef) std.mem.Allocator { return default_allocator; } @@ -1512,9 +1498,9 @@ pub const ArrayBuffer = struct { pub const MarkedArrayBuffer = struct { buffer: ArrayBuffer, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, - 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: js.JSTypedArrayType) 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 }, .allocator = allocator, @@ -1527,7 +1513,7 @@ pub const MarkedArrayBuffer = struct { content.allocator.destroy(this); } - pub fn init(allocator: *std.mem.Allocator, size: u32, typed_array_type: js.JSTypedArrayType) !*MarkedArrayBuffer { + pub fn init(allocator: std.mem.Allocator, size: u32, typed_array_type: js.JSTypedArrayType) !*MarkedArrayBuffer { const bytes = try allocator.alloc(u8, size); var container = try allocator.create(MarkedArrayBuffer); container.* = MarkedArrayBuffer.fromBytes(bytes, allocator, typed_array_type); @@ -1539,10 +1525,10 @@ pub const MarkedArrayBuffer = struct { } }; -export fn MarkedArrayBuffer_deallocator(bytes_: *c_void, ctx_: *c_void) void { +export fn MarkedArrayBuffer_deallocator(bytes_: *anyopaque, ctx_: *anyopaque) void { var ctx = @ptrCast(*MarkedArrayBuffer, @alignCast(@alignOf(*MarkedArrayBuffer), ctx_)); - if (comptime isDebug) std.debug.assert(ctx.buffer.ptr == @ptrCast([*]u8, bytes_)); + if (comptime Environment.allow_assert) std.debug.assert(ctx.buffer.ptr == @ptrCast([*]u8, bytes_)); ctx.destroy(); } diff --git a/src/javascript/jsc/bindings/ZigGlobalObject.cpp b/src/javascript/jsc/bindings/ZigGlobalObject.cpp index d21497d20..e113e4df2 100644 --- a/src/javascript/jsc/bindings/ZigGlobalObject.cpp +++ b/src/javascript/jsc/bindings/ZigGlobalObject.cpp @@ -121,6 +121,99 @@ extern "C" JSC__JSGlobalObject *Zig__GlobalObject__create(JSClassRef *globalObje return globalObject; } +extern "C" void *Zig__GlobalObject__getModuleRegistryMap(JSC__JSGlobalObject *arg0) { + if (JSC::JSObject *loader = + JSC::jsDynamicCast<JSC::JSObject *>(arg0->vm(), arg0->moduleLoader())) { + JSC::JSMap *map = JSC::jsDynamicCast<JSC::JSMap *>( + arg0->vm(), + loader->getDirect(arg0->vm(), JSC::Identifier::fromString(arg0->vm(), "registry"))); + + JSC::JSMap *cloned = map->clone(arg0, arg0->vm(), arg0->mapStructure()); + JSC::gcProtect(cloned); + + return cloned; + } + + return nullptr; +} + +extern "C" bool Zig__GlobalObject__resetModuleRegistryMap(JSC__JSGlobalObject *globalObject, + void *map_ptr) { + if (map_ptr == nullptr) return false; + JSC::JSMap *map = reinterpret_cast<JSC::JSMap *>(map_ptr); + JSC::VM &vm = globalObject->vm(); + if (JSC::JSObject *obj = + JSC::jsDynamicCast<JSC::JSObject *>(globalObject->vm(), globalObject->moduleLoader())) { + auto identifier = JSC::Identifier::fromString(globalObject->vm(), "registry"); + + if (JSC::JSMap *oldMap = JSC::jsDynamicCast<JSC::JSMap *>( + globalObject->vm(), obj->getDirect(globalObject->vm(), identifier))) { + oldMap->clear(globalObject); + // vm.finalizeSynchronousJSExecution(); + + obj->putDirect(globalObject->vm(), identifier, + map->clone(globalObject, globalObject->vm(), globalObject->mapStructure())); + + // vm.deleteAllLinkedCode(JSC::DeleteAllCodeEffort::DeleteAllCodeIfNotCollecting); + // JSC::Heap::PreventCollectionScope(vm.heap); + + // vm.heap.completeAllJITPlans(); + + // vm.forEachScriptExecutableSpace([&](auto &spaceAndSet) { + // JSC::HeapIterationScope heapIterationScope(vm.heap); + // auto &set = spaceAndSet.set; + // set.forEachLiveCell([&](JSC::HeapCell *cell, JSC::HeapCell::Kind) { + // if (JSC::ModuleProgramExecutable *executable = + // JSC::jsDynamicCast<JSC::ModuleProgramExecutable *>(cell)) { + // executable->clearCode(set); + // } + // }); + // }); + } + // globalObject->vm().heap.deleteAllUnlinkedCodeBlocks( + // JSC::DeleteAllCodeEffort::PreventCollectionAndDeleteAllCode); + // vm.whenIdle([globalObject, oldMap, map]() { + // auto recordIdentifier = JSC::Identifier::fromString(globalObject->vm(), "module"); + + // JSC::JSModuleRecord *record; + // JSC::JSValue key; + // JSC::JSValue value; + // JSC::JSObject *mod; + // JSC::JSObject *nextObject; + // JSC::forEachInIterable( + // globalObject, oldMap, + // [&](JSC::VM &vm, JSC::JSGlobalObject *globalObject, JSC::JSValue nextValue) { + // nextObject = JSC::jsDynamicCast<JSC::JSObject *>(vm, nextValue); + // key = nextObject->getIndex(globalObject, static_cast<unsigned>(0)); + + // if (!map->has(globalObject, key)) { + // value = nextObject->getIndex(globalObject, static_cast<unsigned>(1)); + // mod = JSC::jsDynamicCast<JSC::JSObject *>(vm, value); + // if (mod) { + // record = JSC::jsDynamicCast<JSC::JSModuleRecord *>( + // vm, mod->getDirect(vm, recordIdentifier)); + // if (record) { + // auto code = &record->sourceCode(); + // if (code) { + + // Zig::SourceProvider *provider = + // reinterpret_cast<Zig::SourceProvider *>(code->provider()); + // // code->~SourceCode(); + // if (provider) { provider->freeSourceCode(); } + // } + // } + // } + // } + // }); + + // oldMap->clear(globalObject); + // } + // } + // map + } + return true; +} + namespace Zig { const JSC::ClassInfo GlobalObject::s_info = {"GlobalObject", &Base::s_info, nullptr, nullptr, @@ -274,98 +367,6 @@ JSC::JSInternalPromise *GlobalObject::moduleLoaderImportModule(JSGlobalObject *g return result; } -extern "C" void *Zig__GlobalObject__getModuleRegistryMap(JSC__JSGlobalObject *arg0) { - if (JSC::JSObject *loader = - JSC::jsDynamicCast<JSC::JSObject *>(arg0->vm(), arg0->moduleLoader())) { - JSC::JSMap *map = JSC::jsDynamicCast<JSC::JSMap *>( - arg0->vm(), - loader->getDirect(arg0->vm(), JSC::Identifier::fromString(arg0->vm(), "registry"))); - - JSC::JSMap *cloned = map->clone(arg0, arg0->vm(), arg0->mapStructure()); - JSC::gcProtect(cloned); - - return cloned; - } - - return nullptr; -} - -extern "C" bool Zig__GlobalObject__resetModuleRegistryMap(JSC__JSGlobalObject *globalObject, - void *map_ptr) { - if (map_ptr == nullptr) return false; - JSC::JSMap *map = reinterpret_cast<JSC::JSMap *>(map_ptr); - JSC::VM &vm = globalObject->vm(); - if (JSC::JSObject *obj = - JSC::jsDynamicCast<JSC::JSObject *>(globalObject->vm(), globalObject->moduleLoader())) { - auto identifier = JSC::Identifier::fromString(globalObject->vm(), "registry"); - - if (JSC::JSMap *oldMap = JSC::jsDynamicCast<JSC::JSMap *>( - globalObject->vm(), obj->getDirect(globalObject->vm(), identifier))) { - oldMap->clear(globalObject); - // vm.finalizeSynchronousJSExecution(); - - obj->putDirect(globalObject->vm(), identifier, - map->clone(globalObject, globalObject->vm(), globalObject->mapStructure())); - - // vm.deleteAllLinkedCode(JSC::DeleteAllCodeEffort::DeleteAllCodeIfNotCollecting); - // JSC::Heap::PreventCollectionScope(vm.heap); - - // vm.heap.completeAllJITPlans(); - - // vm.forEachScriptExecutableSpace([&](auto &spaceAndSet) { - // JSC::HeapIterationScope heapIterationScope(vm.heap); - // auto &set = spaceAndSet.set; - // set.forEachLiveCell([&](JSC::HeapCell *cell, JSC::HeapCell::Kind) { - // if (JSC::ModuleProgramExecutable *executable = - // JSC::jsDynamicCast<JSC::ModuleProgramExecutable *>(cell)) { - // executable->clearCode(set); - // } - // }); - // }); - } - // globalObject->vm().heap.deleteAllUnlinkedCodeBlocks( - // JSC::DeleteAllCodeEffort::PreventCollectionAndDeleteAllCode); - // vm.whenIdle([globalObject, oldMap, map]() { - // auto recordIdentifier = JSC::Identifier::fromString(globalObject->vm(), "module"); - - // JSC::JSModuleRecord *record; - // JSC::JSValue key; - // JSC::JSValue value; - // JSC::JSObject *mod; - // JSC::JSObject *nextObject; - // JSC::forEachInIterable( - // globalObject, oldMap, - // [&](JSC::VM &vm, JSC::JSGlobalObject *globalObject, JSC::JSValue nextValue) { - // nextObject = JSC::jsDynamicCast<JSC::JSObject *>(vm, nextValue); - // key = nextObject->getIndex(globalObject, static_cast<unsigned>(0)); - - // if (!map->has(globalObject, key)) { - // value = nextObject->getIndex(globalObject, static_cast<unsigned>(1)); - // mod = JSC::jsDynamicCast<JSC::JSObject *>(vm, value); - // if (mod) { - // record = JSC::jsDynamicCast<JSC::JSModuleRecord *>( - // vm, mod->getDirect(vm, recordIdentifier)); - // if (record) { - // auto code = &record->sourceCode(); - // if (code) { - - // Zig::SourceProvider *provider = - // reinterpret_cast<Zig::SourceProvider *>(code->provider()); - // // code->~SourceCode(); - // if (provider) { provider->freeSourceCode(); } - // } - // } - // } - // } - // }); - - // oldMap->clear(globalObject); - // } - // } - // map - } - return true; -} JSC::JSInternalPromise *GlobalObject::moduleLoaderFetch(JSGlobalObject *globalObject, JSModuleLoader *loader, JSValue key, JSValue value1, JSValue value2) { diff --git a/src/javascript/jsc/bindings/bindings-generator.zig b/src/javascript/jsc/bindings/bindings-generator.zig index 7e23ab636..cdf87a0d1 100644 --- a/src/javascript/jsc/bindings/bindings-generator.zig +++ b/src/javascript/jsc/bindings/bindings-generator.zig @@ -1,7 +1,8 @@ const Bindings = @import("bindings.zig"); +const Exports = @import("exports.zig"); const HeaderGen = @import("./header-gen.zig").HeaderGen; const std = @import("std"); -const builtin = std.builtin; +const builtin = @import("builtin"); const io = std.io; const fs = std.fs; const process = std.process; @@ -27,6 +28,7 @@ pub fn main() anyerror!void { const HeaderGenerator = HeaderGen( Bindings, + Exports, "src/javascript/jsc/bindings/bindings.zig", ); HeaderGenerator.exec(HeaderGenerator{}, file, cpp); diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index d59065bad..b0d760853 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -1,9 +1,19 @@ -usingnamespace @import("./shared.zig"); -usingnamespace @import("./headers.zig"); pub const Shimmer = @import("./shimmer.zig").Shimmer; +const std = @import("std"); +const _global = @import("../../../global.zig"); +const string = _global.string; +const Output = _global.Output; const hasRef = std.meta.trait.hasField("ref"); -const C_API = @import("../JavascriptCore.zig"); +const C_API = @import("../../../jsc.zig").C; const StringPointer = @import("../../../api/schema.zig").Api.StringPointer; +const Exports = @import("./exports.zig"); + +const ErrorableZigString = Exports.ErrorableZigString; +const ErrorableResolvedSource = Exports.ErrorableResolvedSource; +const ZigException = Exports.ZigException; +const ZigStackTrace = Exports.ZigStackTrace; +const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false; + pub const JSObject = extern struct { pub const shim = Shimmer("JSC", "JSObject", @This()); bytes: shim.Bytes, @@ -18,8 +28,8 @@ pub const JSObject = extern struct { }); } - const InitializeCallback = fn (ctx: ?*c_void, obj: [*c]JSObject, global: [*c]JSGlobalObject) callconv(.C) void; - pub fn create(global_object: *JSGlobalObject, length: usize, ctx: *c_void, initializer: InitializeCallback) JSValue { + const InitializeCallback = fn (ctx: ?*anyopaque, obj: [*c]JSObject, global: [*c]JSGlobalObject) callconv(.C) void; + pub fn create(global_object: *JSGlobalObject, length: usize, ctx: *anyopaque, initializer: InitializeCallback) JSValue { return cppFn("create", .{ global_object, length, @@ -30,7 +40,7 @@ pub const JSObject = extern struct { pub fn Initializer(comptime Ctx: type, comptime func: fn (*Ctx, obj: *JSObject, global: *JSGlobalObject) void) type { return struct { - pub fn call(this: ?*c_void, obj: [*c]JSObject, global: [*c]JSGlobalObject) callconv(.C) void { + pub fn call(this: ?*anyopaque, obj: [*c]JSObject, global: [*c]JSGlobalObject) callconv(.C) void { @call(.{ .modifier = .always_inline }, func, .{ @ptrCast(*Ctx, @alignCast(@alignOf(*Ctx), this.?)), obj.?, global.? }); } }; @@ -625,7 +635,7 @@ pub const JSFunction = extern struct { pub const name = "JSC::JSFunction"; pub const namespace = "JSC"; - pub const NativeFunctionCallback = fn (ctx: ?*c_void, global: [*c]JSGlobalObject, call_frame: [*c]CallFrame) callconv(.C) JSValue; + pub const NativeFunctionCallback = fn (ctx: ?*anyopaque, global: [*c]JSGlobalObject, call_frame: [*c]CallFrame) callconv(.C) JSValue; // pub fn createFromSourceCode( // global: *JSGlobalObject, @@ -652,7 +662,7 @@ pub const JSFunction = extern struct { global: *JSGlobalObject, argument_count: u16, name_: ?*const String, - ctx: ?*c_void, + ctx: ?*anyopaque, func: NativeFunctionCallback, ) *JSFunction { return cppFn("createFromNative", .{ global, argument_count, name_, ctx, func }); @@ -921,7 +931,7 @@ pub const JSGlobalObject = extern struct { return cppFn("asyncGeneratorFunctionPrototype", .{this}); } - pub fn createAggregateError(globalObject: *JSGlobalObject, errors: [*]*c_void, errors_len: u16, message: *const ZigString) JSValue { + pub fn createAggregateError(globalObject: *JSGlobalObject, errors: [*]*anyopaque, errors_len: u16, message: *const ZigString) JSValue { return cppFn("createAggregateError", .{ globalObject, errors, errors_len, message }); } @@ -1503,8 +1513,8 @@ pub const JSValue = enum(i64) { return @ptrCast(C_API.JSObjectRef, this.asVoid()); } - pub inline fn asVoid(this: JSValue) *c_void { - return @intToPtr(*c_void, @intCast(usize, @enumToInt(this))); + pub inline fn asVoid(this: JSValue) *anyopaque { + return @intToPtr(*anyopaque, @intCast(usize, @enumToInt(this))); } pub const Extern = [_][]const u8{ "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" }; @@ -1513,8 +1523,6 @@ pub const JSValue = enum(i64) { extern "c" fn Microtask__run(*Microtask, *JSGlobalObject) void; pub const Microtask = opaque { - pub const name = "Zig::JSMicrotaskCallback"; - pub fn run(this: *Microtask, global_object: *JSGlobalObject) void { if (comptime is_bindgen) { return; @@ -1721,7 +1729,7 @@ pub const ThrowScope = extern struct { pub fn declare( vm: *VM, - function_name: [*]u8, + _: [*]u8, file: [*]u8, line: usize, ) ThrowScope { @@ -1764,7 +1772,7 @@ pub const CatchScope = extern struct { file: [*]u8, line: usize, ) CatchScope { - return cppFn("declare", .{ vm, file, line }); + return cppFn("declare", .{ vm, function_name, file, line }); } pub fn exception(this: *CatchScope) ?*Exception { @@ -1802,6 +1810,7 @@ pub const CallFrame = extern struct { pub inline fn argument(call_frame: *const CallFrame, i: u16) JSValue { return cppFn("argument", .{ call_frame, + i, }); } pub inline fn thisValue(call_frame: *const CallFrame) ?JSValue { @@ -1960,7 +1969,7 @@ pub const Identifier = extern struct { }; }; -const DeinitFunction = fn (ctx: *c_void, buffer: [*]u8, len: usize) callconv(.C) void; +const DeinitFunction = fn (ctx: *anyopaque, buffer: [*]u8, len: usize) callconv(.C) void; pub const StringImpl = extern struct { pub const shim = Shimmer("WTF", "StringImpl", @This()); @@ -2136,8 +2145,6 @@ pub const StringView = extern struct { }; }; -pub usingnamespace @import("exports.zig"); - pub const Callback = struct { // zig: Value, }; diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index 00f58e745..7491f9e0a 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -1,13 +1,35 @@ -usingnamespace @import("./bindings.zig"); +const JSC = @import("./bindings.zig"); usingnamespace @import("./shared.zig"); const Fs = @import("../../../fs.zig"); -const CAPI = @import("../JavascriptCore.zig"); +const CAPI = @import("../../../jsc.zig").C; const JS = @import("../javascript.zig"); const JSBase = @import("../base.zig"); const ZigURL = @import("../../../query_string_map.zig").URL; const Api = @import("../../../api/schema.zig").Api; +const _global = @import("../../../global.zig"); +const std = @import("std"); +const Shimmer = @import("./shimmer.zig").Shimmer; +const strings = @import("strings"); +const default_allocator = _global.default_allocator; +const NewGlobalObject = JSC.NewGlobalObject; +const JSGlobalObject = JSC.JSGlobalObject; +const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false; +const ZigString = JSC.ZigString; +const string = _global.string; +const JSValue = JSC.JSValue; +const Output = _global.Output; +const Environment = _global.Environment; +const ScriptArguments = JSC.ScriptArguments; +const JSPromise = JSC.JSPromise; +const JSPromiseRejectionOperation = JSC.JSPromiseRejectionOperation; +const Exception = JSC.Exception; +const JSModuleLoader = JSC.JSModuleLoader; +const JSModuleRecord = JSC.JSModuleRecord; +const Microtask = JSC.Microtask; +const JSPrivateDataPtr = @import("../base.zig").JSPrivateDataPtr; + const Handler = struct { - pub export fn global_signal_handler_fn(sig: i32, info: *const std.os.siginfo_t, ctx_ptr: ?*const c_void) callconv(.C) void { + pub export fn global_signal_handler_fn(_: i32, _: *const std.os.siginfo_t, _: ?*const anyopaque) callconv(.C) void { var stdout = std.io.getStdOut(); var stderr = std.io.getStdErr(); var source = Output.Source.init(stdout, stderr); @@ -28,7 +50,7 @@ const Handler = struct { pub const ZigGlobalObject = extern struct { pub const shim = Shimmer("Zig", "GlobalObject", @This()); bytes: shim.Bytes, - pub const Type = *c_void; + pub const Type = *anyopaque; pub const name = "Zig::GlobalObject"; pub const include = "\"ZigGlobalObject.h\""; pub const namespace = shim.namespace; @@ -37,27 +59,27 @@ pub const ZigGlobalObject = extern struct { pub var sigaction: std.os.Sigaction = undefined; pub var sigaction_installed = false; - pub fn create(class_ref: [*]CAPI.JSClassRef, count: i32, console: *c_void) *JSGlobalObject { + pub fn create(class_ref: [*]CAPI.JSClassRef, count: i32, console: *anyopaque) *JSGlobalObject { if (!sigaction_installed) { sigaction_installed = true; sigaction = std.mem.zeroes(std.os.Sigaction); sigaction.handler = .{ .sigaction = Handler.global_signal_handler_fn }; - std.os.sigaction(std.os.SIGABRT, &sigaction, null); - if (comptime !isDebug) { - std.os.sigaction(std.os.SIGTRAP, &sigaction, null); + std.os.sigaction(std.os.SIG.ABRT, &sigaction, null); + if (comptime !Environment.isDebug) { + std.os.sigaction(std.os.SIG.TRAP, &sigaction, null); } } return shim.cppFn("create", .{ class_ref, count, console }); } - pub fn getModuleRegistryMap(global: *JSGlobalObject) *c_void { + pub fn getModuleRegistryMap(global: *JSGlobalObject) *anyopaque { return shim.cppFn("getModuleRegistryMap", .{global}); } - pub fn resetModuleRegistryMap(global: *JSGlobalObject, map: *c_void) bool { + pub fn resetModuleRegistryMap(global: *JSGlobalObject, map: *anyopaque) bool { return shim.cppFn("resetModuleRegistryMap", .{ global, map }); } @@ -165,9 +187,9 @@ pub const ZigErrorType = extern struct { pub const namespace = shim.namespace; code: ErrorCode, - ptr: ?*c_void, + ptr: ?*anyopaque, - pub fn isPrivateData(ptr: ?*c_void) callconv(.C) bool { + pub fn isPrivateData(ptr: ?*anyopaque) callconv(.C) bool { return JSBase.JSPrivateDataPtr.isValidPtr(ptr); } @@ -202,7 +224,7 @@ pub fn Errorable(comptime Type: type) type { } threadlocal var err_buf: [4096]u8 = undefined; - pub fn err(code: anyerror, ptr: *c_void) @This() { + pub fn err(code: anyerror, ptr: *anyopaque) @This() { return @This(){ .result = .{ .err = .{ @@ -226,14 +248,14 @@ pub const ResolvedSource = extern struct { source_url: ZigString, hash: u32, - allocator: ?*c_void, + allocator: ?*anyopaque, // 0 means disabled bytecodecache_fd: u64, }; -export fn ZigString__free(ptr: [*]const u8, len: usize, allocator_: ?*c_void) void { - var allocator: *std.mem.Allocator = @ptrCast(*std.mem.Allocator, @alignCast(@alignOf(*std.mem.Allocator), allocator_ orelse return)); +export fn ZigString__free(ptr: [*]const u8, len: usize, allocator_: ?*anyopaque) void { + var allocator: std.mem.Allocator = @ptrCast(*std.mem.Allocator, @alignCast(@alignOf(*std.mem.Allocator), allocator_ orelse return)).*; var str = ptr[0..len]; allocator.free(str); @@ -323,7 +345,7 @@ pub const ZigStackTrace = extern struct { frames_ptr: [*c]ZigStackFrame, frames_len: u8, - pub fn toAPI(this: *const ZigStackTrace, allocator: *std.mem.Allocator) !Api.StackTrace { + pub fn toAPI(this: *const ZigStackTrace, allocator: std.mem.Allocator) !Api.StackTrace { var stack_trace: Api.StackTrace = std.mem.zeroes(Api.StackTrace); { var source_lines_iter = this.sourceLineIterator(); @@ -414,7 +436,7 @@ pub const ZigStackFrame = extern struct { position: ZigStackFramePosition, code_type: ZigStackFrameCode, - pub fn toAPI(this: *const ZigStackFrame, allocator: *std.mem.Allocator) !Api.StackFrame { + pub fn toAPI(this: *const ZigStackFrame, allocator: std.mem.Allocator) !Api.StackFrame { var frame: Api.StackFrame = std.mem.zeroes(Api.StackFrame); if (this.function_name.len > 0) { frame.function_name = try allocator.dupe(u8, this.function_name.slice()); @@ -443,7 +465,7 @@ pub const ZigStackFrame = extern struct { enable_color: bool, origin: *const ZigURL, root_path: string = "", - pub fn format(this: SourceURLFormatter, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(this: SourceURLFormatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { try writer.writeAll(this.origin.displayProtocol()); try writer.writeAll("://"); try writer.writeAll(this.origin.displayHostname()); @@ -472,7 +494,7 @@ pub const ZigStackFrame = extern struct { code_type: ZigStackFrameCode, enable_color: bool, - pub fn format(this: NameFormatter, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(this: NameFormatter, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { const name = this.function_name.slice(); switch (this.code_type) { @@ -555,7 +577,7 @@ pub const ZigException = extern struct { message: ZigString, stack: ZigStackTrace, - exception: ?*c_void, + exception: ?*anyopaque, pub const shim = Shimmer("Zig", "Exception", @This()); pub const name = "ZigException"; @@ -662,7 +684,7 @@ pub const ErrorableJSValue = Errorable(JSValue); pub const ZigConsoleClient = struct { pub const shim = Shimmer("Zig", "ConsoleClient", @This()); - pub const Type = *c_void; + pub const Type = *anyopaque; pub const name = "Zig::ConsoleClient"; pub const include = "\"ZigConsoleClient.h\""; pub const namespace = shim.namespace; @@ -675,7 +697,7 @@ pub const ZigConsoleClient = struct { pub const List = std.MultiArrayList(Entry); }; counts: Entry.List, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, }; const BufferedWriter = std.io.BufferedWriter(4096, Output.WriterType); error_writer: BufferedWriter, @@ -691,9 +713,12 @@ pub const ZigConsoleClient = struct { /// TODO: support %s %d %f %o %O /// https://console.spec.whatwg.org/#formatter pub fn messageWithTypeAndLevel( - console_: ZigConsoleClient.Type, - message_type: u32, - message_level: u32, + //console_: ZigConsoleClient.Type, + _: ZigConsoleClient.Type, + //message_type: u32, + _: u32, + //message_level: u32, + _: u32, global: *JSGlobalObject, vals: [*]JSValue, len: usize, @@ -733,8 +758,6 @@ pub const ZigConsoleClient = struct { var values = vals[0..len]; defer buffered_writer.flush() catch {}; - var last_count: usize = 0; - var tail: u8 = 0; if (Output.enable_ansi_colors) { while (i < len) : (i += 1) { @@ -770,7 +793,7 @@ pub const ZigConsoleClient = struct { if (value.isCell()) { if (CAPI.JSObjectGetPrivate(value.asRef())) |private_data_ptr| { - const priv_data = JS.JSPrivateDataPtr.from(private_data_ptr); + const priv_data = JSPrivateDataPtr.from(private_data_ptr); switch (priv_data.tag()) { .BuildError => { const build_error = priv_data.as(JS.BuildError); @@ -831,14 +854,39 @@ pub const ZigConsoleClient = struct { } }; - pub fn count(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} - pub fn countReset(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} + pub fn count( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // chars + _: [*]const u8, + // len + _: usize, + ) callconv(.C) void {} + pub fn countReset( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // chars + _: [*]const u8, + // len + _: usize, + ) callconv(.C) void {} const PendingTimers = std.AutoHashMap(u64, ?std.time.Timer); threadlocal var pending_time_logs: PendingTimers = undefined; threadlocal var pending_time_logs_loaded = false; - pub fn time(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void { + pub fn time( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + chars: [*]const u8, + len: usize, + ) callconv(.C) void { const id = std.hash.Wyhash.hash(0, chars[0..len]); if (!pending_time_logs_loaded) { pending_time_logs = PendingTimers.init(default_allocator); @@ -851,7 +899,14 @@ pub const ZigConsoleClient = struct { result.value_ptr.* = std.time.Timer.start() catch unreachable; } } - pub fn timeEnd(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void { + pub fn timeEnd( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + chars: [*]const u8, + len: usize, + ) callconv(.C) void { if (!pending_time_logs_loaded) { return; } @@ -870,14 +925,80 @@ pub const ZigConsoleClient = struct { Output.flush(); } - pub fn timeLog(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize, args: *ScriptArguments) callconv(.C) void {} - pub fn profile(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} - pub fn profileEnd(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} - pub fn takeHeapSnapshot(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} - pub fn timeStamp(console: ZigConsoleClient.Type, global: *JSGlobalObject, args: *ScriptArguments) callconv(.C) void {} - pub fn record(console: ZigConsoleClient.Type, global: *JSGlobalObject, args: *ScriptArguments) callconv(.C) void {} - pub fn recordEnd(console: ZigConsoleClient.Type, global: *JSGlobalObject, args: *ScriptArguments) callconv(.C) void {} - pub fn screenshot(console: ZigConsoleClient.Type, global: *JSGlobalObject, args: *ScriptArguments) callconv(.C) void {} + pub fn timeLog( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // chars + _: [*]const u8, + // len + _: usize, + // args + _: *ScriptArguments, + ) callconv(.C) void {} + pub fn profile( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // chars + _: [*]const u8, + // len + _: usize, + ) callconv(.C) void {} + pub fn profileEnd( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // chars + _: [*]const u8, + // len + _: usize, + ) callconv(.C) void {} + pub fn takeHeapSnapshot( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // chars + _: [*]const u8, + // len + _: usize, + ) callconv(.C) void {} + pub fn timeStamp( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // args + _: *ScriptArguments, + ) callconv(.C) void {} + pub fn record( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // args + _: *ScriptArguments, + ) callconv(.C) void {} + pub fn recordEnd( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // args + _: *ScriptArguments, + ) callconv(.C) void {} + pub fn screenshot( + // console + _: ZigConsoleClient.Type, + // global + _: *JSGlobalObject, + // args + _: *ScriptArguments, + ) callconv(.C) void {} pub const Export = shim.exportFunctions(.{ .@"messageWithTypeAndLevel" = messageWithTypeAndLevel, @@ -958,7 +1079,7 @@ pub const ZigConsoleClient = struct { // pub const CommonJSModule = struct { // pub const shim = Shimmer("Zig", "CommonJSModule", @This()); -// pub const Type = *c_void; +// pub const Type = *anyopaque; // pub const name = "Zig::CommonJSModule"; // pub const include = "\"CommonJSModule.h\""; // pub const namespace = shim.namespace; diff --git a/src/javascript/jsc/bindings/header-gen.zig b/src/javascript/jsc/bindings/header-gen.zig index 7c175d6c4..7c73dd3aa 100644 --- a/src/javascript/jsc/bindings/header-gen.zig +++ b/src/javascript/jsc/bindings/header-gen.zig @@ -40,7 +40,7 @@ pub fn cTypeLabel(comptime Type: type) ?[]const u8 { i64 => "int64_t", f64 => "double", f32 => "float", - *c_void => "void*", + *anyopaque => "void*", [*]bool => "bool*", [*]usize => "size_t*", [*]isize => "int*", @@ -301,18 +301,19 @@ pub const C_Generator = struct { comptime meta: UnionMeta, comptime static_types: anytype, ) void { - self.write("typedef union "); + @compileError("Not implemented"); + // self.write("typedef union "); - self.write(name ++ " {\n"); + // self.write(name ++ " {\n"); - inline for (meta.fields) |field, i| { - self.write(" "); + // inline for (meta.fields) |field, i| { + // self.write(" "); - self.writeType(comptime FieldType); + // self.writeType(comptime FieldType); - self.write(" " ++ field.name ++ ";\n"); - } - self.write("} " ++ name ++ ";\n\n"); + // self.write(" " ++ field.name ++ ";\n"); + // } + // self.write("} " ++ name ++ ";\n\n"); } fn writeType( @@ -411,7 +412,7 @@ pub const C_Generator = struct { } }; -const builtin = std.builtin; +const builtin = @import("builtin"); const TypeInfo = builtin.TypeInfo; const Declaration = TypeInfo.Declaration; @@ -463,8 +464,8 @@ pub fn getCStruct(comptime T: type) ?NamedStruct { return null; } -pub fn HeaderGen(comptime import: type, comptime fname: []const u8) type { - const all_decls = std.meta.declarations(import); +pub fn HeaderGen(comptime first_import: type, comptime second_import: type, comptime fname: []const u8) type { + const all_decls = comptime std.meta.declarations(first_import) ++ std.meta.declarations(second_import); return struct { source_file: []const u8 = fname, @@ -552,7 +553,7 @@ pub fn HeaderGen(comptime import: type, comptime fname: []const u8) type { comptime func, comptime func, comptime &.{}, - comptime ENABLE_REWRITE_RETURN and @typeInfo(fn_meta.return_type) == .Struct, + false, // comptime ENABLE_REWRITE_RETURN and @typeInfo(fn_meta.return_type) == .Struct, ); }, else => {}, diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index 026bc6fd7..8ea8496b8 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1640302889 +//-- AUTOGENERATED FILE -- 1640830883 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers-replacements.zig b/src/javascript/jsc/bindings/headers-replacements.zig index a7e8350cf..d2f9e0564 100644 --- a/src/javascript/jsc/bindings/headers-replacements.zig +++ b/src/javascript/jsc/bindings/headers-replacements.zig @@ -1,5 +1,5 @@ -const bindings = @import("./bindings.zig"); -pub usingnamespace @import("../JavascriptCore.zig"); +// GENERATED FILE - do not modify! +const bindings = @import("../../../jsc.zig"); pub const struct_JSC__StringPrototype = bindings.StringPrototype; pub const struct_JSC__SetIteratorPrototype = bindings.SetIteratorPrototype; @@ -51,3 +51,4 @@ pub const ZigException = bindings.ZigException; pub const ResolvedSource = bindings.ResolvedSource; pub const ZigStackTrace = bindings.ZigStackTrace; pub const ReturnableException = bindings.ReturnableException; +const JSClassRef = bindings.C.JSClassRef; diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index 4820ef035..108738586 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,5 +1,5 @@ // clang-format: off -//-- AUTOGENERATED FILE -- 1640302889 +//-- AUTOGENERATED FILE -- 1640830883 #pragma once #include <stddef.h> @@ -108,7 +108,6 @@ typedef void* JSClassRef; typedef ZigException ZigException; typedef struct JSC__SetIteratorPrototype JSC__SetIteratorPrototype; // JSC::SetIteratorPrototype typedef bJSC__SourceCode JSC__SourceCode; // JSC::SourceCode - typedef struct Zig__JSMicrotaskCallback Zig__JSMicrotaskCallback; // Zig::JSMicrotaskCallback typedef bJSC__JSCell JSC__JSCell; // JSC::JSCell typedef struct JSC__BigIntPrototype JSC__BigIntPrototype; // JSC::BigIntPrototype typedef struct JSC__GeneratorFunctionPrototype JSC__GeneratorFunctionPrototype; // JSC::GeneratorFunctionPrototype @@ -176,9 +175,6 @@ typedef void* JSClassRef; class StringView; class ExternalStringImpl; } - namespace Zig { - class JSMicrotaskCallback; - } namespace Inspector { class ScriptArguments; } @@ -230,7 +226,6 @@ typedef void* JSClassRef; 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 @@ -593,7 +588,7 @@ ZIG_DECL void Zig__GlobalObject__fetch(ErrorableResolvedSource* arg0, JSC__JSGlo 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 void Zig__GlobalObject__queueMicrotaskToEventLoop(JSC__JSGlobalObject* arg0, Zig__JSMicrotaskCallback* arg1); +ZIG_DECL void Zig__GlobalObject__queueMicrotaskToEventLoop(JSC__JSGlobalObject* arg0, Microtask* arg1); 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); diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig index 89b7044af..1f2a83fba 100644 --- a/src/javascript/jsc/bindings/headers.zig +++ b/src/javascript/jsc/bindings/headers.zig @@ -1,7 +1,70 @@ -usingnamespace @import("./headers-replacements.zig"); -pub usingnamespace @import("std").zig.c_builtins; +// GENERATED FILE - do not modify! +const bindings = @import("../../../jsc.zig"); + +pub const struct_JSC__StringPrototype = bindings.StringPrototype; +pub const struct_JSC__SetIteratorPrototype = bindings.SetIteratorPrototype; +pub const struct_JSC__RegExpPrototype = bindings.RegExpPrototype; +pub const struct_JSC__ObjectPrototype = bindings.ObjectPrototype; +pub const struct_JSC__MapIteratorPrototype = bindings.MapIteratorPrototype; +pub const struct_JSC__JSPromisePrototype = bindings.JSPromisePrototype; +pub const struct_JSC__IteratorPrototype = bindings.IteratorPrototype; +pub const struct_JSC__GeneratorPrototype = bindings.GeneratorPrototype; +pub const struct_JSC__GeneratorFunctionPrototype = bindings.GeneratorFunctionPrototype; +pub const struct_JSC__FunctionPrototype = bindings.FunctionPrototype; +pub const struct_JSC__BigIntPrototype = bindings.BigIntPrototype; +pub const struct_JSC__AsyncIteratorPrototype = bindings.AsyncIteratorPrototype; +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 struct_JSC__ArrayIteratorPrototype = bindings.ArrayIteratorPrototype; +pub const bWTF__URL = bindings.URL; +pub const bWTF__StringView = bindings.StringView; +pub const bWTF__StringImpl = bindings.StringImpl; +pub const bWTF__String = bindings.String; +pub const bWTF__ExternalStringImpl = bindings.ExternalStringImpl; +pub const bJSC__VM = bindings.VM; +pub const bJSC__ThrowScope = bindings.ThrowScope; +pub const bJSC__SourceOrigin = bindings.SourceOrigin; +pub const bJSC__SourceCode = bindings.SourceCode; +pub const bJSC__PropertyName = bindings.PropertyName; +pub const bJSC__JSString = bindings.JSString; +pub const bJSC__JSPromise = bindings.JSPromise; +pub const bJSC__JSObject = bindings.JSObject; +pub const bJSC__JSModuleRecord = bindings.JSModuleRecord; +pub const bJSC__JSModuleLoader = bindings.JSModuleLoader; +pub const bJSC__JSLock = bindings.JSLock; +pub const bJSC__JSInternalPromise = bindings.JSInternalPromise; +pub const bJSC__JSGlobalObject = bindings.JSGlobalObject; +pub const bJSC__JSFunction = bindings.JSFunction; +pub const bJSC__JSCell = bindings.JSCell; +pub const bJSC__Identifier = bindings.Identifier; +pub const bJSC__Exception = bindings.Exception; +pub const bJSC__CatchScope = bindings.CatchScope; +pub const bJSC__CallFrame = bindings.CallFrame; +pub const bInspector__ScriptArguments = bindings.ScriptArguments; +pub const JSC__JSValue = bindings.JSValue; + +// Inlined types +pub const ZigString = bindings.ZigString; +pub const ZigException = bindings.ZigException; +pub const ResolvedSource = bindings.ResolvedSource; +pub const ZigStackTrace = bindings.ZigStackTrace; +pub const ReturnableException = bindings.ReturnableException; +const JSClassRef = bindings.C.JSClassRef; +// GENERATED CODE - DO NOT MODIFY BY HAND + pub const ptrdiff_t = c_long; -pub const wchar_t = c_uint; +pub const wchar_t = c_int; +pub const __uint16_t = c_ushort; +pub const __int32_t = c_int; +pub const __uint32_t = c_uint; +pub const __int64_t = c_longlong; +pub const __uint64_t = c_ulonglong; +pub const __mbstate_t = extern union { + __mbstate8: [128]u8, + _mbstateL: c_longlong, +}; pub const JSC__RegExpPrototype = struct_JSC__RegExpPrototype; @@ -39,8 +102,6 @@ pub const JSC__JSPromise = bJSC__JSPromise; pub const JSC__SetIteratorPrototype = struct_JSC__SetIteratorPrototype; pub const JSC__SourceCode = bJSC__SourceCode; - -pub const Zig__JSMicrotaskCallback = struct_Zig__JSMicrotaskCallback; pub const JSC__JSCell = bJSC__JSCell; pub const JSC__BigIntPrototype = struct_JSC__BigIntPrototype; @@ -63,7 +124,7 @@ pub const JSC__ObjectPrototype = struct_JSC__ObjectPrototype; pub const JSC__CallFrame = bJSC__CallFrame; pub const JSC__MapIteratorPrototype = struct_JSC__MapIteratorPrototype; -pub extern fn JSC__JSObject__create(arg0: [*c]JSC__JSGlobalObject, arg1: usize, arg2: ?*c_void, ArgFn3: ?fn (?*c_void, [*c]JSC__JSObject, [*c]JSC__JSGlobalObject) callconv(.C) void) JSC__JSValue; +pub extern fn JSC__JSObject__create(arg0: [*c]JSC__JSGlobalObject, arg1: usize, arg2: ?*anyopaque, ArgFn3: ?fn (?*anyopaque, [*c]JSC__JSObject, [*c]JSC__JSGlobalObject) callconv(.C) void) JSC__JSValue; pub extern fn JSC__JSObject__getArrayLength(arg0: [*c]JSC__JSObject) usize; pub extern fn JSC__JSObject__getDirect(arg0: [*c]JSC__JSObject, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]const ZigString) JSC__JSValue; pub extern fn JSC__JSObject__getIndex(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: u32) JSC__JSValue; @@ -129,7 +190,7 @@ pub extern fn JSC__JSFunction__constructWithArguments(JSValue0: JSC__JSValue, ar pub extern fn JSC__JSFunction__constructWithArgumentsAndNewTarget(JSValue0: JSC__JSValue, JSValue1: JSC__JSValue, arg2: [*c]JSC__JSGlobalObject, arg3: [*c]JSC__JSValue, arg4: usize, arg5: *?*JSC__Exception, arg6: [*c]const u8) JSC__JSValue; pub extern fn JSC__JSFunction__constructWithNewTarget(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, JSValue2: JSC__JSValue, arg3: *?*JSC__Exception, arg4: [*c]const u8) JSC__JSValue; pub extern fn JSC__JSFunction__constructWithoutAnyArgumentsOrNewTarget(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: *?*JSC__Exception, arg3: [*c]const u8) JSC__JSValue; -pub extern fn JSC__JSFunction__createFromNative(arg0: [*c]JSC__JSGlobalObject, arg1: u16, arg2: [*c]const WTF__String, arg3: ?*c_void, ArgFn4: ?fn (?*c_void, [*c]JSC__JSGlobalObject, [*c]JSC__CallFrame) callconv(.C) JSC__JSValue) [*c]JSC__JSFunction; +pub extern fn JSC__JSFunction__createFromNative(arg0: [*c]JSC__JSGlobalObject, arg1: u16, arg2: [*c]const WTF__String, arg3: ?*anyopaque, ArgFn4: ?fn (?*anyopaque, [*c]JSC__JSGlobalObject, [*c]JSC__CallFrame) callconv(.C) JSC__JSValue) [*c]JSC__JSFunction; pub extern fn JSC__JSFunction__displayName(arg0: [*c]JSC__JSFunction, arg1: [*c]JSC__VM) bWTF__String; pub extern fn JSC__JSFunction__getName(arg0: [*c]JSC__JSFunction, arg1: [*c]JSC__VM) bWTF__String; pub extern fn JSC__JSGlobalObject__arrayIteratorPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__ArrayIteratorPrototype; @@ -140,7 +201,7 @@ pub extern fn JSC__JSGlobalObject__asyncGeneratorPrototype(arg0: [*c]JSC__JSGlob pub extern fn JSC__JSGlobalObject__asyncIteratorPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__AsyncIteratorPrototype; pub extern fn JSC__JSGlobalObject__bigIntPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__BigIntPrototype; pub extern fn JSC__JSGlobalObject__booleanPrototype(arg0: [*c]JSC__JSGlobalObject) [*c]JSC__JSObject; -pub extern fn JSC__JSGlobalObject__createAggregateError(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]*c_void, arg2: u16, arg3: [*c]const ZigString) JSC__JSValue; +pub extern fn JSC__JSGlobalObject__createAggregateError(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]*anyopaque, arg2: u16, arg3: [*c]const ZigString) JSC__JSValue; pub extern fn JSC__JSGlobalObject__datePrototype(arg0: [*c]JSC__JSGlobalObject) [*c]JSC__JSObject; pub extern fn JSC__JSGlobalObject__errorPrototype(arg0: [*c]JSC__JSGlobalObject) [*c]JSC__JSObject; pub extern fn JSC__JSGlobalObject__functionPrototype(arg0: [*c]JSC__JSGlobalObject) ?*JSC__FunctionPrototype; @@ -319,7 +380,7 @@ pub extern fn WTF__StringImpl__isStatic(arg0: [*c]const WTF__StringImpl) bool; pub extern fn WTF__StringImpl__length(arg0: [*c]const WTF__StringImpl) usize; pub extern fn WTF__ExternalStringImpl__characters16(arg0: [*c]const WTF__ExternalStringImpl) [*c]const u16; pub extern fn WTF__ExternalStringImpl__characters8(arg0: [*c]const WTF__ExternalStringImpl) [*c]const u8; -pub extern fn WTF__ExternalStringImpl__create(arg0: [*c]const u8, arg1: usize, ArgFn2: ?fn (?*c_void, [*c]u8, usize) callconv(.C) void) bWTF__ExternalStringImpl; +pub extern fn WTF__ExternalStringImpl__create(arg0: [*c]const u8, arg1: usize, ArgFn2: ?fn (?*anyopaque, [*c]u8, usize) callconv(.C) void) bWTF__ExternalStringImpl; pub extern fn WTF__ExternalStringImpl__is16Bit(arg0: [*c]const WTF__ExternalStringImpl) bool; pub extern fn WTF__ExternalStringImpl__is8Bit(arg0: [*c]const WTF__ExternalStringImpl) bool; pub extern fn WTF__ExternalStringImpl__isEmpty(arg0: [*c]const WTF__ExternalStringImpl) bool; @@ -331,7 +392,7 @@ pub extern fn WTF__StringView__is16Bit(arg0: [*c]const WTF__StringView) bool; pub extern fn WTF__StringView__is8Bit(arg0: [*c]const WTF__StringView) bool; pub extern fn WTF__StringView__isEmpty(arg0: [*c]const WTF__StringView) bool; pub extern fn WTF__StringView__length(arg0: [*c]const WTF__StringView) usize; -pub extern fn Zig__GlobalObject__create(arg0: [*c]JSClassRef, arg1: i32, arg2: ?*c_void) [*c]JSC__JSGlobalObject; -pub extern fn Zig__GlobalObject__getModuleRegistryMap(arg0: [*c]JSC__JSGlobalObject) ?*c_void; -pub extern fn Zig__GlobalObject__resetModuleRegistryMap(arg0: [*c]JSC__JSGlobalObject, arg1: ?*c_void) bool; +pub extern fn Zig__GlobalObject__create(arg0: [*c]JSClassRef, arg1: i32, arg2: ?*anyopaque) [*c]JSC__JSGlobalObject; +pub extern fn Zig__GlobalObject__getModuleRegistryMap(arg0: [*c]JSC__JSGlobalObject) ?*anyopaque; +pub extern fn Zig__GlobalObject__resetModuleRegistryMap(arg0: [*c]JSC__JSGlobalObject, arg1: ?*anyopaque) bool; pub extern fn ZigException__fromException(arg0: [*c]JSC__Exception) ZigException; diff --git a/src/javascript/jsc/bindings/shared.zig b/src/javascript/jsc/bindings/shared.zig index cb0a8a73f..e69de29bb 100644 --- a/src/javascript/jsc/bindings/shared.zig +++ b/src/javascript/jsc/bindings/shared.zig @@ -1,9 +0,0 @@ -pub const std = @import("std"); -pub const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false; -pub const StaticExport = @import("./static_export.zig"); -pub const c_char = StaticExport.c_char; -pub usingnamespace @import("../../../global.zig"); - -pub fn zigCast(comptime Destination: type, value: anytype) *Destination { - return @ptrCast(*Destination, @alignCast(@alignOf(*Destination), value)); -} diff --git a/src/javascript/jsc/bindings/shimmer.zig b/src/javascript/jsc/bindings/shimmer.zig index 93f36a0a6..b4d5fe16b 100644 --- a/src/javascript/jsc/bindings/shimmer.zig +++ b/src/javascript/jsc/bindings/shimmer.zig @@ -9,8 +9,6 @@ fn isNullableType(comptime Type: type) bool { } pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comptime Parent: type) type { - const extern_count: usize = if (@hasDecl(Parent, "Extern")) Parent.Extern.len else 0; - return struct { pub const namespace = _namespace; pub const name = _name; @@ -18,13 +16,13 @@ pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comp // fn toCppType(comptime FromType: type) type { // var NewReturnType = FromType; - // if (NewReturnType == c_void) { + // if (NewReturnType == anyopaque) { // return FromType; // } // var ReturnTypeInfo: std.builtin.TypeInfo = @typeInfo(FromType); - // if (ReturnTypeInfo == .Pointer and NewReturnType != *c_void) { + // if (ReturnTypeInfo == .Pointer and NewReturnType != *anyopaque) { // NewReturnType = ReturnTypeInfo.Pointer.child; // ReturnTypeInfo = @typeInfo(NewReturnType); // } @@ -71,19 +69,6 @@ pub fn Shimmer(comptime _namespace: []const u8, comptime _name: []const u8, comp pub const is_return = true; }; - pub inline fn getConvertibleType(comptime ZigType: type) type { - if (@typeInfo(ZigType) == .Struct) { - const Struct: std.builtin.TypeInfo.Struct = ChildType.Struct; - for (Struct.fields) |field| { - if (std.mem.eql(u8, field.name, "ref")) { - return field.field_type; - } - } - } - - return ZigType; - } - fn pointerChild(comptime Type: type) type { if (@typeInfo(Type) == .Pointer) { return @typeInfo(Type).Pointer.child_type; diff --git a/src/javascript/jsc/config.zig b/src/javascript/jsc/config.zig index 1356bc8eb..c206b2358 100644 --- a/src/javascript/jsc/config.zig +++ b/src/javascript/jsc/config.zig @@ -1,4 +1,13 @@ -usingnamespace @import("../../global.zig"); +const _global = @import("../../global.zig"); +const string = _global.string; +const Output = _global.Output; +const Global = _global.Global; +const Environment = _global.Environment; +const strings = _global.strings; +const MutableString = _global.MutableString; +const stringZ = _global.stringZ; +const default_allocator = _global.default_allocator; +const C = _global.C; const std = @import("std"); const Fs = @import("../../fs.zig"); @@ -22,7 +31,7 @@ pub const DefaultBunDefines = struct { }; }; -pub fn configureTransformOptionsForBunVM(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions { +pub fn configureTransformOptionsForBunVM(allocator: std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions { var args = _args; // args.serve = false; @@ -32,7 +41,7 @@ pub fn configureTransformOptionsForBunVM(allocator: *std.mem.Allocator, _args: A return try configureTransformOptionsForBun(allocator, args); } -pub fn configureTransformOptionsForBun(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions { +pub fn configureTransformOptionsForBun(_: std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions { var args = _args; args.platform = Api.Platform.bun; return args; diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index d826db486..520ac2d7a 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -1,4 +1,22 @@ const std = @import("std"); +const is_bindgen: bool = std.meta.globalOption("bindgen", bool) orelse false; +const StaticExport = @import("./bindings/static_export.zig"); +const c_char = StaticExport.c_char; +const _global = @import("../../global.zig"); +const string = _global.string; +const Output = _global.Output; +const Global = _global.Global; +const Environment = _global.Environment; +const strings = _global.strings; +const MutableString = _global.MutableString; +const stringZ = _global.stringZ; +const default_allocator = _global.default_allocator; +const StoredFileDescriptorType = _global.StoredFileDescriptorType; +const C = _global.C; + +pub fn zigCast(comptime Destination: type, value: anytype) *Destination { + return @ptrCast(*Destination, @alignCast(@alignOf(*Destination), value)); +} const Fs = @import("../../fs.zig"); const Resolver = @import("../../resolver/resolver.zig"); @@ -18,11 +36,7 @@ const http = @import("../../http.zig"); const NodeFallbackModules = @import("../../node_fallbacks.zig"); const ImportKind = ast.ImportKind; const Analytics = @import("../../analytics/analytics_thread.zig"); -usingnamespace @import("./base.zig"); -usingnamespace @import("./webcore/response.zig"); -usingnamespace @import("./config.zig"); -usingnamespace @import("./bindings/exports.zig"); -usingnamespace @import("./bindings/bindings.zig"); +const ZigString = @import("javascript_core").ZigString; const Runtime = @import("../../runtime.zig"); const Router = @import("./api/router.zig"); const ImportRecord = ast.ImportRecord; @@ -30,6 +44,37 @@ const DotEnv = @import("../../env_loader.zig"); const ParseResult = @import("../../bundler.zig").ParseResult; const PackageJSON = @import("../../resolver/package_json.zig").PackageJSON; const MacroRemap = @import("../../resolver/package_json.zig").MacroMap; +const WebCore = @import("javascript_core").WebCore; +const Request = WebCore.Request; +const Response = WebCore.Response; +const Headers = WebCore.Headers; +const Fetch = WebCore.Fetch; +const FetchEvent = WebCore.FetchEvent; +const js = @import("javascript_core").C; +const JSError = @import("./base.zig").JSError; +const d = @import("./base.zig").d; +const MarkedArrayBuffer = @import("./base.zig").MarkedArrayBuffer; +const getAllocator = @import("./base.zig").getAllocator; +const JSValue = @import("javascript_core").JSValue; +const NewClass = @import("./base.zig").NewClass; +const Microtask = @import("javascript_core").Microtask; +const JSGlobalObject = @import("javascript_core").JSGlobalObject; +const ExceptionValueRef = @import("javascript_core").ExceptionValueRef; +const JSPrivateDataPtr = @import("javascript_core").JSPrivateDataPtr; +const ZigConsoleClient = @import("javascript_core").ZigConsoleClient; +const ZigException = @import("javascript_core").ZigException; +const ZigStackTrace = @import("javascript_core").ZigStackTrace; +const ErrorableResolvedSource = @import("javascript_core").ErrorableResolvedSource; +const ResolvedSource = @import("javascript_core").ResolvedSource; +const JSPromise = @import("javascript_core").JSPromise; +const JSInternalPromise = @import("javascript_core").JSInternalPromise; +const JSModuleLoader = @import("javascript_core").JSModuleLoader; +const JSPromiseRejectionOperation = @import("javascript_core").JSPromiseRejectionOperation; +const Exception = @import("javascript_core").Exception; +const ErrorableZigString = @import("javascript_core").ErrorableZigString; +const ZigGlobalObject = @import("javascript_core").ZigGlobalObject; +const VM = @import("javascript_core").VM; +const Config = @import("./config.zig"); pub const GlobalClasses = [_]type{ Request.Class, @@ -60,7 +105,7 @@ pub const Bun = struct { pub fn onImportCSS( resolve_result: *const Resolver.Result, import_record: *ImportRecord, - source_dir: string, + _: string, ) void { if (!css_imports_buf_loaded) { css_imports_buf = std.ArrayList(u8).initCapacity( @@ -99,10 +144,13 @@ pub const Bun = struct { } pub fn registerMacro( - this: void, + // this + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + // function + _: js.JSObjectRef, + // thisObject + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSValueRef { @@ -134,60 +182,60 @@ pub const Bun = struct { } pub fn getCWD( - this: void, - ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: void, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(VirtualMachine.vm.bundler.fs.top_level_dir).toValue(VirtualMachine.vm.global).asRef(); } pub fn getOrigin( - this: void, - ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: void, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(VirtualMachine.vm.bundler.options.origin.origin).toValue(VirtualMachine.vm.global).asRef(); } pub fn enableANSIColors( - this: void, + _: void, ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSValueMakeBoolean(ctx, Output.enable_ansi_colors); } pub fn getMain( - this: void, - ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: void, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(VirtualMachine.vm.main).toValue(VirtualMachine.vm.global).asRef(); } pub fn getAssetPrefix( - this: void, - ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: void, + _: js.JSContextRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(VirtualMachine.vm.bundler.options.routes.asset_prefix_path).toValue(VirtualMachine.vm.global).asRef(); } pub fn getArgv( - this: void, + _: void, ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { if (comptime Environment.isWindows) { @compileError("argv not supported on windows"); @@ -205,11 +253,11 @@ pub const Bun = struct { } pub fn getRoutesDir( - this: void, + _: void, ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { if (!VirtualMachine.vm.bundler.options.routes.routes_enabled or VirtualMachine.vm.bundler.options.routes.dir.len == 0) { return js.JSValueMakeUndefined(ctx); @@ -287,12 +335,12 @@ pub const Bun = struct { } pub fn getImportedStyles( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { defer flushCSSImports(); const styles = getCSSImports(); @@ -386,12 +434,12 @@ pub const Bun = struct { } pub fn getRouteFiles( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { if (VirtualMachine.vm.bundler.router == null) return js.JSValueMakeNull(ctx); @@ -407,12 +455,12 @@ pub const Bun = struct { } pub fn getRouteNames( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { if (VirtualMachine.vm.bundler.router == null) return js.JSValueMakeNull(ctx); @@ -428,10 +476,10 @@ pub const Bun = struct { } pub fn readFileAsBytes( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSValueRef { @@ -445,10 +493,10 @@ pub const Bun = struct { } pub fn readFileAsString( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSValueRef { @@ -478,12 +526,12 @@ pub const Bun = struct { } pub fn sleepSync( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSValueRef { if (js.JSValueIsNumber(ctx, arguments[0])) { const ms = JSValue.fromRef(arguments[0]).asNumber(); @@ -496,12 +544,12 @@ pub const Bun = struct { var public_path_temp_str: [std.fs.MAX_PATH_BYTES]u8 = undefined; pub fn getPublicPathJS( - this: void, - ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: void, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSValueRef { var zig_str: ZigString = ZigString.Empty; JSValue.toZigString(JSValue.fromRef(arguments[0]), &zig_str, VirtualMachine.vm.global); @@ -647,11 +695,11 @@ pub const Bun = struct { ); pub fn getter( - this: void, + _: void, ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSObjectMake(ctx, EnvironmentVariables.Class.get().*, null); } @@ -663,9 +711,9 @@ pub const Bun = struct { pub fn getProperty( ctx: js.JSContextRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, propertyName: js.JSStringRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) callconv(.C) js.JSValueRef { const len = js.JSStringGetLength(propertyName); var ptr = js.JSStringGetCharacters8Ptr(propertyName); @@ -685,8 +733,8 @@ pub const Bun = struct { } pub fn hasProperty( - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, + _: js.JSContextRef, + _: js.JSObjectRef, propertyName: js.JSStringRef, ) callconv(.C) bool { const len = js.JSStringGetLength(propertyName); @@ -696,8 +744,8 @@ pub const Bun = struct { } pub fn getPropertyNames( - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, + _: js.JSContextRef, + _: js.JSObjectRef, props: js.JSPropertyNameAccumulatorRef, ) callconv(.C) void { var iter = VirtualMachine.vm.bundler.env.map.iter(); @@ -726,12 +774,12 @@ pub const Performance = struct { ); pub fn now( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSValueMakeNumber( ctx, @@ -760,7 +808,7 @@ pub const Task = TaggedPointerUnion(.{ // Its unavailable on Linux pub const VirtualMachine = struct { global: *JSGlobalObject, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, node_modules: ?*NodeModuleBundle = null, bundler: Bundler, watcher: ?*http.Watcher = null, @@ -858,7 +906,7 @@ pub const VirtualMachine = struct { } pub fn init( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, _args: Api.TransformOptions, existing_bundle: ?*NodeModuleBundle, _log: ?*logger.Log, @@ -878,7 +926,7 @@ pub const VirtualMachine = struct { const bundler = try Bundler.init( allocator, log, - try configureTransformOptionsForBunVM(allocator, _args), + try Config.configureTransformOptionsForBunVM(allocator, _args), existing_bundle, env_loader, ); @@ -957,7 +1005,7 @@ pub const VirtualMachine = struct { inline fn _fetch( global: *JSGlobalObject, _specifier: string, - source: string, + _: string, log: *logger.Log, ) !ResolvedSource { std.debug.assert(VirtualMachine.vm_loaded); @@ -1083,7 +1131,7 @@ pub const VirtualMachine = struct { vm.bundler.resetStore(); const hash = http.Watcher.getHash(path.text); - var allocator = if (vm.has_loaded) &vm.arena.allocator else vm.allocator; + var allocator = if (vm.has_loaded) vm.arena.allocator() else vm.allocator; var fd: ?StoredFileDescriptorType = null; var package_json: ?*PackageJSON = null; @@ -1178,7 +1226,7 @@ pub const VirtualMachine = struct { } return ResolvedSource{ - .allocator = if (vm.has_loaded) vm.allocator else null, + .allocator = if (vm.has_loaded) &vm.allocator else null, .source_code = ZigString.init(vm.allocator.dupe(u8, source_code_printer.ctx.written) catch unreachable), .specifier = ZigString.init(specifier), .source_url = ZigString.init(path.text), @@ -1188,7 +1236,7 @@ pub const VirtualMachine = struct { }, else => { return ResolvedSource{ - .allocator = vm.allocator, + .allocator = &vm.allocator, .source_code = ZigString.init(try strings.quotedAlloc(VirtualMachine.vm.allocator, path.pretty)), .specifier = ZigString.init(path.text), .source_url = ZigString.init(path.text), @@ -1271,7 +1319,7 @@ pub const VirtualMachine = struct { const package = &node_modules_bundle.bundle.packages[pkg_id]; - if (isDebug) { + if (Environment.isDebug) { std.debug.assert(strings.eql(node_modules_bundle.str(package.name), package_json.name)); } @@ -1324,7 +1372,7 @@ pub const VirtualMachine = struct { }; { - res.* = ErrorableZigString.err(err, @ptrCast(*c_void, ResolveError.create(vm.allocator, msg, source.slice()))); + res.* = ErrorableZigString.err(err, @ptrCast(*anyopaque, ResolveError.create(vm.allocator, msg, source.slice()))); } return; @@ -1356,13 +1404,13 @@ pub const VirtualMachine = struct { } // This double prints - pub fn promiseRejectionTracker(global: *JSGlobalObject, promise: *JSPromise, rejection: JSPromiseRejectionOperation) callconv(.C) JSValue { + pub fn promiseRejectionTracker(_: *JSGlobalObject, _: *JSPromise, _: JSPromiseRejectionOperation) callconv(.C) JSValue { // VirtualMachine.vm.defaultErrorHandler(promise.result(global.vm()), null); return JSValue.jsUndefined(); } const main_file_name: string = "bun:main"; - threadlocal var errors_stack: [256]*c_void = undefined; + threadlocal var errors_stack: [256]*anyopaque = undefined; pub fn fetch(ret: *ErrorableResolvedSource, global: *JSGlobalObject, specifier: ZigString, source: ZigString) callconv(.C) void { var log = logger.Log.init(vm.bundler.allocator); const spec = specifier.slice(); @@ -1418,7 +1466,7 @@ pub const VirtualMachine = struct { .data = logger.rangeData(null, logger.Range.None, std.fmt.allocPrint(vm.allocator, "{s} while building {s}", .{ @errorName(err), specifier.slice() }) catch unreachable), }; { - ret.* = ErrorableResolvedSource.err(err, @ptrCast(*c_void, BuildError.create(vm.bundler.allocator, msg))); + ret.* = ErrorableResolvedSource.err(err, @ptrCast(*anyopaque, BuildError.create(vm.bundler.allocator, msg))); } return; }, @@ -1463,7 +1511,7 @@ pub const VirtualMachine = struct { } // TODO: - pub fn deinit(this: *VirtualMachine) void {} + pub fn deinit(_: *VirtualMachine) void {} pub const ExceptionList = std.ArrayList(Api.JsException); @@ -1594,7 +1642,7 @@ pub const VirtualMachine = struct { pub fn iteratorWithOutColor(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, nextValue: JSValue) callconv(.C) void { iterator(_vm, globalObject, nextValue, false); } - inline fn iterator(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, nextValue: JSValue, comptime color: bool) void { + inline fn iterator(_: [*c]VM, _: [*c]JSGlobalObject, nextValue: JSValue, comptime color: bool) void { VirtualMachine.vm.printErrorlikeObject(nextValue, null, current_exception_list, color); } }; @@ -1618,7 +1666,7 @@ pub const VirtualMachine = struct { was_internal = this.printErrorFromMaybePrivateData(value.asRef(), exception_list, allow_ansi_color); } - pub fn printErrorFromMaybePrivateData(this: *VirtualMachine, value: ?*c_void, exception_list: ?*ExceptionList, comptime allow_ansi_color: bool) bool { + pub fn printErrorFromMaybePrivateData(this: *VirtualMachine, value: ?*anyopaque, exception_list: ?*ExceptionList, comptime allow_ansi_color: bool) bool { const private_data_ptr = JSPrivateDataPtr.from(value); switch (private_data_ptr.tag()) { @@ -1658,7 +1706,7 @@ pub const VirtualMachine = struct { }, else => { this.printErrorInstance(@intToEnum(JSValue, @intCast(i64, (@ptrToInt(value)))), exception_list, allow_ansi_color) catch |err| { - if (comptime isDebug) { + if (comptime Environment.isDebug) { // yo dawg Output.printErrorln("Error while printing Error-like object: {s}", .{@errorName(err)}); Output.flush(); @@ -1944,7 +1992,7 @@ pub const EventListenerMixin = struct { var listeners = vm.event_listeners.get(EventType.fetch) orelse (return onError(ctx, error.NoListeners, JSValue.jsUndefined(), request_context) catch {}); if (listeners.items.len == 0) return onError(ctx, error.NoListeners, JSValue.jsUndefined(), request_context) catch {}; const FetchEventRejectionHandler = struct { - pub fn onRejection(_ctx: *c_void, err: anyerror, fetch_event: *FetchEvent, value: JSValue) void { + pub fn onRejection(_ctx: *anyopaque, err: anyerror, fetch_event: *FetchEvent, value: JSValue) void { onError( @intToPtr(*CtxType, @ptrToInt(_ctx)), err, @@ -1960,14 +2008,12 @@ pub const EventListenerMixin = struct { fetch_event.* = FetchEvent{ .request_context = request_context, .request = Request{ .request_context = request_context }, - .onPromiseRejectionCtx = @as(*c_void, ctx), + .onPromiseRejectionCtx = @as(*anyopaque, ctx), .onPromiseRejectionHandler = FetchEventRejectionHandler.onRejection, }; var fetch_args: [1]js.JSObjectRef = undefined; for (listeners.items) |listener_ref| { - var listener = @intToEnum(JSValue, @intCast(i64, @ptrToInt(listener_ref))); - fetch_args[0] = FetchEvent.Class.make(vm.global.ref(), fetch_event); var result = js.JSObjectCallAsFunctionReturnValue(vm.global.ref(), listener_ref, null, 1, &fetch_args); @@ -2002,11 +2048,11 @@ pub const EventListenerMixin = struct { const Handler = struct { pub fn addListener( ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, argumentCount: usize, _arguments: [*c]const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) callconv(.C) js.JSValueRef { const arguments = _arguments[0..argumentCount]; if (arguments.len == 0 or arguments.len == 1 or !js.JSValueIsString(ctx, arguments[0]) or !js.JSValueIsObject(ctx, arguments[arguments.len - 1]) or !js.JSObjectIsFunction(ctx, arguments[arguments.len - 1])) { @@ -2054,11 +2100,11 @@ pub const EventListenerMixin = struct { pub const ResolveError = struct { msg: logger.Msg, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, referrer: ?Fs.Path = null, logged: bool = false, - pub fn fmt(allocator: *std.mem.Allocator, specifier: string, referrer: string, err: anyerror) !string { + pub fn fmt(allocator: std.mem.Allocator, specifier: string, referrer: string, err: anyerror) !string { switch (err) { error.ModuleNotFound => { if (Resolver.isPackagePath(specifier)) { @@ -2119,7 +2165,7 @@ pub const ResolveError = struct { ); pub fn create( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, msg: logger.Msg, referrer: string, ) js.JSObjectRef { @@ -2137,8 +2183,8 @@ pub const ResolveError = struct { pub fn getPosition( this: *ResolveError, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, + _: js.JSObjectRef, + _: js.JSStringRef, exception: js.ExceptionRef, ) js.JSValueRef { return BuildError.generatePositionObject(this.msg, ctx, exception); @@ -2146,30 +2192,30 @@ pub const ResolveError = struct { pub fn getMessage( this: *ResolveError, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(this.msg.data.text).toValue(VirtualMachine.vm.global).asRef(); } pub fn getSpecifier( this: *ResolveError, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(this.msg.metadata.resolve.specifier.slice(this.msg.data.text)).toValue(VirtualMachine.vm.global).asRef(); } pub fn getImportKind( this: *ResolveError, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(@tagName(this.msg.metadata.resolve.import_kind)).toValue(VirtualMachine.vm.global).asRef(); } @@ -2177,9 +2223,9 @@ pub const ResolveError = struct { pub fn getReferrer( this: *ResolveError, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.referrer) |referrer| { return ZigString.init(referrer.text).toValue(VirtualMachine.vm.global).asRef(); @@ -2190,11 +2236,11 @@ pub const ResolveError = struct { const BuildErrorName = "ResolveError"; pub fn getName( - this: *ResolveError, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: *ResolveError, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(BuildErrorName).toValue(VirtualMachine.vm.global).asRef(); } @@ -2203,7 +2249,7 @@ pub const ResolveError = struct { pub const BuildError = struct { msg: logger.Msg, // resolve_result: Resolver.Result, - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, logged: bool = false, pub const Class = NewClass( @@ -2231,7 +2277,7 @@ pub const BuildError = struct { ); pub fn create( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, msg: logger.Msg, // resolve_result: *const Resolver.Result, ) js.JSObjectRef { @@ -2250,8 +2296,8 @@ pub const BuildError = struct { pub fn getPosition( this: *BuildError, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, + _: js.JSObjectRef, + _: js.JSStringRef, exception: js.ExceptionRef, ) js.JSValueRef { return generatePositionObject(this.msg, ctx, exception); @@ -2383,21 +2429,21 @@ pub const BuildError = struct { pub fn getMessage( this: *BuildError, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(this.msg.data.text).toValue(VirtualMachine.vm.global).asRef(); } const BuildErrorName = "BuildError"; pub fn getName( - this: *BuildError, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: *BuildError, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(BuildErrorName).toValue(VirtualMachine.vm.global).asRef(); } diff --git a/src/javascript/jsc/JavascriptCore.zig b/src/javascript/jsc/javascript_core_c_api.zig index 807ea0053..ecd37c44a 100644 --- a/src/javascript/jsc/JavascriptCore.zig +++ b/src/javascript/jsc/javascript_core_c_api.zig @@ -1,6 +1,6 @@ const cpp = @import("./bindings/bindings.zig"); const generic = opaque {}; -pub const Private = c_void; +pub const Private = anyopaque; pub const struct_OpaqueJSContextGroup = generic; pub const JSContextGroupRef = ?*const struct_OpaqueJSContextGroup; pub const struct_OpaqueJSContext = generic; @@ -18,7 +18,7 @@ pub const struct_OpaqueJSPropertyNameArray = generic; pub const JSPropertyNameArrayRef = ?*struct_OpaqueJSPropertyNameArray; pub const struct_OpaqueJSPropertyNameAccumulator = generic; pub const JSPropertyNameAccumulatorRef = ?*struct_OpaqueJSPropertyNameAccumulator; -pub const JSTypedArrayBytesDeallocator = ?fn (*c_void, *c_void) callconv(.C) void; +pub const JSTypedArrayBytesDeallocator = ?fn (*anyopaque, *anyopaque) callconv(.C) void; pub const OpaqueJSValue = generic; pub const JSValueRef = ?*OpaqueJSValue; pub const JSObjectRef = ?*OpaqueJSValue; @@ -167,7 +167,7 @@ pub extern const kJSClassDefinitionEmpty: JSClassDefinition; pub extern "c" fn JSClassCreate(definition: [*c]const JSClassDefinition) JSClassRef; pub extern "c" fn JSClassRetain(jsClass: JSClassRef) JSClassRef; pub extern "c" fn JSClassRelease(jsClass: JSClassRef) void; -pub extern "c" fn JSObjectMake(ctx: JSContextRef, jsClass: JSClassRef, data: ?*c_void) JSObjectRef; +pub extern "c" fn JSObjectMake(ctx: JSContextRef, jsClass: JSClassRef, data: ?*anyopaque) JSObjectRef; pub extern "c" fn JSObjectMakeFunctionWithCallback(ctx: JSContextRef, name: JSStringRef, callAsFunction: JSObjectCallAsFunctionCallback) JSObjectRef; pub extern "c" fn JSObjectMakeConstructor(ctx: JSContextRef, jsClass: JSClassRef, callAsConstructor: JSObjectCallAsConstructorCallback) JSObjectRef; pub extern "c" fn JSObjectMakeArray(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: ExceptionRef) JSObjectRef; @@ -188,8 +188,8 @@ pub extern "c" fn JSObjectSetPropertyForKey(ctx: JSContextRef, object: JSObjectR pub extern "c" fn JSObjectDeletePropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: ExceptionRef) bool; pub extern "c" fn JSObjectGetPropertyAtIndex(ctx: JSContextRef, object: JSObjectRef, propertyIndex: c_uint, exception: ExceptionRef) JSValueRef; pub extern "c" fn JSObjectSetPropertyAtIndex(ctx: JSContextRef, object: JSObjectRef, propertyIndex: c_uint, value: JSValueRef, exception: ExceptionRef) void; -pub extern "c" fn JSObjectGetPrivate(object: JSObjectRef) ?*c_void; -pub extern "c" fn JSObjectSetPrivate(object: JSObjectRef, data: ?*c_void) bool; +pub extern "c" fn JSObjectGetPrivate(object: JSObjectRef) ?*anyopaque; +pub extern "c" fn JSObjectSetPrivate(object: JSObjectRef, data: ?*anyopaque) bool; pub extern "c" fn JSObjectIsFunction(ctx: JSContextRef, object: JSObjectRef) bool; pub extern "c" fn JSObjectCallAsFunction(ctx: JSContextRef, object: JSObjectRef, thisObject: JSObjectRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: ExceptionRef) JSValueRef; pub extern "c" fn JSObjectIsConstructor(ctx: JSContextRef, object: JSObjectRef) bool; @@ -224,18 +224,17 @@ pub extern fn JSStringGetUTF8CString(string: JSStringRef, buffer: [*c]u8, buffer pub extern fn JSStringIsEqual(a: JSStringRef, b: JSStringRef) bool; pub extern fn JSStringIsEqualToUTF8CString(a: JSStringRef, b: [*c]const u8) bool; pub extern fn JSObjectMakeTypedArray(ctx: JSContextRef, arrayType: JSTypedArrayType, length: usize, exception: ExceptionRef) JSObjectRef; -pub extern fn JSObjectMakeTypedArrayWithBytesNoCopy(ctx: JSContextRef, arrayType: JSTypedArrayType, bytes: ?*c_void, byteLength: usize, bytesDeallocator: JSTypedArrayBytesDeallocator, deallocatorContext: ?*c_void, exception: ExceptionRef) JSObjectRef; +pub extern fn JSObjectMakeTypedArrayWithBytesNoCopy(ctx: JSContextRef, arrayType: JSTypedArrayType, bytes: ?*anyopaque, byteLength: usize, bytesDeallocator: JSTypedArrayBytesDeallocator, deallocatorContext: ?*anyopaque, exception: ExceptionRef) JSObjectRef; pub extern fn JSObjectMakeTypedArrayWithArrayBuffer(ctx: JSContextRef, arrayType: JSTypedArrayType, buffer: JSObjectRef, exception: ExceptionRef) JSObjectRef; pub extern fn JSObjectMakeTypedArrayWithArrayBufferAndOffset(ctx: JSContextRef, arrayType: JSTypedArrayType, buffer: JSObjectRef, byteOffset: usize, length: usize, exception: ExceptionRef) JSObjectRef; -pub extern fn JSObjectGetTypedArrayBytesPtr(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) ?*c_void; +pub extern fn JSObjectGetTypedArrayBytesPtr(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) ?*anyopaque; pub extern fn JSObjectGetTypedArrayLength(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) usize; pub extern fn JSObjectGetTypedArrayByteLength(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) usize; pub extern fn JSObjectGetTypedArrayByteOffset(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) usize; pub extern fn JSObjectGetTypedArrayBuffer(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) JSObjectRef; -pub extern fn JSObjectMakeArrayBufferWithBytesNoCopy(ctx: JSContextRef, bytes: ?*c_void, byteLength: usize, bytesDeallocator: JSTypedArrayBytesDeallocator, deallocatorContext: ?*c_void, exception: ExceptionRef) JSObjectRef; -pub extern fn JSObjectGetArrayBufferBytesPtr(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) ?*c_void; +pub extern fn JSObjectMakeArrayBufferWithBytesNoCopy(ctx: JSContextRef, bytes: ?*anyopaque, byteLength: usize, bytesDeallocator: JSTypedArrayBytesDeallocator, deallocatorContext: ?*anyopaque, exception: ExceptionRef) JSObjectRef; +pub extern fn JSObjectGetArrayBufferBytesPtr(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) ?*anyopaque; pub extern fn JSObjectGetArrayBufferByteLength(ctx: JSContextRef, object: JSObjectRef, exception: ExceptionRef) usize; -pub extern fn JSStringCreateWithCFString(string: CFStringRef) JSStringRef; pub const OpaqueJSContextGroup = struct_OpaqueJSContextGroup; pub const OpaqueJSContext = struct_OpaqueJSContext; pub const OpaqueJSString = struct_OpaqueJSString; @@ -384,10 +383,10 @@ pub const CellType = enum(u8) { }; } }; -pub const ExternalStringFinalizer = fn (finalize_ptr: ?*c_void, ref: JSStringRef, buffer: *c_void, byteLength: usize) callconv(.C) void; +pub const ExternalStringFinalizer = fn (finalize_ptr: ?*anyopaque, ref: JSStringRef, buffer: *anyopaque, byteLength: usize) callconv(.C) void; pub extern fn JSStringCreate(string: UTF8Ptr, length: usize) JSStringRef; pub extern fn JSStringCreateStatic(string: UTF8Ptr, length: usize) JSStringRef; -pub extern fn JSStringCreateExternal(string: UTF8Ptr, length: usize, finalize_ptr: ?*c_void, finalizer: ExternalStringFinalizer) JSStringRef; +pub extern fn JSStringCreateExternal(string: UTF8Ptr, length: usize, finalize_ptr: ?*anyopaque, finalizer: ExternalStringFinalizer) JSStringRef; pub extern fn JSStringIsEqualToString(a: JSStringRef, string: UTF8Ptr, length: usize) bool; pub extern fn JSStringEncoding(string: JSStringRef) Encoding; pub extern fn JSStringGetCharacters8Ptr(string: JSStringRef) UTF8Ptr; @@ -396,10 +395,10 @@ pub extern fn JSCellType(cell: JSCellValue) CellType; pub extern fn JSStringIsStatic(ref: JSStringRef) bool; pub extern fn JSStringIsExternal(ref: JSStringRef) bool; -pub const JStringIteratorAppendCallback = fn (ctx: *JSStringIterator_, ptr: *c_void, length: u32) callconv(.C) c_void; -pub const JStringIteratorWriteCallback = fn (ctx: *JSStringIterator_, ptr: *c_void, length: u32, offset: u32) callconv(.C) c_void; +pub const JStringIteratorAppendCallback = fn (ctx: *JSStringIterator_, ptr: *anyopaque, length: u32) callconv(.C) anyopaque; +pub const JStringIteratorWriteCallback = fn (ctx: *JSStringIterator_, ptr: *anyopaque, length: u32, offset: u32) callconv(.C) anyopaque; const JSStringIterator_ = extern struct { - ctx: *c_void, + ctx: *anyopaque, stop: u8, append8: JStringIteratorAppendCallback, @@ -409,7 +408,7 @@ const JSStringIterator_ = extern struct { }; pub const JSString = struct { - pub const Callback = fn (finalize_ptr_: ?*c_void, ref: JSStringRef, buffer: *c_void, byteLength: usize) callconv(.C) void; + pub const Callback = fn (finalize_ptr_: ?*anyopaque, ref: JSStringRef, buffer: *anyopaque, byteLength: usize) callconv(.C) void; _ref: JSStringRef = null, backing: Backing = .{ .gc = 0 }, @@ -427,8 +426,8 @@ pub const JSString = struct { const ExternalString = struct { callback: Callback, - external_callback: *c_void, - external_ptr: ?*c_void = null, + external_callback: *anyopaque, + external_ptr: ?*anyopaque = null, slice: []const u8, }; @@ -436,7 +435,7 @@ pub const JSString = struct { const CallbackFunctionType = @TypeOf(callback); const ExternalWrapper = struct { - pub fn finalizer_callback(finalize_ptr_: ?*c_void, buffer: *c_void, byteLength: usize) callconv(.C) void { + pub fn finalizer_callback(finalize_ptr_: ?*anyopaque, buffer: *anyopaque, byteLength: usize) callconv(.C) void { var finalize_ptr = finalize_ptr_ orelse return; var jsstring = @ptrCast( @@ -510,12 +509,6 @@ pub const JSString = struct { return this._ref; } - pub fn fromStringRef(string_ref: JSStringRef) JSString {} - - pub fn init(str: []const u8) JSString {} - - pub fn static(str: []const u8) JSString {} - pub fn finalize(this: *JSString) void { this.loaded = false; } @@ -536,13 +529,6 @@ pub const JSString = struct { // } - pub fn eqlJS(this: *const JSString, ctx: JSContextRef, comptime Type: type, that: Type) bool { - switch (comptime Type) { - JSValueRef => {}, - JSStringRef => {}, - JSString => {}, - } - } }; // not official api functions diff --git a/src/javascript/jsc/typescript.zig b/src/javascript/jsc/typescript.zig index 23c0f037e..d2068254d 100644 --- a/src/javascript/jsc/typescript.zig +++ b/src/javascript/jsc/typescript.zig @@ -1,9 +1,9 @@ -usingnamespace @import("./base.zig"); +const d = @import("./base.zig").d; const std = @import("std"); const Api = @import("../../api/schema.zig").Api; const Router = @import("./api/router.zig"); const JavaScript = @import("./javascript.zig"); -const builtin = std.builtin; +const builtin = @import("builtin"); const io = std.io; const fs = std.fs; const process = std.process; @@ -14,7 +14,6 @@ const mem = std.mem; const testing = std.testing; const Allocator = std.mem.Allocator; const resolve_path = @import("../../resolver/resolve_path.zig"); -usingnamespace @import("./webcore/response.zig"); const modules = [_]d.ts.decl{ Router.Class.typescriptDeclaration(), diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig index 565843423..84e5040bd 100644 --- a/src/javascript/jsc/webcore/response.zig +++ b/src/javascript/jsc/webcore/response.zig @@ -1,19 +1,45 @@ -usingnamespace @import("../base.zig"); const std = @import("std"); const Api = @import("../../../api/schema.zig").Api; const RequestContext = @import("../../../http.zig").RequestContext; const MimeType = @import("../../../http.zig").MimeType; -usingnamespace @import("../javascript.zig"); -usingnamespace @import("../bindings/bindings.zig"); const ZigURL = @import("../../../query_string_map.zig").URL; const HTTPClient = @import("http"); const NetworkThread = @import("network_thread"); +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 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 Response = struct { pub const Class = NewClass( Response, @@ -50,7 +76,7 @@ pub const Response = struct { }, ); - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, body: Body, status_text: string = "", @@ -59,9 +85,9 @@ pub const Response = struct { pub fn getOK( this: *Response, ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { // https://developer.mozilla.org/en-US/docs/Web/API/Response/ok return js.JSValueMakeBoolean(ctx, this.body.init.status_code == 304 or (this.body.init.status_code >= 200 and this.body.init.status_code <= 299)); @@ -69,11 +95,11 @@ pub const Response = struct { pub fn getText( this: *Response, - ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { // https://developer.mozilla.org/en-US/docs/Web/API/Response/text defer this.body.value = .Empty; @@ -113,9 +139,9 @@ pub const Response = struct { pub fn getJson( this: *Response, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSValueRef { var zig_string = ZigString.init(""); @@ -184,9 +210,9 @@ pub const Response = struct { pub fn getArrayBuffer( this: *Response, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSValueRef { defer this.body.value = .Empty; @@ -249,9 +275,9 @@ pub const Response = struct { pub fn getStatus( this: *Response, ctx: js.JSContextRef, - thisObject: js.JSValueRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSValueRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { // https://developer.mozilla.org/en-US/docs/Web/API/Response/status return js.JSValueMakeNumber(ctx, @intToFloat(f64, this.body.init.status_code)); @@ -297,7 +323,7 @@ pub const Response = struct { pub fn constructor( ctx: js.JSContextRef, - function: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSObjectRef { @@ -437,7 +463,7 @@ pub const Fetch = struct { tasklet: *FetchTasklet, }; - pub fn init(allocator: *std.mem.Allocator) anyerror!FetchTasklet { + pub fn init(_: std.mem.Allocator) anyerror!FetchTasklet { return FetchTasklet{}; } @@ -467,7 +493,7 @@ pub const Fetch = struct { this.release(); } - pub fn reset(this: *FetchTasklet) void {} + pub fn reset(_: *FetchTasklet) void {} pub fn release(this: *FetchTasklet) void { js.JSValueUnprotect(this.global_this.ref(), this.resolve); @@ -490,12 +516,12 @@ pub const Fetch = struct { pub const FetchResolver = struct { pub fn call( - ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments_len: usize, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: usize, arguments: [*c]const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) callconv(.C) js.JSObjectRef { return JSPrivateDataPtr.from(js.JSObjectGetPrivate(arguments[0])) .get(FetchTaskletContext).?.tasklet.onResolve().asObjectRef(); @@ -505,12 +531,12 @@ pub const Fetch = struct { pub const FetchRejecter = struct { pub fn call( - ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments_len: usize, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: usize, arguments: [*c]const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) callconv(.C) js.JSObjectRef { return JSPrivateDataPtr.from(js.JSObjectGetPrivate(arguments[0])) .get(FetchTaskletContext).?.tasklet.onReject().asObjectRef(); @@ -557,7 +583,7 @@ pub const Fetch = struct { } pub fn get( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, method: Method, url: ZigURL, headers: Headers.Entries, @@ -586,7 +612,7 @@ pub const Fetch = struct { } pub fn queue( - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, global: *JSGlobalObject, method: Method, url: ZigURL, @@ -617,10 +643,10 @@ pub const Fetch = struct { }; pub fn call( - this: void, + _: void, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSObjectRef { @@ -753,14 +779,10 @@ pub const Fetch = struct { // https://developer.mozilla.org/en-US/docs/Web/API/Headers pub const Headers = struct { - pub const Kv = struct { - name: Api.StringPointer, - value: Api.StringPointer, - }; - pub const Entries = std.MultiArrayList(Kv); - entries: Entries, + pub usingnamespace HTTPClient.Headers; + entries: Headers.Entries, buf: std.ArrayListUnmanaged(u8), - allocator: *std.mem.Allocator, + allocator: std.mem.Allocator, used: u32 = 0, guard: Guard = Guard.none, @@ -778,10 +800,10 @@ pub const Headers = struct { pub fn get( this: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSValueRef { if (arguments.len == 0 or !js.JSValueIsString(ctx, arguments[0]) or js.JSStringIsEqual(arguments[0], Properties.Refs.empty_string)) { return js.JSValueMakeNull(ctx); @@ -805,10 +827,10 @@ pub const Headers = struct { pub fn set( this: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.guard == .request or arguments.len < 2 or !js.JSValueIsString(ctx, arguments[0]) or js.JSStringIsEqual(arguments[0], Properties.Refs.empty_string) or !js.JSValueIsString(ctx, arguments[1])) { return js.JSValueMakeUndefined(ctx); @@ -822,10 +844,10 @@ pub const Headers = struct { pub fn append( this: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.guard == .request or arguments.len < 2 or !js.JSValueIsString(ctx, arguments[0]) or js.JSStringIsEqual(arguments[0], Properties.Refs.empty_string) or !js.JSValueIsString(ctx, arguments[1])) { return js.JSValueMakeUndefined(ctx); @@ -837,10 +859,10 @@ pub const Headers = struct { pub fn delete( this: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.guard == .request or arguments.len < 1 or !js.JSValueIsString(ctx, arguments[0]) or js.JSStringIsEqual(arguments[0], Properties.Refs.empty_string)) { return js.JSValueMakeUndefined(ctx); @@ -856,34 +878,34 @@ pub const Headers = struct { return js.JSValueMakeUndefined(ctx); } pub fn entries( - this: *Headers, + _: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { Output.prettyErrorln("<r><b>Headers.entries()<r> is not implemented yet - sorry!!", .{}); return js.JSValueMakeNull(ctx); } pub fn keys( - this: *Headers, + _: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { - Output.prettyErrorln("H<r><b>eaders.keys()<r> is not implemented yet- sorry!!", .{}); + Output.prettyErrorln("H<r><b>Headers.keys()<r> is not implemented yet- sorry!!", .{}); return js.JSValueMakeNull(ctx); } pub fn values( - this: *Headers, + _: *Headers, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { Output.prettyErrorln("<r><b>Headers.values()<r> is not implemented yet - sorry!!", .{}); return js.JSValueMakeNull(ctx); @@ -963,9 +985,9 @@ pub const Headers = struct { // https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers pub fn constructor( ctx: js.JSContextRef, - function: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.ExceptionRef, ) js.JSObjectRef { var headers = getAllocator(ctx).create(Headers) catch unreachable; if (arguments.len > 0 and js.JSValueIsObjectOfClass(ctx, arguments[0], Headers.Class.get().*)) { @@ -1052,7 +1074,7 @@ pub const Headers = struct { none, }; - pub fn fromPicoHeaders(allocator: *std.mem.Allocator, picohttp_headers: []const picohttp.Header) !Headers { + pub fn fromPicoHeaders(allocator: std.mem.Allocator, picohttp_headers: []const picohttp.Header) !Headers { var total_len: usize = 0; for (picohttp_headers) |header| { total_len += header.name.len; @@ -1062,7 +1084,7 @@ pub const Headers = struct { total_len += picohttp_headers.len * 2; var headers = Headers{ .allocator = allocator, - .entries = Entries{}, + .entries = Headers.Entries{}, .buf = std.ArrayListUnmanaged(u8){}, }; try headers.entries.ensureTotalCapacity(allocator, picohttp_headers.len); @@ -1071,7 +1093,7 @@ pub const Headers = struct { headers.guard = Guard.request; for (picohttp_headers) |header| { - headers.entries.appendAssumeCapacity(Kv{ + headers.entries.appendAssumeCapacity(.{ .name = headers.appendString( string, header.name, @@ -1093,7 +1115,7 @@ pub const Headers = struct { } // TODO: is it worth making this lazy? instead of copying all the request headers, should we just do it on get/put/iterator? - pub fn fromRequestCtx(allocator: *std.mem.Allocator, request: *RequestContext) !Headers { + pub fn fromRequestCtx(allocator: std.mem.Allocator, request: *RequestContext) !Headers { return fromPicoHeaders(allocator, request.request.headers); } @@ -1167,7 +1189,7 @@ pub const Headers = struct { headers.buf.expandToCapacity(); headers.entries.append( headers.allocator, - Kv{ + .{ .name = headers.appendString( string, key, @@ -1248,7 +1270,7 @@ pub const Headers = struct { } pub fn appendInit(this: *Headers, ctx: js.JSContextRef, key: js.JSStringRef, comptime value_type: js.JSType, value: js.JSValueRef) !void { - this.entries.append(this.allocator, Kv{ + this.entries.append(this.allocator, .{ .name = this.appendString(js.JSStringRef, key, true, true, false), .value = switch (comptime value_type) { js.JSType.kJSTypeNumber => this.appendNumber(js.JSValueToNumber(ctx, value, null)), @@ -1277,9 +1299,9 @@ pub const Body = struct { value: Value, ptr: ?[*]u8 = null, len: usize = 0, - ptr_allocator: ?*std.mem.Allocator = null, + ptr_allocator: ?std.mem.Allocator = null, - pub fn deinit(this: *Body, allocator: *std.mem.Allocator) void { + pub fn deinit(this: *Body, allocator: std.mem.Allocator) void { if (this.init.headers) |headers| { headers.deinit(); } @@ -1297,7 +1319,7 @@ pub const Body = struct { headers: ?Headers, status_code: u16, - pub fn init(allocator: *std.mem.Allocator, ctx: js.JSContextRef, init_ref: js.JSValueRef) !?Init { + pub fn init(_: std.mem.Allocator, ctx: js.JSContextRef, init_ref: js.JSValueRef) !?Init { var result = Init{ .headers = null, .status_code = 0 }; var array = js.JSObjectCopyPropertyNames(ctx, init_ref); defer js.JSPropertyNameArrayRelease(array); @@ -1363,7 +1385,7 @@ pub const Body = struct { } }; - pub fn @"404"(ctx: js.JSContextRef) Body { + pub fn @"404"(_: js.JSContextRef) Body { return Body{ .init = Init{ .headers = null, .status_code = 404, @@ -1409,7 +1431,7 @@ pub const Body = struct { if (maybeInit) |init_| { body.init = init_; } - } else |err| {} + } else |_| {} } var wtf_string = JSValue.fromRef(body_ref).toWTFString(VirtualMachine.vm.global); @@ -1465,7 +1487,7 @@ pub const Body = struct { if (maybeInit) |init_| { body.init = init_; } - } else |err| {} + } else |_| {} } body.value = Value{ .ArrayBuffer = buffer }; body.ptr = buffer.ptr[buffer.offset..buffer.byte_len].ptr; @@ -1547,38 +1569,38 @@ pub const Request = struct { ); pub fn getCache( - this: *Request, + _: *Request, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSValueMakeString(ctx, ZigString.init(Properties.UTF8.default).toValueGC(VirtualMachine.vm.global).asRef()); } pub fn getCredentials( - this: *Request, + _: *Request, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSValueMakeString(ctx, ZigString.init(Properties.UTF8.include).toValueGC(VirtualMachine.vm.global).asRef()); } pub fn getDestination( - this: *Request, + _: *Request, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSValueMakeString(ctx, ZigString.init("").toValueGC(VirtualMachine.vm.global).asRef()); } pub fn getHeaders( this: *Request, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.headers == null) { this.headers = Headers.fromRequestCtx(getAllocator(ctx), this.request_context) catch unreachable; @@ -1587,20 +1609,20 @@ pub const Request = struct { return Headers.Class.make(ctx, &this.headers.?); } pub fn getIntegrity( - this: *Request, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: *Request, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.Empty.toValueGC(VirtualMachine.vm.global).asRef(); } pub fn getMethod( this: *Request, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { const string_contents: string = switch (this.request_context.method) { .GET => Properties.UTF8.GET, @@ -1616,29 +1638,29 @@ pub const Request = struct { } pub fn getMode( - this: *Request, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: *Request, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(Properties.UTF8.navigate).toValueGC(VirtualMachine.vm.global).asRef(); } pub fn getRedirect( - this: *Request, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: *Request, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init(Properties.UTF8.follow).toValueGC(VirtualMachine.vm.global).asRef(); } pub fn getReferrer( this: *Request, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.request_context.header("Referrer")) |referrer| { return ZigString.init(referrer.value).toValueGC(VirtualMachine.vm.global).asRef(); @@ -1647,20 +1669,20 @@ pub const Request = struct { } } pub fn getReferrerPolicy( - this: *Request, - ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: *Request, + _: js.JSContextRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return ZigString.init("").toValueGC(VirtualMachine.vm.global).asRef(); } pub fn getUrl( this: *Request, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { if (this.url_string_ref == null) { this.url_string_ref = js.JSStringCreateWithUTF8CString(this.request_context.getFullURL()); @@ -1678,8 +1700,8 @@ pub const FetchEvent = struct { request: Request, pending_promise: ?*JSInternalPromise = null, - onPromiseRejectionCtx: *c_void = undefined, - onPromiseRejectionHandler: ?fn (ctx: *c_void, err: anyerror, fetch_event: *FetchEvent, value: JSValue) void = null, + onPromiseRejectionCtx: *anyopaque = undefined, + onPromiseRejectionHandler: ?fn (ctx: *anyopaque, err: anyerror, fetch_event: *FetchEvent, value: JSValue) void = null, rejected: bool = false, pub const Class = NewClass( @@ -1723,11 +1745,11 @@ pub const FetchEvent = struct { ); pub fn getClient( - this: *FetchEvent, + _: *FetchEvent, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { Output.prettyErrorln("FetchEvent.client is not implemented yet - sorry!!", .{}); Output.flush(); @@ -1736,9 +1758,9 @@ pub const FetchEvent = struct { pub fn getRequest( this: *FetchEvent, ctx: js.JSContextRef, - thisObject: js.JSObjectRef, - prop: js.JSStringRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSStringRef, + _: js.ExceptionRef, ) js.JSValueRef { return Request.Class.make(ctx, &this.request); } @@ -1747,8 +1769,8 @@ pub const FetchEvent = struct { pub fn respondWith( this: *FetchEvent, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, + _: js.JSObjectRef, + _: js.JSObjectRef, arguments: []const js.JSValueRef, exception: js.ExceptionRef, ) js.JSValueRef { @@ -1906,13 +1928,15 @@ pub const FetchEvent = struct { return js.JSValueMakeUndefined(ctx); } + // our implementation of the event listener already does this + // so this is a no-op for us pub fn waitUntil( - this: *FetchEvent, + _: *FetchEvent, ctx: js.JSContextRef, - function: js.JSObjectRef, - thisObject: js.JSObjectRef, - arguments: []const js.JSValueRef, - exception: js.ExceptionRef, + _: js.JSObjectRef, + _: js.JSObjectRef, + _: []const js.JSValueRef, + _: js.ExceptionRef, ) js.JSValueRef { return js.JSValueMakeUndefined(ctx); } |