From 9e31c94d02c51d89db472193e46ebe65d3330231 Mon Sep 17 00:00:00 2001 From: Cam Walter Date: Sun, 11 Aug 2024 16:08:14 -0600 Subject: [PATCH 1/3] Add ES2019 Symbol.prototype.description --- .../org/mozilla/javascript/NativeSymbol.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java b/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java index 8add15dae2..a3c23949ef 100644 --- a/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java +++ b/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java @@ -23,6 +23,8 @@ public class NativeSymbol extends IdScriptableObject implements Symbol { private static final Object GLOBAL_TABLE_KEY = new Object(); private static final Object CONSTRUCTOR_SLOT = new Object(); + static final SymbolKey GETDESCRIPTION = new SymbolKey("[Symbol.getDescription]"); + private final SymbolKey key; private final NativeSymbol symbolData; @@ -30,6 +32,12 @@ public static void init(Context cx, Scriptable scope, boolean sealed) { NativeSymbol obj = new NativeSymbol(""); ScriptableObject ctor = obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, false); + ScriptableObject desc = (ScriptableObject) cx.newObject(scope); + desc.put("enumerable", desc, Boolean.FALSE); + desc.put("configurable", desc, Boolean.TRUE); + desc.put("get", desc, obj.get(GETDESCRIPTION, obj)); + obj.defineOwnProperty(cx, "description", desc); + cx.putThreadLocal(CONSTRUCTOR_SLOT, Boolean.TRUE); try { createStandardSymbol(cx, scope, ctor, "iterator", SymbolKey.ITERATOR); @@ -134,6 +142,8 @@ protected int findPrototypeId(Symbol key) { return SymbolId_toStringTag; } else if (SymbolKey.TO_PRIMITIVE.equals(key)) { return SymbolId_toPrimitive; + } else if (GETDESCRIPTION.equals(key)) { + return SymbolId_description; } return 0; } @@ -143,9 +153,10 @@ protected int findPrototypeId(Symbol key) { Id_constructor = 1, Id_toString = 2, Id_valueOf = 4, + SymbolId_description = 6, SymbolId_toStringTag = 3, SymbolId_toPrimitive = 5, - MAX_PROTOTYPE_ID = SymbolId_toPrimitive; + MAX_PROTOTYPE_ID = SymbolId_description; @Override protected void initPrototypeId(int id) { @@ -159,6 +170,9 @@ protected void initPrototypeId(int id) { case Id_valueOf: initPrototypeMethod(CLASS_NAME, id, "valueOf", 0); break; + case SymbolId_description: + initPrototypeMethod(CLASS_NAME, id, GETDESCRIPTION, "get description", 0); + break; case SymbolId_toStringTag: initPrototypeValue(id, SymbolKey.TO_STRING_TAG, CLASS_NAME, DONTENUM | READONLY); break; @@ -201,6 +215,9 @@ public Object execIdCall( case Id_valueOf: case SymbolId_toPrimitive: return getSelf(cx, scope, thisObj).js_valueOf(); + + case SymbolId_description: + return getSelf(cx, scope, thisObj).js_getDescription(); default: return super.execIdCall(f, cx, scope, thisObj, args); } @@ -271,6 +288,10 @@ private Object js_keyFor(Context cx, Scriptable scope, Object[] args) { return Undefined.instance; } + private String js_getDescription() { + return key.getName(); + } + @Override public String toString() { return key.toString(); From 50f7426602a0216e9d182462b814935ca4537431 Mon Sep 17 00:00:00 2001 From: Cam Walter Date: Sun, 11 Aug 2024 19:48:24 -0600 Subject: [PATCH 2/3] Make Symbols with undefined not be an empty string description --- .../java/org/mozilla/javascript/NativeSymbol.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java b/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java index a3c23949ef..97c53de69c 100644 --- a/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java +++ b/rhino/src/main/java/org/mozilla/javascript/NativeSymbol.java @@ -235,12 +235,12 @@ private static NativeSymbol js_constructor(Object[] args) { String desc; if (args.length > 0) { if (Undefined.instance.equals(args[0])) { - desc = ""; + desc = null; } else { desc = ScriptRuntime.toString(args[0]); } } else { - desc = ""; + desc = null; } if (args.length > 1) { @@ -288,8 +288,12 @@ private Object js_keyFor(Context cx, Scriptable scope, Object[] args) { return Undefined.instance; } - private String js_getDescription() { - return key.getName(); + private Object js_getDescription() { + String name = key.getName(); + if (name == null) { + return Undefined.instance; + } + return name; } @Override From ab8343da428b5e34014b53c2d18ea038f29bcfc4 Mon Sep 17 00:00:00 2001 From: Cam Walter Date: Sun, 11 Aug 2024 23:42:45 -0600 Subject: [PATCH 3/3] Update test262.properties --- tests/testsrc/test262.properties | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/testsrc/test262.properties b/tests/testsrc/test262.properties index 61c058fe64..ce2039f956 100644 --- a/tests/testsrc/test262.properties +++ b/tests/testsrc/test262.properties @@ -2426,10 +2426,9 @@ built-ins/String 140/1182 (11.84%) built-ins/StringIteratorPrototype 0/7 (0.0%) -built-ins/Symbol 34/92 (36.96%) +built-ins/Symbol 28/92 (30.43%) asyncIterator/prop-desc.js for/cross-realm.js - for/description.js for/not-a-constructor.js {unsupported: [Reflect.construct]} hasInstance/cross-realm.js isConcatSpreadable/cross-realm.js @@ -2439,12 +2438,7 @@ built-ins/Symbol 34/92 (36.96%) keyFor/not-a-constructor.js {unsupported: [Reflect.construct]} matchAll 2/2 (100.0%) match/cross-realm.js - prototype/description/description-symboldescriptivestring.js - prototype/description/descriptor.js - prototype/description/get.js - prototype/description/this-val-non-symbol.js - prototype/description/this-val-symbol.js - prototype/description/wrapper.js + prototype/description/this-val-non-symbol.js {unsupported: [Proxy]} prototype/Symbol.toPrimitive/name.js prototype/Symbol.toPrimitive/prop-desc.js prototype/Symbol.toPrimitive/redefined-symbol-wrapper-ordinary-toprimitive.js