summaryrefslogtreecommitdiff
path: root/js/src/builtin
diff options
context:
space:
mode:
authorJob Bautista <jobbautista9@protonmail.com>2023-01-12 21:42:27 +0800
committerJob Bautista <jobbautista9@aol.com>2023-05-17 17:21:08 +0800
commit61d8ec547899cc75fb5bbf017168d1cd80d49959 (patch)
tree995ad2feba082266c0d3cdc4c85d47b5fe9a1de5 /js/src/builtin
parent3f09edfde013e3df1c7eec6c4195bf425b9b7e04 (diff)
downloaduxp-61d8ec547899cc75fb5bbf017168d1cd80d49959.tar.gz
Issue #2246 - Implement Array find from last.
Based on the implementation from Mozilla bug 1704385.
Diffstat (limited to 'js/src/builtin')
-rw-r--r--js/src/builtin/Array.js66
-rw-r--r--js/src/builtin/TypedArray.js85
2 files changed, 151 insertions, 0 deletions
diff --git a/js/src/builtin/Array.js b/js/src/builtin/Array.js
index 1e7776e228..f97e1df571 100644
--- a/js/src/builtin/Array.js
+++ b/js/src/builtin/Array.js
@@ -1210,6 +1210,72 @@ function ArrayAt(index) {
return O[k];
}
+// https://github.com/tc39/proposal-array-find-from-last
+// Array.prototype.findLast ( predicate, thisArg )
+function ArrayFindLast(predicate/*, thisArg*/) {
+ /* Steps 1. */
+ var O = ToObject(this);
+
+ /* Steps 2. */
+ var len = ToLength(O.length);
+
+ /* Step 3. */
+ if (arguments.length === 0) {
+ ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.findLast");
+ }
+ if (!IsCallable(predicate)) {
+ ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
+ }
+
+ var T = arguments.length > 1 ? arguments[1] : undefined;
+
+ /* Step 4-5. */
+ for (var k = len - 1; k >= 0; k--) {
+ /* Steps 5.a-b. */
+ var kValue = O[k];
+ /* Steps 5.c-d. */
+ if (callContentFunction(predicate, T, kValue, k, O)) {
+ return kValue;
+ }
+ }
+
+ /* Step 6. */
+ return undefined;
+}
+
+// https://github.com/tc39/proposal-array-find-from-last
+// Array.prototype.findLastIndex ( predicate, thisArg )
+function ArrayFindLastIndex(predicate/*, thisArg*/) {
+ /* Steps 1. */
+ var O = ToObject(this);
+
+ /* Steps 2. */
+ var len = ToLength(O.length);
+
+ /* Step 3. */
+ if (arguments.length === 0) {
+ ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "Array.prototype.findLastIndex");
+ }
+ if (!IsCallable(predicate)) {
+ ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
+ }
+
+ var T = arguments.length > 1 ? arguments[1] : undefined;
+
+ /* Step 4-5. */
+ for (var k = len - 1; k >= 0; k--) {
+ /* Steps 5.a-b. */
+ var kValue = O[k];
+ /* Steps 5.c-d. */
+ if (callContentFunction(predicate, T, kValue, k, O)) {
+ return k;
+ }
+ }
+
+ /* Step 6. */
+ return -1;
+}
+
function ArrayStaticConcat(arr, arg1) {
if (arguments.length < 1)
ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, 'Array.concat');
diff --git a/js/src/builtin/TypedArray.js b/js/src/builtin/TypedArray.js
index 5b7a1dfc22..22023aa7ca 100644
--- a/js/src/builtin/TypedArray.js
+++ b/js/src/builtin/TypedArray.js
@@ -1347,6 +1347,91 @@ function TypedArrayAt(index) {
return obj[k];
}
+// https://github.com/tc39/proposal-array-find-from-last
+// %TypedArray%.prototype.findLast ( predicate, thisArg )
+function TypedArrayFindLast(predicate/*, thisArg*/) {
+ // Step 1.
+ var O = this;
+
+ // Step 2.
+ var isTypedArray = IsTypedArrayEnsuringArrayBuffer(O);
+
+ // If we got here, `this` is either a typed array or a wrapper for one.
+
+ // Step 3.
+ var len;
+ if (isTypedArray) {
+ len = TypedArrayLength(O);
+ } else {
+ len = callFunction(CallTypedArrayMethodIfWrapped, O, "TypedArrayLengthMethod");
+ }
+
+ // Step 4.
+ if (arguments.length === 0) {
+ ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.findLast");
+ }
+ if (!IsCallable(predicate)) {
+ ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
+ }
+
+ var thisArg = arguments.length > 1 ? arguments[1] : void 0;
+
+ // Steps 5-6.
+ for (var k = len - 1; k >= 0; k--) {
+ // Steps 6.a-b.
+ var kValue = O[k];
+
+ // Steps 6.c-d.
+ if (callContentFunction(predicate, thisArg, kValue, k, O)) {
+ return kValue;
+ }
+ }
+
+ // Step 7.
+ return undefined;
+}
+
+// https://github.com/tc39/proposal-array-find-from-last
+// %TypedArray%.prototype.findLastIndex ( predicate, thisArg )
+function TypedArrayFindLastIndex(predicate/*, thisArg*/) {
+ // Step 1.
+ var O = this;
+
+ // Step 2.
+ var isTypedArray = IsTypedArrayEnsuringArrayBuffer(O);
+
+ // If we got here, `this` is either a typed array or a wrapper for one.
+
+ // Step 3.
+ var len;
+ if (isTypedArray) {
+ len = TypedArrayLength(O);
+ } else {
+ len = callFunction(CallTypedArrayMethodIfWrapped, O, "TypedArrayLengthMethod");
+ }
+
+ // Step 4.
+ if (arguments.length === 0) {
+ ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, "%TypedArray%.prototype.findLastIndex");
+ }
+ if (!IsCallable(predicate)) {
+ ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(0, predicate));
+ }
+
+ var thisArg = arguments.length > 1 ? arguments[1] : void 0;
+
+ // Steps 5-6.
+ for (var k = len - 1; k >= 0; k--) {
+ // Steps 6.a-f.
+ if (callContentFunction(predicate, thisArg, O[k], k, O)) {
+ return k;
+ }
+ }
+
+ // Step 7.
+ return -1;
+}
+
// ES6 draft rev30 (2014/12/24) 22.2.3.30 %TypedArray%.prototype.values()
function TypedArrayValues() {
// Step 1.