Skip to content

Commit 515162f

Browse files
feat: add STDLIB_MATH_BASE_NAPI_MODULE_ID_D macro in math/base/napi/binary
PR-URL: #4522 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 5d255dc commit 515162f

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
@@ -331,6 +331,46 @@ The function accepts the following arguments:
331331
void stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t ) );
332332
```
333333
334+
#### stdlib_math_base_napi_id_d( env, info, fcn )
335+
336+
Invokes a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number.
337+
338+
```c
339+
#include <node_api.h>
340+
#include <stdint.h>
341+
342+
// ...
343+
344+
static double mul( const int32_t x, const double y ) {
345+
return x * y;
346+
}
347+
348+
// ...
349+
350+
/**
351+
* Receives JavaScript callback invocation data.
352+
*
353+
* @param env environment under which the function is invoked
354+
* @param info callback data
355+
* @return Node-API value
356+
*/
357+
napi_value addon( napi_env env, napi_callback_info info ) {
358+
return stdlib_math_base_napi_id_d( env, info, mul );
359+
}
360+
361+
// ...
362+
```
363+
364+
The function accepts the following arguments:
365+
366+
- **env**: `[in] napi_env` environment under which the function is invoked.
367+
- **info**: `[in] napi_callback_info` callback data.
368+
- **fcn**: `[in] double (*fcn)( int32_t, double )` binary function.
369+
370+
```c
371+
void stdlib_math_base_napi_id_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, double ) );
372+
```
373+
334374
#### stdlib_math_base_napi_ii_i( env, info, fcn )
335375
336376
Invokes a binary function accepting and returning signed 32-bit integers.
@@ -820,6 +860,29 @@ The macro expects the following arguments:
820860

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

863+
#### STDLIB_MATH_BASE_NAPI_MODULE_ID_D( fcn )
864+
865+
Macro for registering a Node-API module exporting an interface invoking a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number.
866+
867+
```c
868+
#include <stdint.h>
869+
870+
static double mul( const int32_t x, const double y ) {
871+
return x * y;
872+
}
873+
874+
// ...
875+
876+
// Register a Node-API module:
877+
STDLIB_MATH_BASE_NAPI_MODULE_ID_D( mul );
878+
```
879+
880+
The macro expects the following arguments:
881+
882+
- **fcn**: `double (*fcn)( int32_t, double )` binary function.
883+
884+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
885+
823886
#### STDLIB_MATH_BASE_NAPI_MODULE_FI_F( fcn )
824887
825888
Macro for registering a Node-API module exporting an interface invoking a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number.

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
@@ -342,6 +342,48 @@
342342
}; \
343343
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_di_d_init )
344344

345+
/**
346+
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number.
347+
*
348+
* @param fcn binary function
349+
*
350+
* @example
351+
* #include <stdint.h>
352+
*
353+
* static double mul( const int32_t n, const double x ) {
354+
* return x * n;
355+
* }
356+
*
357+
* // ...
358+
*
359+
* // Register a Node-API module:
360+
* STDLIB_MATH_BASE_NAPI_MODULE_ID_D( mul );
361+
*/
362+
#define STDLIB_MATH_BASE_NAPI_MODULE_ID_D( fcn ) \
363+
static napi_value stdlib_math_base_napi_id_d_wrapper( \
364+
napi_env env, \
365+
napi_callback_info info \
366+
) { \
367+
return stdlib_math_base_napi_id_d( env, info, fcn ); \
368+
}; \
369+
static napi_value stdlib_math_base_napi_id_d_init( \
370+
napi_env env, \
371+
napi_value exports \
372+
) { \
373+
napi_value fcn; \
374+
napi_status status = napi_create_function( \
375+
env, \
376+
"exports", \
377+
NAPI_AUTO_LENGTH, \
378+
stdlib_math_base_napi_id_d_wrapper, \
379+
NULL, \
380+
&fcn \
381+
); \
382+
assert( status == napi_ok ); \
383+
return fcn; \
384+
}; \
385+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_id_d_init )
386+
345387
/**
346388
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number.
347389
*
@@ -690,6 +732,11 @@ napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, st
690732
*/
691733
napi_value stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t ) );
692734

735+
/**
736+
* Invokes a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number.
737+
*/
738+
napi_value stdlib_math_base_napi_id_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, double ) );
739+
693740
/**
694741
* Invokes a binary function accepting signed 32-bit integers and returning a double-precision floating-point number.
695742
*/

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
@@ -524,6 +524,68 @@ napi_value stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, do
524524
return v;
525525
}
526526

527+
/**
528+
* Invokes a binary function accepting a signed 32-bit integer and a double-precision floating-point number and returning a double-precision floating-point number.
529+
*
530+
* ## Notes
531+
*
532+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
533+
*
534+
* - `x`: input value.
535+
* - `y`: input value.
536+
*
537+
* @param env environment under which the function is invoked
538+
* @param info callback data
539+
* @param fcn binary function
540+
* @return function return value as a Node-API double-precision floating-point number
541+
*/
542+
napi_value stdlib_math_base_napi_id_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, double ) ) {
543+
napi_status status;
544+
545+
size_t argc = 2;
546+
napi_value argv[ 2 ];
547+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
548+
assert( status == napi_ok );
549+
550+
if ( argc < 2 ) {
551+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." );
552+
assert( status == napi_ok );
553+
return NULL;
554+
}
555+
556+
napi_valuetype vtype0;
557+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
558+
assert( status == napi_ok );
559+
if ( vtype0 != napi_number ) {
560+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
561+
assert( status == napi_ok );
562+
return NULL;
563+
}
564+
565+
napi_valuetype vtype1;
566+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
567+
assert( status == napi_ok );
568+
if ( vtype1 != napi_number ) {
569+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
570+
assert( status == napi_ok );
571+
return NULL;
572+
}
573+
574+
int32_t x;
575+
status = napi_get_value_int32( env, argv[ 0 ], &x );
576+
assert( status == napi_ok );
577+
578+
double y;
579+
status = napi_get_value_double( env, argv[ 1 ], &y );
580+
assert( status == napi_ok );
581+
582+
napi_value v;
583+
status = napi_create_double( env, fcn( x, y ), &v );
584+
assert( status == napi_ok );
585+
586+
return v;
587+
}
588+
527589
/**
528590
* Invokes a binary function accepting signed 32-bit integers and returning a double-precision floating-point number.
529591
*

0 commit comments

Comments
 (0)