|
3 | 3 | #include "../common.h"
|
4 | 4 |
|
5 | 5 | napi_value CreateDataView(napi_env env, napi_callback_info info) {
|
| 6 | + size_t argc = 3; |
| 7 | + napi_value args [3]; |
| 8 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); |
| 9 | + |
| 10 | + NAPI_ASSERT(env, argc == 3, "Wrong number of arguments"); |
| 11 | + |
| 12 | + napi_valuetype valuetype0; |
| 13 | + napi_value arraybuffer = args[0]; |
| 14 | + |
| 15 | + NAPI_CALL(env, napi_typeof(env, arraybuffer, &valuetype0)); |
| 16 | + NAPI_ASSERT(env, valuetype0 == napi_object, |
| 17 | + "Wrong type of arguments. Expects a ArrayBuffer as the first " |
| 18 | + "argument."); |
| 19 | + |
| 20 | + bool is_arraybuffer; |
| 21 | + NAPI_CALL(env, napi_is_arraybuffer(env, arraybuffer, &is_arraybuffer)); |
| 22 | + NAPI_ASSERT(env, is_arraybuffer, |
| 23 | + "Wrong type of arguments. Expects a ArrayBuffer as the first " |
| 24 | + "argument."); |
| 25 | + |
| 26 | + napi_valuetype valuetype1; |
| 27 | + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); |
| 28 | + |
| 29 | + NAPI_ASSERT(env, valuetype1 == napi_number, |
| 30 | + "Wrong type of arguments. Expects a number as second argument."); |
| 31 | + |
| 32 | + size_t byte_offset = 0; |
| 33 | + NAPI_CALL(env, napi_get_value_uint32(env, args[1], (uint32_t*)(&byte_offset))); |
| 34 | + |
| 35 | + napi_valuetype valuetype2; |
| 36 | + NAPI_CALL(env, napi_typeof(env, args[2], &valuetype2)); |
| 37 | + |
| 38 | + NAPI_ASSERT(env, valuetype2 == napi_number, |
| 39 | + "Wrong type of arguments. Expects a number as third argument."); |
| 40 | + |
| 41 | + size_t length = 0; |
| 42 | + NAPI_CALL(env, napi_get_value_uint32(env, args[2], (uint32_t*)(&length))); |
| 43 | + |
| 44 | + napi_value output_dataview; |
| 45 | + NAPI_CALL(env, |
| 46 | + napi_create_dataview(env, length, arraybuffer, |
| 47 | + byte_offset, &output_dataview)); |
| 48 | + |
| 49 | + return output_dataview; |
| 50 | +} |
| 51 | + |
| 52 | +napi_value CreateDataViewFromJSDataView(napi_env env, napi_callback_info info) { |
6 | 53 | size_t argc = 1;
|
7 | 54 | napi_value args [1];
|
8 | 55 | NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
|
@@ -34,12 +81,15 @@ napi_value CreateDataView(napi_env env, napi_callback_info info) {
|
34 | 81 | napi_create_dataview(env, length, buffer,
|
35 | 82 | byte_offset, &output_dataview));
|
36 | 83 |
|
| 84 | + |
37 | 85 | return output_dataview;
|
38 | 86 | }
|
39 | 87 |
|
40 | 88 | napi_value Init(napi_env env, napi_value exports) {
|
41 | 89 | napi_property_descriptor descriptors[] = {
|
42 |
| - DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView) |
| 90 | + DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView), |
| 91 | + DECLARE_NAPI_PROPERTY("CreateDataViewFromJSDataView", |
| 92 | + CreateDataViewFromJSDataView) |
43 | 93 | };
|
44 | 94 |
|
45 | 95 | NAPI_CALL(env, napi_define_properties(
|
|
0 commit comments