diff options
author | cjihrig <cjihrig@gmail.com> | 2017-06-26 21:31:55 +0300 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2017-07-01 18:40:14 +0300 |
commit | 732ae419b42fd1392dc00097210e516d05947d63 (patch) | |
tree | 39429b293af7e5a439b9f1af06bc041d912ec769 /test/addons-napi/test_object | |
parent | f803e77b1e2347dbce9d8098f3b6e231f4ce5fcd (diff) |
n-api: add napi_delete_property()
Fixes: https://github.com/nodejs/node/issues/13924
PR-URL: https://github.com/nodejs/node/pull/13934
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Diffstat (limited to 'test/addons-napi/test_object')
-rw-r--r-- | test/addons-napi/test_object/test.js | 44 | ||||
-rw-r--r-- | test/addons-napi/test_object/test_object.c | 26 |
2 files changed, 70 insertions, 0 deletions
diff --git a/test/addons-napi/test_object/test.js b/test/addons-napi/test_object/test.js index c9c549c8274..fe6fb169b6c 100644 --- a/test/addons-napi/test_object/test.js +++ b/test/addons-napi/test_object/test.js @@ -120,3 +120,47 @@ assert.strictEqual(newObject.test_string, 'test string'); assert(wrapper.protoA, true); assert(wrapper.protoB, true); } + +{ + // Verify that normal and nonexistent properties can be deleted. + const sym = Symbol(); + const obj = { foo: 'bar', [sym]: 'baz' }; + + assert.strictEqual('foo' in obj, true); + assert.strictEqual(sym in obj, true); + assert.strictEqual('does_not_exist' in obj, false); + assert.strictEqual(test_object.Delete(obj, 'foo'), true); + assert.strictEqual('foo' in obj, false); + assert.strictEqual(sym in obj, true); + assert.strictEqual('does_not_exist' in obj, false); + assert.strictEqual(test_object.Delete(obj, sym), true); + assert.strictEqual('foo' in obj, false); + assert.strictEqual(sym in obj, false); + assert.strictEqual('does_not_exist' in obj, false); +} + +{ + // Verify that non-configurable properties are not deleted. + const obj = {}; + + Object.defineProperty(obj, 'foo', { configurable: false }); + assert.strictEqual(test_object.Delete(obj, 'foo'), false); + assert.strictEqual('foo' in obj, true); +} + +{ + // Verify that prototype properties are not deleted. + function Foo() { + this.foo = 'bar'; + } + + Foo.prototype.foo = 'baz'; + + const obj = new Foo(); + + assert.strictEqual(obj.foo, 'bar'); + assert.strictEqual(test_object.Delete(obj, 'foo'), true); + assert.strictEqual(obj.foo, 'baz'); + assert.strictEqual(test_object.Delete(obj, 'foo'), true); + assert.strictEqual(obj.foo, 'baz'); +} diff --git a/test/addons-napi/test_object/test_object.c b/test/addons-napi/test_object/test_object.c index 383fe46342a..ad24d61ca78 100644 --- a/test/addons-napi/test_object/test_object.c +++ b/test/addons-napi/test_object/test_object.c @@ -86,6 +86,31 @@ napi_value Has(napi_env env, napi_callback_info info) { return ret; } +napi_value Delete(napi_env env, napi_callback_info info) { + size_t argc = 2; + napi_value args[2]; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + NAPI_ASSERT(env, argc == 2, "Wrong number of arguments"); + + napi_valuetype valuetype0; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); + NAPI_ASSERT(env, valuetype0 == napi_object, + "Wrong type of arguments. Expects an object as first argument."); + + napi_valuetype valuetype1; + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); + NAPI_ASSERT(env, valuetype1 == napi_string || valuetype1 == napi_symbol, + "Wrong type of arguments. Expects a string or symbol as second."); + + bool result; + napi_value ret; + NAPI_CALL(env, napi_delete_property(env, args[0], args[1], &result)); + NAPI_CALL(env, napi_get_boolean(env, result, &ret)); + + return ret; +} + napi_value New(napi_env env, napi_callback_info info) { napi_value ret; NAPI_CALL(env, napi_create_object(env, &ret)); @@ -171,6 +196,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { DECLARE_NAPI_PROPERTY("Get", Get), DECLARE_NAPI_PROPERTY("Set", Set), DECLARE_NAPI_PROPERTY("Has", Has), + DECLARE_NAPI_PROPERTY("Delete", Delete), DECLARE_NAPI_PROPERTY("New", New), DECLARE_NAPI_PROPERTY("Inflate", Inflate), DECLARE_NAPI_PROPERTY("Wrap", Wrap), |