|
17 | 17 | */
|
18 | 18 |
|
19 | 19 | #include "stdlib/math/base/special/wrap.h"
|
20 |
| -#include <node_api.h> |
21 |
| -#include <assert.h> |
| 20 | +#include "stdlib/math/base/napi/ternary.h" |
22 | 21 |
|
23 |
| -/** |
24 |
| -* Receives JavaScript callback invocation data. |
25 |
| -* |
26 |
| -* @param env environment under which the function is invoked |
27 |
| -* @param info callback data |
28 |
| -* @return Node-API value |
29 |
| -*/ |
30 |
| -static napi_value addon( napi_env env, napi_callback_info info ) { |
31 |
| - napi_status status; |
32 |
| - |
33 |
| - // Get callback arguments: |
34 |
| - size_t argc = 3; |
35 |
| - napi_value argv[ 3 ]; |
36 |
| - status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); |
37 |
| - assert( status == napi_ok ); |
38 |
| - |
39 |
| - // Check whether we were provided the correct number of arguments: |
40 |
| - if ( argc < 3 ) { |
41 |
| - status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); |
42 |
| - assert( status == napi_ok ); |
43 |
| - return NULL; |
44 |
| - } |
45 |
| - if ( argc > 3 ) { |
46 |
| - status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); |
47 |
| - assert( status == napi_ok ); |
48 |
| - return NULL; |
49 |
| - } |
50 |
| - |
51 |
| - napi_valuetype vtype0; |
52 |
| - status = napi_typeof( env, argv[ 0 ], &vtype0 ); |
53 |
| - assert( status == napi_ok ); |
54 |
| - if ( vtype0 != napi_number ) { |
55 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); |
56 |
| - assert( status == napi_ok ); |
57 |
| - return NULL; |
58 |
| - } |
59 |
| - |
60 |
| - napi_valuetype vtype1; |
61 |
| - status = napi_typeof( env, argv[ 1 ], &vtype1 ); |
62 |
| - assert( status == napi_ok ); |
63 |
| - if ( vtype0 != napi_number ) { |
64 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); |
65 |
| - assert( status == napi_ok ); |
66 |
| - return NULL; |
67 |
| - } |
68 |
| - |
69 |
| - napi_valuetype vtype2; |
70 |
| - status = napi_typeof( env, argv[ 2 ], &vtype2 ); |
71 |
| - assert( status == napi_ok ); |
72 |
| - if ( vtype0 != napi_number ) { |
73 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." ); |
74 |
| - assert( status == napi_ok ); |
75 |
| - return NULL; |
76 |
| - } |
77 |
| - |
78 |
| - double v; |
79 |
| - status = napi_get_value_double( env, argv[ 0 ], &v ); |
80 |
| - assert( status == napi_ok ); |
81 |
| - |
82 |
| - double min; |
83 |
| - status = napi_get_value_double( env, argv[ 1 ], &min ); |
84 |
| - assert( status == napi_ok ); |
85 |
| - |
86 |
| - double max; |
87 |
| - status = napi_get_value_double( env, argv[ 2 ], &max ); |
88 |
| - assert( status == napi_ok ); |
89 |
| - |
90 |
| - double out = stdlib_base_wrap( v, min, max ); |
91 |
| - |
92 |
| - napi_value w; |
93 |
| - status = napi_create_double( env, out, &w ); |
94 |
| - assert( status == napi_ok ); |
95 |
| - |
96 |
| - return w; |
97 |
| -} |
98 |
| - |
99 |
| -/** |
100 |
| -* Initializes a Node-API module. |
101 |
| -* |
102 |
| -* @param env environment under which the function is invoked |
103 |
| -* @param exports exports object |
104 |
| -* @return main export |
105 |
| -*/ |
106 |
| -static napi_value init( napi_env env, napi_value exports ) { |
107 |
| - napi_value fcn; |
108 |
| - napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); |
109 |
| - assert( status == napi_ok ); |
110 |
| - return fcn; |
111 |
| -} |
112 |
| - |
113 |
| -NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) |
| 22 | +// cppcheck-suppress shadowFunction |
| 23 | +STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( stdlib_base_wrap ) |
0 commit comments