Skip to content

Commit 7d8ab5a

Browse files
authored
feat: add ii_f API in math/base/napi/binary
PR-URL: #3315 Reviewed-by: Athan Reines <[email protected]>
1 parent a0ba090 commit 7d8ab5a

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,46 @@ The function accepts the following arguments:
595595
void stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, double (*fcn)( int64_t, int64_t ) );
596596
```
597597
598+
#### stdlib_math_base_napi_ii_f( env, info, fcn )
599+
600+
Invokes a binary function accepting signed 32-bit integers and returning a single-precision floating-point number.
601+
602+
```c
603+
#include <node_api.h>
604+
#include <stdint.h>
605+
606+
// ...
607+
608+
static float fcn( const int32_t x, const int32_t y ) {
609+
// ...
610+
}
611+
612+
// ...
613+
614+
/**
615+
* Receives JavaScript callback invocation data.
616+
*
617+
* @param env environment under which the function is invoked
618+
* @param info callback data
619+
* @return Node-API value
620+
*/
621+
napi_value addon( napi_env env, napi_callback_info info ) {
622+
return stdlib_math_base_napi_ii_f( env, info, fcn );
623+
}
624+
625+
// ...
626+
```
627+
628+
The function accepts the following arguments:
629+
630+
- **env**: `[in] napi_env` environment under which the function is invoked.
631+
- **info**: `[in] napi_callback_info` callback data.
632+
- **fcn**: `[in] float (*fcn)( int32_t, int32_t )` binary function.
633+
634+
```c
635+
void stdlib_math_base_napi_ii_f( napi_env env, napi_callback_info info, float (*fcn)( int32_t, int32_t ) );
636+
```
637+
598638
#### STDLIB_MATH_BASE_NAPI_MODULE_DD_D( fcn )
599639
600640
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning double-precision floating-point numbers.
@@ -964,6 +1004,29 @@ The macro expects the following arguments:
9641004

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

1007+
#### STDLIB_MATH_BASE_NAPI_MODULE_II_F( fcn )
1008+
1009+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting signed 32-bit integers and returning a single-precision floating-point number.
1010+
1011+
```c
1012+
#include <stdint.h>
1013+
1014+
static float fcn( const int32_t x, const int32_t y ) {
1015+
// ...
1016+
}
1017+
1018+
// ...
1019+
1020+
// Register a Node-API module:
1021+
STDLIB_MATH_BASE_NAPI_MODULE_II_F( fcn );
1022+
```
1023+
1024+
The macro expects the following arguments:
1025+
1026+
- **fcn**: `float (*fcn)( int32_t, int32_t )` binary function.
1027+
1028+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
1029+
9671030
</section>
9681031
9691032
<!-- /.usage -->

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,48 @@
616616
}; \
617617
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_ll_d_init )
618618

619+
/**
620+
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting signed 32-bit integers and returning a single-precision floating-point number.
621+
*
622+
* @param fcn binary function
623+
*
624+
* @example
625+
* #include <stdint.h>
626+
*
627+
* static float fcn( const int_32 x, const int_32 y ) {
628+
* // ...
629+
* }
630+
*
631+
* // ...
632+
*
633+
* // Register a Node-API module:
634+
* STDLIB_MATH_BASE_NAPI_MODULE_II_F( fcn );
635+
*/
636+
#define STDLIB_MATH_BASE_NAPI_MODULE_II_F( fcn ) \
637+
static napi_value stdlib_math_base_napi_ii_f_wrapper( \
638+
napi_env env, \
639+
napi_callback_info info \
640+
) { \
641+
return stdlib_math_base_napi_ii_f( env, info, fcn ); \
642+
}; \
643+
static napi_value stdlib_math_base_napi_ii_f_init( \
644+
napi_env env, \
645+
napi_value exports \
646+
) { \
647+
napi_value fcn; \
648+
napi_status status = napi_create_function( \
649+
env, \
650+
"exports", \
651+
NAPI_AUTO_LENGTH, \
652+
stdlib_math_base_napi_ii_f_wrapper, \
653+
NULL, \
654+
&fcn \
655+
); \
656+
assert( status == napi_ok ); \
657+
return fcn; \
658+
}; \
659+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_ii_f_init )
660+
619661
/*
620662
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
621663
*/
@@ -688,6 +730,11 @@ napi_value stdlib_math_base_napi_cf_c( napi_env env, napi_callback_info info, st
688730
*/
689731
napi_value stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, double (*fcn)( int64_t, int64_t ) );
690732

733+
/**
734+
* Invokes a binary function accepting signed 32-bit integers and returning a single-precision floating-point number.
735+
*/
736+
napi_value stdlib_math_base_napi_ii_f( napi_env env, napi_callback_info info, float (*fcn)( int32_t, int32_t ) );
737+
691738
#ifdef __cplusplus
692739
}
693740
#endif

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,65 @@ napi_value stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, do
12471247

12481248
return v;
12491249
}
1250+
1251+
/**
1252+
* Invokes a binary function accepting signed 32-bit integers and returning a single-precision floating-point number.
1253+
*
1254+
* ## Notes
1255+
*
1256+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
1257+
*
1258+
* - `x`: input value.
1259+
* - `y`: input value.
1260+
*
1261+
* @param env environment under which the function is invoked
1262+
* @param info callback data
1263+
* @param fcn binary function
1264+
* @return function return value as a Node-API single-precision floating-point number
1265+
*/
1266+
napi_value stdlib_math_base_napi_ii_f( napi_env env, napi_callback_info info, float (*fcn)( int32_t, int32_t ) ) {
1267+
napi_status status;
1268+
1269+
size_t argc = 2;
1270+
napi_value argv[ 2 ];
1271+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
1272+
assert( status == napi_ok );
1273+
1274+
if ( argc < 2 ) {
1275+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." );
1276+
assert( status == napi_ok );
1277+
return NULL;
1278+
}
1279+
1280+
napi_valuetype vtype0;
1281+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
1282+
assert( status == napi_ok );
1283+
if ( vtype0 != napi_number ) {
1284+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
1285+
assert( status == napi_ok );
1286+
return NULL;
1287+
}
1288+
1289+
napi_valuetype vtype1;
1290+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
1291+
assert( status == napi_ok );
1292+
if ( vtype1 != napi_number ) {
1293+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
1294+
assert( status == napi_ok );
1295+
return NULL;
1296+
}
1297+
1298+
int32_t x;
1299+
status = napi_get_value_int32( env, argv[ 0 ], &x );
1300+
assert( status == napi_ok );
1301+
1302+
int32_t y;
1303+
status = napi_get_value_int32( env, argv[ 1 ], &y );
1304+
assert( status == napi_ok );
1305+
1306+
napi_value v;
1307+
status = napi_create_double( env, (double)fcn( x, y ), &v );
1308+
assert( status == napi_ok );
1309+
1310+
return v;
1311+
}

0 commit comments

Comments
 (0)