forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
n-api: ensure scope present for finalization
Refs: nodejs/node-addon-api#722 Ensure a scope is on stack during finalization as finalization functions can create JS Objects Signed-off-by: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_worker_terminate_finalization", | ||
"sources": [ "test_worker_terminate_finalization.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,18 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const { Worker, isMainThread } = require('worker_threads'); | ||
|
||
if (isMainThread) { | ||
const worker = new Worker(__filename); | ||
worker.on('error', common.mustNotCall()); | ||
} else { | ||
const { Test } = | ||
require(`./build/${common.buildType}/test_worker_terminate_finalization`); | ||
|
||
// Spin up thread and call add-on create the right sequence | ||
// of rerences to hit the case reported in | ||
// https://github.com/nodejs/node-addon-api/issues/722 | ||
// will crash if run under debug and its not possible to | ||
// create object in the specific finalizer | ||
Test(new Object()); | ||
} |
41 changes: 41 additions & 0 deletions
41
test/node-api/test_worker_terminate_finalization/test_worker_terminate_finalization.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,41 @@ | ||
#include <stdio.h> | ||
#include <node_api.h> | ||
#include <assert.h> | ||
#include <malloc.h> | ||
#include "../../js-native-api/common.h" | ||
|
||
int wrappedNativeData; | ||
napi_ref ref; | ||
void WrapFinalizer(napi_env env, void* data, void* hint) { | ||
uint32_t count; | ||
NAPI_CALL_RETURN_VOID(env, napi_reference_unref(env, ref, &count)); | ||
} | ||
|
||
napi_value Test(napi_env env, napi_callback_info info) { | ||
size_t argc = 1; | ||
napi_value argv[1]; | ||
napi_value result; | ||
napi_ref ref; | ||
napi_ref wrapResult; | ||
|
||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); | ||
NAPI_CALL(env, napi_create_external_buffer(env, 4, (void*) malloc(4), NULL, NULL, &result)); | ||
NAPI_CALL(env, napi_create_reference(env, result, 1, &ref)); | ||
NAPI_CALL(env, napi_wrap(env, argv[0], (void*) &wrappedNativeData, WrapFinalizer, NULL, &wrapResult)); | ||
return NULL; | ||
} | ||
|
||
napi_value Init(napi_env env, napi_value exports) { | ||
napi_property_descriptor properties[] = { | ||
DECLARE_NAPI_PROPERTY("Test", Test) | ||
}; | ||
|
||
NAPI_CALL(env, napi_define_properties( | ||
env, exports, sizeof(properties) / sizeof(*properties), properties)); | ||
|
||
return exports; | ||
} | ||
|
||
// Do not start using NAPI_MODULE_INIT() here, so that we can test | ||
// compatibility of Workers with NAPI_MODULE(). | ||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |