diff options
-rw-r--r-- | doc/api/n-api.md | 22 | ||||
-rw-r--r-- | src/node_api.cc | 20 | ||||
-rw-r--r-- | src/node_api.h | 4 | ||||
-rw-r--r-- | test/addons-napi/test_array/test.js | 11 | ||||
-rw-r--r-- | test/addons-napi/test_array/test_array.c | 36 |
5 files changed, 93 insertions, 0 deletions
diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 078d9642e2..052c6cbdb4 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2400,6 +2400,27 @@ Returns `napi_ok` if the API succeeded. This API returns if the Object passed in has an element at the requested index. +#### *napi_delete_element* +<!-- YAML +added: REPLACEME +--> +```C +napi_status napi_delete_element(napi_env env, + napi_value object, + uint32_t index, + bool* result); +``` + +- `[in] env`: The environment that the N-API call is invoked under. +- `[in] object`: The object to query. +- `[in] index`: The index of the property to delete. +- `[out] result`: Whether the element deletion succeeded or not. `result` can +optionally be ignored by passing `NULL`. + +Returns `napi_ok` if the API succeeded. + +This API attempts to delete the specified `index` from `object`. + #### *napi_define_properties* <!-- YAML added: v8.0.0 @@ -3051,6 +3072,7 @@ support it: [`napi_create_type_error`]: #n_api_napi_create_type_error [`napi_delete_async_work`]: #n_api_napi_delete_async_work [`napi_define_class`]: #n_api_napi_define_class +[`napi_delete_element`]: #n_api_napi_delete_element [`napi_delete_reference`]: #n_api_napi_delete_reference [`napi_escape_handle`]: #n_api_napi_escape_handle [`napi_get_array_length`]: #n_api_napi_get_array_length diff --git a/src/node_api.cc b/src/node_api.cc index 18ff7b64e4..c237e78031 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1139,6 +1139,26 @@ napi_status napi_get_element(napi_env env, return GET_RETURN_STATUS(env); } +napi_status napi_delete_element(napi_env env, + napi_value object, + uint32_t index, + bool* result) { + NAPI_PREAMBLE(env); + + v8::Isolate* isolate = env->isolate; + v8::Local<v8::Context> context = isolate->GetCurrentContext(); + v8::Local<v8::Object> obj; + + CHECK_TO_OBJECT(env, context, obj, object); + v8::Maybe<bool> delete_maybe = obj->Delete(context, index); + CHECK_MAYBE_NOTHING(env, delete_maybe, napi_generic_failure); + + if (result != NULL) + *result = delete_maybe.FromMaybe(false); + + return GET_RETURN_STATUS(env); +} + napi_status napi_define_properties(napi_env env, napi_value object, size_t property_count, diff --git a/src/node_api.h b/src/node_api.h index a184094360..58222e7431 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -250,6 +250,10 @@ NAPI_EXTERN napi_status napi_get_element(napi_env env, napi_value object, uint32_t index, napi_value* result); +NAPI_EXTERN napi_status napi_delete_element(napi_env env, + napi_value object, + uint32_t index, + bool* result); NAPI_EXTERN napi_status napi_define_properties(napi_env env, napi_value object, diff --git a/test/addons-napi/test_array/test.js b/test/addons-napi/test_array/test.js index c1df4633b2..75c181d9da 100644 --- a/test/addons-napi/test_array/test.js +++ b/test/addons-napi/test_array/test.js @@ -47,3 +47,14 @@ assert(test_array.NewWithLength(0) instanceof Array); assert(test_array.NewWithLength(1) instanceof Array); // check max allowed length for an array 2^32 -1 assert(test_array.NewWithLength(4294967295) instanceof Array); + +{ + // Verify that array elements can be deleted. + const arr = ['a', 'b', 'c', 'd']; + + assert.strictEqual(arr.length, 4); + assert.strictEqual(2 in arr, true); + assert.strictEqual(test_array.TestDeleteElement(arr, 2), true); + assert.strictEqual(arr.length, 4); + assert.strictEqual(2 in arr, false); +} diff --git a/test/addons-napi/test_array/test_array.c b/test/addons-napi/test_array/test_array.c index ed1380efc9..4bb12d4e01 100644 --- a/test/addons-napi/test_array/test_array.c +++ b/test/addons-napi/test_array/test_array.c @@ -84,6 +84,41 @@ napi_value TestHasElement(napi_env env, napi_callback_info info) { return ret; } +napi_value TestDeleteElement(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 array as first argument."); + + napi_valuetype valuetype1; + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); + NAPI_ASSERT(env, valuetype1 == napi_number, + "Wrong type of arguments. Expects an integer as second argument."); + + napi_value array = args[0]; + int32_t index; + bool result; + napi_value ret; + + NAPI_CALL(env, napi_get_value_int32(env, args[1], &index)); + NAPI_CALL(env, napi_is_array(env, array, &result)); + + if (!result) { + return NULL; + } + + NAPI_CALL(env, napi_delete_element(env, array, index, &result)); + NAPI_CALL(env, napi_get_boolean(env, result, &ret)); + + return ret; +} + napi_value New(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1]; @@ -138,6 +173,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("TestGetElement", TestGetElement), DECLARE_NAPI_PROPERTY("TestHasElement", TestHasElement), + DECLARE_NAPI_PROPERTY("TestDeleteElement", TestDeleteElement), DECLARE_NAPI_PROPERTY("New", New), DECLARE_NAPI_PROPERTY("NewWithLength", NewWithLength), }; |