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
18 static const char *TAG = "[jsapi_typeof]";
19
testNapiTypeof(napi_env env,napi_callback_info info)20 napi_value testNapiTypeof(napi_env env, napi_callback_info info)
21 {
22 // pages/javascript/jsabstractops/typeof
23 size_t requireArgc = PARAM1;
24 size_t argc = PARAM1;
25 napi_status status;
26 napi_valuetype result;
27 napi_value resultStr;
28 napi_value args[PARAM1] = {nullptr};
29 const napi_extended_error_info *extended_error_info;
30
31 // Get args
32 status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
33 if (status != napi_ok) {
34 getErrMsg(status, env, extended_error_info, "get cb info", TAG);
35 return NULL;
36 }
37 if (argc < requireArgc) {
38 std::string errMsg = "Expected " + std::to_string(requireArgc) + " arguments";
39 napi_throw_error(env, NULL, errMsg.c_str());
40 return NULL;
41 }
42
43 // Call napi_typeof(), any -> napi_valuetype
44 status = napi_typeof(env, args[PARAM0], &result);
45 if (status != napi_ok) {
46 getErrMsg(status, env, extended_error_info, "call napi_typeof()", TAG);
47 return NULL;
48 }
49
50 // napi_valuetype -> string
51 status = napiValueType2Str(env, result, &resultStr);
52 if (status != napi_ok) {
53 std::string errMsg = "Failed to convert napi_valuetype " + std::to_string(status) + " to string";
54 napi_throw_error(env, NULL, errMsg.c_str());
55 return NULL;
56 }
57 return resultStr;
58 }