diff --git a/ecmascript/builtins/builtins_array.cpp b/ecmascript/builtins/builtins_array.cpp index 83f12a88b69b7f8ab45d57c917f04f8dd2c154d9..ea6dc4cf5842bd2896628b7edccf0fbc3366ea95 100644 --- a/ecmascript/builtins/builtins_array.cpp +++ b/ecmascript/builtins/builtins_array.cpp @@ -2220,7 +2220,7 @@ JSTaggedValue BuiltinsArray::Some(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 05b6e20918cf00f41ee1c7d11127bd802a0a0bc8..0b56ab449358d70a0253a8ac9618d9864084a290 100644 --- a/ecmascript/builtins/builtins_typedarray.cpp +++ b/ecmascript/builtins/builtins_typedarray.cpp @@ -1423,11 +1423,77 @@ JSTaggedValue BuiltinsTypedArray::Slice(EcmaRuntimeCallInfo *argv) JSTaggedValue BuiltinsTypedArray::Some(EcmaRuntimeCallInfo *argv) { ASSERT(argv); - BUILTINS_API_TRACE(argv->GetThread(), TypedArray, Some); + JSThread *thread = argv->GetThread(); + BUILTINS_API_TRACE(thread, TypedArray, Some); 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()); + } + + // 1. Let O be ToObject(this value). + JSHandle thisHandle = GetThis(argv); + JSHandle typedArray(thisHandle); + JSTaggedValue srcBuffer = JSHandle::Cast(thisHandle)->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(srcBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of typedArray is detached buffer.", + JSTaggedValue::Exception()); + } + + 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(callbackfn) is false, throw a TypeError exception. + JSHandle callbackFnHandle = GetCallArg(argv, 0); + if (!callbackFnHandle->IsCallable()) { + THROW_TYPE_ERROR_AND_RETURN(thread, "the callbackfun 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 kPresent be HasProperty(O, Pk). + // c. ReturnIfAbrupt(kPresent). + // d. If kPresent is true, then + // i. Let kValue be Get(O, Pk). + // ii. ReturnIfAbrupt(kValue). + // iii. Let testResult be ToBoolean(Call(callbackfn, T, «kValue, k, and O»)). + // iv. ReturnIfAbrupt(testResult). + // v. If testResult is true, return true. + // e. Increase k by 1. + JSMutableHandle key(thread, JSTaggedValue::Undefined()); + int64_t k = 0; + while (k < len) { + bool exists = (thisHandle->IsTypedArray() || JSTaggedValue::HasProperty(thread, thisObjVal, k)); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + if (exists) { + key.Update(JSTaggedValue(k)); + JSHandle kValue = JSArray::FastGetPropertyByValue(thread, thisObjVal, key); + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); + const uint32_t argsLength = 3; // 3: «kValue, k, O» + JSHandle undefined = thread->GlobalConstants()->GetHandledUndefined(); + 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 GetTaggedBoolean(true); + } + } + k++; } - return BuiltinsArray::Some(argv); + + // 9. Return false. + return GetTaggedBoolean(false); } // 22.2.3.25 diff --git a/test/regresstest/regresstest_skip_tests.json b/test/regresstest/regresstest_skip_tests.json index 721f3145e96d9b9dba43972daff1f81c93f6c9a2..d7c66cb3dcb27c7d048fb8e43fb7deec569b44aa 100644 --- a/test/regresstest/regresstest_skip_tests.json +++ b/test/regresstest/regresstest_skip_tests.json @@ -3037,7 +3037,6 @@ "mjsunit/es6/proxies-cross-realm-exception.js", "mjsunit/es6/super-with-spread-modify-next.js", "mjsunit/es6/array-of.js", - "mjsunit/es6/typedarray-iteration.js", "mjsunit/es6/array-iterator-detached.js", "mjsunit/es6/block-let-semantics-sloppy.js", "mjsunit/es6/string-iterator6.js",