diff --git a/ecmascript/base/atomic_helper.cpp b/ecmascript/base/atomic_helper.cpp index 9e94fd22366e4b49c5b6ea5e3e9c4cf41994ffef..2a983307858ed6d632eea2972f43e96c1cc15876 100644 --- a/ecmascript/base/atomic_helper.cpp +++ b/ecmascript/base/atomic_helper.cpp @@ -113,7 +113,8 @@ JSTaggedValue AtomicHelper::AtomicStore(JSThread *thread, const JSHandle(thread, integerValue); } - if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer.GetTaggedValue())) { + JSTaggedValue detachedBuffer = JSHandle::Cast(typedArray)->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", JSTaggedValue::Exception()); } @@ -129,7 +130,8 @@ JSTaggedValue AtomicHelper::AtomicLoad(JSThread *thread, const JSHandle::Cast(typedArray)->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", JSTaggedValue::Exception()); } diff --git a/ecmascript/builtins/builtins_atomics.cpp b/ecmascript/builtins/builtins_atomics.cpp index 20ccca3a73e72451606bcab711da39be5b817b74..ea732e1393049320025f792ac6931b5a3e347567 100644 --- a/ecmascript/builtins/builtins_atomics.cpp +++ b/ecmascript/builtins/builtins_atomics.cpp @@ -291,7 +291,8 @@ JSTaggedValue BuiltinsAtomics::AtomicReadModifyWrite(JSThread *thread, const JSH // 3. Let arrayTypeName be typedArray.[[TypedArrayName]]. JSHandle arrayTypeName(thread, JSTypedArray::Cast(typedArray->GetTaggedObject())->GetTypedArrayName()); - if (BuiltinsArrayBuffer::IsDetachedBuffer(buffer.GetTaggedValue())) { + JSTaggedValue detachedBuffer = JSHandle::Cast(typedArray)->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", JSTaggedValue::Exception()); } @@ -345,12 +346,23 @@ JSTaggedValue BuiltinsAtomics::HandleWithUint8(JSThread *thread, uint32_t size, BUILTINS_API_TRACE(thread, Atomics, HandleWithUint8); JSHandle value = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD); uint8_t tag = JSTaggedValue::ToUint8(thread, value); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } if (size == 3) { // the number of parameters is 3 auto result = op((block + indexedPosition), &tag); return BuiltinsBase::GetTaggedInt(result); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); uint8_t newTag = JSTaggedValue::ToUint8(thread, newValue); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } uint8_t arg[ARGS_NUMBER] = {0}; arg[0] = tag; arg[1] = newTag; @@ -366,12 +378,23 @@ JSTaggedValue BuiltinsAtomics::HandleWithInt8(JSThread *thread, uint32_t size, u BUILTINS_API_TRACE(thread, Atomics, HandleWithInt8); JSHandle value = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD); int8_t tag = JSTaggedValue::ToInt8(thread, value); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &tag); return BuiltinsBase::GetTaggedInt(result); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); int8_t newTag = JSTaggedValue::ToInt8(thread, newValue); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } int8_t arg[ARGS_NUMBER] = {0}; arg[0] = tag; arg[1] = newTag; @@ -387,12 +410,23 @@ JSTaggedValue BuiltinsAtomics::HandleWithUint16(JSThread *thread, uint32_t size, BUILTINS_API_TRACE(thread, Atomics, HandleWithUint16); JSHandle value = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD); uint16_t tag = JSTaggedValue::ToUint16(thread, value); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &tag); return BuiltinsBase::GetTaggedInt(result); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); uint16_t newTag = JSTaggedValue::ToUint16(thread, newValue); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } uint16_t arg[ARGS_NUMBER] = {0}; arg[0] = tag; arg[1] = newTag; @@ -408,12 +442,23 @@ JSTaggedValue BuiltinsAtomics::HandleWithInt16(JSThread *thread, uint32_t size, BUILTINS_API_TRACE(thread, Atomics, HandleWithInt16); JSHandle value = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD); int16_t tag = JSTaggedValue::ToInt16(thread, value); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &tag); return BuiltinsBase::GetTaggedInt(result); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); int16_t newTag = JSTaggedValue::ToInt16(thread, newValue); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } int16_t arg[ARGS_NUMBER] = {0}; arg[0] = tag; arg[1] = newTag; @@ -429,12 +474,23 @@ JSTaggedValue BuiltinsAtomics::HandleWithUint32(JSThread *thread, uint32_t size, BUILTINS_API_TRACE(thread, Atomics, HandleWithUint32); JSHandle value = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD); uint32_t tag = JSTaggedValue::ToUint32(thread, value); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &tag); return JSTaggedValue(result); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); uint32_t newTag = JSTaggedValue::ToUint32(thread, newValue); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } uint32_t arg[ARGS_NUMBER] = {0}; arg[0] = tag; arg[1] = newTag; @@ -450,12 +506,23 @@ JSTaggedValue BuiltinsAtomics::HandleWithInt32(JSThread *thread, uint32_t size, BUILTINS_API_TRACE(thread, Atomics, HandleWithInt32); JSHandle value = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::THIRD); int32_t tag = JSTaggedValue::ToInt32(thread, value); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &tag); return BuiltinsBase::GetTaggedInt(result); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); int32_t newTag = JSTaggedValue::ToInt32(thread, newValue); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } int32_t arg[ARGS_NUMBER] = {0}; arg[0] = tag; arg[1] = newTag; @@ -473,14 +540,25 @@ JSTaggedValue BuiltinsAtomics::HandleWithBigInt64(JSThread *thread, uint32_t siz int64_t val = 0; bool lossless = true; BigInt::BigIntToInt64(thread, value, &val, &lossless); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &val); return BigInt::Int64ToBigInt(thread, result).GetTaggedValue(); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); int64_t newVal = 0; BigInt::BigIntToInt64(thread, newValue, &newVal, &lossless); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); int64_t arg[ARGS_NUMBER] = {0}; arg[0] = val; @@ -499,13 +577,26 @@ JSTaggedValue BuiltinsAtomics::HandleWithBigUint64(JSThread *thread, uint32_t si uint64_t val = 0; bool lossless = true; BigInt::BigIntToUint64(thread, value, &val, &lossless); + JSTaggedValue detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); if (size == 3) { // the number of parameters is 3 auto result = op(reinterpret_cast(block + indexedPosition), &val); return BigInt::Uint64ToBigInt(thread, result).GetTaggedValue(); } + JSHandle newValue = BuiltinsBase::GetCallArg(argv, BuiltinsBase::ArgsPosition::FOURTH); uint64_t newVal = 0; BigInt::BigIntToUint64(thread, newValue, &newVal, &lossless); + detachedBuffer = JSHandle::Cast(GetCallArg(argv, 0))->GetViewedArrayBufferOrByteArray(); + if (BuiltinsArrayBuffer::IsDetachedBuffer(detachedBuffer)) { + THROW_TYPE_ERROR_AND_RETURN(thread, "The ArrayBuffer of this value is detached buffer.", + JSTaggedValue::Exception()); + } + RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); uint64_t arg[ARGS_NUMBER] = {0}; arg[0] = val; arg[1] = newVal; diff --git a/ecmascript/js_bigint.cpp b/ecmascript/js_bigint.cpp index 1f9342fcc204a38257acf97a0fe33502a491f136..aedc5503697f64d55814f0e548c1d0d9bb971a4c 100644 --- a/ecmascript/js_bigint.cpp +++ b/ecmascript/js_bigint.cpp @@ -608,7 +608,13 @@ void BigInt::BigIntToInt64(JSThread *thread, JSHandle bigint, int if (bigint->IsBoolean()) { bigint = JSHandle(thread, JSTaggedValue::ToBigInt(thread, bigint)); RETURN_IF_ABRUPT_COMPLETION(thread); + } else if (!bigint->IsBigInt()) { + JSHandle bigInt64(thread, JSTaggedValue::ToBigInt64(thread, bigint)); + RETURN_IF_ABRUPT_COMPLETION(thread); + *cValue = bigInt64->ToInt64(); + return; } + JSHandle bigInt64(thread, JSTaggedValue::ToBigInt64(thread, bigint)); RETURN_IF_ABRUPT_COMPLETION(thread); if (Equal(bigInt64.GetTaggedValue(), bigint.GetTaggedValue())) { @@ -626,7 +632,13 @@ void BigInt::BigIntToUint64(JSThread *thread, JSHandle bigint, ui if (bigint->IsBoolean()) { bigint = JSHandle(thread, JSTaggedValue::ToBigInt(thread, bigint)); RETURN_IF_ABRUPT_COMPLETION(thread); + } else if (!bigint->IsBigInt()) { + JSHandle bigInt64(thread, JSTaggedValue::ToBigUint64(thread, bigint)); + RETURN_IF_ABRUPT_COMPLETION(thread); + *cValue = bigInt64->ToInt64(); + return; } + JSHandle bigUint64(thread, JSTaggedValue::ToBigUint64(thread, bigint)); RETURN_IF_ABRUPT_COMPLETION(thread); if (Equal(bigUint64.GetTaggedValue(), bigint.GetTaggedValue())) {