idlize

Форк
0
/
win-dynamic-node.cc 
414 строк · 15.3 Кб
1
/*
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
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
#include <stdio.h>
16
#include <windows.h>
17
#include "node_api.h"
18

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) \
38
   op(napi_throw) \
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) \
55
   op(napi_typeof) \
56
   op(napi_get_property) \
57
   op(napi_get_named_property) \
58
   op(napi_create_reference) \
59
   op(napi_get_global) \
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)
66

67
#define DECL_NAPI_IMPL(fn_name, ...) decltype(&fn_name) p_##fn_name;
68

69
NAPI_FUNCTIONS(DECL_NAPI_IMPL)
70

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");
77

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) {
83
      return false;
84
    }
85
  }
86
  bool apiLoadFailed = false;
87

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;
92

93
  // Assign the addresses of the needed functions to the "p*" named pointers.
94
  NAPI_FUNCTIONS(GET_NAPI_IMPL);
95

96
  // If any required APIs failed to load, return false
97
  if (apiLoadFailed) return false;
98

99
  isLoaded = true;
100

101
  return true;
102
}
103

104
NAPI_EXTERN void NAPI_CDECL
105
napi_module_register(napi_module* mod) {
106
    LoadNapiFunctions();
107
    p_napi_module_register(mod);
108
}
109

110
NAPI_EXTERN napi_status NAPI_CDECL
111
napi_get_last_error_info(napi_env env, const napi_extended_error_info** result) {
112
    LoadNapiFunctions();
113
    return p_napi_get_last_error_info(env, result);
114
}
115

116
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bigint_uint64(
117
    napi_env env, napi_value value, uint64_t* result, bool* lossless) {
118
  LoadNapiFunctions();
119
  return p_napi_get_value_bigint_uint64(env, value, result, lossless);
120
}
121

122
NAPI_EXTERN napi_status NAPI_CDECL napi_create_object(napi_env env, napi_value* result) {
123
  LoadNapiFunctions();
124
  return p_napi_create_object(env, result);
125
}
126

127
NAPI_EXTERN napi_status NAPI_CDECL napi_get_arraybuffer_info(napi_env env, napi_value arraybuffer, void** data, size_t* byte_length) {
128
  LoadNapiFunctions();
129
  return p_napi_get_arraybuffer_info(env, arraybuffer, data, byte_length);
130
}
131

132
NAPI_EXTERN napi_status NAPI_CDECL
133
napi_create_bigint_uint64(napi_env env, uint64_t value, napi_value* result) {
134
  LoadNapiFunctions();
135
  return p_napi_create_bigint_uint64(env, value, result);
136
}
137

138
NAPI_EXTERN napi_status NAPI_CDECL napi_is_typedarray(napi_env env, napi_value value, bool* result) {
139
  LoadNapiFunctions();
140
  return p_napi_is_typedarray(env, value, result);
141
}
142

143
NAPI_EXTERN napi_status NAPI_CDECL
144
napi_add_finalizer(napi_env env,
145
                   napi_value js_object,
146
                   void* finalize_data,
147
                   node_api_nogc_finalize finalize_cb,
148
                   void* finalize_hint,
149
                   napi_ref* result) {
150
  LoadNapiFunctions();
151
  return p_napi_add_finalizer(env, js_object, finalize_data, finalize_cb, finalize_hint, result);
152
}
153

154

155
NAPI_EXTERN napi_status NAPI_CDECL
156
napi_get_typedarray_info(napi_env env,
157
                         napi_value typedarray,
158
                         napi_typedarray_type* type,
159
                         size_t* length,
160
                         void** data,
161
                         napi_value* arraybuffer,
162
                         size_t* byte_offset) {
163
  LoadNapiFunctions();
164
  return p_napi_get_typedarray_info(env, typedarray, type, length, data, arraybuffer, byte_offset);
165
}
166

167
NAPI_EXTERN napi_status NAPI_CDECL napi_set_property(napi_env env,
168
                                                     napi_value object,
169
                                                     napi_value key,
170
                                                     napi_value value) {
171
  LoadNapiFunctions();
172
  return p_napi_set_property(env, object, key, value);
173
}
174

175
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_bool(napi_env env,
176
                                                       napi_value value,
177
                                                       bool* result) {
178
  LoadNapiFunctions();
179
  return p_napi_get_value_bool(env, value, result);
180
}
181

182
NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_string(napi_env env,
183
                                                         napi_value value,
184
                                                         napi_value* result) {
185
  LoadNapiFunctions();
186
  return p_napi_coerce_to_string(env, value, result);
187
}
188

189
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_int32(napi_env env,
190
                                                        napi_value value,
191
                                                        int32_t* result) {
192
  LoadNapiFunctions();
193
  return p_napi_get_value_int32(env, value, result);
194
}
195

196
NAPI_EXTERN napi_status NAPI_CDECL napi_get_cb_info(
197
    napi_env env,
198
    napi_callback_info cbinfo,
199
    size_t* argc,
200
    napi_value* argv,
201
    napi_value* this_arg,
202
    void** data) {
203
  LoadNapiFunctions();
204
  return p_napi_get_cb_info(env, cbinfo, argc, argv, this_arg, data);
205
}
206

207
NAPI_EXTERN napi_status NAPI_CDECL napi_create_string_utf8(napi_env env,
208
                                                           const char* str,
209
                                                           size_t length,
210
                                                           napi_value* result) {
211
  LoadNapiFunctions();
212
  return p_napi_create_string_utf8(env, str, length, result);
213
}
214

215

216
NAPI_EXTERN napi_status NAPI_CDECL napi_throw(napi_env env, napi_value error) {
217
  LoadNapiFunctions();
218
  return p_napi_throw(env, error);
219
}
220

221
NAPI_EXTERN napi_status NAPI_CDECL napi_create_error(napi_env env,
222
                                                     napi_value code,
223
                                                     napi_value msg,
224
                                                     napi_value* result) {
225
  LoadNapiFunctions();
226
  return p_napi_create_error(env, code, msg, result);
227
}
228

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) {
231
  LoadNapiFunctions();
232
  return p_napi_get_value_string_utf8(env, value, buf, bufsize, result);
233
}
234

235
NAPI_EXTERN napi_status NAPI_CDECL
236
napi_define_properties(napi_env env,
237
                       napi_value object,
238
                       size_t property_count,
239
                       const napi_property_descriptor* properties) {
240
  LoadNapiFunctions();
241
  return p_napi_define_properties(env, object, property_count, properties);
242
}
243

244
NAPI_EXTERN napi_status NAPI_CDECL napi_delete_reference(napi_env env,
245
                                                         napi_ref ref) {
246
  LoadNapiFunctions();
247
  return p_napi_delete_reference(env, ref);
248
}
249

250
NAPI_EXTERN napi_status NAPI_CDECL napi_get_reference_value(napi_env env,
251
                                                            napi_ref ref,
252
                                                            napi_value* result) {
253
  LoadNapiFunctions();
254
  return p_napi_get_reference_value(env, ref, result);
255
}
256
NAPI_EXTERN napi_status NAPI_CDECL
257
napi_open_handle_scope(napi_env env, napi_handle_scope* result) {
258
  LoadNapiFunctions();
259
  return p_napi_open_handle_scope(env, result);
260
}
261

262
NAPI_EXTERN napi_status NAPI_CDECL
263
napi_close_handle_scope(napi_env env, napi_handle_scope scope) {
264
  LoadNapiFunctions();
265
  return p_napi_close_handle_scope(env, scope);
266
}
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);
270
}
271
NAPI_EXTERN napi_status NAPI_CDECL napi_close_escapable_handle_scope(
272
    napi_env env, napi_escapable_handle_scope scope) {
273
  LoadNapiFunctions();
274
  return p_napi_close_escapable_handle_scope(env, scope);
275
}
276
NAPI_EXTERN napi_status NAPI_CDECL napi_is_exception_pending(napi_env env,
277
                                                             bool* result) {
278
  LoadNapiFunctions();
279
  return p_napi_is_exception_pending(env, result);
280
}
281

282
NAPI_EXTERN napi_status NAPI_CDECL napi_get_property(napi_env env,
283
                                                     napi_value object,
284
                                                     napi_value key,
285
                                                     napi_value* result) {
286
  LoadNapiFunctions();
287
  return p_napi_get_property(env, object, key, result);
288
}
289

290
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_uint32(napi_env env,
291
                                                         napi_value value,
292
                                                         uint32_t* result) {
293
  LoadNapiFunctions();
294
  return p_napi_get_value_uint32(env, value, result);
295
}
296

297
NAPI_EXTERN napi_status NAPI_CDECL napi_typeof(napi_env env,
298
                                               napi_value value,
299
                                               napi_valuetype* result) {
300
  LoadNapiFunctions();
301
  return p_napi_typeof(env, value, result);
302
}
303
NAPI_EXTERN napi_status NAPI_CDECL
304
napi_get_and_clear_last_exception(napi_env env, napi_value* result) {
305
  LoadNapiFunctions();
306
  return p_napi_get_and_clear_last_exception(env, result);
307
}
308
NAPI_EXTERN NAPI_NO_RETURN void NAPI_CDECL
309
napi_fatal_error(const char* location,
310
                 size_t location_len,
311
                 const char* message,
312
                 size_t message_len) {
313
  LoadNapiFunctions();
314
  p_napi_fatal_error(location, location_len, message, message_len);
315
}
316

317
NAPI_EXTERN napi_status NAPI_CDECL napi_create_double(napi_env env,
318
                                                      double value,
319
                                                      napi_value* result) {
320
  LoadNapiFunctions();
321
  return p_napi_create_double(env, value, result);
322
}
323

324
NAPI_EXTERN napi_status NAPI_CDECL napi_create_type_error(napi_env env,
325
                                                          napi_value code,
326
                                                          napi_value msg,
327
                                                          napi_value* result) {
328
  LoadNapiFunctions();
329
  return p_napi_create_type_error(env, code, msg, result);
330
}
331
NAPI_EXTERN napi_status NAPI_CDECL napi_get_named_property(napi_env env,
332
                                                           napi_value object,
333
                                                           const char* utf8name,
334
                                                           napi_value* result) {
335
  return napi_ok;
336
}
337
NAPI_EXTERN napi_status NAPI_CDECL
338
napi_create_reference(napi_env env,
339
                      napi_value value,
340
                      uint32_t initial_refcount,
341
                      napi_ref* result) {
342
  LoadNapiFunctions();
343
  return p_napi_create_reference(env, value, initial_refcount, result);
344
}
345

346
NAPI_EXTERN napi_status NAPI_CDECL
347
napi_escape_handle(napi_env env,
348
                   napi_escapable_handle_scope scope,
349
                   napi_value escapee,
350
                   napi_value* result);
351
NAPI_EXTERN napi_status NAPI_CDECL napi_get_global(napi_env env,
352
                                                   napi_value* result) {
353

354
  LoadNapiFunctions();
355
  return p_napi_get_global(env, result);
356
}
357
NAPI_EXTERN napi_status NAPI_CDECL napi_has_property(napi_env env,
358
                                                     napi_value object,
359
                                                     napi_value key,
360
                                                     bool* result) {
361
  LoadNapiFunctions();
362
  return p_napi_has_property(env, object, key, result);
363
}
364
NAPI_EXTERN napi_status NAPI_CDECL napi_create_function(napi_env env,
365
                                                        const char* utf8name,
366
                                                        size_t length,
367
                                                        napi_callback cb,
368
                                                        void* data,
369
                                                        napi_value* result) {
370
  LoadNapiFunctions();
371
  return p_napi_create_function(env, utf8name, length, cb, data, result);
372
}
373

374
NAPI_EXTERN napi_status NAPI_CDECL
375
napi_escape_handle(napi_env env,
376
                   napi_escapable_handle_scope scope,
377
                   napi_value escapee,
378
                   napi_value* result) {
379
  LoadNapiFunctions();
380
  return p_napi_escape_handle(env, scope, escapee, result);
381
}
382
NAPI_EXTERN napi_status NAPI_CDECL napi_get_undefined(napi_env env,
383
                                                      napi_value* result) {
384
  LoadNapiFunctions();
385
  return p_napi_get_undefined(env, result);
386
}
387
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_double(napi_env env,
388
                                                         napi_value value,
389
                                                         double* result) {
390
  LoadNapiFunctions();
391
  return p_napi_get_value_double(env, value, result);
392
}
393

394
NAPI_EXTERN napi_status NAPI_CDECL
395
napi_close_callback_scope(napi_env env, napi_callback_scope scope) {
396
  LoadNapiFunctions();
397
  return p_napi_close_callback_scope(env, scope);
398
}
399

400
NAPI_EXTERN napi_status NAPI_CDECL
401
napi_async_destroy(napi_env env, napi_async_context async_context) {
402
  LoadNapiFunctions();
403
  return p_napi_async_destroy(env, async_context);
404
}
405

406
NAPI_EXTERN napi_status NAPI_CDECL napi_call_function(napi_env env,
407
                                                      napi_value recv,
408
                                                      napi_value func,
409
                                                      size_t argc,
410
                                                      const napi_value* argv,
411
                                                      napi_value* result) {
412
  LoadNapiFunctions();
413
  return p_napi_call_function(env, recv, func, argc, argv, result);
414
}
415

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.