| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fc29cf9 commit 83e225b
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -453,6 +453,8 @@ typedef enum { | |||
| 453 | 453 | napi_closing, | |
| 454 | 454 | napi_bigint_expected, | |
| 455 | 455 | napi_date_expected, | |
| 456 | + napi_arraybuffer_expected, | ||
| 457 | + napi_detachable_arraybuffer_expected, | ||
| 456 | 458 | } napi_status; | |
| 457 | 459 | ``` | |
| 458 | 460 | ||
@@ -3232,6 +3234,30 @@ Returns `napi_ok` if the API succeeded. | |||
| 3232 | 3234 | This API represents the invocation of the Strict Equality algorithm as | |
| 3233 | 3235 | defined in [Section 7.2.14][] of the ECMAScript Language Specification. | |
| 3234 | 3236 | ||
| 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 | + | ||
| 3235 | 3261 | ## Working with JavaScript Properties | |
| 3236 | 3262 | ||
| 3237 | 3263 | 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. | |||
| 5216 | 5242 | [Section 22.1]: https://tc39.github.io/ecma262/#sec-array-objects | |
| 5217 | 5243 | [Section 22.2]: https://tc39.github.io/ecma262/#sec-typedarray-objects | |
| 5218 | 5244 | [Section 24.1]: https://tc39.github.io/ecma262/#sec-arraybuffer-objects | |
| 5245 | + [Section 24.1.1.3]: https://tc39.es/ecma262/#sec-detacharraybuffer | ||
| 5219 | 5246 | [Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects | |
| 5220 | 5247 | [Section 25.4]: https://tc39.github.io/ecma262/#sec-promise-objects | |
| 5221 | 5248 | [Section 6.1.4]: https://tc39.github.io/ecma262/#sec-ecmascript-language-types-string-type | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -514,6 +514,10 @@ NAPI_EXTERN napi_status napi_set_instance_data(napi_env env, | |||
| 514 | 514 | ||
| 515 | 515 | NAPI_EXTERN napi_status napi_get_instance_data(napi_env env, | |
| 516 | 516 | void** data); | |
| 517 | + | ||
| 518 | + // ArrayBuffer detaching | ||
| 519 | + NAPI_EXTERN napi_status napi_detach_arraybuffer(napi_env env, | ||
| 520 | + napi_value arraybuffer); | ||
| 517 | 521 | #endif // NAPI_EXPERIMENTAL | |
| 518 | 522 | ||
| 519 | 523 | EXTERN_C_END | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,6 +80,8 @@ typedef enum { | |||
| 80 | 80 | napi_closing, | |
| 81 | 81 | napi_bigint_expected, | |
| 82 | 82 | napi_date_expected, | |
| 83 | + napi_arraybuffer_expected, | ||
| 84 | + napi_detachable_arraybuffer_expected, | ||
| 83 | 85 | } napi_status; | |
| 84 | 86 | // Note: when adding a new enum value to `napi_status`, please also update | |
| 85 | 87 | // `const int last_status` in `napi_get_last_error_info()' definition, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -703,6 +703,8 @@ const char* error_messages[] = {nullptr, | |||
| 703 | 703 | "Thread-safe function handle is closing", | |
| 704 | 704 | "A bigint was expected", | |
| 705 | 705 | "A date was expected", | |
| 706 | + "An arraybuffer was expected", | ||
| 707 | + "A detachable arraybuffer was expected", | ||
| 706 | 708 | }; | |
| 707 | 709 | ||
| 708 | 710 | napi_status napi_get_last_error_info(napi_env env, | |
@@ -714,7 +716,7 @@ napi_status napi_get_last_error_info(napi_env env, | |||
| 714 | 716 | // message in the `napi_status` enum each time a new error message is added. | |
| 715 | 717 | // We don't have a napi_status_last as this would result in an ABI | |
| 716 | 718 | // change each time a message was added. | |
| 717 | - const int last_status = napi_date_expected; | ||
| 719 | + const int last_status = napi_detachable_arraybuffer_expected; | ||
| 718 | 720 | ||
| 719 | 721 | static_assert( | |
| 720 | 722 | NAPI_ARRAYSIZE(error_messages) == last_status + 1, | |
@@ -3037,3 +3039,22 @@ napi_status napi_get_instance_data(napi_env env, | |||
| 3037 | 3039 | ||
| 3038 | 3040 | return napi_clear_last_error(env); | |
| 3039 | 3041 | } | |
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,3 +74,21 @@ nonByteArrayTypes.forEach((currentType) => { | |||
| 74 | 74 | console.log(`start of offset ${currentType}`); | |
| 75 | 75 | }, RangeError); | |
| 76 | 76 | }); | |
| 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + #define NAPI_EXPERIMENTAL | ||
| 1 | 2 | #include <js_native_api.h> | |
| 2 | 3 | #include <string.h> | |
| 3 | 4 | #include "../common.h" | |
@@ -165,12 +166,30 @@ static napi_value CreateTypedArray(napi_env env, napi_callback_info info) { | |||
| 165 | 166 | return output_array; | |
| 166 | 167 | } | |
| 167 | 168 | ||
| 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 | + | ||
| 168 | 186 | EXTERN_C_START | |
| 169 | 187 | napi_value Init(napi_env env, napi_value exports) { | |
| 170 | 188 | napi_property_descriptor descriptors[] = { | |
| 171 | 189 | DECLARE_NAPI_PROPERTY("Multiply", Multiply), | |
| 172 | 190 | DECLARE_NAPI_PROPERTY("External", External), | |
| 173 | 191 | DECLARE_NAPI_PROPERTY("CreateTypedArray", CreateTypedArray), | |
| 192 | + DECLARE_NAPI_PROPERTY("Detach", Detach), | ||
| 174 | 193 | }; | |
| 175 | 194 | ||
| 176 | 195 | NAPI_CALL(env, napi_define_properties( | |
| Back | FazBrowse Home | New Git URL |
0 commit comments