Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n-api: add napi_delete_element() #13949

Merged
merged 1 commit into from
Jun 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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