Skip to content

Commit a9562fe

Browse files
jasonginMylesBorins
authored andcommittedApr 16, 2018
n-api: add support for abi stable module API
Add support for abi stable module API (N-API) as "Experimental feature". The goal of this API is to provide a stable Node API for native module developers. N-API aims to provide ABI compatibility guarantees across different Node versions and also across different Node VMs - allowing N-API enabled native modules to just work across different versions and flavors of Node.js without recompilation. A more detailed introduction is provided in: https://github.com/nodejs/node-eps/blob/master/005-ABI-Stable-Module-API.md and https://github.com/nodejs/abi-stable-node/blob/doc/VM%20Summit.pdf. The feature, during its experimental state, will be guarded by a runtime flag "--napi-modules". Only when this flag is added to the command line will N-API modules along with regular non N-API modules be supported. The API is defined by the methods in "src/node_api.h" and "src/node_api_types.h". This is the best starting point to review the API surface. More documentation will follow. In addition to the implementation of the API using V8, which is included in this PR, the API has also been validated against chakracore and that port is available in https://github.com/nodejs/abi-stable-node/tree/api-prototype-chakracore-8.x. The current plan is to provide N-API support in versions 8.X and 6.X directly. For older versions, such as 4.X or pre N-API versions of 6.X, we plan to create an external npm module to provide a migration path that will allow modules targeting older Node.js versions to use the API, albeit without getting the advantage of not having to recompile. In addition, we also plan an external npm package with C++ sugar to simplify the use of the API. The sugar will be in-line only and will only use the exported N-API methods but is not part of the N-API itself. The current version is in: https://github.com/nodejs/node-api. This PR is a result of work in the abi-stable-node repo: https://github.com/nodejs/abi-stable-node/tree/doc, with this PR being the cumulative work on the api-prototype-8.x branch with the following contributors in alphabetical order: Author: Arunesh Chandra <arunesh.chandra@microsoft.com> Author: Gabriel Schulhof <gabriel.schulhof@intel.com> Author: Hitesh Kanwathirtha <hiteshk@microsoft.com> Author: Ian Halliday <ianhall@microsoft.com> Author: Jason Ginchereau <jasongin@microsoft.com> Author: Michael Dawson <michael_dawson@ca.ibm.com> Author: Sampson Gao <sampsong@ca.ibm.com> Author: Taylor Woll <taylor.woll@microsoft.com> Backport-PR-URL: #19447 PR-URL: #11975 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e54b8e8 commit a9562fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+6586
-18
lines changed
 

‎Makefile

+52-8
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ v8:
120120

121121
test: all
122122
$(MAKE) build-addons
123+
$(MAKE) build-addons-napi
123124
$(MAKE) cctest
124125
$(PYTHON) tools/test.py --mode=release -J \
125-
doctool inspector known_issues message pseudo-tty parallel sequential addons
126+
doctool inspector known_issues message pseudo-tty parallel sequential addons addons-napi
126127
$(MAKE) lint
127128

128129
test-parallel: all
@@ -189,6 +190,41 @@ test/addons/.buildstamp: config.gypi \
189190
# TODO(bnoordhuis) Force rebuild after gyp update.
190191
build-addons: $(NODE_EXE) test/addons/.buildstamp
191192

193+
ADDONS_NAPI_BINDING_GYPS := \
194+
$(filter-out test/addons-napi/??_*/binding.gyp, \
195+
$(wildcard test/addons-napi/*/binding.gyp))
196+
197+
ADDONS_NAPI_BINDING_SOURCES := \
198+
$(filter-out test/addons-napi/??_*/*.cc, $(wildcard test/addons-napi/*/*.cc)) \
199+
$(filter-out test/addons-napi/??_*/*.h, $(wildcard test/addons-napi/*/*.h))
200+
201+
# Implicitly depends on $(NODE_EXE), see the build-addons-napi rule for rationale.
202+
test/addons-napi/.buildstamp: config.gypi \
203+
deps/npm/node_modules/node-gyp/package.json \
204+
$(ADDONS_NAPI_BINDING_GYPS) $(ADDONS_NAPI_BINDING_SOURCES) \
205+
deps/uv/include/*.h deps/v8/include/*.h \
206+
src/node.h src/node_buffer.h src/node_object_wrap.h src/node_version.h \
207+
src/node_api.h src/node_api_types.h
208+
# Cannot use $(wildcard test/addons-napi/*/) here, it's evaluated before
209+
# embedded addons have been generated from the documentation.
210+
@for dirname in test/addons-napi/*/; do \
211+
printf "\nBuilding addon $$PWD/$$dirname\n" ; \
212+
env MAKEFLAGS="-j1" $(NODE) deps/npm/node_modules/node-gyp/bin/node-gyp \
213+
--loglevel=$(LOGLEVEL) rebuild \
214+
--python="$(PYTHON)" \
215+
--directory="$$PWD/$$dirname" \
216+
--nodedir="$$PWD" || exit 1 ; \
217+
done
218+
touch $@
219+
220+
# .buildstamp and .docbuildstamp need $(NODE_EXE) but cannot depend on it
221+
# directly because it calls make recursively. The parent make cannot know
222+
# if the subprocess touched anything so it pessimistically assumes that
223+
# .buildstamp and .docbuildstamp are out of date and need a rebuild.
224+
# Just goes to show that recursive make really is harmful...
225+
# TODO(bnoordhuis) Force rebuild after gyp or node-gyp update.
226+
build-addons-napi: $(NODE_EXE) test/addons-napi/.buildstamp
227+
192228
clear-stalled:
193229
# Clean up any leftover processes but don't error if found.
194230
ps awwx | grep Release/node | grep -v grep | cat
@@ -200,20 +236,22 @@ clear-stalled:
200236
test-gc: all test/gc/node_modules/weak/build/Release/weakref.node
201237
$(PYTHON) tools/test.py --mode=release gc
202238

203-
test-build: | all build-addons
239+
test-build: | all build-addons build-addons-napi
240+
241+
test-build-addons-napi: all build-addons-napi
204242

205243
test-all: test-build test/gc/node_modules/weak/build/Release/weakref.node
206244
$(PYTHON) tools/test.py --mode=debug,release
207245

208246
test-all-valgrind: test-build
209247
$(PYTHON) tools/test.py --mode=debug,release --valgrind
210248

211-
CI_NATIVE_SUITES := addons
249+
CI_NATIVE_SUITES := addons addons-napi
212250
CI_JS_SUITES := doctool inspector known_issues message parallel pseudo-tty sequential
213251

214252
# Build and test addons without building anything else
215253
test-ci-native: LOGLEVEL := info
216-
test-ci-native: | test/addons/.buildstamp
254+
test-ci-native: | test/addons/.buildstamp test/addons-napi/.buildstamp
217255
$(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \
218256
--mode=release --flaky-tests=$(FLAKY_TESTS) \
219257
$(TEST_CI_ARGS) $(CI_NATIVE_SUITES)
@@ -231,11 +269,11 @@ test-ci-js: | clear-stalled
231269
fi
232270

233271
test-ci: LOGLEVEL := info
234-
test-ci: | clear-stalled build-addons
272+
test-ci: | clear-stalled build-addons build-addons-napi
235273
out/Release/cctest --gtest_output=tap:cctest.tap
236274
$(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \
237275
--mode=release --flaky-tests=$(FLAKY_TESTS) \
238-
$(TEST_CI_ARGS) $(CI_JS_SUITES) $(CI_NATIVE_SUITES)
276+
$(TEST_CI_ARGS) $(CI_JS_SUITES) addons-napi $(CI_NATIVE_SUITES)
239277
# Clean up any leftover processes, error if found.
240278
ps awwx | grep Release/node | grep -v grep | cat
241279
@PS_OUT=`ps awwx | grep Release/node | grep -v grep | awk '{print $$1}'`; \
@@ -282,7 +320,10 @@ test-npm: $(NODE_EXE)
282320
test-npm-publish: $(NODE_EXE)
283321
npm_package_config_publishtest=true $(NODE) deps/npm/test/run.js
284322

285-
test-addons: test-build
323+
test-addons-napi: test-build-addons-napi
324+
$(PYTHON) tools/test.py --mode=release addons-napi
325+
326+
test-addons: test-build test-addons-napi
286327
$(PYTHON) tools/test.py --mode=release addons
287328

288329
test-addons-clean:
@@ -813,6 +854,8 @@ LINT_CPP_FILES = $(filter-out $(LINT_CPP_EXCLUDE), $(wildcard \
813854
test/addons/*/*.h \
814855
test/cctest/*.cc \
815856
test/cctest/*.h \
857+
test/addons-napi/*/*.cc \
858+
test/addons-napi/*/*.h \
816859
tools/icu/*.cc \
817860
tools/icu/*.h \
818861
))
@@ -857,5 +900,6 @@ endif
857900
bench-buffer bench-net bench-http bench-fs bench-tls cctest run-ci \
858901
test-v8 test-v8-intl test-v8-benchmarks test-v8-all v8 lint-ci \
859902
bench-ci lint-js-ci doc-only $(TARBALL)-headers test-ci test-ci-native \
860-
test-ci-js build-ci test-hash-seed clear-stalled
903+
test-ci-js build-ci test-hash-seed clear-stalled test-addons-napi \
904+
build-addons-napi
861905

‎doc/api/cli.md

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ added: v6.0.0
119119

120120
Silence all process warnings (including deprecations).
121121

122+
### `--napi-modules`
123+
<!-- YAML
124+
added: REPLACEME
125+
-->
126+
127+
Enable loading native modules compiled with the ABI-stable Node.js API (N-API)
128+
(experimental).
129+
122130
### `--trace-warnings`
123131
<!-- YAML
124132
added: v6.0.0

0 commit comments

Comments
 (0)
Please sign in to comment.