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_internal.h"
17 #include "ark_interop_napi.h"
18 #include "ark_interop_log.h"
19 
20 #include <cstdint>
21 #include <cassert>
22 #include <cstdlib>
23 #include <vector>
24 
25 using namespace panda;
26 using namespace panda::ecmascript;
27 
ARKTS_CreateUtf8(ARKTS_Env env,const char * value,int32_t size)28 ARKTS_Value ARKTS_CreateUtf8(ARKTS_Env env, const char* value, int32_t size)
29 {
30     ARKTS_ASSERT_P(env, "env is null");
31     ARKTS_ASSERT_P(value, "value is null");
32 
33     auto vm = P_CAST(env, EcmaVM*);
34     Local<JSValueRef> result = StringRef::NewFromUtf8(vm, value, size);
35 
36     return ARKTS_FromHandle(result);
37 }
38 
ARKTS_IsString(ARKTS_Env env,ARKTS_Value value)39 bool ARKTS_IsString(ARKTS_Env env, ARKTS_Value value)
40 {
41     auto v = BIT_CAST(value, JSValueRef);
42     if (!v.IsHeapObject()) {
43         return false;
44     }
45     v = *P_CAST(value, JSValueRef*);
46     auto vm = P_CAST(env, EcmaVM*);
47     return v.IsString(vm);
48 }
49 
ARKTS_GetValueUtf8Size(ARKTS_Env env,ARKTS_Value value)50 int32_t ARKTS_GetValueUtf8Size(ARKTS_Env env, ARKTS_Value value)
51 {
52     ARKTS_ASSERT_I(env, "env is null");
53     ARKTS_ASSERT_I(ARKTS_IsString(env, value), "not a string");
54     auto vm = P_CAST(env, EcmaVM*);
55     auto v = BIT_CAST(value, Local<StringRef>);
56     return v->Utf8Length(vm, true);
57 }
58 
ARKTS_GetValueUtf8(ARKTS_Env env,ARKTS_Value value,int32_t capacity,char * buffer)59 int32_t ARKTS_GetValueUtf8(ARKTS_Env env, ARKTS_Value value, int32_t capacity, char* buffer)
60 {
61     ARKTS_ASSERT_I(ARKTS_IsString(env, value), "not a string");
62     auto vm = P_CAST(env, EcmaVM*);
63     auto v = BIT_CAST(value, Local<StringRef>);
64     return v->WriteUtf8(vm, buffer, capacity, true);
65 }
66 
ARKTS_GetValueCString(ARKTS_Env env,ARKTS_Value value)67 const char* ARKTS_GetValueCString(ARKTS_Env env, ARKTS_Value value)
68 {
69     ARKTS_ASSERT_I(env, "env is null");
70     ARKTS_ASSERT_I(ARKTS_IsString(env, value), "not a string");
71     auto vm = P_CAST(env, EcmaVM*);
72     auto v = BIT_CAST(value, Local<StringRef>);
73     auto size = v->Utf8Length(vm, true);
74     if (size <= 0) {
75         return nullptr;
76     }
77     auto result = (char*) malloc(sizeof(char) * size);
78     if (!result) {
79         LOGE("ARKTS_GetValueCString fail, out of memory");
80         return nullptr;
81     }
82     v->WriteUtf8(vm, result, size, true);
83     return result;
84 }
85 
ARKTS_FreeCString(const char * src)86 void ARKTS_FreeCString(const char* src)
87 {
88     free(reinterpret_cast<void*>(const_cast<char*>(src)));
89 }
90