-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #723 from gabrielschulhof/backport-4e885069-pr-475
[v2.x] Backport ObjectWrap reference-related changes
- Loading branch information
Showing
11 changed files
with
193 additions
and
15 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
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 <assert.h> | ||
|
||
namespace { | ||
|
||
static int dtor_called = 0; | ||
|
||
class DtorCounter { | ||
public: | ||
~DtorCounter() { | ||
assert(dtor_called == 0); | ||
dtor_called++; | ||
} | ||
}; | ||
|
||
Napi::Value GetDtorCalled(const Napi::CallbackInfo& info) { | ||
return Napi::Number::New(info.Env(), dtor_called); | ||
} | ||
|
||
class Test : public Napi::ObjectWrap<Test> { | ||
public: | ||
Test(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Test>(info) { | ||
#ifdef NAPI_CPP_EXCEPTIONS | ||
throw Napi::Error::New(Env(), "Some error"); | ||
#else | ||
Napi::Error::New(Env(), "Some error").ThrowAsJavaScriptException(); | ||
#endif | ||
} | ||
|
||
static void Initialize(Napi::Env env, Napi::Object exports) { | ||
exports.Set("Test", DefineClass(env, "Test", {})); | ||
exports.Set("getDtorCalled", Napi::Function::New(env, GetDtorCalled)); | ||
} | ||
|
||
private: | ||
DtorCounter dtor_counter_; | ||
}; | ||
|
||
} // anonymous namespace | ||
|
||
Napi::Object InitObjectWrapRemoveWrap(Napi::Env env) { | ||
Napi::Object exports = Napi::Object::New(env); | ||
Test::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,35 @@ | ||
'use strict'; | ||
|
||
if (process.argv[2] === 'child') { | ||
// Create a single wrapped instance then exit. | ||
return new (require(process.argv[3]).objectwrap.Test)(); | ||
} | ||
|
||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
const { spawnSync } = require('child_process'); | ||
|
||
const test = (bindingName) => { | ||
const binding = require(bindingName); | ||
const Test = binding.objectwrap_removewrap.Test; | ||
const getDtorCalled = binding.objectwrap_removewrap.getDtorCalled; | ||
|
||
assert.strictEqual(getDtorCalled(), 0); | ||
assert.throws(() => { | ||
new Test(); | ||
}); | ||
assert.strictEqual(getDtorCalled(), 1); | ||
global.gc(); // Does not crash. | ||
|
||
// Start a child process that creates a single wrapped instance to ensure that | ||
// it is properly freed at its exit. It must not segfault. | ||
// Re: https://github.com/nodejs/node-addon-api/issues/660 | ||
const child = spawnSync(process.execPath, [ | ||
__filename, 'child', bindingName | ||
]); | ||
assert.strictEqual(child.signal, null); | ||
assert.strictEqual(child.status, 0); | ||
} | ||
|
||
test(`./build/${buildType}/binding.node`); | ||
test(`./build/${buildType}/binding_noexcept.node`); |
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,27 @@ | ||
#include <napi.h> | ||
|
||
class ConstructorExceptionTest : | ||
public Napi::ObjectWrap<ConstructorExceptionTest> { | ||
public: | ||
ConstructorExceptionTest(const Napi::CallbackInfo& info) : | ||
Napi::ObjectWrap<ConstructorExceptionTest>(info) { | ||
fprintf(stderr, "That was the interesting reference\n"); | ||
Napi::Error error = Napi::Error::New(info.Env(), "an exception"); | ||
#ifdef NAPI_DISABLE_CPP_EXCEPTIONS | ||
error.ThrowAsJavaScriptException(); | ||
#else | ||
throw error; | ||
#endif // NAPI_DISABLE_CPP_EXCEPTIONS | ||
} | ||
|
||
static void Initialize(Napi::Env env, Napi::Object exports) { | ||
const char* name = "ConstructorExceptionTest"; | ||
exports.Set(name, DefineClass(env, name, {})); | ||
} | ||
}; | ||
|
||
Napi::Object InitObjectWrapConstructorException(Napi::Env env) { | ||
Napi::Object exports = Napi::Object::New(env); | ||
ConstructorExceptionTest::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,12 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
const test = (binding) => { | ||
const { ConstructorExceptionTest } = binding.objectwrapConstructorException; | ||
assert.throws(() => (new ConstructorExceptionTest()), /an exception/); | ||
global.gc(); | ||
} | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); |