mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.4 KiB
56 lines
1.4 KiB
8 years ago
|
#include <node_api.h>
|
||
|
|
||
|
void Test(napi_env env, napi_callback_info info) {
|
||
|
napi_status status;
|
||
|
|
||
|
size_t argc;
|
||
|
status = napi_get_cb_args_length(env, info, &argc);
|
||
|
if (status != napi_ok) return;
|
||
|
|
||
|
if (argc < 1) {
|
||
|
napi_throw_type_error(env, "Wrong number of arguments");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
napi_value args[1];
|
||
|
status = napi_get_cb_args(env, info, args, 1);
|
||
|
if (status != napi_ok) return;
|
||
|
|
||
|
napi_valuetype valuetype;
|
||
|
status = napi_typeof(env, args[0], &valuetype);
|
||
|
if (status != napi_ok) return;
|
||
|
|
||
|
if (valuetype != napi_number) {
|
||
|
napi_throw_type_error(env, "Wrong type of argments. Expects a number.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
double input;
|
||
|
status = napi_get_value_double(env, args[0], &input);
|
||
|
if (status != napi_ok) return;
|
||
|
|
||
|
napi_value output;
|
||
|
status = napi_create_number(env, input, &output);
|
||
|
if (status != napi_ok) return;
|
||
|
|
||
|
status = napi_set_return_value(env, info, output);
|
||
|
if (status != napi_ok) return;
|
||
|
}
|
||
|
|
||
|
#define DECLARE_NAPI_METHOD(name, func) \
|
||
|
{ name, func, 0, 0, 0, napi_default, 0 }
|
||
|
|
||
|
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
|
||
|
napi_status status;
|
||
|
|
||
|
napi_property_descriptor descriptors[] = {
|
||
|
DECLARE_NAPI_METHOD("Test", Test),
|
||
|
};
|
||
|
|
||
|
status = napi_define_properties(
|
||
|
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors);
|
||
|
if (status != napi_ok) return;
|
||
|
}
|
||
|
|
||
|
NAPI_MODULE(addon, Init)
|