• 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 #include "hilog/log.h"
19 static const char *TAG = "[javascriptapi_object_wrap]";
20 
21 class MyNode {
22 public:
23     napi_status status;
24     napi_valuetype result;
25     napi_value resultStr;
26     const napi_extended_error_info *extended_error_info;
MyNode(napi_env env,napi_value val)27     MyNode(napi_env env, napi_value val)
28     {
29         // Call napi_typeof(), any -> napi_valuetype
30         status = napi_typeof(env, val, &result);
31         if (status != napi_ok) {
32             getErrMsg(status, env, extended_error_info, "call napi_typeof()", TAG);
33         }
34 
35         // napi_valuetype -> string
36         status = napiValueType2Str(env, result, &resultStr);
37         if (status != napi_ok) {
38             std::string errMsg = "Failed to convert napi_valuetype " + std::to_string(status) + " to string";
39             napi_throw_error(env, NULL, errMsg.c_str());
40         }
41     }
GetResult(napi_env env)42     napi_value GetResult(napi_env env)
43     {
44         return resultStr;
45     }
46 };
47 
testNapiUnwrap(napi_env env,napi_callback_info info)48 napi_value testNapiUnwrap(napi_env env, napi_callback_info info)
49 {
50     size_t argc = PARAM1;
51     napi_value argv[PARAM1];
52     napi_value thisObj;
53     void *data = nullptr;
54     napi_status status;
55     napi_value cons;
56     const napi_extended_error_info *extended_error_info;
57     // 获取回调函数的参数信息
58     status = napi_get_cb_info(env, info, &argc, argv, &thisObj, &data);
59     if (status != napi_ok) {
60         getErrMsg(status, env, extended_error_info, "Failed to get callback info", TAG);
61         return NULL;
62     }
63     auto instance = new MyNode(env, argv[PARAM0]);
64     status = napi_wrap(
65         env, thisObj, instance,
66         [](napi_env environment, void *data, void *hint) {
67             auto objInfo = reinterpret_cast<MyNode *>(data);
68             if (objInfo != nullptr) {
69                 delete objInfo;
70             }
71         }, NULL, NULL);
72     if (status != napi_ok) {
73         getErrMsg(status, env, extended_error_info, "wrap", TAG);
74         return NULL;
75     }
76 
77     MyNode *obj;
78     status = napi_unwrap(env, thisObj, reinterpret_cast<void **>(&obj));
79     if (status != napi_ok) {
80         getErrMsg(status, env, extended_error_info, "unwrap", TAG);
81         return NULL;
82     }
83     napi_value resultStrValue = obj->GetResult(env);
84     if (resultStrValue == nullptr) {
85         // 处理错误情况
86         napi_throw_error(env, NULL, "Failed to get result string");
87         return NULL;
88     }
89     return resultStrValue;
90 }
91