Skip to content

Commit 46cda32

Browse files
gunjjoshikgryte
andauthored
feat: add LL_D in math/base/napi/binary
PR-URL: #2726 Ref: #2206 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 43439d0 commit 46cda32

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
@@ -555,6 +555,46 @@ The function accepts the following arguments:
555555
void stdlib_math_base_napi_ci_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( stdlib_complex64_t, int32_t ) );
556556
```
557557
558+
#### stdlib_math_base_napi_ll_d( env, info, fcn )
559+
560+
Invokes a binary function accepting signed 64-bit integers and returning a double-precision floating-point number.
561+
562+
```c
563+
#include <node_api.h>
564+
#include <stdint.h>
565+
566+
// ...
567+
568+
static double fcn( const int64_t x, const int64_t y ) {
569+
// ...
570+
}
571+
572+
// ...
573+
574+
/**
575+
* Receives JavaScript callback invocation data.
576+
*
577+
* @param env environment under which the function is invoked
578+
* @param info callback data
579+
* @return Node-API value
580+
*/
581+
napi_value addon( napi_env env, napi_callback_info info ) {
582+
return stdlib_math_base_napi_ll_d( env, info, fcn );
583+
}
584+
585+
// ...
586+
```
587+
588+
The function accepts the following arguments:
589+
590+
- **env**: `[in] napi_env` environment under which the function is invoked.
591+
- **info**: `[in] napi_callback_info` callback data.
592+
- **fcn**: `[in] double (*fcn)( int64_t, int64_t )` binary function.
593+
594+
```c
595+
void stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, double (*fcn)( int64_t, int64_t ) );
596+
```
597+
558598
#### STDLIB_MATH_BASE_NAPI_MODULE_DD_D( fcn )
559599
560600
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning double-precision floating-point numbers.
@@ -897,6 +937,29 @@ The macro expects the following arguments:
897937
898938
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
899939
940+
#### STDLIB_MATH_BASE_NAPI_MODULE_LL_D( fcn )
941+
942+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting signed 64-bit integers and returning a double-precision floating-point number.
943+
944+
```c
945+
#include <stdint.h>
946+
947+
static double fcn( const int64_t x, const int64_t y ) {
948+
// ...
949+
}
950+
951+
// ...
952+
953+
// Register a Node-API module:
954+
STDLIB_MATH_BASE_NAPI_MODULE_II_D( fcn );
955+
```
956+
957+
The macro expects the following arguments:
958+
959+
- **fcn**: `double (*fcn)( int64_t, int64_t )` binary function.
960+
961+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
962+
900963
</section>
901964

902965
<!-- /.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
@@ -570,6 +570,48 @@
570570
}; \
571571
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_cf_c_init )
572572

573+
/**
574+
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting signed 64-bit integers and returning a double-precision floating-point number.
575+
*
576+
* @param fcn binary function
577+
*
578+
* @example
579+
* #include <stdint.h>
580+
*
581+
* static double fcn( const int_64 x, const int_64 y ) {
582+
* // ...
583+
* }
584+
*
585+
* // ...
586+
*
587+
* // Register a Node-API module:
588+
* STDLIB_MATH_BASE_NAPI_MODULE_LL_D( fcn );
589+
*/
590+
#define STDLIB_MATH_BASE_NAPI_MODULE_LL_D( fcn ) \
591+
static napi_value stdlib_math_base_napi_ll_d_wrapper( \
592+
napi_env env, \
593+
napi_callback_info info \
594+
) { \
595+
return stdlib_math_base_napi_ll_d( env, info, fcn ); \
596+
}; \
597+
static napi_value stdlib_math_base_napi_ll_d_init( \
598+
napi_env env, \
599+
napi_value exports \
600+
) { \
601+
napi_value fcn; \
602+
napi_status status = napi_create_function( \
603+
env, \
604+
"exports", \
605+
NAPI_AUTO_LENGTH, \
606+
stdlib_math_base_napi_ll_d_wrapper, \
607+
NULL, \
608+
&fcn \
609+
); \
610+
assert( status == napi_ok ); \
611+
return fcn; \
612+
}; \
613+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_ll_d_init )
614+
573615
/*
574616
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
575617
*/
@@ -637,6 +679,11 @@ napi_value stdlib_math_base_napi_zd_z( napi_env env, napi_callback_info info, st
637679
*/
638680
napi_value stdlib_math_base_napi_cf_c( napi_env env, napi_callback_info info, stdlib_complex64_t (*fcn)( stdlib_complex64_t, float ) );
639681

682+
/**
683+
* Invokes a binary function accepting signed 64-bit integers and returning a double-precision floating-point number.
684+
*/
685+
napi_value stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, double (*fcn)( int64_t, int64_t ) );
686+
640687
#ifdef __cplusplus
641688
}
642689
#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
@@ -1185,3 +1185,65 @@ napi_value stdlib_math_base_napi_cf_c( napi_env env, napi_callback_info info, st
11851185

11861186
return obj;
11871187
}
1188+
1189+
/**
1190+
* Invokes a binary function accepting signed 64-bit integers and returning a double-precision floating-point number.
1191+
*
1192+
* ## Notes
1193+
*
1194+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
1195+
*
1196+
* - `x`: input value.
1197+
* - `y`: input value.
1198+
*
1199+
* @param env environment under which the function is invoked
1200+
* @param info callback data
1201+
* @param fcn binary function
1202+
* @return function return value as a Node-API double-precision floating-point number
1203+
*/
1204+
napi_value stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, double (*fcn)( int64_t, int64_t ) ) {
1205+
napi_status status;
1206+
1207+
size_t argc = 2;
1208+
napi_value argv[ 2 ];
1209+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
1210+
assert( status == napi_ok );
1211+
1212+
if ( argc < 2 ) {
1213+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." );
1214+
assert( status == napi_ok );
1215+
return NULL;
1216+
}
1217+
1218+
napi_valuetype vtype0;
1219+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
1220+
assert( status == napi_ok );
1221+
if ( vtype0 != napi_number ) {
1222+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
1223+
assert( status == napi_ok );
1224+
return NULL;
1225+
}
1226+
1227+
napi_valuetype vtype1;
1228+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
1229+
assert( status == napi_ok );
1230+
if ( vtype1 != napi_number ) {
1231+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
1232+
assert( status == napi_ok );
1233+
return NULL;
1234+
}
1235+
1236+
int64_t x;
1237+
status = napi_get_value_int64( env, argv[ 0 ], &x );
1238+
assert( status == napi_ok );
1239+
1240+
int64_t y;
1241+
status = napi_get_value_int64( env, argv[ 1 ], &y );
1242+
assert( status == napi_ok );
1243+
1244+
napi_value v;
1245+
status = napi_create_double( env, fcn( x, y ), &v );
1246+
assert( status == napi_ok );
1247+
1248+
return v;
1249+
}

0 commit comments

Comments
 (0)