Skip to content

Commit

Permalink
arguments[Symbol.iterator] points to Array Array.prototype[Symbol.ite…
Browse files Browse the repository at this point in the history
…rator]

  see #14
  • Loading branch information
rbri committed Jan 10, 2024
1 parent 1f771f6 commit 2446750
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 22 deletions.
25 changes: 6 additions & 19 deletions src/org/mozilla/javascript/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package org.mozilla.javascript;

import org.mozilla.javascript.NativeArrayIterator.ARRAY_ITERATOR_TYPE;

/**
* This class implements the "arguments" object.
*
Expand Down Expand Up @@ -41,7 +39,12 @@ public Arguments(NativeCall activation) {
callerObj = NOT_FOUND;
}

defineProperty(SymbolKey.ITERATOR, iteratorMethod, ScriptableObject.DONTENUM);
defineProperty(
SymbolKey.ITERATOR,
TopLevel.getBuiltinPrototype(
ScriptableObject.getTopLevelScope(parent), TopLevel.Builtins.Array)
.get("values", parent),
ScriptableObject.DONTENUM);
}

@Override
Expand Down Expand Up @@ -402,22 +405,6 @@ void defineAttributesForStrictMode() {
calleeObj = null;
}

private static BaseFunction iteratorMethod =
new BaseFunction() {
private static final long serialVersionUID = 4239122318596177391L;

@Override
public Object call(
Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
// TODO : call %ArrayProto_values%
// 9.4.4.6 CreateUnmappedArgumentsObject(argumentsList)
// 1. Perform DefinePropertyOrThrow(obj, @@iterator, PropertyDescriptor
// {[[Value]]:%ArrayProto_values%,
// [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}).
return new NativeArrayIterator(scope, thisObj, ARRAY_ITERATOR_TYPE.VALUES);
}
};

private static class ThrowTypeError extends BaseFunction {
private static final long serialVersionUID = -744615873947395749L;
private String propertyName;
Expand Down
66 changes: 66 additions & 0 deletions testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* 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 Arguments support. */
public class ArgumentsTest {

@Test
public void argumentsSymbolIterator() {
String code =
"function foo() {"
+ " return arguments[Symbol.iterator] === Array.prototype.values;"
+ "}"
+ "foo()";

test(true, code);
}

@Test
public void argumentsSymbolIterator2() {
String code =
"function foo() {"
+ " return arguments[Symbol.iterator] === [][Symbol.iterator];"
+ "}"
+ "foo()";

test(true, code);
}

@Test
public void argumentsForOf() {
String code =
"function foo() {"
+ " var res = '';"
+ " for (arg of arguments) {"
+ " res += arg;"
+ " }"
+ " return res;"
+ "}"
+ "foo(1, 2, 3, 5)";

test("1235", 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;
});
}
}
4 changes: 1 addition & 3 deletions testsrc/test262.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2446,7 +2446,7 @@ built-ins/WeakMap 1/88 (1.14%)
built-ins/WeakSet 1/75 (1.33%)
proto-from-ctor-realm.js {unsupported: [Reflect]}

language/arguments-object 191/260 (73.46%)
language/arguments-object 189/260 (72.69%)
mapped/mapped-arguments-nonconfigurable-3.js non-strict
mapped/mapped-arguments-nonconfigurable-delete-1.js non-strict
mapped/mapped-arguments-nonconfigurable-delete-2.js non-strict
Expand Down Expand Up @@ -2482,8 +2482,6 @@ language/arguments-object 191/260 (73.46%)
mapped/nonwritable-nonenumerable-nonconfigurable-descriptors-basic.js non-strict
mapped/nonwritable-nonenumerable-nonconfigurable-descriptors-set-by-arguments.js non-strict
mapped/nonwritable-nonenumerable-nonconfigurable-descriptors-set-by-param.js non-strict
mapped/Symbol.iterator.js non-strict
unmapped/Symbol.iterator.js non-strict
unmapped/via-params-dflt.js
unmapped/via-params-dstr.js non-strict
unmapped/via-params-rest.js
Expand Down

0 comments on commit 2446750

Please sign in to comment.