You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.6 KiB

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> PR-URL: https://github.com/nodejs/node/pull/11975 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
8 years ago
'use strict';
const common = require('../../common');
const test_error = require(`./build/${common.buildType}/test_error`);
const assert = require('assert');
const theError = new Error('Some error');
const theTypeError = new TypeError('Some type error');
const theSyntaxError = new SyntaxError('Some syntax error');
const theRangeError = new RangeError('Some type error');
const theReferenceError = new ReferenceError('Some reference error');
const theURIError = new URIError('Some URI error');
const theEvalError = new EvalError('Some eval error');
class MyError extends Error { }
const myError = new MyError('Some MyError');
// Test that native error object is correctly classed
assert.strictEqual(test_error.checkError(theError), true,
'Error object correctly classed by napi_is_error');
// Test that native type error object is correctly classed
assert.strictEqual(test_error.checkError(theTypeError), true,
'Type error object correctly classed by napi_is_error');
// Test that native syntax error object is correctly classed
assert.strictEqual(test_error.checkError(theSyntaxError), true,
'Syntax error object correctly classed by napi_is_error');
// Test that native range error object is correctly classed
assert.strictEqual(test_error.checkError(theRangeError), true,
'Range error object correctly classed by napi_is_error');
// Test that native reference error object is correctly classed
assert.strictEqual(test_error.checkError(theReferenceError), true,
'Reference error object correctly classed by' +
' napi_is_error');
// Test that native URI error object is correctly classed
assert.strictEqual(test_error.checkError(theURIError), true,
'URI error object correctly classed by napi_is_error');
// Test that native eval error object is correctly classed
assert.strictEqual(test_error.checkError(theEvalError), true,
'Eval error object correctly classed by napi_is_error');
// Test that class derived from native error is correctly classed
assert.strictEqual(test_error.checkError(myError), true,
'Class derived from native error correctly classed by' +
' napi_is_error');
// Test that non-error object is correctly classed
assert.strictEqual(test_error.checkError({}), false,
'Non-error object correctly classed by napi_is_error');
// Test that non-error primitive is correctly classed
assert.strictEqual(test_error.checkError('non-object'), false,
'Non-error primitive correctly classed by napi_is_error');
assert.throws(() => {
test_error.throwExistingError();
}, /^Error: existing error$/);
assert.throws(() => {
test_error.throwError();
}, /^Error: error$/);
assert.throws(() => {
test_error.throwRangeError();
}, /^RangeError: range error$/);
assert.throws(() => {
test_error.throwTypeError();
}, /^TypeError: type error$/);
let error = test_error.createError();
assert.ok(error instanceof Error, 'expected error to be an instance of Error');
assert.strictEqual(error.message, 'error', 'expected message to be "error"');
error = test_error.createRangeError();
assert.ok(error instanceof RangeError,
'expected error to be an instance of RangeError');
assert.strictEqual(error.message,
'range error',
'expected message to be "range error"');
error = test_error.createTypeError();
assert.ok(error instanceof TypeError,
'expected error to be an instance of TypeError');
assert.strictEqual(error.message,
'type error',
'expected message to be "type error"');