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 "utils/napi_utils.h"
17
18 #include <mutex>
19 #include <string.h>
20 #include <vector>
21
22 #include "base/resource.h"
23
24 namespace OHOS {
25 namespace Ace {
26 namespace Drawable {
ParseJsResource(napi_env env,napi_value value)27 Resource NapiUtils::ParseJsResource(napi_env env, napi_value value)
28 {
29 napi_value jsId = GetNamedProperty(env, value, "id");
30 napi_value jsType = GetNamedProperty(env, value, "type");
31 napi_value jsBundleName = GetNamedProperty(env, value, "bundleName");
32 napi_value jsModuleName = GetNamedProperty(env, value, "moduleName");
33
34 auto id = ParseJsInt(env, jsId);
35 auto type = ParseJsInt(env, jsType);
36 auto bundleName = ParseJsString(env, jsBundleName);
37 auto moduleName = ParseJsString(env, jsModuleName);
38
39 std::vector<ResourceObjectParams> params;
40
41 // parse the params of the resource
42 return Resource(id, type, params, bundleName, moduleName);
43 }
44
ParseJsString(napi_env env,napi_value value)45 std::string NapiUtils::ParseJsString(napi_env env, napi_value value)
46 {
47 static constexpr size_t max_length = 2048;
48 if (ParseJsType(env, value) != napi_string) {
49 return {};
50 }
51 std::string result;
52 size_t stringLength = 0;
53 NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, nullptr, 0, &stringLength), result);
54 if (stringLength == 0 || stringLength > max_length) {
55 return result;
56 }
57 std::vector<char> buffer(stringLength + 1, 0);
58 size_t length = 0;
59 NAPI_CALL_BASE(env, napi_get_value_string_utf8(env, value, buffer.data(), buffer.size(), &length), result);
60 if (length > 0) {
61 result.append(buffer.data(), length);
62 }
63 return result;
64 }
65
ParseJsType(napi_env env,napi_value value)66 napi_valuetype NapiUtils::ParseJsType(napi_env env, napi_value value)
67 {
68 if (value == nullptr) {
69 return napi_undefined;
70 }
71
72 napi_valuetype valueType = napi_undefined;
73 NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), napi_undefined);
74 return valueType;
75 }
76
ParseJsInt(napi_env env,napi_value value)77 int32_t NapiUtils::ParseJsInt(napi_env env, napi_value value)
78 {
79 int32_t num;
80 napi_get_value_int32(env, value, &num);
81 return num;
82 }
83
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)84 napi_value NapiUtils::GetNamedProperty(napi_env env, napi_value object, const std::string& propertyName)
85 {
86 napi_value value = nullptr;
87 NAPI_CALL(env, napi_get_named_property(env, object, propertyName.c_str(), &value));
88 return value;
89 }
90
IsArray(napi_env env,napi_value value)91 bool NapiUtils::IsArray(napi_env env, napi_value value)
92 {
93 bool isArray = false;
94 napi_status ret = napi_is_array(env, value, &isArray);
95 if (ret == napi_ok) {
96 return isArray;
97 }
98 return false;
99 }
100
ParseJsBool(napi_env env,napi_value value)101 bool NapiUtils::ParseJsBool(napi_env env, napi_value value)
102 {
103 bool boolValue = false;
104 napi_status ret = napi_get_value_bool(env, value, &boolValue);
105 if (ret == napi_ok) {
106 return boolValue;
107 }
108 return false;
109 }
110
CreateString(napi_env env,const char * str)111 napi_value NapiUtils::CreateString(napi_env env, const char* str)
112 {
113 napi_value value = nullptr;
114 if (napi_create_string_utf8(env, str, strlen(str), &value) != napi_ok) {
115 return nullptr;
116 }
117 return value;
118 }
119 } // namespace Drawable
120 } // namespace Ace
121 } // namespace OHOS
122