-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: #49313 Refs: #36510 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com>
1 parent
5877c40
commit 96874e8
Showing
16 changed files
with
307 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,12 @@ | |
"sources": [ | ||
"test_reference.c" | ||
] | ||
}, | ||
{ | ||
"target_name": "test_finalizer", | ||
"sources": [ | ||
"test_finalizer.c" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <assert.h> | ||
#include <js_native_api.h> | ||
#include <stdlib.h> | ||
#include "../common.h" | ||
#include "../entry_point.h" | ||
|
||
static int test_value = 1; | ||
static int finalize_count = 0; | ||
|
||
static void FinalizeExternalCallJs(napi_env env, void* data, void* hint) { | ||
int* actual_value = data; | ||
NODE_API_ASSERT_RETURN_VOID( | ||
env, | ||
actual_value == &test_value, | ||
"The correct pointer was passed to the finalizer"); | ||
|
||
napi_ref finalizer_ref = (napi_ref)hint; | ||
napi_value js_finalizer; | ||
napi_value recv; | ||
NODE_API_CALL_RETURN_VOID( | ||
env, napi_get_reference_value(env, finalizer_ref, &js_finalizer)); | ||
NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv)); | ||
NODE_API_CALL_RETURN_VOID( | ||
env, napi_call_function(env, recv, js_finalizer, 0, NULL, NULL)); | ||
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, finalizer_ref)); | ||
} | ||
|
||
static napi_value CreateExternalWithJsFinalize(napi_env env, | ||
napi_callback_info info) { | ||
size_t argc = 1; | ||
napi_value args[1]; | ||
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments"); | ||
napi_value finalizer = args[0]; | ||
napi_valuetype finalizer_valuetype; | ||
NODE_API_CALL(env, napi_typeof(env, finalizer, &finalizer_valuetype)); | ||
NODE_API_ASSERT(env, | ||
finalizer_valuetype == napi_function, | ||
"Wrong type of first argument"); | ||
napi_ref finalizer_ref; | ||
NODE_API_CALL(env, napi_create_reference(env, finalizer, 1, &finalizer_ref)); | ||
|
||
napi_value result; | ||
NODE_API_CALL(env, | ||
napi_create_external(env, | ||
&test_value, | ||
FinalizeExternalCallJs, | ||
finalizer_ref, /* finalize_hint */ | ||
&result)); | ||
|
||
finalize_count = 0; | ||
return result; | ||
} | ||
|
||
EXTERN_C_START | ||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_property_descriptor descriptors[] = { | ||
DECLARE_NODE_API_PROPERTY("createExternalWithJsFinalize", | ||
CreateExternalWithJsFinalize), | ||
}; | ||
|
||
NODE_API_CALL( | ||
env, | ||
napi_define_properties(env, | ||
exports, | ||
sizeof(descriptors) / sizeof(*descriptors), | ||
descriptors)); | ||
|
||
return exports; | ||
} | ||
EXTERN_C_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include <node_api.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "../../js-native-api/common.h" | ||
|
||
static const char theText[] = | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."; | ||
|
||
static void malignDeleter(napi_env env, void* data, void* finalize_hint) { | ||
NODE_API_ASSERT_RETURN_VOID( | ||
env, data != NULL && strcmp(data, theText) == 0, "invalid data"); | ||
napi_ref finalizer_ref = (napi_ref)finalize_hint; | ||
napi_value js_finalizer; | ||
napi_value recv; | ||
NODE_API_CALL_RETURN_VOID( | ||
env, napi_get_reference_value(env, finalizer_ref, &js_finalizer)); | ||
NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &recv)); | ||
NODE_API_CALL_RETURN_VOID( | ||
env, napi_call_function(env, recv, js_finalizer, 0, NULL, NULL)); | ||
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, finalizer_ref)); | ||
} | ||
|
||
static napi_value malignFinalizerBuffer(napi_env env, napi_callback_info info) { | ||
size_t argc = 1; | ||
napi_value args[1]; | ||
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments"); | ||
napi_value finalizer = args[0]; | ||
napi_valuetype finalizer_valuetype; | ||
NODE_API_CALL(env, napi_typeof(env, finalizer, &finalizer_valuetype)); | ||
NODE_API_ASSERT(env, | ||
finalizer_valuetype == napi_function, | ||
"Wrong type of first argument"); | ||
napi_ref finalizer_ref; | ||
NODE_API_CALL(env, napi_create_reference(env, finalizer, 1, &finalizer_ref)); | ||
|
||
napi_value theBuffer; | ||
NODE_API_CALL(env, | ||
napi_create_external_buffer(env, | ||
sizeof(theText), | ||
(void*)theText, | ||
malignDeleter, | ||
finalizer_ref, // finalize_hint | ||
&theBuffer)); | ||
return theBuffer; | ||
} | ||
|
||
static napi_value Init(napi_env env, napi_value exports) { | ||
napi_property_descriptor methods[] = { | ||
DECLARE_NODE_API_PROPERTY("malignFinalizerBuffer", malignFinalizerBuffer), | ||
}; | ||
|
||
NODE_API_CALL( | ||
env, | ||
napi_define_properties( | ||
env, exports, sizeof(methods) / sizeof(methods[0]), methods)); | ||
|
||
return exports; | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
test/node-api/test_threadsafe_function/test_uncaught_exception.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
26 changes: 3 additions & 23 deletions
26
test/node-api/test_threadsafe_function/test_uncaught_exception.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
8 changes: 8 additions & 0 deletions
8
test/node-api/test_threadsafe_function/test_uncaught_exception_v9.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
31 changes: 31 additions & 0 deletions
31
test/node-api/test_threadsafe_function/uncaught_exception.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; |