From 7d01bfb0bb457701e8e4f0eb0c7c701e7b25ff07 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Thu, 20 Jul 2023 18:09:53 +0200 Subject: [PATCH 1/3] typeof fix --- src/org/mozilla/javascript/ScriptRuntime.java | 1 + .../javascript/tests/es6/Symbol3Test.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java diff --git a/src/org/mozilla/javascript/ScriptRuntime.java b/src/org/mozilla/javascript/ScriptRuntime.java index 0a5dfea781..0200824943 100644 --- a/src/org/mozilla/javascript/ScriptRuntime.java +++ b/src/org/mozilla/javascript/ScriptRuntime.java @@ -2864,6 +2864,7 @@ public static String typeof(Object value) { if (value instanceof BigInteger) return "bigint"; if (value instanceof Number) return "number"; if (value instanceof Boolean) return "boolean"; + if (isSymbol(value)) return "symbol"; throw errorWithClassName("msg.invalid.type", value); } diff --git a/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java b/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java new file mode 100644 index 0000000000..19b4f168fc --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java @@ -0,0 +1,53 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.javascript.tests.es6; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.tests.Utils; + +/** Tests for Symbol support. */ +public class Symbol3Test { + + @Test + public void scriptRuntimeTypeofSymbolKey() { + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + String code = + "function foo() {" + + " var sym = Object.getOwnPropertySymbols(arguments);" + + " return '' + sym.length + ' ' + typeof sym[0];" + + "}" + + "foo()"; + String result = (String) cx.evaluateString(scope, code, "test", 1, null); + assertEquals("1 symbol", result); + + return null; + }); + } + + @Test + public void scriptRuntimeTypeofSymbol() { + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + String result = + (String) + cx.evaluateString( + scope, "typeof Symbol.toStringTag", "test", 1, null); + assertEquals("symbol", result); + + return null; + }); + } +} From 49c29c30fd99eab46305efa66504e51b950e21ef Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Thu, 20 Jul 2023 20:02:04 +0200 Subject: [PATCH 2/3] take care of SymbolKey --- src/org/mozilla/javascript/NativeSymbol.java | 2 +- src/org/mozilla/javascript/ScriptRuntime.java | 6 +++ .../javascript/tests/es6/Symbol3Test.java | 43 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/org/mozilla/javascript/NativeSymbol.java b/src/org/mozilla/javascript/NativeSymbol.java index eb14e807fe..6157398a79 100644 --- a/src/org/mozilla/javascript/NativeSymbol.java +++ b/src/org/mozilla/javascript/NativeSymbol.java @@ -67,7 +67,7 @@ private NativeSymbol(String desc) { this.symbolData = null; } - private NativeSymbol(SymbolKey key) { + NativeSymbol(SymbolKey key) { this.key = key; this.symbolData = this; } diff --git a/src/org/mozilla/javascript/ScriptRuntime.java b/src/org/mozilla/javascript/ScriptRuntime.java index 0200824943..825eb2537c 100644 --- a/src/org/mozilla/javascript/ScriptRuntime.java +++ b/src/org/mozilla/javascript/ScriptRuntime.java @@ -1225,6 +1225,12 @@ public static Scriptable toObject(Context cx, Scriptable scope, Object val) { } if (isSymbol(val)) { + if (val instanceof SymbolKey) { + NativeSymbol result = new NativeSymbol((SymbolKey) val); + setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Symbol); + return result; + } + NativeSymbol result = new NativeSymbol((NativeSymbol) val); setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Symbol); return result; diff --git a/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java b/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java index 19b4f168fc..32a1ede83c 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java +++ b/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java @@ -9,6 +9,7 @@ import org.junit.Test; import org.mozilla.javascript.Context; import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.SymbolKey; import org.mozilla.javascript.tests.Utils; /** Tests for Symbol support. */ @@ -50,4 +51,46 @@ public void scriptRuntimeTypeofSymbol() { return null; }); } + + @Test + public void symbolProperty() throws Exception { + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + final String script = + "var sym = Object.getOwnPropertySymbols(MyHostObject);" + + "var str = sym[0].toString();" + + "var result = sym[0].toString();" + + "result += ' ' + sym.length + ' ' + typeof sym[0];" + + "result += ' ' + MyHostObject[sym[0]]"; + + try { + final ScriptableObject jsObj = new MyHostObject(); + jsObj.setParentScope(scope); + + jsObj.defineProperty( + SymbolKey.TO_STRING_TAG, "foo", ScriptableObject.DONTENUM); + + scope.put("MyHostObject", scope, jsObj); + } catch (Exception e) { + } + + final String result = + (String) cx.evaluateString(scope, script, "myScript", 1, null); + + assertEquals("Symbol(Symbol.toStringTag) 1 symbol foo", result); + + return null; + }); + } + + public static class MyHostObject extends ScriptableObject { + + @Override + public String getClassName() { + return "MyHostObject"; + } + } } From 9073b742770fb683e5dda6520c14beb123ba3aa9 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Thu, 20 Jul 2023 20:06:10 +0200 Subject: [PATCH 3/3] more tests passing --- testsrc/jstests/es6/symbols.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/testsrc/jstests/es6/symbols.js b/testsrc/jstests/es6/symbols.js index ef9f51ef6b..534fbe22fd 100644 --- a/testsrc/jstests/es6/symbols.js +++ b/testsrc/jstests/es6/symbols.js @@ -242,9 +242,8 @@ function TestCall() { assertTrue(symbols[i].getThisProto() === Symbol.prototype) } } -// Rhino not sure what's up! TestCall() +TestCall() -/* Rhino no collections function TestCollections() { var set = new Set @@ -269,7 +268,6 @@ function TestCollections() { } TestCollections() -*/ function TestKeySet(obj) { //assertTrue(%HasFastProperties(obj)) @@ -356,7 +354,6 @@ function TestKeyDelete(obj) { } -/* Rhino no Map var objs = [{}, [], Object.create({}), Object(1), new Map, function(){}] for (var i in objs) { @@ -371,7 +368,7 @@ for (var i in objs) { TestKeyDescriptor(obj) TestKeyDelete(obj) } -*/ + function TestDefineProperties() { var properties = {} @@ -567,7 +564,7 @@ function TestStringify(expected, input) { JSON.stringify(input, (key, value) => value, "=")); } -// Rhino TestStringify(undefined, Symbol("a")); +TestStringify(undefined, Symbol("a")); TestStringify('[{}]', [Object(Symbol())]); var symbol_wrapper = Object(Symbol("a")) TestStringify('{}', symbol_wrapper);