emnapi 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CMakeLists.txt CHANGED
@@ -70,7 +70,7 @@ if(EMNAPI_FIND_NODE_ADDON_API)
70
70
  else()
71
71
  string(REGEX REPLACE "(\r?\n)|\"" "" OUTPUT_NAA_INCLUDE_DIR "${OUTPUT_NAA_INCLUDE_DIR}")
72
72
  string(REPLACE "\\" "/" OUTPUT_NAA_INCLUDE_DIR "${OUTPUT_NAA_INCLUDE_DIR}")
73
- list(PREPEND EMNAPI_INCLUDE "${OUTPUT_NAA_INCLUDE_DIR}")
73
+ list(APPEND EMNAPI_INCLUDE "${OUTPUT_NAA_INCLUDE_DIR}")
74
74
  endif()
75
75
  endif()
76
76
 
@@ -191,8 +191,6 @@ install(FILES
191
191
  ${CMAKE_CURRENT_SOURCE_DIR}/include/emnapi.h
192
192
  ${CMAKE_CURRENT_SOURCE_DIR}/include/js_native_api_types.h
193
193
  ${CMAKE_CURRENT_SOURCE_DIR}/include/js_native_api.h
194
- ${CMAKE_CURRENT_SOURCE_DIR}/include/napi-inl.h
195
- ${CMAKE_CURRENT_SOURCE_DIR}/include/napi.h
196
194
  ${CMAKE_CURRENT_SOURCE_DIR}/include/node_api_types.h
197
195
  ${CMAKE_CURRENT_SOURCE_DIR}/include/node_api.h
198
196
  ${CMAKE_CURRENT_SOURCE_DIR}/include/uv.h
package/README.md CHANGED
@@ -35,6 +35,7 @@ You will need to install:
35
35
  - (Optional) CMake `>= v3.13`
36
36
  - (Optional) ninja
37
37
  - (Optional) make
38
+ - (Optional) [node-addon-api](https://github.com/nodejs/node-addon-api) `>= 6.1.0`
38
39
 
39
40
  There are several choices to get `make` for Windows user
40
41
 
@@ -94,6 +95,9 @@ npm install @emnapi/runtime
94
95
 
95
96
  # for non-emscripten
96
97
  npm install @emnapi/core
98
+
99
+ # if you use node-addon-api
100
+ npm install node-addon-api
97
101
  ```
98
102
 
99
103
  Each package should match the same version.
@@ -404,9 +408,13 @@ instantiateNapiModule(fetch('./hello.wasm'), {
404
408
 
405
409
  </details>
406
410
 
407
- ### Using C++
411
+ ### Using C++ and node-addon-api
408
412
 
409
- Alternatively, you can also use [`node-addon-api`](https://github.com/nodejs/node-addon-api) which is official Node-API C++ wrapper, already shipped ([v6.0.0](https://github.com/nodejs/node-addon-api/releases/tag/v6.0.0)) in this package but without Node.js specific API such as `CallbackScope`.
413
+ Require [`node-addon-api`](https://github.com/nodejs/node-addon-api) `>= 6.1.0`
414
+
415
+ ```bash
416
+ npm install node-addon-api
417
+ ```
410
418
 
411
419
  **Note: C++ wrapper can only be used to target Node.js v14.6.0+ and modern browsers those support `FinalizationRegistry` and `WeakRef` ([v8 engine v8.4+](https://v8.dev/blog/v8-release-84))!**
412
420
 
@@ -441,6 +449,7 @@ em++ -O3 \
441
449
  -DNAPI_DISABLE_CPP_EXCEPTIONS \
442
450
  -DNODE_ADDON_API_ENABLE_MAYBE \
443
451
  -I./node_modules/emnapi/include \
452
+ -I./node_modules/node-addon-api \
444
453
  -L./node_modules/emnapi/lib/wasm32-emscripten \
445
454
  --js-library=./node_modules/emnapi/dist/library_napi.js \
446
455
  -sEXPORTED_FUNCTIONS="['_napi_register_wasm_v1','_malloc','_free']" \
@@ -459,6 +468,7 @@ clang++ -O3 \
459
468
  -DNAPI_DISABLE_CPP_EXCEPTIONS \
460
469
  -DNODE_ADDON_API_ENABLE_MAYBE \
461
470
  -I./node_modules/emnapi/include \
471
+ -I./node_modules/node-addon-api \
462
472
  -L./node_modules/emnapi/lib/wasm32-wasi \
463
473
  --target=wasm32-wasi \
464
474
  --sysroot=$WASI_SDK_PATH/share/wasi-sysroot \
@@ -563,15 +573,20 @@ elseif((CMAKE_C_COMPILER_TARGET STREQUAL "wasm32") OR (CMAKE_C_COMPILER_TARGET S
563
573
  endif()
564
574
  ```
565
575
 
576
+ If you use node-addon-api, you can use `-DEMNAPI_FIND_NODE_ADDON_API=ON` or manually add node-addon-api directory to the include dir via `include_directories()` or `target_include_directories()`.
577
+
566
578
  ```bash
567
579
  mkdir build
568
580
 
569
581
  # emscripten
570
- emcmake cmake -DCMAKE_BUILD_TYPE=Release -G Ninja -H. -Bbuild
582
+ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
583
+ -DEMNAPI_FIND_NODE_ADDON_API=ON \
584
+ -G Ninja -H. -Bbuild
571
585
 
572
586
  # wasi-sdk
573
587
  cmake -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk.cmake \
574
588
  -DWASI_SDK_PREFIX=$WASI_SDK_PATH \
589
+ -DEMNAPI_FIND_NODE_ADDON_API=ON \
575
590
  -DCMAKE_BUILD_TYPE=Release \
576
591
  -G Ninja -H. -Bbuild
577
592
 
@@ -848,11 +863,15 @@ endif()
848
863
 
849
864
  ```bash
850
865
  # emscripten
851
- emcmake cmake -DCMAKE_BUILD_TYPE=Release -DEMNAPI_WORKER_POOL_SIZE=4 -G Ninja -H. -Bbuild
866
+ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
867
+ -DEMNAPI_FIND_NODE_ADDON_API=ON \
868
+ -DEMNAPI_WORKER_POOL_SIZE=4 \
869
+ -G Ninja -H. -Bbuild
852
870
 
853
871
  # wasi-sdk with thread support (Experimental)
854
872
  cmake -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk-pthread.cmake \
855
873
  -DWASI_SDK_PREFIX=$WASI_SDK_PATH \
874
+ -DEMNAPI_FIND_NODE_ADDON_API=ON \
856
875
  -DCMAKE_BUILD_TYPE=Release \
857
876
  -G Ninja -H. -Bbuild
858
877
 
@@ -1043,47 +1062,48 @@ This option only has effect if you use emscripten `-pthread`. Default is `1` if
1043
1062
 
1044
1063
  See source code [here](https://github.com/toyobayashi/emnapi/tree/main/packages/bench)
1045
1064
 
1046
- - OS: Windows_NT x64 10.0.22621.963
1047
- - CPU: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz 2.30 GHz
1048
- - Memory: 16.0 GB
1049
- - Chrome: 108.0.5359.125
1050
- - Emscripten: 3.1.28
1051
- - emnapi: 0.22.0
1065
+ - OS: macOS 13.3.1 22E261 arm64
1066
+ - CPU: Apple M2 Pro
1067
+ - Memory: 32.0 GB
1068
+ - Chrome: 112.0.5615.137
1069
+ - Emscripten: 3.1.36
1070
+ - emnapi: 0.38.0
1071
+ - node-addon-api 6.1.0
1052
1072
 
1053
1073
  ```
1054
1074
  binding: function () {}
1055
- embind #emptyFunction x 37,148,158 ops/sec ±0.39% (67 runs sampled)
1056
- emnapi #emptyFunction x 40,207,668 ops/sec ±0.79% (67 runs sampled)
1057
- node-addon-api + emnapi #emptyFunction x 13,871,523 ops/sec ±0.35% (67 runs sampled)
1075
+ embind #emptyFunction x 49,956,250 ops/sec ±0.32% (68 runs sampled)
1076
+ emnapi #emptyFunction x 57,543,501 ops/sec ±0.76% (67 runs sampled)
1077
+ node-addon-api + emnapi #emptyFunction x 21,352,147 ops/sec ±0.62% (66 runs sampled)
1058
1078
  Fastest is emnapi #emptyFunction
1059
1079
 
1060
1080
  binding: function (obj) { return obj }
1061
- embind #returnParam x 19,230,099 ops/sec ±0.59% (68 runs sampled)
1062
- emnapi #returnParam x 14,930,264 ops/sec ±0.68% (64 runs sampled)
1063
- node-addon-api + emnapi #returnParam x 11,402,133 ops/sec ±0.47% (67 runs sampled)
1064
- Fastest is embind #returnParam
1081
+ embind #returnParam x 21,426,261 ops/sec ±0.67% (67 runs sampled)
1082
+ emnapi #returnParam x 25,111,317 ops/sec ±0.40% (68 runs sampled)
1083
+ node-addon-api + emnapi #returnParam x 17,811,804 ops/sec ±0.24% (68 runs sampled)
1084
+ Fastest is emnapi #returnParam
1065
1085
 
1066
1086
  binding: function (int) { return copy(int) }
1067
- embind #convertInteger x 9,178,287 ops/sec ±0.67% (67 runs sampled)
1068
- emnapi #convertInteger x 9,898,681 ops/sec ±0.56% (66 runs sampled)
1069
- node-addon-api + emnapi #convertInteger x 7,888,491 ops/sec ±0.79% (67 runs sampled)
1087
+ embind #convertInteger x 9,748,310 ops/sec ±0.52% (68 runs sampled)
1088
+ emnapi #convertInteger x 16,128,395 ops/sec ±0.38% (70 runs sampled)
1089
+ node-addon-api + emnapi #convertInteger x 11,692,687 ops/sec ±0.41% (67 runs sampled)
1070
1090
  Fastest is emnapi #convertInteger
1071
1091
 
1072
1092
  binding: function (str) { return copy(str) }
1073
- embind #convertString x 2,574,960 ops/sec ±0.46% (66 runs sampled)
1074
- emnapi #convertString x 3,412,941 ops/sec ±0.66% (68 runs sampled)
1075
- node-addon-api + emnapi #convertString x 2,913,797 ops/sec ±0.62% (67 runs sampled)
1093
+ embind #convertString x 3,681,066 ops/sec ±0.24% (69 runs sampled)
1094
+ emnapi #convertString x 4,555,446 ops/sec ±0.40% (69 runs sampled)
1095
+ node-addon-api + emnapi #convertString x 3,826,496 ops/sec ±0.24% (68 runs sampled)
1076
1096
  Fastest is emnapi #convertString
1077
1097
 
1078
1098
  binding: function (param) { return param.length }
1079
- embind #ObjectGet x 6,192,531 ops/sec ±0.73% (67 runs sampled)
1080
- emnapi #ObjectGet x 5,268,653 ops/sec ±0.59% (66 runs sampled)
1081
- node-addon-api + emnapi #ObjectGet x 4,828,204 ops/sec ±0.57% (67 runs sampled)
1082
- Fastest is embind #ObjectGet
1099
+ embind #ObjectGet x 6,874,783 ops/sec ±0.39% (68 runs sampled)
1100
+ emnapi #ObjectGet x 6,926,130 ops/sec ±0.34% (69 runs sampled)
1101
+ node-addon-api + emnapi #ObjectGet x 6,075,822 ops/sec ±0.41% (68 runs sampled)
1102
+ Fastest is emnapi #ObjectGet
1083
1103
 
1084
1104
  binding: function (obj, key, value) { obj[key] = value }
1085
- embind #ObjectSet x 11,467,433 ops/sec ±0.55% (67 runs sampled)
1086
- emnapi #ObjectSet x 9,476,512 ops/sec ±0.91% (66 runs sampled)
1087
- node-addon-api + emnapi #ObjectSet x 7,647,341 ops/sec ±0.36% (66 runs sampled)
1088
- Fastest is embind #ObjectSet
1105
+ embind #ObjectSet x 11,184,221 ops/sec ±0.35% (66 runs sampled)
1106
+ emnapi #ObjectSet x 15,812,566 ops/sec ±0.64% (66 runs sampled)
1107
+ node-addon-api + emnapi #ObjectSet x 12,022,909 ops/sec ±0.51% (66 runs sampled)
1108
+ Fastest is emnapi #ObjectSet
1089
1109
  ```
@@ -403,7 +403,7 @@ emnapiImplement2('emnapi_is_support_weakref', 'i', emnapi_is_support_weakref);
403
403
  emnapiImplement2('emnapi_is_support_bigint', 'i', emnapi_is_support_bigint);
404
404
  emnapiImplement2('emnapi_is_node_binding_available', 'i', emnapi_is_node_binding_available);
405
405
  emnapiImplement2('emnapi_create_memory_view', 'ipippppp', _emnapi_create_memory_view, ['napi_add_finalizer', '$emnapiExternalMemory']);
406
- emnapiImplement2('emnapi_sync_memory', 'ipppppi', emnapi_sync_memory, ['$emnapiSyncMemory']);
406
+ emnapiImplement2('emnapi_sync_memory', 'ipippp', emnapi_sync_memory, ['$emnapiSyncMemory']);
407
407
  emnapiImplement2('emnapi_get_memory_address', 'ipppp', emnapi_get_memory_address, ['$emnapiGetMemoryAddress']);
408
408
  function napi_set_instance_data(env, data, finalize_cb, finalize_hint) {
409
409
  if (env == 0)
@@ -2465,22 +2465,15 @@ function napi_run_script(env, script, result) {
2465
2465
  return status;
2466
2466
  }
2467
2467
  emnapiImplement('napi_run_script', 'ippp', napi_run_script, ['napi_set_last_error']);
2468
- /* eslint-disable @typescript-eslint/indent */
2469
2468
  var emnapiString = {
2470
2469
  utf8Decoder: undefined,
2471
2470
  utf16Decoder: undefined,
2472
2471
  init: function () {
2473
2472
  #if !TEXTDECODER || TEXTDECODER == 1
2474
2473
  var fallbackDecoder = {
2475
- decode: function (input) {
2476
- var isArrayBuffer = input instanceof ArrayBuffer;
2477
- var isView = ArrayBuffer.isView(input);
2478
- if (!isArrayBuffer && !isView) {
2479
- throw new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView');
2480
- }
2481
- var bytes = isArrayBuffer ? new Uint8Array(input) : new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
2474
+ decode: function (bytes) {
2482
2475
  var inputIndex = 0;
2483
- var pendingSize = Math.min(256 * 256, bytes.length + 1);
2476
+ var pendingSize = Math.min(0x1000, bytes.length + 1);
2484
2477
  var pending = new Uint16Array(pendingSize);
2485
2478
  var chunks = [];
2486
2479
  var pendingIndex = 0;
@@ -2540,17 +2533,18 @@ var emnapiString = {
2540
2533
  #if !TEXTDECODER || TEXTDECODER == 1
2541
2534
  var fallbackDecoder2 = {
2542
2535
  decode: function (input) {
2543
- var isArrayBuffer = input instanceof ArrayBuffer;
2544
- var isView = ArrayBuffer.isView(input);
2545
- if (!isArrayBuffer && !isView) {
2546
- throw new TypeError('The "input" argument must be an instance of ArrayBuffer or ArrayBufferView');
2536
+ var bytes = new Uint16Array(input.buffer, input.byteOffset, input.byteLength / 2);
2537
+ if (bytes.length <= 0x1000) {
2538
+ return String.fromCharCode.apply(null, bytes);
2547
2539
  }
2548
- var bytes = isArrayBuffer ? new Uint16Array(input) : new Uint16Array(input.buffer, input.byteOffset, input.byteLength / 2);
2549
- var wcharArray = Array(bytes.length);
2550
- for (var i = 0; i < bytes.length; ++i) {
2551
- wcharArray[i] = String.fromCharCode(bytes[i]);
2540
+ var chunks = [];
2541
+ var i = 0;
2542
+ var len = 0;
2543
+ for (; i < bytes.length; i += len) {
2544
+ len = Math.min(0x1000, bytes.length - i);
2545
+ chunks.push(String.fromCharCode.apply(null, bytes.subarray(i, i + len)));
2552
2546
  }
2553
- return wcharArray.join('');
2547
+ return chunks.join('');
2554
2548
  }
2555
2549
  };
2556
2550
  #endif
@@ -2586,9 +2580,9 @@ var emnapiString = {
2586
2580
  return len;
2587
2581
  },
2588
2582
  UTF8ToString: function (ptr, length) {
2589
- ptr >>>= 0;
2590
2583
  if (!ptr || !length)
2591
2584
  return '';
2585
+ ptr >>>= 0;
2592
2586
  var HEAPU8 = new Uint8Array(wasmMemory.buffer);
2593
2587
  var end = ptr;
2594
2588
  if (length === -1) {
@@ -2598,6 +2592,40 @@ var emnapiString = {
2598
2592
  else {
2599
2593
  end = ptr + (length >>> 0);
2600
2594
  }
2595
+ #if TEXTDECODER != 2
2596
+ length = end - ptr;
2597
+ if (length <= 16) {
2598
+ var idx = ptr;
2599
+ var str = '';
2600
+ while (idx < end) {
2601
+ var u0 = HEAPU8[idx++];
2602
+ if (!(u0 & 0x80)) {
2603
+ str += String.fromCharCode(u0);
2604
+ continue;
2605
+ }
2606
+ var u1 = HEAPU8[idx++] & 63;
2607
+ if ((u0 & 0xE0) === 0xC0) {
2608
+ str += String.fromCharCode(((u0 & 31) << 6) | u1);
2609
+ continue;
2610
+ }
2611
+ var u2 = HEAPU8[idx++] & 63;
2612
+ if ((u0 & 0xF0) === 0xE0) {
2613
+ u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
2614
+ }
2615
+ else {
2616
+ u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (HEAPU8[idx++] & 63);
2617
+ }
2618
+ if (u0 < 0x10000) {
2619
+ str += String.fromCharCode(u0);
2620
+ }
2621
+ else {
2622
+ var ch = u0 - 0x10000;
2623
+ str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
2624
+ }
2625
+ }
2626
+ return str;
2627
+ }
2628
+ #endif
2601
2629
  return emnapiString.utf8Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'end') }}});
2602
2630
  },
2603
2631
  stringToUTF8: function (str, outPtr, maxBytesToWrite) {
@@ -2646,9 +2674,9 @@ var emnapiString = {
2646
2674
  return outIdx - startIdx;
2647
2675
  },
2648
2676
  UTF16ToString: function (ptr, length) {
2649
- ptr >>>= 0;
2650
2677
  if (!ptr || !length)
2651
2678
  return '';
2679
+ ptr >>>= 0;
2652
2680
  var end = ptr;
2653
2681
  if (length === -1) {
2654
2682
  var idx = end >> 1;
@@ -2660,6 +2688,12 @@ var emnapiString = {
2660
2688
  else {
2661
2689
  end = ptr + (length >>> 0) * 2;
2662
2690
  }
2691
+ #if TEXTDECODER != 2
2692
+ length = end - ptr;
2693
+ if (length <= 32) {
2694
+ return String.fromCharCode.apply(null, new Uint16Array(wasmMemory.buffer, ptr, length / 2));
2695
+ }
2696
+ #endif
2663
2697
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2664
2698
  var HEAPU8 = new Uint8Array(wasmMemory.buffer);
2665
2699
  return emnapiString.utf16Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'end') }}});
Binary file
@@ -1,5 +1,5 @@
1
- emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.34 (57b21b8fdcbe3ebb523178b79465254668eab408)
2
- clang version 17.0.0 (https://github.com/llvm/llvm-project a031f72187ce495b9faa4ccf99b1e901a3872f4b)
1
+ emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.36 (518d9fea335f7cd2b5771e43df76e0535c6df5dd)
2
+ clang version 17.0.0 (https://github.com/llvm/llvm-project 5084abbea933b1a510556726a9e226b5ae22f19f)
3
3
  Target: wasm32-unknown-emscripten
4
4
  Thread model: posix
5
5
  InstalledDir: /home/runner/work/emnapi/emnapi/emsdk-cache/emsdk-main/upstream/bin
package/package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "name": "emnapi",
3
- "version": "0.37.0",
3
+ "version": "0.38.0",
4
4
  "description": "Node-API implementation for Emscripten",
5
5
  "main": "index.js",
6
+ "peerDependencies": {
7
+ "node-addon-api": ">= 6.1.0"
8
+ },
9
+ "peerDependenciesMeta": {
10
+ "node-addon-api": {
11
+ "optional": true
12
+ }
13
+ },
6
14
  "devDependencies": {
7
15
  "ts-macros": "2.0.1"
8
16
  },
@@ -1,186 +0,0 @@
1
- #ifndef SRC_NAPI_INL_DEPRECATED_H_
2
- #define SRC_NAPI_INL_DEPRECATED_H_
3
-
4
- ////////////////////////////////////////////////////////////////////////////////
5
- // PropertyDescriptor class
6
- ////////////////////////////////////////////////////////////////////////////////
7
-
8
- template <typename Getter>
9
- inline PropertyDescriptor PropertyDescriptor::Accessor(
10
- const char* utf8name,
11
- Getter getter,
12
- napi_property_attributes attributes,
13
- void* /*data*/) {
14
- using CbData = details::CallbackData<Getter, Napi::Value>;
15
- // TODO: Delete when the function is destroyed
16
- auto callbackData = new CbData({getter, nullptr});
17
-
18
- return PropertyDescriptor({utf8name,
19
- nullptr,
20
- nullptr,
21
- CbData::Wrapper,
22
- nullptr,
23
- nullptr,
24
- attributes,
25
- callbackData});
26
- }
27
-
28
- template <typename Getter>
29
- inline PropertyDescriptor PropertyDescriptor::Accessor(
30
- const std::string& utf8name,
31
- Getter getter,
32
- napi_property_attributes attributes,
33
- void* data) {
34
- return Accessor(utf8name.c_str(), getter, attributes, data);
35
- }
36
-
37
- template <typename Getter>
38
- inline PropertyDescriptor PropertyDescriptor::Accessor(
39
- napi_value name,
40
- Getter getter,
41
- napi_property_attributes attributes,
42
- void* /*data*/) {
43
- using CbData = details::CallbackData<Getter, Napi::Value>;
44
- // TODO: Delete when the function is destroyed
45
- auto callbackData = new CbData({getter, nullptr});
46
-
47
- return PropertyDescriptor({nullptr,
48
- name,
49
- nullptr,
50
- CbData::Wrapper,
51
- nullptr,
52
- nullptr,
53
- attributes,
54
- callbackData});
55
- }
56
-
57
- template <typename Getter>
58
- inline PropertyDescriptor PropertyDescriptor::Accessor(
59
- Name name, Getter getter, napi_property_attributes attributes, void* data) {
60
- napi_value nameValue = name;
61
- return PropertyDescriptor::Accessor(nameValue, getter, attributes, data);
62
- }
63
-
64
- template <typename Getter, typename Setter>
65
- inline PropertyDescriptor PropertyDescriptor::Accessor(
66
- const char* utf8name,
67
- Getter getter,
68
- Setter setter,
69
- napi_property_attributes attributes,
70
- void* /*data*/) {
71
- using CbData = details::AccessorCallbackData<Getter, Setter>;
72
- // TODO: Delete when the function is destroyed
73
- auto callbackData = new CbData({getter, setter, nullptr});
74
-
75
- return PropertyDescriptor({utf8name,
76
- nullptr,
77
- nullptr,
78
- CbData::GetterWrapper,
79
- CbData::SetterWrapper,
80
- nullptr,
81
- attributes,
82
- callbackData});
83
- }
84
-
85
- template <typename Getter, typename Setter>
86
- inline PropertyDescriptor PropertyDescriptor::Accessor(
87
- const std::string& utf8name,
88
- Getter getter,
89
- Setter setter,
90
- napi_property_attributes attributes,
91
- void* data) {
92
- return Accessor(utf8name.c_str(), getter, setter, attributes, data);
93
- }
94
-
95
- template <typename Getter, typename Setter>
96
- inline PropertyDescriptor PropertyDescriptor::Accessor(
97
- napi_value name,
98
- Getter getter,
99
- Setter setter,
100
- napi_property_attributes attributes,
101
- void* /*data*/) {
102
- using CbData = details::AccessorCallbackData<Getter, Setter>;
103
- // TODO: Delete when the function is destroyed
104
- auto callbackData = new CbData({getter, setter, nullptr});
105
-
106
- return PropertyDescriptor({nullptr,
107
- name,
108
- nullptr,
109
- CbData::GetterWrapper,
110
- CbData::SetterWrapper,
111
- nullptr,
112
- attributes,
113
- callbackData});
114
- }
115
-
116
- template <typename Getter, typename Setter>
117
- inline PropertyDescriptor PropertyDescriptor::Accessor(
118
- Name name,
119
- Getter getter,
120
- Setter setter,
121
- napi_property_attributes attributes,
122
- void* data) {
123
- napi_value nameValue = name;
124
- return PropertyDescriptor::Accessor(
125
- nameValue, getter, setter, attributes, data);
126
- }
127
-
128
- template <typename Callable>
129
- inline PropertyDescriptor PropertyDescriptor::Function(
130
- const char* utf8name,
131
- Callable cb,
132
- napi_property_attributes attributes,
133
- void* /*data*/) {
134
- using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
135
- using CbData = details::CallbackData<Callable, ReturnType>;
136
- // TODO: Delete when the function is destroyed
137
- auto callbackData = new CbData({cb, nullptr});
138
-
139
- return PropertyDescriptor({utf8name,
140
- nullptr,
141
- CbData::Wrapper,
142
- nullptr,
143
- nullptr,
144
- nullptr,
145
- attributes,
146
- callbackData});
147
- }
148
-
149
- template <typename Callable>
150
- inline PropertyDescriptor PropertyDescriptor::Function(
151
- const std::string& utf8name,
152
- Callable cb,
153
- napi_property_attributes attributes,
154
- void* data) {
155
- return Function(utf8name.c_str(), cb, attributes, data);
156
- }
157
-
158
- template <typename Callable>
159
- inline PropertyDescriptor PropertyDescriptor::Function(
160
- napi_value name,
161
- Callable cb,
162
- napi_property_attributes attributes,
163
- void* /*data*/) {
164
- using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
165
- using CbData = details::CallbackData<Callable, ReturnType>;
166
- // TODO: Delete when the function is destroyed
167
- auto callbackData = new CbData({cb, nullptr});
168
-
169
- return PropertyDescriptor({nullptr,
170
- name,
171
- CbData::Wrapper,
172
- nullptr,
173
- nullptr,
174
- nullptr,
175
- attributes,
176
- callbackData});
177
- }
178
-
179
- template <typename Callable>
180
- inline PropertyDescriptor PropertyDescriptor::Function(
181
- Name name, Callable cb, napi_property_attributes attributes, void* data) {
182
- napi_value nameValue = name;
183
- return PropertyDescriptor::Function(nameValue, cb, attributes, data);
184
- }
185
-
186
- #endif // !SRC_NAPI_INL_DEPRECATED_H_