-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tsfn: add error checking on GetContext (#583)
PR-URL: #583 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
- Loading branch information
Showing
6 changed files
with
86 additions
and
1 deletion.
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
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,63 @@ | ||
#include "napi.h" | ||
|
||
#if (NAPI_VERSION > 3) | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
class TSFNWrap : public ObjectWrap<TSFNWrap> { | ||
public: | ||
static Object Init(Napi::Env env, Object exports); | ||
TSFNWrap(const CallbackInfo &info); | ||
|
||
Napi::Value GetContext(const CallbackInfo & /*info*/) { | ||
Reference<Napi::Value> *ctx = _tsfn.GetContext(); | ||
return ctx->Value(); | ||
}; | ||
|
||
Napi::Value Release(const CallbackInfo &info) { | ||
Napi::Env env = info.Env(); | ||
_deferred = std::unique_ptr<Promise::Deferred>(new Promise::Deferred(env)); | ||
_tsfn.Release(); | ||
return _deferred->Promise(); | ||
}; | ||
|
||
private: | ||
ThreadSafeFunction _tsfn; | ||
std::unique_ptr<Promise::Deferred> _deferred; | ||
}; | ||
|
||
Object TSFNWrap::Init(Napi::Env env, Object exports) { | ||
Function func = | ||
DefineClass(env, "TSFNWrap", | ||
{InstanceMethod("getContext", &TSFNWrap::GetContext), | ||
InstanceMethod("release", &TSFNWrap::Release)}); | ||
|
||
exports.Set("TSFNWrap", func); | ||
return exports; | ||
} | ||
|
||
TSFNWrap::TSFNWrap(const CallbackInfo &info) : ObjectWrap<TSFNWrap>(info) { | ||
Napi::Env env = info.Env(); | ||
|
||
Reference<Napi::Value> *_ctx = new Reference<Napi::Value>; | ||
*_ctx = Persistent(info[0]); | ||
|
||
_tsfn = ThreadSafeFunction::New( | ||
info.Env(), Function::New(env, [](const CallbackInfo & /*info*/) {}), | ||
Object::New(env), "Test", 1, 1, _ctx, | ||
[this](Napi::Env env, Reference<Napi::Value> *ctx) { | ||
_deferred->Resolve(env.Undefined()); | ||
ctx->Reset(); | ||
delete ctx; | ||
}); | ||
} | ||
|
||
} // namespace | ||
|
||
Object InitThreadSafeFunctionCtx(Env env) { | ||
return TSFNWrap::Init(env, Object::New(env)); | ||
} | ||
|
||
#endif |
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,16 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const buildType = process.config.target_defaults.default_configuration; | ||
|
||
module.exports = Promise.all[ | ||
test(require(`../build/${buildType}/binding.node`)), | ||
test(require(`../build/${buildType}/binding_noexcept.node`)) | ||
]; | ||
|
||
async function test(binding) { | ||
const ctx = { }; | ||
const tsfn = new binding.threadsafe_function_ctx.TSFNWrap(ctx); | ||
assert(tsfn.getContext() === ctx); | ||
await tsfn.release(); | ||
} |