-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worker: improve integration with native addons
Native addons are now unloaded if all Environments referring to it have been cleaned up, except if it also loaded by the main Environment. Thanks go to Alexey Orlenko, Olivia Hugger and Stephen Belanger for reviewing the original version of this change. Refs: ayojs/ayo#118
- Loading branch information
Showing
6 changed files
with
255 additions
and
76 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
|
||
const w = new Worker(` | ||
require('worker_threads').parentPort.postMessage( | ||
require(${JSON.stringify(binding)}).hello());`, { eval: true }); | ||
w.on('message', common.mustCall((message) => { | ||
assert.strictEqual(message, 'world'); | ||
})); |
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,45 @@ | ||
#include <assert.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <node.h> | ||
#include <v8.h> | ||
|
||
using v8::Context; | ||
using v8::HandleScope; | ||
using v8::Isolate; | ||
using v8::Local; | ||
using v8::Object; | ||
using v8::Value; | ||
|
||
size_t count = 0; | ||
|
||
struct statically_allocated { | ||
statically_allocated() { | ||
assert(count == 0); | ||
printf("ctor "); | ||
} | ||
~statically_allocated() { | ||
assert(count == 0); | ||
printf("dtor"); | ||
} | ||
} var; | ||
|
||
void Dummy(void*) { | ||
assert(0); | ||
} | ||
|
||
void Cleanup(void* str) { | ||
printf("%s ", static_cast<const char*>(str)); | ||
} | ||
|
||
void Initialize(Local<Object> exports, | ||
Local<Value> module, | ||
Local<Context> context) { | ||
node::AddEnvironmentCleanupHook( | ||
context->GetIsolate(), Cleanup, | ||
const_cast<void*>(static_cast<const void*>("cleanup"))); | ||
node::AddEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr); | ||
node::RemoveEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr); | ||
} | ||
|
||
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize) |
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,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], | ||
'sources': [ 'binding.cc' ] | ||
} | ||
] | ||
} |
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,17 @@ | ||
// Flags: --experimental-worker | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const child_process = require('child_process'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
|
||
if (process.argv[2] === 'child') { | ||
new Worker(`require(${JSON.stringify(binding)});`, { eval: true }); | ||
} else { | ||
const proc = child_process.spawnSync(process.execPath, [__filename, 'child']); | ||
assert.strictEqual(proc.stderr.toString(), ''); | ||
assert.strictEqual(proc.stdout.toString(), 'ctor cleanup dtor'); | ||
assert.strictEqual(proc.status, 0); | ||
} |