Skip to content

Commit

Permalink
fix: Improve our console.log output for functions
Browse files Browse the repository at this point in the history
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]'
  • Loading branch information
Jake Champion authored and JakeChampion committed Apr 11, 2024
1 parent 7c5798c commit 9a97fc1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion integration-tests/js-compute/fixtures/app/src/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ routes.set("/console", () => {
console.log('Set:',arg)
arg = [1, 2, 3, [], 5]
console.log('Array:',arg)
arg = { a: 1, b: 2, c: 3, d(){}, get f(){return 1}, g: function bar() {} }
arg = { a: 1, b: 2, c: 3, d(){}, get f(){return 1}, g: function bar() {}, h: Array.from }
console.log('Object:',arg)
arg = function () { }
console.log('function:',arg)
Expand Down
12 changes: 6 additions & 6 deletions integration-tests/js-compute/fixtures/app/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -1043,19 +1043,19 @@
},
"logs": [
"stdout :: Log: Happy birthday Aki and Yuki!",
"stdout :: Log: Map: Map(2) { { a: 1, b: { c: 2 } } => 2, [Function foo] => {} }",
"stdout :: Log: Map: Map(2) { { a: 1, b: { c: 2 } } => 2, [ function foo() {\n }] => {} }",
"stdout :: Log: Set: Set(3) { { a: 1, b: { c: 2 } }, 2, 3 }",
"stdout :: Log: Array: [1, 2, 3, [], 5]",
"stdout :: Log: Object: { a: 1, b: 2, c: 3, d: [Function d], f: [Getter], g: [Function bar] }",
"stdout :: Log: function: [Function]",
"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}] }",
"stdout :: Log: function: [ function() {\n }]",
"stdout :: Log: boolean: true",
"stdout :: Log: undefined: undefined",
"stdout :: Log: null: null",
"stdout :: Log: proxy: { a: 21 }",
"stdout :: Log: Infinity: Infinity",
"stdout :: Log: NaN: NaN",
"stdout :: Log: Symbol: Symbol(\"wow\")",
"stdout :: Log: Error: (new Error(\"uh oh\", \"<stdin>\", 40))",
"stdout :: Log: Error: (new Error(\"uh oh\", \"<stdin>\", 7644))",
"stdout :: Log: Number: 1",
"stdout :: Log: Number: 1.111",
"stdout :: Log: BigInt: 10n",
Expand All @@ -1077,9 +1077,9 @@
"stdout :: Log: WeakSet: WeakSet { <items unknown> }",
"stdout :: Log: Promise: Promise { <pending> }",
"stdout :: Log: resolved promise: Promise { 9 }",
"stdout :: Log: rejected promise: Promise { <rejected> (new Error(\"oops\", \"<stdin>\", 85)) }",
"stdout :: Log: rejected promise: Promise { <rejected> (new Error(\"oops\", \"<stdin>\", 7689)) }",
"stdout :: Log: Response: Response { redirected: false, type: \"default\", url: \"\", status: 200, ok: true, statusText: \"\", version: 2, headers: Headers {}, body: ReadableStream { locked: false }, bodyUsed: false }",
"stdout :: Log: Request: Request { method: \"POST\", url: \"https://www.fastly.com/\", version: 2, headers: Headers {}, body: null, bodyUsed: false }",
"stdout :: Log: Request: Request { method: \"POST\", url: \"https://www.fastly.com/\", version: 2, headers: Headers {}, backend: undefined, body: null, bodyUsed: false }",
"stdout :: Log: ReadableStream: ReadableStream { locked: false }",
"stdout :: Log: TransformStream: TransformStream { readable: ReadableStream { locked: false }, writable: WritableStream {} }",
"stdout :: Log: WritableStream: WritableStream {}",
Expand Down
11 changes: 4 additions & 7 deletions runtime/js-compute-runtime/builtins/shared/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,16 @@ JS::Result<mozilla::Ok> ToSource(JSContext *cx, std::string &sourceOut, JS::Hand
JS::RootedObject obj(cx, &val.toObject());

if (JS_ObjectIsFunction(obj)) {
sourceOut += "[Function";
sourceOut += "[";
std::string source;
JS::Rooted<JSFunction *> fun(cx, JS_ValueToFunction(cx, val));
if (fun) {
JS::RootedString name(cx);
if (!JS_GetFunctionId(cx, fun, &name)) {
JS::RootedString result(cx, JS_DecompileFunction(cx, fun));
if (!result) {
return JS::Result<mozilla::Ok>(JS::Error());
}
if (!name) {
name = JS_AtomizeAndPinString(cx, "");
}
sourceOut += " ";
auto msg = core::encode(cx, name);
auto msg = core::encode(cx, result);
if (!msg) {
return JS::Result<mozilla::Ok>(JS::Error());
}
Expand Down

0 comments on commit 9a97fc1

Please sign in to comment.