• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device 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 "demo_javascript_class.h"
17 
18 #include "napi/native_api.h"
19 #include "napi/native_node_api.h"
20 
21 /*
22  * Sync callback
23  */
Add(napi_env env,napi_callback_info info)24 static napi_value Add(napi_env env, napi_callback_info info)
25 {
26     size_t requireArgc = 2;
27     size_t argc = 2;
28     napi_value args[2] = { nullptr };
29     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
30 
31     NAPI_ASSERT(env, argc >= requireArgc, "Wrong number of arguments");
32 
33     napi_valuetype valuetype0;
34     NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
35 
36     napi_valuetype valuetype1;
37     NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
38 
39     NAPI_ASSERT(env, valuetype0 == napi_number && valuetype1 == napi_number, "Wrong argument type. Numbers expected.");
40 
41     double value0;
42     NAPI_CALL(env, napi_get_value_double(env, args[0], &value0));
43 
44     double value1;
45     NAPI_CALL(env, napi_get_value_double(env, args[1], &value1));
46 
47     napi_value sum;
48     NAPI_CALL(env, napi_create_double(env, value0 + value1, &sum));
49 
50     return sum;
51 }
52 
53 struct AsyncCallbackInfo {
54     napi_async_work asyncWork = nullptr;
55     napi_deferred deferred = nullptr;
56     napi_ref callback[2] = { 0 };
57 };
58 
59 /**
60  * Promise
61  */
TestPromise(napi_env env,napi_callback_info)62 static napi_value TestPromise(napi_env env, napi_callback_info)
63 {
64     napi_deferred deferred = nullptr;
65     napi_value promise = nullptr;
66     NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
67 
68     auto asyncCallbackInfo = new AsyncCallbackInfo {
69         .asyncWork = nullptr,
70         .deferred = deferred,
71     };
72 
73     napi_value resourceName = nullptr;
74     napi_create_string_latin1(env, "TestPromise", NAPI_AUTO_LENGTH, &resourceName);
75     napi_create_async_work(
76         env, nullptr, resourceName, [](napi_env env, void* data) {},
77         [](napi_env env, napi_status status, void* data) {
78             AsyncCallbackInfo* asyncCallbackInfo = (AsyncCallbackInfo*)data;
79             napi_value result = nullptr;
80             napi_create_string_utf8(env, "TestPromise", NAPI_AUTO_LENGTH, &result);
81             napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
82             napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
83             delete asyncCallbackInfo;
84         },
85         (void*)asyncCallbackInfo, &asyncCallbackInfo->asyncWork);
86     napi_queue_async_work(env, asyncCallbackInfo->asyncWork);
87     return promise;
88 }
89 
90 /*
91  * Promise or async callback
92  */
TestPromiseOrAsyncCallback(napi_env env,napi_callback_info info)93 static napi_value TestPromiseOrAsyncCallback(napi_env env, napi_callback_info info)
94 {
95     size_t argc = 2;
96     napi_value args[2] = { 0 };
97     napi_value thisArg = nullptr;
98     void* data = nullptr;
99     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisArg, &data));
100 
101     auto asyncCallbackInfo = new AsyncCallbackInfo {
102         .asyncWork = nullptr,
103         .deferred = nullptr,
104     };
105 
106     if (argc != 0) {
107         napi_value resourceName = nullptr;
108         napi_create_string_latin1(env, "TestPromiseOrAsyncCallback1", NAPI_AUTO_LENGTH, &resourceName);
109 
110         for (size_t i = 0; i < argc; i++) {
111             napi_valuetype valuetype = napi_undefined;
112             NAPI_CALL(env, napi_typeof(env, args[i], &valuetype));
113             NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected.");
114             napi_create_reference(env, args[i], 1, &asyncCallbackInfo->callback[i]);
115         }
116 
117         napi_create_async_work(
118             env, nullptr, resourceName, [](napi_env env, void* data) {},
119             [](napi_env env, napi_status status, void* data) {
120                 AsyncCallbackInfo* asyncCallbackInfo = (AsyncCallbackInfo*)data;
121 
122                 napi_value callback = nullptr;
123                 napi_value undefined = nullptr;
124                 napi_value result = nullptr;
125                 napi_value callbackResult = nullptr;
126                 napi_create_string_utf8(env, "TestPromiseOrAsyncCallback", NAPI_AUTO_LENGTH, &result);
127                 napi_get_undefined(env, &undefined);
128 
129                 if (true) {
130                     napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback);
131                     napi_call_function(env, undefined, callback, 1, &result, &callbackResult);
132                 } else {
133                     if (asyncCallbackInfo->callback[1]) {
134                         napi_get_reference_value(env, asyncCallbackInfo->callback[1], &callback);
135                         napi_call_function(env, undefined, callback, 1, &result, &callbackResult);
136                     } else {
137                         napi_throw_error(env, "error", "foo");
138                     }
139                 }
140 
141                 if (asyncCallbackInfo->callback[0] != nullptr) {
142                     napi_delete_reference(env, asyncCallbackInfo->callback[0]);
143                 }
144                 if (asyncCallbackInfo->callback[1] != nullptr) {
145                     napi_delete_reference(env, asyncCallbackInfo->callback[1]);
146                 }
147                 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
148                 delete asyncCallbackInfo;
149             },
150             (void*)asyncCallbackInfo, &asyncCallbackInfo->asyncWork);
151 
152         NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
153 
154         return nullptr;
155     } else {
156         napi_value resourceName = nullptr;
157         napi_create_string_latin1(env, "TestPromiseOrAsyncCallback2", NAPI_AUTO_LENGTH, &resourceName);
158 
159         napi_deferred deferred = nullptr;
160         napi_value promise = nullptr;
161         NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
162 
163         asyncCallbackInfo->deferred = deferred;
164 
165         napi_create_async_work(
166             env, nullptr, resourceName, [](napi_env env, void* data) {},
167             [](napi_env env, napi_status status, void* data) {
168                 AsyncCallbackInfo* asyncCallbackInfo = (AsyncCallbackInfo*)data;
169 
170                 napi_value result = nullptr;
171                 napi_create_string_utf8(env, "TestPromiseOrAsyncCallback", NAPI_AUTO_LENGTH, &result);
172                 if (true) {
173                     napi_resolve_deferred(env, asyncCallbackInfo->deferred, result);
174                 } else {
175                     napi_reject_deferred(env, asyncCallbackInfo->deferred, result);
176                 }
177 
178                 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
179                 delete asyncCallbackInfo;
180             },
181             (void*)asyncCallbackInfo, &asyncCallbackInfo->asyncWork);
182         napi_queue_async_work(env, asyncCallbackInfo->asyncWork);
183         return promise;
184     }
185 }
186 
187 EXTERN_C_START
188 /*
189  * function for module exports
190  */
Init(napi_env env,napi_value exports)191 static napi_value Init(napi_env env, napi_value exports)
192 {
193     /*
194      * Properties define
195      */
196     napi_property_descriptor desc[] = {
197         DECLARE_NAPI_FUNCTION("add", Add),
198         DECLARE_NAPI_FUNCTION("TestPromise", TestPromise),
199         DECLARE_NAPI_FUNCTION("TestPromiseOrAsyncCallback", TestPromiseOrAsyncCallback),
200     };
201     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
202 
203     DemoJavascriptClassInit(env, exports);
204 
205     return exports;
206 }
207 EXTERN_C_END
208 
209 /*
210  * Module define
211  */
212 static napi_module demoModule = {
213     .nm_version = 1,
214     .nm_flags = 0,
215     .nm_filename = nullptr,
216     .nm_register_func = Init,
217     .nm_modname = "demo",
218     .nm_priv = ((void*)0),
219     .reserved = { 0 },
220 };
221 /*
222  * Module register function
223  */
RegisterModule(void)224 extern "C" __attribute__((constructor)) void RegisterModule(void)
225 {
226     napi_module_register(&demoModule);
227 }
228