diff options
| author | 2021-08-02 22:26:13 -0700 | |
|---|---|---|
| committer | 2021-08-02 22:26:13 -0700 | |
| commit | 81f9e0b9e68bbbf4a2081317052bdbf5b56cf596 (patch) | |
| tree | 657ac2f40bdd03ef2490819128dbf25cf14f6207 /src/javascript/jsc | |
| parent | dbda84ff87ba42462a433f377efbcda1b68e0e20 (diff) | |
| download | bun-81f9e0b9e68bbbf4a2081317052bdbf5b56cf596.tar.gz bun-81f9e0b9e68bbbf4a2081317052bdbf5b56cf596.tar.zst bun-81f9e0b9e68bbbf4a2081317052bdbf5b56cf596.zip | |
fix errorssome names
Former-commit-id: a0ceae3471aa3b16356588645eeca1f2159de356
Diffstat (limited to 'src/javascript/jsc')
| -rw-r--r-- | src/javascript/jsc/bindings/bindings.cpp | 70 | ||||
| -rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 14 | ||||
| -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 | 5 | ||||
| -rw-r--r-- | src/javascript/jsc/bindings/headers.zig | 3 |
6 files changed, 122 insertions, 12 deletions
diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp index cdefa8d42..c38cb2bb0 100644 --- a/src/javascript/jsc/bindings/bindings.cpp +++ b/src/javascript/jsc/bindings/bindings.cpp @@ -675,6 +675,11 @@ bool JSC__JSValue__isBigInt32(JSC__JSValue JSValue0) { bool JSC__JSValue__isBoolean(JSC__JSValue JSValue0) { return JSC::JSValue::decode(JSValue0).isBoolean(); } + +bool JSC__JSValue__isClass(JSC__JSValue JSValue0, JSC__JSGlobalObject *arg1) { + JSC::JSValue value = JSC::JSValue::decode(JSValue0); + return value.isConstructor(arg1->vm()); +} bool JSC__JSValue__isCell(JSC__JSValue JSValue0) { return JSC::JSValue::decode(JSValue0).isCell(); } bool JSC__JSValue__isCustomGetterSetter(JSC__JSValue JSValue0) { return JSC::JSValue::decode(JSValue0).isCustomGetterSetter(); @@ -1114,6 +1119,71 @@ void exceptionFromString(ZigException *except, JSC::JSValue value, JSC::JSGlobal ref->ref(); } +static WTF::StringView function_string_view = WTF::StringView("Function"); +void JSC__JSValue__getClassName(JSC__JSValue JSValue0, JSC__JSGlobalObject *arg1, ZigString *arg2) { + JSC::JSCell *cell = JSC::JSValue::decode(JSValue0).asCell(); + if (cell == nullptr) { + arg2->len = 0; + return; + } + + const char *ptr = cell->className(arg1->vm()); + auto view = WTF::StringView(ptr); + + // Fallback to .name if className is empty + if (view.length() == 0 || view == function_string_view) { + JSC__JSValue__getNameProperty(JSValue0, arg1, arg2); + return; + } else { + *arg2 = Zig::toZigString(view); + return; + } + + arg2->len = 0; +} +void JSC__JSValue__getNameProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject *arg1, + ZigString *arg2) { + + JSC::JSObject *obj = JSC::JSValue::decode(JSValue0).getObject(); + + if (obj == nullptr) { + arg2->len = 0; + return; + } + + JSC::JSValue name = obj->getDirect(arg1->vm(), arg1->vm().propertyNames->name); + if (name && name.isString()) { + auto str = name.toWTFString(arg1); + if (!str.isEmpty()) { + *arg2 = Zig::toZigString(str); + return; + } + } + + if (JSC::JSFunction *function = JSC::jsDynamicCast<JSC::JSFunction *>(arg1->vm(), obj)) { + + WTF::String actualName = function->name(arg1->vm()); + if (!actualName.isEmpty() || function->isHostOrBuiltinFunction()) { + *arg2 = Zig::toZigString(actualName); + return; + } + + actualName = function->jsExecutable()->name().string(); + + *arg2 = Zig::toZigString(actualName); + return; + } + + if (JSC::InternalFunction *function = + JSC::jsDynamicCast<JSC::InternalFunction *>(arg1->vm(), obj)) { + auto view = WTF::StringView(function->name()); + *arg2 = Zig::toZigString(view); + return; + } + + arg2->len = 0; +} + void JSC__JSValue__toZigException(JSC__JSValue JSValue0, JSC__JSGlobalObject *arg1, ZigException *exception) { JSC::JSValue value = JSC::JSValue::decode(JSValue0); diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 6fbcd7adf..0701c515e 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -1214,6 +1214,18 @@ pub const JSValue = enum(i64) { return cppFn("isObject", .{this}); } + pub fn isClass(this: JSValue, global: *JSGlobalObject) bool { + return cppFn("isClass", .{ this, global }); + } + + pub fn getNameProperty(this: JSValue, global: *JSGlobalObject, ret: *ZigString) void { + cppFn("getNameProperty", .{ this, global, ret }); + } + + pub fn getClassName(this: JSValue, global: *JSGlobalObject, ret: *ZigString) void { + cppFn("getClassName", .{ this, global, ret }); + } + pub fn isCell(this: JSValue) bool { return cppFn("isCell", .{this}); } @@ -1322,7 +1334,7 @@ pub const JSValue = enum(i64) { return @intToPtr(*c_void, @intCast(usize, @enumToInt(this))); } - pub const Extern = [_][]const u8{ "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" }; + pub const Extern = [_][]const u8{ "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" }; }; pub const PropertyName = extern struct { diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index 189a1aa35..8f055d93e 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -26,7 +26,7 @@ pub const ZigGlobalObject = extern struct { if (!sigaction_installed) { sigaction_installed = true; - sigaction = std.mem.zeroes(std.os.Sigaction); + sigaction = std.mem.zeroes(std.os.Sigaction); sigaction.handler = .{ .sigaction = Handler.global_signal_handler_fn }; std.os.sigaction(std.os.SIGABRT, &sigaction, null); @@ -548,9 +548,21 @@ pub const ZigConsoleClient = struct { if (len == 1) { if (Output.enable_ansi_colors) { - FormattableType.format(@TypeOf(buffered_writer.unbuffered_writer), buffered_writer.unbuffered_writer, vals[0], true) catch {}; + FormattableType.format( + @TypeOf(buffered_writer.unbuffered_writer), + buffered_writer.unbuffered_writer, + vals[0], + global, + true, + ) catch {}; } else { - FormattableType.format(@TypeOf(buffered_writer.unbuffered_writer), buffered_writer.unbuffered_writer, vals[0], false) catch {}; + FormattableType.format( + @TypeOf(buffered_writer.unbuffered_writer), + buffered_writer.unbuffered_writer, + vals[0], + global, + false, + ) catch {}; } _ = buffered_writer.unbuffered_writer.write("\n") catch 0; @@ -567,13 +579,13 @@ pub const ZigConsoleClient = struct { while (i < len) : (i += 1) { _ = if (i > 0) (writer.write(" ") catch 0); - FormattableType.format(@TypeOf(writer), writer, values[i], true) catch {}; + FormattableType.format(@TypeOf(writer), writer, values[i], global, true) catch {}; } } else { while (i < len) : (i += 1) { _ = if (i > 0) (writer.write(" ") catch 0); - FormattableType.format(@TypeOf(writer), writer, values[i], false) catch {}; + FormattableType.format(@TypeOf(writer), writer, values[i], global, false) catch {}; } } @@ -589,8 +601,8 @@ pub const ZigConsoleClient = struct { Null, Boolean, const CellType = CAPI.CellType; - - pub fn format(comptime Writer: type, writer: Writer, value: JSValue, comptime enable_ansi_colors: bool) anyerror!void { + threadlocal var name_buf: [512]u8 = undefined; + pub fn format(comptime Writer: type, writer: Writer, value: JSValue, globalThis: *JSGlobalObject, comptime enable_ansi_colors: bool) anyerror!void { if (value.isCell()) { if (CAPI.JSObjectGetPrivate(value.asRef())) |private_data_ptr| { const priv_data = JS.JSPrivateDataPtr.from(private_data_ptr); @@ -633,10 +645,20 @@ pub const ZigConsoleClient = struct { try writer.print(comptime Output.prettyFmt("<r><yellow>null<r>", enable_ansi_colors), .{}); } else if (value.isBoolean()) { if (value.toBoolean()) { - try writer.print(comptime Output.prettyFmt("<r><blue>true<r>", enable_ansi_colors), .{}); + try writer.print(comptime Output.prettyFmt("<r><yellow>true<r>", enable_ansi_colors), .{}); } else { - try writer.print(comptime Output.prettyFmt("<r><blue>false<r>", enable_ansi_colors), .{}); + try writer.print(comptime Output.prettyFmt("<r><yellow>false<r>", enable_ansi_colors), .{}); } + // } else if (value.isSymbol()) { + // try writer.print(comptime Output.prettyFmt("<r><yellow>Symbol(\"{s}\")<r>", enable_ansi_colors), .{ value.getDescriptionProperty() }); + } else if (value.isClass(globalThis)) { + var printable = ZigString.init(&name_buf); + value.getClassName(globalThis, &printable); + try writer.print("[class {s}]", .{printable.slice()}); + } else if (value.isCallable(globalThis.vm())) { + var printable = ZigString.init(&name_buf); + value.getNameProperty(globalThis, &printable); + try writer.print("[Function {s}]", .{printable.slice()}); } else { var str = value.toWTFString(JS.VirtualMachine.vm.global); _ = try writer.write(str.slice()); diff --git a/src/javascript/jsc/bindings/headers-cpp.h b/src/javascript/jsc/bindings/headers-cpp.h index 16459f5f9..cb4efcc4e 100644 --- a/src/javascript/jsc/bindings/headers-cpp.h +++ b/src/javascript/jsc/bindings/headers-cpp.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1627963169 +//-- AUTOGENERATED FILE -- 1627967644 // clang-format off #pragma once diff --git a/src/javascript/jsc/bindings/headers.h b/src/javascript/jsc/bindings/headers.h index 642e6aa7e..1ec583f22 100644 --- a/src/javascript/jsc/bindings/headers.h +++ b/src/javascript/jsc/bindings/headers.h @@ -1,4 +1,4 @@ -//-- AUTOGENERATED FILE -- 1627963169 +//-- AUTOGENERATED FILE -- 1627967644 // clang-format: off #pragma once @@ -413,7 +413,9 @@ CPP_DECL JSC__JSString* JSC__JSValue__asString(JSC__JSValue JSValue0); 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__getClassName(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2); CPP_DECL JSC__JSValue JSC__JSValue__getErrorsProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); +CPP_DECL void JSC__JSValue__getNameProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2); CPP_DECL JSC__JSValue JSC__JSValue__getPrototype(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); CPP_DECL bool JSC__JSValue__isAggregateError(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); CPP_DECL bool JSC__JSValue__isAnyInt(JSC__JSValue JSValue0); @@ -422,6 +424,7 @@ CPP_DECL bool JSC__JSValue__isBigInt32(JSC__JSValue JSValue0); CPP_DECL bool JSC__JSValue__isBoolean(JSC__JSValue JSValue0); CPP_DECL bool JSC__JSValue__isCallable(JSC__JSValue JSValue0, JSC__VM* arg1); CPP_DECL bool JSC__JSValue__isCell(JSC__JSValue JSValue0); +CPP_DECL bool JSC__JSValue__isClass(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1); CPP_DECL bool JSC__JSValue__isCustomGetterSetter(JSC__JSValue JSValue0); CPP_DECL bool JSC__JSValue__isError(JSC__JSValue JSValue0); CPP_DECL bool JSC__JSValue__isException(JSC__JSValue JSValue0, JSC__VM* arg1); diff --git a/src/javascript/jsc/bindings/headers.zig b/src/javascript/jsc/bindings/headers.zig index 15c47ff5c..167d4f944 100644 --- a/src/javascript/jsc/bindings/headers.zig +++ b/src/javascript/jsc/bindings/headers.zig @@ -236,7 +236,9 @@ pub extern fn JSC__JSValue__asString(JSValue0: JSC__JSValue) [*c]JSC__JSString; 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__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; +pub extern fn JSC__JSValue__getNameProperty(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigString) void; pub extern fn JSC__JSValue__getPrototype(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue; pub extern fn JSC__JSValue__isAggregateError(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) bool; pub extern fn JSC__JSValue__isAnyInt(JSValue0: JSC__JSValue) bool; @@ -245,6 +247,7 @@ pub extern fn JSC__JSValue__isBigInt32(JSValue0: JSC__JSValue) bool; pub extern fn JSC__JSValue__isBoolean(JSValue0: JSC__JSValue) bool; pub extern fn JSC__JSValue__isCallable(JSValue0: JSC__JSValue, arg1: [*c]JSC__VM) bool; pub extern fn JSC__JSValue__isCell(JSValue0: JSC__JSValue) bool; +pub extern fn JSC__JSValue__isClass(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) bool; pub extern fn JSC__JSValue__isCustomGetterSetter(JSValue0: JSC__JSValue) bool; pub extern fn JSC__JSValue__isError(JSValue0: JSC__JSValue) bool; pub extern fn JSC__JSValue__isException(JSValue0: JSC__JSValue, arg1: [*c]JSC__VM) bool; |
