n-api: add napi_delete_element()
Refs: https://github.com/nodejs/node/issues/13924 PR-URL: https://github.com/nodejs/node/pull/13949 Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
This commit is contained in:
parent
9e5a2118cc
commit
f803e77b1e
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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),
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user