From bd032a158aa5be0ecd5e1c1e6b1a535d6977bc83 Mon Sep 17 00:00:00 2001 From: Shivanth MP Date: Thu, 20 Jul 2017 01:50:43 +0530 Subject: [PATCH] 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(). Backport-PR-URL: https://github.com/nodejs/node/pull/19447 PR-URL: https://github.com/nodejs/node/pull/14382 Fixes: https://github.com/nodejs/node/issues/13926 Reviewed-By: Anna Henningsen Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig --- doc/api/n-api.md | 84 +++++++++++++++++++ src/node_api.cc | 66 +++++++++++++++ src/node_api.h | 15 ++++ test/addons-napi/test_dataview/binding.gyp | 8 ++ test/addons-napi/test_dataview/test.js | 14 ++++ .../addons-napi/test_dataview/test_dataview.c | 49 +++++++++++ 6 files changed, 236 insertions(+) create mode 100644 test/addons-napi/test_dataview/binding.gyp create mode 100644 test/addons-napi/test_dataview/test.js create mode 100644 test/addons-napi/test_dataview/test_dataview.c diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 91780846ff2e4a..8f8aa76f83ac7b 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1331,6 +1331,40 @@ 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* + + +```C +napi_status napi_create_dataview(napi_env env, + size_t byte_length, + 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, +but one which allows items of different size and type in the ArrayBuffer. + +It is required that `byte_length + byte_offset` is less than or equal to the +size in bytes of the array passed in. If not, a RangeError exception is raised. + +JavaScript DataView Objects are described in +[Section 24.3][] of the ECMAScript Language Specification. + ### Functions to convert from C types to N-API #### *napi_create_number* + +```C +napi_status napi_get_dataview_info(napi_env env, + napi_value dataview, + size_t* byte_length, + 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] byte_length`: 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* + +```C +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 passed in is a DataView. + ### *napi_strict_equals*