1 #include <js_native_api.h>
2 #include <string.h>
3 #include "../common.h"
4
CreateDataView(napi_env env,napi_callback_info info)5 static 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
CreateDataViewFromJSDataView(napi_env env,napi_callback_info info)52 static napi_value CreateDataViewFromJSDataView(napi_env env, napi_callback_info info) {
53 size_t argc = 1;
54 napi_value args [1];
55 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
56
57 NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
58
59 napi_valuetype valuetype;
60 napi_value input_dataview = args[0];
61
62 NAPI_CALL(env, napi_typeof(env, input_dataview, &valuetype));
63 NAPI_ASSERT(env, valuetype == napi_object,
64 "Wrong type of arguments. Expects a DataView as the first "
65 "argument.");
66
67 bool is_dataview;
68 NAPI_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview));
69 NAPI_ASSERT(env, is_dataview,
70 "Wrong type of arguments. Expects a DataView as the first "
71 "argument.");
72 size_t byte_offset = 0;
73 size_t length = 0;
74 napi_value buffer;
75 NAPI_CALL(env,
76 napi_get_dataview_info(env, input_dataview, &length, NULL,
77 &buffer, &byte_offset));
78
79 napi_value output_dataview;
80 NAPI_CALL(env,
81 napi_create_dataview(env, length, buffer,
82 byte_offset, &output_dataview));
83
84
85 return output_dataview;
86 }
87
88 EXTERN_C_START
Init(napi_env env,napi_value exports)89 napi_value Init(napi_env env, napi_value exports) {
90 napi_property_descriptor descriptors[] = {
91 DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView),
92 DECLARE_NAPI_PROPERTY("CreateDataViewFromJSDataView",
93 CreateDataViewFromJSDataView)
94 };
95
96 NAPI_CALL(env, napi_define_properties(
97 env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
98
99 return exports;
100 }
101 EXTERN_C_END
102