Skip to content

Commit

Permalink
some code cleanup for array at support (mozilla#1331)
Browse files Browse the repository at this point in the history
* some code cleanup

* more cleanup
  • Loading branch information
rbri authored May 31, 2023
1 parent 4f531c8 commit a63f215
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 31 deletions.
9 changes: 5 additions & 4 deletions src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ protected void fillConstructorProperties(IdFunctionObject ctor) {
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_isArray, "isArray", 1);
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_of, "of", 0);
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_from, "from", 1);
addIdFunctionProperty(ctor, ARRAY_TAG, ConstructorId_at, "at", 1);
super.fillConstructorProperties(ctor);
}

Expand Down Expand Up @@ -1984,9 +1983,12 @@ private static Object js_copyWithin(

private static Object js_at(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Scriptable o = ScriptRuntime.toObject(cx, scope, thisObj);
Object targetArg = (args.length >= 1) ? args[0] : Undefined.instance;
long relativeIndex = (long) ScriptRuntime.toInteger(targetArg);
long len = getLengthProperty(cx, o);

long relativeIndex = 0;
if (args.length >= 1) {
relativeIndex = (long) ScriptRuntime.toInteger(args[0]);
}
long k = (relativeIndex >= 0) ? relativeIndex : len + relativeIndex;
if ((k < 0) || (k >= len)) {
return Undefined.instance;
Expand Down Expand Up @@ -2680,7 +2682,6 @@ protected int findPrototypeId(String s) {
ConstructorId_findIndex = -Id_findIndex,
ConstructorId_reduce = -Id_reduce,
ConstructorId_reduceRight = -Id_reduceRight,
ConstructorId_at = -Id_at,
ConstructorId_isArray = -26,
ConstructorId_of = -27,
ConstructorId_from = -28;
Expand Down
2 changes: 0 additions & 2 deletions src/org/mozilla/javascript/NativeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ protected void fillConstructorProperties(IdFunctionObject ctor) {
addIdFunctionProperty(ctor, STRING_TAG, ConstructorId_match, "match", 2);
addIdFunctionProperty(ctor, STRING_TAG, ConstructorId_search, "search", 2);
addIdFunctionProperty(ctor, STRING_TAG, ConstructorId_replace, "replace", 2);
addIdFunctionProperty(ctor, STRING_TAG, ConstructorId_at, "at", 1);
addIdFunctionProperty(ctor, STRING_TAG, ConstructorId_localeCompare, "localeCompare", 2);
addIdFunctionProperty(
ctor, STRING_TAG, ConstructorId_toLocaleLowerCase, "toLocaleLowerCase", 1);
Expand Down Expand Up @@ -1415,7 +1414,6 @@ protected int findPrototypeId(String s) {
ConstructorId_match = -Id_match,
ConstructorId_search = -Id_search,
ConstructorId_replace = -Id_replace,
ConstructorId_at = -Id_at,
ConstructorId_localeCompare = -Id_localeCompare,
ConstructorId_toLocaleLowerCase = -Id_toLocaleLowerCase;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,12 @@ private Object js_subarray(Context cx, Scriptable scope, int s, int e) {
}

private Object js_at(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object targetArg = (args.length >= 1) ? args[0] : Undefined.instance;
int relativeIndex = (int) ScriptRuntime.toInteger(targetArg);
int k = (relativeIndex >= 0) ? relativeIndex : length + relativeIndex;
long relativeIndex = 0;
if (args.length >= 1) {
relativeIndex = (long) ScriptRuntime.toInteger(args[0]);
}

long k = (relativeIndex >= 0) ? relativeIndex : length + relativeIndex;

if ((k < 0) || (k >= length)) {
return Undefined.instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// 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/.

load("testsrc/assert.js");

assertEquals(1,Array.prototype.at.length)

const alphaArray = ["b","c","d"];
Expand All @@ -13,6 +18,9 @@ assertEquals(0, intArray.at(0))
assertEquals(-50, intArray.at(-1))
assertEquals(-10, intArray.at(2))

//it treats missing arg like 0
assertEquals(0,intArray.at());

//it treats certain non numerics like 0
assertEquals(0,intArray.at("a"));
assertEquals(0,intArray.at({}));
Expand Down Expand Up @@ -62,8 +70,7 @@ assertEquals(undefined,Array.prototype.at.call(123456789,-1));
assertEquals(undefined,Array.prototype.at.call({'foo':'bar'},0));

//it throws when called on null or undefined
assertThrows(function() { Array.prototype.at.call(null);
}, TypeError);
assertThrows(function() { Array.prototype.at.call(undefined);
}, TypeError);
assertThrows(function() { Array.prototype.at.call(null); }, TypeError);
assertThrows(function() { Array.prototype.at.call(undefined); }, TypeError);

"success"
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// 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/.

load("testsrc/assert.js");

const s = "foobar";
Expand All @@ -7,6 +11,9 @@ assertEquals("f", s.at(0));
assertEquals("r", s.at(-1));
assertEquals("r", s.at(5));

//it treats missing arg like 0
assertEquals("f", s.at());

//it treats certain non numeric arguments like zero
assertEquals("f", s.at(undefined));
assertEquals("f", s.at(null));
Expand All @@ -31,13 +38,12 @@ assertThrows(function() { s.at(Symbol());
}, TypeError);

//it throws when called on null or undefined
assertThrows(function() { String.prototype.at.call(null);
}, TypeError);
assertThrows(function() { String.prototype.at.call(undefined);
}, TypeError);
assertThrows(function() { String.prototype.at.call(null); }, TypeError);
assertThrows(function() { String.prototype.at.call(undefined); }, TypeError);

//it performs as in V8 when called on other non-strings
assertEquals(',',String.prototype.at.call(['a','b'],1));
assertEquals('[',String.prototype.at.call({'foo':'bar'}, 0));
assertEquals('2', String.prototype.at.call(12,1));

"success"
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// 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/.

load("testsrc/assert.js");

const int8 = new Int8Array([0, 10, -10, 20, -30, 40, -50]);
Expand Down Expand Up @@ -30,6 +34,9 @@ assertEquals(0,sparsefloat32.at(0));
assertEquals(6,sparsefloat32.at(-1));


//it treats missing arg like 0
assertEquals(0,int8.at());

//it treats certain non numeric arguments like zero
assertEquals(0,int8.at("a"));
assertEquals(0,int8.at({}));
Expand All @@ -53,20 +60,18 @@ assertEquals(NaN,sparsefloat32.at(-2));
//it returns undefined for out of range arguments
assertEquals(undefined, int8.at(Infinity))
assertEquals(undefined, int8.at(11))
assertEquals(undefined, int8.at(-11))

//it acts as normal with multiple arguments
assertEquals(float32[1],float32.at(1,2,3))

//it throws when called on null or undefined
assertThrows(function() { Int16Array.prototype.at.call(null);
}, TypeError);
assertThrows(function() { Int16Array.prototype.at.call(undefined);
}, TypeError);
assertThrows(function() { Int16Array.prototype.at.call(null); }, TypeError);
assertThrows(function() { Int16Array.prototype.at.call(undefined); }, TypeError);

//it throws when called on other non-TypedArrays
assertThrows(function() { Int16Array.prototype.at.call([1,2],1);
}, TypeError);
assertThrows(function() { Int16Array.prototype.at.call({'foo':'bar'},1);
}, TypeError);
assertThrows(function() { Int16Array.prototype.at.call([1,2],1); }, TypeError);
assertThrows(function() { Int16Array.prototype.at.call({'foo':'bar'},1); }, TypeError);
assertThrows(function() { Int16Array.prototype.at.call(12,1)});

"success"
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* 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.harmony;
package org.mozilla.javascript.tests.es2020;

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/harmony/array-at.js")
@RhinoTest("testsrc/jstests/es2020/array-at.js")
@LanguageVersion(Context.VERSION_ES6)
public class ArrayAtTest extends ScriptTestsBase {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* 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.harmony;
package org.mozilla.javascript.tests.es2020;

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/harmony/string-at.js")
@RhinoTest("testsrc/jstests/es2020/string-at.js")
@LanguageVersion(Context.VERSION_ES6)
public class StringAtTest extends ScriptTestsBase {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* 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.harmony;
package org.mozilla.javascript.tests.es2020;

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/harmony/typed-array-at.js")
@RhinoTest("testsrc/jstests/es2020/typed-array-at.js")
@LanguageVersion(Context.VERSION_ES6)
public class TypedArrayAtTest extends ScriptTestsBase {}

0 comments on commit a63f215

Please sign in to comment.