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/base/string_helper.h"
17#include "ecmascript/ecma_vm.h"
18#include "ecmascript/global_env.h"
19#include "ecmascript/interpreter/fast_runtime_stub-inl.h"
20#include "ecmascript/js_function.h"
21#include "ecmascript/js_thread.h"
22#include "ecmascript/platform/time.h"
23#include "ecmascript/tests/test_helper.h"
24
25using namespace panda::ecmascript;
26
27namespace panda::test {
28class BuiltinsLazyTest : public BaseTestWithScope<true> {
29};
30
31HWTEST_F_L0(BuiltinsLazyTest, SlowGetOwnProperty)
32{
33    EcmaVM *ecmaVM = thread->GetEcmaVM();
34    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
35    ObjectFactory *factory = ecmaVM->GetFactory();
36    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("Date"));
37    auto globalObj = JSHandle<JSObject>(thread, globalEnv->GetGlobalObject());
38    PropertyDescriptor desc(thread);
39    bool success = JSObject::GetOwnProperty(thread, globalObj, key, desc);
40    ASSERT_TRUE(success);
41    ASSERT_TRUE(desc.GetValue()->IsJSFunction());
42}
43
44HWTEST_F_L0(BuiltinsLazyTest, SlowSetProperty)
45{
46    EcmaVM *ecmaVM = thread->GetEcmaVM();
47    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
48    ObjectFactory *factory = ecmaVM->GetFactory();
49    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("Date"));
50    auto globalObj = JSHandle<JSTaggedValue>(thread, globalEnv->GetGlobalObject());
51    JSHandle<JSTaggedValue> value(factory->NewFromUtf8("Value"));
52    bool success = JSObject::SetProperty(thread, globalObj, key, value);
53    ASSERT_TRUE(success);
54    PropertyDescriptor desc(thread);
55    JSObject::GetOwnProperty(thread, JSHandle<JSObject>::Cast(globalObj), key, desc);
56    ASSERT_FALSE(desc.IsAccessorDescriptor());
57}
58
59HWTEST_F_L0(BuiltinsLazyTest, EnvGetDateConstructorTest)
60{
61    EcmaVM *ecmaVM = thread->GetEcmaVM();
62    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
63    JSHandle<JSTaggedValue> dateFunction = globalEnv->GetDateFunction();
64    ASSERT_TRUE(dateFunction->IsJSFunction());
65    ObjectFactory *factory = ecmaVM->GetFactory();
66    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("now"));
67    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, dateFunction, key));
68}
69
70HWTEST_F_L0(BuiltinsLazyTest, GlobalGetDateConstructorTest)
71{
72    EcmaVM *ecmaVM = thread->GetEcmaVM();
73    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
74    ObjectFactory *factory = ecmaVM->GetFactory();
75    auto key = factory->NewFromUtf8("Date");
76    auto globalObj = globalEnv->GetGlobalObject();
77    auto dateFunction = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key.GetTaggedValue());
78    ASSERT_TRUE(dateFunction.IsJSFunction());
79}
80
81HWTEST_F_L0(BuiltinsLazyTest, EnvGetSetConstructorTest)
82{
83    EcmaVM *ecmaVM = thread->GetEcmaVM();
84    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
85    JSHandle<JSTaggedValue> setFunction = globalEnv->GetBuiltinsSetFunction();
86    ASSERT_TRUE(setFunction->IsJSFunction());
87    ObjectFactory *factory = ecmaVM->GetFactory();
88    JSHandle<JSTaggedValue> setObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(setFunction),
89        setFunction));
90    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("add"));
91    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, setObject, key));
92}
93
94HWTEST_F_L0(BuiltinsLazyTest, GlobalGetSetConstructorTest)
95{
96    EcmaVM *ecmaVM = thread->GetEcmaVM();
97    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
98    ObjectFactory *factory = ecmaVM->GetFactory();
99    JSTaggedValue key = factory->NewFromUtf8("Set").GetTaggedValue();
100    auto globalObj = globalEnv->GetGlobalObject();
101    auto setFunction = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
102    ASSERT_TRUE(setFunction.IsJSFunction());
103}
104
105HWTEST_F_L0(BuiltinsLazyTest, EnvGetMapConstructorTest)
106{
107    EcmaVM *ecmaVM = thread->GetEcmaVM();
108    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
109    JSHandle<JSTaggedValue> mapFunction = globalEnv->GetBuiltinsMapFunction();
110    ASSERT_TRUE(mapFunction->IsJSFunction());
111    ObjectFactory *factory = ecmaVM->GetFactory();
112    JSHandle<JSTaggedValue> mapObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(mapFunction),
113        mapFunction));
114    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("clear"));
115    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, mapObject, key));
116}
117
118HWTEST_F_L0(BuiltinsLazyTest, GlobalGetMapConstructorTest)
119{
120    EcmaVM *ecmaVM = thread->GetEcmaVM();
121    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
122    ObjectFactory *factory = ecmaVM->GetFactory();
123    JSTaggedValue key = factory->NewFromUtf8("Map").GetTaggedValue();
124    auto globalObj = globalEnv->GetGlobalObject();
125    auto mapFunction = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
126    ASSERT_TRUE(mapFunction.IsJSFunction());
127}
128
129HWTEST_F_L0(BuiltinsLazyTest, EnvGetWeakMapConstructorTest)
130{
131    EcmaVM *ecmaVM = thread->GetEcmaVM();
132    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
133    JSHandle<JSTaggedValue> weakMapFunction = globalEnv->GetBuiltinsWeakMapFunction();
134    ASSERT_TRUE(weakMapFunction->IsJSFunction());
135    ObjectFactory *factory = ecmaVM->GetFactory();
136    JSHandle<JSTaggedValue> weakMapObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(weakMapFunction),
137        weakMapFunction));
138    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("delete"));
139    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, weakMapObject, key));
140}
141
142HWTEST_F_L0(BuiltinsLazyTest, GlobalGetWeakMapConstructorTest)
143{
144    EcmaVM *ecmaVM = thread->GetEcmaVM();
145    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
146    ObjectFactory *factory = ecmaVM->GetFactory();
147    JSTaggedValue key = factory->NewFromUtf8("WeakMap").GetTaggedValue();
148    auto globalObj = globalEnv->GetGlobalObject();
149    auto weakMapFunction = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
150    ASSERT_TRUE(weakMapFunction.IsJSFunction());
151}
152
153HWTEST_F_L0(BuiltinsLazyTest, EnvGetWeakSetConstructorTest)
154{
155    EcmaVM *ecmaVM = thread->GetEcmaVM();
156    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
157    JSHandle<JSTaggedValue> weakSetFunction = globalEnv->GetBuiltinsWeakSetFunction();
158    ASSERT_TRUE(weakSetFunction->IsJSFunction());
159    ObjectFactory *factory = ecmaVM->GetFactory();
160    JSHandle<JSTaggedValue> weakSetObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(weakSetFunction),
161        weakSetFunction));
162    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("delete"));
163    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, weakSetObject, key));
164}
165
166HWTEST_F_L0(BuiltinsLazyTest, GlobalGetWeakSetConstructorTest)
167{
168    EcmaVM *ecmaVM = thread->GetEcmaVM();
169    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
170    ObjectFactory *factory = ecmaVM->GetFactory();
171    JSTaggedValue key = factory->NewFromUtf8("WeakSet").GetTaggedValue();
172    auto globalObj = globalEnv->GetGlobalObject();
173    auto weakSetFunction = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
174    ASSERT_TRUE(weakSetFunction.IsJSFunction());
175}
176
177HWTEST_F_L0(BuiltinsLazyTest, EnvGetWeakRefConstructorTest)
178{
179    EcmaVM *ecmaVM = thread->GetEcmaVM();
180    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
181    JSHandle<JSTaggedValue> weakRefFunction = globalEnv->GetBuiltinsWeakRefFunction();
182    ASSERT_TRUE(weakRefFunction->IsJSFunction());
183    ObjectFactory *factory = ecmaVM->GetFactory();
184    JSHandle<JSTaggedValue> weakRefObject(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(weakRefFunction),
185        weakRefFunction));
186    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("deref"));
187    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, weakRefObject, key));
188}
189
190HWTEST_F_L0(BuiltinsLazyTest, GlobalGetWeakRefConstructorTest)
191{
192    EcmaVM *ecmaVM = thread->GetEcmaVM();
193    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
194    ObjectFactory *factory = ecmaVM->GetFactory();
195    JSTaggedValue key = factory->NewFromUtf8("WeakRef").GetTaggedValue();
196    auto globalObj = globalEnv->GetGlobalObject();
197    auto weakRefFunction = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
198    ASSERT_TRUE(weakRefFunction.IsJSFunction());
199}
200
201HWTEST_F_L0(BuiltinsLazyTest, EnvGetFinalizationRegistryConstructorTest)
202{
203    EcmaVM *ecmaVM = thread->GetEcmaVM();
204    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
205    JSHandle<JSTaggedValue> function = globalEnv->GetBuiltinsFinalizationRegistryFunction();
206    ASSERT_TRUE(function->IsJSFunction());
207    ObjectFactory *factory = ecmaVM->GetFactory();
208    JSHandle<JSTaggedValue> object(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(function), function));
209    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("register"));
210    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, object, key));
211}
212
213HWTEST_F_L0(BuiltinsLazyTest, GlobalGetFinalizationRegistryConstructorTest)
214{
215    EcmaVM *ecmaVM = thread->GetEcmaVM();
216    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
217    ObjectFactory *factory = ecmaVM->GetFactory();
218    JSTaggedValue key = factory->NewFromUtf8("FinalizationRegistry").GetTaggedValue();
219    auto globalObj = globalEnv->GetGlobalObject();
220    auto function = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
221    ASSERT_TRUE(function.IsJSFunction());
222}
223
224HWTEST_F_L0(BuiltinsLazyTest, EnvGetInt8ArrayConstructorTest)
225{
226    EcmaVM *ecmaVM = thread->GetEcmaVM();
227    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
228    JSHandle<JSTaggedValue> function = globalEnv->GetInt8ArrayFunction();
229    ASSERT_TRUE(function->IsJSFunction());
230    ObjectFactory *factory = ecmaVM->GetFactory();
231    JSHandle<JSTaggedValue> object(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(function), function));
232    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("BYTES_PER_ELEMENT"));
233    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, object, key));
234}
235
236HWTEST_F_L0(BuiltinsLazyTest, GlobalGetInt8ArrayConstructorTest)
237{
238    EcmaVM *ecmaVM = thread->GetEcmaVM();
239    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
240    ObjectFactory *factory = ecmaVM->GetFactory();
241    JSTaggedValue key = factory->NewFromUtf8("Int8Array").GetTaggedValue();
242    auto globalObj = globalEnv->GetGlobalObject();
243    auto function = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
244    ASSERT_TRUE(function.IsJSFunction());
245}
246
247HWTEST_F_L0(BuiltinsLazyTest, EnvGetArrayBufferConstructorTest)
248{
249    EcmaVM *ecmaVM = thread->GetEcmaVM();
250    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
251    JSHandle<JSTaggedValue> function = globalEnv->GetArrayBufferFunction();
252    ASSERT_TRUE(function->IsJSFunction());
253    ObjectFactory *factory = ecmaVM->GetFactory();
254    JSHandle<JSTaggedValue> object(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(function), function));
255    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("slice"));
256    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, object, key));
257}
258
259HWTEST_F_L0(BuiltinsLazyTest, GlobalGetArrayBufferConstructorTest)
260{
261    EcmaVM *ecmaVM = thread->GetEcmaVM();
262    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
263    ObjectFactory *factory = ecmaVM->GetFactory();
264    JSTaggedValue key = factory->NewFromUtf8("ArrayBuffer").GetTaggedValue();
265    auto globalObj = globalEnv->GetGlobalObject();
266    auto function = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
267    ASSERT_TRUE(function.IsJSFunction());
268}
269
270HWTEST_F_L0(BuiltinsLazyTest, EnvGetDataViewConstructorTest)
271{
272    EcmaVM *ecmaVM = thread->GetEcmaVM();
273    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
274    JSHandle<JSTaggedValue> function = globalEnv->GetDataViewFunction();
275    ASSERT_TRUE(function->IsJSFunction());
276    ObjectFactory *factory = ecmaVM->GetFactory();
277    JSHandle<JSTaggedValue> object(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(function), function));
278    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("getFloat32"));
279    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, object, key));
280}
281
282HWTEST_F_L0(BuiltinsLazyTest, GlobalGetDataViewConstructorTest)
283{
284    EcmaVM *ecmaVM = thread->GetEcmaVM();
285    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
286    ObjectFactory *factory = ecmaVM->GetFactory();
287    JSTaggedValue key = factory->NewFromUtf8("DataView").GetTaggedValue();
288    auto globalObj = globalEnv->GetGlobalObject();
289    auto function = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
290    ASSERT_TRUE(function.IsJSFunction());
291}
292
293HWTEST_F_L0(BuiltinsLazyTest, EnvGetSharedArrayBufferConstructorTest)
294{
295    EcmaVM *ecmaVM = thread->GetEcmaVM();
296    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
297    JSHandle<JSTaggedValue> function = globalEnv->GetSharedArrayBufferFunction();
298    ASSERT_TRUE(function->IsJSFunction());
299    ObjectFactory *factory = ecmaVM->GetFactory();
300    JSHandle<JSTaggedValue> object(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(function), function));
301    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("slice"));
302    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, object, key));
303}
304
305HWTEST_F_L0(BuiltinsLazyTest, GlobalGetSharedArrayBufferConstructorTest)
306{
307    EcmaVM *ecmaVM = thread->GetEcmaVM();
308    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
309    ObjectFactory *factory = ecmaVM->GetFactory();
310    JSTaggedValue key = factory->NewFromUtf8("SharedArrayBuffer").GetTaggedValue();
311    auto globalObj = globalEnv->GetGlobalObject();
312    auto function = FastRuntimeStub::GetGlobalOwnProperty(thread, globalObj, key);
313    ASSERT_TRUE(function.IsJSFunction());
314}
315
316HWTEST_F_L0(BuiltinsLazyTest, EnvGetLocaleConstructorTest)
317{
318    EcmaVM *ecmaVM = thread->GetEcmaVM();
319    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
320    JSHandle<JSTaggedValue> function = globalEnv->GetLocaleFunction();
321    ASSERT_TRUE(function->IsJSFunction());
322    ObjectFactory *factory = ecmaVM->GetFactory();
323    JSHandle<JSTaggedValue> object(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(function), function));
324    JSHandle<JSTaggedValue> key(factory->NewFromUtf8("maximize"));
325    ASSERT_TRUE(JSTaggedValue::HasProperty(thread, object, key));
326}
327
328HWTEST_F_L0(BuiltinsLazyTest, IntlGetLocaleFunctionConstructorTest)
329{
330    EcmaVM *ecmaVM = thread->GetEcmaVM();
331    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
332    ObjectFactory *factory = ecmaVM->GetFactory();
333    JSTaggedValue key = factory->NewFromUtf8("Locale").GetTaggedValue();
334    auto intlObj = globalEnv->GetIntlFunction().GetTaggedValue();
335    auto function = FastRuntimeStub::GetPropertyByName(thread, intlObj, key);
336    ASSERT_TRUE(function.IsJSFunction());
337}
338}  // namespace panda::test
339