• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "nodeapi.h"
17 #include <thread>
18 
19 static const char *TAG = "[nodeapi_threadsafefunc]";
20 
21 // 线程安全的 JavaScript 函数对象
22 napi_threadsafe_function g_threadsafeFunction;
23 
24 // JavaScript 回调函数
CallbackFunction(napi_env env,napi_value jsCallback,void * context,void * data)25 static void CallbackFunction(napi_env env, napi_value jsCallback, void *context, void *data)
26 {
27     // 在 JavaScript 环境中执行回调函数
28     size_t argc = 1;
29     napi_value argv[1];
30     napi_create_string_utf8(env, "test_threadsafe_func", NAPI_AUTO_LENGTH, &argv[0]);
31     napi_call_function(env, nullptr, jsCallback, argc, argv, nullptr);
32 }
33 
34 // 线程函数,在这里异步调用 JavaScript 函数
ThreadFunction(void * data)35 static void ThreadFunction(void *data)
36 {
37     // 在另一个线程中异步调用 JavaScript 函数
38     napi_call_threadsafe_function(g_threadsafeFunction, nullptr, napi_tsfn_nonblocking);
39 }
40 
setThreadsafefunc(napi_env env,napi_callback_info info)41 napi_value setThreadsafefunc(napi_env env, napi_callback_info info)
42 {
43     // pages/nodeapi/envlifecycleapis/napithreadsafefunc
44     // 创建线程安全的 JavaScript 函数对象
45 
46     size_t argc = PARAM2;
47     napi_value argv[PARAM2];
48     napi_status status;
49     char buffer[PARAM100];
50     const napi_extended_error_info *extended_error_info;
51 
52     // 解析传入的参数
53     status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
54     if (status != napi_ok) {
55         napi_throw_error(env, NULL, "Failed to parse arguments");
56         return NULL;
57     }
58 
59     // 检查参数数量
60     if (argc < 1) {
61         napi_throw_error(env, NULL, "Expected 1 arguments");
62         return NULL;
63     }
64     napi_value name;
65     napi_create_string_utf8(env, "testThreadsafefunc", NAPI_AUTO_LENGTH, &name);
66     status = napi_create_threadsafe_function(env, argv[0], nullptr, name, 0, 1,
67         nullptr, nullptr, nullptr, CallbackFunction, &g_threadsafeFunction);
68     if (status != napi_ok) {
69         status = napi_get_last_error_info(env, &extended_error_info);
70         if (status == napi_ok && extended_error_info != NULL) {
71             const char *errorMessage =
72                 extended_error_info->error_message != NULL ? extended_error_info->error_message : "Unknown error";
73             OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "errmsg %{public}s!, engine_err_code %{public}d!.",
74                          errorMessage, extended_error_info->engine_error_code);
75             std::string res = "Failed to create threadsafe function em = " + std::string(errorMessage) +
76                               ", eec = " + std::to_string(extended_error_info->engine_error_code) +
77                               ", ec = " + std::to_string(extended_error_info->error_code);
78             napi_throw_error(env, NULL, res.c_str());
79             return NULL;
80         }
81     }
82 //
83 //    // 启动一个新线程
84     std::thread newThread(ThreadFunction, nullptr);
85     newThread.join();
86 
87     // 执行加法操作
88     int32_t result = 0;
89     if (result > PARAM10) {
90         napi_throw_error(env, NULL, "Invalid result > 10.");
91         return NULL;
92     }
93 
94     // 返回结果
95     napi_value resultValue;
96     status = napi_create_int32(env, result, &resultValue);
97     if (status != napi_ok) {
98         napi_throw_error(env, NULL, "Failed to create result value");
99         return NULL;
100     }
101 
102     return resultValue;
103 }
104