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

(Partial) implementation of [Symbol.species] #1448

Merged
merged 6 commits into from
Mar 4, 2024
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
5 changes: 3 additions & 2 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public class NativeArray extends IdScriptableObject implements List {
private static final Object ARRAY_TAG = "Array";
private static final Long NEGATIVE_ONE = Long.valueOf(-1);

static void init(Scriptable scope, boolean sealed) {
static void init(Context cx, Scriptable scope, boolean sealed) {
NativeArray obj = new NativeArray(0);
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
IdFunctionObject constructor = obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed);
ScriptRuntimeES6.addSymbolSpecies(cx, scope, constructor);
}

static int getMaximumInitialCapacity() {
Expand Down
4 changes: 3 additions & 1 deletion src/org/mozilla/javascript/NativeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ public class NativeMap extends IdScriptableObject {

static void init(Context cx, Scriptable scope, boolean sealed) {
NativeMap obj = new NativeMap();
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, false);
IdFunctionObject constructor = 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(NativeSet.GETSIZE, obj));
obj.defineOwnProperty(cx, "size", desc);

ScriptRuntimeES6.addSymbolSpecies(cx, scope, constructor);

if (sealed) {
obj.sealObject();
}
Expand Down
14 changes: 1 addition & 13 deletions src/org/mozilla/javascript/NativePromise.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,7 @@ public static void init(Context cx, Scriptable scope, boolean sealed) {
constructor.defineConstructorMethod(
scope, "race", 1, NativePromise::race, DONTENUM, DONTENUM | READONLY);

ScriptableObject speciesDescriptor = (ScriptableObject) cx.newObject(scope);
ScriptableObject.putProperty(speciesDescriptor, "enumerable", false);
ScriptableObject.putProperty(speciesDescriptor, "configurable", true);
ScriptableObject.putProperty(
speciesDescriptor,
"get",
new LambdaFunction(
scope,
"get [Symbol.species]",
0,
(Context lcx, Scriptable lscope, Scriptable thisObj, Object[] args) ->
constructor));
constructor.defineOwnProperty(cx, SymbolKey.SPECIES, speciesDescriptor, false);
ScriptRuntimeES6.addSymbolSpecies(cx, scope, constructor);

constructor.definePrototypeMethod(
scope,
Expand Down
4 changes: 3 additions & 1 deletion src/org/mozilla/javascript/NativeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ public class NativeSet extends IdScriptableObject {

static void init(Context cx, Scriptable scope, boolean sealed) {
NativeSet obj = new NativeSet();
obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, false);
IdFunctionObject constructor = 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(GETSIZE, obj));
obj.defineOwnProperty(cx, "size", desc);

ScriptRuntimeES6.addSymbolSpecies(cx, scope, constructor);

if (sealed) {
obj.sealObject();
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/ScriptRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static ScriptableObject initSafeStandardObjects(
NativeError.init(scope, sealed);
NativeGlobal.init(cx, scope, sealed);

NativeArray.init(scope, sealed);
NativeArray.init(cx, scope, sealed);
if (cx.getOptimizationLevel() > 0) {
// When optimizing, attempt to fulfill all requests for new Array(N)
// with a higher threshold before switching to a sparse
Expand Down
18 changes: 18 additions & 0 deletions src/org/mozilla/javascript/ScriptRuntimeES6.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@ public static Object requireObjectCoercible(
}
return val;
}

/** Registers the symbol <code>[Symbol.species]</code> on the given constructor function. */
public static void addSymbolSpecies(
Context cx, Scriptable scope, IdScriptableObject constructor) {
ScriptableObject speciesDescriptor = (ScriptableObject) cx.newObject(scope);
ScriptableObject.putProperty(speciesDescriptor, "enumerable", false);
ScriptableObject.putProperty(speciesDescriptor, "configurable", true);
ScriptableObject.putProperty(
speciesDescriptor,
"get",
new LambdaFunction(
scope,
"get [Symbol.species]",
0,
(Context lcx, Scriptable lscope, Scriptable thisObj, Object[] args) ->
thisObj));
constructor.defineOwnProperty(cx, SymbolKey.SPECIES, speciesDescriptor, false);
}
}
3 changes: 3 additions & 0 deletions src/org/mozilla/javascript/regexp/NativeRegExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.ScriptRuntime;
import org.mozilla.javascript.ScriptRuntimeES6;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Symbol;
Expand Down Expand Up @@ -134,6 +135,8 @@ public static void init(Context cx, Scriptable scope, boolean sealed) {
}

defineProperty(scope, "RegExp", ctor, ScriptableObject.DONTENUM);

ScriptRuntimeES6.addSymbolSpecies(cx, scope, ctor);
}

NativeRegExp(Scriptable scope, RECompiled regexpCompiled) {
Expand Down
5 changes: 5 additions & 0 deletions testsrc/jstests/es6/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ var a = Array.from(Array(2));
assertTrue(0 in a);
assertEquals(a.map(_ => 'a'), ['a', 'a']);

(function TestSymbolSpecies() {
var symbolSpeciesValue = Array[Symbol.species];
assertEquals(Array, symbolSpeciesValue);
})();

"success";
5 changes: 5 additions & 0 deletions testsrc/jstests/es6/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,9 @@ function logElement(value, key, m) {
}
})();

(function TestSymbolSpecies() {
var symbolSpeciesValue = Map[Symbol.species];
assertEquals(Map, symbolSpeciesValue);
})();

"success";
7 changes: 6 additions & 1 deletion testsrc/jstests/es6/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,11 @@ function assertAsyncDone(iteration) {
assertEquals(1, callCount);
})();

assertAsyncDone()
assertAsyncDone();

(function TestSymbolSpecies() {
var symbolSpeciesValue = Promise[Symbol.species];
assertEquals(Promise, symbolSpeciesValue);
})();

'success';
8 changes: 8 additions & 0 deletions testsrc/jstests/es6/regexp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load('testsrc/assert.js');

(function TestSymbolSpecies() {
var symbolSpeciesValue = RegExp[Symbol.species];
assertEquals(RegExp, symbolSpeciesValue);
})();

'success';
5 changes: 5 additions & 0 deletions testsrc/jstests/es6/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ function logElement(value, key) {
assertEquals(1, mySet.size);
})();

(function TestSymbolSpecies() {
var symbolSpeciesValue = Set[Symbol.species];
assertEquals(Set, symbolSpeciesValue);
})();

"success";
1 change: 0 additions & 1 deletion testsrc/org/mozilla/javascript/tests/Test262SuiteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public class Test262SuiteTest {
"Reflect.set",
"Reflect.setPrototypeOf",
"SharedArrayBuffer",
"Symbol.species",
"async-functions",
"async-iteration",
"class",
Expand Down
14 changes: 14 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/RegExpBasicTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* 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 org.mozilla.javascript.Context;
import org.mozilla.javascript.drivers.LanguageVersion;
import org.mozilla.javascript.drivers.RhinoTest;
import org.mozilla.javascript.drivers.ScriptTestsBase;

@RhinoTest("testsrc/jstests/es6/regexp.js")
@LanguageVersion(Context.VERSION_ES6)
public class RegExpBasicTest extends ScriptTestsBase {}
Loading
Loading