From 2446750460f322e1d444ed185661dbfa66723e59 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Wed, 10 Jan 2024 19:23:08 +0100 Subject: [PATCH] arguments[Symbol.iterator] points to Array Array.prototype[Symbol.iterator] see https://github.com/HtmlUnit/htmlunit-rhino-fork/pull/14 --- src/org/mozilla/javascript/Arguments.java | 25 ++----- .../javascript/tests/es6/ArgumentsTest.java | 66 +++++++++++++++++++ testsrc/test262.properties | 4 +- 3 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java diff --git a/src/org/mozilla/javascript/Arguments.java b/src/org/mozilla/javascript/Arguments.java index b0300b22e6..7674b6f187 100644 --- a/src/org/mozilla/javascript/Arguments.java +++ b/src/org/mozilla/javascript/Arguments.java @@ -6,8 +6,6 @@ package org.mozilla.javascript; -import org.mozilla.javascript.NativeArrayIterator.ARRAY_ITERATOR_TYPE; - /** * This class implements the "arguments" object. * @@ -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 @@ -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; diff --git a/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java b/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java new file mode 100644 index 0000000000..d961ea53e5 --- /dev/null +++ b/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java @@ -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; + }); + } +} diff --git a/testsrc/test262.properties b/testsrc/test262.properties index 7ce3d32554..626371927d 100644 --- a/testsrc/test262.properties +++ b/testsrc/test262.properties @@ -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 @@ -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