Bug 206888

Summary: [ESNext][BigInt] We don't support BigInt literal as PropertyName
Product: WebKit Reporter: Caio Lima <ticaiolima>
Component: JavaScriptCoreAssignee: Caio Lima <ticaiolima>
Status: RESOLVED FIXED    
Severity: Normal CC: commit-queue, ews-watchlist, keith_miller, mark.lam, msaboff, rmorisset, ross.kirsling, saam, tzagallo, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch none

Description Caio Lima 2020-01-28 09:52:43 PST
According to spec, BigInt is a valid PropertyName (https://tc39.es/ecma262/#prod-PropertyName) and it should be evaluated using ToString (https://tc39.es/ecma262/#sec-object-initializer-runtime-semantics-evaluation). Given this, this script should be evaluated without error:

```
// Object Literal

let o = { 1n: "foo" };
assert(a[1n] === "foo");
assert(a[1] === "foo");
assert(a["1"] === "foo");

// MethodDeclaration

o = { 1n() => "bar" };
assert(a[1n]() === "bar");
assert(a[1]() === "bar");
assert(a["1"]() === "bar");

class C {
  1n() { return "baz"; }
}

let c = new C();
assert(c.[1n]() === "baz");
assert(c.[1]() === "baz");
assert(c.["1"] === "baz");

// Destructuring

let {1n: a} = {1n: "foo"};
assert(a === "foo");

```

Original report from https://bugzilla.mozilla.org/show_bug.cgi?id=1605835
Comment 1 Caio Lima 2020-01-28 10:15:27 PST
Oops, previous script is not correct. Correct version is:

```
// Object Literal

let o = { 1n: "foo" };
assert.sameValue(o[1n], "foo");
assert.sameValue(o[1], "foo");
assert.sameValue(o["1"], "foo");

// MethodDeclaration

o = { 1n() { return "bar"; } };
assert.sameValue(o[1n](), "bar");
assert.sameValue(o[1](), "bar");
assert.sameValue(o["1"](), "bar");

class C {
  1n() { return "baz"; }
}

let c = new C();
assert.sameValue(c[1n](), "baz");
assert.sameValue(c[1](), "baz");
assert.sameValue(c["1"](), "baz");

// Destructuring

let {1n: a} = {1n: "foo"};
assert.sameValue(a, "foo");
```
Comment 2 Caio Lima 2020-01-28 10:28:37 PST
Created attachment 389029 [details]
Patch
Comment 3 Caio Lima 2020-02-11 07:23:31 PST
Created attachment 390368 [details]
Patch
Comment 4 Ross Kirsling 2020-02-11 10:02:34 PST
Wow, interesting that every engine missed this!
Looks like this is the test262 PR? https://github.com/tc39/test262/pull/2457
Comment 5 Ross Kirsling 2020-02-11 10:27:21 PST
Comment on attachment 390368 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=390368&action=review

r=me with comments.

Seems like Mozilla's tests also cover binary/octal/hex literals:
https://searchfox.org/mozilla-central/source/js/src/tests/non262/BigInt/property-name.js
Do you think it's worth adding tests for these (maybe not necessary since they're all just BIGINT tokens)?

> Source/JavaScriptCore/parser/Parser.cpp:4177
> +        InferName inferName = ident && *ident == m_vm.propertyNames->underscoreProto ? InferName::Disallowed : InferName::Allowed;

bigIntString can't be underscoreProto though, can it? Should this be an ASSERT?

> JSTests/ChangeLog:8
> +        * stress/big-int-as-proeprty-name.js: Added.

Nit: spelling in filename :p
Comment 6 Caio Lima 2020-02-11 10:55:54 PST
Comment on attachment 390368 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=390368&action=review

I don't think it hurts to add tests with binary, octals and hexdecimals. I'll add those as well.

>> Source/JavaScriptCore/parser/Parser.cpp:4177
>> +        InferName inferName = ident && *ident == m_vm.propertyNames->underscoreProto ? InferName::Disallowed : InferName::Allowed;
> 
> bigIntString can't be underscoreProto though, can it? Should this be an ASSERT?

This should not be here! I think I'll put InferName::Allowed directly on createProperty.

>> JSTests/ChangeLog:8
>> +        * stress/big-int-as-proeprty-name.js: Added.
> 
> Nit: spelling in filename :p

Oops.
Comment 7 Caio Lima 2020-02-11 14:02:00 PST
Created attachment 390419 [details]
Patch
Comment 8 Caio Lima 2020-02-11 14:04:04 PST
(In reply to Ross Kirsling from comment #5)
> Comment on attachment 390368 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=390368&action=review
> 
> r=me with comments.
> 
> Seems like Mozilla's tests also cover binary/octal/hex literals:
> https://searchfox.org/mozilla-central/source/js/src/tests/non262/BigInt/
> property-name.js
> Do you think it's worth adding tests for these (maybe not necessary since
> they're all just BIGINT tokens)?

It turns out that those cases found a bug on implementation! Thank you for pointing this out. I updated a new version of the patch to handle cases when we use non-decimal literals. Do you mind take another look?
Comment 9 Ross Kirsling 2020-02-11 15:44:54 PST
Comment on attachment 390419 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=390419&action=review

Hooray for tests finding bugs!

> Source/JavaScriptCore/parser/ParserArena.cpp:90
> +    // FIXME: We are allocating a JSBigInt just to be able to use
> +    // JSBigInt::tryGetString when radix is not 10.
> +    // This creates some GC pressure, but since these identifiers
> +    // will only be created when BigInt literal is used as a property name,
> +    // it wont be much problematic, given such cases are very rare.

Should this FIXME have a bug?
Comment 10 Caio Lima 2020-02-12 05:05:45 PST
(In reply to Ross Kirsling from comment #9)
> Comment on attachment 390419 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=390419&action=review
> 
> Hooray for tests finding bugs!
> 
> > Source/JavaScriptCore/parser/ParserArena.cpp:90
> > +    // FIXME: We are allocating a JSBigInt just to be able to use
> > +    // JSBigInt::tryGetString when radix is not 10.
> > +    // This creates some GC pressure, but since these identifiers
> > +    // will only be created when BigInt literal is used as a property name,
> > +    // it wont be much problematic, given such cases are very rare.
> 
> Should this FIXME have a bug?

Yes. I was waiting until get an r+ before creating it. I created https://bugs.webkit.org/show_bug.cgi?id=207627.
Comment 11 Caio Lima 2020-02-12 05:30:44 PST
Created attachment 390507 [details]
Patch
Comment 12 Caio Lima 2020-02-13 13:17:13 PST
Comment on attachment 390507 [details]
Patch

Thank you very much for the review!
Comment 13 WebKit Commit Bot 2020-02-13 13:59:16 PST
Comment on attachment 390507 [details]
Patch

Clearing flags on attachment: 390507

Committed r256541: <https://trac.webkit.org/changeset/256541>
Comment 14 WebKit Commit Bot 2020-02-13 13:59:18 PST
All reviewed patches have been landed.  Closing bug.
Comment 15 Radar WebKit Bug Importer 2020-02-13 14:00:16 PST
<rdar://problem/59436664>