Skip to content

Commit

Permalink
fixup! remove unnecessary MaybeUnwrapOr
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Jun 18, 2021
1 parent 6012787 commit baf6ab8
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 115 deletions.
16 changes: 0 additions & 16 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ To properly test these build flavors, all values returned by a function defined
with `Napi::MaybeOrValue<>` return types in node-addon-api test suite, should
use one of the following test helpers to handle possible JavaScript exceptions.

```cpp
#include "napi.h"
#include "test_helper.h"

using namespace Napi;

void fn(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Value value = MaybeUnwrap(obj->Get("foobar")); // <- `obj->Get` is calling
// into JavaScript and may throw JavaScript Exceptions. Here we just assert
// getting the parameter must not fail for convenience.

// ... do works with the value.
}
```
There are three test helper functions to conveniently convert `Napi::MaybeOrValue<>`
type to raw types.

Expand Down
8 changes: 4 additions & 4 deletions test/basic_types/value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ static Value IsExternal(const CallbackInfo& info) {
}

static Value ToBoolean(const CallbackInfo& info) {
return MaybeUnwrapOr(info[0].ToBoolean());
return MaybeUnwrap(info[0].ToBoolean());
}

static Value ToNumber(const CallbackInfo& info) {
return MaybeUnwrapOr(info[0].ToNumber());
return MaybeUnwrap(info[0].ToNumber());
}

static Value ToString(const CallbackInfo& info) {
return MaybeUnwrapOr(info[0].ToString());
return MaybeUnwrap(info[0].ToString());
}

static Value ToObject(const CallbackInfo& info) {
return MaybeUnwrapOr(info[0].ToObject());
return MaybeUnwrap(info[0].ToObject());
}

Object InitBasicTypesValue(Env env) {
Expand Down
2 changes: 1 addition & 1 deletion test/common/test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ inline T MaybeUnwrap(MaybeOrValue<T> maybe) {
* Do nothing when NODE_ADDON_API_ENABLE_MAYBE is not defined.
*/
template <typename T>
inline T MaybeUnwrapOr(MaybeOrValue<T> maybe, const T& default_value = T()) {
inline T MaybeUnwrapOr(MaybeOrValue<T> maybe, const T& default_value) {
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
return maybe.UnwrapOr(default_value);
#else
Expand Down
24 changes: 12 additions & 12 deletions test/function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Value ValueCallbackWithData(const CallbackInfo& info) {

Value CallWithArgs(const CallbackInfo& info) {
Function func = info[0].As<Function>();
return MaybeUnwrapOr(
return MaybeUnwrap(
func.Call(std::initializer_list<napi_value>{info[1], info[2], info[3]}));
}

Expand All @@ -65,7 +65,7 @@ Value CallWithVector(const CallbackInfo& info) {
args.push_back(info[1]);
args.push_back(info[2]);
args.push_back(info[3]);
return MaybeUnwrapOr(func.Call(args));
return MaybeUnwrap(func.Call(args));
}

Value CallWithCStyleArray(const CallbackInfo& info) {
Expand All @@ -75,7 +75,7 @@ Value CallWithCStyleArray(const CallbackInfo& info) {
args.push_back(info[1]);
args.push_back(info[2]);
args.push_back(info[3]);
return MaybeUnwrapOr(func.Call(args.size(), args.data()));
return MaybeUnwrap(func.Call(args.size(), args.data()));
}

Value CallWithReceiverAndCStyleArray(const CallbackInfo& info) {
Expand All @@ -86,13 +86,13 @@ Value CallWithReceiverAndCStyleArray(const CallbackInfo& info) {
args.push_back(info[2]);
args.push_back(info[3]);
args.push_back(info[4]);
return MaybeUnwrapOr(func.Call(receiver, args.size(), args.data()));
return MaybeUnwrap(func.Call(receiver, args.size(), args.data()));
}

Value CallWithReceiverAndArgs(const CallbackInfo& info) {
Function func = info[0].As<Function>();
Value receiver = info[1];
return MaybeUnwrapOr(func.Call(
return MaybeUnwrap(func.Call(
receiver, std::initializer_list<napi_value>{info[2], info[3], info[4]}));
}

Expand All @@ -104,18 +104,18 @@ Value CallWithReceiverAndVector(const CallbackInfo& info) {
args.push_back(info[2]);
args.push_back(info[3]);
args.push_back(info[4]);
return MaybeUnwrapOr(func.Call(receiver, args));
return MaybeUnwrap(func.Call(receiver, args));
}

Value CallWithInvalidReceiver(const CallbackInfo& info) {
Function func = info[0].As<Function>();
return MaybeUnwrapOr(
func.Call(Value(), std::initializer_list<napi_value>{}));
return MaybeUnwrapOr(func.Call(Value(), std::initializer_list<napi_value>{}),
Value());
}

Value CallConstructorWithArgs(const CallbackInfo& info) {
Function func = info[0].As<Function>();
return MaybeUnwrapOr(
return MaybeUnwrap(
func.New(std::initializer_list<napi_value>{info[1], info[2], info[3]}));
}

Expand All @@ -126,7 +126,7 @@ Value CallConstructorWithVector(const CallbackInfo& info) {
args.push_back(info[1]);
args.push_back(info[2]);
args.push_back(info[3]);
return MaybeUnwrapOr(func.New(args));
return MaybeUnwrap(func.New(args));
}

Value CallConstructorWithCStyleArray(const CallbackInfo& info) {
Expand All @@ -136,7 +136,7 @@ Value CallConstructorWithCStyleArray(const CallbackInfo& info) {
args.push_back(info[1]);
args.push_back(info[2]);
args.push_back(info[3]);
return MaybeUnwrapOr(func.New(args.size(), args.data()));
return MaybeUnwrap(func.New(args.size(), args.data()));
}

void IsConstructCall(const CallbackInfo& info) {
Expand Down Expand Up @@ -195,7 +195,7 @@ void MakeCallbackWithInvalidReceiver(const CallbackInfo& info) {

Value CallWithFunctionOperator(const CallbackInfo& info) {
Function func = info[0].As<Function>();
return MaybeUnwrapOr(func({info[1], info[2], info[3]}));
return MaybeUnwrap(func({info[1], info[2], info[3]}));
}

} // end anonymous namespace
Expand Down
4 changes: 2 additions & 2 deletions test/functionreference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Value Call(const CallbackInfo& info) {
FunctionReference ref;
ref.Reset(info[0].As<Function>());

return MaybeUnwrapOr(ref.Call({}));
return MaybeUnwrapOr(ref.Call({}), Value());
}

Value Construct(const CallbackInfo& info) {
HandleScope scope(info.Env());
FunctionReference ref;
ref.Reset(info[0].As<Function>());

return MaybeUnwrapOr(ref.New({}));
return MaybeUnwrapOr(ref.New({}), Object());
}
} // namespace

Expand Down
8 changes: 4 additions & 4 deletions test/globalObject/global_object_delete_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@ Value DeletePropertyWithCStyleStringAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
String key = info[0].As<String>();
return Boolean::New(
info.Env(), MaybeUnwrapOr(globalObject.Delete(key.Utf8Value().c_str())));
info.Env(), MaybeUnwrap(globalObject.Delete(key.Utf8Value().c_str())));
}

Value DeletePropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
String key = info[0].As<String>();
return Boolean::New(info.Env(),
MaybeUnwrapOr(globalObject.Delete(key.Utf8Value())));
MaybeUnwrap(globalObject.Delete(key.Utf8Value())));
}

Value DeletePropertyWithInt32AsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
Number key = info[0].As<Number>();
return Boolean::New(info.Env(),
MaybeUnwrapOr(globalObject.Delete(key.Uint32Value())));
MaybeUnwrap(globalObject.Delete(key.Uint32Value())));
}

Value DeletePropertyWithNapiValueAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
Name key = info[0].As<Name>();
return Boolean::New(info.Env(), MaybeUnwrapOr(globalObject.Delete(key)));
return Boolean::New(info.Env(), MaybeUnwrap(globalObject.Delete(key)));
}
8 changes: 4 additions & 4 deletions test/globalObject/global_object_get_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ using namespace Napi;
Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
Name key = info[0].As<Name>();
return MaybeUnwrapOr(globalObject.Get(key));
return MaybeUnwrap(globalObject.Get(key));
}

Value GetPropertyWithInt32AsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
Number key = info[0].As<Napi::Number>();
return MaybeUnwrapOr(globalObject.Get(key.Uint32Value()));
return MaybeUnwrapOr(globalObject.Get(key.Uint32Value()), Value());
}

Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
String cStrkey = info[0].As<String>();
return MaybeUnwrapOr(globalObject.Get(cStrkey.Utf8Value().c_str()));
return MaybeUnwrapOr(globalObject.Get(cStrkey.Utf8Value().c_str()), Value());
}

Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
String cppStrKey = info[0].As<String>();
return MaybeUnwrapOr(globalObject.Get(cppStrKey.Utf8Value()));
return MaybeUnwrapOr(globalObject.Get(cppStrKey.Utf8Value()), Value());
}

void CreateMockTestObject(const CallbackInfo& info) {
Expand Down
8 changes: 5 additions & 3 deletions test/globalObject/global_object_has_own_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ Value HasPropertyWithCStyleStringAsKey(const CallbackInfo& info) {
String key = info[0].As<String>();
return Boolean::New(
info.Env(),
MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value().c_str())));
MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value().c_str()),
false));
}

Value HasPropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
String key = info[0].As<String>();
return Boolean::New(
info.Env(), MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value())));
info.Env(),
MaybeUnwrapOr(globalObject.HasOwnProperty(key.Utf8Value()), false));
}

Value HasPropertyWithNapiValueAsKey(const CallbackInfo& info) {
Object globalObject = info.Env().Global();
Name key = info[0].As<Name>();
return Boolean::New(info.Env(),
MaybeUnwrapOr(globalObject.HasOwnProperty(key)));
MaybeUnwrap(globalObject.HasOwnProperty(key)));
}
16 changes: 9 additions & 7 deletions test/object/delete_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@ using namespace Napi;
Value DeletePropertyWithUint32(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Number key = info[1].As<Number>();
return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(key.Uint32Value())));
return Boolean::New(info.Env(), MaybeUnwrap(obj.Delete(key.Uint32Value())));
}

Value DeletePropertyWithNapiValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return Boolean::New(info.Env(),
MaybeUnwrapOr(obj.Delete(static_cast<napi_value>(key))));
return Boolean::New(
info.Env(),
MaybeUnwrapOr(obj.Delete(static_cast<napi_value>(key)), false));
}

Value DeletePropertyWithNapiWrapperValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(key)));
return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(key), false));
}

Value DeletePropertyWithCStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return Boolean::New(info.Env(),
MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value().c_str())));
return Boolean::New(
info.Env(), MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value().c_str()), false));
}

Value DeletePropertyWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return Boolean::New(info.Env(), MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value())));
return Boolean::New(info.Env(),
MaybeUnwrapOr(obj.Delete(jsKey.Utf8Value()), false));
}
10 changes: 5 additions & 5 deletions test/object/get_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ using namespace Napi;
Value GetPropertyWithNapiValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return MaybeUnwrapOr(obj.Get(static_cast<napi_value>(key)));
return MaybeUnwrapOr(obj.Get(static_cast<napi_value>(key)), Value());
}

Value GetPropertyWithNapiWrapperValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return MaybeUnwrapOr(obj.Get(key));
return MaybeUnwrapOr(obj.Get(key), Value());
}

Value GetPropertyWithUint32(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Number key = info[1].As<Number>();
return MaybeUnwrapOr(obj.Get(key.Uint32Value()));
return MaybeUnwrap(obj.Get(key.Uint32Value()));
}

Value GetPropertyWithCStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value().c_str()));
return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value().c_str()), Value());
}

Value GetPropertyWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value()));
return MaybeUnwrapOr(obj.Get(jsKey.Utf8Value()), Value());
}
12 changes: 7 additions & 5 deletions test/object/has_own_property.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ Value HasOwnPropertyWithNapiValue(const CallbackInfo& info) {
Name key = info[1].As<Name>();
return Boolean::New(
info.Env(),
MaybeUnwrapOr(obj.HasOwnProperty(static_cast<napi_value>(key))));
MaybeUnwrapOr(obj.HasOwnProperty(static_cast<napi_value>(key)), false));
}

Value HasOwnPropertyWithNapiWrapperValue(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
Name key = info[1].As<Name>();
return Boolean::New(info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(key)));
return Boolean::New(info.Env(),
MaybeUnwrapOr(obj.HasOwnProperty(key), false));
}

Value HasOwnPropertyWithCStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return Boolean::New(
info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(jsKey.Utf8Value().c_str())));
info.Env(),
MaybeUnwrapOr(obj.HasOwnProperty(jsKey.Utf8Value().c_str()), false));
}

Value HasOwnPropertyWithCppStyleString(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
String jsKey = info[1].As<String>();
return Boolean::New(info.Env(),
MaybeUnwrapOr(obj.HasOwnProperty(jsKey.Utf8Value())));
return Boolean::New(
info.Env(), MaybeUnwrapOr(obj.HasOwnProperty(jsKey.Utf8Value()), false));
}
Loading

0 comments on commit baf6ab8

Please sign in to comment.