Skip to content

Commit 60e0f2b

Browse files
shivanthaddaleax
authored andcommitted
n-api: add support for DataView
Basic support for Dataview is added in this commit. This is achieved by using three functions, napi_create_dataview(), napi_is_dataview() and napi_get_dataview_info(). PR-URL: #14382 Fixes: #13926 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent b849b3d commit 60e0f2b

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

doc/api/n-api.md

+84
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,40 @@ JavaScript TypedArray Objects are described in
13311331
[Section 22.2](https://tc39.github.io/ecma262/#sec-typedarray-objects)
13321332
of the ECMAScript Language Specification.
13331333

1334+
1335+
#### *napi_create_dataview*
1336+
<!-- YAML
1337+
added: REPLACEME
1338+
-->
1339+
1340+
```C
1341+
napi_status napi_create_dataview(napi_env env,
1342+
size_t byte_length,
1343+
napi_value arraybuffer,
1344+
size_t byte_offset,
1345+
napi_value* result)
1346+
1347+
```
1348+
1349+
- `[in] env`: The environment that the API is invoked under.
1350+
- `[in] length`: Number of elements in the DataView.
1351+
- `[in] arraybuffer`: ArrayBuffer underlying the DataView.
1352+
- `[in] byte_offset`: The byte offset within the ArrayBuffer from which to
1353+
start projecting the DataView.
1354+
- `[out] result`: A `napi_value` representing a JavaScript DataView.
1355+
1356+
Returns `napi_ok` if the API succeeded.
1357+
1358+
This API creates a JavaScript DataView object over an existing ArrayBuffer.
1359+
DataView objects provide an array-like view over an underlying data buffer,
1360+
but one which allows items of different size and type in the ArrayBuffer.
1361+
1362+
It is required that `byte_length + byte_offset` is less than or equal to the
1363+
size in bytes of the array passed in. If not, a RangeError exception is raised.
1364+
1365+
JavaScript DataView Objects are described in
1366+
[Section 24.3][] of the ECMAScript Language Specification.
1367+
13341368
### Functions to convert from C types to N-API
13351369
#### *napi_create_number*
13361370
<!-- YAML
@@ -1552,6 +1586,36 @@ This API returns various properties of a typed array.
15521586
*Warning*: Use caution while using this API since the underlying data buffer
15531587
is managed by the VM
15541588

1589+
1590+
1591+
#### *napi_get_dataview_info*
1592+
<!-- YAML
1593+
added: REPLACEME
1594+
-->
1595+
1596+
```C
1597+
napi_status napi_get_dataview_info(napi_env env,
1598+
napi_value dataview,
1599+
size_t* byte_length,
1600+
void** data,
1601+
napi_value* arraybuffer,
1602+
size_t* byte_offset)
1603+
```
1604+
1605+
- `[in] env`: The environment that the API is invoked under.
1606+
- `[in] dataview`: `napi_value` representing the DataView whose
1607+
properties to query.
1608+
- `[out] byte_length`: Number of bytes in the DataView.
1609+
- `[out] data`: The data buffer underlying the DataView.
1610+
- `[out] arraybuffer`: ArrayBuffer underlying the DataView.
1611+
- `[out] byte_offset`: The byte offset within the data buffer from which
1612+
to start projecting the DataView.
1613+
1614+
Returns `napi_ok` if the API succeeded.
1615+
1616+
This API returns various properties of a DataView.
1617+
1618+
15551619
#### *napi_get_value_bool*
15561620
<!-- YAML
15571621
added: v8.0.0
@@ -2019,6 +2083,25 @@ Returns `napi_ok` if the API succeeded.
20192083

20202084
This API checks if the Object passsed in is a typed array.
20212085

2086+
2087+
2088+
### *napi_is_dataview*
2089+
<!-- YAML
2090+
added: REPLACEME
2091+
-->
2092+
2093+
```C
2094+
napi_status napi_is_dataview(napi_env env, napi_value value, bool* result)
2095+
```
2096+
2097+
- `[in] env`: The environment that the API is invoked under.
2098+
- `[in] value`: The JavaScript value to check.
2099+
- `[out] result`: Whether the given `napi_value` represents a DataView.
2100+
2101+
Returns `napi_ok` if the API succeeded.
2102+
2103+
This API checks if the Object passed in is a DataView.
2104+
20222105
### *napi_strict_equals*
20232106
<!-- YAML
20242107
added: v8.0.0
@@ -3165,6 +3248,7 @@ support it:
31653248
[Object Wrap]: #n_api_object_wrap
31663249
[Section 9.1.6]: https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-defineownproperty-p-desc
31673250
[Section 12.5.5]: https://tc39.github.io/ecma262/#sec-typeof-operator
3251+
[Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects
31683252
[Working with JavaScript Functions]: #n_api_working_with_javascript_functions
31693253
[Working with JavaScript Properties]: #n_api_working_with_javascript_properties
31703254
[Working with JavaScript Values]: #n_api_working_with_javascript_values

src/node_api.cc

+66
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,72 @@ napi_status napi_get_typedarray_info(napi_env env,
30163016
return napi_clear_last_error(env);
30173017
}
30183018

3019+
napi_status napi_create_dataview(napi_env env,
3020+
size_t byte_length,
3021+
napi_value arraybuffer,
3022+
size_t byte_offset,
3023+
napi_value* result) {
3024+
NAPI_PREAMBLE(env);
3025+
CHECK_ARG(env, arraybuffer);
3026+
CHECK_ARG(env, result);
3027+
3028+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer);
3029+
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg);
3030+
3031+
v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>();
3032+
v8::Local<v8::DataView> DataView = v8::DataView::New(buffer, byte_offset,
3033+
byte_length);
3034+
3035+
*result = v8impl::JsValueFromV8LocalValue(DataView);
3036+
return GET_RETURN_STATUS(env);
3037+
}
3038+
3039+
napi_status napi_is_dataview(napi_env env, napi_value value, bool* result) {
3040+
CHECK_ENV(env);
3041+
CHECK_ARG(env, value);
3042+
CHECK_ARG(env, result);
3043+
3044+
v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);
3045+
*result = val->IsDataView();
3046+
3047+
return napi_clear_last_error(env);
3048+
}
3049+
3050+
napi_status napi_get_dataview_info(napi_env env,
3051+
napi_value dataview,
3052+
size_t* byte_length,
3053+
void** data,
3054+
napi_value* arraybuffer,
3055+
size_t* byte_offset) {
3056+
CHECK_ENV(env);
3057+
CHECK_ARG(env, dataview);
3058+
3059+
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(dataview);
3060+
RETURN_STATUS_IF_FALSE(env, value->IsDataView(), napi_invalid_arg);
3061+
3062+
v8::Local<v8::DataView> array = value.As<v8::DataView>();
3063+
3064+
if (byte_length != nullptr) {
3065+
*byte_length = array->ByteLength();
3066+
}
3067+
3068+
v8::Local<v8::ArrayBuffer> buffer = array->Buffer();
3069+
if (data != nullptr) {
3070+
*data = static_cast<uint8_t*>(buffer->GetContents().Data()) +
3071+
array->ByteOffset();
3072+
}
3073+
3074+
if (arraybuffer != nullptr) {
3075+
*arraybuffer = v8impl::JsValueFromV8LocalValue(buffer);
3076+
}
3077+
3078+
if (byte_offset != nullptr) {
3079+
*byte_offset = array->ByteOffset();
3080+
}
3081+
3082+
return napi_clear_last_error(env);
3083+
}
3084+
30193085
napi_status napi_get_version(napi_env env, uint32_t* result) {
30203086
CHECK_ENV(env);
30213087
CHECK_ARG(env, result);

src/node_api.h

+15
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,21 @@ NAPI_EXTERN napi_status napi_get_typedarray_info(napi_env env,
494494
napi_value* arraybuffer,
495495
size_t* byte_offset);
496496

497+
NAPI_EXTERN napi_status napi_create_dataview(napi_env env,
498+
size_t length,
499+
napi_value arraybuffer,
500+
size_t byte_offset,
501+
napi_value* result);
502+
NAPI_EXTERN napi_status napi_is_dataview(napi_env env,
503+
napi_value value,
504+
bool* result);
505+
NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env,
506+
napi_value dataview,
507+
size_t* bytelength,
508+
void** data,
509+
napi_value* arraybuffer,
510+
size_t* byte_offset);
511+
497512
// Methods to manage simple async operations
498513
NAPI_EXTERN
499514
napi_status napi_create_async_work(napi_env env,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "test_dataview",
5+
"sources": [ "test_dataview.c" ]
6+
}
7+
]
8+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
const common = require('../../common');
3+
const assert = require('assert');
4+
5+
// Testing api calls for arrays
6+
const test_dataview = require(`./build/${common.buildType}/test_dataview`);
7+
8+
//create dataview
9+
const buffer = new ArrayBuffer(128);
10+
const template = Reflect.construct(DataView, [buffer]);
11+
12+
const theDataview = test_dataview.CreateDataView(template);
13+
assert.ok(theDataview instanceof DataView,
14+
'The new variable should be of type Dataview');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <node_api.h>
2+
#include <string.h>
3+
#include "../common.h"
4+
5+
napi_value CreateDataView(napi_env env, napi_callback_info info) {
6+
size_t argc = 1;
7+
napi_value args [1];
8+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
9+
10+
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
11+
12+
napi_valuetype valuetype;
13+
napi_value input_dataview = args[0];
14+
15+
NAPI_CALL(env, napi_typeof(env, input_dataview, &valuetype));
16+
NAPI_ASSERT(env, valuetype == napi_object,
17+
"Wrong type of arguments. Expects a DataView as the first "
18+
"argument.");
19+
20+
bool is_dataview;
21+
NAPI_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview));
22+
NAPI_ASSERT(env, is_dataview,
23+
"Wrong type of arguments. Expects a DataView as the first "
24+
"argument.");
25+
size_t byte_offset = 0;
26+
size_t length = 0;
27+
napi_value buffer;
28+
NAPI_CALL(env,
29+
napi_get_dataview_info(env, input_dataview, &length, NULL,
30+
&buffer, &byte_offset));
31+
32+
napi_value output_dataview;
33+
NAPI_CALL(env,
34+
napi_create_dataview(env, length, buffer,
35+
byte_offset, &output_dataview));
36+
37+
return output_dataview;
38+
}
39+
40+
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
41+
napi_property_descriptor descriptors[] = {
42+
DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView)
43+
};
44+
45+
NAPI_CALL_RETURN_VOID(env, napi_define_properties(
46+
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
47+
}
48+
49+
NAPI_MODULE(addon, Init)

0 commit comments

Comments
 (0)