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