Bug 153909
| Summary: | Bound functions toString method should return "bound " + the target function's toString() | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Nikita Vasilyev <nvasilyev> |
| Component: | JavaScriptCore | Assignee: | Nobody <webkit-unassigned> |
| Status: | NEW | ||
| Severity: | Normal | CC: | benjamin, burg, cdumez, fpizlo, ggaren, gskachkov, joepeck, keith_miller, mark.lam, msaboff, nvasilyev, oliver, saam, sukolsak, ysuzuki |
| Priority: | P2 | ||
| Version: | WebKit Nightly Build | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
Nikita Vasilyev
Via Bug 153796 [ES6] bound functions .name property should be "bound " + the target function's name
We improved bound functions .name, but .toString() wasn't affected.
> function foo() {return 42}
> boundFunc = foo.bind(null)
> boundFunc.name
< "bound foo"
> boundFunc.toString()
< "function foo() {
[native code]
}"
What if we return the following instead:
> boundFunc.toString()
< "function bound foo() {return 42}"
or this:
> boundFunc.toString()
< "bound function foo() {return 42}"
I have no preference.
Two things are different from the current behavior:
1. Instead of "[native code]", the original content of the function is shown.
This is much more convenient for debugging. Currently, all anonymous
functions look alike:
function () {
[native code]
}
2. Added "bound" prefix.
This is just an idea; I don't think ES6 spec touches this point.
What do you think?
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Saam Barati
(In reply to comment #0)
> Via Bug 153796 [ES6] bound functions .name property should be "bound " + the
> target function's name
>
> We improved bound functions .name, but .toString() wasn't affected.
>
> > function foo() {return 42}
> > boundFunc = foo.bind(null)
> > boundFunc.name
> < "bound foo"
> > boundFunc.toString()
> < "function foo() {
> [native code]
> }"
>
>
> What if we return the following instead:
>
> > boundFunc.toString()
> < "function bound foo() {return 42}"
>
> or this:
>
> > boundFunc.toString()
> < "bound function foo() {return 42}"
>
> I have no preference.
>
> Two things are different from the current behavior:
>
> 1. Instead of "[native code]", the original content of the function is shown.
> This is much more convenient for debugging. Currently, all anonymous
> functions look alike:
>
> function () {
> [native code]
> }
Depending on how we do this, it's not spec compliant.
toString() needs to return a string that if eval()ed
in the original context, would reproduce an identical
function. Or, if that's not possible, the toString(),
when eval()ed, must throw a syntax error.
We could achieve a syntax error with your proposal of:
"function bound foo { return 42; }"
And I considered doing this.
But, there is new a new proposal that would require
the use of "[native code]" here:
https://raw.githubusercontent.com/michaelficarra/Function-prototype-toString-revision/fc69a0a31dc6d625736bfbb712ac782eb241ff3d/proposal.html
Saam Barati
though that proposal is a bit ambiguous.
It says that it's implementation dependent if the bound
function contains info about its target. But, it also
says that it must comply with "the rules below", which
would lead me to think we must show "[native code]"