GeezerVison is showing github.com — n-api: implement napi_is_detached_arraybuffer · nodejs/node@b744ffd
File treeExpand file treeCollapse file tree Open diff view settings test/addons-napi/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 |
|---|
@@ -2972,6 +2972,31 @@ that is, created with [`napi_create_external_arraybuffer`][]. | 2972 | 2972 | This API represents the invocation of the `ArrayBuffer` detach operation as | 2973 | 2973 | defined in [Section 24.1.1.3][] of the ECMAScript Language Specification. | 2974 | 2974 |
| | 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 | + | 2975 | 3000 | ## Working with JavaScript Properties | 2976 | 3001 |
| 2977 | 3002 | 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. | 4924 | 4949 | [Section 8.7]: https://tc39.es/ecma262/#sec-agents | 4925 | 4950 | [Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc | 4926 | 4951 | [Working with JavaScript Functions]: #n_api_working_with_javascript_functions | | 4952 | +[Section 24.1.1.2]: https://tc39.es/ecma262/#sec-isdetachedbuffer | 4927 | 4953 | [Working with JavaScript Properties]: #n_api_working_with_javascript_properties | 4928 | 4954 | [Working with JavaScript Values - Abstract Operations]: #n_api_working_with_javascript_values_abstract_operations | 4929 | 4955 | [Working with JavaScript Values]: #n_api_working_with_javascript_values | |
Collapse file Copy file name to clipboardExpand all lines: src/node_api.cc +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4403,3 +4403,18 @@ napi_status napi_detach_arraybuffer(napi_env env, napi_value arraybuffer) { | 4403 | 4403 |
| 4404 | 4404 | return napi_clear_last_error(env); | 4405 | 4405 | } | | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/node_api.h +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -752,6 +752,11 @@ napi_get_all_property_names(napi_env env, | 752 | 752 | NAPI_EXTERN napi_status | 753 | 753 | napi_detach_arraybuffer(napi_env env, | 754 | 754 | napi_value arraybuffer); | | 755 | + | | 756 | +NAPI_EXTERN napi_status | | 757 | +napi_is_detached_arraybuffer(napi_env env, | | 758 | + napi_value value, | | 759 | + bool* result); | 755 | 760 | #endif // NAPI_EXPERIMENTAL | 756 | 761 |
| 757 | 762 | EXTERN_C_END | |
Collapse file Copy file name to clipboardExpand all lines: test/addons-napi/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/addons-napi/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,12 +192,35 @@ 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 | static napi_value Init(napi_env env, napi_value exports) { | 187 | 217 | napi_property_descriptor descriptors[] = { | 188 | 218 | DECLARE_NAPI_PROPERTY("Multiply", Multiply), | 189 | 219 | DECLARE_NAPI_PROPERTY("External", External), | | 220 | + DECLARE_NAPI_PROPERTY("NullArrayBuffer", NullArrayBuffer), | 190 | 221 | DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray), | 191 | 222 | DECLARE_NAPI_PROPERTY("Detach", Detach), | | 223 | + DECLARE_NAPI_PROPERTY("IsDetached", IsDetached), | 192 | 224 | }; | 193 | 225 |
| 194 | 226 | NAPI_CALL(env, napi_define_properties( | |
|