git clone https://github.com/toyobayashi/emnapi.gitcd ./emnapinpm install -g node-gypnpm installnpm run build # output ./packages/*/distnode ./script/release.js # output ./out# test for emscriptennpm run rebuild -w packages/testnpm run test -w packages/test# test for wasi-sdknpm run rebuild:wt -w packages/testnpm run test:wt -w packages/test
npm install -D emnapinpm install @emnapi/runtime# additionally, for wasi-sdk, install @emnapi/corenpm install @emnapi/core# if you need node-addon-apinpm install node-addon-api
The wasi-libc futex ABI changed in wasi-sdk 34. The package keeps the
original wasm32-wasip1-threads archive set for wasi-sdk 33 or older and
ships the new ABI under wasm32-wasip1-threads-wasi-sdk-34 for wasi-sdk 34 or
newer. Keep the directory and the wasi-sdk used for linking matched; the
archive name remains -lemnapi-mt in both cases.
import init from './out/binding.js'import { createContext } from '@emnapi/runtime'const emnapiCtx = createContext()const Module = await init()const binding = Module.emnapiInit({ context: emnapiCtx, asyncWorkPoolSize: 4 // the same effect to UV_THREADPOOL_SIZE, must less than PTHREAD_POOL_SIZE})binding.run(/* ... */)// cleanupModule._uv_library_shutdown()emnapiCtx.destroy()
# Linux or macOSexport GYP_CROSSCOMPILE=1# emscriptenexport AR_target="$EMSDK/upstream/emscripten/emar"export CC_target="$EMSDK/upstream/emscripten/emcc"export CXX_target="$EMSDK/upstream/emscripten/em++"# wasi-sdkexport AR_target="$WASI_SDK_PATH/bin/ar"export CC_target="$WASI_SDK_PATH/bin/clang"export CXX_target="$WASI_SDK_PATH/bin/clang++"
@REM Windowsset GYP_CROSSCOMPILE=1@REM emscriptencall set AR_target=%%EMSDK:\=/%%/upstream/emscripten/emar.batcall set CC_target=%%EMSDK:\=/%%/upstream/emscripten/emcc.batcall set CXX_target=%%EMSDK:\=/%%/upstream/emscripten/em++.bat@REM wasi-sdkcall set AR_target=%%WASI_SDK_PATH:\=/%%/bin/ar.execall set CC_target=%%WASI_SDK_PATH:\=/%%/bin/clang.execall set CXX_target=%%WASI_SDK_PATH:\=/%%/bin/clang++.exe
@REM Use make generator on Windows@REM Run the bat file in POSIX-like environment (e.g. Cygwin)@REM emscriptencall npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-emscriptencall emmake.bat make -C %~dp0build@REM wasicall npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-wasimake -C %~dp0build@REM bare wasm32call npx.cmd node-gyp configure --arch=wasm32 --nodedir=./node_modules/emnapi -- -f make-wasmmake -C %~dp0build
Experimental V8 API shim and NAN
The V8 API shim is experimental and incomplete. It currently provides a
minimal compatibility layer focused on the V8 APIs NAN uses; it is not a
complete V8 implementation.
The bundled uv.h exposes the libuv types NAN needs to compile without
including pthread headers. Thread-backed libuv functions are still implemented
only in threaded builds. In particular, NAN's AsyncWorker and progress
workers call uv_queue_work, so addons that use them need the threaded
targets:
The CMake targets add the required Emscripten JavaScript libraries. The V8
shim is available for Emscripten and WASI targets; the bare
wasm32-unknown-unknown target does not build it.
For Emscripten, initialize the generated module with an emnapi context:
To instantiate a WASI module through @emnapi/core, pass the V8 plugin and
the WASI instance to instantiateAddon:
import { instantiateAddon } from '@emnapi/core'import v8 from '@emnapi/core/plugins/v8'import { createContext } from '@emnapi/runtime'const { addonModule } = await instantiateAddon(wasmBytes, { context: createContext(), wasi, plugins: [v8]})const binding = addonModule.exports
The V8 plugin is the only plugin required for the V8 shim.
The N-API-named APIs are deprecated for the 2.0.0 beta and will be removed
in 2.0.0-rc. The legacy napiModule result property follows the same schedule.
// emnapi main thread (could be in a Worker)instantiateAddon(input, { context: getDefaultContext(), asyncWorkPoolSize: 4, // the same effect to UV_THREADPOOL_SIZE, must less than `reuseWorker.size` wasi: new WASI(/* ... */), /** * Setting this to `true` or a delay (ms) makes * pthread_create() do not return until worker actually start. * It will throw error if emnapi runs in browser main thread * since browser disallow blocking the main thread (Atomics.wait). * @defaultValue false */ waitThreadStart: isNode || (isBrowser && !isBrowserMainThread), /** * Reuse the thread worker after thread exit to avoid re-creatation * * With `size > 0`, `instantiateAddon` / `loadAddon` wait until every pool * worker has loaded. `instantiateAddonSync` / `loadAddonSync` also accept a
Note: For browsers, all the multithreaded features relying on Web Workers (Emscripten pthread also relying on Web Workers)
require cross-origin isolation to enable SharedArrayBuffer. You can make a page cross-origin isolated
by serving the page with these headers: