Skip to content

Commit 8171cef

Browse files
lundibundiaddaleax
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 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 a2f440d commit 8171cef

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

doc/api/n-api.md

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

3261+
### napi_is_detached_arraybuffer
3262+
<!-- YAML
3263+
added: REPLACEME
3264+
-->
3265+
3266+
> Stability: 1 - Experimental
3267+
3268+
```C
3269+
napi_status napi_is_detached_arraybuffer(napi_env env,
3270+
napi_value arraybuffer,
3271+
bool* result)
3272+
```
3273+
3274+
* `[in] env`: The environment that the API is invoked under.
3275+
* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be checked.
3276+
* `[out] result`: Whether the `arraybuffer` is detached.
3277+
3278+
Returns `napi_ok` if the API succeeded.
3279+
3280+
The `ArrayBuffer` is considered detached if its internal data is `null`.
3281+
3282+
This API represents the invocation of the `ArrayBuffer` `IsDetachedBuffer`
3283+
operation as defined in [Section 24.1.1.2][] of the ECMAScript Language
3284+
Specification.
3285+
32613286
## Working with JavaScript Properties
32623287

32633288
N-API exposes a set of APIs to get and set properties on JavaScript
@@ -5259,6 +5284,7 @@ This API may only be called from the main thread.
52595284
[Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations
52605285
[Section 8.7]: https://tc39.es/ecma262/#sec-agents
52615286
[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
5287+
[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer
52625288
[Travis CI]: https://travis-ci.org
52635289
[Visual Studio]: https://visualstudio.microsoft.com
52645290
[Working with JavaScript Properties]: #n_api_working_with_javascript_properties

src/js_native_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ NAPI_EXTERN napi_status napi_get_instance_data(napi_env env,
518518
// ArrayBuffer detaching
519519
NAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env,
520520
napi_value arraybuffer);
521+
522+
NAPI_EXTERN napi_status napi_is_detached_arraybuffer(napi_env env,
523+
napi_value value,
524+
bool* result);
521525
#endif // NAPI_EXPERIMENTAL
522526

523527
EXTERN_C_END

src/js_native_api_v8.cc

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

30413041
return napi_clear_last_error(env);
30423042
}
3043+
3044+
napi_status napi_is_detached_arraybuffer(napi_env env,
3045+
napi_value arraybuffer,
3046+
bool* result) {
3047+
CHECK_ENV(env);
3048+
CHECK_ARG(env, arraybuffer);
3049+
CHECK_ARG(env, result);
3050+
3051+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
3052+
3053+
*result = value->IsArrayBuffer() &&
3054+
value.As<v8::ArrayBuffer>()->GetBackingStore()->Data() == nullptr;
3055+
3056+
return napi_clear_last_error(env);
3057+
}

test/js-native-api/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/js-native-api/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,13 +192,36 @@ 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
EXTERN_C_START
187217
napi_value Init(napi_env env, napi_value exports) {
188218
napi_property_descriptor descriptors[] = {
189219
DECLARE_NAPI_PROPERTY("Multiply", Multiply),
190220
DECLARE_NAPI_PROPERTY("External", External),
221+
DECLARE_NAPI_PROPERTY("NullArrayBuffer", NullArrayBuffer),
191222
DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray),
192223
DECLARE_NAPI_PROPERTY("Detach", Detach),
224+
DECLARE_NAPI_PROPERTY("IsDetached", IsDetached),
193225
};
194226

195227
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)