Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbol fixes #1357

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
}
}
Loading