Rollback InsertByIndex in LinkedList

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IB5ELW

Signed-off-by: chengzhihao <chengzhihao7@h-partners.com>
This commit is contained in:
chengzhihao 2024-11-19 16:42:44 +08:00
parent 0426395fc2
commit 3627ec3efc
6 changed files with 15 additions and 269 deletions

View File

@ -151,28 +151,6 @@ JSTaggedValue ContainersLinkedList::Length(EcmaRuntimeCallInfo *argv)
return JSTaggedValue(jsAPILinkedList->Length());
}
JSTaggedValue ContainersLinkedList::InsertIntoLinkedList(JSThread *thread,
JSHandle<JSTaggedValue> self, JSHandle<JSTaggedValue> value,
JSHandle<JSTaggedValue> index)
{
if (index->IsDouble()) {
index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble()));
}
if (!index->IsInt()) {
JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
CString errorMsg =
"The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result);
JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
}
JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self);
JSTaggedValue result =
JSAPILinkedList::Insert(thread, jsAPILinkedList, value, index->GetInt());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result;
}
JSTaggedValue ContainersLinkedList::Insert(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv != nullptr);
@ -193,31 +171,22 @@ JSTaggedValue ContainersLinkedList::Insert(EcmaRuntimeCallInfo *argv)
JSHandle<JSTaggedValue> value = GetCallArg(argv, 1);
JSHandle<JSTaggedValue> index = GetCallArg(argv, 0);
JSTaggedValue result = InsertIntoLinkedList(thread, self, value, index);
return result;
}
JSTaggedValue ContainersLinkedList::InsertByIndex(EcmaRuntimeCallInfo *argv)
{
ASSERT(argv != nullptr);
JSThread *thread = argv->GetThread();
BUILTINS_API_TRACE(thread, LinkedList, InsertByIndex);
[[maybe_unused]] EcmaHandleScope handleScope(thread);
JSHandle<JSTaggedValue> self = GetThis(argv);
if (!self->IsJSAPILinkedList()) {
if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPILinkedList()) {
self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
} else {
JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
"The insertByIndex method cannot be bound");
THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
}
if (index->IsDouble()) {
index = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(index->GetDouble()));
}
JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
JSHandle<JSTaggedValue> index = GetCallArg(argv, 1);
JSTaggedValue result = InsertIntoLinkedList(thread, self, value, index);
if (!index->IsInt()) {
JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, index.GetTaggedValue());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
CString errorMsg =
"The type of \"index\" must be small integer. Received value is: " + ConvertToString(*result);
JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
}
JSHandle<JSAPILinkedList> jsAPILinkedList = JSHandle<JSAPILinkedList>::Cast(self);
JSTaggedValue result =
JSAPILinkedList::Insert(thread, jsAPILinkedList, value, index->GetInt());
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
return result;
}

View File

@ -28,7 +28,6 @@ public:
static JSTaggedValue GetFirst(EcmaRuntimeCallInfo *argv);
static JSTaggedValue GetLast(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Insert(EcmaRuntimeCallInfo *argv);
static JSTaggedValue InsertByIndex(EcmaRuntimeCallInfo *argv);
static JSTaggedValue AddFirst(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Clear(EcmaRuntimeCallInfo *argv);
static JSTaggedValue Clone(EcmaRuntimeCallInfo *argv);
@ -47,11 +46,6 @@ public:
static JSTaggedValue ConvertToArray(EcmaRuntimeCallInfo *argv);
static JSTaggedValue ForEach(EcmaRuntimeCallInfo *argv);
static JSTaggedValue GetIteratorObj(EcmaRuntimeCallInfo *argv);
private:
static JSTaggedValue InsertIntoLinkedList(JSThread *thread,
JSHandle<JSTaggedValue> self, JSHandle<JSTaggedValue> value,
JSHandle<JSTaggedValue> index);
};
} // namespace panda::ecmascript::containers
#endif // ECMASCRIPT_CONTAINERS_CONTAINERS_LINKED_LIST_H

View File

@ -1094,9 +1094,7 @@ JSHandle<JSTaggedValue> ContainersPrivate::InitializeLinkedList(JSThread *thread
JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(linkedListFuncPrototype), constructorKey, linkedListFunction);
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
SetFrozenFunction(thread, linkedListFuncPrototype, "add", ContainersLinkedList::Add, FuncLength::ONE);
SetFrozenFunction(thread, linkedListFuncPrototype, "insert", ContainersLinkedList::Insert, FuncLength::TWO);
SetFrozenFunction(thread, linkedListFuncPrototype, "insertByIndex", ContainersLinkedList::InsertByIndex,
FuncLength::TWO);
SetFrozenFunction(thread, linkedListFuncPrototype, "insert", ContainersLinkedList::Insert, FuncLength::ONE);
SetFrozenFunction(thread, linkedListFuncPrototype, "clear", ContainersLinkedList::Clear, FuncLength::ONE);
SetFrozenFunction(thread, linkedListFuncPrototype, "clone", ContainersLinkedList::Clone, FuncLength::ONE);
SetFrozenFunction(thread, linkedListFuncPrototype, "removeFirst", ContainersLinkedList::RemoveFirst,

View File

@ -126,21 +126,6 @@ protected:
return result;
}
JSTaggedValue LinkedListInsertByIndex(JSHandle<JSAPILinkedList> linkedlist, JSTaggedValue index,
JSTaggedValue value)
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
callInfo->SetFunction(JSTaggedValue::Undefined());
callInfo->SetThis(linkedlist.GetTaggedValue());
callInfo->SetCallArg(0, value);
callInfo->SetCallArg(1, index);
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
JSTaggedValue result = ContainersLinkedList::InsertByIndex(callInfo);
TestHelper::TearDownFrame(thread, prev);
return result;
}
JSTaggedValue LinkedListGet(JSHandle<JSAPILinkedList> linkedlist, JSTaggedValue index)
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
@ -238,49 +223,6 @@ HWTEST_F_L0(ContainersLinkedListTest, InsertAndGet)
}
}
HWTEST_F_L0(ContainersLinkedListTest, InsertByIndexAndGet)
{
constexpr uint32_t NODE_NUMBERS = 8;
JSTaggedValue result = JSTaggedValue::Hole();
JSHandle<JSAPILinkedList> linkedlist = CreateJSAPILinkedList();
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(i), JSTaggedValue(5));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(i + 1));
}
// Insert in position 0(first) with value 10
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(0), JSTaggedValue(10));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS + 1));
// Insert in position NODE_NUMBERS / 2(middle) with value 10
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(NODE_NUMBERS / 2), JSTaggedValue(10));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS + 2));
// Insert in position NODE_NUMBERS + 2(last) with value 10
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(NODE_NUMBERS + 2), JSTaggedValue(10));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS + 3));
uint32_t length = static_cast<uint32_t>(linkedlist->Length());
for (uint32_t i = 0; i < length; i++) {
if (i == 0) {
result = LinkedListGet(linkedlist, JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue(10));
} else if (i == NODE_NUMBERS / 2) {
result = LinkedListGet(linkedlist, JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue(10));
} else if (i == NODE_NUMBERS + 2) {
result = LinkedListGet(linkedlist, JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue(10));
} else {
result = LinkedListGet(linkedlist, JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue(5));
}
}
}
HWTEST_F_L0(ContainersLinkedListTest, Remove)
{
constexpr uint32_t NODE_NUMBERS = 20;
@ -306,31 +248,6 @@ HWTEST_F_L0(ContainersLinkedListTest, Remove)
}
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveAndInsertByIndex)
{
constexpr uint32_t NODE_NUMBERS = 20;
JSTaggedValue result = JSTaggedValue::Hole();
JSHandle<JSAPILinkedList> linkedlist = CreateJSAPILinkedList();
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(i), JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(i + 1));
}
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
callInfo->SetFunction(JSTaggedValue::Undefined());
callInfo->SetThis(linkedlist.GetTaggedValue());
callInfo->SetCallArg(0, JSTaggedValue(NODE_NUMBERS / 2));
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
JSTaggedValue rvalue = ContainersLinkedList::Remove(callInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_EQ(rvalue, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 1));
}
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveByIndex)
{
constexpr uint32_t NODE_NUMBERS = 20;
@ -353,28 +270,6 @@ HWTEST_F_L0(ContainersLinkedListTest, RemoveByIndex)
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 2));
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveByIndexAndInsertByIndex)
{
constexpr uint32_t NODE_NUMBERS = 20;
JSTaggedValue result = JSTaggedValue::Hole();
JSHandle<JSAPILinkedList> linkedlist = CreateJSAPILinkedList();
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(i), JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(i + 1));
}
// Remove index > (NODE_NUMBERS / 2)
result = LinkedListRemoveByIndex(linkedlist, JSTaggedValue(16));
EXPECT_EQ(result, JSTaggedValue(16));
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 1));
// Remove index < (NODE_NUMBERS / 2)
result = LinkedListRemoveByIndex(linkedlist, JSTaggedValue(6));
EXPECT_EQ(result, JSTaggedValue(6));
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 2));
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveFirst)
{
constexpr uint32_t NODE_NUMBERS = 20;
@ -412,43 +307,6 @@ HWTEST_F_L0(ContainersLinkedListTest, RemoveFirst)
}
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveFirstAndInsertByIndex)
{
constexpr uint32_t NODE_NUMBERS = 20;
JSTaggedValue result = JSTaggedValue::Hole();
JSHandle<JSAPILinkedList> linkedlist = CreateJSAPILinkedList();
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(i), JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(i + 1));
}
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
callInfo->SetFunction(JSTaggedValue::Undefined());
callInfo->SetThis(linkedlist.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
JSTaggedValue rvalue = ContainersLinkedList::RemoveFirst(callInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_EQ(rvalue, JSTaggedValue(0));
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 1));
}
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
callInfo->SetFunction(JSTaggedValue::Undefined());
callInfo->SetThis(linkedlist.GetTaggedValue());
callInfo->SetCallArg(0, JSTaggedValue(15));
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
JSTaggedValue rvalue = ContainersLinkedList::RemoveFirstFound(callInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_EQ(rvalue, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 2));
}
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveLast)
{
constexpr uint32_t NODE_NUMBERS = 20;
@ -486,43 +344,6 @@ HWTEST_F_L0(ContainersLinkedListTest, RemoveLast)
}
}
HWTEST_F_L0(ContainersLinkedListTest, RemoveLastAndInsertByIndex)
{
constexpr uint32_t NODE_NUMBERS = 20;
JSTaggedValue result = JSTaggedValue::Hole();
JSHandle<JSAPILinkedList> linkedlist = CreateJSAPILinkedList();
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
result = LinkedListInsertByIndex(linkedlist, JSTaggedValue(i), JSTaggedValue(i));
EXPECT_EQ(result, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(i + 1));
}
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
callInfo->SetFunction(JSTaggedValue::Undefined());
callInfo->SetThis(linkedlist.GetTaggedValue());
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
JSTaggedValue rvalue = ContainersLinkedList::RemoveLast(callInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_EQ(rvalue, JSTaggedValue(19));
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 1));
}
{
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
callInfo->SetFunction(JSTaggedValue::Undefined());
callInfo->SetThis(linkedlist.GetTaggedValue());
callInfo->SetCallArg(0, JSTaggedValue(8));
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
JSTaggedValue rvalue = ContainersLinkedList::RemoveLastFound(callInfo);
TestHelper::TearDownFrame(thread, prev);
EXPECT_EQ(rvalue, JSTaggedValue::True());
EXPECT_EQ(linkedlist->Length(), static_cast<int>(NODE_NUMBERS - 2));
}
}
HWTEST_F_L0(ContainersLinkedListTest, Clear)
{
constexpr uint32_t NODE_NUMBERS = 8;
@ -702,7 +523,6 @@ HWTEST_F_L0(ContainersLinkedListTest, ProxyOfLength)
HWTEST_F_L0(ContainersLinkedListTest, ExceptionReturn1)
{
CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersLinkedList, Insert);
CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersLinkedList, InsertByIndex);
CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersLinkedList, Get);
CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersLinkedList, Add);
CONTAINERS_API_TYPE_MISMATCH_EXCEPTION_TEST(ContainersLinkedList, AddFirst);
@ -721,11 +541,6 @@ HWTEST_F_L0(ContainersLinkedListTest, ExceptionReturn1)
callInfo->SetThis(linkedlist.GetTaggedValue());
CONTAINERS_API_EXCEPTION_TEST(ContainersLinkedList, Insert, callInfo);
}
{
auto callInfo = NewEmptyCallInfo(thread);
callInfo->SetThis(linkedlist.GetTaggedValue());
CONTAINERS_API_EXCEPTION_TEST(ContainersLinkedList, InsertByIndex, callInfo);
}
{
auto callInfo = NewEmptyCallInfo(thread);
callInfo->SetThis(linkedlist.GetTaggedValue());

View File

@ -1187,7 +1187,6 @@ namespace panda::ecmascript {
V(LinkedList, GetFirst) \
V(LinkedList, GetLast) \
V(LinkedList, Insert) \
V(LinkedList, InsertByIndex) \
V(LinkedList, AddFirst) \
V(LinkedList, Clear) \
V(LinkedList, Clone) \

View File

@ -68,16 +68,6 @@ if (globalThis["ArkPrivate"] != undefined) {
}
map.set("test linkedlist insert:", res);
list.insertByIndex(777, 3);
testArray.splice(3, 0, 777);
res = true;
for(let i = 0; i < testArray.length; i++) {
if (list[i] !== testArray[i]) {
res = false;
}
}
map.set("test linkedlist insertByIndex", res);
list.set(5, 888);
testArray[5] = 888;
res = true;
@ -211,16 +201,6 @@ if (globalThis["ArkPrivate"] != undefined) {
}
map.set("test linkedlist insert:", res);
proxy.insertByIndex(777, 3);
testArray2.splice(3, 0, 777);
res = true;
for(let i = 0; i < testArray2.length; i++) {
if (proxy[i] !== testArray2[i]) {
res = false;
}
}
map.set("test linkedlist insertByIndex:", res);
proxy.set(5, 888);
testArray2[5] = 888;
res = true;
@ -426,15 +406,6 @@ if (globalThis["ArkPrivate"] != undefined) {
myList1.set(Math.floor(1.5), 888);
myList1.removeByIndex(Math.floor(1.5));
let myList2 = new LinkedList();
myList2.add(1);
myList2.add(2);
myList2.add(3);
myList2.insertByIndex(999, Math.floor(1.5));
myList2.get(Math.floor(1.5));
myList2.set(Math.floor(1.5), 888);
myList2.removeByIndex(Math.floor(1.5));
if (mList.getLast() != 3 ||
("convertToArray = " + mList.convertToArray()) != "convertToArray = 1,2,3") {
print("Test LinkedList fail!!!");