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.
objectwrap: gracefully handle constructor exceptions
Ensure that no native instance pointer is associated with the JavaScript object under construction if the native constructor causes a JavaScript exception to be thrown. Two different cases must be taken into consideration: 1. The exception in the constructor was caused by `ObjectWrap<T>::ObjectWrap` when the call to `napi_wrap()` failed. 2. The exception in the constructor was caused by the constructor of the subclass of `ObjectWrap<T>` after `napi_wrap()` was already successful. Fixes: nodejs#599 Co-authored-by: blagoev <lubo@blagoev.com> PR-URL: nodejs#600 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
- Loading branch information
Showing
7 changed files
with
99 additions
and
5 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,26 @@ | ||
#include <napi.h> | ||
|
||
class ConstructorExceptionTest : | ||
public Napi::ObjectWrap<ConstructorExceptionTest> { | ||
public: | ||
ConstructorExceptionTest(const Napi::CallbackInfo& info) : | ||
Napi::ObjectWrap<ConstructorExceptionTest>(info) { | ||
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`)); |