Skip to content

Commit f93e655

Browse files
chore: refactoring addon
1 parent cca14c3 commit f93e655

File tree

5 files changed

+88
-100
lines changed

5 files changed

+88
-100
lines changed

lib/node_modules/@stdlib/math/base/special/minmax/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ Returns the minimum and maximum value.
155155
double min;
156156
double max;
157157

158-
stdlib_base_minmax( 4.0, -5.0, &min, &max );
158+
stdlib_base_minmax( 3.14, NaN );
159+
// returns [ NaN, NaN ]
159160
```
160161
161162
The function accepts the following arguments:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var Float64Array = require( '@stdlib/array/float64' );
24+
var addon = require( './../src/addon.node' );
25+
26+
27+
// MAIN //
28+
29+
/**
30+
* Returns the minimum and maximum values.
31+
*
32+
* @private
33+
* @param {number} x - first number
34+
* @param {number} y - second number
35+
* @returns {Array<number>} minimum and maximum values
36+
*
37+
* @example
38+
* var v = minmax( 3.14, 4.2 );
39+
* // returns [ 3.14, 4.2 ]
40+
*
41+
* @example
42+
* var v = minmax( 3.14, NaN );
43+
* // returns [ NaN, NaN ]
44+
*
45+
* @example
46+
* var v = minmax( +0.0, -0.0 );
47+
* // returns [ -0.0, 0.0 ]
48+
*/
49+
function minmax( x, y ) {
50+
var out = new Float64Array( 2 );
51+
addon( x, y, out );
52+
return [ out[ 0 ], out[ 1 ] ];
53+
}
54+
55+
56+
// EXPORTS //
57+
58+
module.exports = minmax;

lib/node_modules/@stdlib/math/base/special/minmax/manifest.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"libraries": [],
3737
"libpath": [],
3838
"dependencies": [
39+
"@stdlib/napi/argv",
40+
"@stdlib/napi/argv-double",
41+
"@stdlib/napi/argv-float64array",
42+
"@stdlib/napi/create-double",
43+
"@stdlib/napi/export",
3944
"@stdlib/math/base/assert/is-nan",
4045
"@stdlib/math/base/assert/is-negative-zero"
4146
]
@@ -51,6 +56,11 @@
5156
"libraries": [],
5257
"libpath": [],
5358
"dependencies": [
59+
"@stdlib/napi/argv",
60+
"@stdlib/napi/argv-double",
61+
"@stdlib/napi/argv-float64array",
62+
"@stdlib/napi/create-double",
63+
"@stdlib/napi/export",
5464
"@stdlib/math/base/assert/is-nan",
5565
"@stdlib/math/base/assert/is-negative-zero"
5666
]
@@ -66,6 +76,11 @@
6676
"libraries": [],
6777
"libpath": [],
6878
"dependencies": [
79+
"@stdlib/napi/argv",
80+
"@stdlib/napi/argv-double",
81+
"@stdlib/napi/argv-float64array",
82+
"@stdlib/napi/create-double",
83+
"@stdlib/napi/export",
6984
"@stdlib/math/base/assert/is-nan",
7085
"@stdlib/math/base/assert/is-negative-zero"
7186
]

lib/node_modules/@stdlib/math/base/special/minmax/src/addon.c

Lines changed: 11 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/minmax.h"
20-
#include <stdint.h>
21-
#include <assert.h>
20+
#include "stdlib/napi/export.h"
21+
#include "stdlib/napi/argv.h"
22+
#include "stdlib/napi/argv_double.h"
23+
#include "stdlib/napi/argv_float64array.h"
2224
#include <node_api.h>
2325

2426
/**
@@ -29,101 +31,12 @@
2931
* @return Node-API value
3032
*/
3133
static napi_value addon( napi_env env, napi_callback_info info ) {
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 );
102-
103-
double min;
104-
double max;
105-
stdlib_base_minmax( value1, value2, &min, &max );
106-
107-
double *op = (double *)Out;
108-
op[ 0 ] = min;
109-
op[ 1 ] = max;
110-
111-
return NULL;
112-
}
113-
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;
34+
STDLIB_NAPI_ARGV( env, info, argv, argc, 3 );
35+
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
36+
STDLIB_NAPI_ARGV_DOUBLE( env, y, argv, 1 );
37+
STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, out, outlen, argv, 2 );
38+
stdlib_base_minmax( x, y, &out[ 0 ], &out[ 1 ] );
39+
return NULL;
12740
}
12841

129-
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
42+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

lib/node_modules/@stdlib/math/base/special/minmax/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
*
3535
* double min;
3636
* double max;
37-
* stdlib_base_minmax( 4.0, -5.0, &min, &max );
37+
* stdlib_base_minmax( 3.14, NaN );
38+
* // returns [ NaN, NaN ]
3839
*/
3940

4041
void stdlib_base_minmax( const double x, const double y, double* min, double* max ) {

0 commit comments

Comments
 (0)