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/builtins/builtins_reflect.h"
17
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/ecma_string.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_array.h"
23 #include "ecmascript/js_function.h"
24 #include "ecmascript/js_object-inl.h"
25 #include "ecmascript/js_primitive_ref.h"
26 #include "ecmascript/tagged_array-inl.h"
27 #include "ecmascript/object_factory.h"
28 #include "ecmascript/tests/test_helper.h"
29
30 using namespace panda::ecmascript;
31 using namespace panda::ecmascript::builtins;
32 using BuiltinsBase = panda::ecmascript::base::BuiltinsBase;
33 using JSArray = panda::ecmascript::JSArray;
34
35 namespace panda::test {
36 class BuiltinsReflectTest : public testing::Test {
37 public:
SetUpTestCase()38 static void SetUpTestCase()
39 {
40 GTEST_LOG_(INFO) << "SetUpTestCase";
41 }
42
TearDownTestCase()43 static void TearDownTestCase()
44 {
45 GTEST_LOG_(INFO) << "TearDownCase";
46 }
47
SetUp()48 void SetUp() override
49 {
50 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
51 }
52
TearDown()53 void TearDown() override
54 {
55 TestHelper::DestroyEcmaVMWithScope(instance, scope);
56 }
57
58 EcmaVM *instance {nullptr};
59 EcmaHandleScope *scope {nullptr};
60 JSThread *thread {nullptr};
61 };
62
63 // use for create a JSObject of test
TestObjectCreate(JSThread * thread)64 static JSHandle<JSFunction> TestObjectCreate(JSThread *thread)
65 {
66 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
67 return JSHandle<JSFunction>::Cast(env->GetObjectFunction());
68 }
69
70 // native function for test Reflect.apply
TestReflectApply(EcmaRuntimeCallInfo * argv)71 JSTaggedValue TestReflectApply(EcmaRuntimeCallInfo *argv)
72 {
73 auto thread = argv->GetThread();
74 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
75
76 int result = 0;
77 for (uint32_t index = 0; index < argv->GetArgsNumber(); ++index) {
78 result += BuiltinsBase::GetCallArg(argv, index).GetTaggedValue().GetInt();
79 }
80 JSHandle<JSTaggedValue> thisValue = BuiltinsBase::GetThis(argv);
81
82 JSTaggedValue testA = JSObject::GetProperty(thread, thisValue,
83 JSHandle<JSTaggedValue>(factory->NewFromASCII("test_reflect_apply_a"))).GetValue().GetTaggedValue();
84 JSTaggedValue testB = JSObject::GetProperty(thread, thisValue,
85 JSHandle<JSTaggedValue>(factory->NewFromASCII("test_reflect_apply_b"))).GetValue().GetTaggedValue();
86
87 result = result + testA.GetInt() + testB.GetInt();
88 return BuiltinsBase::GetTaggedInt(result);
89 }
90
91 // Reflect.apply (target, thisArgument, argumentsList)
HWTEST_F_L0(BuiltinsReflectTest,ReflectApply)92 HWTEST_F_L0(BuiltinsReflectTest, ReflectApply)
93 {
94 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
95
96 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
97
98 // target
99 JSHandle<JSFunction> target = factory->NewJSFunction(env, reinterpret_cast<void *>(TestReflectApply));
100 // thisArgument
101 JSHandle<JSObject> thisArgument =
102 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
103 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArgument),
104 JSHandle<JSTaggedValue>(factory->NewFromASCII("test_reflect_apply_a")),
105 JSHandle<JSTaggedValue>(thread, JSTaggedValue(11)));
106 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(thisArgument),
107 JSHandle<JSTaggedValue>(factory->NewFromASCII("test_reflect_apply_b")),
108 JSHandle<JSTaggedValue>(thread, JSTaggedValue(22)));
109 // argumentsList
110 JSHandle<JSObject> argumentsList(JSArray::ArrayCreate(thread, JSTaggedNumber(2)));
111 PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(33)));
112 JSArray::DefineOwnProperty(thread, argumentsList, JSHandle<JSTaggedValue>(thread, JSTaggedValue(0)), desc);
113
114 PropertyDescriptor desc1(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(44)));
115 JSArray::DefineOwnProperty(thread, argumentsList, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)), desc1);
116
117 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
118 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
119 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
120 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
121 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(*thisArgument));
122 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(*argumentsList));
123
124 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
125 JSTaggedValue result = BuiltinsReflect::ReflectApply(ecmaRuntimeCallInfo);
126 ASSERT_EQ(result.GetRawData(), JSTaggedValue(110).GetRawData());
127
128 JSObject::DeleteProperty(thread, (thisArgument),
129 JSHandle<JSTaggedValue>(factory->NewFromASCII("test_reflect_apply_a")));
130 JSObject::DeleteProperty(thread, (thisArgument),
131 JSHandle<JSTaggedValue>(factory->NewFromASCII("test_reflect_apply_b")));
132 TestHelper::TearDownFrame(thread, prev);
133 }
134
135 // Reflect.construct (target, argumentsList [ , newTarget])
HWTEST_F_L0(BuiltinsReflectTest,ReflectConstruct)136 HWTEST_F_L0(BuiltinsReflectTest, ReflectConstruct)
137 {
138 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
139
140 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
141
142 // target
143 JSHandle<JSFunction> target = JSHandle<JSFunction>::Cast(env->GetStringFunction());
144 // argumentsList
145 JSHandle<JSObject> argumentsList(JSArray::ArrayCreate(thread, JSTaggedNumber(1)));
146 PropertyDescriptor desc(thread,
147 JSHandle<JSTaggedValue>::Cast(factory->NewFromASCII("ReflectConstruct")));
148 JSArray::DefineOwnProperty(thread, argumentsList, JSHandle<JSTaggedValue>(thread, JSTaggedValue(0)), desc);
149
150 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
151 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
152 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
153 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
154 ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(*argumentsList));
155
156 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
157 JSTaggedValue result = BuiltinsReflect::ReflectConstruct(ecmaRuntimeCallInfo);
158
159 ASSERT_TRUE(result.IsECMAObject());
160 JSHandle<JSTaggedValue> taggedResult(thread, result);
161 JSHandle<JSPrimitiveRef> refResult = JSHandle<JSPrimitiveRef>::Cast(taggedResult);
162 JSHandle<EcmaString> ruler = factory->NewFromASCII("ReflectConstruct");
163 ASSERT_EQ(EcmaStringAccessor::Compare(instance,
164 JSHandle<EcmaString>(thread, EcmaString::Cast(refResult->GetValue())), ruler), 0);
165 TestHelper::TearDownFrame(thread, prev);
166 }
167
168 // Reflect.defineProperty (target, propertyKey, attributes)
HWTEST_F_L0(BuiltinsReflectTest,ReflectDefineProperty)169 HWTEST_F_L0(BuiltinsReflectTest, ReflectDefineProperty)
170 {
171 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
172
173 // target
174 JSHandle<JSObject> target =
175 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
176 // propertyKey
177 JSHandle<JSTaggedValue> key(factory->NewFromASCII("test_reflect_define_property"));
178 // attributes
179 JSHandle<JSObject> attributes =
180 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
181 // attributes value
182 auto globalConst = thread->GlobalConstants();
183 JSHandle<JSTaggedValue> valueKey = globalConst->GetHandledValueString();
184 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(100));
185 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(attributes), valueKey, value);
186 // attributes writable
187 JSHandle<JSTaggedValue> writableKey = globalConst->GetHandledWritableString();
188 JSHandle<JSTaggedValue> writable(thread, JSTaggedValue::True());
189 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(attributes), writableKey, writable);
190 // attributes enumerable
191 JSHandle<JSTaggedValue> enumerableKey = globalConst->GetHandledEnumerableString();
192 JSHandle<JSTaggedValue> enumerable(thread, JSTaggedValue::False());
193 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(attributes), enumerableKey, enumerable);
194 // attributes configurable
195 JSHandle<JSTaggedValue> configurableKey = globalConst->GetHandledConfigurableString();
196 JSHandle<JSTaggedValue> configurable(thread, JSTaggedValue::True());
197 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(attributes), configurableKey, configurable);
198
199 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
200 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
201 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
202 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
203 ecmaRuntimeCallInfo->SetCallArg(1, key.GetTaggedValue());
204 ecmaRuntimeCallInfo->SetCallArg(2, JSTaggedValue(*attributes));
205
206 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
207 JSTaggedValue result = BuiltinsReflect::ReflectDefineProperty(ecmaRuntimeCallInfo);
208 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
209
210 PropertyDescriptor descRuler(thread);
211 JSObject::GetOwnProperty(thread, target, key, descRuler);
212 ASSERT_EQ(descRuler.GetValue()->GetInt(), 100);
213 ASSERT_EQ(descRuler.IsWritable(), true);
214 ASSERT_EQ(descRuler.IsEnumerable(), false);
215 ASSERT_EQ(descRuler.IsConfigurable(), true);
216 TestHelper::TearDownFrame(thread, prev);
217 }
218
219 // Reflect.deleteProperty (target, propertyKey)
HWTEST_F_L0(BuiltinsReflectTest,ReflectDeleteProperty)220 HWTEST_F_L0(BuiltinsReflectTest, ReflectDeleteProperty)
221 {
222 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
223
224 // target
225 JSHandle<JSObject> target =
226 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
227 // propertyKey
228 JSHandle<JSTaggedValue> key(factory->NewFromASCII("test_reflect_delete_property"));
229 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(101));
230 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(target), key, value);
231
232 PropertyDescriptor desc(thread);
233 ASSERT_EQ(JSObject::GetOwnProperty(thread, target, key, desc), true);
234
235 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
236 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
237 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
238 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
239 ecmaRuntimeCallInfo->SetCallArg(1, key.GetTaggedValue());
240
241 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
242 JSTaggedValue result = BuiltinsReflect::ReflectDeleteProperty(ecmaRuntimeCallInfo);
243 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
244 ASSERT_EQ(JSObject::GetOwnProperty(thread, target, key, desc), false);
245 TestHelper::TearDownFrame(thread, prev);
246 }
247
248 // Reflect.get (target, propertyKey [ , receiver])
HWTEST_F_L0(BuiltinsReflectTest,ReflectGet)249 HWTEST_F_L0(BuiltinsReflectTest, ReflectGet)
250 {
251 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
252
253 // target
254 JSHandle<JSObject> target =
255 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
256 // propertyKey
257 JSHandle<JSTaggedValue> key(factory->NewFromASCII("test_reflect_get"));
258 // set property
259 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(101.5));
260 JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(target), key, value);
261
262 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
263 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
264 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
265 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
266 ecmaRuntimeCallInfo->SetCallArg(1, key.GetTaggedValue());
267
268 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
269 JSTaggedValue result = BuiltinsReflect::ReflectGet(ecmaRuntimeCallInfo);
270
271 JSHandle<JSTaggedValue> resultValue(thread, result);
272 ASSERT_EQ(resultValue->GetDouble(), 101.5);
273 TestHelper::TearDownFrame(thread, prev);
274 }
275
276 // Reflect.getOwnPropertyDescriptor ( target, propertyKey )
HWTEST_F_L0(BuiltinsReflectTest,ReflectGetOwnPropertyDescriptor)277 HWTEST_F_L0(BuiltinsReflectTest, ReflectGetOwnPropertyDescriptor)
278 {
279 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
280
281 // target
282 JSHandle<JSObject> target =
283 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
284 // propertyKey
285 JSHandle<JSTaggedValue> key(factory->NewFromASCII("test_reflect_get_property_descriptor"));
286 PropertyDescriptor desc(thread, JSHandle<JSTaggedValue>(thread, JSTaggedValue(102)), true, false, true);
287 ASSERT_EQ(JSTaggedValue::DefinePropertyOrThrow(thread, JSHandle<JSTaggedValue>(target), key, desc), true);
288
289 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
290 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
291 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
292 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
293 ecmaRuntimeCallInfo->SetCallArg(1, key.GetTaggedValue());
294
295 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
296 JSTaggedValue result = BuiltinsReflect::ReflectGetOwnPropertyDescriptor(ecmaRuntimeCallInfo);
297
298 ASSERT_TRUE(result.IsECMAObject());
299
300 JSHandle<JSTaggedValue> resultObj(thread, result);
301 // test value
302 auto globalConst = thread->GlobalConstants();
303 JSHandle<JSTaggedValue> valueKey = globalConst->GetHandledValueString();
304 JSHandle<JSTaggedValue> resultValue = JSObject::GetProperty(thread, resultObj, valueKey).GetValue();
305 ASSERT_EQ(resultValue->GetInt(), 102);
306 // test writable
307 JSHandle<JSTaggedValue> writableKey = globalConst->GetHandledWritableString();
308 JSHandle<JSTaggedValue> resultWritable = JSObject::GetProperty(thread, resultObj, writableKey).GetValue();
309 ASSERT_EQ(resultWritable->ToBoolean(), true);
310 // test enumerable
311 JSHandle<JSTaggedValue> enumerableKey = globalConst->GetHandledEnumerableString();
312 JSHandle<JSTaggedValue> resultEnumerable = JSObject::GetProperty(thread, resultObj, enumerableKey).GetValue();
313 ASSERT_EQ(resultEnumerable->ToBoolean(), false);
314 // test configurable
315 JSHandle<JSTaggedValue> configurableKey = globalConst->GetHandledConfigurableString();
316 JSHandle<JSTaggedValue> resultConfigurable = JSObject::GetProperty(thread, resultObj, configurableKey).GetValue();
317 ASSERT_EQ(resultConfigurable->ToBoolean(), true);
318 TestHelper::TearDownFrame(thread, prev);
319 }
320
321 // Reflect.getPrototypeOf (target)
HWTEST_F_L0(BuiltinsReflectTest,ReflectGetPrototypeOf)322 HWTEST_F_L0(BuiltinsReflectTest, ReflectGetPrototypeOf)
323 {
324 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
325
326 JSHandle<JSObject> target =
327 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
328 JSHandle<JSObject> proto =
329 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
330
331 ASSERT_EQ(JSObject::SetPrototype(thread, target, JSHandle<JSTaggedValue>(proto)), true);
332
333 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
334 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
335 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
336 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
337
338 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
339 JSTaggedValue result = BuiltinsReflect::ReflectGetPrototypeOf(ecmaRuntimeCallInfo);
340 ASSERT_TRUE(result.IsECMAObject());
341 JSHandle<JSTaggedValue> resultObj(thread, JSTaggedValue(reinterpret_cast<TaggedObject *>(result.GetRawData())));
342 ASSERT_EQ(JSTaggedValue::SameValue(resultObj.GetTaggedValue(), proto.GetTaggedValue()), true);
343 TestHelper::TearDownFrame(thread, prev);
344 }
345
346 // Reflect.has (target, propertyKey)
HWTEST_F_L0(BuiltinsReflectTest,ReflectHas)347 HWTEST_F_L0(BuiltinsReflectTest, ReflectHas)
348 {
349 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
350
351 // target
352 JSHandle<JSObject> target =
353 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
354 // propertyKey
355 JSHandle<JSTaggedValue> key(factory->NewFromASCII("test_reflect_has"));
356 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(103));
357 ASSERT_EQ(JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(target), key, value), true);
358
359 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
360 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
361 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
362 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
363 ecmaRuntimeCallInfo->SetCallArg(1, key.GetTaggedValue());
364
365 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
366 JSTaggedValue result = BuiltinsReflect::ReflectHas(ecmaRuntimeCallInfo);
367
368 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
369 TestHelper::TearDownFrame(thread, prev);
370 }
371
372 // Reflect.isExtensible (target)
HWTEST_F_L0(BuiltinsReflectTest,ReflectIsExtensible)373 HWTEST_F_L0(BuiltinsReflectTest, ReflectIsExtensible)
374 {
375 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
376
377 // target
378 JSHandle<JSObject> target =
379 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
380 target->GetJSHClass()->SetExtensible(false);
381
382 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
383 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
384 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
385 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
386
387 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
388 JSTaggedValue result = BuiltinsReflect::ReflectIsExtensible(ecmaRuntimeCallInfo);
389
390 ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
391 TestHelper::TearDownFrame(thread, prev);
392 }
393
394 // Reflect.ownKeys (target)
HWTEST_F_L0(BuiltinsReflectTest,ReflectOwnKeys)395 HWTEST_F_L0(BuiltinsReflectTest, ReflectOwnKeys)
396 {
397 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
398
399 // target
400 JSHandle<JSObject> target =
401 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
402 JSHandle<JSTaggedValue> key0(factory->NewFromASCII("test_reflect_own_keys1"));
403 JSHandle<JSTaggedValue> value0(thread, JSTaggedValue(104));
404 ASSERT_EQ(JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(target), key0, value0), true);
405 JSHandle<JSTaggedValue> key1(factory->NewFromASCII("test_reflect_own_keys2"));
406 JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(105));
407 ASSERT_EQ(JSObject::SetProperty(thread, JSHandle<JSTaggedValue>(target), key1, value1), true);
408
409 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
410 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
411 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
412 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
413
414 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
415 JSTaggedValue result = BuiltinsReflect::ReflectOwnKeys(ecmaRuntimeCallInfo);
416
417 ASSERT_TRUE(result.IsECMAObject());
418 JSHandle<JSTaggedValue> resultTaggedValue(thread, reinterpret_cast<TaggedObject *>(result.GetRawData()));
419 JSHandle<JSArray> resultArray = JSHandle<JSArray>::Cast(resultTaggedValue);
420 // test length
421 JSHandle<JSTaggedValue> resultLengthKey = thread->GlobalConstants()->GetHandledLengthString();
422 JSHandle<JSTaggedValue> resultLength =
423 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(resultArray), resultLengthKey).GetValue();
424 ASSERT_EQ(resultLength->GetInt(), 2);
425 // test array[0]
426 JSHandle<JSTaggedValue> resultKey0(thread, JSTaggedValue(0));
427 JSHandle<JSTaggedValue> resultValue0 =
428 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(resultArray), resultKey0).GetValue();
429 ASSERT_EQ(EcmaStringAccessor::Compare(instance,
430 JSHandle<EcmaString>(resultValue0),
431 JSHandle<EcmaString>(key0)),
432 0);
433 // test array[1]
434 JSHandle<JSTaggedValue> resultKey1(thread, JSTaggedValue(1));
435 JSHandle<JSTaggedValue> resultValue1 =
436 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(resultArray), resultKey1).GetValue();
437 ASSERT_EQ(EcmaStringAccessor::Compare(instance,
438 JSHandle<EcmaString>(resultValue1),
439 JSHandle<EcmaString>(key1)),
440 0);
441 TestHelper::TearDownFrame(thread, prev);
442 }
443
444 // Reflect.preventExtensions (target)
HWTEST_F_L0(BuiltinsReflectTest,ReflectPreventExtensions)445 HWTEST_F_L0(BuiltinsReflectTest, ReflectPreventExtensions)
446 {
447 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
448
449 // target
450 JSHandle<JSObject> target =
451 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
452 target->GetJSHClass()->SetExtensible(true);
453
454 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
455 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
456 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
457 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
458
459 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
460 JSTaggedValue result = BuiltinsReflect::ReflectPreventExtensions(ecmaRuntimeCallInfo);
461
462 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
463 ASSERT_EQ(target->IsExtensible(), false);
464 TestHelper::TearDownFrame(thread, prev);
465 }
466
467 // Reflect.set (target, propertyKey, V [ , receiver])
HWTEST_F_L0(BuiltinsReflectTest,ReflectSet)468 HWTEST_F_L0(BuiltinsReflectTest, ReflectSet)
469 {
470 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
471
472 // target
473 JSHandle<JSObject> target =
474 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
475 // propertyKey
476 JSHandle<JSTaggedValue> key(factory->NewFromASCII("test_reflect_set"));
477 // value
478 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(106));
479
480 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
481 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
482 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
483 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
484 ecmaRuntimeCallInfo->SetCallArg(1, key.GetTaggedValue());
485 ecmaRuntimeCallInfo->SetCallArg(2, value.GetTaggedValue());
486
487 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
488 JSTaggedValue result = BuiltinsReflect::ReflectSet(ecmaRuntimeCallInfo);
489
490 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
491
492 JSHandle<JSTaggedValue> ruler = JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(target), key).GetValue();
493 ASSERT_EQ(JSTaggedValue::ToInt32(thread, ruler), 106);
494 TestHelper::TearDownFrame(thread, prev);
495 }
496
497 // Reflect.setPrototypeOf (target, proto)
HWTEST_F_L0(BuiltinsReflectTest,ReflectSetPrototypeOf)498 HWTEST_F_L0(BuiltinsReflectTest, ReflectSetPrototypeOf)
499 {
500 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
501
502 JSHandle<JSObject> target =
503 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
504 JSHandle<JSObject> proto =
505 factory->NewJSObjectByConstructor(TestObjectCreate(thread), JSHandle<JSTaggedValue>(TestObjectCreate(thread)));
506
507 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
508 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
509 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
510 ecmaRuntimeCallInfo->SetCallArg(0, target.GetTaggedValue());
511 ecmaRuntimeCallInfo->SetCallArg(1, proto.GetTaggedValue());
512
513 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
514 JSTaggedValue result = BuiltinsReflect::ReflectSetPrototypeOf(ecmaRuntimeCallInfo);
515
516 ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
517 JSHandle<JSTaggedValue> resultObj(thread, target->GetJSHClass()->GetPrototype());
518 ASSERT_EQ(JSTaggedValue::SameValue(resultObj.GetTaggedValue(), proto.GetTaggedValue()), true);
519 TestHelper::TearDownFrame(thread, prev);
520 }
521 } // namespace panda::test
522