diff options
author | 2022-03-02 22:14:35 -0800 | |
---|---|---|
committer | 2022-03-02 22:14:35 -0800 | |
commit | b878f9054c1ac9a2a9f8f3870d266467c1d44522 (patch) | |
tree | f0f7e437b095492ea2e8e60ed6b27eb87918a55e | |
parent | 871780fd287eef1d8784b90d5d0c36d009085beb (diff) | |
download | bun-b878f9054c1ac9a2a9f8f3870d266467c1d44522.tar.gz bun-b878f9054c1ac9a2a9f8f3870d266467c1d44522.tar.zst bun-b878f9054c1ac9a2a9f8f3870d266467c1d44522.zip |
[bun.js] Add `Bun.inspect` – like util.inspect()
-rw-r--r-- | integration/bunjs-only-snippets/inspect.test.js | 7 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 9 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/exports.zig | 40 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers-cpp.h | 2 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.h | 487 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/headers.zig | 4 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 173 |
7 files changed, 446 insertions, 276 deletions
diff --git a/integration/bunjs-only-snippets/inspect.test.js b/integration/bunjs-only-snippets/inspect.test.js new file mode 100644 index 000000000..70ba8ce73 --- /dev/null +++ b/integration/bunjs-only-snippets/inspect.test.js @@ -0,0 +1,7 @@ +import { it, expect } from "bun:test"; + +it("inspect", () => { + expect(Bun.inspect(new TypeError("what")).includes("TypeError: what")).toBe( + true + ); +}); diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index ae952265e..d30de035a 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -2179,8 +2179,13 @@ pub const JSValue = enum(i64) { return cppFn("isAggregateError", .{ this, globalObject }); } - pub fn forEach(this: JSValue, globalObject: *JSGlobalObject, callback: fn (vm: [*c]VM, globalObject: [*c]JSGlobalObject, nextValue: JSValue) callconv(.C) void) void { - return cppFn("forEach", .{ this, globalObject, callback }); + pub fn forEach( + this: JSValue, + globalObject: *JSGlobalObject, + ctx: ?*anyopaque, + callback: fn (vm: [*c]VM, globalObject: [*c]JSGlobalObject, ctx: ?*anyopaque, nextValue: JSValue) callconv(.C) void, + ) void { + return cppFn("forEach", .{ this, globalObject, ctx, callback }); } pub fn isIterable(this: JSValue, globalObject: *JSGlobalObject) bool { diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index da0a8cb73..52d1a224a 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -804,7 +804,7 @@ pub const ZigConsoleClient = struct { vals: [*]JSValue, len: usize, ) callconv(.C) void { - if (comptime @hasDecl(@import("root"), "bindgen")) { + if (comptime is_bindgen) { return; } @@ -835,7 +835,19 @@ pub const ZigConsoleClient = struct { var writer = buffered_writer.writer(); const BufferedWriterType = @TypeOf(writer); + format(level, global, vals, len, BufferedWriterType, writer, enable_colors, true); + } + pub fn format( + level: MessageLevel, + global: *JSGlobalObject, + vals: [*]const JSValue, + len: usize, + comptime BufferedWriterType: type, + writer: BufferedWriterType, + enable_colors: bool, + add_newline: bool, + ) void { var fmt: Formatter = undefined; defer { if (fmt.map_node) |node| { @@ -849,7 +861,7 @@ pub const ZigConsoleClient = struct { fmt = Formatter{ .remaining_values = &[_]JSValue{} }; const tag = Formatter.Tag.get(vals[0], global); - var unbuffered_writer = buffered_writer.unbuffered_writer.context.writer(); + var unbuffered_writer = writer.context.unbuffered_writer.context.writer(); const UnbufferedWriterType = @TypeOf(unbuffered_writer); if (tag.tag == .String) { @@ -878,9 +890,9 @@ pub const ZigConsoleClient = struct { false, ); } - _ = unbuffered_writer.write("\n") catch 0; + if (add_newline) _ = unbuffered_writer.write("\n") catch 0; } else { - defer buffered_writer.flush() catch {}; + defer writer.context.flush() catch {}; if (enable_colors) { fmt.format( tag, @@ -900,13 +912,13 @@ pub const ZigConsoleClient = struct { false, ); } - _ = writer.write("\n") catch 0; + if (add_newline) _ = writer.write("\n") catch 0; } return; } - defer buffered_writer.flush() catch {}; + defer writer.context.flush() catch {}; var this_value: JSValue = vals[0]; fmt = Formatter{ .remaining_values = vals[0..len][1..] }; @@ -959,11 +971,11 @@ pub const ZigConsoleClient = struct { } } - _ = writer.write("\n") catch 0; + if (add_newline) _ = writer.write("\n") catch 0; } pub const Formatter = struct { - remaining_values: []JSValue = &[_]JSValue{}, + remaining_values: []const JSValue = &[_]JSValue{}, map: Visited.Map = undefined, map_node: ?*Visited.Pool.Node = null, hide_native: bool = false, @@ -1308,7 +1320,15 @@ pub const ZigConsoleClient = struct { writer.print(comptime Output.prettyFmt("<r><yellow>null<r>", enable_ansi_colors), .{}); }, .Error => { - JS.VirtualMachine.printErrorlikeObject(JS.VirtualMachine.vm, value, null, null, enable_ansi_colors); + JS.VirtualMachine.vm.printErrorlikeObject( + value, + null, + null, + + Writer, + writer_, + enable_ansi_colors, + ); }, .Class => { var printable = ZigString.init(&name_buf); @@ -1521,7 +1541,7 @@ pub const ZigConsoleClient = struct { } pub fn format(this: *Formatter, result: Tag.Result, comptime Writer: type, writer: Writer, value: JSValue, globalThis: *JSGlobalObject, comptime enable_ansi_colors: bool) void { - if (comptime @hasDecl(@import("root"), "bindgen")) { + if (comptime is_bindgen) { return; } diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index 3a3800139..2b5abfd75 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1645755471 +//-- AUTOGENERATED FILE -- 1646285827 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index aa100ee4b..1b47caeb4 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,17 +1,17 @@ // clang-format: off -//-- AUTOGENERATED FILE -- 1645755471 +//-- AUTOGENERATED FILE -- 1646285827 #pragma once +#include <stdbool.h> #include <stddef.h> #include <stdint.h> -#include <stdbool.h> #ifdef __cplusplus - #define AUTO_EXTERN_C extern "C" - #define AUTO_EXTERN_C_ZIG extern "C" __attribute__((weak)) +#define AUTO_EXTERN_C extern "C" +#define AUTO_EXTERN_C_ZIG extern "C" __attribute__((weak)) #else - #define AUTO_EXTERN_C - #define AUTO_EXTERN_C_ZIG __attribute__((weak)) +#define AUTO_EXTERN_C +#define AUTO_EXTERN_C_ZIG __attribute__((weak)) #endif #define ZIG_DECL AUTO_EXTERN_C_ZIG #define CPP_DECL AUTO_EXTERN_C @@ -26,227 +26,269 @@ typedef void* JSClassRef; #include <JavaScriptCore/JSClassRef.h> #endif #include "headers-handwritten.h" - typedef struct bJSC__JSModuleRecord { unsigned char bytes[216]; } bJSC__JSModuleRecord; - typedef char* bJSC__JSModuleRecord_buf; - typedef struct bJSC__ThrowScope { unsigned char bytes[8]; } bJSC__ThrowScope; - typedef char* bJSC__ThrowScope_buf; - typedef struct bJSC__CallFrame { unsigned char bytes[8]; } bJSC__CallFrame; - typedef char* bJSC__CallFrame_buf; - typedef struct bJSC__PropertyName { unsigned char bytes[8]; } bJSC__PropertyName; - typedef char* bJSC__PropertyName_buf; - typedef struct bJSC__CatchScope { unsigned char bytes[8]; } bJSC__CatchScope; - typedef char* bJSC__CatchScope_buf; - typedef struct bWTF__String { unsigned char bytes[8]; } bWTF__String; - typedef char* bWTF__String_buf; - typedef struct bWTF__StringView { unsigned char bytes[16]; } bWTF__StringView; - typedef char* bWTF__StringView_buf; - typedef struct bJSC__JSModuleLoader { unsigned char bytes[16]; } bJSC__JSModuleLoader; - typedef char* bJSC__JSModuleLoader_buf; - typedef struct bJSC__Exception { unsigned char bytes[40]; } bJSC__Exception; - typedef char* bJSC__Exception_buf; - typedef struct bJSC__VM { unsigned char bytes[48824]; } bJSC__VM; - typedef char* bJSC__VM_buf; - typedef struct bJSC__JSString { unsigned char bytes[16]; } bJSC__JSString; - typedef char* bJSC__JSString_buf; - typedef struct bJSC__SourceOrigin { unsigned char bytes[48]; } bJSC__SourceOrigin; - typedef char* bJSC__SourceOrigin_buf; - typedef struct bWTF__ExternalStringImpl { unsigned char bytes[32]; } bWTF__ExternalStringImpl; - typedef char* bWTF__ExternalStringImpl_buf; - typedef struct bWTF__StringImpl { unsigned char bytes[24]; } bWTF__StringImpl; - typedef char* bWTF__StringImpl_buf; - typedef struct bJSC__SourceCode { unsigned char bytes[24]; } bJSC__SourceCode; - typedef char* bJSC__SourceCode_buf; - typedef struct bJSC__JSPromise { unsigned char bytes[32]; } bJSC__JSPromise; - typedef char* bJSC__JSPromise_buf; - typedef struct bWTF__URL { unsigned char bytes[40]; } bWTF__URL; - typedef char* bWTF__URL_buf; - typedef struct bJSC__JSFunction { unsigned char bytes[32]; } bJSC__JSFunction; - typedef char* bJSC__JSFunction_buf; - typedef struct bJSC__JSGlobalObject { unsigned char bytes[2400]; } bJSC__JSGlobalObject; - typedef char* bJSC__JSGlobalObject_buf; - typedef struct bJSC__JSCell { unsigned char bytes[8]; } bJSC__JSCell; - typedef char* bJSC__JSCell_buf; - typedef struct bJSC__JSLock { unsigned char bytes[40]; } bJSC__JSLock; - typedef char* bJSC__JSLock_buf; - typedef struct bInspector__ScriptArguments { unsigned char bytes[32]; } bInspector__ScriptArguments; - typedef char* bInspector__ScriptArguments_buf; - typedef struct bJSC__JSInternalPromise { unsigned char bytes[32]; } bJSC__JSInternalPromise; - typedef char* bJSC__JSInternalPromise_buf; - typedef struct bJSC__JSObject { unsigned char bytes[16]; } bJSC__JSObject; - typedef char* bJSC__JSObject_buf; - typedef struct bJSC__Identifier { unsigned char bytes[8]; } bJSC__Identifier; - typedef char* bJSC__Identifier_buf; +typedef struct bJSC__SourceCode { + unsigned char bytes[24]; +} bJSC__SourceCode; +typedef char* bJSC__SourceCode_buf; +typedef struct bWTF__URL { + unsigned char bytes[40]; +} bWTF__URL; +typedef char* bWTF__URL_buf; +typedef struct bJSC__JSModuleRecord { + unsigned char bytes[216]; +} bJSC__JSModuleRecord; +typedef char* bJSC__JSModuleRecord_buf; +typedef struct bJSC__ThrowScope { + unsigned char bytes[8]; +} bJSC__ThrowScope; +typedef char* bJSC__ThrowScope_buf; +typedef struct bJSC__CallFrame { + unsigned char bytes[8]; +} bJSC__CallFrame; +typedef char* bJSC__CallFrame_buf; +typedef struct bJSC__JSFunction { + unsigned char bytes[32]; +} bJSC__JSFunction; +typedef char* bJSC__JSFunction_buf; +typedef struct bJSC__PropertyName { + unsigned char bytes[8]; +} bJSC__PropertyName; +typedef char* bJSC__PropertyName_buf; +typedef struct bJSC__JSGlobalObject { + unsigned char bytes[2400]; +} bJSC__JSGlobalObject; +typedef char* bJSC__JSGlobalObject_buf; +typedef struct bJSC__JSCell { + unsigned char bytes[8]; +} bJSC__JSCell; +typedef char* bJSC__JSCell_buf; +typedef struct bJSC__CatchScope { + unsigned char bytes[8]; +} bJSC__CatchScope; +typedef char* bJSC__CatchScope_buf; +typedef struct bWTF__String { + unsigned char bytes[8]; +} bWTF__String; +typedef char* bWTF__String_buf; +typedef struct bWTF__StringView { + unsigned char bytes[16]; +} bWTF__StringView; +typedef char* bWTF__StringView_buf; +typedef struct bJSC__JSModuleLoader { + unsigned char bytes[16]; +} bJSC__JSModuleLoader; +typedef char* bJSC__JSModuleLoader_buf; +typedef struct bInspector__ScriptArguments { + unsigned char bytes[32]; +} bInspector__ScriptArguments; +typedef char* bInspector__ScriptArguments_buf; +typedef struct bJSC__Exception { + unsigned char bytes[40]; +} bJSC__Exception; +typedef char* bJSC__Exception_buf; +typedef struct bJSC__VM { + unsigned char bytes[48824]; +} bJSC__VM; +typedef char* bJSC__VM_buf; +typedef struct bJSC__JSString { + unsigned char bytes[16]; +} bJSC__JSString; +typedef char* bJSC__JSString_buf; +typedef struct bJSC__SourceOrigin { + unsigned char bytes[48]; +} bJSC__SourceOrigin; +typedef char* bJSC__SourceOrigin_buf; +typedef struct bWTF__ExternalStringImpl { + unsigned char bytes[32]; +} bWTF__ExternalStringImpl; +typedef char* bWTF__ExternalStringImpl_buf; +typedef struct bJSC__JSInternalPromise { + unsigned char bytes[32]; +} bJSC__JSInternalPromise; +typedef char* bJSC__JSInternalPromise_buf; +typedef struct bWTF__StringImpl { + unsigned char bytes[24]; +} bWTF__StringImpl; +typedef char* bWTF__StringImpl_buf; +typedef struct bJSC__JSPromise { + unsigned char bytes[32]; +} bJSC__JSPromise; +typedef char* bJSC__JSPromise_buf; +typedef struct bJSC__JSObject { + unsigned char bytes[16]; +} bJSC__JSObject; +typedef char* bJSC__JSObject_buf; +typedef struct bJSC__Identifier { + unsigned char bytes[8]; +} bJSC__Identifier; +typedef char* bJSC__Identifier_buf; #ifndef __cplusplus - typedef bJSC__CatchScope JSC__CatchScope; // JSC::CatchScope - typedef struct JSC__GeneratorPrototype JSC__GeneratorPrototype; // JSC::GeneratorPrototype - typedef struct JSC__ArrayIteratorPrototype JSC__ArrayIteratorPrototype; // JSC::ArrayIteratorPrototype - typedef ErrorableResolvedSource ErrorableResolvedSource; - typedef struct JSC__JSPromisePrototype JSC__JSPromisePrototype; // JSC::JSPromisePrototype - typedef ErrorableZigString ErrorableZigString; - typedef bJSC__PropertyName JSC__PropertyName; // JSC::PropertyName - typedef bJSC__JSObject JSC__JSObject; // JSC::JSObject - typedef bWTF__ExternalStringImpl WTF__ExternalStringImpl; // WTF::ExternalStringImpl - typedef struct JSC__AsyncIteratorPrototype JSC__AsyncIteratorPrototype; // JSC::AsyncIteratorPrototype - typedef bJSC__JSLock JSC__JSLock; // JSC::JSLock - typedef bJSC__JSModuleLoader JSC__JSModuleLoader; // JSC::JSModuleLoader - typedef struct JSC__AsyncGeneratorPrototype JSC__AsyncGeneratorPrototype; // JSC::AsyncGeneratorPrototype - typedef struct JSC__AsyncGeneratorFunctionPrototype JSC__AsyncGeneratorFunctionPrototype; // JSC::AsyncGeneratorFunctionPrototype - typedef bJSC__Identifier JSC__Identifier; // JSC::Identifier - typedef struct JSC__ArrayPrototype JSC__ArrayPrototype; // JSC::ArrayPrototype - typedef struct Zig__JSMicrotaskCallback Zig__JSMicrotaskCallback; // Zig::JSMicrotaskCallback - typedef bJSC__JSPromise JSC__JSPromise; // JSC::JSPromise - typedef struct JSC__SetIteratorPrototype JSC__SetIteratorPrototype; // JSC::SetIteratorPrototype - typedef SystemError SystemError; - typedef bJSC__JSCell JSC__JSCell; // JSC::JSCell - typedef bJSC__SourceOrigin JSC__SourceOrigin; // JSC::SourceOrigin - typedef bJSC__JSModuleRecord JSC__JSModuleRecord; // JSC::JSModuleRecord - typedef bWTF__String WTF__String; // WTF::String - typedef bWTF__URL WTF__URL; // WTF::URL - typedef struct JSC__IteratorPrototype JSC__IteratorPrototype; // JSC::IteratorPrototype - typedef Bun__Readable Bun__Readable; - typedef bJSC__JSInternalPromise JSC__JSInternalPromise; // JSC::JSInternalPromise - typedef Bun__Writable Bun__Writable; - typedef struct JSC__RegExpPrototype JSC__RegExpPrototype; // JSC::RegExpPrototype - typedef bJSC__CallFrame JSC__CallFrame; // JSC::CallFrame - typedef struct JSC__MapIteratorPrototype JSC__MapIteratorPrototype; // JSC::MapIteratorPrototype - typedef bWTF__StringView WTF__StringView; // WTF::StringView - typedef bJSC__ThrowScope JSC__ThrowScope; // JSC::ThrowScope - typedef bWTF__StringImpl WTF__StringImpl; // WTF::StringImpl - typedef bJSC__VM JSC__VM; // JSC::VM - typedef JSClassRef JSClassRef; - typedef Bun__ArrayBuffer Bun__ArrayBuffer; - typedef bJSC__JSGlobalObject JSC__JSGlobalObject; // JSC::JSGlobalObject - typedef bJSC__JSFunction JSC__JSFunction; // JSC::JSFunction - typedef struct JSC__AsyncFunctionPrototype JSC__AsyncFunctionPrototype; // JSC::AsyncFunctionPrototype - typedef ZigException ZigException; - typedef bJSC__SourceCode JSC__SourceCode; // JSC::SourceCode - typedef struct JSC__BigIntPrototype JSC__BigIntPrototype; // JSC::BigIntPrototype - typedef struct JSC__GeneratorFunctionPrototype JSC__GeneratorFunctionPrototype; // JSC::GeneratorFunctionPrototype - typedef ZigString ZigString; - typedef int64_t JSC__JSValue; - typedef struct JSC__FunctionPrototype JSC__FunctionPrototype; // JSC::FunctionPrototype - typedef bInspector__ScriptArguments Inspector__ScriptArguments; // Inspector::ScriptArguments - typedef bJSC__Exception JSC__Exception; // JSC::Exception - typedef bJSC__JSString JSC__JSString; // JSC::JSString - typedef struct JSC__ObjectPrototype JSC__ObjectPrototype; // JSC::ObjectPrototype - typedef struct JSC__StringPrototype JSC__StringPrototype; // JSC::StringPrototype +typedef bJSC__CatchScope JSC__CatchScope; // JSC::CatchScope +typedef struct JSC__GeneratorPrototype JSC__GeneratorPrototype; // JSC::GeneratorPrototype +typedef struct JSC__ArrayIteratorPrototype JSC__ArrayIteratorPrototype; // JSC::ArrayIteratorPrototype +typedef ErrorableResolvedSource ErrorableResolvedSource; +typedef struct JSC__JSPromisePrototype JSC__JSPromisePrototype; // JSC::JSPromisePrototype +typedef ErrorableZigString ErrorableZigString; +typedef bJSC__PropertyName JSC__PropertyName; // JSC::PropertyName +typedef bJSC__JSObject JSC__JSObject; // JSC::JSObject +typedef bWTF__ExternalStringImpl WTF__ExternalStringImpl; // WTF::ExternalStringImpl +typedef struct JSC__AsyncIteratorPrototype JSC__AsyncIteratorPrototype; // JSC::AsyncIteratorPrototype +typedef bJSC__JSModuleLoader JSC__JSModuleLoader; // JSC::JSModuleLoader +typedef struct JSC__AsyncGeneratorPrototype JSC__AsyncGeneratorPrototype; // JSC::AsyncGeneratorPrototype +typedef struct JSC__AsyncGeneratorFunctionPrototype JSC__AsyncGeneratorFunctionPrototype; // JSC::AsyncGeneratorFunctionPrototype +typedef bJSC__Identifier JSC__Identifier; // JSC::Identifier +typedef struct JSC__ArrayPrototype JSC__ArrayPrototype; // JSC::ArrayPrototype +typedef struct Zig__JSMicrotaskCallback Zig__JSMicrotaskCallback; // Zig::JSMicrotaskCallback +typedef bJSC__JSPromise JSC__JSPromise; // JSC::JSPromise +typedef struct JSC__SetIteratorPrototype JSC__SetIteratorPrototype; // JSC::SetIteratorPrototype +typedef SystemError SystemError; +typedef bJSC__JSCell JSC__JSCell; // JSC::JSCell +typedef bJSC__SourceOrigin JSC__SourceOrigin; // JSC::SourceOrigin +typedef bJSC__JSModuleRecord JSC__JSModuleRecord; // JSC::JSModuleRecord +typedef bWTF__String WTF__String; // WTF::String +typedef bWTF__URL WTF__URL; // WTF::URL +typedef struct JSC__IteratorPrototype JSC__IteratorPrototype; // JSC::IteratorPrototype +typedef Bun__Readable Bun__Readable; +typedef bJSC__JSInternalPromise JSC__JSInternalPromise; // JSC::JSInternalPromise +typedef Bun__Writable Bun__Writable; +typedef struct JSC__RegExpPrototype JSC__RegExpPrototype; // JSC::RegExpPrototype +typedef bJSC__CallFrame JSC__CallFrame; // JSC::CallFrame +typedef struct JSC__MapIteratorPrototype JSC__MapIteratorPrototype; // JSC::MapIteratorPrototype +typedef bWTF__StringView WTF__StringView; // WTF::StringView +typedef bJSC__ThrowScope JSC__ThrowScope; // JSC::ThrowScope +typedef bWTF__StringImpl WTF__StringImpl; // WTF::StringImpl +typedef bJSC__VM JSC__VM; // JSC::VM +typedef JSClassRef JSClassRef; +typedef Bun__ArrayBuffer Bun__ArrayBuffer; +typedef bJSC__JSGlobalObject JSC__JSGlobalObject; // JSC::JSGlobalObject +typedef bJSC__JSFunction JSC__JSFunction; // JSC::JSFunction +typedef struct JSC__AsyncFunctionPrototype JSC__AsyncFunctionPrototype; // JSC::AsyncFunctionPrototype +typedef ZigException ZigException; +typedef bJSC__SourceCode JSC__SourceCode; // JSC::SourceCode +typedef struct JSC__BigIntPrototype JSC__BigIntPrototype; // JSC::BigIntPrototype +typedef struct JSC__GeneratorFunctionPrototype JSC__GeneratorFunctionPrototype; // JSC::GeneratorFunctionPrototype +typedef ZigString ZigString; +typedef int64_t JSC__JSValue; +typedef struct JSC__FunctionPrototype JSC__FunctionPrototype; // JSC::FunctionPrototype +typedef bInspector__ScriptArguments Inspector__ScriptArguments; // Inspector::ScriptArguments +typedef bJSC__Exception JSC__Exception; // JSC::Exception +typedef bJSC__JSString JSC__JSString; // JSC::JSString +typedef struct JSC__ObjectPrototype JSC__ObjectPrototype; // JSC::ObjectPrototype +typedef struct JSC__StringPrototype JSC__StringPrototype; // JSC::StringPrototype #endif #ifdef __cplusplus - namespace JSC { - class JSCell; - class Exception; - class JSPromisePrototype; - class StringPrototype; - class GeneratorFunctionPrototype; - class ArrayPrototype; - class JSString; - class JSObject; - class AsyncIteratorPrototype; - class AsyncGeneratorFunctionPrototype; - class Identifier; - class JSPromise; - class RegExpPrototype; - class AsyncFunctionPrototype; - class CatchScope; - class VM; - class BigIntPrototype; - class SourceOrigin; - class ThrowScope; - class SetIteratorPrototype; - class AsyncGeneratorPrototype; - class PropertyName; - class MapIteratorPrototype; - class JSModuleRecord; - class JSInternalPromise; - class ArrayIteratorPrototype; - class JSFunction; - class JSModuleLoader; - class GeneratorPrototype; - class JSGlobalObject; - class SourceCode; - class JSLock; - class FunctionPrototype; - class IteratorPrototype; - class CallFrame; - class ObjectPrototype; - } - namespace WTF { - class URL; - class StringImpl; - class String; - class StringView; - class ExternalStringImpl; - } - namespace Zig { - class JSMicrotaskCallback; - } - namespace Inspector { - class ScriptArguments; - } - - typedef ErrorableResolvedSource ErrorableResolvedSource; - typedef ErrorableZigString ErrorableZigString; - typedef SystemError SystemError; - typedef Bun__Readable Bun__Readable; - typedef Bun__Writable Bun__Writable; - typedef JSClassRef JSClassRef; - typedef Bun__ArrayBuffer Bun__ArrayBuffer; - typedef ZigException ZigException; - typedef ZigString ZigString; - typedef int64_t JSC__JSValue; - using JSC__JSCell = JSC::JSCell; - using JSC__Exception = JSC::Exception; - using JSC__JSPromisePrototype = JSC::JSPromisePrototype; - using JSC__StringPrototype = JSC::StringPrototype; - using JSC__GeneratorFunctionPrototype = JSC::GeneratorFunctionPrototype; - using JSC__ArrayPrototype = JSC::ArrayPrototype; - using JSC__JSString = JSC::JSString; - using JSC__JSObject = JSC::JSObject; - using JSC__AsyncIteratorPrototype = JSC::AsyncIteratorPrototype; - using JSC__AsyncGeneratorFunctionPrototype = JSC::AsyncGeneratorFunctionPrototype; - using JSC__Identifier = JSC::Identifier; - using JSC__JSPromise = JSC::JSPromise; - using JSC__RegExpPrototype = JSC::RegExpPrototype; - using JSC__AsyncFunctionPrototype = JSC::AsyncFunctionPrototype; - using JSC__CatchScope = JSC::CatchScope; - using JSC__VM = JSC::VM; - using JSC__BigIntPrototype = JSC::BigIntPrototype; - using JSC__SourceOrigin = JSC::SourceOrigin; - using JSC__ThrowScope = JSC::ThrowScope; - using JSC__SetIteratorPrototype = JSC::SetIteratorPrototype; - using JSC__AsyncGeneratorPrototype = JSC::AsyncGeneratorPrototype; - using JSC__PropertyName = JSC::PropertyName; - using JSC__MapIteratorPrototype = JSC::MapIteratorPrototype; - using JSC__JSModuleRecord = JSC::JSModuleRecord; - using JSC__JSInternalPromise = JSC::JSInternalPromise; - using JSC__ArrayIteratorPrototype = JSC::ArrayIteratorPrototype; - using JSC__JSFunction = JSC::JSFunction; - using JSC__JSModuleLoader = JSC::JSModuleLoader; - using JSC__GeneratorPrototype = JSC::GeneratorPrototype; - using JSC__JSGlobalObject = JSC::JSGlobalObject; - using JSC__SourceCode = JSC::SourceCode; - using JSC__JSLock = JSC::JSLock; - using JSC__FunctionPrototype = JSC::FunctionPrototype; - using JSC__IteratorPrototype = JSC::IteratorPrototype; - using JSC__CallFrame = JSC::CallFrame; - using JSC__ObjectPrototype = JSC::ObjectPrototype; - using WTF__URL = WTF::URL; - using WTF__StringImpl = WTF::StringImpl; - using WTF__String = WTF::String; - using WTF__StringView = WTF::StringView; - using WTF__ExternalStringImpl = WTF::ExternalStringImpl; - using Zig__JSMicrotaskCallback = Zig::JSMicrotaskCallback; - using Inspector__ScriptArguments = Inspector::ScriptArguments; +namespace JSC { +class JSCell; +class Exception; +class JSPromisePrototype; +class StringPrototype; +class GeneratorFunctionPrototype; +class ArrayPrototype; +class JSString; +class JSObject; +class AsyncIteratorPrototype; +class AsyncGeneratorFunctionPrototype; +class Identifier; +class JSPromise; +class RegExpPrototype; +class AsyncFunctionPrototype; +class CatchScope; +class VM; +class BigIntPrototype; +class SourceOrigin; +class ThrowScope; +class SetIteratorPrototype; +class AsyncGeneratorPrototype; +class PropertyName; +class MapIteratorPrototype; +class JSModuleRecord; +class JSInternalPromise; +class ArrayIteratorPrototype; +class JSFunction; +class JSModuleLoader; +class GeneratorPrototype; +class JSGlobalObject; +class SourceCode; +class FunctionPrototype; +class IteratorPrototype; +class CallFrame; +class ObjectPrototype; +} +namespace WTF { +class URL; +class StringImpl; +class String; +class StringView; +class ExternalStringImpl; +} +namespace Zig { +class JSMicrotaskCallback; +} +namespace Inspector { +class ScriptArguments; +} + +typedef ErrorableResolvedSource ErrorableResolvedSource; +typedef ErrorableZigString ErrorableZigString; +typedef SystemError SystemError; +typedef Bun__Readable Bun__Readable; +typedef Bun__Writable Bun__Writable; +typedef JSClassRef JSClassRef; +typedef Bun__ArrayBuffer Bun__ArrayBuffer; +typedef ZigException ZigException; +typedef ZigString ZigString; +typedef int64_t JSC__JSValue; +using JSC__JSCell = JSC::JSCell; +using JSC__Exception = JSC::Exception; +using JSC__JSPromisePrototype = JSC::JSPromisePrototype; +using JSC__StringPrototype = JSC::StringPrototype; +using JSC__GeneratorFunctionPrototype = JSC::GeneratorFunctionPrototype; +using JSC__ArrayPrototype = JSC::ArrayPrototype; +using JSC__JSString = JSC::JSString; +using JSC__JSObject = JSC::JSObject; +using JSC__AsyncIteratorPrototype = JSC::AsyncIteratorPrototype; +using JSC__AsyncGeneratorFunctionPrototype = JSC::AsyncGeneratorFunctionPrototype; +using JSC__Identifier = JSC::Identifier; +using JSC__JSPromise = JSC::JSPromise; +using JSC__RegExpPrototype = JSC::RegExpPrototype; +using JSC__AsyncFunctionPrototype = JSC::AsyncFunctionPrototype; +using JSC__CatchScope = JSC::CatchScope; +using JSC__VM = JSC::VM; +using JSC__BigIntPrototype = JSC::BigIntPrototype; +using JSC__SourceOrigin = JSC::SourceOrigin; +using JSC__ThrowScope = JSC::ThrowScope; +using JSC__SetIteratorPrototype = JSC::SetIteratorPrototype; +using JSC__AsyncGeneratorPrototype = JSC::AsyncGeneratorPrototype; +using JSC__PropertyName = JSC::PropertyName; +using JSC__MapIteratorPrototype = JSC::MapIteratorPrototype; +using JSC__JSModuleRecord = JSC::JSModuleRecord; +using JSC__JSInternalPromise = JSC::JSInternalPromise; +using JSC__ArrayIteratorPrototype = JSC::ArrayIteratorPrototype; +using JSC__JSFunction = JSC::JSFunction; +using JSC__JSModuleLoader = JSC::JSModuleLoader; +using JSC__GeneratorPrototype = JSC::GeneratorPrototype; +using JSC__JSGlobalObject = JSC::JSGlobalObject; +using JSC__SourceCode = JSC::SourceCode; +using JSC__FunctionPrototype = JSC::FunctionPrototype; +using JSC__IteratorPrototype = JSC::IteratorPrototype; +using JSC__CallFrame = JSC::CallFrame; +using JSC__ObjectPrototype = JSC::ObjectPrototype; +using WTF__URL = WTF::URL; +using WTF__StringImpl = WTF::StringImpl; +using WTF__String = WTF::String; +using WTF__StringView = WTF::StringView; +using WTF__ExternalStringImpl = WTF::ExternalStringImpl; +using Zig__JSMicrotaskCallback = Zig::JSMicrotaskCallback; +using Inspector__ScriptArguments = Inspector::ScriptArguments; #endif - #pragma mark - JSC::JSObject -CPP_DECL JSC__JSValue JSC__JSObject__create(JSC__JSGlobalObject* arg0, size_t arg1, void* arg2, void (* ArgFn3)(void* arg0, JSC__JSObject* arg1, JSC__JSGlobalObject* arg2)); +CPP_DECL JSC__JSValue JSC__JSObject__create(JSC__JSGlobalObject* arg0, size_t arg1, void* arg2, void (*ArgFn3)(void* arg0, JSC__JSObject* arg1, JSC__JSGlobalObject* arg2)); CPP_DECL size_t JSC__JSObject__getArrayLength(JSC__JSObject* arg0); CPP_DECL JSC__JSValue JSC__JSObject__getDirect(JSC__JSObject* arg0, JSC__JSGlobalObject* arg1, const ZigString* arg2); CPP_DECL JSC__JSValue JSC__JSObject__getIndex(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, uint32_t arg2); @@ -344,7 +386,7 @@ CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArguments(JSC__JSValue JSVal CPP_DECL JSC__JSValue JSC__JSFunction__constructWithArgumentsAndNewTarget(JSC__JSValue JSValue0, JSC__JSValue JSValue1, JSC__JSGlobalObject* arg2, JSC__JSValue* arg3, size_t arg4, JSC__Exception** arg5, const unsigned char* arg6); CPP_DECL JSC__JSValue JSC__JSFunction__constructWithNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2, JSC__Exception** arg3, const unsigned char* arg4); CPP_DECL JSC__JSValue JSC__JSFunction__constructWithoutAnyArgumentsOrNewTarget(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, JSC__Exception** arg2, const unsigned char* arg3); -CPP_DECL JSC__JSFunction* JSC__JSFunction__createFromNative(JSC__JSGlobalObject* arg0, uint16_t arg1, const WTF__String* arg2, void* arg3, JSC__JSValue (* ArgFn4)(void* arg0, JSC__JSGlobalObject* arg1, JSC__CallFrame* arg2)); +CPP_DECL JSC__JSFunction* JSC__JSFunction__createFromNative(JSC__JSGlobalObject* arg0, uint16_t arg1, const WTF__String* arg2, void* arg3, JSC__JSValue (*ArgFn4)(void* arg0, JSC__JSGlobalObject* arg1, JSC__CallFrame* arg2)); CPP_DECL bWTF__String JSC__JSFunction__displayName(JSC__JSFunction* arg0, JSC__VM* arg1); CPP_DECL bWTF__String JSC__JSFunction__getName(JSC__JSFunction* arg0, JSC__VM* arg1); @@ -443,7 +485,7 @@ CPP_DECL JSC__JSValue JSC__JSValue__createStringArray(JSC__JSGlobalObject* arg0, CPP_DECL JSC__JSValue JSC__JSValue__createTypeError(const ZigString* arg0, const ZigString* arg1, JSC__JSGlobalObject* arg2); CPP_DECL bool JSC__JSValue__eqlCell(JSC__JSValue JSValue0, JSC__JSCell* arg1); CPP_DECL bool JSC__JSValue__eqlValue(JSC__JSValue JSValue0, JSC__JSValue JSValue1); -CPP_DECL void JSC__JSValue__forEach(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void (* ArgFn2)(JSC__VM* arg0, JSC__JSGlobalObject* arg1, JSC__JSValue JSValue2)); +CPP_DECL void JSC__JSValue__forEach(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, void* arg2, void (*ArgFn3)(JSC__VM* arg0, JSC__JSGlobalObject* arg1, void* arg2, JSC__JSValue JSValue3)); CPP_DECL JSC__JSValue JSC__JSValue__fromEntries(JSC__JSGlobalObject* arg0, ZigString* arg1, ZigString* arg2, size_t arg3, bool arg4); CPP_DECL void JSC__JSValue__getClassName(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2); CPP_DECL JSC__JSValue JSC__JSValue__getErrorsProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); @@ -521,14 +563,13 @@ CPP_DECL JSC__JSValue JSC__Exception__value(JSC__Exception* arg0); #pragma mark - JSC::VM -CPP_DECL JSC__JSLock* JSC__VM__apiLock(JSC__VM* arg0); CPP_DECL void JSC__VM__clearExecutionTimeLimit(JSC__VM* arg0); CPP_DECL JSC__VM* JSC__VM__create(unsigned char HeapType0); CPP_DECL void JSC__VM__deinit(JSC__VM* arg0, JSC__JSGlobalObject* arg1); CPP_DECL void JSC__VM__deleteAllCode(JSC__VM* arg0, JSC__JSGlobalObject* arg1); CPP_DECL void JSC__VM__drainMicrotasks(JSC__VM* arg0); CPP_DECL bool JSC__VM__executionForbidden(JSC__VM* arg0); -CPP_DECL void JSC__VM__holdAPILock(JSC__VM* arg0, void* arg1, void (* ArgFn2)(void* arg0)); +CPP_DECL void JSC__VM__holdAPILock(JSC__VM* arg0, void* arg1, void (*ArgFn2)(void* arg0)); CPP_DECL bool JSC__VM__isEntered(JSC__VM* arg0); CPP_DECL bool JSC__VM__isJITEnabled(); CPP_DECL JSC__JSValue JSC__VM__runGC(JSC__VM* arg0, bool arg1); @@ -536,7 +577,7 @@ CPP_DECL void JSC__VM__setExecutionForbidden(JSC__VM* arg0, bool arg1); CPP_DECL void JSC__VM__setExecutionTimeLimit(JSC__VM* arg0, double arg1); CPP_DECL void JSC__VM__shrinkFootprint(JSC__VM* arg0); CPP_DECL bool JSC__VM__throwError(JSC__VM* arg0, JSC__JSGlobalObject* arg1, JSC__ThrowScope* arg2, const unsigned char* arg3, size_t arg4); -CPP_DECL void JSC__VM__whenIdle(JSC__VM* arg0, void (* ArgFn1)()); +CPP_DECL void JSC__VM__whenIdle(JSC__VM* arg0, void (*ArgFn1)()); #pragma mark - JSC::ThrowScope @@ -594,7 +635,7 @@ CPP_DECL size_t WTF__StringImpl__length(const WTF__StringImpl* arg0); CPP_DECL const uint16_t* WTF__ExternalStringImpl__characters16(const WTF__ExternalStringImpl* arg0); CPP_DECL const unsigned char* WTF__ExternalStringImpl__characters8(const WTF__ExternalStringImpl* arg0); -CPP_DECL bWTF__ExternalStringImpl WTF__ExternalStringImpl__create(const unsigned char* arg0, size_t arg1, void (* ArgFn2)(void* arg0, unsigned char* arg1, size_t arg2)); +CPP_DECL bWTF__ExternalStringImpl WTF__ExternalStringImpl__create(const unsigned char* arg0, size_t arg1, void (*ArgFn2)(void* arg0, unsigned char* arg1, size_t arg2)); CPP_DECL bool WTF__ExternalStringImpl__is16Bit(const WTF__ExternalStringImpl* arg0); CPP_DECL bool WTF__ExternalStringImpl__is8Bit(const WTF__ExternalStringImpl* arg0); CPP_DECL bool WTF__ExternalStringImpl__isEmpty(const WTF__ExternalStringImpl* arg0); @@ -706,7 +747,6 @@ CPP_DECL ZigException ZigException__fromException(JSC__Exception* arg0); #pragma mark - Zig::ConsoleClient - #ifdef __cplusplus ZIG_DECL void Zig__ConsoleClient__count(void* arg0, JSC__JSGlobalObject* arg1, const unsigned char* arg2, size_t arg3); @@ -727,7 +767,6 @@ ZIG_DECL void Zig__ConsoleClient__timeStamp(void* arg0, JSC__JSGlobalObject* arg #pragma mark - Bun__Timer - #ifdef __cplusplus ZIG_DECL JSC__JSValue Bun__Timer__clearInterval(JSC__JSGlobalObject* arg0, JSC__JSValue JSValue1); diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig index 93169276e..a4ca0a37a 100644 --- a/src/javascript/jsc/bindings/headers.zig +++ b/src/javascript/jsc/bindings/headers.zig @@ -83,7 +83,6 @@ pub const JSC__JSObject = bJSC__JSObject; pub const WTF__ExternalStringImpl = bWTF__ExternalStringImpl; pub const JSC__AsyncIteratorPrototype = struct_JSC__AsyncIteratorPrototype; -pub const JSC__JSLock = bJSC__JSLock; pub const JSC__JSModuleLoader = bJSC__JSModuleLoader; pub const JSC__AsyncGeneratorPrototype = struct_JSC__AsyncGeneratorPrototype; @@ -287,7 +286,7 @@ pub extern fn JSC__JSValue__createStringArray(arg0: [*c]JSC__JSGlobalObject, arg pub extern fn JSC__JSValue__createTypeError(arg0: [*c]const ZigString, arg1: [*c]const ZigString, arg2: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn JSC__JSValue__eqlCell(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSCell) bool; pub extern fn JSC__JSValue__eqlValue(JSValue0: JSC__JSValue, JSValue1: JSC__JSValue) bool; -pub extern fn JSC__JSValue__forEach(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, ArgFn2: ?fn ([*c]JSC__VM, [*c]JSC__JSGlobalObject, JSC__JSValue) callconv(.C) void) void; +pub extern fn JSC__JSValue__forEach(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: ?*anyopaque, ArgFn3: ?fn ([*c]JSC__VM, [*c]JSC__JSGlobalObject, ?*anyopaque, JSC__JSValue) callconv(.C) void) void; pub extern fn JSC__JSValue__fromEntries(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]ZigString, arg2: [*c]ZigString, arg3: usize, arg4: bool) JSC__JSValue; pub extern fn JSC__JSValue__getClassName(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigString) void; pub extern fn JSC__JSValue__getErrorsProperty(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; @@ -355,7 +354,6 @@ pub extern fn JSC__PropertyName__uid(arg0: [*c]JSC__PropertyName) [*c]const WTF_ pub extern fn JSC__Exception__create(arg0: [*c]JSC__JSGlobalObject, arg1: [*c]JSC__JSObject, StackCaptureAction2: u8) [*c]JSC__Exception; pub extern fn JSC__Exception__getStackTrace(arg0: [*c]JSC__Exception, arg1: [*c]ZigStackTrace) void; pub extern fn JSC__Exception__value(arg0: [*c]JSC__Exception) JSC__JSValue; -pub extern fn JSC__VM__apiLock(arg0: [*c]JSC__VM) [*c]JSC__JSLock; pub extern fn JSC__VM__clearExecutionTimeLimit(arg0: [*c]JSC__VM) void; pub extern fn JSC__VM__create(HeapType0: u8) [*c]JSC__VM; pub extern fn JSC__VM__deinit(arg0: [*c]JSC__VM, arg1: [*c]JSC__JSGlobalObject) void; diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 64270cc60..afdf01587 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -236,6 +236,45 @@ pub const Bun = struct { return css_imports_list_strings[0..tail]; } + pub fn inspect( + // this + _: void, + ctx: js.JSContextRef, + // function + _: js.JSObjectRef, + // thisObject + _: js.JSObjectRef, + arguments: []const js.JSValueRef, + _: js.ExceptionRef, + ) js.JSValueRef { + if (arguments.len == 0) + return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); + + var array = std.ArrayList(u8).init(getAllocator(ctx)); + var writer = array.writer(); + // we buffer this because it'll almost always be < 4096 + const BufferedWriter = std.io.BufferedWriter(4096, std.ArrayList(u8).Writer); + var buffered_writer = BufferedWriter{ .unbuffered_writer = writer }; + ZigConsoleClient.format( + .Debug, + ctx.ptr(), + @ptrCast([*]const JSValue, arguments.ptr), + arguments.len, + @TypeOf(buffered_writer.writer()), + buffered_writer.writer(), + false, + false, + ); + buffered_writer.flush() catch unreachable; + var zig_str = ZigString.init(array.toOwnedSlice()).withEncoding(); + if (zig_str.len == 0) return ZigString.Empty.toValue(ctx.ptr()).asObjectRef(); + if (!zig_str.isUTF8()) { + return zig_str.toExternalValue(ctx.ptr()).asObjectRef(); + } else { + return zig_str.toValueGC(ctx.ptr()).asObjectRef(); + } + } + pub fn registerMacro( // this _: void, @@ -808,6 +847,13 @@ pub const Bun = struct { .@"return" = "string[]", }, }, + .inspect = .{ + .rfn = Bun.inspect, + .ts = d.ts{ + .name = "inspect", + .@"return" = "string", + }, + }, .getRouteFiles = .{ .rfn = Bun.getRouteFiles, .ts = d.ts{ @@ -1197,6 +1243,7 @@ pub const Bun = struct { countdown: JSValue, repeat: bool, ) !void { + if (comptime is_bindgen) unreachable; var timeout = try VirtualMachine.vm.allocator.create(Timeout); js.JSValueProtect(globalThis.ref(), callback.asObjectRef()); timeout.* = Timeout{ .id = id, .callback = callback, .interval = countdown.toInt32(), .repeat = repeat }; @@ -1211,6 +1258,7 @@ pub const Bun = struct { callback: JSValue, countdown: JSValue, ) callconv(.C) JSValue { + if (comptime is_bindgen) unreachable; const id = VirtualMachine.vm.timer.last_id; VirtualMachine.vm.timer.last_id +%= 1; @@ -1224,6 +1272,7 @@ pub const Bun = struct { callback: JSValue, countdown: JSValue, ) callconv(.C) JSValue { + if (comptime is_bindgen) unreachable; const id = VirtualMachine.vm.timer.last_id; VirtualMachine.vm.timer.last_id +%= 1; @@ -1234,6 +1283,7 @@ pub const Bun = struct { } pub fn clearTimer(id: JSValue, _: *JSGlobalObject) void { + if (comptime is_bindgen) unreachable; var timer: *Timeout = VirtualMachine.vm.timer.timeouts.get(id.toInt32()) orelse return; timer.cancelled = true; } @@ -1242,6 +1292,7 @@ pub const Bun = struct { globalThis: *JSGlobalObject, id: JSValue, ) callconv(.C) JSValue { + if (comptime is_bindgen) unreachable; Timer.clearTimer(id, globalThis); return JSValue.jsUndefined(); } @@ -1249,6 +1300,7 @@ pub const Bun = struct { globalThis: *JSGlobalObject, id: JSValue, ) callconv(.C) JSValue { + if (comptime is_bindgen) unreachable; Timer.clearTimer(id, globalThis); return JSValue.jsUndefined(); } @@ -2506,11 +2558,17 @@ pub const VirtualMachine = struct { pub const ExceptionList = std.ArrayList(Api.JsException); - pub fn printException(this: *VirtualMachine, exception: *Exception, exception_list: ?*ExceptionList) void { + pub fn printException( + this: *VirtualMachine, + exception: *Exception, + exception_list: ?*ExceptionList, + comptime Writer: type, + writer: Writer, + ) void { if (Output.enable_ansi_colors) { - this.printErrorlikeObject(exception.value(), exception, exception_list, true); + this.printErrorlikeObject(exception.value(), exception, exception_list, Writer, writer, true); } else { - this.printErrorlikeObject(exception.value(), exception, exception_list, false); + this.printErrorlikeObject(exception.value(), exception, exception_list, Writer, writer, false); } } @@ -2518,11 +2576,16 @@ pub const VirtualMachine = struct { if (result.isException(this.global.vm())) { var exception = @ptrCast(*Exception, result.asVoid()); - this.printException(exception, exception_list); + this.printException( + exception, + exception_list, + @TypeOf(Output.errorWriter()), + Output.errorWriter(), + ); } else if (Output.enable_ansi_colors) { - this.printErrorlikeObject(result, null, exception_list, true); + this.printErrorlikeObject(result, null, exception_list, @TypeOf(Output.errorWriter()), Output.errorWriter(), true); } else { - this.printErrorlikeObject(result, null, exception_list, false); + this.printErrorlikeObject(result, null, exception_list, @TypeOf(Output.errorWriter()), Output.errorWriter(), false); } } @@ -2629,8 +2692,16 @@ pub const VirtualMachine = struct { // In that case, this function becomes recursive. // In all other cases, we will convert it to a ZigException. const errors_property = ZigString.init("errors"); - pub fn printErrorlikeObject(this: *VirtualMachine, value: JSValue, exception: ?*Exception, exception_list: ?*ExceptionList, comptime allow_ansi_color: bool) void { - if (comptime @hasDecl(@import("root"), "bindgen")) { + pub fn printErrorlikeObject( + this: *VirtualMachine, + value: JSValue, + exception: ?*Exception, + exception_list: ?*ExceptionList, + comptime Writer: type, + writer: Writer, + comptime allow_ansi_color: bool, + ) void { + if (comptime JSC.is_bindgen) { return; } @@ -2643,16 +2714,11 @@ pub const VirtualMachine = struct { var zig_exception: *ZigException = holder.zigException(); exception_.getStackTrace(&zig_exception.stack); if (zig_exception.stack.frames_len > 0) { - var buffered_writer = std.io.bufferedWriter(Output.errorWriter()); - var writer = buffered_writer.writer(); - - if (Output.enable_ansi_colors) { - printStackTrace(@TypeOf(writer), writer, zig_exception.stack, true) catch {}; + if (allow_ansi_color) { + printStackTrace(Writer, writer, zig_exception.stack, true) catch {}; } else { - printStackTrace(@TypeOf(writer), writer, zig_exception.stack, false) catch {}; + printStackTrace(Writer, writer, zig_exception.stack, false) catch {}; } - - buffered_writer.flush() catch {}; } if (exception_list) |list| { @@ -2662,40 +2728,75 @@ pub const VirtualMachine = struct { } } + const ChildWriterType = comptime if (@typeInfo(Writer) == .Pointer) + Writer + else + *Writer; + if (value.isAggregateError(this.global)) { const AggregateErrorIterator = struct { pub var current_exception_list: ?*ExceptionList = null; - pub fn iteratorWithColor(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, nextValue: JSValue) callconv(.C) void { - iterator(_vm, globalObject, nextValue, true); + pub fn iteratorWithColor(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, ctx: ?*anyopaque, nextValue: JSValue) callconv(.C) void { + iterator(_vm, globalObject, nextValue, ctx.?, true); } - pub fn iteratorWithOutColor(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, nextValue: JSValue) callconv(.C) void { - iterator(_vm, globalObject, nextValue, false); + pub fn iteratorWithOutColor(_vm: [*c]VM, globalObject: [*c]JSGlobalObject, ctx: ?*anyopaque, nextValue: JSValue) callconv(.C) void { + iterator(_vm, globalObject, nextValue, ctx.?, false); } - inline fn iterator(_: [*c]VM, _: [*c]JSGlobalObject, nextValue: JSValue, comptime color: bool) void { - VirtualMachine.vm.printErrorlikeObject(nextValue, null, current_exception_list, color); + inline fn iterator(_: [*c]VM, _: [*c]JSGlobalObject, nextValue: JSValue, ctx: ?*anyopaque, comptime color: bool) void { + var casted = @intToPtr(ChildWriterType, @ptrToInt(ctx)); + if (comptime ChildWriterType == Writer) { + VirtualMachine.vm.printErrorlikeObject(nextValue, null, current_exception_list, ChildWriterType, casted, color); + } else { + VirtualMachine.vm.printErrorlikeObject(nextValue, null, current_exception_list, Writer, casted.*, color); + } } }; AggregateErrorIterator.current_exception_list = exception_list; defer AggregateErrorIterator.current_exception_list = null; + var writer_ctx: ?*anyopaque = null; + if (comptime @typeInfo(Writer) == .Pointer) { + writer_ctx = @intToPtr(?*anyopaque, @ptrToInt(writer)); + } else { + writer_ctx = @intToPtr(?*anyopaque, @ptrToInt(&writer)); + } if (comptime allow_ansi_color) { - value.getErrorsProperty(this.global).forEach(this.global, AggregateErrorIterator.iteratorWithColor); + value.getErrorsProperty(this.global).forEach(this.global, writer_ctx, AggregateErrorIterator.iteratorWithColor); } else { - value.getErrorsProperty(this.global).forEach(this.global, AggregateErrorIterator.iteratorWithOutColor); + value.getErrorsProperty(this.global).forEach(this.global, writer_ctx, AggregateErrorIterator.iteratorWithOutColor); } return; } if (js.JSValueIsObject(this.global.ref(), value.asRef())) { if (js.JSObjectGetPrivate(value.asRef())) |priv| { - was_internal = this.printErrorFromMaybePrivateData(priv, exception_list, allow_ansi_color); + was_internal = this.printErrorFromMaybePrivateData( + priv, + exception_list, + Writer, + writer, + allow_ansi_color, + ); return; } } - was_internal = this.printErrorFromMaybePrivateData(value.asRef(), exception_list, allow_ansi_color); + was_internal = this.printErrorFromMaybePrivateData( + value.asRef(), + exception_list, + Writer, + writer, + allow_ansi_color, + ); } - pub fn printErrorFromMaybePrivateData(this: *VirtualMachine, value: ?*anyopaque, exception_list: ?*ExceptionList, comptime allow_ansi_color: bool) bool { + pub fn printErrorFromMaybePrivateData( + this: *VirtualMachine, + value: ?*anyopaque, + exception_list: ?*ExceptionList, + comptime Writer: type, + writer: Writer, + comptime allow_ansi_color: bool, + ) bool { const private_data_ptr = JSPrivateDataPtr.from(value); switch (private_data_ptr.tag()) { @@ -2703,7 +2804,6 @@ pub const VirtualMachine = struct { defer Output.flush(); var build_error = private_data_ptr.as(BuildError); if (!build_error.logged) { - var writer = Output.errorWriter(); build_error.msg.writeFormat(writer, allow_ansi_color) catch {}; writer.writeAll("\n") catch {}; build_error.logged = true; @@ -2720,7 +2820,6 @@ pub const VirtualMachine = struct { defer Output.flush(); var resolve_error = private_data_ptr.as(ResolveError); if (!resolve_error.logged) { - var writer = Output.errorWriter(); resolve_error.msg.writeFormat(writer, allow_ansi_color) catch {}; resolve_error.logged = true; } @@ -2735,7 +2834,13 @@ pub const VirtualMachine = struct { return true; }, else => { - this.printErrorInstance(@intToEnum(JSValue, @intCast(i64, (@ptrToInt(value)))), exception_list, allow_ansi_color) catch |err| { + this.printErrorInstance( + @intToEnum(JSValue, @intCast(i64, (@ptrToInt(value)))), + exception_list, + Writer, + writer, + allow_ansi_color, + ) catch |err| { if (comptime Environment.isDebug) { // yo dawg Output.printErrorln("Error while printing Error-like object: {s}", .{@errorName(err)}); @@ -2780,7 +2885,7 @@ pub const VirtualMachine = struct { } } - pub fn printErrorInstance(this: *VirtualMachine, error_instance: JSValue, exception_list: ?*ExceptionList, comptime allow_ansi_color: bool) !void { + pub fn printErrorInstance(this: *VirtualMachine, error_instance: JSValue, exception_list: ?*ExceptionList, comptime Writer: type, writer: Writer, comptime allow_ansi_color: bool) !void { var exception_holder = ZigException.Holder.init(); var exception = exception_holder.zigException(); error_instance.toZigException(vm.global, exception); @@ -2790,11 +2895,6 @@ pub const VirtualMachine = struct { this.had_errors = true; - var stderr: std.fs.File = Output.errorStream(); - var buffered = std.io.bufferedWriter(stderr.writer()); - var writer = buffered.writer(); - defer buffered.flush() catch unreachable; - var line_numbers = exception.stack.source_lines_numbers[0..exception.stack.source_lines_len]; var max_line: i32 = -1; for (line_numbers) |line| max_line = std.math.max(max_line, line); @@ -3018,6 +3118,7 @@ pub const EventListenerMixin = struct { ctx: *CtxType, comptime onError: fn (ctx: *CtxType, err: anyerror, value: JSValue, request_ctx: *http.RequestContext) anyerror!void, ) !void { + if (comptime JSC.is_bindgen) unreachable; defer { if (request_context.has_called_done) request_context.arena.deinit(); } |