-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: add AsyncWorker destruction suppression
Add method `SuppressDestruct()` to `AsyncWorker`, which will cause an instance of the class to remain allocated even after the `OnOK` callback fires. Such an instance must be explicitly `delete`-ed from user code. Re: #231 Re: nodejs/abi-stable-node#353
- Loading branch information
Gabriel Schulhof
committed
Dec 13, 2018
1 parent
d8e9c22
commit d1e9315
Showing
8 changed files
with
113 additions
and
2 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
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,69 @@ | ||
#include "napi.h" | ||
|
||
// A variant of TestWorker wherein destruction is suppressed. That is, instances | ||
// are not destroyed during the `OnOK` callback. They must be explicitly | ||
// destroyed. | ||
|
||
using namespace Napi; | ||
|
||
class PersistentTestWorker : public AsyncWorker { | ||
public: | ||
static PersistentTestWorker* current_worker; | ||
static void DoWork(const CallbackInfo& info) { | ||
bool succeed = info[0].As<Boolean>(); | ||
Object resource = info[1].As<Object>(); | ||
Function cb = info[2].As<Function>(); | ||
Value data = info[3]; | ||
|
||
PersistentTestWorker* worker = | ||
new PersistentTestWorker(cb, "TestResource", resource); | ||
current_worker = worker; | ||
|
||
worker->SuppressDestruct(); | ||
worker->Receiver().Set("data", data); | ||
worker->_succeed = succeed; | ||
worker->Queue(); | ||
} | ||
|
||
static Value GetWorkerGone(const CallbackInfo& info) { | ||
return Boolean::New(info.Env(), current_worker == nullptr); | ||
} | ||
|
||
static void DeleteWorker(const CallbackInfo& info) { | ||
if (current_worker == nullptr) { | ||
Error::New(info.Env(), "No current worker").ThrowAsJavaScriptException(); | ||
} | ||
delete current_worker; | ||
} | ||
|
||
~PersistentTestWorker() { | ||
current_worker = nullptr; | ||
} | ||
|
||
protected: | ||
void Execute() override { | ||
if (!_succeed) { | ||
SetError("test error"); | ||
} | ||
} | ||
|
||
private: | ||
PersistentTestWorker(Function cb, | ||
const char* resource_name, | ||
const Object& resource) | ||
: AsyncWorker(cb, resource_name, resource) {} | ||
bool _succeed; | ||
}; | ||
|
||
PersistentTestWorker* PersistentTestWorker::current_worker = nullptr; | ||
|
||
Object InitPersistentAsyncWorker(Env env) { | ||
Object exports = Object::New(env); | ||
exports["doWork"] = Function::New(env, PersistentTestWorker::DoWork); | ||
exports.DefineProperty( | ||
PropertyDescriptor::Accessor("workerGone", | ||
PersistentTestWorker::GetWorkerGone)); | ||
exports["deleteWorker"] = | ||
Function::New(env, PersistentTestWorker::DeleteWorker); | ||
return exports; | ||
} |
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 buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const common = require('./common'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
binding.persistentasyncworker.doWork(false, {}, function (e) { | ||
setImmediate(() => { | ||
assert.strictEqual(binding.persistentasyncworker.workerGone, false); | ||
binding.persistentasyncworker.deleteWorker(); | ||
assert.strictEqual(binding.persistentasyncworker.workerGone, true); | ||
}); | ||
}, 'test data'); | ||
|
||
} |
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