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.
Merge branch 'add-napi_remove_wrap' of github.com:addaleax/node-api
Fix crashes when the ctor of an ObjectWrap subclass throws. See nodejs#475
- Loading branch information
Showing
7 changed files
with
84 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <napi.h> | ||
#include <assert.h> | ||
|
||
#ifdef NAPI_CPP_EXCEPTIONS | ||
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) { | ||
throw Napi::Error::New(Env(), "Some error"); | ||
} | ||
|
||
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_ounter_; | ||
}; | ||
|
||
} // anonymous namespace | ||
#endif // NAPI_CPP_EXCEPTIONS | ||
|
||
Napi::Object InitObjectWrapRemoveWrap(Napi::Env env) { | ||
Napi::Object exports = Napi::Object::New(env); | ||
#ifdef NAPI_CPP_EXCEPTIONS | ||
Test::Initialize(env, exports); | ||
#endif | ||
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,17 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
const test = (binding) => { | ||
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. | ||
} | ||
|
||
test(require(`./build/${buildType}/binding.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