Skip to content

Commit 4340ba6

Browse files
gunjjoshikgryte
andauthored
feat: add I_F macro to math/base/napi/unary
PR-URL: #2903 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent e6b6ffb commit 4340ba6

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

lib/node_modules/@stdlib/math/base/napi/unary/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,46 @@ The function accepts the following arguments:
462462
void stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) );
463463
```
464464

465+
#### stdlib_math_base_napi_i_f( env, info, fcn )
466+
467+
Invokes a unary function accepting a signed 32-bit integer and returning a single-precision floating-point number.
468+
469+
```c
470+
#include <node_api.h>
471+
#include <stdint.h>
472+
473+
// ...
474+
475+
static float fcn( const int32_t x ) {
476+
// ...
477+
}
478+
479+
// ...
480+
481+
/**
482+
* Receives JavaScript callback invocation data.
483+
*
484+
* @param env environment under which the function is invoked
485+
* @param info callback data
486+
* @return Node-API value
487+
*/
488+
napi_value addon( napi_env env, napi_callback_info info ) {
489+
return stdlib_math_base_napi_i_f( env, info, fcn );
490+
}
491+
492+
// ...
493+
```
494+
495+
The function accepts the following arguments:
496+
497+
- **env**: `[in] napi_env` environment under which the function is invoked.
498+
- **info**: `[in] napi_callback_info` callback data.
499+
- **fcn**: `[in] float (*fcn)( int32_t )` unary function.
500+
501+
```c
502+
void stdlib_math_base_napi_i_f( napi_env env, napi_callback_info info, float (*fcn)( int32_t ) );
503+
```
504+
465505
#### STDLIB_MATH_BASE_NAPI_MODULE_D_D( fcn )
466506

467507
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning double-precision floating-point numbers.
@@ -683,6 +723,29 @@ The macro expects the following arguments:
683723
684724
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
685725
726+
#### STDLIB_MATH_BASE_NAPI_MODULE_I_F( fcn )
727+
728+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting a signed 32-bit integer and returning a single-precision floating-point number.
729+
730+
```c
731+
#include <stdint.h>
732+
733+
static float fcn( const int32_t x ) {
734+
// ...
735+
}
736+
737+
// ...
738+
739+
// Register a Node-API module:
740+
STDLIB_MATH_BASE_NAPI_MODULE_I_F( fcn );
741+
```
742+
743+
The macro expects the following arguments:
744+
745+
- **fcn**: `float (*fcn)( int32_t )` unary function.
746+
747+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
748+
686749
</section>
687750

688751
<!-- /.usage -->

lib/node_modules/@stdlib/math/base/napi/unary/include/stdlib/math/base/napi/unary.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,48 @@
417417
}; \
418418
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_f_i_init )
419419

420+
/**
421+
* Macro for registering a Node-API module exporting an interface invoking a unary function accepting a signed 32-bit integer and returning a single-precision floating-point number.
422+
*
423+
* @param fcn unary function
424+
*
425+
* @example
426+
* #include <stdint.h>
427+
*
428+
* static float fcn( const int32_t x ) {
429+
* // ...
430+
* }
431+
*
432+
* // ...
433+
*
434+
* // Register a Node-API module:
435+
* STDLIB_MATH_BASE_NAPI_MODULE_I_F( fcn );
436+
*/
437+
#define STDLIB_MATH_BASE_NAPI_MODULE_I_F( fcn ) \
438+
static napi_value stdlib_math_base_napi_i_f_wrapper( \
439+
napi_env env, \
440+
napi_callback_info info \
441+
) { \
442+
return stdlib_math_base_napi_i_f( env, info, fcn ); \
443+
}; \
444+
static napi_value stdlib_math_base_napi_i_f_init( \
445+
napi_env env, \
446+
napi_value exports \
447+
) { \
448+
napi_value fcn; \
449+
napi_status status = napi_create_function( \
450+
env, \
451+
"exports", \
452+
NAPI_AUTO_LENGTH, \
453+
stdlib_math_base_napi_i_f_wrapper, \
454+
NULL, \
455+
&fcn \
456+
); \
457+
assert( status == napi_ok ); \
458+
return fcn; \
459+
}; \
460+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_i_f_init )
461+
420462
/*
421463
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
422464
*/
@@ -469,6 +511,11 @@ napi_value stdlib_math_base_napi_i_d( napi_env env, napi_callback_info info, dou
469511
*/
470512
napi_value stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) );
471513

514+
/**
515+
* Invokes a unary function accepting a signed 32-bit integer and returning a single-precision floating-point number.
516+
*/
517+
napi_value stdlib_math_base_napi_i_f( napi_env env, napi_callback_info info, float (*fcn)( int32_t ) );
518+
472519
#ifdef __cplusplus
473520
}
474521
#endif

lib/node_modules/@stdlib/math/base/napi/unary/src/main.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,51 @@ napi_value stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int
647647

648648
return v;
649649
}
650+
651+
/**
652+
* Invokes a unary function accepting a signed 32-bit integer and returning a single-precision floating-point number.
653+
*
654+
* ## Notes
655+
*
656+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
657+
*
658+
* - `x`: input value.
659+
*
660+
* @param env environment under which the function is invoked
661+
* @param info callback data
662+
* @param fcn unary function
663+
* @return function return value as a Node-API double-precision floating-point number
664+
*/
665+
napi_value stdlib_math_base_napi_i_f( napi_env env, napi_callback_info info, float (*fcn)( int32_t ) ) {
666+
napi_status status;
667+
668+
size_t argc = 1;
669+
napi_value argv[ 1 ];
670+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
671+
assert( status == napi_ok );
672+
673+
if ( argc < 1 ) {
674+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." );
675+
assert( status == napi_ok );
676+
return NULL;
677+
}
678+
679+
napi_valuetype vtype0;
680+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
681+
assert( status == napi_ok );
682+
if ( vtype0 != napi_number ) {
683+
status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." );
684+
assert( status == napi_ok );
685+
return NULL;
686+
}
687+
688+
int32_t x;
689+
status = napi_get_value_int32( env, argv[ 0 ], &x );
690+
assert( status == napi_ok );
691+
692+
napi_value v;
693+
status = napi_create_double( env, (double)fcn( x ), &v );
694+
assert( status == napi_ok );
695+
696+
return v;
697+
}

0 commit comments

Comments
 (0)