summaryrefslogtreecommitdiff
path: root/src/node_api.cc
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-06-05 23:29:18 +0200
committerAnna Henningsen <anna@addaleax.net>2019-06-14 13:03:12 +0200
commit3ca0df22e67704f061e0f3b3dcdff89bb66ae4f2 (patch)
tree4a8d7e7a167ffd5eb48ff0c887c4edc8c65d7ed2 /src/node_api.cc
parentfb4d5286b05dd02054ae0e471ab9d3c3b3845381 (diff)
downloadandroid-node-v8-3ca0df22e67704f061e0f3b3dcdff89bb66ae4f2.tar.gz
android-node-v8-3ca0df22e67704f061e0f3b3dcdff89bb66ae4f2.tar.bz2
android-node-v8-3ca0df22e67704f061e0f3b3dcdff89bb66ae4f2.zip
n-api: defer Buffer finalizer with SetImmediate
We have a test that verifies that JS execution from the Buffer finalizer is accepted, and that errors thrown are passed down synchronously. However, since the finalizer executes during GC, this is behaviour is fundamentally invalid and, for good reasons, disallowed by the JS engine. This leaves us with the options of either finding a way to allow JS execution from the callback, or explicitly forbidding it on the N-API side as well. This commit implements the former option, since it is the more backwards-compatible one, in the sense that the current situation sometimes appears to work as well and we should not break that behaviour if we don’t have to, but rather try to actually make it work reliably. Since GC timing is largely unobservable anyway, this commit moves the callback into a `SetImmediate()`, as we do elsewhere in the code, and a second pass callback is not an easily implemented option, as the API is supposed to wrap around Node’s `Buffer` API. In this case, exceptions are handled like other uncaught exceptions. Two tests have to be adjusted to account for the timing difference. This is unfortunate, but unavoidable if we want to conform to the JS engine API contract and keep all tests. Fixes: https://github.com/nodejs/node/issues/26754 PR-URL: https://github.com/nodejs/node/pull/28082 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_api.cc')
-rw-r--r--src/node_api.cc29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/node_api.cc b/src/node_api.cc
index 91e6a14cdc..c4d8634286 100644
--- a/src/node_api.cc
+++ b/src/node_api.cc
@@ -32,21 +32,30 @@ namespace v8impl {
namespace {
-class BufferFinalizer: private Finalizer {
+class BufferFinalizer : private Finalizer {
public:
// node::Buffer::FreeCallback
static void FinalizeBufferCallback(char* data, void* hint) {
BufferFinalizer* finalizer = static_cast<BufferFinalizer*>(hint);
- if (finalizer->_finalize_callback != nullptr) {
- NapiCallIntoModuleThrow(finalizer->_env, [&]() {
- finalizer->_finalize_callback(
- finalizer->_env,
- data,
- finalizer->_finalize_hint);
- });
- }
+ finalizer->_finalize_data = data;
+ static_cast<node_napi_env>(finalizer->_env)->node_env()
+ ->SetImmediate([](node::Environment* env, void* hint) {
+ BufferFinalizer* finalizer = static_cast<BufferFinalizer*>(hint);
+
+ if (finalizer->_finalize_callback != nullptr) {
+ v8::HandleScope handle_scope(finalizer->_env->isolate);
+ v8::Context::Scope context_scope(finalizer->_env->context());
+
+ NapiCallIntoModuleThrow(finalizer->_env, [&]() {
+ finalizer->_finalize_callback(
+ finalizer->_env,
+ finalizer->_finalize_data,
+ finalizer->_finalize_hint);
+ });
+ }
- Delete(finalizer);
+ Delete(finalizer);
+ }, hint);
}
};