Skip to content

Commit b744ffd

Browse files
lundibundirichardlau
authored andcommitted
n-api: implement napi_is_detached_arraybuffer
This implements ArrayBuffer#IsDetachedBuffer operation as per ECMAScript specification Section 24.1.1.2 https://tc39.es/ecma262/#sec-isdetachedbuffer Closes: #29955 PR-URL: #30613 Backport-PR-URL: #33061 Fixes: #29955 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 961598b commit b744ffd

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

doc/api/n-api.md

+26
Original file line numberDiff line numberDiff line change
@@ -2972,6 +2972,31 @@ that is, created with [`napi_create_external_arraybuffer`][].
29722972
This API represents the invocation of the `ArrayBuffer` detach operation as
29732973
defined in [Section 24.1.1.3][] of the ECMAScript Language Specification.
29742974

2975+
### napi_is_detached_arraybuffer
2976+
<!-- YAML
2977+
added: REPLACEME
2978+
-->
2979+
2980+
> Stability: 1 - Experimental
2981+
2982+
```C
2983+
napi_status napi_is_detached_arraybuffer(napi_env env,
2984+
napi_value arraybuffer,
2985+
bool* result)
2986+
```
2987+
2988+
* `[in] env`: The environment that the API is invoked under.
2989+
* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be checked.
2990+
* `[out] result`: Whether the `arraybuffer` is detached.
2991+
2992+
Returns `napi_ok` if the API succeeded.
2993+
2994+
The `ArrayBuffer` is considered detached if its internal data is `null`.
2995+
2996+
This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
2997+
operation as defined in [Section 24.1.1.2][] of the ECMAScript Language
2998+
Specification.
2999+
29753000
## Working with JavaScript Properties
29763001

29773002
N-API exposes a set of APIs to get and set properties on JavaScript
@@ -4924,6 +4949,7 @@ This API may only be called from the main thread.
49244949
[Section 8.7]: https://tc39.es/ecma262/#sec-agents
49254950
[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
49264951
[Working with JavaScript Functions]: #n_api_working_with_javascript_functions
4952+
[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer
49274953
[Working with JavaScript Properties]: #n_api_working_with_javascript_properties
49284954
[Working with JavaScript Values - Abstract Operations]: #n_api_working_with_javascript_values_abstract_operations
49294955
[Working with JavaScript Values]: #n_api_working_with_javascript_values

src/node_api.cc

+15
Original file line numberDiff line numberDiff line change
@@ -4403,3 +4403,18 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) {
44034403

44044404
return napi_clear_last_error(env);
44054405
}
4406+
4407+
napi_status napi_is_detached_arraybuffer(napi_env env,
4408+
napi_value arraybuffer,
4409+
bool* result) {
4410+
CHECK_ENV(env);
4411+
CHECK_ARG(env, arraybuffer);
4412+
CHECK_ARG(env, result);
4413+
4414+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
4415+
4416+
*result = value->IsArrayBuffer() &&
4417+
value.As<v8::ArrayBuffer>()->GetContents().Data() == nullptr;
4418+
4419+
return napi_clear_last_error(env);
4420+
}

src/node_api.h

+5
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,11 @@ napi_get_all_property_names(napi_env env,
752752
NAPI_EXTERN napi_status
753753
napi_detach_arraybuffer(napi_env env,
754754
napi_value arraybuffer);
755+
756+
NAPI_EXTERN napi_status
757+
napi_is_detached_arraybuffer(napi_env env,
758+
napi_value value,
759+
bool* result);
755760
#endif // NAPI_EXPERIMENTAL
756761

757762
EXTERN_C_END

test/addons-napi/test_typedarray/test.js

+13
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,21 @@ arrayTypes.forEach((currentType) => {
8787
assert.ok(externalResult instanceof Int8Array);
8888
assert.strictEqual(externalResult.length, 3);
8989
assert.strictEqual(externalResult.byteLength, 3);
90+
assert.ok(!test_typedarray.IsDetached(buffer.buffer));
9091
test_typedarray.Detach(buffer);
92+
assert.ok(test_typedarray.IsDetached(buffer.buffer));
9193
assert.ok(externalResult instanceof Int8Array);
9294
assert.strictEqual(buffer.length, 0);
9395
assert.strictEqual(buffer.byteLength, 0);
9496
}
97+
98+
{
99+
const buffer = new ArrayBuffer(128);
100+
assert.ok(!test_typedarray.IsDetached(buffer));
101+
}
102+
103+
{
104+
const buffer = test_typedarray.NullArrayBuffer();
105+
assert.ok(buffer instanceof ArrayBuffer);
106+
assert.ok(test_typedarray.IsDetached(buffer));
107+
}

test/addons-napi/test_typedarray/test_typedarray.c

+32
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ static napi_value External(napi_env env, napi_callback_info info) {
9797
return output_array;
9898
}
9999

100+
101+
static napi_value NullArrayBuffer(napi_env env, napi_callback_info info) {
102+
static void* data = NULL;
103+
napi_value arraybuffer;
104+
NAPI_CALL(env,
105+
napi_create_external_arraybuffer(env, data, 0, NULL, NULL, &arraybuffer));
106+
return arraybuffer;
107+
}
108+
100109
static napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
101110
size_t argc = 4;
102111
napi_value args[4];
@@ -183,12 +192,35 @@ static napi_value Detach(napi_env env, napi_callback_info info) {
183192
return NULL;
184193
}
185194

195+
static napi_value IsDetached(napi_env env, napi_callback_info info) {
196+
size_t argc = 1;
197+
napi_value args[1];
198+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
199+
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments.");
200+
201+
napi_value array_buffer = args[0];
202+
bool is_arraybuffer;
203+
NAPI_CALL(env, napi_is_arraybuffer(env, array_buffer, &is_arraybuffer));
204+
NAPI_ASSERT(env, is_arraybuffer,
205+
"Wrong type of arguments. Expects an array buffer as first argument.");
206+
207+
bool is_detached;
208+
NAPI_CALL(env, napi_is_detached_arraybuffer(env, array_buffer, &is_detached));
209+
210+
napi_value result;
211+
NAPI_CALL(env, napi_get_boolean(env, is_detached, &result));
212+
213+
return result;
214+
}
215+
186216
static napi_value Init(napi_env env, napi_value exports) {
187217
napi_property_descriptor descriptors[] = {
188218
DECLARE_NAPI_PROPERTY("Multiply", Multiply),
189219
DECLARE_NAPI_PROPERTY("External", External),
220+
DECLARE_NAPI_PROPERTY("NullArrayBuffer", NullArrayBuffer),
190221
DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray),
191222
DECLARE_NAPI_PROPERTY("Detach", Detach),
223+
DECLARE_NAPI_PROPERTY("IsDetached", IsDetached),
192224
};
193225

194226
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)