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 "ecmascript/cross_vm/dynamic_object_accessor.h"
17
18 #include "ecmascript/js_tagged_value.h"
19 #include "ecmascript/js_tagged_value-inl.h"
20 #include "ecmascript/object_operator.h"
21 #include "ecmascript/object_factory.h"
22 #include "common_interfaces/objects/base_object_dispatcher.h"
23
24 namespace panda::ecmascript {
25
26 DynamicObjectAccessor DynamicObjectAccessor::dynObjectAccessor_;
27
Initialize()28 void DynamicObjectAccessor::Initialize()
29 {
30 common::BaseObjectDispatcher::GetDispatcher().RegisterDynamicObjectAccessor(&dynObjectAccessor_);
31 }
32
HasProperty(ThreadHolder * thread,const BaseObject * obj,const char * name) const33 bool DynamicObjectAccessor::HasProperty(ThreadHolder *thread, const BaseObject *obj, const char *name) const
34 {
35 JSThread *jsThread = thread->GetJSThread();
36 ObjectFactory *factory = jsThread->GetEcmaVM()->GetFactory();
37 [[maybe_unused]] EcmaHandleScope handleScope(jsThread);
38 JSHandle<JSTaggedValue> holderHandle(jsThread, TaggedObject::Cast(obj));
39 JSHandle<JSTaggedValue> keyHandle(factory->NewFromUtf8(name));
40 return JSTaggedValue::HasProperty(jsThread, holderHandle, keyHandle);
41 }
42
GetProperty(ThreadHolder * thread,const BaseObject * obj,const char * name) const43 JSTaggedValue DynamicObjectAccessor::GetProperty(ThreadHolder *thread, const BaseObject *obj, const char *name) const
44 {
45 JSThread *jsThread = thread->GetJSThread();
46 ObjectFactory *factory = jsThread->GetEcmaVM()->GetFactory();
47 [[maybe_unused]] EcmaHandleScope handleScope(jsThread);
48 JSHandle<JSTaggedValue> holderHandle(jsThread, TaggedObject::Cast(obj));
49 JSHandle<JSTaggedValue> keyHandle(factory->NewFromUtf8(name));
50 return JSTaggedValue::GetProperty(jsThread, holderHandle, keyHandle).GetValue().GetTaggedValue();
51 }
52
SetProperty(ThreadHolder * thread,BaseObject * obj,const char * name,JSTaggedValue value)53 bool DynamicObjectAccessor::SetProperty(ThreadHolder *thread, BaseObject *obj, const char *name, JSTaggedValue value)
54 {
55 JSThread *jsThread = thread->GetJSThread();
56 ObjectFactory *factory = jsThread->GetEcmaVM()->GetFactory();
57 [[maybe_unused]] EcmaHandleScope handleScope(jsThread);
58 JSHandle<JSTaggedValue> holderHandle(jsThread, TaggedObject::Cast(obj));
59 JSHandle<JSTaggedValue> keyHandle(factory->NewFromUtf8(name));
60 JSHandle<JSTaggedValue> valueHandle(jsThread, value);
61 return JSTaggedValue::SetProperty(jsThread, holderHandle, keyHandle, valueHandle);
62 }
63
HasElementByIdx(ThreadHolder * thread,const BaseObject * obj,const uint32_t index) const64 bool DynamicObjectAccessor::HasElementByIdx(ThreadHolder *thread, const BaseObject *obj, const uint32_t index) const
65 {
66 JSThread *jsThread = thread->GetJSThread();
67 [[maybe_unused]] EcmaHandleScope handleScope(jsThread);
68 JSHandle<JSTaggedValue> holderHandle(jsThread, TaggedObject::Cast(obj));
69 return JSTaggedValue::HasProperty(jsThread, holderHandle, index);
70 }
71
GetElementByIdx(ThreadHolder * thread,const BaseObject * obj,const uint32_t index) const72 JSTaggedValue DynamicObjectAccessor::GetElementByIdx(ThreadHolder *thread, const BaseObject *obj,
73 const uint32_t index) const
74 {
75 JSThread *jsThread = thread->GetJSThread();
76 [[maybe_unused]] EcmaHandleScope handleScope(jsThread);
77 JSHandle<JSTaggedValue> holderHandle(jsThread, TaggedObject::Cast(obj));
78 return JSTaggedValue::GetProperty(jsThread, holderHandle, index).GetValue().GetTaggedValue();
79 }
80
SetElementByIdx(ThreadHolder * thread,BaseObject * obj,const uint32_t index,JSTaggedValue value)81 bool DynamicObjectAccessor::SetElementByIdx(ThreadHolder *thread, BaseObject *obj, const uint32_t index,
82 JSTaggedValue value)
83 {
84 JSThread *jsThread = thread->GetJSThread();
85 [[maybe_unused]] EcmaHandleScope handleScope(jsThread);
86 JSHandle<JSTaggedValue> holderHandle(jsThread, TaggedObject::Cast(obj));
87 JSHandle<JSTaggedValue> valueHandle(jsThread, value);
88 return JSTaggedValue::SetProperty(jsThread, holderHandle, index, valueHandle);
89 }
90 }
91