From 56e881d0b0b2997b518753cb627eb3b50eeb6f62 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Mon, 20 Mar 2017 14:55:26 -0700 Subject: 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 Author: Gabriel Schulhof Author: Hitesh Kanwathirtha Author: Ian Halliday Author: Jason Ginchereau Author: Michael Dawson Author: Sampson Gao Author: Taylor Woll PR-URL: https://github.com/nodejs/node/pull/11975 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- test/addons-napi/1_hello_world/binding.c | 22 ++++++++++++++++++++++ test/addons-napi/1_hello_world/binding.gyp | 8 ++++++++ test/addons-napi/1_hello_world/test.js | 6 ++++++ 3 files changed, 36 insertions(+) create mode 100644 test/addons-napi/1_hello_world/binding.c create mode 100644 test/addons-napi/1_hello_world/binding.gyp create mode 100644 test/addons-napi/1_hello_world/test.js (limited to 'test/addons-napi/1_hello_world') diff --git a/test/addons-napi/1_hello_world/binding.c b/test/addons-napi/1_hello_world/binding.c new file mode 100644 index 00000000000..882508b9548 --- /dev/null +++ b/test/addons-napi/1_hello_world/binding.c @@ -0,0 +1,22 @@ +#include + +void Method(napi_env env, napi_callback_info info) { + napi_status status; + napi_value world; + status = napi_create_string_utf8(env, "world", -1, &world); + if (status != napi_ok) return; + status = napi_set_return_value(env, info, world); + if (status != napi_ok) return; +} + +#define DECLARE_NAPI_METHOD(name, func) \ + { name, func, 0, 0, 0, napi_default, 0 } + +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { + napi_status status; + napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", Method); + status = napi_define_properties(env, exports, 1, &desc); + if (status != napi_ok) return; +} + +NAPI_MODULE(addon, Init) diff --git a/test/addons-napi/1_hello_world/binding.gyp b/test/addons-napi/1_hello_world/binding.gyp new file mode 100644 index 00000000000..62381d5e54f --- /dev/null +++ b/test/addons-napi/1_hello_world/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "binding", + "sources": [ "binding.c" ] + } + ] +} diff --git a/test/addons-napi/1_hello_world/test.js b/test/addons-napi/1_hello_world/test.js new file mode 100644 index 00000000000..c975c48a733 --- /dev/null +++ b/test/addons-napi/1_hello_world/test.js @@ -0,0 +1,6 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const addon = require(`./build/${common.buildType}/binding`); + +assert.strictEqual(addon.hello(), 'world'); -- cgit v1.2.3