Bug 149743 - Function.toString.call(class Foo {}) doesn't say "class"
Summary: Function.toString.call(class Foo {}) doesn't say "class"
Status: RESOLVED DUPLICATE of bug 155336
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: Safari 9
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-02 01:08 PDT by Jordan Harband
Modified: 2016-06-06 23:23 PDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jordan Harband 2015-10-02 01:08:20 PDT
`Function.toString.call(class Foo {})` should return `class Foo {}`, not `function Foo() {}`.

Function#toString is definitely underspecified in ES6, but the current spec text *allows* for the correct representation, and future spec text will require it. Please fix this ASAP.
Comment 1 Yusuke Suzuki 2015-10-05 14:47:41 PDT
When implementing methods in class body, what happens?


function A() {
}

A.prototype.method = function () { };


class B {
    method() { }
}

In the above case, A.toString() just returns the function code, but B.toString() returns the class body including the method definition.
I don't think there should be difference because class declaration behaves like a syntax sugar.

So I think waiting the things specified in the spec.
Comment 2 Yusuke Suzuki 2015-10-05 14:51:05 PDT
/So I think waiting the things specified in the spec./So I think waiting the things specified in the spec is better./s
Comment 3 Jordan Harband 2015-10-05 15:34:40 PDT
In that example - when the method is dynamically added - the "method" function absolutely wouldn't appear. If you define a function's name later, it doesn't affect its string representation.

While it's fine to wait, this means it's *impossible* to detect when a "class" was defined using "class" syntax, and also does not comply with how Chrome or Edge does it (although it does comply with how Firefox does it).

Waiting for the spec to define it isn't the worst idea, but Chrome and Edge's behavior will almost certainly become the standard - so it'd be nice if WebKit/Safari followed suit.
Comment 4 Saam Barati 2015-10-05 17:27:05 PDT
(In reply to comment #3)
> In that example - when the method is dynamically added - the "method"
> function absolutely wouldn't appear. If you define a function's name later,
> it doesn't affect its string representation.
> 
> While it's fine to wait, this means it's *impossible* to detect when a
> "class" was defined using "class" syntax, and also does not comply with how
> Chrome or Edge does it (although it does comply with how Firefox does it).
Why would you want to detect this?

> Waiting for the spec to define it isn't the worst idea, but Chrome and
> Edge's behavior will almost certainly become the standard - so it'd be nice
> if WebKit/Safari followed suit.
Comment 5 Jordan Harband 2015-10-05 19:59:22 PDT
> Why would you want to detect this?

There's any number of reasons. When I give them, the typical response is, "don't do that" - ie, determinine if a function will throw on [[Call]], or to help determine `IsConstructor` in an engine that doesn't support `new.target`. I've also written modules that detect if something's an arrow function, or a generator, etc - it's a use case that exists. Sadly, it's impossible without this fix.
Comment 6 Joseph Pecoraro 2016-06-06 20:39:35 PDT
This is working as expected now. I believe this was fixed by:

Implement Function.name and Function#toString for ES6 class.
https://bugs.webkit.org/show_bug.cgi?id=155336
http://trac.webkit.org/r198042

*** This bug has been marked as a duplicate of bug 155336 ***
Comment 7 Jordan Harband 2016-06-06 21:22:03 PDT
This is still not fixed in stable, nor in nightly from 2016-06-06.

`Function.toString.call(class Foo {})` returns `
"function () {
    
}"` which is decidedly not correct, especially given the `Function#toString` spec changes that are now stage 3 in ECMAScript.
Comment 8 Joseph Pecoraro 2016-06-06 22:57:34 PDT
(In reply to comment #7)
> This is still not fixed in stable, nor in nightly from 2016-06-06.
> 
> `Function.toString.call(class Foo {})` returns `
> "function () {
>     
> }"` which is decidedly not correct, especially given the `Function#toString`
> spec changes that are now stage 3 in ECMAScript.

When I run this I get:

js> Function.toString.call(class Foo {})
"class Foo {}"

I checked Safari Technology Preview Release 5, WebKit Nightly 6/6 (r201731), and a Trunk Build in `jsc` from around 201700 (give or take 15 commits).

Could you clarify in what version of WebKit you are testing this?
Comment 9 Jordan Harband 2016-06-06 23:23:32 PDT
ahh oops sorry, I was looking at the wrong bugs site :-p this is broken in firefox but works fine in the latest Safari Tech Preview.

Thanks for fixing this!!

*** This bug has been marked as a duplicate of bug 155336 ***