Skip to content

Commit

Permalink
Array.prototype[Symbol.iterator] points to the 'values' functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jan 10, 2024
1 parent ef18cb9 commit 1f771f6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 12 deletions.
50 changes: 41 additions & 9 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ protected void fillConstructorProperties(IdFunctionObject ctor) {

@Override
protected void initPrototypeId(int id) {
if (id == SymbolId_iterator) {
initPrototypeMethod(ARRAY_TAG, id, SymbolKey.ITERATOR, "[Symbol.iterator]", 0);
return;
}

String s, fnName = null;
int arity;
switch (id) {
Expand Down Expand Up @@ -471,7 +466,6 @@ public Object execIdCall(
scope, thisObj, NativeArrayIterator.ARRAY_ITERATOR_TYPE.ENTRIES);

case Id_values:
case SymbolId_iterator:
thisObj = ScriptRuntime.toObject(cx, scope, thisObj);
return new NativeArrayIterator(
scope, thisObj, NativeArrayIterator.ARRAY_ITERATOR_TYPE.VALUES);
Expand All @@ -495,6 +489,42 @@ public boolean has(int index, Scriptable start) {
return super.has(index, start);
}

@Override
public boolean has(Symbol key, Scriptable start) {
if (SymbolKey.ITERATOR.equals(key)) {
return super.has("values", start);
}

return super.has(key, start);
}

@Override
public Object get(Symbol key, Scriptable start) {
if (SymbolKey.ITERATOR.equals(key)) {
return super.get("values", start);
}

return super.get(key, start);
}

@Override
public void put(Symbol key, Scriptable start, Object value) {
if (SymbolKey.ITERATOR.equals(key)) {
super.put("values", start, value);
}

super.put(key, start, value);
}

@Override
public void delete(Symbol key) {
if (SymbolKey.ITERATOR.equals(key)) {
super.delete("values");
}

super.delete(key);
}

private static long toArrayIndex(Object id) {
if (id instanceof String) {
return toArrayIndex((String) id);
Expand Down Expand Up @@ -2519,7 +2549,10 @@ private void checkModCount(int modCount) {
@Override
protected int findPrototypeId(Symbol k) {
if (SymbolKey.ITERATOR.equals(k)) {
return SymbolId_iterator;
// "Symbol.iterator" property of the prototype has the "same value"
// as the "values" property. We implement this by returning the
// ID of "values" when the iterator symbol is accessed.
return Id_values;
}
return 0;
}
Expand Down Expand Up @@ -2729,8 +2762,7 @@ protected int findPrototypeId(String s) {
Id_at = 32,
Id_flat = 33,
Id_flatMap = 34,
SymbolId_iterator = 35,
MAX_PROTOTYPE_ID = SymbolId_iterator;
MAX_PROTOTYPE_ID = Id_flatMap;
private static final int ConstructorId_join = -Id_join,
ConstructorId_reverse = -Id_reverse,
ConstructorId_sort = -Id_sort,
Expand Down
2 changes: 1 addition & 1 deletion testsrc/jstests/harmony/for-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ assertThrows('for each (n of [1,2]) {}', SyntaxError);

assertThrows('[n*n for each (n of [1,2])]', SyntaxError);

assertEquals('[Symbol.iterator]', Array.prototype[Symbol.iterator].name);
assertEquals('values', Array.prototype[Symbol.iterator].name);
assertEquals('[Symbol.iterator]', String.prototype[Symbol.iterator].name);

// should have `value` and `done` property.
Expand Down
74 changes: 74 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/NativeArray3Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* 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 NativeArray support. */
public class NativeArray3Test {

@Test
public void iteratorPrototype() {
String code = "Array.prototype.values === [][Symbol.iterator]";

test(true, code);
}

@Test
public void iteratorInstances() {
String code = "[1, 2][Symbol.iterator] === [][Symbol.iterator]";

test(true, code);
}

@Test
public void iteratorPrototypeName() {
String code = "Array.prototype.values.name;";

test("values", code);
}

@Test
public void iteratorInstanceName() {
String code = "[][Symbol.iterator].name;";

test("values", code);
}

@Test
public void redefineIterator() {
String code =
"var res = '';\n"
+ "var arr = ['hello', 'world'];\n"
+ "res += arr[Symbol.iterator].toString().includes('return i;');\n"
+ "res += ' - ';\n"
+ "arr[Symbol.iterator] = function () { return i; };\n"
+ "res += arr[Symbol.iterator].toString().includes('return i;');\n"
+ "res += ' - ';\n"
+ "delete arr[Symbol.iterator];\n"
+ "res += arr[Symbol.iterator].toString().includes('return i;');\n"
+ "res;";

test("false - true - false", code);
}

private static void test(Object expected, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

Object result = cx.evaluateString(scope, js, "test", 1, null);
assertEquals(expected, result);

return null;
});
}
}
3 changes: 1 addition & 2 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is a configuration file for Test262SuiteTest.java. See ./README.md for more info about this file

built-ins/Array 177/2670 (6.63%)
built-ins/Array 176/2670 (6.59%)
from/calling-from-valid-1-noStrict.js non-strict Spec pretty clearly says this should be undefined
from/elements-deleted-after.js Checking to see if length changed, but spec says it should not
from/iter-map-fn-this-non-strict.js non-strict Error propagation needs work in general
Expand Down Expand Up @@ -169,7 +169,6 @@ built-ins/Array 177/2670 (6.63%)
prototype/toLocaleString/primitive_this_value_getter.js strict
prototype/unshift/throws-with-string-receiver.js
prototype/methods-called-as-functions.js {unsupported: [Symbol.species]}
prototype/Symbol.iterator.js Expects a particular string value
Symbol.species 4/4 (100.0%)
proto-from-ctor-realm-one.js {unsupported: [Reflect]}
proto-from-ctor-realm-two.js {unsupported: [Reflect]}
Expand Down

0 comments on commit 1f771f6

Please sign in to comment.