Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ea3d4e8

Browse filesBrowse files
lundibundiMylesBorins
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 Backport-PR-URL: #31422 PR-URL: #30613 Fixes: #29955 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent f63e440 commit ea3d4e8
Copy full SHA for ea3d4e8

File tree

Expand file treeCollapse file tree

5 files changed

+90
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+90
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/n-api.md‎

Copy file name to clipboardExpand all lines: doc/api/n-api.md
+26Lines changed: 26 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,31 @@ that is, created with [`napi_create_external_arraybuffer`][].
32593259
This API represents the invocation of the `ArrayBuffer` detach operation as
32603260
defined in [Section 24.1.1.3][] of the ECMAScript Language Specification.
32613261

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

32643289
N-API exposes a set of APIs to get and set properties on JavaScript
@@ -5272,6 +5297,7 @@ This API may only be called from the main thread.
52725297
[Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations
52735298
[Section 8.7]: https://tc39.es/ecma262/#sec-agents
52745299
[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
5300+
[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer
52755301
[Travis CI]: https://travis-ci.org
52765302
[Visual Studio]: https://visualstudio.microsoft.com
52775303
[Working with JavaScript Properties]: #n_api_working_with_javascript_properties
Collapse file

‎src/js_native_api.h‎

Copy file name to clipboardExpand all lines: src/js_native_api.h
+4Lines changed: 4 additions & 0 deletions
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
Collapse file

‎src/js_native_api_v8.cc‎

Copy file name to clipboardExpand all lines: src/js_native_api_v8.cc
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,3 +3060,18 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) {
30603060

30613061
return napi_clear_last_error(env);
30623062
}
3063+
3064+
napi_status napi_is_detached_arraybuffer(napi_env env,
3065+
napi_value arraybuffer,
3066+
bool* result) {
3067+
CHECK_ENV(env);
3068+
CHECK_ARG(env, arraybuffer);
3069+
CHECK_ARG(env, result);
3070+
3071+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
3072+
3073+
*result = value->IsArrayBuffer() &&
3074+
value.As<v8::ArrayBuffer>()->GetContents().Data() == nullptr;
3075+
3076+
return napi_clear_last_error(env);
3077+
}
Collapse file

‎test/js-native-api/test_typedarray/test.js‎

Copy file name to clipboardExpand all lines: test/js-native-api/test_typedarray/test.js
+13Lines changed: 13 additions & 0 deletions
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+
}
Collapse file

‎test/js-native-api/test_typedarray/test_typedarray.c‎

Copy file name to clipboardExpand all lines: test/js-native-api/test_typedarray/test_typedarray.c
+32Lines changed: 32 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.