1 /*
2 * Copyright (c) 2021 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/tests/test_helper.h"
17
18 #include "ecmascript/base/builtins_base.h"
19 #include "ecmascript/builtins.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_handle.h"
23 #include "ecmascript/js_object-inl.h"
24 #include "ecmascript/js_thread.h"
25 #include "thread_manager.h"
26
27 using namespace panda::ecmascript;
28 using namespace panda::ecmascript::base;
29
30 namespace panda::test {
31 class BuiltinsTest : public testing::Test {
32 public:
SetUpTestCase()33 static void SetUpTestCase()
34 {
35 GTEST_LOG_(INFO) << "SetUpTestCase";
36 }
37
TearDownTestCase()38 static void TearDownTestCase()
39 {
40 GTEST_LOG_(INFO) << "TearDownCase";
41 }
42
SetUp()43 void SetUp() override
44 {
45 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
46 }
47
TearDown()48 void TearDown() override
49 {
50 TestHelper::DestroyEcmaVMWithScope(instance, scope);
51 }
52
53 PandaVM *instance {nullptr};
54 EcmaHandleScope *scope {nullptr};
55 JSThread *thread {nullptr};
56 };
57
HWTEST_F_L0(BuiltinsTest,ObjectInit)58 HWTEST_F_L0(BuiltinsTest, ObjectInit)
59 {
60 auto ecmaVM = thread->GetEcmaVM();
61 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
62
63 JSHandle<JSFunction> objectFunction(env->GetObjectFunction());
64 ASSERT_NE(*objectFunction, nullptr);
65 }
66
HWTEST_F_L0(BuiltinsTest,FunctionInit)67 HWTEST_F_L0(BuiltinsTest, FunctionInit)
68 {
69 auto ecmaVM = thread->GetEcmaVM();
70 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
71
72 JSHandle<JSFunction> functionFunction(env->GetFunctionFunction());
73 ASSERT_NE(*functionFunction, nullptr);
74 }
75
HWTEST_F_L0(BuiltinsTest,NumberInit)76 HWTEST_F_L0(BuiltinsTest, NumberInit)
77 {
78 auto ecmaVM = thread->GetEcmaVM();
79 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
80
81 JSHandle<JSFunction> numberFunction(env->GetNumberFunction());
82 ASSERT_NE(*numberFunction, nullptr);
83 }
84
HWTEST_F_L0(BuiltinsTest,SetInit)85 HWTEST_F_L0(BuiltinsTest, SetInit)
86 {
87 auto ecmaVM = thread->GetEcmaVM();
88 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
89
90 JSHandle<JSFunction> setFunction(env->GetBuiltinsSetFunction());
91 ASSERT_NE(*setFunction, nullptr);
92 }
93
HWTEST_F_L0(BuiltinsTest,MapInit)94 HWTEST_F_L0(BuiltinsTest, MapInit)
95 {
96 auto ecmaVM = thread->GetEcmaVM();
97 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
98
99 JSHandle<JSFunction> mapFunction(env->GetBuiltinsMapFunction());
100 ASSERT_NE(*mapFunction, nullptr);
101 }
102
HWTEST_F_L0(BuiltinsTest,StrictModeForbiddenAccess)103 HWTEST_F_L0(BuiltinsTest, StrictModeForbiddenAccess)
104 {
105 auto ecmaVM = thread->GetEcmaVM();
106 JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
107
108 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
109
110 JSHandle<JSFunction> function = factory->NewJSFunction(env, static_cast<void *>(nullptr));
111
112 JSHandle<JSTaggedValue> callerKey(factory->NewFromCanBeCompressString("caller"));
113 JSHandle<JSTaggedValue> argumentsKey(factory->NewFromCanBeCompressString("arguments"));
114
115 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(function), callerKey);
116 ASSERT_EQ(thread->HasPendingException(), true);
117
118 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(function), argumentsKey);
119 ASSERT_EQ(thread->HasPendingException(), true);
120 }
121 } // namespace panda::test
122