1 /*
2 * Copyright (c) 2025 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 "common_interfaces/thread/thread_holder-inl.h"
17 #include "ecmascript/cross_vm/dynamic_object_accessor.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/tests/test_helper.h"
20 #include "thread/thread_holder.h"
21
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::base;
24
25 namespace panda::test {
26 class DynamicObjectAccessorTest : public BaseTestWithScope<false> {};
27
JSObjectCreate(JSThread * thread)28 static JSHandle<JSObject> JSObjectCreate(JSThread *thread)
29 {
30 JSHandle<GlobalEnv> globalEnv = thread->GetEcmaVM()->GetGlobalEnv();
31 auto jsFunc = globalEnv->GetObjectFunction().GetObject<JSFunction>();
32 JSHandle<JSTaggedValue> objFunc(thread, jsFunc);
33 auto *factory = thread->GetEcmaVM()->GetFactory();
34 JSHandle<JSObject> jsObject = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFunc), objFunc);
35 return jsObject;
36 }
37
HWTEST_F_L0(DynamicObjectAccessorTest,SetGetHasProperty)38 HWTEST_F_L0(DynamicObjectAccessorTest, SetGetHasProperty)
39 {
40 if (!g_isEnableCMCGC) {
41 return;
42 }
43 ThreadHolder *threadHolder = thread->GetThreadHolder();
44 JSHandle<JSObject> jsobject = JSObjectCreate(thread);
45 EXPECT_TRUE(*jsobject != nullptr);
46
47 char array[] = "x";
48 auto *factory = thread->GetEcmaVM()->GetFactory();
49 JSHandle<JSTaggedValue> key(factory->NewFromASCII(array));
50 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
51
52 DynamicObjectAccessor dynamicObjectAccessor;
53 dynamicObjectAccessor.SetProperty(threadHolder, jsobject.GetTaggedValue().GetTaggedObject(), array,
54 value.GetTaggedValue());
55 EXPECT_EQ(dynamicObjectAccessor.GetProperty(threadHolder,
56 jsobject.GetTaggedValue().GetTaggedObject(), array).GetInt(), 1);
57 EXPECT_EQ(dynamicObjectAccessor.HasProperty(threadHolder,
58 jsobject.GetTaggedValue().GetTaggedObject(), array), true);
59 }
60
HWTEST_F_L0(DynamicObjectAccessorTest,SetGetHasElement)61 HWTEST_F_L0(DynamicObjectAccessorTest, SetGetHasElement)
62 {
63 if (!g_isEnableCMCGC) {
64 return;
65 }
66 ThreadHolder *threadHolder = thread->GetThreadHolder();
67 JSHandle<JSObject> jsobject = JSObjectCreate(thread);
68 EXPECT_TRUE(*jsobject != nullptr);
69
70 uint32_t index = 0;
71 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
72
73 DynamicObjectAccessor dynamicObjectAccessor;
74 dynamicObjectAccessor.SetElementByIdx(threadHolder, jsobject.GetTaggedValue().GetTaggedObject(), index,
75 value.GetTaggedValue());
76 EXPECT_EQ(dynamicObjectAccessor.GetElementByIdx(threadHolder,
77 jsobject.GetTaggedValue().GetTaggedObject(), index).GetInt(), 1);
78 EXPECT_EQ(dynamicObjectAccessor.HasElementByIdx(threadHolder,
79 jsobject.GetTaggedValue().GetTaggedObject(), index), true);
80 }
81 } // namespace panda::test