Skip to content

Commit 93f1898

Browse files
authored
src: return bool on object set and define property (#977)
If c++ exception is disabled, there is no ergonomic way to detect failtures on void returning.
1 parent 331c2ee commit 93f1898

File tree

7 files changed

+113
-104
lines changed

7 files changed

+113
-104
lines changed

doc/object.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Creates a new `Napi::Object` value.
7272
### Set()
7373

7474
```cpp
75-
void Napi::Object::Set (____ key, ____ value);
75+
bool Napi::Object::Set (____ key, ____ value);
7676
```
7777
- `[in] key`: The name for the property being assigned.
7878
- `[in] value`: The value being assigned to the property.
@@ -200,7 +200,7 @@ The key can be any of the following types:
200200
### DefineProperty()
201201
202202
```cpp
203-
void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
203+
bool Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
204204
```
205205
- `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md).
206206

@@ -209,7 +209,7 @@ Define a property on the object.
209209
### DefineProperties()
210210

211211
```cpp
212-
void Napi::Object::DefineProperties (____ properties)
212+
bool Napi::Object::DefineProperties (____ properties)
213213
```
214214
- `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types:
215215
- const std::initializer_list<Napi::PropertyDescriptor>&

doc/object_reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Returns the newly created reference.
8181
8282
### Set
8383
```cpp
84-
void Napi::ObjectReference::Set(___ key, ___ value);
84+
bool Napi::ObjectReference::Set(___ key, ___ value);
8585
```
8686

8787
* `[in] key`: The name for the property being assigned.

napi-inl.h

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,29 +1236,32 @@ inline Value Object::Get(const std::string& utf8name) const {
12361236
}
12371237

12381238
template <typename ValueType>
1239-
inline void Object::Set(napi_value key, const ValueType& value) {
1239+
inline bool Object::Set(napi_value key, const ValueType& value) {
12401240
napi_status status =
12411241
napi_set_property(_env, _value, key, Value::From(_env, value));
1242-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1242+
NAPI_THROW_IF_FAILED(_env, status, false);
1243+
return true;
12431244
}
12441245

12451246
template <typename ValueType>
1246-
inline void Object::Set(Value key, const ValueType& value) {
1247+
inline bool Object::Set(Value key, const ValueType& value) {
12471248
napi_status status =
12481249
napi_set_property(_env, _value, key, Value::From(_env, value));
1249-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1250+
NAPI_THROW_IF_FAILED(_env, status, false);
1251+
return true;
12501252
}
12511253

12521254
template <typename ValueType>
1253-
inline void Object::Set(const char* utf8name, const ValueType& value) {
1255+
inline bool Object::Set(const char* utf8name, const ValueType& value) {
12541256
napi_status status =
12551257
napi_set_named_property(_env, _value, utf8name, Value::From(_env, value));
1256-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1258+
NAPI_THROW_IF_FAILED(_env, status, false);
1259+
return true;
12571260
}
12581261

12591262
template <typename ValueType>
1260-
inline void Object::Set(const std::string& utf8name, const ValueType& value) {
1261-
Set(utf8name.c_str(), value);
1263+
inline bool Object::Set(const std::string& utf8name, const ValueType& value) {
1264+
return Set(utf8name.c_str(), value);
12621265
}
12631266

12641267
inline bool Object::Delete(napi_value key) {
@@ -1298,10 +1301,11 @@ inline Value Object::Get(uint32_t index) const {
12981301
}
12991302

13001303
template <typename ValueType>
1301-
inline void Object::Set(uint32_t index, const ValueType& value) {
1304+
inline bool Object::Set(uint32_t index, const ValueType& value) {
13021305
napi_status status =
13031306
napi_set_element(_env, _value, index, Value::From(_env, value));
1304-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1307+
NAPI_THROW_IF_FAILED(_env, status, false);
1308+
return true;
13051309
}
13061310

13071311
inline bool Object::Delete(uint32_t index) {
@@ -1318,22 +1322,27 @@ inline Array Object::GetPropertyNames() const {
13181322
return Array(_env, result);
13191323
}
13201324

1321-
inline void Object::DefineProperty(const PropertyDescriptor& property) {
1325+
inline bool Object::DefineProperty(const PropertyDescriptor& property) {
13221326
napi_status status = napi_define_properties(_env, _value, 1,
13231327
reinterpret_cast<const napi_property_descriptor*>(&property));
1324-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1328+
NAPI_THROW_IF_FAILED(_env, status, false);
1329+
return true;
13251330
}
13261331

1327-
inline void Object::DefineProperties(const std::initializer_list<PropertyDescriptor>& properties) {
1332+
inline bool Object::DefineProperties(
1333+
const std::initializer_list<PropertyDescriptor>& properties) {
13281334
napi_status status = napi_define_properties(_env, _value, properties.size(),
13291335
reinterpret_cast<const napi_property_descriptor*>(properties.begin()));
1330-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1336+
NAPI_THROW_IF_FAILED(_env, status, false);
1337+
return true;
13311338
}
13321339

1333-
inline void Object::DefineProperties(const std::vector<PropertyDescriptor>& properties) {
1340+
inline bool Object::DefineProperties(
1341+
const std::vector<PropertyDescriptor>& properties) {
13341342
napi_status status = napi_define_properties(_env, _value, properties.size(),
13351343
reinterpret_cast<const napi_property_descriptor*>(properties.data()));
1336-
NAPI_THROW_IF_FAILED_VOID(_env, status);
1344+
NAPI_THROW_IF_FAILED(_env, status, false);
1345+
return true;
13371346
}
13381347

13391348
inline bool Object::InstanceOf(const Function& constructor) const {
@@ -2678,89 +2687,93 @@ inline Napi::Value ObjectReference::Get(const std::string& utf8name) const {
26782687
return scope.Escape(Value().Get(utf8name));
26792688
}
26802689

2681-
inline void ObjectReference::Set(const char* utf8name, napi_value value) {
2690+
inline bool ObjectReference::Set(const char* utf8name, napi_value value) {
26822691
HandleScope scope(_env);
2683-
Value().Set(utf8name, value);
2692+
return Value().Set(utf8name, value);
26842693
}
26852694

2686-
inline void ObjectReference::Set(const char* utf8name, Napi::Value value) {
2695+
inline bool ObjectReference::Set(const char* utf8name, Napi::Value value) {
26872696
HandleScope scope(_env);
2688-
Value().Set(utf8name, value);
2697+
return Value().Set(utf8name, value);
26892698
}
26902699

2691-
inline void ObjectReference::Set(const char* utf8name, const char* utf8value) {
2700+
inline bool ObjectReference::Set(const char* utf8name, const char* utf8value) {
26922701
HandleScope scope(_env);
2693-
Value().Set(utf8name, utf8value);
2702+
return Value().Set(utf8name, utf8value);
26942703
}
26952704

2696-
inline void ObjectReference::Set(const char* utf8name, bool boolValue) {
2705+
inline bool ObjectReference::Set(const char* utf8name, bool boolValue) {
26972706
HandleScope scope(_env);
2698-
Value().Set(utf8name, boolValue);
2707+
return Value().Set(utf8name, boolValue);
26992708
}
27002709

2701-
inline void ObjectReference::Set(const char* utf8name, double numberValue) {
2710+
inline bool ObjectReference::Set(const char* utf8name, double numberValue) {
27022711
HandleScope scope(_env);
2703-
Value().Set(utf8name, numberValue);
2712+
return Value().Set(utf8name, numberValue);
27042713
}
27052714

2706-
inline void ObjectReference::Set(const std::string& utf8name, napi_value value) {
2715+
inline bool ObjectReference::Set(const std::string& utf8name,
2716+
napi_value value) {
27072717
HandleScope scope(_env);
2708-
Value().Set(utf8name, value);
2718+
return Value().Set(utf8name, value);
27092719
}
27102720

2711-
inline void ObjectReference::Set(const std::string& utf8name, Napi::Value value) {
2721+
inline bool ObjectReference::Set(const std::string& utf8name,
2722+
Napi::Value value) {
27122723
HandleScope scope(_env);
2713-
Value().Set(utf8name, value);
2724+
return Value().Set(utf8name, value);
27142725
}
27152726

2716-
inline void ObjectReference::Set(const std::string& utf8name, std::string& utf8value) {
2727+
inline bool ObjectReference::Set(const std::string& utf8name,
2728+
std::string& utf8value) {
27172729
HandleScope scope(_env);
2718-
Value().Set(utf8name, utf8value);
2730+
return Value().Set(utf8name, utf8value);
27192731
}
27202732

2721-
inline void ObjectReference::Set(const std::string& utf8name, bool boolValue) {
2733+
inline bool ObjectReference::Set(const std::string& utf8name, bool boolValue) {
27222734
HandleScope scope(_env);
2723-
Value().Set(utf8name, boolValue);
2735+
return Value().Set(utf8name, boolValue);
27242736
}
27252737

2726-
inline void ObjectReference::Set(const std::string& utf8name, double numberValue) {
2738+
inline bool ObjectReference::Set(const std::string& utf8name,
2739+
double numberValue) {
27272740
HandleScope scope(_env);
2728-
Value().Set(utf8name, numberValue);
2741+
return Value().Set(utf8name, numberValue);
27292742
}
27302743

27312744
inline Napi::Value ObjectReference::Get(uint32_t index) const {
27322745
EscapableHandleScope scope(_env);
27332746
return scope.Escape(Value().Get(index));
27342747
}
27352748

2736-
inline void ObjectReference::Set(uint32_t index, napi_value value) {
2749+
inline bool ObjectReference::Set(uint32_t index, napi_value value) {
27372750
HandleScope scope(_env);
2738-
Value().Set(index, value);
2751+
return Value().Set(index, value);
27392752
}
27402753

2741-
inline void ObjectReference::Set(uint32_t index, Napi::Value value) {
2754+
inline bool ObjectReference::Set(uint32_t index, Napi::Value value) {
27422755
HandleScope scope(_env);
2743-
Value().Set(index, value);
2756+
return Value().Set(index, value);
27442757
}
27452758

2746-
inline void ObjectReference::Set(uint32_t index, const char* utf8value) {
2759+
inline bool ObjectReference::Set(uint32_t index, const char* utf8value) {
27472760
HandleScope scope(_env);
2748-
Value().Set(index, utf8value);
2761+
return Value().Set(index, utf8value);
27492762
}
27502763

2751-
inline void ObjectReference::Set(uint32_t index, const std::string& utf8value) {
2764+
inline bool ObjectReference::Set(uint32_t index, const std::string& utf8value) {
27522765
HandleScope scope(_env);
2753-
Value().Set(index, utf8value);
2766+
return Value().Set(index, utf8value);
27542767
}
27552768

2756-
inline void ObjectReference::Set(uint32_t index, bool boolValue) {
2769+
inline bool ObjectReference::Set(uint32_t index, bool boolValue) {
27572770
HandleScope scope(_env);
2758-
Value().Set(index, boolValue);
2771+
return Value().Set(index, boolValue);
27592772
}
27602773

2761-
inline void ObjectReference::Set(uint32_t index, double numberValue) {
2774+
inline bool ObjectReference::Set(uint32_t index, double numberValue) {
27622775
HandleScope scope(_env);
2763-
Value().Set(index, numberValue);
2776+
return Value().Set(index, numberValue);
27642777
}
27652778

27662779
////////////////////////////////////////////////////////////////////////////////

napi.h

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -675,30 +675,26 @@ namespace Napi {
675675

676676
/// Sets a property.
677677
template <typename ValueType>
678-
void Set(
679-
napi_value key, ///< Property key primitive
680-
const ValueType& value ///< Property value primitive
678+
bool Set(napi_value key, ///< Property key primitive
679+
const ValueType& value ///< Property value primitive
681680
);
682681

683682
/// Sets a property.
684683
template <typename ValueType>
685-
void Set(
686-
Value key, ///< Property key
687-
const ValueType& value ///< Property value
684+
bool Set(Value key, ///< Property key
685+
const ValueType& value ///< Property value
688686
);
689687

690688
/// Sets a named property.
691689
template <typename ValueType>
692-
void Set(
693-
const char* utf8name, ///< UTF-8 encoded null-terminated property name
694-
const ValueType& value
695-
);
690+
bool Set(
691+
const char* utf8name, ///< UTF-8 encoded null-terminated property name
692+
const ValueType& value);
696693

697694
/// Sets a named property.
698695
template <typename ValueType>
699-
void Set(
700-
const std::string& utf8name, ///< UTF-8 encoded property name
701-
const ValueType& value ///< Property value primitive
696+
bool Set(const std::string& utf8name, ///< UTF-8 encoded property name
697+
const ValueType& value ///< Property value primitive
702698
);
703699

704700
/// Delete property.
@@ -733,9 +729,8 @@ namespace Napi {
733729

734730
/// Sets an indexed property or array element.
735731
template <typename ValueType>
736-
void Set(
737-
uint32_t index, ///< Property / element index
738-
const ValueType& value ///< Property value primitive
732+
bool Set(uint32_t index, ///< Property / element index
733+
const ValueType& value ///< Property value primitive
739734
);
740735

741736
/// Deletes an indexed property or array element.
@@ -746,19 +741,20 @@ namespace Napi {
746741
Array GetPropertyNames() const; ///< Get all property names
747742

748743
/// Defines a property on the object.
749-
void DefineProperty(
750-
const PropertyDescriptor& property ///< Descriptor for the property to be defined
744+
bool DefineProperty(
745+
const PropertyDescriptor&
746+
property ///< Descriptor for the property to be defined
751747
);
752748

753749
/// Defines properties on the object.
754-
void DefineProperties(
755-
const std::initializer_list<PropertyDescriptor>& properties
750+
bool DefineProperties(
751+
const std::initializer_list<PropertyDescriptor>& properties
756752
///< List of descriptors for the properties to be defined
757753
);
758754

759755
/// Defines properties on the object.
760-
void DefineProperties(
761-
const std::vector<PropertyDescriptor>& properties
756+
bool DefineProperties(
757+
const std::vector<PropertyDescriptor>& properties
762758
///< Vector of descriptors for the properties to be defined
763759
);
764760

@@ -1261,26 +1257,26 @@ namespace Napi {
12611257

12621258
Napi::Value Get(const char* utf8name) const;
12631259
Napi::Value Get(const std::string& utf8name) const;
1264-
void Set(const char* utf8name, napi_value value);
1265-
void Set(const char* utf8name, Napi::Value value);
1266-
void Set(const char* utf8name, const char* utf8value);
1267-
void Set(const char* utf8name, bool boolValue);
1268-
void Set(const char* utf8name, double numberValue);
1269-
void Set(const std::string& utf8name, napi_value value);
1270-
void Set(const std::string& utf8name, Napi::Value value);
1271-
void Set(const std::string& utf8name, std::string& utf8value);
1272-
void Set(const std::string& utf8name, bool boolValue);
1273-
void Set(const std::string& utf8name, double numberValue);
1260+
bool Set(const char* utf8name, napi_value value);
1261+
bool Set(const char* utf8name, Napi::Value value);
1262+
bool Set(const char* utf8name, const char* utf8value);
1263+
bool Set(const char* utf8name, bool boolValue);
1264+
bool Set(const char* utf8name, double numberValue);
1265+
bool Set(const std::string& utf8name, napi_value value);
1266+
bool Set(const std::string& utf8name, Napi::Value value);
1267+
bool Set(const std::string& utf8name, std::string& utf8value);
1268+
bool Set(const std::string& utf8name, bool boolValue);
1269+
bool Set(const std::string& utf8name, double numberValue);
12741270

12751271
Napi::Value Get(uint32_t index) const;
1276-
void Set(uint32_t index, const napi_value value);
1277-
void Set(uint32_t index, const Napi::Value value);
1278-
void Set(uint32_t index, const char* utf8value);
1279-
void Set(uint32_t index, const std::string& utf8value);
1280-
void Set(uint32_t index, bool boolValue);
1281-
void Set(uint32_t index, double numberValue);
1272+
bool Set(uint32_t index, const napi_value value);
1273+
bool Set(uint32_t index, const Napi::Value value);
1274+
bool Set(uint32_t index, const char* utf8value);
1275+
bool Set(uint32_t index, const std::string& utf8value);
1276+
bool Set(uint32_t index, bool boolValue);
1277+
bool Set(uint32_t index, double numberValue);
12821278

1283-
protected:
1279+
protected:
12841280
ObjectReference(const ObjectReference&);
12851281
};
12861282

test/object/object.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Value GetPropertyWithCStyleString(const CallbackInfo& info);
1010
Value GetPropertyWithCppStyleString(const CallbackInfo& info);
1111

1212
// Native wrappers for testing Object::Set()
13-
void SetPropertyWithNapiValue(const CallbackInfo& info);
14-
void SetPropertyWithNapiWrapperValue(const CallbackInfo& info);
15-
void SetPropertyWithCStyleString(const CallbackInfo& info);
16-
void SetPropertyWithCppStyleString(const CallbackInfo& info);
13+
Value SetPropertyWithNapiValue(const CallbackInfo& info);
14+
Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info);
15+
Value SetPropertyWithCStyleString(const CallbackInfo& info);
16+
Value SetPropertyWithCppStyleString(const CallbackInfo& info);
1717

1818
// Native wrappers for testing Object::Delete()
1919
Value DeletePropertyWithUint32(const CallbackInfo& info);

0 commit comments

Comments
 (0)