Skip to content

Commit 931d38e

Browse files
committed
Allow get [symbol] syntax in object literals
1 parent 0ef314f commit 931d38e

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4885,7 +4885,8 @@ public static void fillObjectLiteral(
48854885
Callable getterOrSetter = (Callable) value;
48864886
boolean isSetter = getterSetter == 1;
48874887
Integer index = id instanceof Integer ? (Integer) id : null;
4888-
String key = index == null ? ScriptRuntime.toString(id) : null;
4888+
Object key = index != null ? null :
4889+
(id instanceof Symbol ? id : ScriptRuntime.toString(id));
48894890
so.setGetterOrSetter(key, index == null ? 0 : index, getterOrSetter, isSetter);
48904891
}
48914892
}

rhino/src/main/java/org/mozilla/javascript/ScriptableObject.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ public void setAttributes(Symbol key, int attributes) {
559559

560560
/** Implement the legacy "__defineGetter__" and "__defineSetter__" methods. */
561561
public void setGetterOrSetter(
562-
String name, int index, Callable getterOrSetter, boolean isSetter) {
563-
if (name != null && index != 0) throw new IllegalArgumentException(name);
562+
Object name, int index, Callable getterOrSetter, boolean isSetter) {
563+
if (name != null && index != 0) throw new IllegalArgumentException(name.toString());
564564
checkNotSealed(name, index);
565565

566566
AccessorSlot aSlot;

tests/src/test/java/org/mozilla/javascript/tests/ComputedPropertiesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ public void computedPropertyNameForGetterSetterWork() {
109109
});
110110
}
111111

112+
@Test
113+
public void computedPropertyNameAsSymbolForGetterSetterWork() {
114+
Utils.runWithAllOptimizationLevels(
115+
cx -> {
116+
cx.setLanguageVersion(Context.VERSION_ES6);
117+
String script = "var o = { get [Symbol.toStringTag]() { return 'foo'; }}; o.toString()";
118+
119+
ScriptableObject scope = cx.initStandardObjects();
120+
Object value = cx.evaluateString(scope, script, "test", 1, null);
121+
assertEquals("[object foo]", value);
122+
return null;
123+
});
124+
}
125+
112126
@Test
113127
public void yieldWorksForPropertyValues() {
114128
Utils.runWithAllOptimizationLevels(

tests/testsrc/test262.properties

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,7 @@ built-ins/Math 51/327 (15.6%)
10101010

10111011
built-ins/NaN 0/6 (0.0%)
10121012

1013-
built-ins/NativeErrors 29/123 (23.58%)
1014-
AggregateError/errors-iterabletolist-failures.js
1013+
built-ins/NativeErrors 28/123 (22.76%)
10151014
AggregateError/is-a-constructor.js {unsupported: [Reflect.construct]}
10161015
AggregateError/newtarget-proto-custom.js {unsupported: [Reflect.construct]}
10171016
AggregateError/newtarget-proto-fallback.js
@@ -1063,7 +1062,7 @@ built-ins/Number 24/335 (7.16%)
10631062
S9.3.1_A3_T1_U180E.js {unsupported: [u180e]}
10641063
S9.3.1_A3_T2_U180E.js {unsupported: [u180e]}
10651064

1066-
built-ins/Object 216/3408 (6.34%)
1065+
built-ins/Object 215/3408 (6.31%)
10671066
assign/assignment-to-readonly-property-of-target-must-throw-a-typeerror-exception.js
10681067
assign/not-a-constructor.js {unsupported: [Reflect.construct]}
10691068
assign/source-own-prop-desc-missing.js {unsupported: [Proxy]}
@@ -1133,7 +1132,6 @@ built-ins/Object 216/3408 (6.34%)
11331132
freeze/throws-when-false.js
11341133
freeze/typedarray-backed-by-resizable-buffer.js {unsupported: [resizable-arraybuffer]}
11351134
fromEntries/not-a-constructor.js {unsupported: [Reflect.construct]}
1136-
fromEntries/uses-keys-not-iterator.js
11371135
getOwnPropertyDescriptors/not-a-constructor.js {unsupported: [Reflect.construct]}
11381136
getOwnPropertyDescriptors/observable-operations.js {unsupported: [Proxy]}
11391137
getOwnPropertyDescriptors/order-after-define-property.js {unsupported: [Reflect]}
@@ -1679,7 +1677,7 @@ built-ins/Promise 406/631 (64.34%)
16791677

16801678
~built-ins/Reflect
16811679

1682-
built-ins/RegExp 1152/1854 (62.14%)
1680+
built-ins/RegExp 1151/1854 (62.08%)
16831681
CharacterClassEscapes 24/24 (100.0%)
16841682
dotall 4/4 (100.0%)
16851683
escape 20/20 (100.0%)
@@ -1762,7 +1760,6 @@ built-ins/RegExp 1152/1854 (62.14%)
17621760
prototype/Symbol.matchAll/isregexp-this-throws.js
17631761
prototype/Symbol.matchAll/not-a-constructor.js {unsupported: [Reflect.construct]}
17641762
prototype/Symbol.matchAll/species-constructor.js
1765-
prototype/Symbol.matchAll/species-constructor-get-species-throws.js
17661763
prototype/Symbol.matchAll/this-get-flags.js
17671764
prototype/Symbol.matchAll/this-not-object-throws.js
17681765
prototype/Symbol.matchAll/this-tostring-flags.js
@@ -2136,7 +2133,7 @@ built-ins/SetIteratorPrototype 0/11 (0.0%)
21362133

21372134
~built-ins/SharedArrayBuffer
21382135

2139-
built-ins/String 101/1182 (8.54%)
2136+
built-ins/String 93/1182 (7.87%)
21402137
fromCharCode/not-a-constructor.js {unsupported: [Reflect.construct]}
21412138
fromCodePoint/not-a-constructor.js {unsupported: [Reflect.construct]}
21422139
prototype/charAt/not-a-constructor.js {unsupported: [Reflect.construct]}
@@ -2222,15 +2219,7 @@ built-ins/String 101/1182 (8.54%)
22222219
prototype/toWellFormed/not-a-constructor.js {unsupported: [Reflect.construct]}
22232220
prototype/toWellFormed/to-string-primitive.js
22242221
prototype/trimEnd/not-a-constructor.js {unsupported: [Reflect.construct]}
2225-
prototype/trimEnd/this-value-object-toprimitive-call-err.js
2226-
prototype/trimEnd/this-value-object-toprimitive-meth-priority.js
2227-
prototype/trimEnd/this-value-object-tostring-meth-priority.js
2228-
prototype/trimEnd/this-value-object-valueof-meth-priority.js
22292222
prototype/trimStart/not-a-constructor.js {unsupported: [Reflect.construct]}
2230-
prototype/trimStart/this-value-object-toprimitive-call-err.js
2231-
prototype/trimStart/this-value-object-toprimitive-meth-priority.js
2232-
prototype/trimStart/this-value-object-tostring-meth-priority.js
2233-
prototype/trimStart/this-value-object-valueof-meth-priority.js
22342223
prototype/trim/not-a-constructor.js {unsupported: [Reflect.construct]}
22352224
prototype/trim/u180e.js {unsupported: [u180e]}
22362225
prototype/valueOf/non-generic-realm.js
@@ -3512,13 +3501,11 @@ language/comments 9/52 (17.31%)
35123501
multi-line-asi-line-separator.js
35133502
multi-line-asi-paragraph-separator.js
35143503

3515-
language/computed-property-names 37/48 (77.08%)
3504+
language/computed-property-names 35/48 (72.92%)
35163505
class/accessor 4/4 (100.0%)
35173506
class/method 11/11 (100.0%)
35183507
class/static 14/14 (100.0%)
3519-
object/accessor/getter.js
35203508
object/accessor/getter-super.js
3521-
object/accessor/setter.js
35223509
object/accessor/setter-super.js
35233510
object/method/generator.js
35243511
object/method/super.js

0 commit comments

Comments
 (0)