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

fix static array functions and add test cases #322

Merged
merged 3 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ else if (start < 0)
private static Object iterativeMethod(Context cx, IdFunctionObject idFunctionObject, Scriptable scope,
Scriptable thisObj, Object[] args)
{
int id = idFunctionObject.methodId();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get what you're fixing here.

This case is already convoluted enough and the use of "Math.abs" is not going to be obvious to someone else using the code.

Can you add a comment to the effect that this works because at the very bottom of the file, we use negative numbers to represent the non-prototype versions of these functions and we want them all to work? Otherwise this looks good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, you are right. Was not really obvious to me also. Will update this later today.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added some inline comments, hope this makes this a bit more clear.

int id = Math.abs(idFunctionObject.methodId());

if (Id_find == id || Id_findIndex == id) thisObj = requireObjectCoercible(cx, thisObj, idFunctionObject);

Expand Down
14 changes: 14 additions & 0 deletions testsrc/doctests/array.every.doctest
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/.

js> Array.every;
function every() { [native code for Array.every, arity=1] }

js> function isSmall(n) { return n < 10; };

js> Array.every([1, 4, 9, 16], isSmall);
false

js> Array.every([1, 4, 9], isSmall);
true
11 changes: 11 additions & 0 deletions testsrc/doctests/array.filter.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

js> Array.filter;
function filter() { [native code for Array.filter, arity=1] }

js> function isSmall(n) { return n < 10; };

js> "" + Array.filter([1, 13, 4, 16, 42], isSmall);
1,4
11 changes: 11 additions & 0 deletions testsrc/doctests/array.find.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

js> Array.find;
function find() { [native code for Array.find, arity=1] }

js> function isSmall(n) { return n < 10; };

js> Array.find([13, 4, 17], isSmall);
4
11 changes: 11 additions & 0 deletions testsrc/doctests/array.findIndex.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

js> Array.findIndex;
function findIndex() { [native code for Array.findIndex, arity=1] }

js> function isSmall(n) { return n < 10; };

js> Array.findIndex([13, 4, 17], isSmall);
1
11 changes: 11 additions & 0 deletions testsrc/doctests/array.forEach.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

js> Array.forEach;
function forEach() { [native code for Array.forEach, arity=1] }

js> res = '';
js> Array.forEach([1, 13, 4], function(elem) { res += elem });
js> res
1134
9 changes: 9 additions & 0 deletions testsrc/doctests/array.map.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +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/.

js> Array.map;
function map() { [native code for Array.map, arity=1] }

js> "" + Array.map([9, 16, 25], Math.sqrt);
3,4,5
11 changes: 11 additions & 0 deletions testsrc/doctests/array.reduce.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

js> Array.reduce;
function reduce() { [native code for Array.reduce, arity=1] }

js> function sum(acc, val) { return acc + val; };

js> Array.reduce([1, 4, 9, 16], sum);
30
11 changes: 11 additions & 0 deletions testsrc/doctests/array.reduceRight.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

js> Array.reduceRight;
function reduceRight() { [native code for Array.reduceRight, arity=1] }

js> function diff(acc, val) { return acc - val; };

js> Array.reduceRight([1, 4, 9, 16], diff);
2
14 changes: 14 additions & 0 deletions testsrc/doctests/array.some.doctest
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/.

js> Array.some;
function some() { [native code for Array.some, arity=1] }

js> function isSmall(n) { return n < 10; };

js> Array.some([1, 4, 9, 16], isSmall);
true

js> Array.some([19, 42], isSmall);
false