worker: improve integration with native addons

Allow loading add-ons from multiple Node.js instances if they are
declared context-aware; in particular, this applies to N-API addons.

Also, plug a memory leak that occurred when registering N-API addons.

Refs: https://github.com/nodejs/node/pull/23319

PR-URL: https://github.com/nodejs/node/pull/26175
Fixes: https://github.com/nodejs/node/issues/21481
Fixes: https://github.com/nodejs/node/issues/21783
Fixes: https://github.com/nodejs/node/issues/25662
Fixes: https://github.com/nodejs/node/issues/20239
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
Anna Henningsen
2019-02-17 23:45:14 +01:00
parent 455b927be0
commit 8ebd339031
11 changed files with 322 additions and 29 deletions

View File

@@ -4,6 +4,11 @@ const assert = require('assert');
const { Worker, isMainThread, workerData } = require('worker_threads');
if (isMainThread) {
// Load the addon in the main thread first.
// This checks that N-API addons can be loaded from multiple contexts
// when they are not loaded through NAPI_MODULE().
require(`./build/${common.buildType}/test_worker_terminate`);
const counter = new Int32Array(new SharedArrayBuffer(4));
const worker = new Worker(__filename, { workerData: { counter } });
worker.on('exit', common.mustCall(() => {

View File

@@ -36,4 +36,6 @@ napi_value Init(napi_env env, napi_value exports) {
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)