napi_set_element adapted for sendable array

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I9PJ08?from=project-issue
Signed-off-by: liu-zelin <liuzelin8@huawei.com>
Change-Id: I42275d4adb93f79e5a8e68219f94c483ecca842f
This commit is contained in:
liu-zelin 2024-05-15 16:07:33 +08:00
parent 4f8774bce0
commit 1a99f55942
4 changed files with 36 additions and 3 deletions

View File

@ -1031,8 +1031,8 @@ class ECMA_PUBLIC_API SendableArrayRef : public ObjectRef {
public:
static Local<SendableArrayRef> New(const EcmaVM *vm, uint32_t length = 0);
uint32_t Length(const EcmaVM *vm);
static bool SetValueAt(const EcmaVM *vm, Local<JSValueRef> obj, uint32_t index, Local<JSValueRef> value);
static Local<JSValueRef> GetValueAt(const EcmaVM *vm, Local<JSValueRef> obj, uint32_t index);
static bool SetProperty(const EcmaVM *vm, Local<JSValueRef> obj, uint32_t index, Local<JSValueRef> value);
};
class ECMA_PUBLIC_API Int8ArrayRef : public TypedArrayRef {

View File

@ -3571,6 +3571,13 @@ Local<SendableArrayRef> SendableArrayRef::New(const EcmaVM *vm, uint32_t length)
ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
JSTaggedNumber arrayLen(length);
JSHandle<JSTaggedValue> array = ecmascript::JSSharedArray::ArrayCreate(thread, arrayLen);
JSHandle<JSTaggedValue> initialValue(thread, JSTaggedValue::Undefined());
JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
for (uint32_t i = 0; i < length; i++) {
key.Update(JSTaggedValue(i));
JSObject::CreateDataPropertyOrThrow(
thread, JSHandle<JSObject>(array), key, initialValue, ecmascript::JSShared::SCheckMode::SKIP);
}
RETURN_VALUE_IF_ABRUPT(thread, JSValueRef::Undefined(vm));
return JSNApiHelper::ToLocal<SendableArrayRef>(array);
}
@ -3590,13 +3597,14 @@ Local<JSValueRef> SendableArrayRef::GetValueAt(const EcmaVM *vm, Local<JSValueRe
return JSNApiHelper::ToLocal<JSValueRef>(result);
}
bool SendableArrayRef::SetValueAt(const EcmaVM *vm, Local<JSValueRef> obj, uint32_t index, Local<JSValueRef> value)
bool SendableArrayRef::SetProperty(const EcmaVM *vm, Local<JSValueRef> obj, uint32_t index, Local<JSValueRef> value)
{
CROSS_THREAD_AND_EXCEPTION_CHECK_WITH_RETURN(vm, false);
ecmascript::ThreadManagedScope managedScope(vm->GetJSThread());
JSHandle<JSTaggedValue> objectHandle = JSNApiHelper::ToJSHandle(obj);
JSHandle<JSTaggedValue> valueHandle = JSNApiHelper::ToJSHandle(value);
return ecmascript::JSSharedArray::FastSetPropertyByValue(thread, objectHandle, index, valueHandle);
return ecmascript::JSSharedArray::SetProperty(
thread, objectHandle, index, valueHandle, true, ecmascript::SCheckMode::CHECK);
}
// ---------------------------------- Error ---------------------------------------

View File

@ -474,6 +474,25 @@ bool JSSharedArray::SetProperty(JSThread *thread, const JSHandle<JSTaggedValue>
return JSObject::SetProperty(&op, value, mayThrow);
}
bool JSSharedArray::SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
uint32_t index, const JSHandle<JSTaggedValue> &value, bool mayThrow,
SCheckMode sCheckMode)
{
// Concurrent check for shared array
[[maybe_unused]] ConcurrentApiScope<JSSharedArray, ModType::WRITE> scope(
thread, obj.GetTaggedValue().GetTaggedObject(), sCheckMode);
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
// 2 ~ 4 findProperty in Receiver, Obj and its parents
ObjectOperator op(thread, obj, index);
// Out of bounds check for shared array
if ((obj->IsJSSharedArray() && sCheckMode == SCheckMode::CHECK) && op.IsElement() && !op.IsFound()) {
auto error = containers::ContainerError::BusinessError(thread, containers::ErrorFlag::RANGE_ERROR,
"The value of index is out of range.");
THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
}
return JSObject::SetProperty(&op, value, mayThrow);
}
// ecma2024 23.1.3.20 Array.prototype.sort(comparefn)
JSTaggedValue JSSharedArray::Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
const JSHandle<JSTaggedValue> &fn)

View File

@ -112,6 +112,12 @@ public:
const JSHandle<JSTaggedValue> &key, SCheckMode sCheckMode);
static bool SetProperty(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const JSHandle<JSTaggedValue> &key,
const JSHandle<JSTaggedValue> &value, bool mayThrow, SCheckMode sCheckMode);
static bool SetProperty(JSThread *thread,
const JSHandle<JSTaggedValue> &obj,
uint32_t index,
const JSHandle<JSTaggedValue> &value,
bool mayThrow,
SCheckMode sCheckMode);
static JSTaggedValue Sort(JSThread *thread, const JSHandle<JSTaggedValue> &obj, const JSHandle<JSTaggedValue> &fn);
static bool IncludeInSortedValue(JSThread *thread, const JSHandle<JSTaggedValue> &obj,