Skip to content

Commit b224509

Browse files
authored
feat: add F_I macro to math/base/napi/unary
PR-URL: #2768 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 140b517 commit b224509

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
@@ -422,6 +422,46 @@ The function accepts the following arguments:
422422
void stdlib_math_base_napi_i_i( napi_env env, napi_callback_info info, double (*fcn)( int32_t ) );
423423
```
424424

425+
#### stdlib_math_base_napi_f_i( env, info, fcn )
426+
427+
Invokes a unary function accepting a single-precision floating-point number and returning a signed 32-bit integer.
428+
429+
```c
430+
#include <node_api.h>
431+
#include <stdint.h>
432+
433+
// ...
434+
435+
static int32_t fcn( const float x ) {
436+
// ...
437+
}
438+
439+
// ...
440+
441+
/**
442+
* Receives JavaScript callback invocation data.
443+
*
444+
* @param env environment under which the function is invoked
445+
* @param info callback data
446+
* @return Node-API value
447+
*/
448+
napi_value addon( napi_env env, napi_callback_info info ) {
449+
return stdlib_math_base_napi_f_i( env, info, fcn );
450+
}
451+
452+
// ...
453+
```
454+
455+
The function accepts the following arguments:
456+
457+
- **env**: `[in] napi_env` environment under which the function is invoked.
458+
- **info**: `[in] napi_callback_info` callback data.
459+
- **fcn**: `[in] int32_t (*fcn)( float )` unary function.
460+
461+
```c
462+
void stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) );
463+
```
464+
425465
#### STDLIB_MATH_BASE_NAPI_MODULE_D_D( fcn )
426466

427467
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning double-precision floating-point numbers.
@@ -620,6 +660,29 @@ The macro expects the following arguments:
620660

621661
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
622662

663+
#### STDLIB_MATH_BASE_NAPI_MODULE_F_I( fcn )
664+
665+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting a single-precision floating-point number and returning a signed 32-bit integer.
666+
667+
```c
668+
#include <stdint.h>
669+
670+
static int32_t fcn( const float x ) {
671+
// ...
672+
}
673+
674+
// ...
675+
676+
// Register a Node-API module:
677+
STDLIB_MATH_BASE_NAPI_MODULE_F_I( fcn );
678+
```
679+
680+
The macro expects the following arguments:
681+
682+
- **fcn**: `int32_t (*fcn)( float )` unary function.
683+
684+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
685+
623686
</section>
624687
625688
<!-- /.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
@@ -375,6 +375,48 @@
375375
}; \
376376
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_i_d_init )
377377

378+
/**
379+
* Macro for registering a Node-API module exporting an interface invoking a unary function accepting a single-precision floating-point number and returning a signed 32-bit integer.
380+
*
381+
* @param fcn unary function
382+
*
383+
* @example
384+
* #include <stdint.h>
385+
*
386+
* static int32_t fcn( const float x ) {
387+
* // ...
388+
* }
389+
*
390+
* // ...
391+
*
392+
* // Register a Node-API module:
393+
* STDLIB_MATH_BASE_NAPI_MODULE_F_I( fcn );
394+
*/
395+
#define STDLIB_MATH_BASE_NAPI_MODULE_F_I( fcn ) \
396+
static napi_value stdlib_math_base_napi_f_i_wrapper( \
397+
napi_env env, \
398+
napi_callback_info info \
399+
) { \
400+
return stdlib_math_base_napi_f_i( env, info, fcn ); \
401+
}; \
402+
static napi_value stdlib_math_base_napi_f_i_init( \
403+
napi_env env, \
404+
napi_value exports \
405+
) { \
406+
napi_value fcn; \
407+
napi_status status = napi_create_function( \
408+
env, \
409+
"exports", \
410+
NAPI_AUTO_LENGTH, \
411+
stdlib_math_base_napi_f_i_wrapper, \
412+
NULL, \
413+
&fcn \
414+
); \
415+
assert( status == napi_ok ); \
416+
return fcn; \
417+
}; \
418+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_f_i_init )
419+
378420
/*
379421
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
380422
*/
@@ -422,6 +464,11 @@ napi_value stdlib_math_base_napi_i_i( napi_env env, napi_callback_info info, int
422464
*/
423465
napi_value stdlib_math_base_napi_i_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t ) );
424466

467+
/**
468+
* Invokes a unary function accepting a single-precision floating-point number and returning a signed 32-bit integer.
469+
*/
470+
napi_value stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) );
471+
425472
#ifdef __cplusplus
426473
}
427474
#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
@@ -599,3 +599,51 @@ napi_value stdlib_math_base_napi_i_d( napi_env env, napi_callback_info info, dou
599599

600600
return v;
601601
}
602+
603+
/**
604+
* Invokes a unary function accepting a single-precision floating-point number and returning a signed 32-bit integer.
605+
*
606+
* ## Notes
607+
*
608+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
609+
*
610+
* - `x`: input value.
611+
*
612+
* @param env environment under which the function is invoked
613+
* @param info callback data
614+
* @param fcn unary function
615+
* @return function return value as a Node-API signed 32-bit integer
616+
*/
617+
napi_value stdlib_math_base_napi_f_i( napi_env env, napi_callback_info info, int32_t (*fcn)( float ) ) {
618+
napi_status status;
619+
620+
size_t argc = 1;
621+
napi_value argv[ 1 ];
622+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
623+
assert( status == napi_ok );
624+
625+
if ( argc < 1 ) {
626+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." );
627+
assert( status == napi_ok );
628+
return NULL;
629+
}
630+
631+
napi_valuetype vtype0;
632+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
633+
assert( status == napi_ok );
634+
if ( vtype0 != napi_number ) {
635+
status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." );
636+
assert( status == napi_ok );
637+
return NULL;
638+
}
639+
640+
double x;
641+
status = napi_get_value_double( env, argv[ 0 ], &x );
642+
assert( status == napi_ok );
643+
644+
napi_value v;
645+
status = napi_create_int32( env, (int32_t)fcn( (float)x ), &v );
646+
assert( status == napi_ok );
647+
648+
return v;
649+
}

0 commit comments

Comments
 (0)