node-api: enable uncaught exceptions policy by default

This enables the option `--force-node-api-uncaught-exceptions-policy`
for a specific Node-API addon when it is compiled with
`NAPI_EXPERIMENTAL` (and this would be the default behavior when
`NAPI_VERSION` 10 releases). This would not break existing Node-API
addons.

PR-URL: https://github.com/nodejs/node/pull/49313
Refs: https://github.com/nodejs/node/pull/36510
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com>
This commit is contained in:
Chengzhong Wu authored and Michaël Zasso committed 2023-11-27 11:57:14 +01:00
1 parent c5857bde42
commit edef4fa668
16 files changed
+307 -133

No files matched your search

@@ -3,6 +3,20 @@
{
'target_name': 'binding',
'sources': ['binding.c']
},
{
'target_name': 'test_uncaught_exception_v9',
'defines': [
'NAPI_VERSION=9'
],
'sources': ['test_uncaught_exception.c']
},
{
'target_name': 'test_uncaught_exception',
'defines': [
'NAPI_EXPERIMENTAL'
],
'sources': ['test_uncaught_exception.c']
}
]
}
@@ -2,7 +2,7 @@
// Flags: --no-force-node-api-uncaught-exceptions-policy
const common = require('../../common');
const binding = require(`./build/${common.buildType}/binding`);
const binding = require(`./build/${common.buildType}/test_uncaught_exception_v9`);
process.on(
'uncaughtException',
@@ -0,0 +1,62 @@
#include <node_api.h>
#include "../../js-native-api/common.h"
// Testing calling into JavaScript
static void ThreadSafeFunctionFinalize(napi_env env,
void* finalize_data,
void* finalize_hint) {
napi_ref js_func_ref = (napi_ref)finalize_data;
napi_value js_func;
napi_value recv;
NODE_API_CALL_RETURN_VOID(
env, napi_get_reference_value(env, js_func_ref, &js_func));
NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv));
NODE_API_CALL_RETURN_VOID(
env, napi_call_function(env, recv, js_func, 0, NULL, NULL));
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_func_ref));
}
// Testing calling into JavaScript
static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
size_t argc = 4;
napi_value argv[4];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
napi_ref finalize_func;
NODE_API_CALL(env, napi_create_reference(env, argv[3], 1, &finalize_func));
napi_threadsafe_function tsfn;
NODE_API_CALL(env,
napi_create_threadsafe_function(env,
argv[0],
argv[1],
argv[2],
0,
1,
finalize_func,
ThreadSafeFunctionFinalize,
NULL,
NULL,
&tsfn));
NODE_API_CALL(env,
napi_call_threadsafe_function(tsfn, NULL, napi_tsfn_blocking));
NODE_API_CALL(env, napi_release_threadsafe_function(tsfn, napi_tsfn_release));
return NULL;
}
// Module init
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor properties[] = {
DECLARE_NODE_API_PROPERTY("CallIntoModule", CallIntoModule),
};
NODE_API_CALL(
env,
napi_define_properties(env,
exports,
sizeof(properties) / sizeof(properties[0]),
properties));
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
@@ -1,27 +1,7 @@
'use strict';
// Flags: --force-node-api-uncaught-exceptions-policy
const common = require('../../common');
const assert = require('assert');
const binding = require(`./build/${common.buildType}/binding`);
const binding = require(`./build/${common.buildType}/test_uncaught_exception`);
const { testUncaughtException } = require('./uncaught_exception');
const callbackCheck = common.mustCall((err) => {
assert.throws(() => { throw err; }, /callback error/);
process.removeListener('uncaughtException', callbackCheck);
process.on('uncaughtException', finalizerCheck);
});
const finalizerCheck = common.mustCall((err) => {
assert.throws(() => { throw err; }, /finalizer error/);
});
process.on('uncaughtException', callbackCheck);
binding.CallIntoModule(
common.mustCall(() => {
throw new Error('callback error');
}),
{},
'resource_name',
common.mustCall(function finalizer() {
throw new Error('finalizer error');
}),
);
testUncaughtException(binding);
@@ -0,0 +1,8 @@
'use strict';
// Flags: --force-node-api-uncaught-exceptions-policy
const common = require('../../common');
const binding = require(`./build/${common.buildType}/test_uncaught_exception_v9`);
const { testUncaughtException } = require('./uncaught_exception');
testUncaughtException(binding);
@@ -0,0 +1,31 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
function testUncaughtException(binding) {
const callbackCheck = common.mustCall((err) => {
assert.throws(() => { throw err; }, /callback error/);
process.removeListener('uncaughtException', callbackCheck);
process.on('uncaughtException', finalizerCheck);
});
const finalizerCheck = common.mustCall((err) => {
assert.throws(() => { throw err; }, /finalizer error/);
});
process.on('uncaughtException', callbackCheck);
binding.CallIntoModule(
common.mustCall(() => {
throw new Error('callback error');
}),
{},
'resource_name',
common.mustCall(function finalizer() {
throw new Error('finalizer error');
}),
);
}
module.exports = {
testUncaughtException,
};