Skip to content

Commit

Permalink
n-api: add napi_delete_element()
Browse files Browse the repository at this point in the history
Refs: nodejs#13924
PR-URL: nodejs#13949
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
  • Loading branch information
cjihrig authored and Gabriel Schulhof committed Apr 10, 2018
1 parent 922ea46 commit ae0bdd5
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3052,6 +3073,7 @@ support it:
[`napi_define_class`]: #n_api_napi_define_class
[`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
Expand Down
20 changes: 20 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,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,
Expand Down
4 changes: 4 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions test/addons-napi/test_array/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
36 changes: 36 additions & 0 deletions test/addons-napi/test_array/test_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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),
};
Expand Down

0 comments on commit ae0bdd5

Please sign in to comment.