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_vm.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_handle.h"
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/tests/test_helper.h"
21
22 using namespace panda::ecmascript;
23
24 namespace panda::test {
25 class BuiltinsSharedObjectTest : public testing::Test {
26 public:
SetUpTestCase()27 static void SetUpTestCase()
28 {
29 GTEST_LOG_(INFO) << "SetUpTestCase";
30 }
31
TearDownTestCase()32 static void TearDownTestCase()
33 {
34 GTEST_LOG_(INFO) << "TearDownCase";
35 }
36
SetUp()37 void SetUp() override
38 {
39 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
40 }
41
TearDown()42 void TearDown() override
43 {
44 TestHelper::DestroyEcmaVMWithScope(instance, scope);
45 }
46
47 EcmaVM *instance {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
HWTEST_F_L0(BuiltinsSharedObjectTest,SharedObject)52 HWTEST_F_L0(BuiltinsSharedObjectTest, SharedObject)
53 {
54 DISALLOW_GARBAGE_COLLECTION;
55 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
56 auto globalConst = thread->GlobalConstants();
57 auto sharedObjectPrototype = env->GetSObjectFunctionPrototype();
58 JSHClass *hclass = sharedObjectPrototype->GetTaggedObject()->GetClass();
59 ASSERT_FALSE(hclass->IsExtensible());
60 ASSERT_FALSE(hclass->IsConstructor());
61 ASSERT_FALSE(hclass->IsCallable());
62 ASSERT_TRUE(hclass->IsJSSharedObject());
63 ASSERT_TRUE(hclass->GetProto().IsNull());
64
65 // SharedObject
66 PropertyDescriptor desc(thread);
67 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
68 JSObject::GetOwnProperty(thread, JSHandle<JSObject>(sharedObjectPrototype), constructorKey, desc);
69 auto ctor = desc.GetValue();
70 ASSERT_EQ(ctor.GetTaggedValue(), env->GetSObjectFunction().GetTaggedValue());
71 JSHClass *ctorHClass = ctor->GetTaggedObject()->GetClass();
72 ASSERT_FALSE(ctorHClass->IsExtensible());
73 ASSERT_TRUE(ctorHClass->IsConstructor());
74 ASSERT_TRUE(ctorHClass->IsCallable());
75 ASSERT_TRUE(ctorHClass->IsJSSharedFunction());
76
77 // SharedFunction.prototype
78 auto proto = ctorHClass->GetProto();
79 ASSERT_EQ(proto, env->GetSFunctionPrototype().GetTaggedValue());
80 ASSERT_TRUE(proto.IsJSSharedFunction());
81 JSHClass *protoHClass = proto.GetTaggedObject()->GetClass();
82 ASSERT_FALSE(protoHClass->IsExtensible());
83 ASSERT_FALSE(protoHClass->IsConstructor());
84 ASSERT_TRUE(protoHClass->IsCallable());
85
86 // SharedObject.prototype
87 auto sObjProto = protoHClass->GetProto();
88 ASSERT_EQ(sObjProto, sharedObjectPrototype.GetTaggedValue());
89 ASSERT_TRUE(sObjProto.IsJSSharedObject());
90 JSHClass *sObjProtoHClass = sObjProto.GetTaggedObject()->GetClass();
91 ASSERT_FALSE(sObjProtoHClass->IsExtensible());
92 ASSERT_FALSE(sObjProtoHClass->IsConstructor());
93 ASSERT_FALSE(sObjProtoHClass->IsCallable());
94 }
95
HWTEST_F_L0(BuiltinsSharedObjectTest,SharedFunction)96 HWTEST_F_L0(BuiltinsSharedObjectTest, SharedFunction)
97 {
98 DISALLOW_GARBAGE_COLLECTION;
99 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
100 auto globalConst = thread->GlobalConstants();
101 auto sFunctionPrototype = env->GetSFunctionPrototype();
102 // SharedFunction
103 PropertyDescriptor desc(thread);
104 JSHandle<JSTaggedValue> constructorKey = globalConst->GetHandledConstructorString();
105 JSObject::GetOwnProperty(thread, JSHandle<JSObject>(sFunctionPrototype), constructorKey, desc);
106 auto ctor = desc.GetValue();
107 ASSERT_EQ(ctor.GetTaggedValue(), env->GetSFunctionFunction().GetTaggedValue());
108 JSHClass *ctorHClass = ctor->GetTaggedObject()->GetClass();
109 ASSERT_FALSE(ctorHClass->IsExtensible());
110 ASSERT_TRUE(ctorHClass->IsConstructor());
111 ASSERT_TRUE(ctorHClass->IsCallable());
112 ASSERT_TRUE(ctorHClass->IsJSSharedFunction());
113
114 JSHandle<JSHClass> normalFunctionClass(env->GetSFunctionClassWithoutProto());
115 ASSERT_FALSE(normalFunctionClass->IsExtensible());
116 ASSERT_FALSE(normalFunctionClass->IsConstructor());
117 ASSERT_TRUE(ctorHClass->IsCallable());
118 ASSERT_TRUE(ctorHClass->IsJSSharedFunction());
119 }
120
HWTEST_F_L0(BuiltinsSharedObjectTest,SharedBuiltinsMethod)121 HWTEST_F_L0(BuiltinsSharedObjectTest, SharedBuiltinsMethod)
122 {
123 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
124 auto factory = thread->GetEcmaVM()->GetFactory();
125 auto sFunctionPrototype = env->GetSFunctionPrototype();
126 auto callStr = factory->NewFromASCII("call");
127 PropertyDescriptor desc(thread);
128 JSObject::GetOwnProperty(thread, JSHandle<JSObject>(sFunctionPrototype), JSHandle<JSTaggedValue>(callStr), desc);
129 auto method = desc.GetValue();
130 JSHClass *hclass = method->GetTaggedObject()->GetClass();
131 ASSERT_FALSE(hclass->IsExtensible());
132 ASSERT_TRUE(!hclass->IsConstructor());
133 ASSERT_TRUE(hclass->IsCallable());
134 ASSERT_TRUE(hclass->IsJSSharedFunction());
135 }
136 } // namespace panda::test
137