GeezerVison is showing github.com — n-api: implement napi_is_detached_arraybuffer · nodejs/node@ea3d4e8
File treeExpand file treeCollapse file tree Open diff view settings test/js-native-api/test_typedarray
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -3259,6 +3259,31 @@ that is, created with [`napi_create_external_arraybuffer`][]. | 3259 | 3259 | This API represents the invocation of the `ArrayBuffer` detach operation as | 3260 | 3260 | defined in [Section 24.1.1.3][] of the ECMAScript Language Specification. | 3261 | 3261 |
| | 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 | + | 3262 | 3287 | ## Working with JavaScript Properties | 3263 | 3288 |
| 3264 | 3289 | 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. | 5272 | 5297 | [Section 7]: https://tc39.github.io/ecma262/#sec-abstract-operations | 5273 | 5298 | [Section 8.7]: https://tc39.es/ecma262/#sec-agents | 5274 | 5299 | [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 | 5275 | 5301 | [Travis CI]: https://travis-ci.org | 5276 | 5302 | [Visual Studio]: https://visualstudio.microsoft.com | 5277 | 5303 | [Working with JavaScript Properties]: #n_api_working_with_javascript_properties | |
Collapse file Copy file name to clipboardExpand all lines: src/js_native_api.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -518,6 +518,10 @@ NAPI_EXTERN napi_status napi_get_instance_data(napi_env env, | 518 | 518 | // ArrayBuffer detaching | 519 | 519 | NAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env, | 520 | 520 | napi_value arraybuffer); | | 521 | + | | 522 | +NAPI_EXTERN napi_status napi_is_detached_arraybuffer(napi_env env, | | 523 | + napi_value value, | | 524 | + bool* result); | 521 | 525 | #endif // NAPI_EXPERIMENTAL | 522 | 526 |
| 523 | 527 | EXTERN_C_END | |
Collapse file Copy file name to clipboardExpand all lines: src/js_native_api_v8.cc +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3060,3 +3060,18 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) { | 3060 | 3060 |
| 3061 | 3061 | return napi_clear_last_error(env); | 3062 | 3062 | } | | 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 Copy file name to clipboardExpand all lines: test/js-native-api/test_typedarray/test.js +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,8 +87,21 @@ arrayTypes.forEach((currentType) => { | 87 | 87 | assert.ok(externalResult instanceof Int8Array); | 88 | 88 | assert.strictEqual(externalResult.length, 3); | 89 | 89 | assert.strictEqual(externalResult.byteLength, 3); | | 90 | + assert.ok(!test_typedarray.IsDetached(buffer.buffer)); | 90 | 91 | test_typedarray.Detach(buffer); | | 92 | + assert.ok(test_typedarray.IsDetached(buffer.buffer)); | 91 | 93 | assert.ok(externalResult instanceof Int8Array); | 92 | 94 | assert.strictEqual(buffer.length, 0); | 93 | 95 | assert.strictEqual(buffer.byteLength, 0); | 94 | 96 | } | | 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 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 number | Diff line number | Diff line change |
|---|
@@ -97,6 +97,15 @@ static napi_value External(napi_env env, napi_callback_info info) { | 97 | 97 | return output_array; | 98 | 98 | } | 99 | 99 |
| | 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 | + | 100 | 109 | static napi_value CreateTypedArray(napi_env env, napi_callback_info info) { | 101 | 110 | size_t argc = 4; | 102 | 111 | napi_value args[4]; | @@ -183,13 +192,36 @@ static napi_value Detach(napi_env env, napi_callback_info info) { | 183 | 192 | return NULL; | 184 | 193 | } | 185 | 194 |
| | 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 | + | 186 | 216 | EXTERN_C_START | 187 | 217 | napi_value Init(napi_env env, napi_value exports) { | 188 | 218 | napi_property_descriptor descriptors[] = { | 189 | 219 | DECLARE_NAPI_PROPERTY("Multiply", Multiply), | 190 | 220 | DECLARE_NAPI_PROPERTY("External", External), | | 221 | + DECLARE_NAPI_PROPERTY("NullArrayBuffer", NullArrayBuffer), | 191 | 222 | DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray), | 192 | 223 | DECLARE_NAPI_PROPERTY("Detach", Detach), | | 224 | + DECLARE_NAPI_PROPERTY("IsDetached", IsDetached), | 193 | 225 | }; | 194 | 226 |
| 195 | 227 | NAPI_CALL(env, napi_define_properties( | |
|