Skip to content

Commit

Permalink
Add API for deletion of object properties.
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node-addon-api#151
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
John French committed Oct 3, 2017
1 parent 375287b commit 4416c11
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
31 changes: 30 additions & 1 deletion napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,28 @@ inline void Object::Set(const std::string& utf8name, double numberValue) {
Set(utf8name.c_str(), Number::New(Env(), numberValue));
}

inline bool Object::Delete(napi_value key) {
bool result;
napi_status status = napi_delete_property(_env, _value, key, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}

inline bool Object::Delete(Value key) {
bool result;
napi_status status = napi_delete_property(_env, _value, key, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}

inline bool Object::Delete(const char* utf8name) {
return Delete(String::New(_env, utf8name));
}

inline bool Object::Delete(const std::string& utf8name) {
return Delete(String::New(_env, utf8name));
}

inline bool Object::Has(uint32_t index) const {
bool result;
napi_status status = napi_has_element(_env, _value, index, &result);
Expand Down Expand Up @@ -797,6 +819,13 @@ inline void Object::Set(uint32_t index, double numberValue) {
Set(index, static_cast<napi_value>(Number::New(Env(), numberValue)));
}

inline bool Object::Delete(uint32_t index) {
bool result;
napi_status status = napi_delete_element(_env, _value, index, &result);
NAPI_THROW_IF_FAILED(_env, status, false);
return result;
}

inline Array Object::GetPropertyNames() {
napi_value result;
napi_status status = napi_get_property_names(_env, _value, &result);
Expand Down Expand Up @@ -1657,7 +1686,7 @@ inline Reference<T>& Reference<T>::operator =(Reference<T>&& other) {
}

template <typename T>
inline Reference<T>::Reference(const Reference<T>& other)
inline Reference<T>::Reference(const Reference<T>& other)
: _env(other._env), _ref(nullptr), _suppressDestruct(false) {
HandleScope scope(_env);

Expand Down
25 changes: 25 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,26 @@ namespace Napi {
double numberValue ///< Property value
);

/// Delete property.
bool Delete(
napi_value key ///< Property key primitive
);

/// Delete property.
bool Delete(
Value key ///< Property key
);

/// Delete property.
bool Delete(
const char* utf8name ///< UTF-8 encoded null-terminated property name
);

/// Delete property.
bool Delete(
const std::string& utf8name ///< UTF-8 encoded property name
);

/// Checks whether an indexed property is present.
bool Has(
uint32_t index ///< Property / element index
Expand Down Expand Up @@ -537,6 +557,11 @@ namespace Napi {
double numberValue ///< Property value
);

/// Deletes an indexed property or array element.
bool Delete(
uint32_t index ///< Property / element index
);

Array GetPropertyNames(); ///< Get all property names

/// Defines a property on the object.
Expand Down
7 changes: 7 additions & 0 deletions test/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ void SetProperty(const CallbackInfo& info) {
obj.Set(name, value);
}

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

Object InitObject(Env env) {
Object exports = Object::New(env);

Expand All @@ -110,6 +116,7 @@ Object InitObject(Env env) {
exports["defineValueProperty"] = Function::New(env, DefineValueProperty);
exports["getProperty"] = Function::New(env, GetProperty);
exports["setProperty"] = Function::New(env, SetProperty);
exports["deleteProperty"] = Function::New(env, DeleteProperty);

return exports;
}
15 changes: 15 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ function test(binding) {
assert.strictEqual(obj.test, 1);
}

{
const obj = { one: 1, two: 2 };
Object.defineProperty(obj, "three", {configurable: false, value: 3});
assert.strictEqual(binding.object.deleteProperty(obj, 'one'), true);
assert.strictEqual(binding.object.deleteProperty(obj, 'missing'), true);

/* Returns true for all cases except when the property is an own non-
configurable property, in which case, false is returned in non-strict mode. */
assert.strictEqual(binding.object.deleteProperty(obj, 'three'), false);
assert.deepStrictEqual(obj, { two: 2 });
}

{
const obj = {'one': 1, 'two': 2, 'three': 3};
var arr = binding.object.GetPropertyNames(obj);
Expand All @@ -94,4 +106,7 @@ function test(binding) {
assert.throws(() => {
binding.object.setProperty(undefined, 'test', 1);
}, /object was expected/);
assert.throws(() => {
binding.object.deleteProperty(undefined, 'test');
}, /object was expected/);
}

0 comments on commit 4416c11

Please sign in to comment.