mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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:
@@ -1,6 +1,8 @@
|
||||
'use strict';
|
||||
const common = require('../../common');
|
||||
const assert = require('assert');
|
||||
const { Worker } = require('worker_threads');
|
||||
|
||||
const bindingPath = require.resolve(`./build/${common.buildType}/binding`);
|
||||
const binding = require(bindingPath);
|
||||
assert.strictEqual(binding.hello(), 'world');
|
||||
@@ -11,3 +13,10 @@ delete require.cache[bindingPath];
|
||||
const rebinding = require(bindingPath);
|
||||
assert.strictEqual(rebinding.hello(), 'world');
|
||||
assert.notStrictEqual(binding.hello, rebinding.hello);
|
||||
|
||||
// Test that workers can load addons declared using NAPI_MODULE_INIT().
|
||||
new Worker(`
|
||||
const { parentPort } = require('worker_threads');
|
||||
const msg = require(${JSON.stringify(bindingPath)}).hello();
|
||||
parentPort.postMessage(msg)`, { eval: true })
|
||||
.on('message', common.mustCall((msg) => assert.strictEqual(msg, 'world')));
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user