Skip to content

Commit eec9cfa

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 bab1647 commit eec9cfa

File tree

4 files changed

+21
-91
lines changed

4 files changed

+21
-91
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
var resolve = require( 'path' ).resolve;
2424
var bench = require( '@stdlib/bench' );
2525
var uniform = require( '@stdlib/random/array/uniform' );
26-
var isArray = require( '@stdlib/assert/is-array' );
26+
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
2727
var tryRequire = require( '@stdlib/utils/try-require' );
2828
var pkg = require( './../package.json' ).name;
2929

@@ -49,12 +49,12 @@ bench( pkg+'::native', opts, function benchmark( b ) {
4949
for ( i = 0; i < b.iterations; i++ ) {
5050
y = modf( x[ i%x.length ] );
5151
if ( typeof y !== 'object' ) {
52-
b.fail( 'should return an array' );
52+
b.fail( 'should return an object' );
5353
}
5454
}
5555
b.toc();
56-
if ( !isArray( y ) ) {
57-
b.fail( 'should return an array' );
56+
if ( !isFloat64Array( y ) ) {
57+
b.fail( 'should return a Float64Array' );
5858
}
5959
b.pass( 'benchmark finished' );
6060
b.end();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ 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 parts = modf( 3.14 );
38-
* // returns [ 3.0, 0.14000000000000012 ]
38+
* // returns <Float64Array>[ 3.0, 0.14000000000000012 ]
3939
*/
4040
function modf( x ) {
4141
var out = new Float64Array( 2 );
42-
addon( out, x );
43-
return [ out[ 0 ], out[ 1 ] ];
42+
addon( x, out );
43+
return out;
4444
}
4545

4646

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
"libraries": [],
3737
"libpath": [],
3838
"dependencies": [
39+
"@stdlib/napi/argv",
40+
"@stdlib/napi/argv-double",
41+
"@stdlib/napi/argv-float64array",
42+
"@stdlib/napi/export",
3943
"@stdlib/math/base/assert/is-nan",
4044
"@stdlib/number/float64/base/from-words",
4145
"@stdlib/number/float64/base/to-words",

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

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

1919
#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"
2024
#include <node_api.h>
21-
#include <stdint.h>
22-
#include <assert.h>
2325

2426
/**
2527
* Receives JavaScript callback invocation data.
@@ -29,87 +31,11 @@
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 = 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 ] );
9838
return NULL;
9939
}
10040

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

Comments
 (0)