1 #include <js_native_api.h>
2 #include "../common.h"
3
Test(napi_env env,napi_callback_info info)4 static napi_value Test(napi_env env, napi_callback_info info) {
5 size_t argc = 1;
6 napi_value args[1];
7 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
8
9 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
10
11 napi_valuetype valuetype0;
12 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
13
14 NAPI_ASSERT(env, valuetype0 == napi_number,
15 "Wrong type of arguments. Expects a number as first argument.");
16
17 double input;
18 NAPI_CALL(env, napi_get_value_double(env, args[0], &input));
19
20 napi_value output;
21 NAPI_CALL(env, napi_create_double(env, input, &output));
22
23 return output;
24 }
25
TestUint32Truncation(napi_env env,napi_callback_info info)26 static napi_value TestUint32Truncation(napi_env env, napi_callback_info info) {
27 size_t argc = 1;
28 napi_value args[1];
29 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
30
31 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
32
33 napi_valuetype valuetype0;
34 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
35
36 NAPI_ASSERT(env, valuetype0 == napi_number,
37 "Wrong type of arguments. Expects a number as first argument.");
38
39 uint32_t input;
40 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &input));
41
42 napi_value output;
43 NAPI_CALL(env, napi_create_uint32(env, input, &output));
44
45 return output;
46 }
47
TestInt32Truncation(napi_env env,napi_callback_info info)48 static napi_value TestInt32Truncation(napi_env env, napi_callback_info info) {
49 size_t argc = 1;
50 napi_value args[1];
51 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
52
53 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
54
55 napi_valuetype valuetype0;
56 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
57
58 NAPI_ASSERT(env, valuetype0 == napi_number,
59 "Wrong type of arguments. Expects a number as first argument.");
60
61 int32_t input;
62 NAPI_CALL(env, napi_get_value_int32(env, args[0], &input));
63
64 napi_value output;
65 NAPI_CALL(env, napi_create_int32(env, input, &output));
66
67 return output;
68 }
69
TestInt64Truncation(napi_env env,napi_callback_info info)70 static napi_value TestInt64Truncation(napi_env env, napi_callback_info info) {
71 size_t argc = 1;
72 napi_value args[1];
73 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
74
75 NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");
76
77 napi_valuetype valuetype0;
78 NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));
79
80 NAPI_ASSERT(env, valuetype0 == napi_number,
81 "Wrong type of arguments. Expects a number as first argument.");
82
83 int64_t input;
84 NAPI_CALL(env, napi_get_value_int64(env, args[0], &input));
85
86 napi_value output;
87 NAPI_CALL(env, napi_create_int64(env, input, &output));
88
89 return output;
90 }
91
92 EXTERN_C_START
Init(napi_env env,napi_value exports)93 napi_value Init(napi_env env, napi_value exports) {
94 napi_property_descriptor descriptors[] = {
95 DECLARE_NAPI_PROPERTY("Test", Test),
96 DECLARE_NAPI_PROPERTY("TestInt32Truncation", TestInt32Truncation),
97 DECLARE_NAPI_PROPERTY("TestUint32Truncation", TestUint32Truncation),
98 DECLARE_NAPI_PROPERTY("TestInt64Truncation", TestInt64Truncation),
99 };
100
101 NAPI_CALL(env, napi_define_properties(
102 env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
103
104 return exports;
105 }
106 EXTERN_C_END
107