Skip to content

Commit

Permalink
Reset unique_ptr when failed at constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
chokobole committed Dec 25, 2019
1 parent 5ff9327 commit bb67f25
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion examples/calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ void CalculatorJs::Init(Napi::Env env, Napi::Object exports) {

CalculatorJs::CalculatorJs(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<CalculatorJs>(info) {
Napi::Env env = info.Env();
if (info.Length() == 0) {
calculator_ = std::unique_ptr<Calculator>(
TypedConstruct(info, &Constructor<Calculator>::CallNew<>));
} else if (info.Length() == 1) {
calculator_ = std::unique_ptr<Calculator>(
TypedConstruct(info, &Constructor<Calculator>::CallNew<int>));
} else {
Napi::Env env = info.Env();
THROW_JS_WRONG_NUMBER_OF_ARGUMENTS(env);
}

if (env.IsExceptionPending()) calculator_.reset();
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
Expand Down

0 comments on commit bb67f25

Please sign in to comment.