• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "common.h"
17 #include "javascriptapi.h"
18 
getErrMsg(napi_status & status,napi_env & env,const napi_extended_error_info * & extended_error_info,const char * info,const char * tag)19 void getErrMsg(napi_status &status, napi_env &env, const napi_extended_error_info *&extended_error_info,
20     const char *info, const char *tag)
21 {
22     status = napi_get_last_error_info(env, &extended_error_info);
23     if (status == napi_ok && extended_error_info != NULL) {
24         const char *errorMessage =
25             extended_error_info->error_message != NULL ? extended_error_info->error_message : "Unknown error";
26         OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, tag, "errmsg %{public}s!, engine_err_code %{public}d!.",
27                      std::to_string(extended_error_info->engine_error_code).c_str(), extended_error_info->error_code);
28         std::string myInfo = info;
29         std::string res = "Failed to " + myInfo + " em = " + std::to_string(extended_error_info->engine_error_code) +
30                           ", eec = " + std::to_string(extended_error_info->engine_error_code) +
31                           ", ec = " + std::to_string(extended_error_info->error_code);
32         napi_throw_error(env, NULL, res.c_str());
33     }
34 }
35 
validateObjectProperty(napi_env & env,napi_value & obj,napi_value & propName,const char * tag)36 bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName, const char *tag)
37 {
38     napi_status status;
39     napi_valuetype valuetype0;
40     napi_valuetype valuetype1;
41     const napi_extended_error_info *extended_error_info;
42 
43     // 确认第一个参数是个对象
44     status = napi_typeof(env, obj, &valuetype0);
45     if (status != napi_ok) {
46         getErrMsg(status, env, extended_error_info, "get obj type", tag);
47         return false;
48     }
49     if (valuetype0 != napi_object) {
50         napi_throw_type_error(env, NULL, "Wrong argument type, expected an object");
51         return false;
52     }
53 
54     // 确认第二个参数是个字符串
55     status = napi_typeof(env, propName, &valuetype1);
56     if (status != napi_ok) {
57         getErrMsg(status, env, extended_error_info, "get propName type", tag);
58         return false;
59     }
60     if (valuetype1 != napi_string) {
61         napi_throw_type_error(env, NULL, "Wrong argument type, expected a string");
62         return false;
63     }
64     return true;
65 }
66 
validateArrayObjProperty(napi_env & env,napi_value & obj,napi_value & propName,const char * tag)67 bool validateArrayObjProperty(napi_env &env, napi_value &obj, napi_value &propName, const char *tag)
68 {
69     napi_status status;
70     napi_valuetype valuetype0;
71     napi_valuetype valuetype1;
72     const napi_extended_error_info *extended_error_info;
73 
74     // 确认第一个参数是个对象
75     status = napi_typeof(env, obj, &valuetype0);
76     if (status != napi_ok) {
77         getErrMsg(status, env, extended_error_info, "get obj type", tag);
78         return false;
79     }
80     if (valuetype0 != napi_object) {
81         napi_throw_type_error(env, NULL, "Wrong argument type, expected an object");
82         return false;
83     }
84 
85     // 确认第二个参数是个数字
86     status = napi_typeof(env, propName, &valuetype1);
87     if (status != napi_ok) {
88         getErrMsg(status, env, extended_error_info, "get propName type", tag);
89         return false;
90     }
91     if (valuetype1 != napi_number) {
92         napi_throw_type_error(env, NULL, "Wrong argument type, expected a number");
93         return false;
94     }
95     return true;
96 }
97 
napiValueType2Str(const napi_env & env,const napi_valuetype type,napi_value * result)98 napi_status napiValueType2Str(const napi_env &env, const napi_valuetype type, napi_value *result)
99 {
100     const char *typeStr = "";
101     napi_status status;
102     // napi_valuetype -> const char *
103     switch (type) {
104         case napi_undefined:
105             typeStr = "undefined";
106             break;
107         case napi_null:
108             typeStr = "null";
109             break;
110         case napi_boolean:
111             typeStr = "boolean";
112             break;
113         case napi_number:
114             typeStr = "number";
115             break;
116         case napi_string:
117             typeStr = "string";
118             break;
119         case napi_symbol:
120             typeStr = "symbol";
121             break;
122         case napi_object:
123             typeStr = "object";
124             break;
125         case napi_function:
126             typeStr = "function";
127             break;
128         case napi_external:
129             typeStr = "external";
130             break;
131         case napi_bigint:
132             typeStr = "bigint";
133             break;
134         default:
135             typeStr = "unknown";
136             break;
137     }
138     // const char * -> napi_value
139     status = napi_create_string_utf8(env, typeStr, NAPI_AUTO_LENGTH, result);
140     return status;
141 }
142