Skip to content

Commit

Permalink
src: return bool on object set and define property (#977)
Browse files Browse the repository at this point in the history
If c++ exception is disabled, there is no ergonomic way to detect
failtures on void returning.
  • Loading branch information
legendecas authored Apr 29, 2021
1 parent 331c2ee commit 93f1898
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 104 deletions.
6 changes: 3 additions & 3 deletions doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Creates a new `Napi::Object` value.
### Set()

```cpp
void Napi::Object::Set (____ key, ____ value);
bool Napi::Object::Set (____ key, ____ value);
```
- `[in] key`: The name for the property being assigned.
- `[in] value`: The value being assigned to the property.
Expand Down Expand Up @@ -200,7 +200,7 @@ The key can be any of the following types:
### DefineProperty()
```cpp
void Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
bool Napi::Object::DefineProperty (const Napi::PropertyDescriptor& property);
```
- `[in] property`: A [`Napi::PropertyDescriptor`](property_descriptor.md).

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

```cpp
void Napi::Object::DefineProperties (____ properties)
bool Napi::Object::DefineProperties (____ properties)
```
- `[in] properties`: A list of [`Napi::PropertyDescriptor`](property_descriptor.md). Can be one of the following types:
- const std::initializer_list<Napi::PropertyDescriptor>&
Expand Down
2 changes: 1 addition & 1 deletion doc/object_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Returns the newly created reference.
### Set
```cpp
void Napi::ObjectReference::Set(___ key, ___ value);
bool Napi::ObjectReference::Set(___ key, ___ value);
```

* `[in] key`: The name for the property being assigned.
Expand Down
109 changes: 61 additions & 48 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1236,29 +1236,32 @@ inline Value Object::Get(const std::string& utf8name) const {
}

template <typename ValueType>
inline void Object::Set(napi_value key, const ValueType& value) {
inline bool Object::Set(napi_value key, const ValueType& value) {
napi_status status =
napi_set_property(_env, _value, key, Value::From(_env, value));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

template <typename ValueType>
inline void Object::Set(Value key, const ValueType& value) {
inline bool Object::Set(Value key, const ValueType& value) {
napi_status status =
napi_set_property(_env, _value, key, Value::From(_env, value));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

template <typename ValueType>
inline void Object::Set(const char* utf8name, const ValueType& value) {
inline bool Object::Set(const char* utf8name, const ValueType& value) {
napi_status status =
napi_set_named_property(_env, _value, utf8name, Value::From(_env, value));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

template <typename ValueType>
inline void Object::Set(const std::string& utf8name, const ValueType& value) {
Set(utf8name.c_str(), value);
inline bool Object::Set(const std::string& utf8name, const ValueType& value) {
return Set(utf8name.c_str(), value);
}

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

template <typename ValueType>
inline void Object::Set(uint32_t index, const ValueType& value) {
inline bool Object::Set(uint32_t index, const ValueType& value) {
napi_status status =
napi_set_element(_env, _value, index, Value::From(_env, value));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

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

inline void Object::DefineProperty(const PropertyDescriptor& property) {
inline bool Object::DefineProperty(const PropertyDescriptor& property) {
napi_status status = napi_define_properties(_env, _value, 1,
reinterpret_cast<const napi_property_descriptor*>(&property));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

inline void Object::DefineProperties(const std::initializer_list<PropertyDescriptor>& properties) {
inline bool Object::DefineProperties(
const std::initializer_list<PropertyDescriptor>& properties) {
napi_status status = napi_define_properties(_env, _value, properties.size(),
reinterpret_cast<const napi_property_descriptor*>(properties.begin()));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

inline void Object::DefineProperties(const std::vector<PropertyDescriptor>& properties) {
inline bool Object::DefineProperties(
const std::vector<PropertyDescriptor>& properties) {
napi_status status = napi_define_properties(_env, _value, properties.size(),
reinterpret_cast<const napi_property_descriptor*>(properties.data()));
NAPI_THROW_IF_FAILED_VOID(_env, status);
NAPI_THROW_IF_FAILED(_env, status, false);
return true;
}

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

inline void ObjectReference::Set(const char* utf8name, napi_value value) {
inline bool ObjectReference::Set(const char* utf8name, napi_value value) {
HandleScope scope(_env);
Value().Set(utf8name, value);
return Value().Set(utf8name, value);
}

inline void ObjectReference::Set(const char* utf8name, Napi::Value value) {
inline bool ObjectReference::Set(const char* utf8name, Napi::Value value) {
HandleScope scope(_env);
Value().Set(utf8name, value);
return Value().Set(utf8name, value);
}

inline void ObjectReference::Set(const char* utf8name, const char* utf8value) {
inline bool ObjectReference::Set(const char* utf8name, const char* utf8value) {
HandleScope scope(_env);
Value().Set(utf8name, utf8value);
return Value().Set(utf8name, utf8value);
}

inline void ObjectReference::Set(const char* utf8name, bool boolValue) {
inline bool ObjectReference::Set(const char* utf8name, bool boolValue) {
HandleScope scope(_env);
Value().Set(utf8name, boolValue);
return Value().Set(utf8name, boolValue);
}

inline void ObjectReference::Set(const char* utf8name, double numberValue) {
inline bool ObjectReference::Set(const char* utf8name, double numberValue) {
HandleScope scope(_env);
Value().Set(utf8name, numberValue);
return Value().Set(utf8name, numberValue);
}

inline void ObjectReference::Set(const std::string& utf8name, napi_value value) {
inline bool ObjectReference::Set(const std::string& utf8name,
napi_value value) {
HandleScope scope(_env);
Value().Set(utf8name, value);
return Value().Set(utf8name, value);
}

inline void ObjectReference::Set(const std::string& utf8name, Napi::Value value) {
inline bool ObjectReference::Set(const std::string& utf8name,
Napi::Value value) {
HandleScope scope(_env);
Value().Set(utf8name, value);
return Value().Set(utf8name, value);
}

inline void ObjectReference::Set(const std::string& utf8name, std::string& utf8value) {
inline bool ObjectReference::Set(const std::string& utf8name,
std::string& utf8value) {
HandleScope scope(_env);
Value().Set(utf8name, utf8value);
return Value().Set(utf8name, utf8value);
}

inline void ObjectReference::Set(const std::string& utf8name, bool boolValue) {
inline bool ObjectReference::Set(const std::string& utf8name, bool boolValue) {
HandleScope scope(_env);
Value().Set(utf8name, boolValue);
return Value().Set(utf8name, boolValue);
}

inline void ObjectReference::Set(const std::string& utf8name, double numberValue) {
inline bool ObjectReference::Set(const std::string& utf8name,
double numberValue) {
HandleScope scope(_env);
Value().Set(utf8name, numberValue);
return Value().Set(utf8name, numberValue);
}

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

inline void ObjectReference::Set(uint32_t index, napi_value value) {
inline bool ObjectReference::Set(uint32_t index, napi_value value) {
HandleScope scope(_env);
Value().Set(index, value);
return Value().Set(index, value);
}

inline void ObjectReference::Set(uint32_t index, Napi::Value value) {
inline bool ObjectReference::Set(uint32_t index, Napi::Value value) {
HandleScope scope(_env);
Value().Set(index, value);
return Value().Set(index, value);
}

inline void ObjectReference::Set(uint32_t index, const char* utf8value) {
inline bool ObjectReference::Set(uint32_t index, const char* utf8value) {
HandleScope scope(_env);
Value().Set(index, utf8value);
return Value().Set(index, utf8value);
}

inline void ObjectReference::Set(uint32_t index, const std::string& utf8value) {
inline bool ObjectReference::Set(uint32_t index, const std::string& utf8value) {
HandleScope scope(_env);
Value().Set(index, utf8value);
return Value().Set(index, utf8value);
}

inline void ObjectReference::Set(uint32_t index, bool boolValue) {
inline bool ObjectReference::Set(uint32_t index, bool boolValue) {
HandleScope scope(_env);
Value().Set(index, boolValue);
return Value().Set(index, boolValue);
}

inline void ObjectReference::Set(uint32_t index, double numberValue) {
inline bool ObjectReference::Set(uint32_t index, double numberValue) {
HandleScope scope(_env);
Value().Set(index, numberValue);
return Value().Set(index, numberValue);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
74 changes: 35 additions & 39 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -675,30 +675,26 @@ namespace Napi {

/// Sets a property.
template <typename ValueType>
void Set(
napi_value key, ///< Property key primitive
const ValueType& value ///< Property value primitive
bool Set(napi_value key, ///< Property key primitive
const ValueType& value ///< Property value primitive
);

/// Sets a property.
template <typename ValueType>
void Set(
Value key, ///< Property key
const ValueType& value ///< Property value
bool Set(Value key, ///< Property key
const ValueType& value ///< Property value
);

/// Sets a named property.
template <typename ValueType>
void Set(
const char* utf8name, ///< UTF-8 encoded null-terminated property name
const ValueType& value
);
bool Set(
const char* utf8name, ///< UTF-8 encoded null-terminated property name
const ValueType& value);

/// Sets a named property.
template <typename ValueType>
void Set(
const std::string& utf8name, ///< UTF-8 encoded property name
const ValueType& value ///< Property value primitive
bool Set(const std::string& utf8name, ///< UTF-8 encoded property name
const ValueType& value ///< Property value primitive
);

/// Delete property.
Expand Down Expand Up @@ -733,9 +729,8 @@ namespace Napi {

/// Sets an indexed property or array element.
template <typename ValueType>
void Set(
uint32_t index, ///< Property / element index
const ValueType& value ///< Property value primitive
bool Set(uint32_t index, ///< Property / element index
const ValueType& value ///< Property value primitive
);

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

/// Defines a property on the object.
void DefineProperty(
const PropertyDescriptor& property ///< Descriptor for the property to be defined
bool DefineProperty(
const PropertyDescriptor&
property ///< Descriptor for the property to be defined
);

/// Defines properties on the object.
void DefineProperties(
const std::initializer_list<PropertyDescriptor>& properties
bool DefineProperties(
const std::initializer_list<PropertyDescriptor>& properties
///< List of descriptors for the properties to be defined
);

/// Defines properties on the object.
void DefineProperties(
const std::vector<PropertyDescriptor>& properties
bool DefineProperties(
const std::vector<PropertyDescriptor>& properties
///< Vector of descriptors for the properties to be defined
);

Expand Down Expand Up @@ -1261,26 +1257,26 @@ namespace Napi {

Napi::Value Get(const char* utf8name) const;
Napi::Value Get(const std::string& utf8name) const;
void Set(const char* utf8name, napi_value value);
void Set(const char* utf8name, Napi::Value value);
void Set(const char* utf8name, const char* utf8value);
void Set(const char* utf8name, bool boolValue);
void Set(const char* utf8name, double numberValue);
void Set(const std::string& utf8name, napi_value value);
void Set(const std::string& utf8name, Napi::Value value);
void Set(const std::string& utf8name, std::string& utf8value);
void Set(const std::string& utf8name, bool boolValue);
void Set(const std::string& utf8name, double numberValue);
bool Set(const char* utf8name, napi_value value);
bool Set(const char* utf8name, Napi::Value value);
bool Set(const char* utf8name, const char* utf8value);
bool Set(const char* utf8name, bool boolValue);
bool Set(const char* utf8name, double numberValue);
bool Set(const std::string& utf8name, napi_value value);
bool Set(const std::string& utf8name, Napi::Value value);
bool Set(const std::string& utf8name, std::string& utf8value);
bool Set(const std::string& utf8name, bool boolValue);
bool Set(const std::string& utf8name, double numberValue);

Napi::Value Get(uint32_t index) const;
void Set(uint32_t index, const napi_value value);
void Set(uint32_t index, const Napi::Value value);
void Set(uint32_t index, const char* utf8value);
void Set(uint32_t index, const std::string& utf8value);
void Set(uint32_t index, bool boolValue);
void Set(uint32_t index, double numberValue);
bool Set(uint32_t index, const napi_value value);
bool Set(uint32_t index, const Napi::Value value);
bool Set(uint32_t index, const char* utf8value);
bool Set(uint32_t index, const std::string& utf8value);
bool Set(uint32_t index, bool boolValue);
bool Set(uint32_t index, double numberValue);

protected:
protected:
ObjectReference(const ObjectReference&);
};

Expand Down
8 changes: 4 additions & 4 deletions test/object/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Value GetPropertyWithCStyleString(const CallbackInfo& info);
Value GetPropertyWithCppStyleString(const CallbackInfo& info);

// Native wrappers for testing Object::Set()
void SetPropertyWithNapiValue(const CallbackInfo& info);
void SetPropertyWithNapiWrapperValue(const CallbackInfo& info);
void SetPropertyWithCStyleString(const CallbackInfo& info);
void SetPropertyWithCppStyleString(const CallbackInfo& info);
Value SetPropertyWithNapiValue(const CallbackInfo& info);
Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info);
Value SetPropertyWithCStyleString(const CallbackInfo& info);
Value SetPropertyWithCppStyleString(const CallbackInfo& info);

// Native wrappers for testing Object::Delete()
Value DeletePropertyWithUint32(const CallbackInfo& info);
Expand Down
Loading

0 comments on commit 93f1898

Please sign in to comment.