Skip to content

Commit

Permalink
Symbol fixes (#1357)
Browse files Browse the repository at this point in the history
Fix Symbol support so that typeof properly returns "symbol"

* fix an problem with typeof when used for symbols
* take care of SymbolKey before casting - this fixes some problems when adding new symbols to java classes mapped into the js engine
  • Loading branch information
rbri authored Aug 2, 2023
1 parent 76fc05e commit ff29b02
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeSymbol.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private NativeSymbol(String desc) {
this.symbolData = null;
}

private NativeSymbol(SymbolKey key) {
NativeSymbol(SymbolKey key) {
this.key = key;
this.symbolData = this;
}
Expand Down
7 changes: 7 additions & 0 deletions src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2864,6 +2870,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);
}

Expand Down
9 changes: 3 additions & 6 deletions testsrc/jstests/es6/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -269,7 +268,6 @@ function TestCollections() {
}
TestCollections()

*/

function TestKeySet(obj) {
//assertTrue(%HasFastProperties(obj))
Expand Down Expand Up @@ -356,7 +354,6 @@ function TestKeyDelete(obj) {
}


/* Rhino no Map
var objs = [{}, [], Object.create({}), Object(1), new Map, function(){}]

for (var i in objs) {
Expand All @@ -371,7 +368,7 @@ for (var i in objs) {
TestKeyDescriptor(obj)
TestKeyDelete(obj)
}
*/


function TestDefineProperties() {
var properties = {}
Expand Down Expand Up @@ -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);
Expand Down
96 changes: 96 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* 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.SymbolKey;
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;
});
}

@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";
}
}
}

0 comments on commit ff29b02

Please sign in to comment.