JavaScript function name inference aka stop function naming mayhem

When you are a Firefox developer you are probably familiar with the interesting way of obtaining a stack-trace from JavaScript code.

(new Error()).stack
/* or */
Components.stack

And historically unnamed (anonymous) functions made the stack-trace totally useless, so for example Firefox code has the policy to name such functions instead. Here is a small example.

var Sandbox = {
execute: function () {
throw "Unsafe!";
}
}
try {
Sandbox.execute();
} catch (error) {
console.log(error.stack)
}

Historically this produced a stack-trace like this (executed in Scratchpad):

@Scratchpad:12
@Scratchpad:18

But in Firefox 17 you should see something like this. (This was implemented in Bug 433529)

Sandbox.execute@Scratchpad/1:12
@Scratchpad/1:16

So you don’t need to make up names like “SandboxExcute” for the function and still get something readable. At the moment Components.stack doesn’t yet include this magic, but it should land soon (Bug 805222). This name inference mechanism should be able to handle a few more cases, for details look at the first bug, I linked.

Tags: mozilla