-
Notifications
You must be signed in to change notification settings - Fork 462
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. Fixes: #599
- Loading branch information
Gabriel Schulhof
committed
Nov 15, 2019
1 parent
df75e08
commit f9a8178
Showing
6 changed files
with
73 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
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,19 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
const test = (binding) => { | ||
const { ConstructorExceptionTest } = binding.objectwrapConstructorException; | ||
let gotException = false; | ||
try { | ||
new ConstructorExceptionTest(); | ||
} catch (anException) { | ||
gotException = true; | ||
} | ||
global.gc(); | ||
|
||
assert.strictEqual(gotException, true); | ||
} | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); |