From 7e5cbc6cbcbfe025615fe3985ab3f2edf38d1bda Mon Sep 17 00:00:00 2001 From: liujia178 Date: Tue, 23 Apr 2024 15:34:44 +0800 Subject: [PATCH] Typedarray findIndex & findLastIndex Issue: id (issueID #I9IYTE) Signed-off-by: liujia178 --- ecmascript/builtins/builtins_array.cpp | 4 +- ecmascript/builtins/builtins_typedarray.cpp | 64 +++++++++++++++++++- test/regresstest/regresstest_skip_tests.json | 2 - 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/ecmascript/builtins/builtins_array.cpp b/ecmascript/builtins/builtins_array.cpp index 83f12a88b..00f499b15 100644 --- a/ecmascript/builtins/builtins_array.cpp +++ b/ecmascript/builtins/builtins_array.cpp @@ -1032,7 +1032,7 @@ JSTaggedValue BuiltinsArray::FindIndex(EcmaRuntimeCallInfo *argv) JSHandle thisObjVal(thisObjHandle); // 3. Let len be ToLength(Get(O, "length")). - uint64_t len = static_cast(ArrayHelper::GetLength(thread, thisObjVal)); + uint64_t len = static_cast(ArrayHelper::GetArrayLength(thread, thisObjVal)); // 4. ReturnIfAbrupt(len). RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); @@ -3319,7 +3319,7 @@ JSTaggedValue BuiltinsArray::FindLastIndex(EcmaRuntimeCallInfo *argv) JSHandle thisObjVal(thisObjHandle); // 3. Let len be ToLength(Get(O, "length")). - int64_t len = ArrayHelper::GetLength(thread, thisObjVal); + int64_t len = ArrayHelper::GetArrayLength(thread, thisObjVal); // 4. ReturnIfAbrupt(len). RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); diff --git a/ecmascript/builtins/builtins_typedarray.cpp b/ecmascript/builtins/builtins_typedarray.cpp index 05b6e2091..31b947c9c 100644 --- a/ecmascript/builtins/builtins_typedarray.cpp +++ b/ecmascript/builtins/builtins_typedarray.cpp @@ -656,7 +656,69 @@ JSTaggedValue BuiltinsTypedArray::FindIndex(EcmaRuntimeCallInfo *argv) if (!GetThis(argv)->IsTypedArray()) { THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "This is not a TypedArray.", JSTaggedValue::Exception()); } - return BuiltinsArray::FindIndex(argv); + JSThread *thread = argv->GetThread(); + [[maybe_unused]] EcmaHandleScope handleScope(thread); + + // 1. Let O be the this value. + JSHandle thisHandle = GetThis(argv); + JSHandle thisObjHandle = JSTaggedValue::ToObject(thread, thisHandle); + // 2. ReturnIfAbrupt(O). + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + JSHandle thisObjVal(thisObjHandle); + + // 3. Let len be TypedArrayLength(taRecord). + uint64_t len = static_cast(JSHandle::Cast(thisHandle)->GetArrayLength()); + // 4. ReturnIfAbrupt(len). + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + + // 5. If IsCallable(predicate) is false, throw a TypeError exception. + JSHandle callbackFnHandle = GetCallArg(argv, 0); + if (!callbackFnHandle->IsCallable()) { + THROW_TYPE_ERROR_AND_RETURN(thread, "the predicate is not callable.", JSTaggedValue::Exception()); + } + + // 6. If thisArg was supplied, let T be thisArg; else let T be undefined. + JSHandle thisArgHandle = GetCallArg(argv, 1); + + // 7. Let k be 0. + // 8. Repeat, while k < len + // a. Let Pk be ToString(k). + // b. Let kValue be Get(O, Pk). + // c. ReturnIfAbrupt(kValue). + // d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)). + // e. ReturnIfAbrupt(testResult). + // f. If testResult is true, return k. + // g. Increase k by 1. + uint32_t k = 0; + JSTaggedValue callResult = GetTaggedBoolean(true); + if (thisObjVal->IsStableJSArray(thread)) { + callResult = JSStableArray::HandleFindIndexOfStable(thread, thisObjHandle, callbackFnHandle, thisArgHandle, k); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (callResult.ToBoolean()) { + return GetTaggedDouble(k); + } + } + JSMutableHandle key(thread, JSTaggedValue::Undefined()); + JSHandle undefined = thread->GlobalConstants()->GetHandledUndefined(); + const uint32_t argsLength = 3; // 3: «kValue, k, O» + while (k < len) { + JSHandle kValue = JSArray::FastGetPropertyByValue(thread, thisObjVal, k); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + key.Update(JSTaggedValue(k)); + EcmaRuntimeCallInfo *info = + EcmaInterpreter::NewRuntimeCallInfo(thread, callbackFnHandle, thisArgHandle, undefined, argsLength); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + info->SetCallArg(kValue.GetTaggedValue(), key.GetTaggedValue(), thisObjVal.GetTaggedValue()); + callResult = JSFunction::Call(info); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (callResult.ToBoolean()) { + return GetTaggedDouble(k); + } + k++; + } + + // 9. Return -1. + return GetTaggedDouble(-1); } // 22.2.3.12 diff --git a/test/regresstest/regresstest_skip_tests.json b/test/regresstest/regresstest_skip_tests.json index 7d65c7fab..a706e2e9f 100644 --- a/test/regresstest/regresstest_skip_tests.json +++ b/test/regresstest/regresstest_skip_tests.json @@ -2796,7 +2796,6 @@ "mjsunit/harmony/regexp-match-indices.js", "mjsunit/harmony/regexp-property-binary.js", "mjsunit/harmony/suppressed-error.js", - "mjsunit/harmony/typedarray-findlastindex.js", "mjsunit/harmony/typed-array-to-sorted.js", "mjsunit/harmony/shadowrealm-wrapped-function-props.js", "mjsunit/harmony/object-groupby-fast-path-assumptions.js", @@ -3102,7 +3101,6 @@ "mjsunit/es6/regexp-sticky.js", "mjsunit/es6/indexed-integer-exotics.js", "mjsunit/es6/proxies-json.js", - "mjsunit/es6/typedarray-findindex.js", "mjsunit/es6/reflect.js", "mjsunit/es6/set-iterator-10.js", "mjsunit/es6/set-iterator-8.js", -- Gitee