|
17 | 17 | */ |
18 | 18 |
|
19 | 19 | #include "stdlib/math/base/special/minmax.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/create_double.h" |
24 | | -#include "stdlib/napi/export.h" |
| 20 | +#include <stdint.h> |
| 21 | +#include <assert.h> |
25 | 22 | #include <node_api.h> |
26 | 23 |
|
27 | 24 | /** |
|
32 | 29 | * @return Node-API value |
33 | 30 | */ |
34 | 31 | static napi_value addon( napi_env env, napi_callback_info info ) { |
35 | | - STDLIB_NAPI_ARGV( env, info, argv, argc, 3 ); |
36 | | - STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 ); |
37 | | - STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 ); |
38 | | - STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, z, zlen, argv, 2 ); |
| 32 | + napi_status status; |
| 33 | + |
| 34 | + // Get callback arguments: |
| 35 | + size_t argc = 3; |
| 36 | + napi_value argv[ 3 ]; |
| 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 < 3 ) { |
| 42 | + status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." ); |
| 43 | + assert( status == napi_ok ); |
| 44 | + return NULL; |
| 45 | + } |
| 46 | + if ( argc > 3 ) { |
| 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_valuetype vtype2; |
| 71 | + status = napi_typeof( env, argv[ 2 ], &vtype2 ); |
| 72 | + assert( status == napi_ok ); |
| 73 | + if ( vtype2 != napi_number ) { |
| 74 | + status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." ); |
| 75 | + assert( status == napi_ok ); |
| 76 | + return NULL; |
| 77 | + } |
| 78 | + |
| 79 | + napi_typedarray_type vtype0; |
| 80 | + size_t len; |
| 81 | + void *Out; |
| 82 | + status = napi_get_typedarray_info( env, argv[ 0 ], &vtype0, &len, &Out, NULL, NULL ); |
| 83 | + assert( status == napi_ok ); |
| 84 | + if ( vtype0 != napi_float64_array ) { |
| 85 | + status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a Float64Array." ); |
| 86 | + assert( status == napi_ok ); |
| 87 | + return NULL; |
| 88 | + } |
| 89 | + if ( len != 2 ) { |
| 90 | + status = napi_throw_range_error( env, NULL, "invalid argument. First argument must have 2 elements." ); |
| 91 | + assert( status == napi_ok ); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + double value1; |
| 96 | + status = napi_get_value_double( env, argv[ 1 ], &value1 ); |
| 97 | + assert( status == napi_ok ); |
| 98 | + |
| 99 | + double value2; |
| 100 | + status = napi_get_value_double( env, argv[ 2 ], &value2 ); |
| 101 | + assert( status == napi_ok ); |
39 | 102 |
|
40 | 103 | double min; |
41 | 104 | double max; |
42 | | - stdlib_base_minmax( x, y, &min, &max ); |
| 105 | + stdlib_base_minmax( value1, value2, &min, &max ); |
43 | 106 |
|
44 | | - double *op = (double *)z; |
| 107 | + double *op = (double *)Out; |
45 | 108 | op[ 0 ] = min; |
46 | 109 | op[ 1 ] = max; |
47 | 110 |
|
48 | 111 | return NULL; |
49 | 112 | } |
50 | 113 |
|
51 | | -STDLIB_NAPI_MODULE_EXPORT_FCN( addon ) |
| 114 | +/** |
| 115 | +* Initializes a Node-API module. |
| 116 | +* |
| 117 | +* @private |
| 118 | +* @param env environment under which the function is invoked |
| 119 | +* @param exports exports object |
| 120 | +* @return main export |
| 121 | +*/ |
| 122 | +static napi_value init( napi_env env, napi_value exports ) { |
| 123 | + napi_value fcn; |
| 124 | + napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn ); |
| 125 | + assert( status == napi_ok ); |
| 126 | + return fcn; |
| 127 | +} |
| 128 | + |
| 129 | +NAPI_MODULE( NODE_GYP_MODULE_NAME, init ) |
0 commit comments