| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1176fe4 commit bdf359b
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2806,6 +2806,10 @@ exceeds the size of the `ArrayBuffer`, a `RangeError` exception is raised. | |||
| 2806 | 2806 | <!-- YAML | |
| 2807 | 2807 | added: v8.3.0 | |
| 2808 | 2808 | napiVersion: 1 | |
| 2809 | + changes: | ||
| 2810 | + - version: REPLACEME | ||
| 2811 | + pr-url: https://github.com/nodejs/node/pull/60473 | ||
| 2812 | + description: Added support for `SharedArrayBuffer`. | ||
| 2809 | 2813 | --> | |
| 2810 | 2814 | ||
| 2811 | 2815 | ```c | |
@@ -2818,16 +2822,18 @@ napi_status napi_create_dataview(napi_env env, | |||
| 2818 | 2822 | ||
| 2819 | 2823 | * `[in] env`: The environment that the API is invoked under. | |
| 2820 | 2824 | * `[in] length`: Number of elements in the `DataView`. | |
| 2821 | - * `[in] arraybuffer`: `ArrayBuffer` underlying the `DataView`. | ||
| 2825 | + * `[in] arraybuffer`: `ArrayBuffer` or `SharedArrayBuffer` underlying the | ||
| 2826 | + `DataView`. | ||
| 2822 | 2827 | * `[in] byte_offset`: The byte offset within the `ArrayBuffer` from which to | |
| 2823 | 2828 | start projecting the `DataView`. | |
| 2824 | 2829 | * `[out] result`: A `napi_value` representing a JavaScript `DataView`. | |
| 2825 | 2830 | ||
| 2826 | 2831 | Returns `napi_ok` if the API succeeded. | |
| 2827 | 2832 | ||
| 2828 | - This API creates a JavaScript `DataView` object over an existing `ArrayBuffer`. | ||
| 2829 | - `DataView` objects provide an array-like view over an underlying data buffer, | ||
| 2830 | - but one which allows items of different size and type in the `ArrayBuffer`. | ||
| 2833 | + This API creates a JavaScript `DataView` object over an existing `ArrayBuffer` | ||
| 2834 | + or `SharedArrayBuffer`. `DataView` objects provide an array-like view over an | ||
| 2835 | + underlying data buffer, but one which allows items of different size and type in | ||
| 2836 | + the `ArrayBuffer` or `SharedArrayBuffer`. | ||
| 2831 | 2837 | ||
| 2832 | 2838 | It is required that `byte_length + byte_offset` is less than or equal to the | |
| 2833 | 2839 | size in bytes of the array passed in. If not, a `RangeError` exception is | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3344,21 +3344,30 @@ napi_status NAPI_CDECL napi_create_dataview(napi_env env, | |||
| 3344 | 3344 | CHECK_ARG(env, result); | |
| 3345 | 3345 | ||
| 3346 | 3346 | v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer); | |
| 3347 | - RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg); | ||
| 3348 | 3347 | ||
| 3349 | - v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>(); | ||
| 3350 | - if (byte_length + byte_offset > buffer->ByteLength()) { | ||
| 3351 | - napi_throw_range_error(env, | ||
| 3352 | - "ERR_NAPI_INVALID_DATAVIEW_ARGS", | ||
| 3353 | - "byte_offset + byte_length should be less than or " | ||
| 3354 | - "equal to the size in bytes of the array passed in"); | ||
| 3355 | - return napi_set_last_error(env, napi_pending_exception); | ||
| 3356 | - } | ||
| 3357 | - v8::Local<v8::DataView> DataView = | ||
| 3358 | - v8::DataView::New(buffer, byte_offset, byte_length); | ||
| 3348 | + auto create_dataview = [&](auto buffer) -> napi_status { | ||
| 3349 | + if (byte_length + byte_offset > buffer->ByteLength()) { | ||
| 3350 | + napi_throw_range_error( | ||
| 3351 | + env, | ||
| 3352 | + "ERR_NAPI_INVALID_DATAVIEW_ARGS", | ||
| 3353 | + "byte_offset + byte_length should be less than or " | ||
| 3354 | + "equal to the size in bytes of the array passed in"); | ||
| 3355 | + return napi_set_last_error(env, napi_pending_exception); | ||
| 3356 | + } | ||
| 3359 | 3357 | ||
| 3360 | - *result = v8impl::JsValueFromV8LocalValue(DataView); | ||
| 3361 | - return GET_RETURN_STATUS(env); | ||
| 3358 | + v8::Local<v8::DataView> data_view = | ||
| 3359 | + v8::DataView::New(buffer, byte_offset, byte_length); | ||
| 3360 | + *result = v8impl::JsValueFromV8LocalValue(data_view); | ||
| 3361 | + return GET_RETURN_STATUS(env); | ||
| 3362 | + }; | ||
| 3363 | + | ||
| 3364 | + if (value->IsArrayBuffer()) { | ||
| 3365 | + return create_dataview(value.As<v8::ArrayBuffer>()); | ||
| 3366 | + } else if (value->IsSharedArrayBuffer()) { | ||
| 3367 | + return create_dataview(value.As<v8::SharedArrayBuffer>()); | ||
| 3368 | + } else { | ||
| 3369 | + return napi_set_last_error(env, napi_invalid_arg); | ||
| 3370 | + } | ||
| 3362 | 3371 | } | |
| 3363 | 3372 | ||
| 3364 | 3373 | napi_status NAPI_CDECL napi_is_dataview(napi_env env, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,10 @@ | |||
| 4 | 4 | "target_name": "test_dataview", | |
| 5 | 5 | "sources": [ | |
| 6 | 6 | "test_dataview.c" | |
| 7 | - ] | ||
| 7 | + ], | ||
| 8 | + | ||
| 9 | + # For node_api_is_sharedarraybuffer | ||
| 10 | + 'defines': [ 'NAPI_EXPERIMENTAL', 'NODE_API_EXPERIMENTAL_NO_WARNING' ] | ||
| 8 | 11 | } | |
| 9 | 12 | ] | |
| 10 | 13 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ const assert = require('assert'); | |||
| 5 | 5 | // Testing api calls for arrays | |
| 6 | 6 | const test_dataview = require(`./build/${common.buildType}/test_dataview`); | |
| 7 | 7 | ||
| 8 | - // Test for creating dataview | ||
| 8 | + // Test for creating dataview with ArrayBuffer | ||
| 9 | 9 | { | |
| 10 | 10 | const buffer = new ArrayBuffer(128); | |
| 11 | 11 | const template = Reflect.construct(DataView, [buffer]); | |
@@ -15,10 +15,30 @@ const test_dataview = require(`./build/${common.buildType}/test_dataview`); | |||
| 15 | 15 | `Expect ${theDataview} to be a DataView`); | |
| 16 | 16 | } | |
| 17 | 17 | ||
| 18 | - // Test for creating dataview with invalid range | ||
| 18 | + // Test for creating dataview with SharedArrayBuffer | ||
| 19 | + { | ||
| 20 | + const buffer = new SharedArrayBuffer(128); | ||
| 21 | + const template = new DataView(buffer); | ||
| 22 | + | ||
| 23 | + const theDataview = test_dataview.CreateDataViewFromJSDataView(template); | ||
| 24 | + assert.ok(theDataview instanceof DataView, | ||
| 25 | + `Expect ${theDataview} to be a DataView`); | ||
| 26 | + | ||
| 27 | + assert.strictEqual(template.buffer, theDataview.buffer); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + // Test for creating dataview with ArrayBuffer and invalid range | ||
| 19 | 31 | { | |
| 20 | 32 | const buffer = new ArrayBuffer(128); | |
| 21 | 33 | assert.throws(() => { | |
| 22 | 34 | test_dataview.CreateDataView(buffer, 10, 200); | |
| 23 | 35 | }, RangeError); | |
| 24 | 36 | } | |
| 37 | + | ||
| 38 | + // Test for creating dataview with SharedArrayBuffer and invalid range | ||
| 39 | + { | ||
| 40 | + const buffer = new SharedArrayBuffer(128); | ||
| 41 | + assert.throws(() => { | ||
| 42 | + test_dataview.CreateDataView(buffer, 10, 200); | ||
| 43 | + }, RangeError); | ||
| 44 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,9 +20,18 @@ static napi_value CreateDataView(napi_env env, napi_callback_info info) { | |||
| 20 | 20 | ||
| 21 | 21 | bool is_arraybuffer; | |
| 22 | 22 | NODE_API_CALL(env, napi_is_arraybuffer(env, arraybuffer, &is_arraybuffer)); | |
| 23 | - NODE_API_ASSERT(env, is_arraybuffer, | ||
| 24 | - "Wrong type of arguments. Expects a ArrayBuffer as the first " | ||
| 25 | - "argument."); | ||
| 23 | + | ||
| 24 | + if (!is_arraybuffer) { | ||
| 25 | + bool is_sharedarraybuffer; | ||
| 26 | + NODE_API_CALL( | ||
| 27 | + env, | ||
| 28 | + node_api_is_sharedarraybuffer(env, arraybuffer, &is_sharedarraybuffer)); | ||
| 29 | + NODE_API_ASSERT(env, | ||
| 30 | + is_sharedarraybuffer, | ||
| 31 | + "Wrong type of arguments. Expects a SharedArrayBuffer or " | ||
| 32 | + "ArrayBuffer as the first " | ||
| 33 | + "argument."); | ||
| 34 | + } | ||
| 26 | 35 | ||
| 27 | 36 | napi_valuetype valuetype1; | |
| 28 | 37 | NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1)); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments