Skip to content

Commit 6316c9a

Browse files
cjihrigaddaleax
authored andcommitted
n-api: add napi_delete_element()
Refs: #13924 PR-URL: #13949 Reviewed-By: Jason Ginchereau <[email protected]>
1 parent 79ead79 commit 6316c9a

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

doc/api/n-api.md

+22
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,27 @@ Returns `napi_ok` if the API succeeded.
24002400
This API returns if the Object passed in has an element at the
24012401
requested index.
24022402

2403+
#### *napi_delete_element*
2404+
<!-- YAML
2405+
added: REPLACEME
2406+
-->
2407+
```C
2408+
napi_status napi_delete_element(napi_env env,
2409+
napi_value object,
2410+
uint32_t index,
2411+
bool* result);
2412+
```
2413+
2414+
- `[in] env`: The environment that the N-API call is invoked under.
2415+
- `[in] object`: The object to query.
2416+
- `[in] index`: The index of the property to delete.
2417+
- `[out] result`: Whether the element deletion succeeded or not. `result` can
2418+
optionally be ignored by passing `NULL`.
2419+
2420+
Returns `napi_ok` if the API succeeded.
2421+
2422+
This API attempts to delete the specified `index` from `object`.
2423+
24032424
#### *napi_define_properties*
24042425
<!-- YAML
24052426
added: v8.0.0
@@ -3051,6 +3072,7 @@ support it:
30513072
[`napi_create_type_error`]: #n_api_napi_create_type_error
30523073
[`napi_delete_async_work`]: #n_api_napi_delete_async_work
30533074
[`napi_define_class`]: #n_api_napi_define_class
3075+
[`napi_delete_element`]: #n_api_napi_delete_element
30543076
[`napi_delete_reference`]: #n_api_napi_delete_reference
30553077
[`napi_escape_handle`]: #n_api_napi_escape_handle
30563078
[`napi_get_array_length`]: #n_api_napi_get_array_length

src/node_api.cc

+20
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,26 @@ napi_status napi_get_element(napi_env env,
11391139
return GET_RETURN_STATUS(env);
11401140
}
11411141

1142+
napi_status napi_delete_element(napi_env env,
1143+
napi_value object,
1144+
uint32_t index,
1145+
bool* result) {
1146+
NAPI_PREAMBLE(env);
1147+
1148+
v8::Isolate* isolate = env->isolate;
1149+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
1150+
v8::Local<v8::Object> obj;
1151+
1152+
CHECK_TO_OBJECT(env, context, obj, object);
1153+
v8::Maybe<bool> delete_maybe = obj->Delete(context, index);
1154+
CHECK_MAYBE_NOTHING(env, delete_maybe, napi_generic_failure);
1155+
1156+
if (result != NULL)
1157+
*result = delete_maybe.FromMaybe(false);
1158+
1159+
return GET_RETURN_STATUS(env);
1160+
}
1161+
11421162
napi_status napi_define_properties(napi_env env,
11431163
napi_value object,
11441164
size_t property_count,

src/node_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ NAPI_EXTERN napi_status napi_get_element(napi_env env,
250250
napi_value object,
251251
uint32_t index,
252252
napi_value* result);
253+
NAPI_EXTERN napi_status napi_delete_element(napi_env env,
254+
napi_value object,
255+
uint32_t index,
256+
bool* result);
253257
NAPI_EXTERN napi_status
254258
napi_define_properties(napi_env env,
255259
napi_value object,

test/addons-napi/test_array/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,14 @@ assert(test_array.NewWithLength(0) instanceof Array);
4747
assert(test_array.NewWithLength(1) instanceof Array);
4848
// check max allowed length for an array 2^32 -1
4949
assert(test_array.NewWithLength(4294967295) instanceof Array);
50+
51+
{
52+
// Verify that array elements can be deleted.
53+
const arr = ['a', 'b', 'c', 'd'];
54+
55+
assert.strictEqual(arr.length, 4);
56+
assert.strictEqual(2 in arr, true);
57+
assert.strictEqual(test_array.TestDeleteElement(arr, 2), true);
58+
assert.strictEqual(arr.length, 4);
59+
assert.strictEqual(2 in arr, false);
60+
}

test/addons-napi/test_array/test_array.c

+36
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,41 @@ napi_value TestHasElement(napi_env env, napi_callback_info info) {
8484
return ret;
8585
}
8686

87+
napi_value TestDeleteElement(napi_env env, napi_callback_info info) {
88+
size_t argc = 2;
89+
napi_value args[2];
90+
91+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
92+
NAPI_ASSERT(env, argc == 2, "Wrong number of arguments");
93+
94+
napi_valuetype valuetype0;
95+
NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
96+
NAPI_ASSERT(env, valuetype0 == napi_object,
97+
"Wrong type of arguments. Expects an array as first argument.");
98+
99+
napi_valuetype valuetype1;
100+
NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
101+
NAPI_ASSERT(env, valuetype1 == napi_number,
102+
"Wrong type of arguments. Expects an integer as second argument.");
103+
104+
napi_value array = args[0];
105+
int32_t index;
106+
bool result;
107+
napi_value ret;
108+
109+
NAPI_CALL(env, napi_get_value_int32(env, args[1], &index));
110+
NAPI_CALL(env, napi_is_array(env, array, &result));
111+
112+
if (!result) {
113+
return NULL;
114+
}
115+
116+
NAPI_CALL(env, napi_delete_element(env, array, index, &result));
117+
NAPI_CALL(env, napi_get_boolean(env, result, &ret));
118+
119+
return ret;
120+
}
121+
87122
napi_value New(napi_env env, napi_callback_info info) {
88123
size_t argc = 1;
89124
napi_value args[1];
@@ -138,6 +173,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
138173
napi_property_descriptor descriptors[] = {
139174
DECLARE_NAPI_PROPERTY("TestGetElement", TestGetElement),
140175
DECLARE_NAPI_PROPERTY("TestHasElement", TestHasElement),
176+
DECLARE_NAPI_PROPERTY("TestDeleteElement", TestDeleteElement),
141177
DECLARE_NAPI_PROPERTY("New", New),
142178
DECLARE_NAPI_PROPERTY("NewWithLength", NewWithLength),
143179
};

0 commit comments

Comments
 (0)