mirror of
https://github.com/openharmony/ark_js_runtime.git
synced 2026-08-27 10:41:15 -04:00
a725676a6c
Description Cut out most of the initialization logic in Runtime::Create and disconnect the inheritance relationship of most classes such as JSThread. Issue: #I53418: Runtime architecture adjustment Signed-off-by: lifansheng <lifansheng1@huawei.com> Change-Id: Ib9c1a40354f77d32e515e23334e7f4b4a4fa3259
268 lines
11 KiB
C++
268 lines
11 KiB
C++
/*
|
|
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "ecmascript/containers/containers_stack.h"
|
|
#include "ecmascript/containers/containers_private.h"
|
|
#include "ecmascript/ecma_runtime_call_info.h"
|
|
#include "ecmascript/global_env.h"
|
|
#include "ecmascript/js_api_stack.h"
|
|
#include "ecmascript/js_api_stack_iterator.h"
|
|
#include "ecmascript/js_handle.h"
|
|
#include "ecmascript/js_tagged_value-inl.h"
|
|
#include "ecmascript/js_thread.h"
|
|
#include "ecmascript/object_factory.h"
|
|
#include "ecmascript/tests/test_helper.h"
|
|
|
|
using namespace panda::ecmascript;
|
|
using namespace panda::ecmascript::containers;
|
|
|
|
namespace panda::test {
|
|
class ContainersStackTest : public testing::Test {
|
|
public:
|
|
static void SetUpTestCase()
|
|
{
|
|
GTEST_LOG_(INFO) << "SetUpTestCase";
|
|
}
|
|
|
|
static void TearDownTestCase()
|
|
{
|
|
GTEST_LOG_(INFO) << "TearDownCase";
|
|
}
|
|
|
|
void SetUp() override
|
|
{
|
|
TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
|
|
}
|
|
|
|
void TearDown() override
|
|
{
|
|
TestHelper::DestroyEcmaVMWithScope(instance, scope);
|
|
}
|
|
|
|
EcmaVM *instance {nullptr};
|
|
EcmaHandleScope *scope {nullptr};
|
|
JSThread *thread {nullptr};
|
|
|
|
class TestClass : public base::BuiltinsBase {
|
|
public:
|
|
static JSTaggedValue TestForEachFunc(EcmaRuntimeCallInfo *argv)
|
|
{
|
|
JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
|
|
JSHandle<JSTaggedValue> key = GetCallArg(argv, 1);
|
|
JSHandle<JSTaggedValue> stack = GetCallArg(argv, 2); // 2 means the secode arg
|
|
if (!stack->IsUndefined()) {
|
|
if (value->IsNumber()) {
|
|
TaggedArray *elements = TaggedArray::Cast(JSAPIStack::Cast(stack.GetTaggedValue().
|
|
GetTaggedObject())->GetElements().GetTaggedObject());
|
|
JSTaggedValue result = elements->Get(key->GetInt());
|
|
EXPECT_EQ(result, value.GetTaggedValue());
|
|
}
|
|
}
|
|
return JSTaggedValue::True();
|
|
}
|
|
};
|
|
protected:
|
|
JSTaggedValue InitializeStackConstructor()
|
|
{
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
|
|
|
JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
|
|
JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
|
|
JSHandle<JSTaggedValue> value =
|
|
JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
|
|
|
|
auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
|
|
objCallInfo->SetFunction(JSTaggedValue::Undefined());
|
|
objCallInfo->SetThis(value.GetTaggedValue());
|
|
objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::Stack)));
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo.get());
|
|
JSTaggedValue result = ContainersPrivate::Load(objCallInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
|
|
return result;
|
|
}
|
|
|
|
JSHandle<JSAPIStack> CreateJSAPIStack(JSTaggedValue compare = JSTaggedValue::Undefined())
|
|
{
|
|
JSHandle<JSTaggedValue> compareHandle(thread, compare);
|
|
JSHandle<JSFunction> newTarget(thread, InitializeStackConstructor());
|
|
auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
|
|
objCallInfo->SetFunction(newTarget.GetTaggedValue());
|
|
objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
|
|
objCallInfo->SetThis(JSTaggedValue::Undefined());
|
|
objCallInfo->SetCallArg(0, compareHandle.GetTaggedValue());
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo.get());
|
|
JSTaggedValue result = ContainersStack::StackConstructor(objCallInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
JSHandle<JSAPIStack> stack(thread, result);
|
|
return stack;
|
|
}
|
|
};
|
|
|
|
HWTEST_F_L0(ContainersStackTest, StackConstructor)
|
|
{
|
|
InitializeStackConstructor();
|
|
JSHandle<JSFunction> newTarget(thread, InitializeStackConstructor());
|
|
|
|
auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
|
|
objCallInfo->SetFunction(newTarget.GetTaggedValue());
|
|
objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
|
|
objCallInfo->SetThis(JSTaggedValue::Undefined());
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo.get());
|
|
JSTaggedValue result = ContainersStack::StackConstructor(objCallInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
|
|
ASSERT_TRUE(result.IsJSAPIStack());
|
|
JSHandle<JSAPIStack> stack(thread, result);
|
|
JSTaggedValue resultProto = JSTaggedValue::GetPrototype(thread, JSHandle<JSTaggedValue>(stack));
|
|
JSTaggedValue funcProto = newTarget->GetFunctionPrototype();
|
|
ASSERT_EQ(resultProto, funcProto);
|
|
}
|
|
|
|
HWTEST_F_L0(ContainersStackTest, PushAndPeek)
|
|
{
|
|
constexpr uint32_t NODE_NUMBERS = 8;
|
|
JSHandle<JSAPIStack> stack = CreateJSAPIStack();
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
JSTaggedValue result = ContainersStack::Push(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
EXPECT_EQ(result, JSTaggedValue(i));
|
|
EXPECT_EQ(ContainersStack::Peek(callInfo.get()), JSTaggedValue(i));
|
|
}
|
|
}
|
|
|
|
HWTEST_F_L0(ContainersStackTest, Pop)
|
|
{
|
|
constexpr uint32_t NODE_NUMBERS = 8;
|
|
JSHandle<JSAPIStack> stack = CreateJSAPIStack();
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
ContainersStack::Push(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
}
|
|
|
|
int num = 7;
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
JSTaggedValue result = ContainersStack::Pop(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
EXPECT_EQ(result, JSTaggedValue(num--));
|
|
}
|
|
}
|
|
|
|
HWTEST_F_L0(ContainersStackTest, IsEmpty)
|
|
{
|
|
constexpr uint32_t NODE_NUMBERS = 8;
|
|
JSHandle<JSAPIStack> stack = CreateJSAPIStack();
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
ContainersStack::Push(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
JSTaggedValue result = ContainersStack::IsEmpty(callInfo.get());
|
|
EXPECT_EQ(result, JSTaggedValue::False());
|
|
}
|
|
|
|
int num = 7;
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
JSTaggedValue result = ContainersStack::Pop(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
EXPECT_EQ(result, JSTaggedValue(num--));
|
|
if (num == -1) {
|
|
JSTaggedValue consequence = ContainersStack::IsEmpty(callInfo.get());
|
|
EXPECT_EQ(consequence, JSTaggedValue::True());
|
|
}
|
|
}
|
|
}
|
|
|
|
HWTEST_F_L0(ContainersStackTest, Locate)
|
|
{
|
|
constexpr uint32_t NODE_NUMBERS = 8;
|
|
JSHandle<JSAPIStack> stack = CreateJSAPIStack();
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
ContainersStack::Push(callInfo.get());
|
|
JSTaggedValue result = ContainersStack::Locate(callInfo.get());
|
|
EXPECT_EQ(result, JSTaggedValue(i));
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
}
|
|
}
|
|
|
|
HWTEST_F_L0(ContainersStackTest, ForEach)
|
|
{
|
|
constexpr uint32_t NODE_NUMBERS = 8;
|
|
JSHandle<JSAPIStack> stack = CreateJSAPIStack();
|
|
for (uint32_t i = 0; i < NODE_NUMBERS; i++) {
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, JSTaggedValue(i));
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
ContainersStack::Push(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
}
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
JSHandle<JSAPIStack> dlist = CreateJSAPIStack();
|
|
{
|
|
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
|
JSHandle<JSFunction> func = factory->NewJSFunction(env, reinterpret_cast<void *>(TestClass::TestForEachFunc));
|
|
auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
|
|
callInfo->SetFunction(JSTaggedValue::Undefined());
|
|
callInfo->SetThis(stack.GetTaggedValue());
|
|
callInfo->SetCallArg(0, func.GetTaggedValue());
|
|
callInfo->SetCallArg(1, dlist.GetTaggedValue());
|
|
|
|
[[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo.get());
|
|
ContainersStack::ForEach(callInfo.get());
|
|
TestHelper::TearDownFrame(thread, prev);
|
|
}
|
|
}
|
|
} // namespace panda::test
|