-
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: allow customization of ObjectWrap behavior
Add new method to allow customization of ObjectWrap behavior PR-URL: nodejs/node-addon-api#1125 Reviewed-By: Michael Dawson <midawson@redhat.com
- Loading branch information
John French
committed
Feb 17, 2022
1 parent
f1597f5
commit 8e9b5ad
Showing
7 changed files
with
106 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
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,45 @@ | ||
#include <napi.h> | ||
#include <unordered_map> | ||
#include "test_helper.h" | ||
|
||
class FunctionTest : public Napi::ObjectWrap<FunctionTest> { | ||
public: | ||
FunctionTest(const Napi::CallbackInfo& info) | ||
: Napi::ObjectWrap<FunctionTest>(info) {} | ||
|
||
static Napi::Value OnCalledAsFunction(const Napi::CallbackInfo& info) { | ||
// If called with a "true" argument, throw an exeption to test the handling. | ||
if (!info[0].IsUndefined() && MaybeUnwrap(info[0].ToBoolean())) { | ||
NAPI_THROW(Napi::Error::New(info.Env(), "an exception"), Napi::Value()); | ||
} | ||
// Otherwise, act as a factory. | ||
std::vector<napi_value> args; | ||
for (size_t i = 0; i < info.Length(); i++) args.push_back(info[i]); | ||
return MaybeUnwrap(GetConstructor(info.Env()).New(args)); | ||
} | ||
|
||
// Constructor-per-env map in a static member because env.SetInstanceData() | ||
// would interfere with Napi::Addon<T> | ||
static std::unordered_map<napi_env, Napi::FunctionReference> constructors; | ||
|
||
static void Initialize(Napi::Env env, Napi::Object exports) { | ||
const char* name = "FunctionTest"; | ||
Napi::Function func = DefineClass(env, name, {}); | ||
constructors[env] = Napi::Persistent(func); | ||
env.AddCleanupHook([env] { constructors.erase(env); }); | ||
exports.Set(name, func); | ||
} | ||
|
||
static Napi::Function GetConstructor(Napi::Env env) { | ||
return constructors[env].Value(); | ||
} | ||
}; | ||
|
||
std::unordered_map<napi_env, Napi::FunctionReference> | ||
FunctionTest::constructors = {}; | ||
|
||
Napi::Object InitObjectWrapFunction(Napi::Env env) { | ||
Napi::Object exports = Napi::Object::New(env); | ||
FunctionTest::Initialize(env, exports); | ||
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,22 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const testUtil = require('./testUtil'); | ||
|
||
function test (binding) { | ||
return testUtil.runGCTests([ | ||
'objectwrap function', | ||
() => { | ||
const { FunctionTest } = binding.objectwrap_function; | ||
const newConstructed = new FunctionTest(); | ||
const functionConstructed = FunctionTest(); | ||
assert(newConstructed instanceof FunctionTest); | ||
assert(functionConstructed instanceof FunctionTest); | ||
assert.throws(() => (FunctionTest(true)), /an exception/); | ||
}, | ||
// Do on gc before returning. | ||
() => {} | ||
]); | ||
} | ||
|
||
module.exports = require('./common').runTest(test); |