node/test/addons-napi/test_dataview/test_dataview.c
Taylor Woll 92e5f5cc09 n-api: refactor napi_addon_register_func
As per discussion in abi-stable-node:
https://github.com/nodejs/abi-stable-node/issues/256,
take a refactor to napi_addon_register_func such that
the result from the register function is assigned to
the module exports property. By making this change,
native module can be agnostic about which type of
module the environment supports.

PR-URL: https://github.com/nodejs/node/pull/15088
Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Hitesh Kanwathirtha <digitalinfinity@gmail.com>
2017-09-14 15:02:10 -04:00

52 lines
1.5 KiB
C

#include <node_api.h>
#include <string.h>
#include "../common.h"
napi_value CreateDataView(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args [1];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NAPI_ASSERT(env, argc == 1, "Wrong number of arguments");
napi_valuetype valuetype;
napi_value input_dataview = args[0];
NAPI_CALL(env, napi_typeof(env, input_dataview, &valuetype));
NAPI_ASSERT(env, valuetype == napi_object,
"Wrong type of arguments. Expects a DataView as the first "
"argument.");
bool is_dataview;
NAPI_CALL(env, napi_is_dataview(env, input_dataview, &is_dataview));
NAPI_ASSERT(env, is_dataview,
"Wrong type of arguments. Expects a DataView as the first "
"argument.");
size_t byte_offset = 0;
size_t length = 0;
napi_value buffer;
NAPI_CALL(env,
napi_get_dataview_info(env, input_dataview, &length, NULL,
&buffer, &byte_offset));
napi_value output_dataview;
NAPI_CALL(env,
napi_create_dataview(env, length, buffer,
byte_offset, &output_dataview));
return output_dataview;
}
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NAPI_PROPERTY("CreateDataView", CreateDataView)
};
NAPI_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)