1#include <js_native_api.h>
2#include <string.h>
3#include "../common.h"
4#include "../entry_point.h"
5
6static napi_value CreateDataView(napi_env env, napi_callback_info info) {
7  size_t argc = 3;
8  napi_value args [3];
9  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
10
11  NODE_API_ASSERT(env, argc == 3, "Wrong number of arguments");
12
13  napi_valuetype valuetype0;
14  napi_value arraybuffer = args[0];
15
16  NODE_API_CALL(env, napi_typeof(env, arraybuffer, &valuetype0));
17  NODE_API_ASSERT(env, valuetype0 == napi_object,
18              "Wrong type of arguments. Expects a ArrayBuffer as the first "
19              "argument.");
20
21  bool is_arraybuffer;
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.");
26
27  napi_valuetype valuetype1;
28  NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
29
30  NODE_API_ASSERT(env, valuetype1 == napi_number,
31      "Wrong type of arguments. Expects a number as second argument.");
32
33  size_t byte_offset = 0;
34  NODE_API_CALL(env, napi_get_value_uint32(env, args[1], (uint32_t*)(&byte_offset)));
35
36  napi_valuetype valuetype2;
37  NODE_API_CALL(env, napi_typeof(env, args[2], &valuetype2));
38
39  NODE_API_ASSERT(env, valuetype2 == napi_number,
40      "Wrong type of arguments. Expects a number as third argument.");
41
42  size_t length = 0;
43  NODE_API_CALL(env, napi_get_value_uint32(env, args[2], (uint32_t*)(&length)));
44
45  napi_value output_dataview;
46  NODE_API_CALL(env,
47            napi_create_dataview(env, length, arraybuffer,
48                                 byte_offset, &output_dataview));
49
50  return output_dataview;
51}
52
53static napi_value CreateDataViewFromJSDataView(napi_env env, napi_callback_info info) {
54  size_t argc = 1;
55  napi_value args [1];
56  NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
57
58  NODE_API_ASSERT(env, argc == 1, "Wrong number of arguments");
59
60  napi_valuetype valuetype;
61  napi_value input_dataview = args[0];
62
63  NODE_API_CALL(env, napi_typeof(env, input_dataview, &valuetype));
64  NODE_API_ASSERT(env, valuetype == napi_object,
65              "Wrong type of arguments. Expects a DataView as the first "
66              "argument.");
67
68  bool is_dataview;
69  NODE_API_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview));
70  NODE_API_ASSERT(env, is_dataview,
71              "Wrong type of arguments. Expects a DataView as the first "
72              "argument.");
73  size_t byte_offset = 0;
74  size_t length = 0;
75  napi_value buffer;
76  NODE_API_CALL(env,
77            napi_get_dataview_info(env, input_dataview, &length, NULL,
78                                   &buffer, &byte_offset));
79
80  napi_value output_dataview;
81  NODE_API_CALL(env,
82            napi_create_dataview(env, length, buffer,
83                                 byte_offset, &output_dataview));
84
85
86  return output_dataview;
87}
88
89EXTERN_C_START
90napi_value Init(napi_env env, napi_value exports) {
91  napi_property_descriptor descriptors[] = {
92    DECLARE_NODE_API_PROPERTY("CreateDataView", CreateDataView),
93    DECLARE_NODE_API_PROPERTY("CreateDataViewFromJSDataView",
94        CreateDataViewFromJSDataView)
95  };
96
97  NODE_API_CALL(env, napi_define_properties(
98      env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
99
100  return exports;
101}
102EXTERN_C_END
103