-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
N api dataview #14382
N api dataview #14382
Changes from 7 commits
43ec67d
b56c76b
3839165
5fc5ffb
260ad4e
11362b5
b5fcb56
3fdc71e
dcac0ac
afca265
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1331,6 +1331,39 @@ JavaScript TypedArray Objects are described in | |
[Section 22.2](https://tc39.github.io/ecma262/#sec-typedarray-objects) | ||
of the ECMAScript Language Specification. | ||
|
||
|
||
#### *napi_create_dataview* | ||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
```C | ||
napi_status napi_create_dataview(napi_env env, | ||
size_t length, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
napi_value arraybuffer, | ||
size_t byte_offset, | ||
napi_value* result) | ||
|
||
``` | ||
|
||
- `[in] env`: The environment that the API is invoked under. | ||
- `[in] length`: Number of elements in the DataView. | ||
- `[in] arraybuffer`: ArrayBuffer underlying the DataView. | ||
- `[in] byte_offset`: The byte offset within the ArrayBuffer from which to | ||
start projecting the DataView. | ||
- `[out] result`: A `napi_value` representing a JavaScript DataView. | ||
|
||
Returns `napi_ok` if the API succeeded. | ||
|
||
This API creates a JavaScript DataView object over an existing ArrayBuffer. | ||
Dataview objects provide an array-like view over an underlying data buffer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Capitalization: |
||
but one which allows items of different size and type in the ArrayBuffer. | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There should one here as well, with "(length * size_of_element)" replaced with |
||
JavaScript DataView Objects are described in | ||
[Section 24.3](https://tc39.github.io/ecma262/#sec-dataview-objects) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move the link to the bottom of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that every napi_create_x function refers to the ECMA standards within it's section through out the document. Shouldn't we stick to this format ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should now just be |
||
of the ECMAScript Language Specification. | ||
|
||
### Functions to convert from C types to N-API | ||
#### *napi_create_number* | ||
<!-- YAML | ||
|
@@ -1552,6 +1585,36 @@ This API returns various properties of a typed array. | |
*Warning*: Use caution while using this API since the underlying data buffer | ||
is managed by the VM | ||
|
||
|
||
|
||
#### *napi_get_dataview_info* | ||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
```C | ||
napi_status napi_get_dataview_info(napi_env env, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments here |
||
napi_value dataview, | ||
size_t* bytelength, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
void** data, | ||
napi_value* arraybuffer, | ||
size_t* byte_offset) | ||
``` | ||
|
||
- `[in] env`: The environment that the API is invoked under. | ||
- `[in] dataview`: `napi_value` representing the DataView whose | ||
properties to query. | ||
- `[out] bytelength`: Number of bytes in the DataView. | ||
- `[out] data`: The data buffer underlying the DataView. | ||
- `[out] arraybuffer`: ArrayBuffer underlying the DataView. | ||
- `[out] byte_offset`: The byte offset within the data buffer from which | ||
to start projecting the DataView. | ||
|
||
Returns `napi_ok` if the API succeeded. | ||
|
||
This API returns various properties of a DataView. | ||
|
||
|
||
#### *napi_get_value_bool* | ||
<!-- YAML | ||
added: v8.0.0 | ||
|
@@ -2019,6 +2082,25 @@ Returns `napi_ok` if the API succeeded. | |
|
||
This API checks if the Object passsed in is a typed array. | ||
|
||
|
||
|
||
### *napi_is_dataview* | ||
<!-- YAML | ||
added: REPLACEME | ||
--> | ||
|
||
```C | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (ditto) |
||
napi_status napi_is_dataview(napi_env env, napi_value value, bool* result) | ||
``` | ||
|
||
- `[in] env`: The environment that the API is invoked under. | ||
- `[in] value`: The JavaScript value to check. | ||
- `[out] result`: Whether the given `napi_value` represents a DataView. | ||
|
||
Returns `napi_ok` if the API succeeded. | ||
|
||
This API checks if the Object passsed in is a DataView. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spelling: |
||
|
||
### *napi_strict_equals* | ||
<!-- YAML | ||
added: v8.0.0 | ||
|
@@ -3169,6 +3251,7 @@ support it: | |
[Working with JavaScript Properties]: #n_api_working_with_javascript_properties | ||
[Working with JavaScript Values]: #n_api_working_with_javascript_values | ||
[Working with JavaScript Values - Abstract Operations]: #n_api_working_with_javascript_values_abstract_operations | ||
[Section 24.3]: https://tc39.github.io/ecma262/#sec-dataview-objects | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move this up a few lines to keep the links sorted. This should come after the Section 12.5.5 link. |
||
|
||
[`napi_cancel_async_work`]: #n_api_napi_cancel_async_work | ||
[`napi_close_escapable_handle_scope`]: #n_api_napi_close_escapable_handle_scope | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2957,6 +2957,73 @@ napi_status napi_get_typedarray_info(napi_env env, | |
return napi_clear_last_error(env); | ||
} | ||
|
||
napi_status napi_create_dataview(napi_env env, | ||
size_t length, | ||
napi_value arraybuffer, | ||
size_t byte_offset, | ||
napi_value* result) { | ||
NAPI_PREAMBLE(env); | ||
CHECK_ARG(env, arraybuffer); | ||
CHECK_ARG(env, result); | ||
|
||
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer); | ||
RETURN_STATUS_IF_FALSE(env, value->IsArrayBuffer(), napi_invalid_arg); | ||
|
||
v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>(); | ||
v8::Local<v8::DataView> DataView = v8::DataView::New(buffer, byte_offset, | ||
length); | ||
|
||
*result = v8impl::JsValueFromV8LocalValue(DataView); | ||
return GET_RETURN_STATUS(env); | ||
} | ||
|
||
napi_status napi_is_dataview(napi_env env, napi_value value, bool* result) { | ||
CHECK_ENV(env); | ||
CHECK_ARG(env, value); | ||
CHECK_ARG(env, result); | ||
|
||
v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value); | ||
*result = val->IsDataView(); | ||
|
||
return napi_clear_last_error(env); | ||
} | ||
|
||
napi_status napi_get_dataview_info(napi_env env, | ||
napi_value dataview, | ||
size_t* bytelength, | ||
void** data, | ||
napi_value* arraybuffer, | ||
size_t* byte_offset) { | ||
CHECK_ENV(env); | ||
CHECK_ARG(env, dataview); | ||
|
||
v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(dataview); | ||
RETURN_STATUS_IF_FALSE(env, value->IsDataView(), napi_invalid_arg); | ||
|
||
v8::Local<v8::DataView> array = value.As<v8::DataView>(); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I would try to avoid multiple consecutive empty lines within functions. It usually does not improve readability. |
||
if (bytelength != nullptr) { | ||
*bytelength = array->ByteLength(); | ||
} | ||
|
||
v8::Local<v8::ArrayBuffer> buffer = array->Buffer(); | ||
if (data != nullptr) { | ||
*data = static_cast<uint8_t*>(buffer->GetContents().Data()) + | ||
array->ByteOffset(); | ||
} | ||
|
||
if (arraybuffer != nullptr) { | ||
*arraybuffer = v8impl::JsValueFromV8LocalValue(buffer); | ||
} | ||
|
||
if (byte_offset != nullptr) { | ||
*byte_offset = array->ByteOffset(); | ||
} | ||
|
||
return napi_clear_last_error(env); | ||
} | ||
|
||
napi_status napi_get_version(napi_env env, uint32_t* result) { | ||
CHECK_ENV(env); | ||
CHECK_ARG(env, result); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_dataview", | ||
"sources": [ "test_dataview.c" ] | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
|
||
// Testing api calls for arrays | ||
const test_dataview = require(`./build/${common.buildType}/test_dataview`); | ||
|
||
//create dataview | ||
const buffer = new ArrayBuffer(128); | ||
const template = Reflect.construct(DataView, [buffer]); | ||
|
||
const theDataview = test_dataview.CreateDataView(template); | ||
assert.ok(theDataview instanceof DataView, | ||
'The new variable should be of type Dataview'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <node_api.h> | ||
#include <string.h> | ||
#include "../common.h" | ||
|
||
napi_value CreateDataView(napi_env env, napi_callback_info info) { | ||
size_t argc = 1; | ||
napi_value args [1]; | ||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
||
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments"); | ||
|
||
napi_valuetype valuetype; | ||
napi_value input_dataview = args[0]; | ||
|
||
NAPI_CALL(env, napi_typeof(env, input_dataview, &valuetype)); | ||
NAPI_ASSERT(env, valuetype == napi_object, | ||
"Wrong type of argments. Expects a dataview as the first argument."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Align the arguments please, like so: NAPI_ASSERT(env, valuetype == napi_object,
"Wrong type of argument. Expects a DataView as the first "
"argument."); |
||
|
||
bool is_dataview; | ||
NAPI_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview)); | ||
NAPI_ASSERT(env,is_dataview, | ||
"Wrong type of arugments. Expects a dataview as first argument."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
size_t byte_offset = 0; | ||
size_t length = 0; | ||
napi_value buffer; | ||
NAPI_CALL(env, napi_get_dataview_info( | ||
env, input_dataview, &length, NULL, &buffer, &byte_offset)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAPI_CALL(env,
napi_get_dataview_info(env, input_dataview, &byte_length, NULL,
&buffer, &byte_offset)); |
||
|
||
napi_value output_dataview; | ||
NAPI_CALL(env, napi_create_dataview( | ||
env, length, buffer, byte_offset, &output_dataview)); | ||
|
||
return output_dataview; | ||
} | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { | ||
napi_property_descriptor descriptors[] = { | ||
DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView) | ||
}; | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_define_properties( | ||
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Four-space indentation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how do I format this 😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just add two more spaces, so that |
||
} | ||
|
||
NAPI_MODULE(addon, Init) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you leave a newline before this?