forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 0
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()`. Fixes: nodejs#654
- Loading branch information
Gabriel Schulhof
committed
Feb 3, 2020
1 parent
4d81618
commit 89aa6b6
Showing
7 changed files
with
110 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,34 @@ | ||
#include <stdio.h> | ||
#define NAPI_EXPERIMENTAL | ||
#include "napi.h" | ||
|
||
class Addon { | ||
public: | ||
Addon() {} | ||
bool verbose = false; | ||
~Addon() { | ||
if (verbose) { | ||
fprintf(stderr, "addon_data: Addon::~Addon\n"); | ||
} | ||
} | ||
}; | ||
|
||
static Napi::Value Getter(const Napi::CallbackInfo& info) { | ||
return Napi::Boolean::New(info.Env(), | ||
info.Env().GetInstanceData<Addon>()->verbose); | ||
} | ||
|
||
static void Setter(const Napi::CallbackInfo& info) { | ||
info.Env().GetInstanceData<Addon>()->verbose = info[0].As<Napi::Boolean>(); | ||
} | ||
|
||
Napi::Object InitAddonData(Napi::Env env) { | ||
env.SetInstanceData(new Addon()); | ||
Napi::Object result = Napi::Object::New(env); | ||
|
||
result.DefineProperties({ | ||
Napi::PropertyDescriptor::Accessor<Getter, Setter>("verbose"), | ||
}); | ||
|
||
return result; | ||
} |
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,35 @@ | ||
'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`)); | ||
|
||
function test(bindingName) { | ||
const binding = require(bindingName); | ||
|
||
// Make sure it is possible to get/set instance data. | ||
assert.strictEqual(binding.addon_data.verbose, false); | ||
binding.addon_data.verbose = true; | ||
assert.strictEqual(binding.addon_data.verbose, true); | ||
binding.addon_data.verbose = false; | ||
assert.strictEqual(binding.addon_data.verbose, false); | ||
|
||
// Make sure the instance data finalizer is called at process exit. | ||
const child = spawn(process.execPath, [ | ||
'-e', | ||
`require('${bindingName}').addon_data.verbose = true;` | ||
]); | ||
let foundMessage = false; | ||
readline | ||
.createInterface({ input: child.stderr }) | ||
.on('line', (line) => { | ||
if (line.match('addon_data: Addon::~Addon')) { | ||
foundMessage = true; | ||
} | ||
}) | ||
.on('close', () => assert.strictEqual(foundMessage, true)); | ||
} |
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