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 
19 using namespace panda;
20 using namespace panda::ecmascript;
21 
ARKTS_CreateSymbol(ARKTS_Env env,const char * description,int32_t length)22 ARKTS_Value ARKTS_CreateSymbol(ARKTS_Env env, const char* description, int32_t length)
23 {
24     ARKTS_ASSERT_P(env, "env is null");
25     auto vm = P_CAST(env, EcmaVM*);
26 
27     panda::Local<panda::JSValueRef> object;
28 
29     if (!description || !length) {
30         const char* str = "";
31         object = panda::StringRef::NewFromUtf8(vm, str, 0);
32     } else {
33         object = panda::StringRef::NewFromUtf8(vm, description, length);
34     }
35     auto symbol = panda::SymbolRef::New(vm, object);
36     return BIT_CAST(symbol, ARKTS_Value);
37 }
38 
ARKTS_IsSymbol(ARKTS_Env env,ARKTS_Value value)39 bool ARKTS_IsSymbol(ARKTS_Env env, ARKTS_Value value)
40 {
41     auto tag = BIT_CAST(value, JSValueRef);
42     if (!tag.IsHeapObject()) {
43         return false;
44     }
45     tag = *BIT_CAST(value, JSValueRef*);
46     auto vm = P_CAST(env, EcmaVM*);
47     return tag.IsSymbol(vm);
48 }
49 
ARKTS_GetSymbolDesc(ARKTS_Env env,ARKTS_Value value)50 const char* ARKTS_GetSymbolDesc(ARKTS_Env env, ARKTS_Value value)
51 {
52     ARKTS_ASSERT_P(env, "env is null");
53     ARKTS_ASSERT_P(ARKTS_IsSymbol(env, value), "value is not a symbol");
54 
55     auto vm = P_CAST(env, EcmaVM*);
56     auto symbol = *P_CAST(value, SymbolRef*);
57     auto desc = symbol.GetDescription(vm);
58     auto desc1 = BIT_CAST(desc, ARKTS_Value);
59     if (ARKTS_IsString(env, desc1)) {
60         return ARKTS_GetValueCString(env, BIT_CAST(desc, ARKTS_Value));
61     }
62     return nullptr;
63 }
64