• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/ecma_context.h"
17 #include "ecmascript/ecma_string_table.h"
18 #include "ecmascript/object_factory.h"
19 #include "ecmascript/tests/test_helper.h"
20 #include "ecmascript/global_env.h"
21 
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::base;
24 namespace panda::test {
25 class EcmaContextTest : public BaseTestWithScope<false> {
26 };
27 
HWTEST_F_L0(EcmaContextTest,Create)28 HWTEST_F_L0(EcmaContextTest, Create)
29 {
30     auto context = EcmaContext::CreateAndInitialize(thread);
31     thread->SwitchCurrentContext(context);
32     CVector<EcmaContext *> Cv1 = thread->GetEcmaContexts();
33     EXPECT_EQ(Cv1.size(), 2);  // 2: size of contexts.
34     auto context2 = EcmaContext::CreateAndInitialize(thread);
35     thread->SwitchCurrentContext(context2);
36     Cv1 = thread->GetEcmaContexts();
37     EXPECT_EQ(Cv1.size(), 3);  // 3: size of contexts.
38     EcmaContext::CheckAndDestroy(thread, context);
39     Cv1 = thread->GetEcmaContexts();
40     EXPECT_EQ(Cv1.size(), 2);  // 2: size of contexts.
41     EcmaContext::CheckAndDestroy(thread, context2);
42 }
43 
HWTEST_F_L0(EcmaContextTest,GetRegExpCache)44 HWTEST_F_L0(EcmaContextTest, GetRegExpCache)
45 {
46     EcmaVM *vm = thread->GetEcmaVM();
47     ObjectFactory *factory = vm->GetFactory();
48     auto context = EcmaContext::CreateAndInitialize(thread);
49     thread->SwitchCurrentContext(context);
50     JSHandle<EcmaString> regexp = factory->NewFromASCII("\\g");
51     JSHandle<JSTaggedValue> value2(regexp);
52     context->SetRegExpCache(value2.GetTaggedValue());
53     JSHandle<JSTaggedValue> res2 = context->GetRegExpCache();
54     EXPECT_EQ(res2.GetTaggedValue(), value2.GetTaggedValue());
55     EcmaContext::CheckAndDestroy(thread, context);
56 }
57 
HWTEST_F_L0(EcmaContextTest,AllowAtomicWait)58 HWTEST_F_L0(EcmaContextTest, AllowAtomicWait)
59 {
60     auto context = EcmaContext::CreateAndInitialize(thread);
61     thread->SwitchCurrentContext(context);
62     bool value = context->GetAllowAtomicWait();
63     EXPECT_TRUE(value);
64     context->SetAllowAtomicWait(false);
65     bool value2 = context->GetAllowAtomicWait();
66     EXPECT_FALSE(value2);
67     EcmaContext::CheckAndDestroy(thread, context);
68 }
69 
HWTEST_F_L0(EcmaContextTest,SwitchCurrentContext)70 HWTEST_F_L0(EcmaContextTest, SwitchCurrentContext)
71 {
72     auto context = EcmaContext::CreateAndInitialize(thread);
73     auto context1 = EcmaContext::CreateAndInitialize(thread);
74 
75     CVector<EcmaContext *> contextVector = thread->GetEcmaContexts();
76     EXPECT_EQ(contextVector.size(), 3);  // 3: size of contexts.
77 
78     thread->SwitchCurrentContext(context);
79 
80     EcmaContext::CheckAndDestroy(thread, context);
81     contextVector = thread->GetEcmaContexts();
82     EXPECT_EQ(contextVector.size(), 2);  // 3: size of contexts.
83     EcmaContext::CheckAndDestroy(thread, context1);
84 }
85 }  // namespace panda::test