• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 Unionman Technology 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 "napi/native_api.h"
17 #include "napi/native_node_api.h"
18 
19 #ifdef __cplusplus
20 #include "um_tf.h"
21 extern "C" {
22 #endif
23 
24 struct tfOnData {
25     napi_async_work asyncWork = nullptr; // 异步工作项
26     napi_deferred deferred = nullptr; // 用于Promise的resolve、reject处理
27     napi_ref callback = nullptr; // 回调函数
28     int args[1] = {};
29     int result = 0; // 业务逻辑处理结果(返回值)
30 };
31 
32 // 业务逻辑处理函数,由worker线程池调度执行。
executeCB(napi_env env,void * data)33 static void executeCB(napi_env env, void* data)
34 {
35     tfOnData* tfData = (tfOnData*)data;
36     int ret = 0;
37     ret = FileExist();
38     tfData->result = ret;
39 }
40 
41 // 业务逻辑处理完成回调函数,在业务逻辑处理函数执行完成或取消后触发,由EventLoop线程中执行。
completeCBForPromise(napi_env env,napi_status status,void * data)42 static void completeCBForPromise(napi_env env, napi_status status, void* data)
43 {
44     tfOnData* tfData = (tfOnData*)data;
45     napi_value result = nullptr;
46     napi_create_int32(env, tfData->result, &result);
47     napi_resolve_deferred(env, tfData->deferred, result);
48 
49     // 删除napi_ref对象
50     if (tfData->callback != nullptr) {
51         napi_delete_reference(env, tfData->callback);
52     }
53 
54     // 删除异步工作项
55     napi_delete_async_work(env, tfData->asyncWork);
56     delete tfData;
57 }
58 
UM_TF_test(napi_env env,napi_callback_info info)59 static napi_value UM_TF_test(napi_env env, napi_callback_info info)
60 {
61     // 创建promise
62     napi_value promise = nullptr;
63     napi_deferred deferred = nullptr;
64     NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
65 
66     // 异步工作项上下文用户数据,传递到异步工作项的execute、complete之间传递数据
67     auto tfData = new tfOnData {
68         .asyncWork = nullptr,
69         .deferred = deferred,
70     };
71     // 创建async work,创建成功后通过最后一个参数(addonData->asyncWork)返回asyncwork的handle
72     napi_value resourceName = nullptr;
73     napi_create_string_utf8(env, "UM_TF_test", NAPI_AUTO_LENGTH, &resourceName);
74     napi_create_async_work(
75         env, nullptr, resourceName, executeCB, completeCBForPromise, (void*)tfData, &tfData->asyncWork);
76 
77     // 将刚创建的async work加到队列,由底层去调度执行
78     napi_queue_async_work(env, tfData->asyncWork);
79 
80     // 返回promise
81     return promise;
82 }
83 
84 /*
85  * 注册接口
86  */
registertfnapiApis(napi_env env,napi_value exports)87 static napi_value registertfnapiApis(napi_env env, napi_value exports)
88 {
89     napi_property_descriptor desc[] = {
90         DECLARE_NAPI_FUNCTION("UM_TF_test", UM_TF_test),
91     };
92     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
93     return exports;
94 }
95 
96 /*
97  * 模块定义
98  */
99 static napi_module tfnapiModule = {
100     .nm_version = 1,
101     .nm_flags = 0,
102     .nm_filename = nullptr,
103     .nm_register_func = registertfnapiApis,
104     .nm_modname = "tftest", // 模块名
105     .nm_priv = ((void*)0),
106     .reserved = { 0 },
107 };
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 /*
113  * 注册模块
114  */
RegistertfnapiModule(void)115 extern "C" __attribute__((constructor)) void RegistertfnapiModule(void)
116 {
117     napi_module_register(&tfnapiModule); // 接口注册函数
118 }
119