Skip to content

Commit 15dff30

Browse files
gunjjoshikgryte
andauthored
refactor: use macros in addon and update examples in math/base/assert/is-evenf
PR-URL: #3115 Ref: c664da7 Private-ref: stdlib-js/todo#2294 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 009becd commit 15dff30

File tree

4 files changed

+22
-70
lines changed

4 files changed

+22
-70
lines changed

lib/node_modules/@stdlib/math/base/assert/is-evenf/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ bool = isEvenf( NaN );
8888
var randu = require( '@stdlib/random/array/discrete-uniform' );
8989
var isEvenf = require( '@stdlib/math/base/assert/is-evenf' );
9090
91-
var bool;
92-
var x;
93-
var i;
94-
95-
x = randu( 100, 0, 100 );
91+
var x = randu( 100, 0, 100 );
9692
93+
var i;
9794
for ( i = 0; i < 100; i++ ) {
98-
bool = isEvenf( x[ i ] );
99-
console.log( '%d is %s', x[ i ], ( bool ) ? 'even' : 'not even' );
95+
console.log( '%d is %s', x[ i ], ( isEvenf( x[ i ] ) ) ? 'even' : 'not even' );
10096
}
10197
```
10298

@@ -135,6 +131,8 @@ for ( i = 0; i < 100; i++ ) {
135131
Tests if a finite single-precision floating-point number is an even number.
136132

137133
```c
134+
#include <stdbool.h>
135+
138136
bool out = stdlib_base_is_evenf( 1.0f );
139137
// returns false
140138
@@ -169,6 +167,7 @@ bool stdlib_base_is_evenf( const float x );
169167
### Examples
170168
171169
```c
170+
#include "stdlib/math/base/assert/is_evenf.h"
172171
#include <stdio.h>
173172
#include <stdlib.h>
174173
#include <stdbool.h>

lib/node_modules/@stdlib/math/base/assert/is-evenf/examples/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
var randu = require( '@stdlib/random/array/discrete-uniform' );
2222
var isEvenf = require( './../lib' );
2323

24-
var bool;
25-
var x;
26-
var i;
27-
28-
x = randu( 100, 0, 100 );
24+
var x = randu( 100, 0, 100 );
2925

26+
var i;
3027
for ( i = 0; i < 100; i++ ) {
31-
bool = isEvenf( x[ i ] );
32-
console.log( '%d is %s', x[ i ], ( bool ) ? 'even' : 'not even' );
28+
console.log( '%d is %s', x[ i ], ( isEvenf( x[ i ] ) ) ? 'even' : 'not even' );
3329
}

lib/node_modules/@stdlib/math/base/assert/is-evenf/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-float",
41+
"@stdlib/napi/create-int32",
42+
"@stdlib/napi/export",
3943
"@stdlib/math/base/assert/is-integerf"
4044
]
4145
},

lib/node_modules/@stdlib/math/base/assert/is-evenf/src/addon.c

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

1919
#include "stdlib/math/base/assert/is_evenf.h"
20+
#include "stdlib/napi/argv.h"
21+
#include "stdlib/napi/argv_float.h"
22+
#include "stdlib/napi/create_int32.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,60 +32,10 @@
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 = 1;
36-
napi_value argv[ 1 ];
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 < 1 ) {
42-
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
43-
assert( status == napi_ok );
44-
return NULL;
45-
}
46-
if ( argc > 1 ) {
47-
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
48-
assert( status == napi_ok );
49-
return NULL;
50-
}
51-
52-
napi_valuetype vtype0;
53-
status = napi_typeof( env, argv[ 0 ], &vtype0 );
54-
assert( status == napi_ok );
55-
if ( vtype0 != napi_number ) {
56-
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
57-
assert( status == napi_ok );
58-
return NULL;
59-
}
60-
61-
double x;
62-
status = napi_get_value_double( env, argv[ 0 ], &x );
63-
assert( status == napi_ok );
64-
65-
bool result = stdlib_base_is_evenf( (float)x );
66-
67-
napi_value v;
68-
status = napi_create_int32( env, (int32_t)result, &v );
69-
assert( status == napi_ok );
70-
71-
return v;
72-
}
73-
74-
/**
75-
* Initializes a Node-API module.
76-
*
77-
* @param env environment under which the function is invoked
78-
* @param exports exports object
79-
* @return main export
80-
*/
81-
static napi_value init( napi_env env, napi_value exports ) {
82-
napi_value fcn;
83-
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
84-
assert( status == napi_ok );
85-
return fcn;
35+
STDLIB_NAPI_ARGV( env, info, argv, argc, 1 );
36+
STDLIB_NAPI_ARGV_FLOAT( env, x, argv, 0 );
37+
STDLIB_NAPI_CREATE_INT32( env, (int32_t)stdlib_base_is_evenf( x ), out );
38+
return out;
8639
}
8740

88-
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )
41+
STDLIB_NAPI_MODULE_EXPORT_FCN( addon )

0 commit comments

Comments
 (0)