|
17 | 17 | */
|
18 | 18 |
|
19 | 19 | #include "stdlib/math/base/assert/is_even.h"
|
| 20 | +#include "stdlib/napi/argv.h" |
| 21 | +#include "stdlib/napi/argv_double.h" |
| 22 | +#include "stdlib/napi/create_int32.h" |
| 23 | +#include "stdlib/napi/export.h" |
20 | 24 | #include <node_api.h>
|
21 | 25 | #include <stdint.h>
|
22 |
| -#include <assert.h> |
23 | 26 |
|
24 | 27 | /**
|
25 | 28 | * Receives JavaScript callback invocation data.
|
|
29 | 32 | * @return Node-API value
|
30 | 33 | */
|
31 | 34 | static napi_value addon( napi_env env, napi_callback_info info ) {
|
32 |
| - napi_status status; |
33 |
| - |
34 |
| - // Get callback arguments: |
35 |
| - size_t argc = 1; |
36 |
| - napi_value argv[ 1 ]; |
37 |
| - status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); |
38 |
| - assert( status == napi_ok ); |
39 |
| - |
40 |
| - // Check whether we were provided the correct number of arguments: |
41 |
| - if ( argc < 1 ) { |
42 |
| - status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); |
43 |
| - assert( status == napi_ok ); |
44 |
| - return NULL; |
45 |
| - } |
46 |
| - if ( argc > 1 ) { |
47 |
| - status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); |
48 |
| - assert( status == napi_ok ); |
49 |
| - return NULL; |
50 |
| - } |
51 |
| - |
52 |
| - napi_valuetype vtype0; |
53 |
| - status = napi_typeof( env, argv[ 0 ], &vtype0 ); |
54 |
| - assert( status == napi_ok ); |
55 |
| - if ( vtype0 != napi_number ) { |
56 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." ); |
57 |
| - assert( status == napi_ok ); |
58 |
| - return NULL; |
59 |
| - } |
60 |
| - |
61 |
| - double x; |
62 |
| - status = napi_get_value_double( env, argv[ 0 ], &x ); |
63 |
| - assert( status == napi_ok ); |
64 |
| - |
65 |
| - bool result = stdlib_base_is_even( x ); |
66 |
| - |
67 |
| - napi_value v; |
68 |
| - status = napi_create_int32( env, (int32_t)result, &v ); |
69 |
| - assert( status == napi_ok ); |
70 |
| - |
71 |
| - return v; |
| 35 | + STDLIB_NAPI_ARGV( env, info, argv, argc, 1 ); |
| 36 | + STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 ); |
| 37 | + STDLIB_NAPI_CREATE_INT32( env, (int32_t)stdlib_base_is_even( x ), out ); |
| 38 | + return out; |
72 | 39 | }
|
73 | 40 |
|
74 |
| -/** |
75 |
| -* Initializes a Node-API module. |
76 |
| -* |
77 |
| -* @param env environment under which the function is invoked |
78 |
| -* @param exports exports object |
79 |
| -* @return main export |
80 |
| -*/ |
81 |
| -static napi_value init( napi_env env, napi_value exports ) { |
82 |
| - napi_value fcn; |
83 |
| - napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); |
84 |
| - assert( status == napi_ok ); |
85 |
| - return fcn; |
86 |
| -} |
| 41 | +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) |
87 | 42 |
|
88 |
| -NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) |
0 commit comments