oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73k stars 2.66k forks source link

`Function.prototype.toString()` omits function name, makes lighthouse fail #8624

Open hilja opened 7 months ago

hilja commented 7 months ago

What version of Bun is running?

1.0.25

What platform is your computer?

Darwin 23.3.0 arm64 arm

What steps can reproduce the bug?

Make a file:

// test.js
function fooBar() {
  return 'bar'
}

function foo(fn) {
  console.log(fn.toString())
}

foo(fooBar)

Run it:

$ node test.js
function fooBar() {
  return 'bar'
}
$ bun test.js
function() {
  return "bar";
}

What is the expected behavior?

String with the function name:

function fooBar() {
  return 'bar'
}

What do you see instead?

Function name omitted:

function() {
  return "bar";
}

Additional information

Lighthouse performance metric package fails here https://github.com/GoogleChrome/lighthouse/blob/5e18c5a0656b427e59890dc9c125164ef9f276c3/core/lib/page-functions.js#L578

They have a util like this:

/**
 * @param {Function} fn
 * @return {string}
 */
function getRuntimeFunctionName(fn) {
  const match = fn.toString().match(/function ([\w$]+)/);
  if (!match) throw new Error(`could not find function name for: ${fn}`);
  return match[1];
}

Bun can't find the match and errors out:

576 |  */
577 | function getRuntimeFunctionName(fn) {
578 |   const match = fn.toString().match(/function ([\w$]+)/);
579 |   if (!match) throw new Error(`could not find function name for: ${fn}`);
                          ^
error: could not find function name for: function(string, characterLimit) {
  return Util.truncate(string, characterLimit);
}
      at getRuntimeFunctionName (/Users/bob/web/foo/node_modules/.pnpm/lighthouse@11.5.0/node_modules/lighthouse/core/lib/page-functions.js:581:21)
      at /Users/bob/web/foo/node_modules/.pnpm/lighthouse@11.5.0/node_modules/lighthouse/core/lib/page-functions.js:589:13
bjon commented 5 months ago

Just stumbled upon this too.

Chrome does the same as Node image

hilja commented 5 months ago

Since ES2018, the spec requires the return value of toString() to be the exact same source code as it was declared, including any whitespace and/or comments

Quoting from MDN, it should keep everything in, pretty unambiguous in that way.

I don't really know Zig but I could whip the tests for it to get the ball rolling, would be nice to get this sorted :)

pyronaur commented 4 months ago

👋 Stumbled on this too - I would really like to be able to run Lighthouse with Bun.