Javascript engines are not following toString specification for arrow functions


In the specification, string representation of a callable function object must have the syntax of a NativeFunction.

It is interesting to note that several popular JavaScript engines do not follow the specification.
For example,

❯ node
Welcome to Node.js v18.8.0.
Type ".help" for more information.
> let f = x => 1;
> f.toString()
'x => 1'
// GraalVM JavaScript
❯ js
> let f = x => 1;
> f.toString()
x => 1

I wonder if the spec has any reason to stick with this "function (..) {..}" format.

Step 2 says that the host can also provide source text of the function instead.

Actually what I said wasn't 100% correct. Though I still think the examples you gave are spec compliant.

Step 2 says to return the [[SourceText]] of the function if it is available and not blocked by the host. For arrow functions the [[SourceText]] is defined as matching the x => 1 ECMAScript® 2023 Language Specification.

The part of the spec I think you are referring to is specifying what the toString should be if there is not source text for the function.

1 Like