-
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 support for addon instance data
Support `napi_get_instance_data()` and `napi_set_instance_data()`. Signed-off-by: Gabriel Schulhof <[email protected]> Fixes: #654 PR-URL: #663 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
Gabriel Schulhof
committed
Apr 25, 2020
1 parent
82a9650
commit 9c9accf
Showing
7 changed files
with
212 additions
and
0 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,97 @@ | ||
#if (NAPI_VERSION > 5) | ||
#include <stdio.h> | ||
#include "napi.h" | ||
|
||
// An overly elaborate way to get/set a boolean stored in the instance data: | ||
// 0. A boolean named "verbose" is stored in the instance data. The constructor | ||
// for JS `VerboseIndicator` instances is also stored in the instance data. | ||
// 1. Add a property named "verbose" onto exports served by a getter/setter. | ||
// 2. The getter returns a object of type VerboseIndicator, which itself has a | ||
// property named "verbose", also served by a getter/setter: | ||
// * The getter returns a boolean, indicating whether "verbose" is set. | ||
// * The setter sets "verbose" on the instance data. | ||
// 3. The setter sets "verbose" on the instance data. | ||
|
||
class Addon { | ||
public: | ||
class VerboseIndicator : public Napi::ObjectWrap<VerboseIndicator> { | ||
public: | ||
VerboseIndicator(const Napi::CallbackInfo& info): | ||
Napi::ObjectWrap<VerboseIndicator>(info) { | ||
info.This().As<Napi::Object>()["verbose"] = | ||
Napi::Boolean::New(info.Env(), | ||
info.Env().GetInstanceData<Addon>()->verbose); | ||
} | ||
|
||
Napi::Value Getter(const Napi::CallbackInfo& info) { | ||
return Napi::Boolean::New(info.Env(), | ||
info.Env().GetInstanceData<Addon>()->verbose); | ||
} | ||
|
||
void Setter(const Napi::CallbackInfo& info, const Napi::Value& val) { | ||
info.Env().GetInstanceData<Addon>()->verbose = val.As<Napi::Boolean>(); | ||
} | ||
|
||
static Napi::FunctionReference Init(Napi::Env env) { | ||
return Napi::Persistent(DefineClass(env, "VerboseIndicator", { | ||
InstanceAccessor< | ||
&VerboseIndicator::Getter, | ||
&VerboseIndicator::Setter>("verbose") | ||
})); | ||
} | ||
}; | ||
|
||
static Napi::Value Getter(const Napi::CallbackInfo& info) { | ||
return info.Env().GetInstanceData<Addon>()->VerboseIndicator.New({}); | ||
} | ||
|
||
static void Setter(const Napi::CallbackInfo& info) { | ||
info.Env().GetInstanceData<Addon>()->verbose = info[0].As<Napi::Boolean>(); | ||
} | ||
|
||
Addon(Napi::Env env): VerboseIndicator(VerboseIndicator::Init(env)) {} | ||
~Addon() { | ||
if (verbose) { | ||
fprintf(stderr, "addon_data: Addon::~Addon\n"); | ||
} | ||
} | ||
|
||
static void DeleteAddon(Napi::Env, Addon* addon, uint32_t* hint) { | ||
delete addon; | ||
fprintf(stderr, "hint: %d\n", *hint); | ||
delete hint; | ||
} | ||
|
||
static Napi::Object Init(Napi::Env env, Napi::Value jshint) { | ||
if (!jshint.IsNumber()) { | ||
NAPI_THROW(Napi::Error::New(env, "Expected number"), Napi::Object()); | ||
} | ||
uint32_t hint = jshint.As<Napi::Number>(); | ||
if (hint == 0) | ||
env.SetInstanceData(new Addon(env)); | ||
else | ||
env.SetInstanceData<Addon, uint32_t, DeleteAddon>(new Addon(env), | ||
new uint32_t(hint)); | ||
Napi::Object result = Napi::Object::New(env); | ||
result.DefineProperties({ | ||
Napi::PropertyDescriptor::Accessor<Getter, Setter>("verbose"), | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
private: | ||
bool verbose = false; | ||
Napi::FunctionReference VerboseIndicator; | ||
}; | ||
|
||
// We use an addon factory so we can cover both the case where there is an | ||
// instance data hint and the case where there isn't. | ||
static Napi::Value AddonFactory(const Napi::CallbackInfo& info) { | ||
return Addon::Init(info.Env(), info[0]); | ||
} | ||
|
||
Napi::Object InitAddonData(Napi::Env env) { | ||
return Napi::Function::New(env, AddonFactory); | ||
} | ||
#endif // (NAPI_VERSION > 5) |
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,42 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const { spawn } = require('child_process'); | ||
const readline = require('readline'); | ||
const path = require('path'); | ||
|
||
test(path.resolve(__dirname, `./build/${buildType}/binding.node`)); | ||
test(path.resolve(__dirname, `./build/${buildType}/binding_noexcept.node`)); | ||
|
||
// Make sure the instance data finalizer is called at process exit. If the hint | ||
// is non-zero, it will be printed out by the child process. | ||
function testFinalizer(bindingName, hint, expected) { | ||
bindingName = bindingName.split('\\').join('\\\\'); | ||
const child = spawn(process.execPath, [ | ||
'-e', | ||
`require('${bindingName}').addon_data(${hint}).verbose = true;` | ||
]); | ||
const actual = []; | ||
readline | ||
.createInterface({ input: child.stderr }) | ||
.on('line', (line) => { | ||
if (expected.indexOf(line) >= 0) { | ||
actual.push(line); | ||
} | ||
}) | ||
.on('close', () => assert.deepStrictEqual(expected, actual)); | ||
} | ||
|
||
function test(bindingName) { | ||
const binding = require(bindingName).addon_data(0); | ||
|
||
// Make sure it is possible to get/set instance data. | ||
assert.strictEqual(binding.verbose.verbose, false); | ||
binding.verbose = true; | ||
assert.strictEqual(binding.verbose.verbose, true); | ||
binding.verbose = false; | ||
assert.strictEqual(binding.verbose.verbose, false); | ||
|
||
testFinalizer(bindingName, 0, ['addon_data: Addon::~Addon']); | ||
testFinalizer(bindingName, 42, ['addon_data: Addon::~Addon', 'hint: 42']); | ||
} |
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