emnapi 1.1.1 → 1.2.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.
Files changed (34) hide show
  1. package/CMakeLists.txt +14 -2
  2. package/README.md +39 -11
  3. package/common.gypi +4 -4
  4. package/dist/library_napi.js +2 -2
  5. package/emnapi.gyp +1 -1
  6. package/include/node/emnapi.h +2 -2
  7. package/lib/wasm32/libdlmalloc-mt.a +0 -0
  8. package/lib/wasm32/libdlmalloc.a +0 -0
  9. package/lib/wasm32/libemmalloc-mt.a +0 -0
  10. package/lib/wasm32/libemmalloc.a +0 -0
  11. package/lib/wasm32/libemnapi-basic-mt.a +0 -0
  12. package/lib/wasm32/libemnapi-basic.a +0 -0
  13. package/lib/wasm32/libemnapi.a +0 -0
  14. package/lib/wasm32-emscripten/libemnapi-basic.a +0 -0
  15. package/lib/wasm32-emscripten/libemnapi-mt.a +0 -0
  16. package/lib/wasm32-emscripten/libemnapi.a +0 -0
  17. package/lib/wasm32-wasi/libemnapi-basic-mt.a +0 -0
  18. package/lib/wasm32-wasi/libemnapi-basic.a +0 -0
  19. package/lib/wasm32-wasi/libemnapi.a +0 -0
  20. package/lib/wasm32-wasi-threads/libemnapi-basic-mt.a +0 -0
  21. package/lib/wasm32-wasi-threads/libemnapi-basic.a +0 -0
  22. package/lib/wasm32-wasi-threads/libemnapi-mt.a +0 -0
  23. package/lib/wasm32-wasi-threads/libemnapi.a +0 -0
  24. package/lib/wasm32-wasip1/libemnapi-basic-mt.a +0 -0
  25. package/lib/wasm32-wasip1/libemnapi-basic.a +0 -0
  26. package/lib/wasm32-wasip1/libemnapi.a +0 -0
  27. package/lib/wasm32-wasip1-threads/libemnapi-basic-mt.a +0 -0
  28. package/lib/wasm32-wasip1-threads/libemnapi-basic.a +0 -0
  29. package/lib/wasm32-wasip1-threads/libemnapi-mt.a +0 -0
  30. package/lib/wasm32-wasip1-threads/libemnapi.a +0 -0
  31. package/lib/wasm64-emscripten/libemnapi-basic.a +0 -0
  32. package/lib/wasm64-emscripten/libemnapi-mt.a +0 -0
  33. package/lib/wasm64-emscripten/libemnapi.a +0 -0
  34. package/package.json +1 -1
package/CMakeLists.txt CHANGED
@@ -17,6 +17,18 @@ else()
17
17
  set(IS_WASM32 OFF)
18
18
  endif()
19
19
 
20
+ if((CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi") OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasip1"))
21
+ set(IS_WASM32_WASIP1 ON)
22
+ else()
23
+ set(IS_WASM32_WASIP1 OFF)
24
+ endif()
25
+
26
+ if((CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads") OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasip1-threads"))
27
+ set(IS_WASM32_WASIP1_THREADS ON)
28
+ else()
29
+ set(IS_WASM32_WASIP1_THREADS OFF)
30
+ endif()
31
+
20
32
  set(UV_SRC
21
33
  "${CMAKE_CURRENT_SOURCE_DIR}/src/uv/uv-common.c"
22
34
  "${CMAKE_CURRENT_SOURCE_DIR}/src/uv/threadpool.c"
@@ -133,7 +145,7 @@ if(IS_EMSCRIPTEN)
133
145
  target_link_options(${EMNAPI_BASIC_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
134
146
  endif()
135
147
 
136
- if(IS_WASM32 OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi") OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads"))
148
+ if(IS_WASM32 OR IS_WASM32_WASIP1 OR IS_WASM32_WASIP1_THREADS)
137
149
  set(EMNAPI_BUILD_BASIC_MT ON)
138
150
  else()
139
151
  set(EMNAPI_BUILD_BASIC_MT OFF)
@@ -155,7 +167,7 @@ if(EMNAPI_BUILD_BASIC_MT)
155
167
  endif()
156
168
  endif()
157
169
 
158
- if(IS_EMSCRIPTEN OR (CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads"))
170
+ if(IS_EMSCRIPTEN OR IS_WASM32_WASIP1_THREADS)
159
171
  set(EMNAPI_BUILD_MT ON)
160
172
  else()
161
173
  set(EMNAPI_BUILD_MT OFF)
package/README.md CHANGED
@@ -23,6 +23,8 @@ This project aims to
23
23
 
24
24
  This project also powers the WebAssembly feature for [napi-rs](https://github.com/napi-rs/napi-rs), and enables many Node.js native addons to run on [StackBlitz](https://stackblitz.com)'s WebContainer.
25
25
 
26
+ [Node-API changes](https://github.com/nodejs/node/pulls?q=is%3Apr+label%3Anode-api+) will be synchronized into this repo.
27
+
26
28
  See documentation for more details:
27
29
  - [https://toyobayashi.github.io/emnapi-docs/guide/](https://toyobayashi.github.io/emnapi-docs/guide/)
28
30
  - [https://emnapi-docs.vercel.app/guide/](https://emnapi-docs.vercel.app/guide/)
@@ -628,9 +630,10 @@ Output code can run in recent version modern browsers and Node.js latest LTS. IE
628
630
  ### Using node-gyp (Experimental)
629
631
 
630
632
  Currently node-gyp works on Linux only and don't support static library linking in cross-compiling.
631
- There are opened PRs to try to make node-gyp work fine.
633
+ There are related PRs to try to make node-gyp work fine.
632
634
 
633
635
  - https://github.com/nodejs/gyp-next/pull/222
636
+ - https://github.com/nodejs/gyp-next/pull/240
634
637
  - https://github.com/nodejs/node-gyp/pull/2974
635
638
 
636
639
  If you experienced issues on Windows or macOS, please check the PRs for upstream changes detail and see
@@ -643,7 +646,7 @@ Arch: `node-gyp configure --arch=<wasm32 | wasm64>`
643
646
  ```ts
644
647
  // node-gyp configure -- -Dvariable_name=value
645
648
 
646
- declare var OS: 'emscripten' | 'wasi' | 'unknown' | ''
649
+ declare var OS: 'emscripten' | 'wasi' | 'unknown' | 'wasm' | ''
647
650
 
648
651
  /**
649
652
  * Enable async work and threadsafe-functions
@@ -699,7 +702,7 @@ declare var emnapi_manual_linking: 0 | 1
699
702
  ["OS == 'wasi'", {
700
703
  # ...
701
704
  }],
702
- ["OS == 'unknown' or OS == ''", {
705
+ ["OS in ' wasm unknown'", {
703
706
  # ...
704
707
  }]
705
708
  ]
@@ -750,19 +753,19 @@ call set CXX_target=%%WASI_SDK_PATH:\=/%%/bin/clang++.exe
750
753
  emmake node-gyp rebuild \
751
754
  --arch=wasm32 \
752
755
  --nodedir=./node_modules/emnapi \
753
- -- -f make-linux -DOS=emscripten # -Dwasm_threads=1
756
+ -- -f make-emscripten # -Dwasm_threads=1
754
757
 
755
758
  # wasi
756
759
  node-gyp rebuild \
757
760
  --arch=wasm32 \
758
761
  --nodedir=./node_modules/emnapi \
759
- -- -f make-linux -DOS=wasi # -Dwasm_threads=1
762
+ -- -f make-wasi # -Dwasm_threads=1
760
763
 
761
764
  # bare wasm32
762
765
  node-gyp rebuild \
763
766
  --arch=wasm32 \
764
767
  --nodedir=./node_modules/emnapi \
765
- -- -f make-linux -DOS=unknown # -Dwasm_threads=1
768
+ -- -f make-wasm # -Dwasm_threads=1
766
769
  ```
767
770
 
768
771
  ```bat
@@ -770,15 +773,15 @@ node-gyp rebuild \
770
773
  @REM Run the bat file in POSIX-like environment (e.g. Cygwin)
771
774
 
772
775
  @REM emscripten
773
- call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-linux -DOS=emscripten
776
+ call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-emscripten
774
777
  call emmake.bat make -C %~dp0build
775
778
 
776
779
  @REM wasi
777
- call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-linux -DOS=wasi
780
+ call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-wasi
778
781
  make -C %~dp0build
779
782
 
780
783
  @REM bare wasm32
781
- call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-linux -DOS=unknown
784
+ call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-wasm
782
785
  make -C %~dp0build
783
786
  ```
784
787
 
@@ -935,7 +938,32 @@ instantiateNapiModule(input, {
935
938
  */
936
939
  asyncWorkPoolSize: 4, // 0: single thread mock, > 0: schedule async work in web worker
937
940
  wasi: new WASI(/* ... */),
938
- // reuseWorker: true,
941
+
942
+ /**
943
+ * Setting this to `true` or a delay (ms) makes
944
+ * pthread_create() do not return until worker actually start.
945
+ * It will throw error if emnapi runs in browser main thread
946
+ * since browser disallow blocking the main thread (Atomics.wait).
947
+ * @defaultValue false
948
+ */
949
+ waitThreadStart: isNode || (isBrowser && !isBrowserMainThread),
950
+
951
+ /**
952
+ * Reuse the thread worker after thread exit to avoid re-creatation
953
+ * @defaultValue false
954
+ */
955
+ reuseWorker: {
956
+ /**
957
+ * @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size | PTHREAD_POOL_SIZE}
958
+ */
959
+ size: 0,
960
+
961
+ /**
962
+ * @see {@link https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size-strict | PTHREAD_POOL_SIZE_STRICT}
963
+ */
964
+ strict: false
965
+ },
966
+
939
967
  onCreateWorker () {
940
968
  return new Worker('./worker.js')
941
969
  // Node.js
@@ -988,7 +1016,7 @@ instantiateNapiModule(input, {
988
1016
  }
989
1017
  })
990
1018
 
991
- WASI = require('./wasi').WASI
1019
+ WASI = require('wasi').WASI
992
1020
  emnapiCore = require('@emnapi/core')
993
1021
  } else {
994
1022
  importScripts('./node_modules/memfs-browser/dist/memfs.js')
package/common.gypi CHANGED
@@ -2,7 +2,7 @@
2
2
  # in https://github.com/lovell/sharp/pull/3522
3
3
  {
4
4
  'variables': {
5
- # OS: 'emscripten' | 'wasi' | 'unknown'
5
+ # OS: 'emscripten' | 'wasi' | 'unknown' | 'wasm'
6
6
  'clang': 1,
7
7
  'target_arch%': 'wasm32',
8
8
  'wasm_threads%': 0,
@@ -139,7 +139,7 @@
139
139
  }],
140
140
  ],
141
141
  }],
142
- ['OS in "wasi unknown "', {
142
+ ['OS in "wasi wasm unknown "', {
143
143
  'configurations': {
144
144
  'Release': {
145
145
  'ldflags': [ '-Wl,--strip-debug' ],
@@ -239,7 +239,7 @@
239
239
  'target_conditions': [
240
240
  ['_type=="executable"', {
241
241
  'conditions': [
242
- ['OS in "wasi unknown "', {
242
+ ['OS in "wasi wasm unknown "', {
243
243
  'product_extension': 'wasm',
244
244
 
245
245
  'ldflags': [
@@ -326,7 +326,7 @@
326
326
  }]
327
327
  ],
328
328
  }],
329
- ['OS in "wasi unknown "', {
329
+ ['OS in "wasi wasm unknown "', {
330
330
  'product_extension': 'wasm',
331
331
 
332
332
  'ldflags': [
@@ -1235,7 +1235,7 @@ function _napi_get_buffer_info(env, buffer, data, length) {
1235
1235
  if (!buffer)
1236
1236
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1237
1237
  var handle = emnapiCtx.handleStore.get(buffer);
1238
- if (!handle.isBuffer())
1238
+ if (!handle.isBuffer(emnapiCtx.feature.Buffer))
1239
1239
  return envObject.setLastError(1 /* napi_status.napi_invalid_arg */);
1240
1240
  return _napi_get_typedarray_info(env, buffer, 0, length, data, 0, 0);
1241
1241
  }
@@ -6393,7 +6393,7 @@ function _napi_is_buffer(env, value, result) {
6393
6393
  var h = emnapiCtx.handleStore.get(value);
6394
6394
  {{{ from64('result') }}};
6395
6395
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
6396
- var r = h.isBuffer() ? 1 : 0;
6396
+ var r = h.isBuffer(emnapiCtx.feature.Buffer) ? 1 : 0;
6397
6397
  {{{ makeSetValue('result', 0, 'r', 'i8') }}};
6398
6398
  return envObject.clearLastError();
6399
6399
  }
package/emnapi.gyp CHANGED
@@ -63,7 +63,7 @@
63
63
  ]
64
64
  },
65
65
  'conditions': [
66
- ['wasm_threads != 0 and OS in " unknown wasi"', {
66
+ ['wasm_threads != 0 and OS in " unknown wasm wasi"', {
67
67
  'sources': [
68
68
  'src/thread/async_worker_create.c',
69
69
  'src/thread/async_worker_init.S',
@@ -6,8 +6,8 @@
6
6
  #include "emnapi_common.h"
7
7
 
8
8
  #define EMNAPI_MAJOR_VERSION 1
9
- #define EMNAPI_MINOR_VERSION 1
10
- #define EMNAPI_PATCH_VERSION 1
9
+ #define EMNAPI_MINOR_VERSION 2
10
+ #define EMNAPI_PATCH_VERSION 0
11
11
 
12
12
  typedef enum {
13
13
  emnapi_runtime,
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emnapi",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "description": "Node-API implementation for Emscripten",
5
5
  "main": "index.js",
6
6
  "gypfile": false,