From 90b29b24a5b13ecb3e1709ef0630a77d5dc9543e Mon Sep 17 00:00:00 2001 From: liujia178 Date: Tue, 23 Apr 2024 16:05:05 +0800 Subject: [PATCH] fix harmony/typedarray-findlast.js Issue: id (issueID #I9IZC5) Signed-off-by: liujia178 --- ecmascript/builtins/builtins_array.cpp | 2 +- ecmascript/builtins/builtins_typedarray.cpp | 59 +++++++++++++++++++- test/regresstest/regresstest_skip_tests.json | 1 - 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/ecmascript/builtins/builtins_array.cpp b/ecmascript/builtins/builtins_array.cpp index 83f12a88b..ccd78a566 100644 --- a/ecmascript/builtins/builtins_array.cpp +++ b/ecmascript/builtins/builtins_array.cpp @@ -3246,7 +3246,7 @@ JSTaggedValue BuiltinsArray::FindLast(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..7e2ed332e 100644 --- a/ecmascript/builtins/builtins_typedarray.cpp +++ b/ecmascript/builtins/builtins_typedarray.cpp @@ -1922,11 +1922,64 @@ JSTaggedValue BuiltinsTypedArray::ToReversed(EcmaRuntimeCallInfo *argv) JSTaggedValue BuiltinsTypedArray::FindLast(EcmaRuntimeCallInfo *argv) { ASSERT(argv); - BUILTINS_API_TRACE(argv->GetThread(), TypedArray, FindLast); + JSThread *thread = argv->GetThread(); + BUILTINS_API_TRACE(thread, TypedArray, FindLast); if (!GetThis(argv)->IsTypedArray()) { - THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "This is not a TypedArray.", JSTaggedValue::Exception()); + THROW_TYPE_ERROR_AND_RETURN(thread, "This is not a TypedArray.", JSTaggedValue::Exception()); } - return BuiltinsArray::FindLast(argv); + + // 1. Let O be ToObject(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 ToLength(Get(O, "length")). + int64_t len = 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 (len - 1). + // 8. Repeat, while k >= 0 + // 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 kValue. + // g. Decrease k by 1. + JSMutableHandle key(thread, JSTaggedValue::Undefined()); + int64_t k = len - 1; + JSHandle undefined = thread->GlobalConstants()->GetHandledUndefined(); + const uint32_t argsLength = 3; // 3: «kValue, k, O» + while (k >= 0) { + 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()); + JSTaggedValue callResult = JSFunction::Call(info); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (callResult.ToBoolean()) { + return kValue.GetTaggedValue(); + } + k--; + } + + // 9. Return undefined. + return JSTaggedValue::Undefined(); } // 23.2.3.14 diff --git a/test/regresstest/regresstest_skip_tests.json b/test/regresstest/regresstest_skip_tests.json index 721f3145e..6a736f963 100644 --- a/test/regresstest/regresstest_skip_tests.json +++ b/test/regresstest/regresstest_skip_tests.json @@ -2807,7 +2807,6 @@ "mjsunit/harmony/modules-skip-1.json", "mjsunit/harmony/async-from-sync-iterator-return-tick-count.js", "mjsunit/harmony/index-fields-nonextensible-global-proxy-no-lazy-feedback.js", - "mjsunit/harmony/typedarray-findlast.js", "mjsunit/harmony/module-parsing-eval.js", "mjsunit/harmony/shadowrealm-importvalue.js", "mjsunit/harmony/to-number.js", -- Gitee