2
* Copyright (c) 2024 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
7
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
#define NAPI_FUNCTIONS(op) \
20
op(napi_module_register) \
21
op(napi_create_function) \
22
op(napi_set_named_property) \
23
op(napi_create_string_utf8) \
24
op(napi_add_env_cleanup_hook) \
25
op(napi_get_last_error_info) \
26
op(napi_get_value_bigint_uint64) \
27
op(napi_create_object) \
28
op(napi_get_arraybuffer_info) \
29
op(napi_create_bigint_uint64) \
30
op(napi_is_typedarray) \
31
op(napi_add_finalizer) \
32
op(napi_get_typedarray_info) \
33
op(napi_set_property) \
34
op(napi_get_value_bool) \
35
op(napi_coerce_to_string) \
36
op(napi_get_value_uint32) \
37
op(napi_get_value_int32) \
39
op(napi_get_cb_info) \
40
op(napi_create_error) \
41
op(napi_get_value_string_utf8) \
42
op(napi_define_properties) \
43
op(napi_delete_reference) \
44
op(napi_get_reference_value) \
45
op(napi_open_handle_scope) \
46
op(napi_close_handle_scope) \
47
op(napi_open_escapable_handle_scope) \
48
op(napi_close_escapable_handle_scope) \
49
op(napi_is_exception_pending) \
50
op(napi_create_type_error) \
51
op(napi_escape_handle) \
52
op(napi_get_and_clear_last_exception) \
53
op(napi_fatal_error) \
54
op(napi_create_double) \
56
op(napi_get_property) \
57
op(napi_get_named_property) \
58
op(napi_create_reference) \
60
op(napi_has_property) \
61
op(napi_get_undefined) \
62
op(napi_get_value_double) \
63
op(napi_close_callback_scope) \
64
op(napi_async_destroy) \
65
op(napi_call_function)
67
#define DECL_NAPI_IMPL(fn_name, ...) decltype(&fn_name) p_##fn_name;
69
NAPI_FUNCTIONS(DECL_NAPI_IMPL)
71
bool LoadNapiFunctions() {
72
static bool isLoaded = false;
73
if (isLoaded) return true;
74
fprintf(stderr, "Init NAPI\n");
75
HMODULE nodeModule = GetModuleHandle(NULL);
76
FARPROC fn_addr = GetProcAddress(nodeModule, "napi_module_register");
78
if (fn_addr == NULL) {
79
nodeModule = GetModuleHandleA("node.dll");
80
if (nodeModule == NULL) return false;
81
fn_addr = GetProcAddress(nodeModule, "napi_module_register");
82
if (fn_addr == NULL) {
86
bool apiLoadFailed = false;
88
#define GET_NAPI_IMPL(fn_name) \
89
fn_addr = GetProcAddress(nodeModule, #fn_name); \
90
if (fn_addr == NULL) apiLoadFailed = true; \
91
p_##fn_name = (decltype(p_##fn_name))fn_addr;
93
// Assign the addresses of the needed functions to the "p*" named pointers.
94
NAPI_FUNCTIONS(GET_NAPI_IMPL);
96
// If any required APIs failed to load, return false
97
if (apiLoadFailed) return false;
104
NAPI_EXTERN void NAPI_CDECL
105
napi_module_register(napi_module* mod) {
107
p_napi_module_register(mod);
110
NAPI_EXTERN napi_status NAPI_CDECL
111
napi_get_last_error_info(napi_env env, const napi_extended_error_info** result) {
113
return p_napi_get_last_error_info(env, result);
116
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bigint_uint64(
117
napi_env env, napi_value value, uint64_t* result, bool* lossless) {
119
return p_napi_get_value_bigint_uint64(env, value, result, lossless);
122
NAPI_EXTERN napi_status NAPI_CDECL napi_create_object(napi_env env, napi_value* result) {
124
return p_napi_create_object(env, result);
127
NAPI_EXTERN napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env, napi_value arraybuffer, void** data, size_t* byte_length) {
129
return p_napi_get_arraybuffer_info(env, arraybuffer, data, byte_length);
132
NAPI_EXTERN napi_status NAPI_CDECL
133
napi_create_bigint_uint64(napi_env env, uint64_t value, napi_value* result) {
135
return p_napi_create_bigint_uint64(env, value, result);
138
NAPI_EXTERN napi_status NAPI_CDECL napi_is_typedarray(napi_env env, napi_value value, bool* result) {
140
return p_napi_is_typedarray(env, value, result);
143
NAPI_EXTERN napi_status NAPI_CDECL
144
napi_add_finalizer(napi_env env,
145
napi_value js_object,
147
node_api_nogc_finalize finalize_cb,
151
return p_napi_add_finalizer(env, js_object, finalize_data, finalize_cb, finalize_hint, result);
155
NAPI_EXTERN napi_status NAPI_CDECL
156
napi_get_typedarray_info(napi_env env,
157
napi_value typedarray,
158
napi_typedarray_type* type,
161
napi_value* arraybuffer,
162
size_t* byte_offset) {
164
return p_napi_get_typedarray_info(env, typedarray, type, length, data, arraybuffer, byte_offset);
167
NAPI_EXTERN napi_status NAPI_CDECL napi_set_property(napi_env env,
172
return p_napi_set_property(env, object, key, value);
175
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bool(napi_env env,
179
return p_napi_get_value_bool(env, value, result);
182
NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_string(napi_env env,
184
napi_value* result) {
186
return p_napi_coerce_to_string(env, value, result);
189
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_int32(napi_env env,
193
return p_napi_get_value_int32(env, value, result);
196
NAPI_EXTERN napi_status NAPI_CDECL napi_get_cb_info(
198
napi_callback_info cbinfo,
201
napi_value* this_arg,
204
return p_napi_get_cb_info(env, cbinfo, argc, argv, this_arg, data);
207
NAPI_EXTERN napi_status NAPI_CDECL napi_create_string_utf8(napi_env env,
210
napi_value* result) {
212
return p_napi_create_string_utf8(env, str, length, result);
216
NAPI_EXTERN napi_status NAPI_CDECL napi_throw(napi_env env, napi_value error) {
218
return p_napi_throw(env, error);
221
NAPI_EXTERN napi_status NAPI_CDECL napi_create_error(napi_env env,
224
napi_value* result) {
226
return p_napi_create_error(env, code, msg, result);
229
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_string_utf8(
230
napi_env env, napi_value value, char* buf, size_t bufsize, size_t* result) {
232
return p_napi_get_value_string_utf8(env, value, buf, bufsize, result);
235
NAPI_EXTERN napi_status NAPI_CDECL
236
napi_define_properties(napi_env env,
238
size_t property_count,
239
const napi_property_descriptor* properties) {
241
return p_napi_define_properties(env, object, property_count, properties);
244
NAPI_EXTERN napi_status NAPI_CDECL napi_delete_reference(napi_env env,
247
return p_napi_delete_reference(env, ref);
250
NAPI_EXTERN napi_status NAPI_CDECL napi_get_reference_value(napi_env env,
252
napi_value* result) {
254
return p_napi_get_reference_value(env, ref, result);
256
NAPI_EXTERN napi_status NAPI_CDECL
257
napi_open_handle_scope(napi_env env, napi_handle_scope* result) {
259
return p_napi_open_handle_scope(env, result);
262
NAPI_EXTERN napi_status NAPI_CDECL
263
napi_close_handle_scope(napi_env env, napi_handle_scope scope) {
265
return p_napi_close_handle_scope(env, scope);
267
NAPI_EXTERN napi_status NAPI_CDECL napi_open_escapable_handle_scope(
268
napi_env env, napi_escapable_handle_scope* result) {
269
return p_napi_open_escapable_handle_scope(env, result);
271
NAPI_EXTERN napi_status NAPI_CDECL napi_close_escapable_handle_scope(
272
napi_env env, napi_escapable_handle_scope scope) {
274
return p_napi_close_escapable_handle_scope(env, scope);
276
NAPI_EXTERN napi_status NAPI_CDECL napi_is_exception_pending(napi_env env,
279
return p_napi_is_exception_pending(env, result);
282
NAPI_EXTERN napi_status NAPI_CDECL napi_get_property(napi_env env,
285
napi_value* result) {
287
return p_napi_get_property(env, object, key, result);
290
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_uint32(napi_env env,
294
return p_napi_get_value_uint32(env, value, result);
297
NAPI_EXTERN napi_status NAPI_CDECL napi_typeof(napi_env env,
299
napi_valuetype* result) {
301
return p_napi_typeof(env, value, result);
303
NAPI_EXTERN napi_status NAPI_CDECL
304
napi_get_and_clear_last_exception(napi_env env, napi_value* result) {
306
return p_napi_get_and_clear_last_exception(env, result);
308
NAPI_EXTERN NAPI_NO_RETURN void NAPI_CDECL
309
napi_fatal_error(const char* location,
312
size_t message_len) {
314
p_napi_fatal_error(location, location_len, message, message_len);
317
NAPI_EXTERN napi_status NAPI_CDECL napi_create_double(napi_env env,
319
napi_value* result) {
321
return p_napi_create_double(env, value, result);
324
NAPI_EXTERN napi_status NAPI_CDECL napi_create_type_error(napi_env env,
327
napi_value* result) {
329
return p_napi_create_type_error(env, code, msg, result);
331
NAPI_EXTERN napi_status NAPI_CDECL napi_get_named_property(napi_env env,
333
const char* utf8name,
334
napi_value* result) {
337
NAPI_EXTERN napi_status NAPI_CDECL
338
napi_create_reference(napi_env env,
340
uint32_t initial_refcount,
343
return p_napi_create_reference(env, value, initial_refcount, result);
346
NAPI_EXTERN napi_status NAPI_CDECL
347
napi_escape_handle(napi_env env,
348
napi_escapable_handle_scope scope,
351
NAPI_EXTERN napi_status NAPI_CDECL napi_get_global(napi_env env,
352
napi_value* result) {
355
return p_napi_get_global(env, result);
357
NAPI_EXTERN napi_status NAPI_CDECL napi_has_property(napi_env env,
362
return p_napi_has_property(env, object, key, result);
364
NAPI_EXTERN napi_status NAPI_CDECL napi_create_function(napi_env env,
365
const char* utf8name,
369
napi_value* result) {
371
return p_napi_create_function(env, utf8name, length, cb, data, result);
374
NAPI_EXTERN napi_status NAPI_CDECL
375
napi_escape_handle(napi_env env,
376
napi_escapable_handle_scope scope,
378
napi_value* result) {
380
return p_napi_escape_handle(env, scope, escapee, result);
382
NAPI_EXTERN napi_status NAPI_CDECL napi_get_undefined(napi_env env,
383
napi_value* result) {
385
return p_napi_get_undefined(env, result);
387
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_double(napi_env env,
391
return p_napi_get_value_double(env, value, result);
394
NAPI_EXTERN napi_status NAPI_CDECL
395
napi_close_callback_scope(napi_env env, napi_callback_scope scope) {
397
return p_napi_close_callback_scope(env, scope);
400
NAPI_EXTERN napi_status NAPI_CDECL
401
napi_async_destroy(napi_env env, napi_async_context async_context) {
403
return p_napi_async_destroy(env, async_context);
406
NAPI_EXTERN napi_status NAPI_CDECL napi_call_function(napi_env env,
410
const napi_value* argv,
411
napi_value* result) {
413
return p_napi_call_function(env, recv, func, argc, argv, result);