Skip to content

Commit 63b7891

Browse files
committed
refactor: use in-house macros for building native addon bindings
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent eec9cfa commit 63b7891

File tree

5 files changed

+81
-107
lines changed

5 files changed

+81
-107
lines changed

lib/node_modules/@stdlib/math/base/special/frexp/benchmark/benchmark.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// MODULES //
2222

2323
var bench = require( '@stdlib/bench' );
24-
var randu = require( '@stdlib/random/base/randu' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
2525
var isArray = require( '@stdlib/assert/is-array' );
2626
var pkg = require( './../package.json' ).name;
2727
var frexp = require( './../lib' );
@@ -34,11 +34,12 @@ bench( pkg, function benchmark( b ) {
3434
var y;
3535
var i;
3636

37+
x = uniform( 100, -5.0e6, 5.0e6 );
38+
3739
b.tic();
3840
for ( i = 0; i < b.iterations; i++ ) {
39-
x = ( randu()*1.0e7 ) - 5.0e6;
40-
y = frexp( x );
41-
if ( typeof out !== 'object' ) {
41+
y = frexp( x[ i%x.length ] );
42+
if ( typeof y !== 'object' ) {
4243
b.fail( 'should return an array' );
4344
}
4445
}
@@ -57,11 +58,11 @@ bench( pkg+':assign', function benchmark( b ) {
5758
var i;
5859

5960
out = [ 0.0, 0.0 ];
61+
x = uniform( 100, -5.0e6, 5.0e6 );
6062

6163
b.tic();
6264
for ( i = 0; i < b.iterations; i++ ) {
63-
x = ( randu()*1.0e7 ) - 5.0e6;
64-
y = frexp.assign( x, out, 1, 0 );
65+
y = frexp.assign( x[ i%x.length ], out, 1, 0 );
6566
if ( typeof out !== 'object' ) {
6667
b.fail( 'should return an array' );
6768
}

lib/node_modules/@stdlib/math/base/special/frexp/benchmark/benchmark.native.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
25-
var randu = require( '@stdlib/random/base/randu' );
26-
var isArray = require( '@stdlib/assert/is-array' );
25+
var uniform = require( '@stdlib/random/array/uniform' );
26+
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
2929

@@ -43,17 +43,18 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4343
var y;
4444
var i;
4545

46+
x = uniform( 100, -5.0e6, 5.0e6 );
47+
4648
b.tic();
4749
for ( i = 0; i < b.iterations; i++ ) {
48-
x = ( randu()*1.0e7 ) - 5.0e6;
49-
y = frexp( x );
50+
y = frexp( x[ i%x.length ] );
5051
if ( typeof y !== 'object' ) {
51-
b.fail( 'should return an array' );
52+
b.fail( 'should return an object' );
5253
}
5354
}
5455
b.toc();
55-
if ( !isArray( y ) ) {
56-
b.fail( 'should return an array' );
56+
if ( !isFloat64Array( y ) ) {
57+
b.fail( 'should return a Float64Array' );
5758
}
5859
b.pass( 'benchmark finished' );
5960
b.end();

lib/node_modules/@stdlib/math/base/special/frexp/lib/native.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,36 @@ var addon = require( './../src/addon.node' );
3131
*
3232
* @private
3333
* @param {number} x - input value
34-
* @returns {Array<number>} output array
34+
* @returns {Float64Array} output array
3535
*
3636
* @example
3737
* var out = frexp( 4.0 );
38-
* // returns [ 0.5, 3 ]
38+
* // returns <Float64Array>[ 0.5, 3 ]
3939
*
4040
* @example
4141
* var out = frexp( 0.0 );
42-
* // returns [ 0.0, 0 ]
42+
* // returns <Float64Array>[ 0.0, 0 ]
4343
*
4444
* @example
4545
* var out = frexp( -0.0 );
46-
* // returns [ -0.0, 0 ]
46+
* // returns <Float64Array>[ -0.0, 0 ]
4747
*
4848
* @example
4949
* var out = frexp( NaN );
50-
* // returns [ NaN, 0 ]
50+
* // returns <Float64Array>[ NaN, 0 ]
5151
*
5252
* @example
5353
* var out = frexp( Infinity );
54-
* // returns [ Infinity , 0 ]
54+
* // returns <Float64Array>[ Infinity , 0 ]
5555
*
5656
* @example
5757
* var out = frexp( -Infinity );
58-
* // returns [ -Infinity , 0 ]
58+
* // returns <Float64Array>[ -Infinity , 0 ]
5959
*/
6060
function frexp( x ) {
6161
var out = new Float64Array( 2 );
62-
addon( out, x );
63-
return [ out[ 0 ], out[ 1 ] ];
62+
addon( x, out );
63+
return out;
6464
}
6565

6666

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

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"options": {},
2+
"options": {
3+
"task": "build"
4+
},
35
"fields": [
46
{
57
"field": "src",
@@ -24,6 +26,7 @@
2426
],
2527
"confs": [
2628
{
29+
"task": "build",
2730
"src": [
2831
"./src/main.c"
2932
],
@@ -33,15 +36,55 @@
3336
"libraries": [],
3437
"libpath": [],
3538
"dependencies": [
39+
"@stdlib/napi/argv",
40+
"@stdlib/napi/argv-double",
41+
"@stdlib/napi/argv-float64array",
42+
"@stdlib/napi/export",
3643
"@stdlib/math/base/assert/is-infinite",
3744
"@stdlib/math/base/assert/is-nan",
38-
"@stdlib/math/base/special/abs",
3945
"@stdlib/number/float64/base/normalize",
4046
"@stdlib/number/float64/base/exponent",
4147
"@stdlib/number/float64/base/from-words",
42-
"@stdlib/number/float32/base/to-word"
48+
"@stdlib/number/float64/base/to-words"
49+
]
50+
},
51+
{
52+
"task": "benchmark",
53+
"src": [
54+
"./src/main.c"
55+
],
56+
"include": [
57+
"./include"
58+
],
59+
"libraries": [],
60+
"libpath": [],
61+
"dependencies": [
62+
"@stdlib/math/base/assert/is-infinite",
63+
"@stdlib/math/base/assert/is-nan",
64+
"@stdlib/number/float64/base/normalize",
65+
"@stdlib/number/float64/base/exponent",
66+
"@stdlib/number/float64/base/from-words",
67+
"@stdlib/number/float64/base/to-words"
68+
]
69+
},
70+
{
71+
"task": "examples",
72+
"src": [
73+
"./src/main.c"
74+
],
75+
"include": [
76+
"./include"
77+
],
78+
"libraries": [],
79+
"libpath": [],
80+
"dependencies": [
81+
"@stdlib/math/base/assert/is-infinite",
82+
"@stdlib/math/base/assert/is-nan",
83+
"@stdlib/number/float64/base/normalize",
84+
"@stdlib/number/float64/base/exponent",
85+
"@stdlib/number/float64/base/from-words",
86+
"@stdlib/number/float64/base/to-words"
4387
]
4488
}
4589
]
4690
}
47-

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

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
*/
1818

1919
#include "stdlib/math/base/special/frexp.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"
2024
#include <node_api.h>
2125
#include <stdint.h>
22-
#include <assert.h>
2326

2427
/**
2528
* Receives JavaScript callback invocation data.
@@ -29,87 +32,13 @@
2932
* @return Node-API value
3033
*/
3134
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 frac;
9135
int32_t exp;
92-
stdlib_base_frexp( value, &frac, &exp );
93-
94-
double *op = (double *)Out;
95-
op[ 0 ] = frac;
96-
op[ 1 ] = (double)exp;
97-
36+
STDLIB_NAPI_ARGV( env, info, argv, argc, 2 );
37+
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
38+
STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, y, ylen, argv, 1 );
39+
stdlib_base_frexp( x, &y[ 0 ], &exp );
40+
y[ 1 ] = (double)exp;
9841
return NULL;
9942
}
10043

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 )
44+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

0 commit comments

Comments
 (0)