Skip to content

Commit efa8a2c

Browse files
committed
src: return Maybe<> on pending exception when cpp exception disabled
1 parent b6f5eb1 commit efa8a2c

23 files changed

+939
-418
lines changed

napi-inl.h

Lines changed: 373 additions & 162 deletions
Large diffs are not rendered by default.

napi.h

Lines changed: 230 additions & 128 deletions
Large diffs are not rendered by default.

test/addon_data.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if (NAPI_VERSION > 5)
22
#include <stdio.h>
33
#include "napi.h"
4+
#include "test_helper.h"
45

56
// An overly elaborate way to get/set a boolean stored in the instance data:
67
// 0. A boolean named "verbose" is stored in the instance data. The constructor
@@ -42,7 +43,8 @@ class Addon {
4243
};
4344

4445
static Napi::Value Getter(const Napi::CallbackInfo& info) {
45-
return info.Env().GetInstanceData<Addon>()->VerboseIndicator.New({});
46+
return MaybeToChecked(
47+
info.Env().GetInstanceData<Addon>()->VerboseIndicator.New({}));
4648
}
4749

4850
static void Setter(const Napi::CallbackInfo& info) {

test/basic_types/value.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "napi.h"
2+
#include "test_helper.h"
23

34
using namespace Napi;
45

@@ -75,19 +76,19 @@ static Value IsExternal(const CallbackInfo& info) {
7576
}
7677

7778
static Value ToBoolean(const CallbackInfo& info) {
78-
return info[0].ToBoolean();
79+
return FromMaybe(info[0].ToBoolean());
7980
}
8081

8182
static Value ToNumber(const CallbackInfo& info) {
82-
return info[0].ToNumber();
83+
return FromMaybe(info[0].ToNumber());
8384
}
8485

8586
static Value ToString(const CallbackInfo& info) {
86-
return info[0].ToString();
87+
return FromMaybe(info[0].ToString());
8788
}
8889

8990
static Value ToObject(const CallbackInfo& info) {
90-
return info[0].ToObject();
91+
return FromMaybe(info[0].ToObject());
9192
}
9293

9394
Object InitBasicTypesValue(Env env) {

test/bigint.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#define NAPI_EXPERIMENTAL
44
#include "napi.h"
55

6+
#include "test_helper.h"
7+
68
using namespace Napi;
79

810
namespace {
@@ -11,7 +13,7 @@ Value IsLossless(const CallbackInfo& info) {
1113
Env env = info.Env();
1214

1315
BigInt big = info[0].As<BigInt>();
14-
bool is_signed = info[1].ToBoolean().Value();
16+
bool is_signed = MaybeToChecked(info[1].ToBoolean()).Value();
1517

1618
bool lossless;
1719
if (is_signed) {

test/binding.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ Object InitThunkingManual(Env env);
7171
Object InitObjectFreezeSeal(Env env);
7272
#endif
7373

74+
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
75+
Object InitMaybeCheck(Env env);
76+
#endif
77+
7478
Object Init(Env env, Object exports) {
7579
#if (NAPI_VERSION > 5)
7680
exports.Set("addon", InitAddon(env));
@@ -147,6 +151,10 @@ Object Init(Env env, Object exports) {
147151
#if (NAPI_VERSION > 7)
148152
exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
149153
#endif
154+
155+
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
156+
exports.Set("maybe_check", InitMaybeCheck(env));
157+
#endif
150158
return exports;
151159
}
152160

test/binding.gyp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
'target_defaults': {
33
'includes': ['../common.gypi'],
4+
'include_dirs': ['./common'],
45
'sources': [
56
'addon.cc',
67
'addon_data.cc',
@@ -25,6 +26,7 @@
2526
'external.cc',
2627
'function.cc',
2728
'handlescope.cc',
29+
'maybe/check.cc',
2830
'movable_callbacks.cc',
2931
'memory_management.cc',
3032
'name.cc',
@@ -81,5 +83,10 @@
8183
'target_name': 'binding_noexcept',
8284
'includes': ['../noexcept.gypi']
8385
},
86+
{
87+
'target_name': 'binding_noexcept_maybe',
88+
'includes': ['../noexcept.gypi'],
89+
'defines': ['NODE_ADDON_API_ENABLE_MAYBE']
90+
},
8491
],
8592
}

test/common/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ exports.runTest = async function(test, buildType) {
8181
const bindings = [
8282
`../build/${buildType}/binding.node`,
8383
`../build/${buildType}/binding_noexcept.node`,
84+
`../build/${buildType}/binding_noexcept_maybe.node`,
8485
].map(it => require.resolve(it));
8586

8687
for (const item of bindings) {
@@ -95,6 +96,7 @@ exports.runTestWithBindingPath = async function(test, buildType) {
9596
const bindings = [
9697
`../build/${buildType}/binding.node`,
9798
`../build/${buildType}/binding_noexcept.node`,
99+
`../build/${buildType}/binding_noexcept_maybe.node`,
98100
].map(it => require.resolve(it));
99101

100102
for (const item of bindings) {

test/common/test_helper.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
#include "napi.h"
3+
4+
namespace Napi {
5+
6+
// Use this when a variable or parameter is unused in order to explicitly
7+
// silence a compiler warning about that.
8+
template <typename T>
9+
inline void USE(T&&) {}
10+
11+
/**
12+
* A test helper that converts MaybeOrValue<T> to T by checking that
13+
* MaybeOrValue is NOT an empty Maybe when NODE_ADDON_API_ENABLE_MAYBE is
14+
* defined.
15+
*
16+
* Do nothing when NODE_ADDON_API_ENABLE_MAYBE is not defined.
17+
*/
18+
template <typename T>
19+
inline T MaybeToChecked(MaybeOrValue<T> maybe) {
20+
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
21+
return maybe.ToChecked();
22+
#else
23+
return maybe;
24+
#endif
25+
}
26+
27+
/**
28+
* A test helper that converts MaybeOrValue<T> to T by getting the value that
29+
* wrapped by the Maybe or return the default_value if the Maybe is empty when
30+
* NODE_ADDON_API_ENABLE_MAYBE is defined.
31+
*
32+
* Do nothing when NODE_ADDON_API_ENABLE_MAYBE is not defined.
33+
*/
34+
template <typename T>
35+
inline T FromMaybe(MaybeOrValue<T> maybe, const T& default_value = T()) {
36+
#if defined(NODE_ADDON_API_ENABLE_MAYBE)
37+
return maybe.FromMaybe(default_value);
38+
#else
39+
USE(default_value);
40+
return maybe;
41+
#endif
42+
}
43+
44+
} // namespace Napi

test/function.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "napi.h"
2+
#include "test_helper.h"
23

34
using namespace Napi;
45

@@ -53,8 +54,8 @@ Value ValueCallbackWithData(const CallbackInfo& info) {
5354

5455
Value CallWithArgs(const CallbackInfo& info) {
5556
Function func = info[0].As<Function>();
56-
return func.Call(
57-
std::initializer_list<napi_value>{info[1], info[2], info[3]});
57+
return FromMaybe(
58+
func.Call(std::initializer_list<napi_value>{info[1], info[2], info[3]}));
5859
}
5960

6061
Value CallWithVector(const CallbackInfo& info) {
@@ -64,7 +65,7 @@ Value CallWithVector(const CallbackInfo& info) {
6465
args.push_back(info[1]);
6566
args.push_back(info[2]);
6667
args.push_back(info[3]);
67-
return func.Call(args);
68+
return FromMaybe(func.Call(args));
6869
}
6970

7071
Value CallWithCStyleArray(const CallbackInfo& info) {
@@ -74,7 +75,7 @@ Value CallWithCStyleArray(const CallbackInfo& info) {
7475
args.push_back(info[1]);
7576
args.push_back(info[2]);
7677
args.push_back(info[3]);
77-
return func.Call(args.size(), args.data());
78+
return FromMaybe(func.Call(args.size(), args.data()));
7879
}
7980

8081
Value CallWithReceiverAndCStyleArray(const CallbackInfo& info) {
@@ -85,13 +86,14 @@ Value CallWithReceiverAndCStyleArray(const CallbackInfo& info) {
8586
args.push_back(info[2]);
8687
args.push_back(info[3]);
8788
args.push_back(info[4]);
88-
return func.Call(receiver, args.size(), args.data());
89+
return FromMaybe(func.Call(receiver, args.size(), args.data()));
8990
}
9091

9192
Value CallWithReceiverAndArgs(const CallbackInfo& info) {
9293
Function func = info[0].As<Function>();
9394
Value receiver = info[1];
94-
return func.Call(receiver, std::initializer_list<napi_value>{ info[2], info[3], info[4] });
95+
return FromMaybe(func.Call(
96+
receiver, std::initializer_list<napi_value>{info[2], info[3], info[4]}));
9597
}
9698

9799
Value CallWithReceiverAndVector(const CallbackInfo& info) {
@@ -102,17 +104,18 @@ Value CallWithReceiverAndVector(const CallbackInfo& info) {
102104
args.push_back(info[2]);
103105
args.push_back(info[3]);
104106
args.push_back(info[4]);
105-
return func.Call(receiver, args);
107+
return FromMaybe(func.Call(receiver, args));
106108
}
107109

108110
Value CallWithInvalidReceiver(const CallbackInfo& info) {
109111
Function func = info[0].As<Function>();
110-
return func.Call(Value(), std::initializer_list<napi_value>{});
112+
return FromMaybe(func.Call(Value(), std::initializer_list<napi_value>{}));
111113
}
112114

113115
Value CallConstructorWithArgs(const CallbackInfo& info) {
114116
Function func = info[0].As<Function>();
115-
return func.New(std::initializer_list<napi_value>{ info[1], info[2], info[3] });
117+
return FromMaybe(
118+
func.New(std::initializer_list<napi_value>{info[1], info[2], info[3]}));
116119
}
117120

118121
Value CallConstructorWithVector(const CallbackInfo& info) {
@@ -122,7 +125,7 @@ Value CallConstructorWithVector(const CallbackInfo& info) {
122125
args.push_back(info[1]);
123126
args.push_back(info[2]);
124127
args.push_back(info[3]);
125-
return func.New(args);
128+
return FromMaybe(func.New(args));
126129
}
127130

128131
Value CallConstructorWithCStyleArray(const CallbackInfo& info) {
@@ -132,7 +135,7 @@ Value CallConstructorWithCStyleArray(const CallbackInfo& info) {
132135
args.push_back(info[1]);
133136
args.push_back(info[2]);
134137
args.push_back(info[3]);
135-
return func.New(args.size(), args.data());
138+
return FromMaybe(func.New(args.size(), args.data()));
136139
}
137140

138141
void IsConstructCall(const CallbackInfo& info) {
@@ -191,7 +194,7 @@ void MakeCallbackWithInvalidReceiver(const CallbackInfo& info) {
191194

192195
Value CallWithFunctionOperator(const CallbackInfo& info) {
193196
Function func = info[0].As<Function>();
194-
return func({info[1], info[2], info[3]});
197+
return FromMaybe(func({info[1], info[2], info[3]}));
195198
}
196199

197200
} // end anonymous namespace

0 commit comments

Comments
 (0)