emnapi 0.45.0 → 1.0.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 +22 -16
- package/README.md +158 -141
- package/cmake/wasm32.cmake +1 -1
- package/common.gypi +423 -0
- package/dist/library_napi.js +7 -3
- package/emnapi.gyp +120 -0
- package/include/node/config.gypi +5 -0
- package/include/{emnapi.h → node/emnapi.h} +2 -2
- package/index.js +4 -2
- package/lib/wasm32/libemmalloc-mt.a +0 -0
- package/lib/wasm32/libemmalloc.a +0 -0
- package/lib/wasm64-emscripten/libemnapi-basic.a +0 -0
- package/lib/wasm64-emscripten/libemnapi-mt.a +0 -0
- package/lib/wasm64-emscripten/libemnapi.a +0 -0
- package/package.json +1 -1
- package/src/js_native_api.c +2 -0
- package/src/malloc/emmalloc/emmalloc.c +8 -3
- package/lib/wasm32-emscripten.txt +0 -5
- package/lib/wasm32-wasi-threads.txt +0 -4
- package/lib/wasm32-wasi.txt +0 -4
- package/lib/wasm32.txt +0 -4
- /package/include/{emnapi_common.h → node/emnapi_common.h} +0 -0
- /package/include/{js_native_api.h → node/js_native_api.h} +0 -0
- /package/include/{js_native_api_types.h → node/js_native_api_types.h} +0 -0
- /package/include/{node_api.h → node/node_api.h} +0 -0
- /package/include/{node_api_types.h → node/node_api_types.h} +0 -0
- /package/include/{uv → node/uv}/threadpool.h +0 -0
- /package/include/{uv → node/uv}/unix.h +0 -0
- /package/include/{uv.h → node/uv.h} +0 -0
package/CMakeLists.txt
CHANGED
|
@@ -38,7 +38,7 @@ set(EMNAPI_THREADS_SRC
|
|
|
38
38
|
)
|
|
39
39
|
set(EMNAPI_SRC ${ENAPI_BASIC_SRC} ${EMNAPI_THREADS_SRC})
|
|
40
40
|
|
|
41
|
-
set(EMNAPI_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
|
41
|
+
set(EMNAPI_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/include/node")
|
|
42
42
|
set(EMNAPI_DEFINES "BUILDING_NODE_EXTENSION")
|
|
43
43
|
|
|
44
44
|
set(EMNAPI_JS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/dist/library_napi.js")
|
|
@@ -209,35 +209,41 @@ if(LIB_ARCH)
|
|
|
209
209
|
endif()
|
|
210
210
|
|
|
211
211
|
install(FILES
|
|
212
|
-
${CMAKE_CURRENT_SOURCE_DIR}/
|
|
213
|
-
${CMAKE_CURRENT_SOURCE_DIR}/
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
${CMAKE_CURRENT_SOURCE_DIR}/include/
|
|
218
|
-
${CMAKE_CURRENT_SOURCE_DIR}/include/
|
|
219
|
-
|
|
212
|
+
${CMAKE_CURRENT_SOURCE_DIR}/common.gypi
|
|
213
|
+
${CMAKE_CURRENT_SOURCE_DIR}/emnapi.gyp
|
|
214
|
+
DESTINATION ".")
|
|
215
|
+
|
|
216
|
+
install(FILES
|
|
217
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/emnapi_common.h
|
|
218
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/emnapi.h
|
|
219
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/js_native_api_types.h
|
|
220
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/js_native_api.h
|
|
221
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/node_api_types.h
|
|
222
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/node_api.h
|
|
223
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/uv.h
|
|
224
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/config.gypi
|
|
225
|
+
DESTINATION "include/node")
|
|
220
226
|
|
|
221
227
|
install(DIRECTORY
|
|
222
|
-
${CMAKE_CURRENT_SOURCE_DIR}/include/uv
|
|
223
|
-
DESTINATION "include
|
|
228
|
+
${CMAKE_CURRENT_SOURCE_DIR}/include/node/uv
|
|
229
|
+
DESTINATION "include/node")
|
|
224
230
|
|
|
225
231
|
install(FILES
|
|
226
232
|
${CMAKE_CURRENT_SOURCE_DIR}/dist/library_napi.js
|
|
227
|
-
DESTINATION "
|
|
233
|
+
DESTINATION "dist")
|
|
228
234
|
|
|
229
235
|
if(EMNAPI_INSTALL_SRC)
|
|
230
236
|
install(FILES
|
|
231
237
|
${EMNAPI_SRC}
|
|
232
238
|
"${CMAKE_CURRENT_SOURCE_DIR}/src/emnapi_internal.h"
|
|
233
|
-
DESTINATION "src
|
|
239
|
+
DESTINATION "src")
|
|
234
240
|
install(DIRECTORY
|
|
235
241
|
${CMAKE_CURRENT_SOURCE_DIR}/src/uv
|
|
236
|
-
DESTINATION "src
|
|
242
|
+
DESTINATION "src")
|
|
237
243
|
install(DIRECTORY
|
|
238
244
|
${CMAKE_CURRENT_SOURCE_DIR}/src/malloc
|
|
239
|
-
DESTINATION "src
|
|
245
|
+
DESTINATION "src")
|
|
240
246
|
install(DIRECTORY
|
|
241
247
|
${CMAKE_CURRENT_SOURCE_DIR}/src/thread
|
|
242
|
-
DESTINATION "src
|
|
248
|
+
DESTINATION "src")
|
|
243
249
|
endif()
|
package/README.md
CHANGED
|
@@ -4,15 +4,25 @@
|
|
|
4
4
|
<img src="https://toyobayashi.github.io/emnapi-docs/emnapi.svg" alt="emnapi logo" width="256" />
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
|
+
## Sponsors
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://cdn.jsdelivr.net/gh/toyobayashi/toyobayashi/sponsorkit/sponsors.svg">
|
|
11
|
+
<img src='https://cdn.jsdelivr.net/gh/toyobayashi/toyobayashi/sponsorkit/sponsors.svg'/>
|
|
12
|
+
</a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
7
15
|
[](https://github.com/toyobayashi/emnapi/actions/workflows/main.yml)
|
|
8
16
|
|
|
9
|
-
[Node-API](https://nodejs.org/docs/latest/api/n-api.html) implementation for [Emscripten](https://emscripten.org/index.html), [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) and clang with wasm support.
|
|
17
|
+
[Node-API](https://nodejs.org/docs/latest/api/n-api.html) implementation for [Emscripten](https://emscripten.org/index.html), [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) and clang with wasm support.
|
|
10
18
|
|
|
11
19
|
This project aims to
|
|
12
20
|
|
|
13
21
|
- Help users port their or existing Node-API native addons to wasm with code change as less as possible.
|
|
14
22
|
- Make runtime behavior matches native Node.js as much as possible.
|
|
15
23
|
|
|
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
|
+
|
|
16
26
|
See documentation for more details:
|
|
17
27
|
- [https://toyobayashi.github.io/emnapi-docs/guide/](https://toyobayashi.github.io/emnapi-docs/guide/)
|
|
18
28
|
- [https://emnapi-docs.vercel.app/guide/](https://emnapi-docs.vercel.app/guide/)
|
|
@@ -167,7 +177,7 @@ module.exports = (function (exports) {
|
|
|
167
177
|
emcc -O3 \
|
|
168
178
|
-DBUILDING_NODE_EXTENSION \
|
|
169
179
|
"-DNAPI_EXTERN=__attribute__((__import_module__(\"env\")))" \
|
|
170
|
-
-I./node_modules/emnapi/include \
|
|
180
|
+
-I./node_modules/emnapi/include/node \
|
|
171
181
|
-L./node_modules/emnapi/lib/wasm32-emscripten \
|
|
172
182
|
--js-library=./node_modules/emnapi/dist/library_napi.js \
|
|
173
183
|
-sEXPORTED_FUNCTIONS="['_malloc','_free','_napi_register_wasm_v1','_node_api_module_get_api_version_v1']" \
|
|
@@ -184,7 +194,7 @@ emcc -O3 \
|
|
|
184
194
|
```bash
|
|
185
195
|
clang -O3 \
|
|
186
196
|
-DBUILDING_NODE_EXTENSION \
|
|
187
|
-
-I./node_modules/emnapi/include \
|
|
197
|
+
-I./node_modules/emnapi/include/node \
|
|
188
198
|
-L./node_modules/emnapi/lib/wasm32-wasi \
|
|
189
199
|
--target=wasm32-wasi \
|
|
190
200
|
--sysroot=$WASI_SDK_PATH/share/wasi-sysroot \
|
|
@@ -212,7 +222,7 @@ Choose `libdlmalloc.a` or `libemmalloc.a` for `malloc` and `free`.
|
|
|
212
222
|
```bash
|
|
213
223
|
clang -O3 \
|
|
214
224
|
-DBUILDING_NODE_EXTENSION \
|
|
215
|
-
-I./node_modules/emnapi/include \
|
|
225
|
+
-I./node_modules/emnapi/include/node \
|
|
216
226
|
-L./node_modules/emnapi/lib/wasm32 \
|
|
217
227
|
--target=wasm32 \
|
|
218
228
|
-nostdlib \
|
|
@@ -456,7 +466,7 @@ em++ -O3 \
|
|
|
456
466
|
"-DNAPI_EXTERN=__attribute__((__import_module__(\"env\")))" \
|
|
457
467
|
-DNAPI_DISABLE_CPP_EXCEPTIONS \
|
|
458
468
|
-DNODE_ADDON_API_ENABLE_MAYBE \
|
|
459
|
-
-I./node_modules/emnapi/include \
|
|
469
|
+
-I./node_modules/emnapi/include/node \
|
|
460
470
|
-I./node_modules/node-addon-api \
|
|
461
471
|
-L./node_modules/emnapi/lib/wasm32-emscripten \
|
|
462
472
|
--js-library=./node_modules/emnapi/dist/library_napi.js \
|
|
@@ -476,7 +486,7 @@ clang++ -O3 \
|
|
|
476
486
|
-DBUILDING_NODE_EXTENSION \
|
|
477
487
|
-DNAPI_DISABLE_CPP_EXCEPTIONS \
|
|
478
488
|
-DNODE_ADDON_API_ENABLE_MAYBE \
|
|
479
|
-
-I./node_modules/emnapi/include \
|
|
489
|
+
-I./node_modules/emnapi/include/node \
|
|
480
490
|
-I./node_modules/node-addon-api \
|
|
481
491
|
-L./node_modules/emnapi/lib/wasm32-wasi \
|
|
482
492
|
--target=wasm32-wasi \
|
|
@@ -508,7 +518,7 @@ You can still use `wasm32-unknown-unknown` target if you use Node-API C API only
|
|
|
508
518
|
```bash
|
|
509
519
|
clang++ -O3 \
|
|
510
520
|
-DBUILDING_NODE_EXTENSION \
|
|
511
|
-
-I./node_modules/emnapi/include \
|
|
521
|
+
-I./node_modules/emnapi/include/node \
|
|
512
522
|
-L./node_modules/emnapi/lib/wasm32 \
|
|
513
523
|
--target=wasm32 \
|
|
514
524
|
-fno-exceptions \
|
|
@@ -615,158 +625,166 @@ cmake --build build
|
|
|
615
625
|
|
|
616
626
|
Output code can run in recent version modern browsers and Node.js latest LTS. IE is not supported.
|
|
617
627
|
|
|
618
|
-
### Using
|
|
628
|
+
### Using node-gyp (Experimental)
|
|
619
629
|
|
|
620
|
-
Currently
|
|
630
|
+
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.
|
|
621
632
|
|
|
622
|
-
|
|
633
|
+
- https://github.com/nodejs/gyp-next/pull/222
|
|
634
|
+
- https://github.com/nodejs/node-gyp/pull/2974
|
|
623
635
|
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
```toml
|
|
628
|
-
[package]
|
|
629
|
-
edition = "2021"
|
|
630
|
-
name = "binding"
|
|
631
|
-
version = "0.0.0"
|
|
632
|
-
|
|
633
|
-
# We should build binary for WASI reactor
|
|
634
|
-
# https://github.com/rust-lang/rust/pull/79997
|
|
635
|
-
# https://github.com/WebAssembly/WASI/issues/24
|
|
636
|
-
# for wasm
|
|
637
|
-
[[bin]]
|
|
638
|
-
name = "binding"
|
|
639
|
-
path = "src/main.rs"
|
|
640
|
-
|
|
641
|
-
# for native
|
|
642
|
-
# [lib]
|
|
643
|
-
# name = "binding"
|
|
644
|
-
# path = "src/lib.rs"
|
|
645
|
-
# crate-type = ["cdylib"]
|
|
646
|
-
|
|
647
|
-
[dependencies]
|
|
648
|
-
napi = { version = "2.12.1", default-features = false, features = ["napi8"] }
|
|
649
|
-
napi-sys = { version = "2.2.3", features = ["napi8"] }
|
|
650
|
-
napi-derive = "2.12.2"
|
|
651
|
-
|
|
652
|
-
[build-dependencies]
|
|
653
|
-
napi-build = "2.0.1"
|
|
654
|
-
|
|
655
|
-
[profile.release]
|
|
656
|
-
strip = "symbols"
|
|
657
|
-
```
|
|
636
|
+
If you experienced issues on Windows or macOS, please check the PRs for upstream changes detail and see
|
|
637
|
+
[emnapi-node-gyp-test](https://github.com/toyobayashi/emnapi-node-gyp-test) for examples.
|
|
658
638
|
|
|
659
|
-
|
|
639
|
+
- Variables
|
|
660
640
|
|
|
661
|
-
|
|
662
|
-
<summary>.cargo/config.toml</summary><br />
|
|
663
|
-
|
|
664
|
-
```toml
|
|
665
|
-
[build]
|
|
666
|
-
target = [
|
|
667
|
-
"wasm32-unknown-unknown",
|
|
668
|
-
"wasm32-wasi"
|
|
669
|
-
]
|
|
670
|
-
|
|
671
|
-
[target.wasm32-unknown-unknown]
|
|
672
|
-
rustflags = [
|
|
673
|
-
"-L./node_modules/emnapi/lib/wasm32",
|
|
674
|
-
"-lemnapi",
|
|
675
|
-
"-ldlmalloc",
|
|
676
|
-
# "-lemmalloc",
|
|
677
|
-
"-C", "link-arg=--no-entry",
|
|
678
|
-
"-C", "link-arg=--initial-memory=16777216",
|
|
679
|
-
"-C", "link-arg=--export-dynamic",
|
|
680
|
-
"-C", "link-arg=--export=malloc",
|
|
681
|
-
"-C", "link-arg=--export=free",
|
|
682
|
-
"-C", "link-arg=--export=napi_register_wasm_v1",
|
|
683
|
-
"-C", "link-arg=--export-if-defined=node_api_module_get_api_version_v1",
|
|
684
|
-
"-C", "link-arg=--export-table",
|
|
685
|
-
"-C", "link-arg=--import-undefined",
|
|
686
|
-
]
|
|
687
|
-
|
|
688
|
-
[target.wasm32-wasi]
|
|
689
|
-
rustflags = [
|
|
690
|
-
"-L./node_modules/emnapi/lib/wasm32-wasi",
|
|
691
|
-
"-lemnapi",
|
|
692
|
-
"-C", "link-arg=--initial-memory=16777216",
|
|
693
|
-
"-C", "link-arg=--export-dynamic",
|
|
694
|
-
"-C", "link-arg=--export=malloc",
|
|
695
|
-
"-C", "link-arg=--export=free",
|
|
696
|
-
"-C", "link-arg=--export=napi_register_wasm_v1",
|
|
697
|
-
"-C", "link-arg=--export-if-defined=node_api_module_get_api_version_v1",
|
|
698
|
-
"-C", "link-arg=--export-table",
|
|
699
|
-
"-C", "link-arg=--import-undefined",
|
|
700
|
-
"-Z", "wasi-exec-model=reactor", # +nightly
|
|
701
|
-
]
|
|
702
|
-
```
|
|
641
|
+
Arch: `node-gyp configure --arch=<wasm32 | wasm64>`
|
|
703
642
|
|
|
704
|
-
|
|
643
|
+
```ts
|
|
644
|
+
// node-gyp configure -- -Dvariable_name=value
|
|
705
645
|
|
|
706
|
-
|
|
707
|
-
<summary>src/main.rs</summary><br />
|
|
646
|
+
declare var OS: 'emscripten' | 'wasi' | 'unknown' | ''
|
|
708
647
|
|
|
709
|
-
|
|
710
|
-
|
|
648
|
+
/**
|
|
649
|
+
* Enable async work and threadsafe-functions
|
|
650
|
+
* @default 0
|
|
651
|
+
*/
|
|
652
|
+
declare var wasm_threads: 0 | 1
|
|
711
653
|
|
|
712
|
-
|
|
654
|
+
/** @default 1048576 */
|
|
655
|
+
declare var stack_size: number
|
|
713
656
|
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
657
|
+
/** @default 16777216 */
|
|
658
|
+
declare var initial_memory: number
|
|
659
|
+
|
|
660
|
+
/** @default 2147483648 */
|
|
661
|
+
declare var max_memory: number
|
|
662
|
+
|
|
663
|
+
/** @default path.join(path.dirname(commonGypiPath,'./dist/library_napi.js')) */
|
|
664
|
+
declare var emnapi_js_library: string
|
|
665
|
+
|
|
666
|
+
/** @default 0 */
|
|
667
|
+
declare var emnapi_manual_linking: 0 | 1
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
- Create `binding.gyp`
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
```py
|
|
674
|
+
{
|
|
675
|
+
"targets": [
|
|
676
|
+
{
|
|
677
|
+
"target_name": "hello",
|
|
678
|
+
"sources": [
|
|
679
|
+
"hello.c"
|
|
680
|
+
],
|
|
681
|
+
"conditions": [
|
|
682
|
+
["OS == 'emscripten'", {
|
|
683
|
+
"product_extension": "js", # required
|
|
684
|
+
|
|
685
|
+
# Windows and Linux
|
|
686
|
+
"cflags": [],
|
|
687
|
+
"cflags_c": [],
|
|
688
|
+
"cflags_cc": [],
|
|
689
|
+
"ldflags": [],
|
|
690
|
+
|
|
691
|
+
# macOS uses following config
|
|
692
|
+
'xcode_settings': {
|
|
693
|
+
"WARNING_CFLAGS": [], # cflags
|
|
694
|
+
"OTHER_CFLAGS": [], # cflags_c
|
|
695
|
+
"OTHER_CPLUSPLUSFLAGS": [], # cflags_cc
|
|
696
|
+
"OTHER_LDFLAGS": [] # ldflags
|
|
697
|
+
}
|
|
698
|
+
}],
|
|
699
|
+
["OS == 'wasi'", {
|
|
700
|
+
# ...
|
|
701
|
+
}],
|
|
702
|
+
["OS == 'unknown' or OS == ''", {
|
|
703
|
+
# ...
|
|
704
|
+
}]
|
|
705
|
+
]
|
|
706
|
+
}
|
|
707
|
+
]
|
|
720
708
|
}
|
|
721
709
|
```
|
|
722
710
|
|
|
723
|
-
|
|
711
|
+
- Add the following environment variables.
|
|
724
712
|
|
|
725
|
-
|
|
726
|
-
|
|
713
|
+
```bash
|
|
714
|
+
# Linux or macOS
|
|
715
|
+
export GYP_CROSSCOMPILE=1
|
|
727
716
|
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
717
|
+
# emscripten
|
|
718
|
+
export AR_target="$EMSDK/upstream/emscripten/emar"
|
|
719
|
+
export CC_target="$EMSDK/upstream/emscripten/emcc"
|
|
720
|
+
export CXX_target="$EMSDK/upstream/emscripten/em++"
|
|
732
721
|
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
722
|
+
# wasi-sdk
|
|
723
|
+
export AR_target="$WASI_SDK_PATH/bin/ar"
|
|
724
|
+
export CC_target="$WASI_SDK_PATH/bin/clang"
|
|
725
|
+
export CXX_target="$WASI_SDK_PATH/bin/clang++"
|
|
726
|
+
```
|
|
738
727
|
|
|
739
|
-
|
|
728
|
+
```bat
|
|
729
|
+
@REM Windows
|
|
740
730
|
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
},
|
|
755
|
-
overwriteImports (importObject) {
|
|
756
|
-
importObject.env = {
|
|
757
|
-
...importObject.env,
|
|
758
|
-
...importObject.napi,
|
|
759
|
-
...importObject.emnapi
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
}).then(({ napiModule }) => {
|
|
763
|
-
const binding = napiModule.exports
|
|
764
|
-
// output: 5
|
|
765
|
-
console.log(binding.fibonacci(5))
|
|
766
|
-
})
|
|
731
|
+
set GYP_CROSSCOMPILE=1
|
|
732
|
+
|
|
733
|
+
@REM emscripten
|
|
734
|
+
call set AR_target=%%EMSDK:\=/%%/upstream/emscripten/emar.bat
|
|
735
|
+
call set CC_target=%%EMSDK:\=/%%/upstream/emscripten/emcc.bat
|
|
736
|
+
call set CXX_target=%%EMSDK:\=/%%/upstream/emscripten/em++.bat
|
|
737
|
+
|
|
738
|
+
@REM wasi-sdk
|
|
739
|
+
call set AR_target=%%WASI_SDK_PATH:\=/%%/bin/ar.exe
|
|
740
|
+
call set CC_target=%%WASI_SDK_PATH:\=/%%/bin/clang.exe
|
|
741
|
+
call set CXX_target=%%WASI_SDK_PATH:\=/%%/bin/clang++.exe
|
|
767
742
|
```
|
|
768
743
|
|
|
769
|
-
|
|
744
|
+
- Build
|
|
745
|
+
|
|
746
|
+
```bash
|
|
747
|
+
# Linux or macOS
|
|
748
|
+
|
|
749
|
+
# emscripten
|
|
750
|
+
emmake node-gyp rebuild \
|
|
751
|
+
--arch=wasm32 \
|
|
752
|
+
--nodedir=./node_modules/emnapi \
|
|
753
|
+
-- -f make -DOS=emscripten # -Dwasm_threads=1
|
|
754
|
+
|
|
755
|
+
# wasi
|
|
756
|
+
node-gyp rebuild \
|
|
757
|
+
--arch=wasm32 \
|
|
758
|
+
--nodedir=./node_modules/emnapi \
|
|
759
|
+
-- -f make -DOS=wasi # -Dwasm_threads=1
|
|
760
|
+
|
|
761
|
+
# bare wasm32
|
|
762
|
+
node-gyp rebuild \
|
|
763
|
+
--arch=wasm32 \
|
|
764
|
+
--nodedir=./node_modules/emnapi \
|
|
765
|
+
-- -f make -DOS=unknown # -Dwasm_threads=1
|
|
766
|
+
```
|
|
767
|
+
|
|
768
|
+
```bat
|
|
769
|
+
@REM Use make generator on Windows
|
|
770
|
+
@REM Run the bat file in POSIX-like environment (e.g. Cygwin)
|
|
771
|
+
|
|
772
|
+
@REM emscripten
|
|
773
|
+
call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make -DOS=emscripten
|
|
774
|
+
call emmake.bat make -C %~dp0build
|
|
775
|
+
|
|
776
|
+
@REM wasi
|
|
777
|
+
call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make -DOS=wasi
|
|
778
|
+
make -C %~dp0build
|
|
779
|
+
|
|
780
|
+
@REM bare wasm32
|
|
781
|
+
call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make -DOS=unknown
|
|
782
|
+
make -C %~dp0build
|
|
783
|
+
```
|
|
784
|
+
|
|
785
|
+
### Using Rust
|
|
786
|
+
|
|
787
|
+
See [napi-rs](https://github.com/napi-rs/napi-rs)
|
|
770
788
|
|
|
771
789
|
### Multithread
|
|
772
790
|
|
|
@@ -846,7 +864,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
|
|
|
846
864
|
"-sDEFAULT_PTHREAD_STACK_SIZE=2MB"
|
|
847
865
|
)
|
|
848
866
|
elseif(CMAKE_C_COMPILER_TARGET STREQUAL "wasm32-wasi-threads")
|
|
849
|
-
# Experimental
|
|
850
867
|
target_link_libraries(hello emnapi-mt)
|
|
851
868
|
set_target_properties(hello PROPERTIES SUFFIX ".wasm")
|
|
852
869
|
target_compile_options(hello PRIVATE "-fno-exceptions" "-pthread")
|
|
@@ -887,7 +904,7 @@ emcmake cmake -DCMAKE_BUILD_TYPE=Release \
|
|
|
887
904
|
-DEMNAPI_WORKER_POOL_SIZE=4 \
|
|
888
905
|
-G Ninja -H. -Bbuild
|
|
889
906
|
|
|
890
|
-
# wasi-sdk with thread support
|
|
907
|
+
# wasi-sdk with thread support
|
|
891
908
|
cmake -DCMAKE_TOOLCHAIN_FILE=$WASI_SDK_PATH/share/cmake/wasi-sdk-pthread.cmake \
|
|
892
909
|
-DWASI_SDK_PREFIX=$WASI_SDK_PATH \
|
|
893
910
|
-DEMNAPI_FIND_NODE_ADDON_API=ON \
|
package/cmake/wasm32.cmake
CHANGED
|
@@ -16,7 +16,7 @@ endif()
|
|
|
16
16
|
|
|
17
17
|
set(CMAKE_C_COMPILER ${LLVM_PREFIX}/bin/clang${WASM_HOST_EXE_SUFFIX})
|
|
18
18
|
set(CMAKE_CXX_COMPILER ${LLVM_PREFIX}/bin/clang++${WASM_HOST_EXE_SUFFIX})
|
|
19
|
-
set(CMAKE_ASM_COMPILER ${LLVM_PREFIX}/bin/clang${
|
|
19
|
+
set(CMAKE_ASM_COMPILER ${LLVM_PREFIX}/bin/clang${WASM_HOST_EXE_SUFFIX})
|
|
20
20
|
set(CMAKE_AR ${LLVM_PREFIX}/bin/llvm-ar${WASM_HOST_EXE_SUFFIX})
|
|
21
21
|
set(CMAKE_RANLIB ${LLVM_PREFIX}/bin/llvm-ranlib${WASM_HOST_EXE_SUFFIX})
|
|
22
22
|
set(CMAKE_C_COMPILER_TARGET ${triple})
|
package/common.gypi
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
# This file is originally created by [RReverser](https://github.com/RReverser)
|
|
2
|
+
# in https://github.com/lovell/sharp/pull/3522
|
|
3
|
+
{
|
|
4
|
+
'variables': {
|
|
5
|
+
# OS: 'emscripten' | 'wasi' | 'unknown'
|
|
6
|
+
'clang': 1,
|
|
7
|
+
'target_arch%': 'wasm32',
|
|
8
|
+
'wasm_threads%': 0,
|
|
9
|
+
'stack_size%': 1048576,
|
|
10
|
+
'initial_memory%': 16777216,
|
|
11
|
+
'max_memory%': 2147483648,
|
|
12
|
+
# must be an absolute path
|
|
13
|
+
'emnapi_js_library%': '<!(node -p "path.resolve(process.argv[1],\'dist/library_napi.js\').replace(process.argv[2],\'/\')" "<(node_root_dir)" "\\\\")',
|
|
14
|
+
'emnapi_manual_linking%': 0,
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
'target_defaults': {
|
|
18
|
+
'type': 'executable',
|
|
19
|
+
|
|
20
|
+
'defines': [
|
|
21
|
+
'BUILDING_NODE_EXTENSION',
|
|
22
|
+
'__STDC_FORMAT_MACROS',
|
|
23
|
+
],
|
|
24
|
+
|
|
25
|
+
'cflags': [
|
|
26
|
+
'-Wall',
|
|
27
|
+
'-Wextra',
|
|
28
|
+
'-Wno-unused-parameter',
|
|
29
|
+
],
|
|
30
|
+
'cflags_cc': [
|
|
31
|
+
'-fno-rtti',
|
|
32
|
+
'-fno-exceptions',
|
|
33
|
+
'-std=c++17'
|
|
34
|
+
],
|
|
35
|
+
|
|
36
|
+
'xcode_settings': {
|
|
37
|
+
# WARNING_CFLAGS == cflags
|
|
38
|
+
# OTHER_CFLAGS == cflags_c
|
|
39
|
+
# OTHER_CPLUSPLUSFLAGS == cflags_cc
|
|
40
|
+
# OTHER_LDFLAGS == ldflags
|
|
41
|
+
|
|
42
|
+
'CLANG_CXX_LANGUAGE_STANDARD': 'c++17',
|
|
43
|
+
'GCC_ENABLE_CPP_RTTI': 'NO',
|
|
44
|
+
'GCC_ENABLE_CPP_EXCEPTIONS': 'NO',
|
|
45
|
+
'WARNING_CFLAGS': [
|
|
46
|
+
'-Wall',
|
|
47
|
+
'-Wextra',
|
|
48
|
+
'-Wno-unused-parameter',
|
|
49
|
+
]
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
'default_configuration': 'Release',
|
|
53
|
+
'configurations': {
|
|
54
|
+
'Debug': {
|
|
55
|
+
'defines': [ 'DEBUG', '_DEBUG' ],
|
|
56
|
+
'cflags': [ '-g', '-O0' ],
|
|
57
|
+
'ldflags': [ '-g', '-O0' ],
|
|
58
|
+
'xcode_settings': {
|
|
59
|
+
'WARNING_CFLAGS': [ '-g', '-O0' ],
|
|
60
|
+
'OTHER_LDFLAGS': [ '-g', '-O0' ],
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
'Release': {
|
|
64
|
+
'cflags': [ '-O3' ],
|
|
65
|
+
'ldflags': [ '-O3' ],
|
|
66
|
+
'xcode_settings': {
|
|
67
|
+
'WARNING_CFLAGS': [ '-O3' ],
|
|
68
|
+
'OTHER_LDFLAGS': [ '-O3' ],
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
'conditions': [
|
|
74
|
+
['OS == "emscripten"', {
|
|
75
|
+
'defines': [
|
|
76
|
+
'NAPI_EXTERN=__attribute__((__import_module__(\"env\")))'
|
|
77
|
+
],
|
|
78
|
+
|
|
79
|
+
'cflags': [
|
|
80
|
+
'-sDEFAULT_TO_CXX=0',
|
|
81
|
+
],
|
|
82
|
+
'ldflags': [
|
|
83
|
+
"-sALLOW_MEMORY_GROWTH=1",
|
|
84
|
+
"-sEXPORTED_FUNCTIONS=['_malloc','_free','_napi_register_wasm_v1','_node_api_module_get_api_version_v1']",
|
|
85
|
+
'-sNODEJS_CATCH_EXIT=0',
|
|
86
|
+
'-sNODEJS_CATCH_REJECTION=0',
|
|
87
|
+
'-sWASM_BIGINT=1',
|
|
88
|
+
'-sMIN_CHROME_VERSION=84',
|
|
89
|
+
'-sMIN_NODE_VERSION=161500',
|
|
90
|
+
'-sSTACK_SIZE=<(stack_size)',
|
|
91
|
+
'-sDEFAULT_PTHREAD_STACK_SIZE=<(stack_size)',
|
|
92
|
+
'-sINITIAL_MEMORY=<(initial_memory)',
|
|
93
|
+
'-sMAXIMUM_MEMORY=<(max_memory)',
|
|
94
|
+
],
|
|
95
|
+
'xcode_settings': {
|
|
96
|
+
'WARNING_CFLAGS': [
|
|
97
|
+
'-sDEFAULT_TO_CXX=0',
|
|
98
|
+
],
|
|
99
|
+
'OTHER_LDFLAGS': [
|
|
100
|
+
"-sALLOW_MEMORY_GROWTH=1",
|
|
101
|
+
"-sEXPORTED_FUNCTIONS=['_malloc','_free','_napi_register_wasm_v1','_node_api_module_get_api_version_v1']",
|
|
102
|
+
'-sNODEJS_CATCH_EXIT=0',
|
|
103
|
+
'-sNODEJS_CATCH_REJECTION=0',
|
|
104
|
+
'-sWASM_BIGINT=1',
|
|
105
|
+
'-sMIN_CHROME_VERSION=84',
|
|
106
|
+
'-sMIN_NODE_VERSION=161500',
|
|
107
|
+
'-sSTACK_SIZE=<(stack_size)',
|
|
108
|
+
'-sDEFAULT_PTHREAD_STACK_SIZE=<(stack_size)',
|
|
109
|
+
'-sINITIAL_MEMORY=<(initial_memory)',
|
|
110
|
+
'-sMAXIMUM_MEMORY=<(max_memory)',
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
'configurations': {
|
|
115
|
+
'Debug': {
|
|
116
|
+
'ldflags': [ '-sSAFE_HEAP=1' ],
|
|
117
|
+
'xcode_settings': {
|
|
118
|
+
'OTHER_LDFLAGS': [ '-sSAFE_HEAP=1' ],
|
|
119
|
+
},
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
'conditions': [
|
|
124
|
+
['target_arch == "wasm64"', {
|
|
125
|
+
'cflags': [ '-sMEMORY64=1' ],
|
|
126
|
+
'ldflags': [ '-sMEMORY64=1' ],
|
|
127
|
+
'xcode_settings': {
|
|
128
|
+
'WARNING_CFLAGS': [ '-sMEMORY64=1' ],
|
|
129
|
+
'OTHER_LDFLAGS': [ '-sMEMORY64=1' ],
|
|
130
|
+
},
|
|
131
|
+
}],
|
|
132
|
+
['wasm_threads != 0', {
|
|
133
|
+
'cflags': [ '-sWASM_WORKERS=1', '-pthread' ],
|
|
134
|
+
'ldflags': [ '-pthread' ],
|
|
135
|
+
'xcode_settings': {
|
|
136
|
+
'WARNING_CFLAGS': [ '-sWASM_WORKERS=1', '-pthread' ],
|
|
137
|
+
'OTHER_LDFLAGS': [ '-pthread' ],
|
|
138
|
+
},
|
|
139
|
+
}],
|
|
140
|
+
],
|
|
141
|
+
}],
|
|
142
|
+
['OS in "wasi unknown "', {
|
|
143
|
+
'configurations': {
|
|
144
|
+
'Release': {
|
|
145
|
+
'ldflags': [ '-Wl,--strip-debug' ],
|
|
146
|
+
'xcode_settings': {
|
|
147
|
+
'OTHER_LDFLAGS': [ '-Wl,--strip-debug' ],
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
'conditions': [
|
|
153
|
+
['target_arch == "wasm64"', {
|
|
154
|
+
'ldflags': [ "-Wl,-mwasm64" ],
|
|
155
|
+
'xcode_settings': {
|
|
156
|
+
'OTHER_LDFLAGS': [ "-Wl,-mwasm64" ],
|
|
157
|
+
},
|
|
158
|
+
}],
|
|
159
|
+
['wasm_threads != 0', {
|
|
160
|
+
'cflags': [ "-matomics", "-mbulk-memory" ],
|
|
161
|
+
'xcode_settings': {
|
|
162
|
+
'WARNING_CFLAGS': [ "-matomics", "-mbulk-memory" ],
|
|
163
|
+
},
|
|
164
|
+
}],
|
|
165
|
+
['OS == "wasi"', {
|
|
166
|
+
'ldflags': [
|
|
167
|
+
'-mexec-model=reactor'
|
|
168
|
+
],
|
|
169
|
+
'xcode_settings': {
|
|
170
|
+
'OTHER_LDFLAGS': [
|
|
171
|
+
'-mexec-model=reactor'
|
|
172
|
+
],
|
|
173
|
+
},
|
|
174
|
+
'conditions': [
|
|
175
|
+
['target_arch == "wasm64"', {
|
|
176
|
+
'conditions': [
|
|
177
|
+
['wasm_threads != 0', {
|
|
178
|
+
# wasi-threads
|
|
179
|
+
'cflags': [ '--target=wasm64-wasi-threads', '-pthread' ],
|
|
180
|
+
'ldflags': [ '--target=wasm64-wasi-threads', '-pthread' ],
|
|
181
|
+
'xcode_settings': {
|
|
182
|
+
'WARNING_CFLAGS': [ '--target=wasm64-wasi-threads', '-pthread' ],
|
|
183
|
+
'OTHER_LDFLAGS': [ '--target=wasm64-wasi-threads', '-pthread' ],
|
|
184
|
+
},
|
|
185
|
+
}, {
|
|
186
|
+
# wasi
|
|
187
|
+
'cflags': [ '--target=wasm64-wasi' ],
|
|
188
|
+
'ldflags': [ '--target=wasm64-wasi' ],
|
|
189
|
+
'xcode_settings': {
|
|
190
|
+
'WARNING_CFLAGS': [ '--target=wasm64-wasi' ],
|
|
191
|
+
'OTHER_LDFLAGS': [ '--target=wasm64-wasi' ],
|
|
192
|
+
},
|
|
193
|
+
}],
|
|
194
|
+
],
|
|
195
|
+
}, {
|
|
196
|
+
'conditions': [
|
|
197
|
+
['wasm_threads != 0', {
|
|
198
|
+
# wasi-threads
|
|
199
|
+
'cflags': [ '--target=wasm32-wasi-threads', '-pthread' ],
|
|
200
|
+
'ldflags': [ '--target=wasm32-wasi-threads', '-pthread' ],
|
|
201
|
+
'xcode_settings': {
|
|
202
|
+
'WARNING_CFLAGS': [ '--target=wasm32-wasi-threads', '-pthread' ],
|
|
203
|
+
'OTHER_LDFLAGS': [ '--target=wasm32-wasi-threads', '-pthread' ],
|
|
204
|
+
},
|
|
205
|
+
}, {
|
|
206
|
+
# wasi
|
|
207
|
+
'cflags': [ '--target=wasm32-wasi' ],
|
|
208
|
+
'ldflags': [ '--target=wasm32-wasi' ],
|
|
209
|
+
'xcode_settings': {
|
|
210
|
+
'WARNING_CFLAGS': [ '--target=wasm32-wasi' ],
|
|
211
|
+
'OTHER_LDFLAGS': [ '--target=wasm32-wasi' ],
|
|
212
|
+
},
|
|
213
|
+
}],
|
|
214
|
+
],
|
|
215
|
+
}],
|
|
216
|
+
],
|
|
217
|
+
}, {
|
|
218
|
+
'conditions': [
|
|
219
|
+
['target_arch == "wasm64"', {
|
|
220
|
+
'cflags': [ '--target=wasm64-unknown-unknown' ],
|
|
221
|
+
'ldflags': [ '--target=wasm64-unknown-unknown' ],
|
|
222
|
+
'xcode_settings': {
|
|
223
|
+
'WARNING_CFLAGS': [ '--target=wasm64-unknown-unknown' ],
|
|
224
|
+
'OTHER_LDFLAGS': [ '--target=wasm64-unknown-unknown' ],
|
|
225
|
+
},
|
|
226
|
+
}, {
|
|
227
|
+
'cflags': [ '--target=wasm32-unknown-unknown' ],
|
|
228
|
+
'ldflags': [ '--target=wasm32-unknown-unknown' ],
|
|
229
|
+
'xcode_settings': {
|
|
230
|
+
'WARNING_CFLAGS': [ '--target=wasm32-unknown-unknown' ],
|
|
231
|
+
'OTHER_LDFLAGS': [ '--target=wasm32-unknown-unknown' ],
|
|
232
|
+
},
|
|
233
|
+
}],
|
|
234
|
+
],
|
|
235
|
+
}],
|
|
236
|
+
]
|
|
237
|
+
}],
|
|
238
|
+
['emnapi_manual_linking != 0', {
|
|
239
|
+
'target_conditions': [
|
|
240
|
+
['_type=="executable"', {
|
|
241
|
+
'conditions': [
|
|
242
|
+
['OS in "wasi unknown "', {
|
|
243
|
+
'product_extension': 'wasm',
|
|
244
|
+
|
|
245
|
+
'ldflags': [
|
|
246
|
+
'-Wl,--export-dynamic',
|
|
247
|
+
'-Wl,--export=malloc',
|
|
248
|
+
'-Wl,--export=free',
|
|
249
|
+
'-Wl,--export=napi_register_wasm_v1',
|
|
250
|
+
'-Wl,--export-if-defined=node_api_module_get_api_version_v1',
|
|
251
|
+
'-Wl,--import-undefined',
|
|
252
|
+
'-Wl,--export-table',
|
|
253
|
+
'-Wl,-zstack-size=<(stack_size)',
|
|
254
|
+
'-Wl,--initial-memory=<(initial_memory)',
|
|
255
|
+
'-Wl,--max-memory=<(max_memory)',
|
|
256
|
+
],
|
|
257
|
+
'xcode_settings': {
|
|
258
|
+
'OTHER_LDFLAGS': [
|
|
259
|
+
'-Wl,--export-dynamic',
|
|
260
|
+
'-Wl,--export=malloc',
|
|
261
|
+
'-Wl,--export=free',
|
|
262
|
+
'-Wl,--export=napi_register_wasm_v1',
|
|
263
|
+
'-Wl,--export-if-defined=node_api_module_get_api_version_v1',
|
|
264
|
+
'-Wl,--import-undefined',
|
|
265
|
+
'-Wl,--export-table',
|
|
266
|
+
'-Wl,-zstack-size=<(stack_size)',
|
|
267
|
+
'-Wl,--initial-memory=<(initial_memory)',
|
|
268
|
+
'-Wl,--max-memory=<(max_memory)',
|
|
269
|
+
],
|
|
270
|
+
},
|
|
271
|
+
|
|
272
|
+
'conditions': [
|
|
273
|
+
['wasm_threads != 0', {
|
|
274
|
+
'ldflags': [
|
|
275
|
+
'-Wl,--import-memory',
|
|
276
|
+
'-Wl,--shared-memory',
|
|
277
|
+
],
|
|
278
|
+
'xcode_settings': {
|
|
279
|
+
'OTHER_LDFLAGS': [
|
|
280
|
+
'-Wl,--import-memory',
|
|
281
|
+
'-Wl,--shared-memory',
|
|
282
|
+
]
|
|
283
|
+
},
|
|
284
|
+
}],
|
|
285
|
+
['OS != "wasi"', {
|
|
286
|
+
'ldflags': [
|
|
287
|
+
'-nostdlib',
|
|
288
|
+
'-Wl,--no-entry',
|
|
289
|
+
],
|
|
290
|
+
'xcode_settings': {
|
|
291
|
+
'OTHER_LDFLAGS': [
|
|
292
|
+
'-nostdlib',
|
|
293
|
+
'-Wl,--no-entry',
|
|
294
|
+
],
|
|
295
|
+
},
|
|
296
|
+
}],
|
|
297
|
+
]
|
|
298
|
+
}]
|
|
299
|
+
],
|
|
300
|
+
}],
|
|
301
|
+
],
|
|
302
|
+
}, {
|
|
303
|
+
'target_conditions': [
|
|
304
|
+
['_type=="executable"', {
|
|
305
|
+
'sources': [
|
|
306
|
+
'src/js_native_api.c',
|
|
307
|
+
'src/node_api.c',
|
|
308
|
+
'src/async_cleanup_hook.c',
|
|
309
|
+
'src/async_context.c',
|
|
310
|
+
'src/async_work.c',
|
|
311
|
+
'src/threadsafe_function.c',
|
|
312
|
+
'src/uv/uv-common.c',
|
|
313
|
+
'src/uv/threadpool.c',
|
|
314
|
+
'src/uv/unix/loop.c',
|
|
315
|
+
'src/uv/unix/thread.c',
|
|
316
|
+
'src/uv/unix/async.c',
|
|
317
|
+
'src/uv/unix/core.c',
|
|
318
|
+
],
|
|
319
|
+
'conditions': [
|
|
320
|
+
['OS == "emscripten"', {
|
|
321
|
+
'conditions': [
|
|
322
|
+
['emnapi_js_library != ""', {
|
|
323
|
+
'libraries': [
|
|
324
|
+
'--js-library=<(emnapi_js_library)',
|
|
325
|
+
],
|
|
326
|
+
}]
|
|
327
|
+
],
|
|
328
|
+
}],
|
|
329
|
+
['OS in "wasi unknown "', {
|
|
330
|
+
'product_extension': 'wasm',
|
|
331
|
+
|
|
332
|
+
'ldflags': [
|
|
333
|
+
'-Wl,--export-dynamic',
|
|
334
|
+
'-Wl,--export=malloc',
|
|
335
|
+
'-Wl,--export=free',
|
|
336
|
+
'-Wl,--export=napi_register_wasm_v1',
|
|
337
|
+
'-Wl,--export-if-defined=node_api_module_get_api_version_v1',
|
|
338
|
+
'-Wl,--import-undefined',
|
|
339
|
+
'-Wl,--export-table',
|
|
340
|
+
'-Wl,-zstack-size=<(stack_size)',
|
|
341
|
+
'-Wl,--initial-memory=<(initial_memory)',
|
|
342
|
+
'-Wl,--max-memory=<(max_memory)',
|
|
343
|
+
],
|
|
344
|
+
'xcode_settings': {
|
|
345
|
+
'OTHER_LDFLAGS': [
|
|
346
|
+
'-Wl,--export-dynamic',
|
|
347
|
+
'-Wl,--export=malloc',
|
|
348
|
+
'-Wl,--export=free',
|
|
349
|
+
'-Wl,--export=napi_register_wasm_v1',
|
|
350
|
+
'-Wl,--export-if-defined=node_api_module_get_api_version_v1',
|
|
351
|
+
'-Wl,--import-undefined',
|
|
352
|
+
'-Wl,--export-table',
|
|
353
|
+
'-Wl,-zstack-size=<(stack_size)',
|
|
354
|
+
'-Wl,--initial-memory=<(initial_memory)',
|
|
355
|
+
'-Wl,--max-memory=<(max_memory)',
|
|
356
|
+
],
|
|
357
|
+
},
|
|
358
|
+
|
|
359
|
+
'conditions': [
|
|
360
|
+
['wasm_threads != 0', {
|
|
361
|
+
'ldflags': [
|
|
362
|
+
'-Wl,--import-memory',
|
|
363
|
+
'-Wl,--shared-memory',
|
|
364
|
+
],
|
|
365
|
+
'xcode_settings': {
|
|
366
|
+
'OTHER_LDFLAGS': [
|
|
367
|
+
'-Wl,--import-memory',
|
|
368
|
+
'-Wl,--shared-memory',
|
|
369
|
+
]
|
|
370
|
+
},
|
|
371
|
+
}],
|
|
372
|
+
['OS != "wasi"', {
|
|
373
|
+
'defines': [
|
|
374
|
+
'PAGESIZE=65536'
|
|
375
|
+
],
|
|
376
|
+
'ldflags': [
|
|
377
|
+
'-nostdlib',
|
|
378
|
+
'-Wl,--no-entry',
|
|
379
|
+
],
|
|
380
|
+
'xcode_settings': {
|
|
381
|
+
'OTHER_LDFLAGS': [
|
|
382
|
+
'-nostdlib',
|
|
383
|
+
'-Wl,--no-entry',
|
|
384
|
+
],
|
|
385
|
+
},
|
|
386
|
+
'sources': [
|
|
387
|
+
'src/malloc/sbrk.c',
|
|
388
|
+
'src/malloc/memcpy.c',
|
|
389
|
+
'src/malloc/memset.c',
|
|
390
|
+
'src/malloc/dlmalloc/dlmalloc.c',
|
|
391
|
+
],
|
|
392
|
+
'conditions': [
|
|
393
|
+
['wasm_threads != 0', {
|
|
394
|
+
# wasm32 + threads
|
|
395
|
+
'sources': [
|
|
396
|
+
'src/thread/async_worker_create.c',
|
|
397
|
+
'src/thread/async_worker_init.S',
|
|
398
|
+
],
|
|
399
|
+
'defines': [
|
|
400
|
+
'USE_LOCKS=1'
|
|
401
|
+
],
|
|
402
|
+
'ldflags': [
|
|
403
|
+
'-Wl,--export=emnapi_async_worker_create',
|
|
404
|
+
'-Wl,--export=emnapi_async_worker_init',
|
|
405
|
+
],
|
|
406
|
+
'xcode_settings': {
|
|
407
|
+
'OTHER_LDFLAGS': [
|
|
408
|
+
'-Wl,--export=emnapi_async_worker_create',
|
|
409
|
+
'-Wl,--export=emnapi_async_worker_init',
|
|
410
|
+
],
|
|
411
|
+
},
|
|
412
|
+
}],
|
|
413
|
+
]
|
|
414
|
+
}],
|
|
415
|
+
]
|
|
416
|
+
}]
|
|
417
|
+
],
|
|
418
|
+
}],
|
|
419
|
+
],
|
|
420
|
+
}]
|
|
421
|
+
],
|
|
422
|
+
}
|
|
423
|
+
}
|
package/dist/library_napi.js
CHANGED
|
@@ -1549,7 +1549,9 @@ function _napi_get_value_int32(env, value, result) {
|
|
|
1549
1549
|
return envObject.setLastError(6 /* napi_status.napi_number_expected */);
|
|
1550
1550
|
}
|
|
1551
1551
|
{{{ from64('result') }}};
|
|
1552
|
-
|
|
1552
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1553
|
+
var v = new Int32Array([handle.value])[0];
|
|
1554
|
+
{{{ makeSetValue('result', 0, 'v', 'i32') }}};
|
|
1553
1555
|
return envObject.clearLastError();
|
|
1554
1556
|
}
|
|
1555
1557
|
/**
|
|
@@ -1584,7 +1586,7 @@ function _napi_get_value_int64(env, value, result) {
|
|
|
1584
1586
|
{{{ makeSetValue('result', 4, '0x7fffffff', 'u32') }}};
|
|
1585
1587
|
}
|
|
1586
1588
|
else {
|
|
1587
|
-
emnapiSetValueI64(result, numberValue);
|
|
1589
|
+
emnapiSetValueI64(result, Math.trunc(numberValue));
|
|
1588
1590
|
}
|
|
1589
1591
|
return envObject.clearLastError();
|
|
1590
1592
|
}
|
|
@@ -1724,7 +1726,9 @@ function _napi_get_value_uint32(env, value, result) {
|
|
|
1724
1726
|
return envObject.setLastError(6 /* napi_status.napi_number_expected */);
|
|
1725
1727
|
}
|
|
1726
1728
|
{{{ from64('result') }}};
|
|
1727
|
-
|
|
1729
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1730
|
+
var v = new Uint32Array([handle.value])[0];
|
|
1731
|
+
{{{ makeSetValue('result', 0, 'v', 'u32') }}};
|
|
1728
1732
|
return envObject.clearLastError();
|
|
1729
1733
|
}
|
|
1730
1734
|
/* eslint-disable @typescript-eslint/indent */
|
package/emnapi.gyp
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
{
|
|
2
|
+
'targets': [
|
|
3
|
+
{
|
|
4
|
+
'target_name': 'dlmalloc',
|
|
5
|
+
'type': 'static_library',
|
|
6
|
+
'defines': [
|
|
7
|
+
'PAGESIZE=65536'
|
|
8
|
+
],
|
|
9
|
+
'sources': [
|
|
10
|
+
'src/malloc/sbrk.c',
|
|
11
|
+
'src/malloc/memcpy.c',
|
|
12
|
+
'src/malloc/memset.c',
|
|
13
|
+
'src/malloc/dlmalloc/dlmalloc.c',
|
|
14
|
+
],
|
|
15
|
+
'conditions': [
|
|
16
|
+
['wasm_threads != 0', {
|
|
17
|
+
'defines': [
|
|
18
|
+
'USE_LOCKS=1'
|
|
19
|
+
],
|
|
20
|
+
}],
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
'target_name': 'emmalloc',
|
|
25
|
+
'type': 'static_library',
|
|
26
|
+
'defines': [
|
|
27
|
+
'PAGESIZE=65536'
|
|
28
|
+
],
|
|
29
|
+
'sources': [
|
|
30
|
+
'src/malloc/sbrk.c',
|
|
31
|
+
'src/malloc/memcpy.c',
|
|
32
|
+
'src/malloc/memset.c',
|
|
33
|
+
'src/malloc/emmalloc/emmalloc.c',
|
|
34
|
+
],
|
|
35
|
+
'cflags': [ '-fno-strict-aliasing' ],
|
|
36
|
+
'xcode_settings': {
|
|
37
|
+
'GCC_STRICT_ALIASING': 'NO',
|
|
38
|
+
},
|
|
39
|
+
'conditions': [
|
|
40
|
+
['wasm_threads != 0', {
|
|
41
|
+
'defines': [
|
|
42
|
+
'__EMSCRIPTEN_SHARED_MEMORY__=1'
|
|
43
|
+
],
|
|
44
|
+
}],
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
'target_name': 'emnapi_basic',
|
|
49
|
+
'type': 'static_library',
|
|
50
|
+
'sources': [
|
|
51
|
+
'src/js_native_api.c',
|
|
52
|
+
'src/node_api.c',
|
|
53
|
+
'src/async_cleanup_hook.c',
|
|
54
|
+
'src/async_context.c',
|
|
55
|
+
],
|
|
56
|
+
'link_settings': {
|
|
57
|
+
'target_conditions': [
|
|
58
|
+
['_type == "executable" and OS == "emscripten"', {
|
|
59
|
+
'libraries': [
|
|
60
|
+
'--js-library=<(emnapi_js_library)',
|
|
61
|
+
]
|
|
62
|
+
}],
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
'conditions': [
|
|
66
|
+
['wasm_threads != 0 and OS in " unknown"', {
|
|
67
|
+
'sources': [
|
|
68
|
+
'src/thread/async_worker_create.c',
|
|
69
|
+
'src/thread/async_worker_init.S',
|
|
70
|
+
],
|
|
71
|
+
'link_settings': {
|
|
72
|
+
'target_conditions': [
|
|
73
|
+
['_type == "executable"', {
|
|
74
|
+
'ldflags': [
|
|
75
|
+
'-Wl,--export=emnapi_async_worker_create',
|
|
76
|
+
'-Wl,--export=emnapi_async_worker_init',
|
|
77
|
+
],
|
|
78
|
+
'xcode_settings': {
|
|
79
|
+
'OTHER_LDFLAGS': [
|
|
80
|
+
'-Wl,--export=emnapi_async_worker_create',
|
|
81
|
+
'-Wl,--export=emnapi_async_worker_init',
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
}],
|
|
85
|
+
]
|
|
86
|
+
},
|
|
87
|
+
}],
|
|
88
|
+
]
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
'target_name': 'emnapi',
|
|
92
|
+
'type': 'static_library',
|
|
93
|
+
'sources': [
|
|
94
|
+
'src/js_native_api.c',
|
|
95
|
+
'src/node_api.c',
|
|
96
|
+
'src/async_cleanup_hook.c',
|
|
97
|
+
'src/async_context.c',
|
|
98
|
+
|
|
99
|
+
'src/uv/uv-common.c',
|
|
100
|
+
'src/uv/threadpool.c',
|
|
101
|
+
'src/uv/unix/loop.c',
|
|
102
|
+
'src/uv/unix/thread.c',
|
|
103
|
+
'src/uv/unix/async.c',
|
|
104
|
+
'src/uv/unix/core.c',
|
|
105
|
+
|
|
106
|
+
'src/async_work.c',
|
|
107
|
+
'src/threadsafe_function.c',
|
|
108
|
+
],
|
|
109
|
+
'link_settings': {
|
|
110
|
+
'target_conditions': [
|
|
111
|
+
['_type == "executable" and OS == "emscripten"', {
|
|
112
|
+
'libraries': [
|
|
113
|
+
'--js-library=<(emnapi_js_library)',
|
|
114
|
+
]
|
|
115
|
+
}],
|
|
116
|
+
]
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
}
|
package/index.js
CHANGED
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, '__esModule', { value: true })
|
|
|
3
3
|
|
|
4
4
|
const path = require('path')
|
|
5
5
|
|
|
6
|
-
const include = path.join(__dirname, 'include')
|
|
6
|
+
const include = path.join(__dirname, 'include/node')
|
|
7
7
|
const includeDir = path.relative(process.cwd(), include)
|
|
8
|
-
const jsLibrary = path.join(__dirname, './dist/library_napi.js')
|
|
8
|
+
const jsLibrary = path.join(__dirname, './dist/library_napi.js').replace(/\\|\\\\/g, '/')
|
|
9
9
|
const sources = [
|
|
10
10
|
path.join(__dirname, './src/js_native_api.c'),
|
|
11
11
|
path.join(__dirname, './src/node_api.c'),
|
|
@@ -20,8 +20,10 @@ const sources = [
|
|
|
20
20
|
path.join(__dirname, './src/uv/unix/async.c'),
|
|
21
21
|
path.join(__dirname, './src/uv/unix/core.c')
|
|
22
22
|
]
|
|
23
|
+
const targets = path.relative(process.cwd(), path.join(__dirname, 'emnapi.gyp'))
|
|
23
24
|
|
|
24
25
|
exports.include = include
|
|
25
26
|
exports.include_dir = includeDir
|
|
26
27
|
exports.js_library = jsLibrary
|
|
27
28
|
exports.sources = sources
|
|
29
|
+
exports.targets = targets
|
|
Binary file
|
package/lib/wasm32/libemmalloc.a
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
package/src/js_native_api.c
CHANGED
|
@@ -42,8 +42,10 @@ napi_status napi_get_last_error_info(
|
|
|
42
42
|
|
|
43
43
|
const int last_status = napi_cannot_run_js;
|
|
44
44
|
|
|
45
|
+
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || defined(__cplusplus)
|
|
45
46
|
static_assert((sizeof(emnapi_error_messages) / sizeof(const char*)) == napi_cannot_run_js + 1,
|
|
46
47
|
"Count of error messages must match count of error values");
|
|
48
|
+
#endif
|
|
47
49
|
|
|
48
50
|
_emnapi_get_last_error_info(env,
|
|
49
51
|
&last_error.error_code,
|
|
@@ -77,6 +77,7 @@ _Noreturn void __assert_fail(const char *expr, const char *file, int line, const
|
|
|
77
77
|
|
|
78
78
|
// Defind by the linker to have the address of the start of the heap.
|
|
79
79
|
extern unsigned char __heap_base;
|
|
80
|
+
extern unsigned char __heap_end;
|
|
80
81
|
|
|
81
82
|
// Behavior of right shifting a signed integer is compiler implementation defined.
|
|
82
83
|
static_assert((((int32_t)0x80000000U) >> 31) == -1, "This malloc implementation requires that right-shifting a signed integer produces a sign-extending (arithmetic) shift!");
|
|
@@ -566,9 +567,13 @@ static bool claim_more_memory(size_t numBytes)
|
|
|
566
567
|
// If this is the first time we're called, see if we can use
|
|
567
568
|
// the initial heap memory set up by wasm-ld.
|
|
568
569
|
if (!listOfAllRegions) {
|
|
569
|
-
unsigned char *
|
|
570
|
-
|
|
571
|
-
|
|
570
|
+
unsigned char *heap_base = &__heap_base;
|
|
571
|
+
unsigned char *heap_end = &__heap_end;
|
|
572
|
+
if (heap_end < heap_base) {
|
|
573
|
+
__builtin_trap();
|
|
574
|
+
}
|
|
575
|
+
if (numBytes <= (size_t)(heap_end - heap_base)) {
|
|
576
|
+
startPtr = heap_base;
|
|
572
577
|
endPtr = heap_end;
|
|
573
578
|
break;
|
|
574
579
|
}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.44 (bec42dac7873903d09d713963e34020c22a8bd2d)
|
|
2
|
-
clang version 17.0.0 (https://github.com/llvm/llvm-project a8cbd27d1f238e104a5d5ca345d93bc1f4d4ab1f)
|
|
3
|
-
Target: wasm32-unknown-emscripten
|
|
4
|
-
Thread model: posix
|
|
5
|
-
InstalledDir: /home/runner/work/_temp/14a1b1b6-9a98-4bac-8f31-5c9ebda805ed/emsdk-main/upstream/bin
|
package/lib/wasm32-wasi.txt
DELETED
package/lib/wasm32.txt
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|