• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "ark_interop_napi.h"
17 #include "ark_interop_internal.h"
18 #include "ark_interop_log.h"
19 
20 using namespace panda;
21 using namespace panda::ecmascript;
22 
ARKTSInner_IsJSKey(ARKTS_Env env,ARKTS_Value value)23 ARKTS_INLINE bool ARKTSInner_IsJSKey(ARKTS_Env env, ARKTS_Value value)
24 {
25     auto tag = BIT_CAST(value, JSValueRef);
26     if (tag.IsNumber()) {
27         return true;
28     }
29     if (!tag.IsHeapObject()) {
30         return false;
31     }
32     tag = *P_CAST(value, JSValueRef*);
33     auto vm = P_CAST(env, EcmaVM*);
34     return tag.IsString(vm) || tag.IsSymbol(vm);
35 }
36 
ARKTS_CreateObject(ARKTS_Env env)37 ARKTS_Value ARKTS_CreateObject(ARKTS_Env env)
38 {
39     ARKTS_ASSERT_P(env, "env is null");
40 
41     auto vm = P_CAST(env, EcmaVM*);
42     auto result = ObjectRef::New(vm);
43 
44     return BIT_CAST(result, ARKTS_Value);
45 }
46 
ARKTS_IsHeapObject(ARKTS_Value value)47 bool ARKTS_IsHeapObject(ARKTS_Value value)
48 {
49     auto v = BIT_CAST(value, JSValueRef);
50     return v.IsHeapObject();
51 }
52 
ARKTS_IsObject(ARKTS_Env env,ARKTS_Value value)53 bool ARKTS_IsObject(ARKTS_Env env, ARKTS_Value value)
54 {
55     ARKTS_ASSERT_F(env, "env is null");
56     auto v = BIT_CAST(value, JSValueRef);
57     if (!v.IsHeapObject()) {
58         return false;
59     }
60     auto handle = BIT_CAST(value, Local<JSValueRef>);
61     auto vm = P_CAST(env, EcmaVM*);
62     return handle->IsObject(vm);
63 }
64 
ARKTS_HasOwnProperty(ARKTS_Env env,ARKTS_Value jobj,ARKTS_Value jkey)65 bool ARKTS_HasOwnProperty(ARKTS_Env env, ARKTS_Value jobj, ARKTS_Value jkey)
66 {
67     ARKTS_ASSERT_F(env, "env is null");
68     ARKTS_ASSERT_F(ARKTS_IsHeapObject(jobj), "object is not heap object");
69     ARKTS_ASSERT_F(ARKTSInner_IsJSKey(env, jkey), "key is not number、string or symbol");
70 
71     auto vm = P_CAST(env, EcmaVM*);
72     auto object = BIT_CAST(jobj, Local<ObjectRef>);
73     auto key = ARKTS_ToHandle<JSValueRef>(jkey);
74     return object->Has(vm, key);
75 }
76 
ARKTS_EnumOwnProperties(ARKTS_Env env,ARKTS_Value jobj)77 ARKTS_Value ARKTS_EnumOwnProperties(ARKTS_Env env, ARKTS_Value jobj)
78 {
79     ARKTS_ASSERT_P(env, "env is null");
80     ARKTS_ASSERT_P(ARKTS_IsHeapObject(jobj), "object is not heap object");
81 
82     auto vm = P_CAST(env, EcmaVM*);
83     auto object = BIT_CAST(jobj, Local<ObjectRef>);
84     auto result = object->GetOwnEnumerablePropertyNames(vm);
85 
86     return BIT_CAST(result, ARKTS_Value);
87 }
88 
ARKTS_DefineOwnProperty(ARKTS_Env env,ARKTS_Value jobj,ARKTS_Value jkey,ARKTS_Value jvalue,ARKTS_PropertyFlag attrs)89 void ARKTS_DefineOwnProperty(ARKTS_Env env, ARKTS_Value jobj, ARKTS_Value jkey, ARKTS_Value jvalue,
90     ARKTS_PropertyFlag attrs)
91 {
92     ARKTS_ASSERT_V(env, "env is null");
93     ARKTS_ASSERT_V(ARKTS_IsHeapObject(jobj), "object is not heap object");
94     ARKTS_ASSERT_V(ARKTSInner_IsJSKey(env, jkey), "key is not number、string or symbol");
95 
96     auto vm = P_CAST(env, EcmaVM*);
97     auto object = BIT_CAST(jobj, Local<ObjectRef>);
98     auto key = ARKTS_ToHandle<JSValueRef>(jkey);
99     auto value = ARKTS_ToHandle<JSValueRef>(jvalue);
100 
101     PropertyAttribute attribute(value, attrs & N_WRITABLE, attrs & N_ENUMERABLE, attrs & N_CONFIGURABLE);
102     object->DefineProperty(vm, key, attribute);
103 }
104 
ARKTS_DefineAccessors(ARKTS_Env env,ARKTS_Value jobj,ARKTS_Value jkey,ARKTS_Accessor accessor)105 void ARKTS_DefineAccessors(ARKTS_Env env, ARKTS_Value jobj, ARKTS_Value jkey, ARKTS_Accessor accessor)
106 {
107     ARKTS_ASSERT_V(env, "env is null");
108     ARKTS_ASSERT_V(ARKTS_IsHeapObject(jobj), "object is not heap object");
109     ARKTS_ASSERT_V(ARKTSInner_IsJSKey(env, jkey), "key is not number、string or symbol");
110 
111     auto undefined = ARKTS_CreateUndefined();
112 
113     ARKTS_ASSERT_V(accessor.setter == undefined || ARKTS_IsCallable(env, accessor.setter), "setter not callable");
114     ARKTS_ASSERT_V(accessor.getter == undefined || ARKTS_IsCallable(env, accessor.getter), "getter not callable");
115     ARKTS_ASSERT_V(accessor.getter != undefined || accessor.setter != undefined, "getter and setter is undefined");
116 
117     auto vm = P_CAST(env, EcmaVM*);
118     auto object = BIT_CAST(jobj, Local<ObjectRef>);
119     auto key = ARKTS_ToHandle<JSValueRef>(jkey);
120     auto handledUndef = ARKTS_ToHandle<JSValueRef>(undefined);
121     auto handledGetter = accessor.getter != undefined ? BIT_CAST(accessor.getter, Local<JSValueRef>) : handledUndef;
122     auto handledSetter = accessor.setter != undefined ? BIT_CAST(accessor.setter, Local<JSValueRef>) : handledUndef;
123 
124     PropertyAttribute attribute(handledUndef,
125         accessor.attrs & N_WRITABLE,
126         accessor.attrs & N_ENUMERABLE,
127         accessor.attrs & N_CONFIGURABLE);
128     object->SetAccessorProperty(vm, key, handledGetter, handledSetter, attribute);
129 }
130 
ARKTS_SetProperty(ARKTS_Env env,ARKTS_Value jobj,ARKTS_Value jkey,ARKTS_Value jvalue)131 void ARKTS_SetProperty(ARKTS_Env env, ARKTS_Value jobj, ARKTS_Value jkey, ARKTS_Value jvalue)
132 {
133     ARKTS_ASSERT_V(env, "env is null");
134     ARKTS_ASSERT_V(ARKTS_IsHeapObject(jobj), "object is not heap object");
135     ARKTS_ASSERT_V(ARKTSInner_IsJSKey(env, jkey), "key is not number、string or symbol");
136 
137     auto vm = P_CAST(env, EcmaVM*);
138     auto object = BIT_CAST(jobj, Local<ObjectRef>);
139     auto key = ARKTS_ToHandle<JSValueRef>(jkey);
140     auto value = ARKTS_ToHandle<JSValueRef>(jvalue);
141 
142     object->Set(vm, key, value);
143 }
144 
ARKTS_GetProperty(ARKTS_Env env,ARKTS_Value jobj,ARKTS_Value jkey)145 ARKTS_Value ARKTS_GetProperty(ARKTS_Env env, ARKTS_Value jobj, ARKTS_Value jkey)
146 {
147     ARKTS_ASSERT_P(env, "env is null");
148     ARKTS_ASSERT_P(ARKTS_IsHeapObject(jobj), "object is not heap object");
149     ARKTS_ASSERT_P(ARKTSInner_IsJSKey(env, jkey), "key is not number、string or symbol");
150 
151     auto vm = P_CAST(env, EcmaVM*);
152     auto object = BIT_CAST(jobj, Local<ObjectRef>);
153     auto key = ARKTS_ToHandle<JSValueRef>(jkey);
154 
155     auto result = object->Get(vm, key);
156     return ARKTS_FromHandle(result);
157 }
158