chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.12k stars 1.2k forks source link

Get name property with empty function name #4951

Closed igor-simoes closed 4 years ago

igor-simoes commented 6 years ago

Hello everyone, I'm testing some JS engines and I found this inconsistency when getting the name property without function name defined.

Chakra version: 1.9.0.0 OS: Ubuntu 16.04

Steps to reproduce

  1. Run this code:
    let foo = function() {};
    let bar = function baz() {};
    print(foo.name);
    print(bar.name);

    Expected output

    foo
    baz

    Actual output

baz



V8, SpiderMonkey and JavascriptCore prints the expected values.

cinfuzz
MSLaguana commented 6 years ago

Looks like this is the part of the spec referring to this: https://www.ecma-international.org/ecma-262/8.0/index.html#sec-assignment-operators-runtime-semantics-evaluation (and a few other "runtime semantics" sections)

It seems that if you are defining a function as part of an assignment, and that function has no name (either a function (...) {…} or (…) => {…}) then we should be setting the name of the function as the name of the variable it is assigned to. I also see that v8 does the same for var x = {f: function () {}}, naming the anonymous function f in that case, but it does not do the same in the case of var x = {}; x.f = function () {}.

Some further reading of the spec at https://www.ecma-international.org/ecma-262/8.0/index.html#sec-object-initializer-runtime-semantics-propertydefinitionevaluation and https://www.ecma-international.org/ecma-262/8.0/index.html#sec-assignment-operators-runtime-semantics-evaluation suggests that chrome's behavior is probably correct, assuming that the abstract operation GetReferencedName(lref) doesn't work / returns empty string for x.f.

jackhorton commented 6 years ago

This is unfortunately a known issue: https://github.com/Microsoft/ChakraCore/issues/3407. Not sure if we should close it as a dupe, though, because its fairly frustrating and impacts Intl quite a bit.

akroshg commented 6 years ago

This should work with -ES6FunctionNameFull. I wonder why we haven't enabled that yet. Is that due to compat break?

farzonl commented 6 years ago

There was a compat break issue in 2014 reported by the edge team. At the time a decision was made to hold off on full es6 function name support until other browsers had fully adopted this feature. Its been 4 years now, most browsers have full function.name support. It may be worth a revisit.

rhuanjl commented 4 years ago

This was fixed in master by #6287