|
17 | 17 | */
|
18 | 18 |
|
19 | 19 | #include "stdlib/math/base/special/modf.h"
|
| 20 | +#include "stdlib/napi/argv.h" |
| 21 | +#include "stdlib/napi/argv_double.h" |
| 22 | +#include "stdlib/napi/argv_float64array.h" |
| 23 | +#include "stdlib/napi/export.h" |
20 | 24 | #include <node_api.h>
|
21 |
| -#include <stdint.h> |
22 |
| -#include <assert.h> |
23 | 25 |
|
24 | 26 | /**
|
25 | 27 | * Receives JavaScript callback invocation data.
|
|
29 | 31 | * @return Node-API value
|
30 | 32 | */
|
31 | 33 | static napi_value addon( napi_env env, napi_callback_info info ) {
|
32 |
| - napi_status status; |
33 |
| - |
34 |
| - // Get callback arguments: |
35 |
| - size_t argc = 2; |
36 |
| - napi_value argv[ 2 ]; |
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 < 2 ) { |
42 |
| - status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); |
43 |
| - assert( status == napi_ok ); |
44 |
| - return NULL; |
45 |
| - } |
46 |
| - if ( argc > 2 ) { |
47 |
| - status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." ); |
48 |
| - assert( status == napi_ok ); |
49 |
| - return NULL; |
50 |
| - } |
51 |
| - |
52 |
| - bool res; |
53 |
| - status = napi_is_typedarray( env, argv[ 0 ], &res ); |
54 |
| - assert( status == napi_ok ); |
55 |
| - if ( res == false ) { |
56 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float64Array." ); |
57 |
| - assert( status == napi_ok ); |
58 |
| - return NULL; |
59 |
| - } |
60 |
| - |
61 |
| - napi_valuetype vtype1; |
62 |
| - status = napi_typeof( env, argv[ 1 ], &vtype1 ); |
63 |
| - assert( status == napi_ok ); |
64 |
| - if ( vtype1 != napi_number ) { |
65 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." ); |
66 |
| - assert( status == napi_ok ); |
67 |
| - return NULL; |
68 |
| - } |
69 |
| - |
70 |
| - napi_typedarray_type vtype0; |
71 |
| - size_t len; |
72 |
| - void *Out; |
73 |
| - status = napi_get_typedarray_info( env, argv[ 0 ], &vtype0, &len, &Out, NULL, NULL ); |
74 |
| - assert( status == napi_ok ); |
75 |
| - if ( vtype0 != napi_float64_array ) { |
76 |
| - status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float64Array." ); |
77 |
| - assert( status == napi_ok ); |
78 |
| - return NULL; |
79 |
| - } |
80 |
| - if ( len != 2 ) { |
81 |
| - status = napi_throw_range_error( env, NULL, "invalid argument. First argument must have 2 elements." ); |
82 |
| - assert( status == napi_ok ); |
83 |
| - return NULL; |
84 |
| - } |
85 |
| - |
86 |
| - double value; |
87 |
| - status = napi_get_value_double( env, argv[ 1 ], &value ); |
88 |
| - assert( status == napi_ok ); |
89 |
| - |
90 |
| - double integral; |
91 |
| - double frac; |
92 |
| - stdlib_base_modf( value, &integral, &frac ); |
93 |
| - |
94 |
| - double *op = (double *)Out; |
95 |
| - op[ 0 ] = integral; |
96 |
| - op[ 1 ] = frac; |
97 |
| - |
| 34 | + STDLIB_NAPI_ARGV( env, info, argv, argc, 2 ); |
| 35 | + STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 ); |
| 36 | + STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, y, ylen, argv, 1 ); |
| 37 | + stdlib_base_modf( x, &y[ 0 ], &y[ 1 ] ); |
98 | 38 | return NULL;
|
99 | 39 | }
|
100 | 40 |
|
101 |
| -/** |
102 |
| -* Initializes a Node-API module. |
103 |
| -* |
104 |
| -* @param env environment under which the function is invoked |
105 |
| -* @param exports exports object |
106 |
| -* @return main export |
107 |
| -*/ |
108 |
| -static napi_value init( napi_env env, napi_value exports ) { |
109 |
| - napi_value fcn; |
110 |
| - napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); |
111 |
| - assert( status == napi_ok ); |
112 |
| - return fcn; |
113 |
| -} |
114 |
| - |
115 |
| -NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) |
| 41 | +STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) |
0 commit comments