!4374 bytearray 适配 bigint

Merge pull request !4374 from weng-changcheng/modify_bigint
This commit is contained in:
openharmony_ci 2023-07-04 17:08:38 +00:00 committed by Gitee
commit 13bc96fe08
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 53 additions and 18 deletions

View File

@ -384,7 +384,7 @@ JSTaggedValue BuiltinsArrayBuffer::SetValueInBuffer(JSThread *thread, JSTaggedVa
void *pointer = GetDataPointFromBuffer(arrBuf);
uint8_t *block = reinterpret_cast<uint8_t *>(pointer);
double val = value.GetTaggedValue().GetNumber();
return SetValueInBuffer(byteIndex, block, type, val, littleEndian);
return SetValueInBuffer(thread, byteIndex, block, type, val, littleEndian);
}
// es12 25.1.2.7 IsBigIntElementType ( type )
@ -631,16 +631,38 @@ void BuiltinsArrayBuffer::SetValueInBufferForBigInt(JSThread *thread,
SetTypeData(block, value, byteIndex);
}
JSTaggedValue BuiltinsArrayBuffer::FastSetValueInBuffer(JSTaggedValue arrBuf, uint32_t byteIndex,
template<typename T>
void BuiltinsArrayBuffer::SetValueInBufferForBigInt(JSThread *thread,
double val, uint8_t *block,
uint32_t byteIndex, bool littleEndian)
{
static_assert(std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t>, "T must be int64_t/uint64_t");
T value = 0;
bool lossless = true;
JSHandle<JSTaggedValue> valHandle(thread, GetTaggedDouble(val));
if constexpr(std::is_same_v<T, uint64_t>) {
BigInt::BigIntToUint64(thread, valHandle, reinterpret_cast<uint64_t *>(&value), &lossless);
} else {
BigInt::BigIntToInt64(thread, valHandle, reinterpret_cast<int64_t *>(&value), &lossless);
}
RETURN_IF_ABRUPT_COMPLETION(thread);
if (!littleEndian) {
value = LittleEndianToBigEndian64Bit<T>(value);
}
SetTypeData(block, value, byteIndex);
}
JSTaggedValue BuiltinsArrayBuffer::FastSetValueInBuffer(JSThread *thread, JSTaggedValue arrBuf, uint32_t byteIndex,
DataViewType type, double val, bool littleEndian)
{
void *pointer = GetDataPointFromBuffer(arrBuf);
uint8_t *block = reinterpret_cast<uint8_t *>(pointer);
return SetValueInBuffer(byteIndex, block, type, val, littleEndian);
return SetValueInBuffer(thread, byteIndex, block, type, val, littleEndian);
}
JSTaggedValue BuiltinsArrayBuffer::SetValueInBuffer(uint32_t byteIndex, uint8_t *block, DataViewType type, double val,
bool littleEndian)
JSTaggedValue BuiltinsArrayBuffer::SetValueInBuffer(JSThread* thread, uint32_t byteIndex, uint8_t *block,
DataViewType type, double val, bool littleEndian)
{
switch (type) {
case DataViewType::UINT8:
@ -670,6 +692,14 @@ JSTaggedValue BuiltinsArrayBuffer::SetValueInBuffer(uint32_t byteIndex, uint8_t
case DataViewType::FLOAT64:
SetValueInBufferForFloat<double>(val, block, byteIndex, littleEndian);
break;
case DataViewType::BIGINT64:
SetValueInBufferForBigInt<int64_t>(thread, val, block, byteIndex, littleEndian);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
break;
case DataViewType::BIGUINT64:
SetValueInBufferForBigInt<uint64_t>(thread, val, block, byteIndex, littleEndian);
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
break;
default:
LOG_ECMA(FATAL) << "this branch is unreachable";
UNREACHABLE();

View File

@ -71,10 +71,10 @@ public:
// es12 25.1.2.7 IsBigIntElementType ( type )
static bool IsBigIntElementType(DataViewType type);
static JSTaggedValue FastSetValueInBuffer(JSTaggedValue arrBuf, uint32_t byteIndex, DataViewType type, double val,
bool littleEndian);
static JSTaggedValue SetValueInBuffer(uint32_t byteIndex, uint8_t *block, DataViewType type, double val,
bool littleEndian);
static JSTaggedValue FastSetValueInBuffer(JSThread* thread, JSTaggedValue arrBuf, uint32_t byteIndex,
DataViewType type, double val, bool littleEndian);
static JSTaggedValue SetValueInBuffer(JSThread* thread, uint32_t byteIndex, uint8_t *block,
DataViewType type, double val, bool littleEndian);
static JSTaggedValue GetValueFromBuffer(JSThread *thread, uint32_t byteIndex, uint8_t *block,
DataViewType type, bool littleEndian);
static void *GetDataPointFromBuffer(JSTaggedValue arrBuf, uint32_t byteOffset = 0);
@ -110,6 +110,10 @@ private:
template<typename T>
static void SetValueInBufferForBigInt(JSThread *thread, const JSHandle<JSTaggedValue> &val,
JSHandle<JSTaggedValue> &arrBuf, uint32_t byteIndex, bool littleEndian);
template<typename T>
static void SetValueInBufferForBigInt(JSThread *thread, double val,
uint8_t *block, uint32_t byteIndex, bool littleEndian);
};
} // namespace panda::ecmascript::builtins

View File

@ -16,14 +16,15 @@
#include "ecmascript/byte_array.h"
#include "ecmascript/builtins/builtins_arraybuffer.h"
#include "ecmascript/mem/tagged_object-inl.h"
namespace panda::ecmascript {
void ByteArray::Set(uint32_t idx, DataViewType type, JSTaggedType val, uint32_t offset)
void ByteArray::Set(JSThread* thread, uint32_t idx, DataViewType type, JSTaggedType val, uint32_t offset)
{
void *pointer = GetData();
auto *block = reinterpret_cast<uint8_t *>(pointer) + offset;
builtins::BuiltinsArrayBuffer::SetValueInBuffer(idx * GetByteLength(), block, type,
JSTaggedValue(val).GetNumber(), true);
builtins::BuiltinsArrayBuffer::SetValueInBuffer(thread, idx * GetByteLength(), block,
type, JSTaggedValue(val).GetNumber(), true);
}
JSTaggedValue ByteArray::Get(JSThread *thread, uint32_t idx, DataViewType type, uint32_t offset)

View File

@ -40,7 +40,7 @@ public:
return reinterpret_cast<void *>(ToUintPtr(this) + DATA_OFFSET + index * GetByteLength());
}
void Set(uint32_t idx, DataViewType type, JSTaggedType val, uint32_t offset = 0);
void Set(JSThread* thread, uint32_t idx, DataViewType type, JSTaggedType val, uint32_t offset = 0);
JSTaggedValue Get(JSThread *thread, uint32_t idx, DataViewType type, uint32_t offset = 0);
static constexpr size_t ARRAY_LENGTH_OFFSET = TaggedObjectSize();

View File

@ -1259,7 +1259,7 @@ JSHandle<JSTaggedValue> JSDeserializer::ReadByteArray()
JSHandle<ByteArray> byteArray = factory_->NewByteArray(arrayLength, arrayType);
for (int32_t i = 0; i < arrayLength; i++) {
JSHandle<JSTaggedValue> val = DeserializeJSTaggedValue();
byteArray->Set(i, viewType, val.GetTaggedType());
byteArray->Set(thread_, i, viewType, val.GetTaggedType());
}
return JSHandle<JSTaggedValue>(byteArray);
}

View File

@ -696,7 +696,7 @@ JSTaggedValue JSTypedArray::FastSetPropertyByIndex(JSThread *thread, const JSTag
// Let elementType be the String value of the Element Type value in Table 49 for arrayTypeName.
DataViewType elementType = TypedArrayHelper::GetType(jsType);
// Perform SetValueInBuffer(buffer, indexedPosition, elementType, numValue).
return BuiltinsArrayBuffer::FastSetValueInBuffer(buffer, byteIndex, elementType, numValue.GetNumber(), true);
return BuiltinsArrayBuffer::FastSetValueInBuffer(thread, buffer, byteIndex, elementType, numValue.GetNumber(), true);
}
JSTaggedValue JSTypedArray::GetOffHeapBuffer(JSThread *thread, JSHandle<JSTypedArray> &typedArray)

View File

@ -78,7 +78,7 @@ HWTEST_F_L0(ByteArrayTest, SetAndGet)
uint32_t value = 4294967295;
JSTaggedType val = JSTaggedValue(value).GetRawData();
JSHandle<ByteArray> byteArray = factory->NewByteArray(3, sizeof(value));
byteArray->Set(1, DataViewType::UINT32, val, 2);
byteArray->Set(thread, 1, DataViewType::UINT32, val, 2);
EXPECT_EQ(byteArray->Get(thread, 1, DataViewType::UINT32, 2), JSTaggedValue(value));
}
} // namespace panda::ecmascript

View File

@ -1712,7 +1712,7 @@ HWTEST_F_L0(JSSerializerTest, SerializeJSTypedArray2)
int byteArrayLength = 10; // 10: arrayLength
JSHandle<ByteArray> byteArray = factory->NewByteArray(byteArrayLength, sizeof(value));
for (int i = 0; i < byteArrayLength; i++) {
byteArray->Set(i, DataViewType::UINT8, val);
byteArray->Set(thread, i, DataViewType::UINT8, val);
}
int8Array->SetViewedArrayBufferOrByteArray(thread, byteArray);
int byteLength = 10;

View File

@ -55,7 +55,7 @@ namespace OHOS {
JSTaggedValue arrayBuffer = BuiltinsArrayBuffer::AllocateArrayBuffer(thread, bufferConstructor, MAXBYTELEN);
double val = JSTaggedValue(input).GetNumber();
BuiltinsArrayBuffer::FastSetValueInBuffer(arrayBuffer, 0, DataViewType::INT32, val, true);
BuiltinsArrayBuffer::FastSetValueInBuffer(thread, arrayBuffer, 0, DataViewType::INT32, val, true);
JSNApi::DestroyJSVM(vm);
}
}