1 #include <js_native_api.h>
2 #include "../common.h"
3
Add(napi_env env,napi_callback_info info)4 static napi_value Add(napi_env env, napi_callback_info info) {
5 size_t argc = 2;
6 napi_value args[2];
7 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
8
9 NAPI_ASSERT(env, argc >= 2, "Wrong number of arguments");
10
11 napi_valuetype valuetype0;
12 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
13
14 napi_valuetype valuetype1;
15 NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1));
16
17 NAPI_ASSERT(env, valuetype0 == napi_number && valuetype1 == napi_number,
18 "Wrong argument type. Numbers expected.");
19
20 double value0;
21 NAPI_CALL(env, napi_get_value_double(env, args[0], &value0));
22
23 double value1;
24 NAPI_CALL(env, napi_get_value_double(env, args[1], &value1));
25
26 napi_value sum;
27 NAPI_CALL(env, napi_create_double(env, value0 + value1, &sum));
28
29 return sum;
30 }
31
32 EXTERN_C_START
Init(napi_env env,napi_value exports)33 napi_value Init(napi_env env, napi_value exports) {
34 napi_property_descriptor desc = DECLARE_NAPI_PROPERTY("add", Add);
35 NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc));
36 return exports;
37 }
38 EXTERN_C_END
39