Skip to content

Commit 9a97fc1

Browse files
Jake ChampionJakeChampion
authored andcommitted
fix: Improve our console.log output for functions
We now log out the function's source, unless it is native code, and then we replace the function body with the text '[native code]'
1 parent 7c5798c commit 9a97fc1

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

integration-tests/js-compute/fixtures/app/src/console.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ routes.set("/console", () => {
1414
console.log('Set:',arg)
1515
arg = [1, 2, 3, [], 5]
1616
console.log('Array:',arg)
17-
arg = { a: 1, b: 2, c: 3, d(){}, get f(){return 1}, g: function bar() {} }
17+
arg = { a: 1, b: 2, c: 3, d(){}, get f(){return 1}, g: function bar() {}, h: Array.from }
1818
console.log('Object:',arg)
1919
arg = function () { }
2020
console.log('function:',arg)

integration-tests/js-compute/fixtures/app/tests.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,19 +1043,19 @@
10431043
},
10441044
"logs": [
10451045
"stdout :: Log: Happy birthday Aki and Yuki!",
1046-
"stdout :: Log: Map: Map(2) { { a: 1, b: { c: 2 } } => 2, [Function foo] => {} }",
1046+
"stdout :: Log: Map: Map(2) { { a: 1, b: { c: 2 } } => 2, [ function foo() {\n }] => {} }",
10471047
"stdout :: Log: Set: Set(3) { { a: 1, b: { c: 2 } }, 2, 3 }",
10481048
"stdout :: Log: Array: [1, 2, 3, [], 5]",
1049-
"stdout :: Log: Object: { a: 1, b: 2, c: 3, d: [Function d], f: [Getter], g: [Function bar] }",
1050-
"stdout :: Log: function: [Function]",
1049+
"stdout :: Log: Object: { a: 1, b: 2, c: 3, d: [ d() {\n }], f: [Getter], g: [ function bar() {\n}], h: [ function from() {\n[native code]\n}] }",
1050+
"stdout :: Log: function: [ function() {\n }]",
10511051
"stdout :: Log: boolean: true",
10521052
"stdout :: Log: undefined: undefined",
10531053
"stdout :: Log: null: null",
10541054
"stdout :: Log: proxy: { a: 21 }",
10551055
"stdout :: Log: Infinity: Infinity",
10561056
"stdout :: Log: NaN: NaN",
10571057
"stdout :: Log: Symbol: Symbol(\"wow\")",
1058-
"stdout :: Log: Error: (new Error(\"uh oh\", \"<stdin>\", 40))",
1058+
"stdout :: Log: Error: (new Error(\"uh oh\", \"<stdin>\", 7644))",
10591059
"stdout :: Log: Number: 1",
10601060
"stdout :: Log: Number: 1.111",
10611061
"stdout :: Log: BigInt: 10n",
@@ -1077,9 +1077,9 @@
10771077
"stdout :: Log: WeakSet: WeakSet { <items unknown> }",
10781078
"stdout :: Log: Promise: Promise { <pending> }",
10791079
"stdout :: Log: resolved promise: Promise { 9 }",
1080-
"stdout :: Log: rejected promise: Promise { <rejected> (new Error(\"oops\", \"<stdin>\", 85)) }",
1080+
"stdout :: Log: rejected promise: Promise { <rejected> (new Error(\"oops\", \"<stdin>\", 7689)) }",
10811081
"stdout :: Log: Response: Response { redirected: false, type: \"default\", url: \"\", status: 200, ok: true, statusText: \"\", version: 2, headers: Headers {}, body: ReadableStream { locked: false }, bodyUsed: false }",
1082-
"stdout :: Log: Request: Request { method: \"POST\", url: \"https://www.fastly.com/\", version: 2, headers: Headers {}, body: null, bodyUsed: false }",
1082+
"stdout :: Log: Request: Request { method: \"POST\", url: \"https://www.fastly.com/\", version: 2, headers: Headers {}, backend: undefined, body: null, bodyUsed: false }",
10831083
"stdout :: Log: ReadableStream: ReadableStream { locked: false }",
10841084
"stdout :: Log: TransformStream: TransformStream { readable: ReadableStream { locked: false }, writable: WritableStream {} }",
10851085
"stdout :: Log: WritableStream: WritableStream {}",

runtime/js-compute-runtime/builtins/shared/console.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,16 @@ JS::Result<mozilla::Ok> ToSource(JSContext *cx, std::string &sourceOut, JS::Hand
286286
JS::RootedObject obj(cx, &val.toObject());
287287

288288
if (JS_ObjectIsFunction(obj)) {
289-
sourceOut += "[Function";
289+
sourceOut += "[";
290290
std::string source;
291291
JS::Rooted<JSFunction *> fun(cx, JS_ValueToFunction(cx, val));
292292
if (fun) {
293-
JS::RootedString name(cx);
294-
if (!JS_GetFunctionId(cx, fun, &name)) {
293+
JS::RootedString result(cx, JS_DecompileFunction(cx, fun));
294+
if (!result) {
295295
return JS::Result<mozilla::Ok>(JS::Error());
296296
}
297-
if (!name) {
298-
name = JS_AtomizeAndPinString(cx, "");
299-
}
300297
sourceOut += " ";
301-
auto msg = core::encode(cx, name);
298+
auto msg = core::encode(cx, result);
302299
if (!msg) {
303300
return JS::Result<mozilla::Ok>(JS::Error());
304301
}

0 commit comments

Comments
 (0)