diff --git a/doc/object.md b/doc/object.md index 596e6d632..06b19f9f1 100644 --- a/doc/object.md +++ b/doc/object.md @@ -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. @@ -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). @@ -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& diff --git a/doc/object_reference.md b/doc/object_reference.md index 8522b5341..38d6acc0e 100644 --- a/doc/object_reference.md +++ b/doc/object_reference.md @@ -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. diff --git a/napi-inl.h b/napi-inl.h index 9d2f366e1..be20dc085 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -1236,29 +1236,32 @@ inline Value Object::Get(const std::string& utf8name) const { } template -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 -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 -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 -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) { @@ -1298,10 +1301,11 @@ inline Value Object::Get(uint32_t index) const { } template -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) { @@ -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(&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& properties) { +inline bool Object::DefineProperties( + const std::initializer_list& properties) { napi_status status = napi_define_properties(_env, _value, properties.size(), reinterpret_cast(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& properties) { +inline bool Object::DefineProperties( + const std::vector& properties) { napi_status status = napi_define_properties(_env, _value, properties.size(), reinterpret_cast(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 { @@ -2678,54 +2687,58 @@ 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 { @@ -2733,34 +2746,34 @@ inline Napi::Value ObjectReference::Get(uint32_t index) const { 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); } //////////////////////////////////////////////////////////////////////////////// diff --git a/napi.h b/napi.h index d27ddb28b..8be2ca3f7 100644 --- a/napi.h +++ b/napi.h @@ -675,30 +675,26 @@ namespace Napi { /// Sets a property. template - 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 - 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 - 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 - 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. @@ -733,9 +729,8 @@ namespace Napi { /// Sets an indexed property or array element. template - 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. @@ -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& properties + bool DefineProperties( + const std::initializer_list& properties ///< List of descriptors for the properties to be defined ); /// Defines properties on the object. - void DefineProperties( - const std::vector& properties + bool DefineProperties( + const std::vector& properties ///< Vector of descriptors for the properties to be defined ); @@ -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&); }; diff --git a/test/object/object.cc b/test/object/object.cc index 3ee454e77..58c0392d8 100644 --- a/test/object/object.cc +++ b/test/object/object.cc @@ -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); diff --git a/test/object/set_property.cc b/test/object/set_property.cc index 19ab245b4..c17366544 100644 --- a/test/object/set_property.cc +++ b/test/object/set_property.cc @@ -2,30 +2,30 @@ using namespace Napi; -void SetPropertyWithNapiValue(const CallbackInfo& info) { +Value SetPropertyWithNapiValue(const CallbackInfo& info) { Object obj = info[0].As(); Name key = info[1].As(); Value value = info[2]; - obj.Set(static_cast(key), value); + return Boolean::New(info.Env(), obj.Set(static_cast(key), value)); } -void SetPropertyWithNapiWrapperValue(const CallbackInfo& info) { +Value SetPropertyWithNapiWrapperValue(const CallbackInfo& info) { Object obj = info[0].As(); Name key = info[1].As(); Value value = info[2]; - obj.Set(key, value); + return Boolean::New(info.Env(), obj.Set(key, value)); } -void SetPropertyWithCStyleString(const CallbackInfo& info) { +Value SetPropertyWithCStyleString(const CallbackInfo& info) { Object obj = info[0].As(); String jsKey = info[1].As(); Value value = info[2]; - obj.Set(jsKey.Utf8Value().c_str(), value); + return Boolean::New(info.Env(), obj.Set(jsKey.Utf8Value().c_str(), value)); } -void SetPropertyWithCppStyleString(const CallbackInfo& info) { +Value SetPropertyWithCppStyleString(const CallbackInfo& info) { Object obj = info[0].As(); String jsKey = info[1].As(); Value value = info[2]; - obj.Set(jsKey.Utf8Value(), value); + return Boolean::New(info.Env(), obj.Set(jsKey.Utf8Value(), value)); } diff --git a/test/object/set_property.js b/test/object/set_property.js index 772538b26..86fab3b75 100644 --- a/test/object/set_property.js +++ b/test/object/set_property.js @@ -7,7 +7,7 @@ module.exports = require('../common').runTest(test); function test(binding) { function testSetProperty(nativeSetProperty) { const obj = {}; - nativeSetProperty(obj, 'test', 1); + assert.strictEqual(nativeSetProperty(obj, 'test', 1), true); assert.strictEqual(obj.test, 1); }