diff options
author | 2022-12-28 11:21:21 +0200 | |
---|---|---|
committer | 2022-12-28 01:21:21 -0800 | |
commit | 092b86321c3210b5435deef1d283648eb8ea5a90 (patch) | |
tree | afe3e9f5642a6942ee7dd3895c22b12d246481cb | |
parent | da07811427cc10754414c0f1064c4158b8941fa2 (diff) | |
download | bun-092b86321c3210b5435deef1d283648eb8ea5a90.tar.gz bun-092b86321c3210b5435deef1d283648eb8ea5a90.tar.zst bun-092b86321c3210b5435deef1d283648eb8ea5a90.zip |
log object string correctly (#1674)
use `Symbol.toStringTag` of the object or that of its `.prototype`
fixes #1584
-rw-r--r-- | Makefile | 21 | ||||
-rw-r--r-- | src/bun.js/bindings/bindings.cpp | 20 | ||||
-rw-r--r-- | src/bun.js/bindings/exports.zig | 7 | ||||
-rw-r--r-- | test/bun.js/console/console-iterator-run.js (renamed from test/bun.js/console-iterator-run.js) | 0 | ||||
-rw-r--r-- | test/bun.js/console/console-iterator.test.js (renamed from test/bun.js/console-iterator.test.js) | 0 | ||||
-rw-r--r-- | test/bun.js/console/console-log.expected.txt | 46 | ||||
-rw-r--r-- | test/bun.js/console/console-log.js (renamed from test/bun.js/console-log.js) | 8 | ||||
-rw-r--r-- | test/bun.js/console/console-log.test.ts | 18 |
8 files changed, 94 insertions, 26 deletions
@@ -81,7 +81,7 @@ CC = $(REAL_CC) CXX = $(REAL_CXX) CCACHE_CC_OR_CC := $(REAL_CC) -CCACHE_PATH := $(shell which ccache 2>/dev/null) +CCACHE_PATH := $(shell which ccache 2>/dev/null) CCACHE_CC_FLAG = CC=$(CCACHE_CC_OR_CC) @@ -399,7 +399,7 @@ endif SHARED_LIB_EXTENSION = .so JSC_BINDINGS = $(BINDINGS_OBJ) $(JSC_FILES) -JSC_BINDINGS_DEBUG = $(DEBUG_BINDINGS_OBJ) $(JSC_FILES_DEBUG) +JSC_BINDINGS_DEBUG = $(DEBUG_BINDINGS_OBJ) $(JSC_FILES_DEBUG) RELEASE_FLAGS= DEBUG_FLAGS= @@ -426,10 +426,12 @@ ARCHIVE_FILES_WITHOUT_LIBCRYPTO = $(MINIMUM_ARCHIVE_FILES) \ -lusockets \ $(BUN_DEPS_OUT_DIR)/libuwsockets.o -ARCHIVE_FILES = $(ARCHIVE_FILES_WITHOUT_LIBCRYPTO) +ARCHIVE_FILES = $(ARCHIVE_FILES_WITHOUT_LIBCRYPTO) STATIC_MUSL_FLAG ?= +WRAP_SYMBOLS_ON_LINUX = + ifeq ($(OS_NAME), linux) WRAP_SYMBOLS_ON_LINUX = -Wl,--wrap=fcntl -Wl,--wrap=fcntl64 -Wl,--wrap=stat64 -Wl,--wrap=pow -Wl,--wrap=exp -Wl,--wrap=log \ -Wl,--wrap=lstat \ @@ -459,10 +461,9 @@ PLATFORM_LINKER_FLAGS = $(BUN_CFLAGS) \ -fno-semantic-interposition \ -flto \ -Wl,--allow-multiple-definition \ - -rdynamic \ - $(WRAP_SYMBOLS_ON_LINUX) + -rdynamic + - endif @@ -475,9 +476,9 @@ BUN_LLD_FLAGS_WITHOUT_JSC = $(ARCHIVE_FILES) \ -BUN_LLD_FLAGS = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(JSC_FILES) $(BINDINGS_OBJ) -lwebcrypto -BUN_LLD_FLAGS_DEBUG = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(JSC_FILES_DEBUG) $(DEBUG_BINDINGS_OBJ) -lwebcrypto-debug -BUN_LLD_FLAGS_FAST = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(JSC_FILES_DEBUG) $(BINDINGS_OBJ) -lwebcrypto-debug +BUN_LLD_FLAGS = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(WRAP_SYMBOLS_ON_LINUX) $(JSC_FILES) $(BINDINGS_OBJ) -lwebcrypto +BUN_LLD_FLAGS_DEBUG = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(WRAP_SYMBOLS_ON_LINUX) $(JSC_FILES_DEBUG) $(DEBUG_BINDINGS_OBJ) -lwebcrypto-debug +BUN_LLD_FLAGS_FAST = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(WRAP_SYMBOLS_ON_LINUX) $(JSC_FILES_DEBUG) $(BINDINGS_OBJ) -lwebcrypto-debug CLANG_VERSION = $(shell $(CC) --version | awk '/version/ {for(i=1; i<=NF; i++){if($$i=="version"){split($$(i+1),v,".");print v[1]}}}') @@ -1733,7 +1734,7 @@ webcrypto: sizegen: mkdir -p $(BUN_TMP_DIR) - $(CXX) src/bun.js/headergen/sizegen.cpp -Wl,-dead_strip -Wl,-dead_strip_dylibs -fuse-ld=lld -o $(BUN_TMP_DIR)/sizegen $(CLANG_FLAGS) -O1 + $(CXX) src/bun.js/headergen/sizegen.cpp -Wl,-dead_strip -Wl,-dead_strip_dylibs -fuse-ld=lld -o $(BUN_TMP_DIR)/sizegen $(CLANG_FLAGS) -O1 $(BUN_TMP_DIR)/sizegen > src/bun.js/bindings/sizes.zig diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index f37af006b..f6f596fcb 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -3062,10 +3062,9 @@ void JSC__JSValue__getClassName(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1 arg2->len = 0; } -void JSC__JSValue__getNameProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, - ZigString* arg2) -{ +void JSC__JSValue__getNameProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigString* arg2) +{ JSC::JSObject* obj = JSC::JSValue::decode(JSValue0).getObject(); JSC::VM& vm = arg1->vm(); @@ -3074,10 +3073,7 @@ void JSC__JSValue__getNameProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* a return; } - JSC::JSValue name = obj->getDirect(vm, vm.propertyNames->toStringTagSymbol); - if (name == JSC::JSValue {}) { - name = obj->getDirect(vm, vm.propertyNames->name); - } + JSC::JSValue name = obj->getIfPropertyExists(arg1, vm.propertyNames->toStringTagSymbol); if (name && name.isString()) { auto str = name.toWTFString(arg1); @@ -3412,7 +3408,10 @@ restart: return true; } - if (entry.key() == vm.propertyNames->constructor || entry.key() == vm.propertyNames->length || entry.key() == vm.propertyNames->name || entry.key() == vm.propertyNames->underscoreProto || entry.key() == vm.propertyNames->toStringTagSymbol) + if (entry.key() == vm.propertyNames->constructor + || entry.key() == vm.propertyNames->length + || entry.key() == vm.propertyNames->underscoreProto + || entry.key() == vm.propertyNames->toStringTagSymbol) return true; if (clientData->builtinNames().bunNativePtrPrivateName() == entry.key()) @@ -3488,8 +3487,9 @@ restart: } if ((slot.attributes() & PropertyAttribute::DontEnum) != 0) { - - if (property == vm.propertyNames->length || property == vm.propertyNames->name || property == vm.propertyNames->underscoreProto || property == vm.propertyNames->toStringTagSymbol) + if (property == vm.propertyNames->length + || property == vm.propertyNames->underscoreProto + || property == vm.propertyNames->toStringTagSymbol) continue; } diff --git a/src/bun.js/bindings/exports.zig b/src/bun.js/bindings/exports.zig index b2460cc0d..debe1f734 100644 --- a/src/bun.js/bindings/exports.zig +++ b/src/bun.js/bindings/exports.zig @@ -1672,18 +1672,17 @@ pub const ZigConsoleClient = struct { const enable_ansi_colors = enable_ansi_colors_; pub fn handleFirstProperty(this: *@This(), globalThis: *JSC.JSGlobalObject, value: JSValue) void { if (!value.jsType().isFunction() and !value.isClass(globalThis)) { - var name_str = ZigString.init(""); - - value.getPrototype(globalThis).getNameProperty(globalThis, &name_str); var writer = WrappedWriter(Writer){ .ctx = this.writer, .failed = false, }; + var name_str = ZigString.init(""); + value.getNameProperty(globalThis, &name_str); if (name_str.len > 0 and !strings.eqlComptime(name_str.slice(), "Object")) { writer.print("{} ", .{name_str}); } else { - value.getNameProperty(globalThis, &name_str); + value.getPrototype(globalThis).getNameProperty(globalThis, &name_str); if (name_str.len > 0 and !strings.eqlComptime(name_str.slice(), "Object")) { writer.print("{} ", .{name_str}); } diff --git a/test/bun.js/console-iterator-run.js b/test/bun.js/console/console-iterator-run.js index 7664c85a1..7664c85a1 100644 --- a/test/bun.js/console-iterator-run.js +++ b/test/bun.js/console/console-iterator-run.js diff --git a/test/bun.js/console-iterator.test.js b/test/bun.js/console/console-iterator.test.js index 533f084e1..533f084e1 100644 --- a/test/bun.js/console-iterator.test.js +++ b/test/bun.js/console/console-iterator.test.js diff --git a/test/bun.js/console/console-log.expected.txt b/test/bun.js/console/console-log.expected.txt new file mode 100644 index 000000000..69fb3eeba --- /dev/null +++ b/test/bun.js/console/console-log.expected.txt @@ -0,0 +1,46 @@ +Hello World! +123 +-123 +123.567 +-123.567 +true +false +null +undefined +Symbol(Symbol Description) +2022-02-27T05:11:48.999Z +[ 123, 456, 789 ] +{ + name: "foo" +} +{ + a: 123, + b: 456, + c: 789 +} +{ + a: { + b: { + c: 123 + }, + bacon: true + }, + name: "bar" +} +Promise { <pending> } +[Function] +[Function: Foo] +{} +[Function: foooo] +/FooRegex/ +Is it a bug or a feature that formatting numbers like 123 is colored +String 123 should be 2nd word, 456 == 456 and percent s %s == What okay +{ + foo: { + name: "baz" + }, + bar: [Circular] +} am +[ + {}, {}, {}, {} +] diff --git a/test/bun.js/console-log.js b/test/bun.js/console/console-log.js index 6252d6773..c22303371 100644 --- a/test/bun.js/console-log.js +++ b/test/bun.js/console/console-log.js @@ -10,6 +10,7 @@ console.log(undefined); console.log(Symbol("Symbol Description")); console.log(new Date(2021, 12, 30, 666, 777, 888, 999)); console.log([123, 456, 789]); +console.log({ name: "foo" }); console.log({ a: 123, b: 456, c: 789 }); console.log({ a: { @@ -18,6 +19,7 @@ console.log({ }, bacon: true, }, + name: "bar", }); console.log(new Promise(() => {})); @@ -38,7 +40,7 @@ console.log( "Is it a bug or a feature that formatting numbers like %d is colored", 123, ); -console.log(globalThis); +//console.log(globalThis); console.log( "String %s should be 2nd word, 456 == %s and percent s %s == %s", @@ -50,7 +52,9 @@ console.log( ); const infinteLoop = { - foo: {}, + foo: { + name: "baz", + }, bar: {}, }; diff --git a/test/bun.js/console/console-log.test.ts b/test/bun.js/console/console-log.test.ts new file mode 100644 index 000000000..1365e174d --- /dev/null +++ b/test/bun.js/console/console-log.test.ts @@ -0,0 +1,18 @@ +import { file, spawn } from "bun"; +import { expect, it } from "bun:test"; +import { bunExe } from "bunExe"; + +it("should log to console correctly", async () => { + const { stdout, stderr, exited } = spawn({ + cmd: [bunExe(), import.meta.dir + "/console-log.js"], + stdin: null, + stdout: "pipe", + stderr: "pipe", + env: { + BUN_DEBUG_QUIET_LOGS: "1", + }, + }); + expect(await exited).toBe(0); + expect(await new Response(stderr).text()).toBe("uh oh\n"); + expect(await new Response(stdout).text()).toBe(await new Response(file(import.meta.dir + "/console-log.expected.txt")).text()); +}); |