Skip to content

Commit 83e225b

Browse files
legendecastargos
authored andcommitted
n-api: add napi_detach_arraybuffer
As ArrayBuffer#detach is an ecma spec operation ([Section 24.1.1.3](https://tc39.es/ecma262/#sec-detacharraybuffer)), it might be good to have it in N-API. Fixes #29674 PR-URL: #29768 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
1 parent fc29cf9 commit 83e225b

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed

doc/api/n-api.md

+27
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,8 @@ typedef enum {
453453
napi_closing,
454454
napi_bigint_expected,
455455
napi_date_expected,
456+
napi_arraybuffer_expected,
457+
napi_detachable_arraybuffer_expected,
456458
} napi_status;
457459
```
458460

@@ -3232,6 +3234,30 @@ Returns `napi_ok` if the API succeeded.
32323234
This API represents the invocation of the Strict Equality algorithm as
32333235
defined in [Section 7.2.14][] of the ECMAScript Language Specification.
32343236

3237+
### napi_detach_arraybuffer
3238+
<!-- YAML
3239+
added: REPLACEME
3240+
-->
3241+
3242+
```C
3243+
napi_status napi_detach_arraybuffer(napi_env env,
3244+
napi_value arraybuffer)
3245+
```
3246+
3247+
* `[in] env`: The environment that the API is invoked under.
3248+
* `[in] arraybuffer`: The JavaScript `ArrayBuffer` to be detached.
3249+
3250+
Returns `napi_ok` if the API succeeded. If a non-detachable `ArrayBuffer` is
3251+
passed in it returns `napi_detachable_arraybuffer_expected`.
3252+
3253+
Generally, an `ArrayBuffer` is non-detachable if it has been detached before.
3254+
The engine may impose additional conditions on whether an `ArrayBuffer` is
3255+
detachable. For example, V8 requires that the `ArrayBuffer` be external,
3256+
that is, created with [`napi_create_external_arraybuffer`][].
3257+
3258+
This API represents the invocation of the `ArrayBuffer` detach operation as
3259+
defined in [Section 24.1.1.3][] of the ECMAScript Language Specification.
3260+
32353261
## Working with JavaScript Properties
32363262

32373263
N-API exposes a set of APIs to get and set properties on JavaScript
@@ -5216,6 +5242,7 @@ This API may only be called from the main thread.
52165242
[Section 22.1]: https://tc39.github.io/ecma262/#sec-array-objects
52175243
[Section 22.2]: https://tc39.github.io/ecma262/#sec-typedarray-objects
52185244
[Section 24.1]: https://tc39.github.io/ecma262/#sec-arraybuffer-objects
5245+
[Section 24.1.1.3]: https://tc39.es/ecma262/#sec-detacharraybuffer
52195246
[Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects
52205247
[Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects
52215248
[Section 6.1.4]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type

src/js_native_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ NAPI_EXTERN napi_status napi_set_instance_data(napi_env env,
514514

515515
NAPI_EXTERN napi_status napi_get_instance_data(napi_env env,
516516
void** data);
517+
518+
// ArrayBuffer detaching
519+
NAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env,
520+
napi_value arraybuffer);
517521
#endif // NAPI_EXPERIMENTAL
518522

519523
EXTERN_C_END

src/js_native_api_types.h

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ typedef enum {
8080
napi_closing,
8181
napi_bigint_expected,
8282
napi_date_expected,
83+
napi_arraybuffer_expected,
84+
napi_detachable_arraybuffer_expected,
8385
} napi_status;
8486
// Note: when adding a new enum value to `napi_status`, please also update
8587
// `const int last_status` in `napi_get_last_error_info()' definition,

src/js_native_api_v8.cc

+22-1
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,8 @@ const char* error_messages[] = {nullptr,
703703
"Thread-safe function handle is closing",
704704
"A bigint was expected",
705705
"A date was expected",
706+
"An arraybuffer was expected",
707+
"A detachable arraybuffer was expected",
706708
};
707709

708710
napi_status napi_get_last_error_info(napi_env env,
@@ -714,7 +716,7 @@ napi_status napi_get_last_error_info(napi_env env,
714716
// message in the `napi_status` enum each time a new error message is added.
715717
// We don't have a napi_status_last as this would result in an ABI
716718
// change each time a message was added.
717-
const int last_status = napi_date_expected;
719+
const int last_status = napi_detachable_arraybuffer_expected;
718720

719721
static_assert(
720722
NAPI_ARRAYSIZE(error_messages) == last_status + 1,
@@ -3037,3 +3039,22 @@ napi_status napi_get_instance_data(napi_env env,
30373039

30383040
return napi_clear_last_error(env);
30393041
}
3042+
3043+
napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) {
3044+
CHECK_ENV(env);
3045+
CHECK_ARG(env, arraybuffer);
3046+
3047+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
3048+
RETURN_STATUS_IF_FALSE(
3049+
env, value->IsArrayBuffer(), napi_arraybuffer_expected);
3050+
3051+
v8::Local<v8::ArrayBuffer> it = value.As<v8::ArrayBuffer>();
3052+
RETURN_STATUS_IF_FALSE(
3053+
env, it->IsExternal(), napi_detachable_arraybuffer_expected);
3054+
RETURN_STATUS_IF_FALSE(
3055+
env, it->IsDetachable(), napi_detachable_arraybuffer_expected);
3056+
3057+
it->Detach();
3058+
3059+
return napi_clear_last_error(env);
3060+
}

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

+18
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,21 @@ nonByteArrayTypes.forEach((currentType) => {
7474
console.log(`start of offset ${currentType}`);
7575
}, RangeError);
7676
});
77+
78+
// Test detaching
79+
arrayTypes.forEach((currentType) => {
80+
const buffer = Reflect.construct(currentType, [8]);
81+
assert.throws(
82+
() => test_typedarray.Detach(buffer),
83+
/A detachable arraybuffer was expected/);
84+
});
85+
{
86+
const buffer = test_typedarray.External();
87+
assert.ok(externalResult instanceof Int8Array);
88+
assert.strictEqual(externalResult.length, 3);
89+
assert.strictEqual(externalResult.byteLength, 3);
90+
test_typedarray.Detach(buffer);
91+
assert.ok(externalResult instanceof Int8Array);
92+
assert.strictEqual(buffer.length, 0);
93+
assert.strictEqual(buffer.byteLength, 0);
94+
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define NAPI_EXPERIMENTAL
12
#include <js_native_api.h>
23
#include <string.h>
34
#include "../common.h"
@@ -165,12 +166,30 @@ static napi_value CreateTypedArray(napi_env env, napi_callback_info info) {
165166
return output_array;
166167
}
167168

169+
static napi_value Detach(napi_env env, napi_callback_info info) {
170+
size_t argc = 1;
171+
napi_value args[1];
172+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
173+
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments.");
174+
175+
bool is_typedarray;
176+
NAPI_CALL(env, napi_is_typedarray(env, args[0], &is_typedarray));
177+
NAPI_ASSERT(env, is_typedarray, "Wrong type of arguments. Expects a typedarray as first argument.");
178+
179+
napi_value arraybuffer;
180+
NAPI_CALL(env, napi_get_typedarray_info(env, args[0], NULL, NULL, NULL, &arraybuffer, NULL));
181+
NAPI_CALL(env, napi_detach_arraybuffer(env, arraybuffer));
182+
183+
return NULL;
184+
}
185+
168186
EXTERN_C_START
169187
napi_value Init(napi_env env, napi_value exports) {
170188
napi_property_descriptor descriptors[] = {
171189
DECLARE_NAPI_PROPERTY("Multiply", Multiply),
172190
DECLARE_NAPI_PROPERTY("External", External),
173191
DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray),
192+
DECLARE_NAPI_PROPERTY("Detach", Detach),
174193
};
175194

176195
NAPI_CALL(env, napi_define_properties(

0 commit comments

Comments
 (0)