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