Skip to content

Commit c5b26dc

Browse files
aman-095Pranavchikukgryte
authored
feat: add Node-API macros for binary functions
PR-URL: #1687 --------- Signed-off-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Signed-off-by: Athan Reines <kgryte@gmail.com> Co-authored-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Pranav <85227306+Pranavchiku@users.noreply.github.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 69c167d commit c5b26dc

File tree

3 files changed

+336
-0
lines changed

3 files changed

+336
-0
lines changed

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

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,86 @@ 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_ii_i( env, info, fcn )
335+
336+
Invokes a binary function accepting and returning signed 32-bit integers.
337+
338+
```c
339+
#include <node_api.h>
340+
#include <stdint.h>
341+
342+
// ...
343+
344+
static int32_t mul( const int32_t x, const int32_t 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_ii_i( 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] int32_t (*fcn)( int32_t, int32_t )` binary function.
369+
370+
```c
371+
void stdlib_math_base_napi_ii_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t, int32_t ) );
372+
```
373+
374+
#### stdlib_math_base_napi_ii_d( env, info, fcn )
375+
376+
Invokes a binary function accepting signed 32-bit integers and returning a double-precision floating-point number.
377+
378+
```c
379+
#include <node_api.h>
380+
#include <stdint.h>
381+
382+
// ...
383+
384+
static double mul( const int32_t x, const int32_t y ) {
385+
return x * y;
386+
}
387+
388+
// ...
389+
390+
/**
391+
* Receives JavaScript callback invocation data.
392+
*
393+
* @param env environment under which the function is invoked
394+
* @param info callback data
395+
* @return Node-API value
396+
*/
397+
napi_value addon( napi_env env, napi_callback_info info ) {
398+
return stdlib_math_base_napi_ii_d( env, info, mul );
399+
}
400+
401+
// ...
402+
```
403+
404+
The function accepts the following arguments:
405+
406+
- **env**: `[in] napi_env` environment under which the function is invoked.
407+
- **info**: `[in] napi_callback_info` callback data.
408+
- **fcn**: `[in] double (*fcn)( int32_t, int32_t )` binary function.
409+
410+
```c
411+
void stdlib_math_base_napi_ii_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, int32_t ) );
412+
```
413+
334414
#### stdlib_math_base_napi_fi_f( env, info, fcn )
335415
336416
Invokes a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number.
@@ -496,6 +576,48 @@ The macro expects the following arguments:
496576

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

579+
#### STDLIB_MATH_BASE_NAPI_MODULE_II_I( fcn )
580+
581+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning signed 32-bit integers.
582+
583+
```c
584+
static int32_t add( const int32_t x, const int32_t y ) {
585+
return x + y;
586+
}
587+
588+
// ...
589+
590+
// Register a Node-API module:
591+
STDLIB_MATH_BASE_NAPI_MODULE_II_I( add );
592+
```
593+
594+
The macro expects the following arguments:
595+
596+
- **fcn**: `int32_t (*fcn)( int32_t, int32_t )` binary function.
597+
598+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
599+
600+
#### STDLIB_MATH_BASE_NAPI_MODULE_II_D( fcn )
601+
602+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting signed 32-bit integers and returning a double-precision floating-point number.
603+
604+
```c
605+
static double add( const int32_t x, const int32_t y ) {
606+
return x + y;
607+
}
608+
609+
// ...
610+
611+
// Register a Node-API module:
612+
STDLIB_MATH_BASE_NAPI_MODULE_II_D( add );
613+
```
614+
615+
The macro expects the following arguments:
616+
617+
- **fcn**: `double (*fcn)( int32_t, int32_t )` binary function.
618+
619+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
620+
499621
#### STDLIB_MATH_BASE_NAPI_MODULE_FF_F( fcn )
500622

501623
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning single-precision floating-point numbers.

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,86 @@
6464
}; \
6565
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_dd_d_init )
6666

67+
/**
68+
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting and returning signed 32-bit integers.
69+
*
70+
* @param fcn binary function
71+
*
72+
* @example
73+
* static int_32 add( const int_32 x, const int_32 y ) {
74+
* return x + y;
75+
* }
76+
*
77+
* // ...
78+
*
79+
* // Register a Node-API module:
80+
* STDLIB_MATH_BASE_NAPI_MODULE_II_I( add );
81+
*/
82+
#define STDLIB_MATH_BASE_NAPI_MODULE_II_I( fcn ) \
83+
static napi_value stdlib_math_base_napi_ii_i_wrapper( \
84+
napi_env env, \
85+
napi_callback_info info \
86+
) { \
87+
return stdlib_math_base_napi_ii_i( env, info, fcn ); \
88+
}; \
89+
static napi_value stdlib_math_base_napi_ii_i_init( \
90+
napi_env env, \
91+
napi_value exports \
92+
) { \
93+
napi_value fcn; \
94+
napi_status status = napi_create_function( \
95+
env, \
96+
"exports", \
97+
NAPI_AUTO_LENGTH, \
98+
stdlib_math_base_napi_ii_i_wrapper, \
99+
NULL, \
100+
&fcn \
101+
); \
102+
assert( status == napi_ok ); \
103+
return fcn; \
104+
}; \
105+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_ii_i_init )
106+
107+
/**
108+
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting signed 32-bit integers and returning a double-precision floating-point number.
109+
*
110+
* @param fcn binary function
111+
*
112+
* @example
113+
* static double add( const int_32 x, const int_32 y ) {
114+
* return x + y;
115+
* }
116+
*
117+
* // ...
118+
*
119+
* // Register a Node-API module:
120+
* STDLIB_MATH_BASE_NAPI_MODULE_II_D( add );
121+
*/
122+
#define STDLIB_MATH_BASE_NAPI_MODULE_II_D( fcn ) \
123+
static napi_value stdlib_math_base_napi_ii_d_wrapper( \
124+
napi_env env, \
125+
napi_callback_info info \
126+
) { \
127+
return stdlib_math_base_napi_ii_d( env, info, fcn ); \
128+
}; \
129+
static napi_value stdlib_math_base_napi_ii_d_init( \
130+
napi_env env, \
131+
napi_value exports \
132+
) { \
133+
napi_value fcn; \
134+
napi_status status = napi_create_function( \
135+
env, \
136+
"exports", \
137+
NAPI_AUTO_LENGTH, \
138+
stdlib_math_base_napi_ii_d_wrapper, \
139+
NULL, \
140+
&fcn \
141+
); \
142+
assert( status == napi_ok ); \
143+
return fcn; \
144+
}; \
145+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_ii_d_init )
146+
67147
/**
68148
* Macro for registering a Node-API module exporting an interface invoking a binary function accepting and returning single-precision floating-point numbers.
69149
*
@@ -522,6 +602,16 @@ napi_value stdlib_math_base_napi_cc_c( napi_env env, napi_callback_info info, st
522602
*/
523603
napi_value stdlib_math_base_napi_di_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t ) );
524604

605+
/**
606+
* Invokes a binary function accepting signed 32-bit integers and returning a double-precision floating-point number.
607+
*/
608+
napi_value stdlib_math_base_napi_ii_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, int32_t ) );
609+
610+
/**
611+
* Invokes a binary function accepting and returning signed 32-bit integers.
612+
*/
613+
napi_value stdlib_math_base_napi_ii_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t, int32_t ) );
614+
525615
/**
526616
* Invokes a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number.
527617
*/

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

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,130 @@ 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 signed 32-bit integers 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_ii_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, int32_t ) ) {
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+
int32_t y;
579+
status = napi_get_value_int32( 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+
589+
/**
590+
* Invokes a binary function accepting and returning signed 32-bit integers.
591+
*
592+
* ## Notes
593+
*
594+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
595+
*
596+
* - `x`: input value.
597+
* - `y`: input value.
598+
*
599+
* @param env environment under which the function is invoked
600+
* @param info callback data
601+
* @param fcn binary function
602+
* @return function return value as a Node-API signed 32-bit integer
603+
*/
604+
napi_value stdlib_math_base_napi_ii_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t, int32_t ) ) {
605+
napi_status status;
606+
607+
size_t argc = 2;
608+
napi_value argv[ 2 ];
609+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
610+
assert( status == napi_ok );
611+
612+
if ( argc < 2 ) {
613+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." );
614+
assert( status == napi_ok );
615+
return NULL;
616+
}
617+
618+
napi_valuetype vtype0;
619+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
620+
assert( status == napi_ok );
621+
if ( vtype0 != napi_number ) {
622+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
623+
assert( status == napi_ok );
624+
return NULL;
625+
}
626+
627+
napi_valuetype vtype1;
628+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
629+
assert( status == napi_ok );
630+
if ( vtype1 != napi_number ) {
631+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
632+
assert( status == napi_ok );
633+
return NULL;
634+
}
635+
636+
int32_t x;
637+
status = napi_get_value_int32( env, argv[ 0 ], &x );
638+
assert( status == napi_ok );
639+
640+
int32_t y;
641+
status = napi_get_value_int32( env, argv[ 1 ], &y );
642+
assert( status == napi_ok );
643+
644+
napi_value v;
645+
status = napi_create_int32( env, fcn( x, y ), &v );
646+
assert( status == napi_ok );
647+
648+
return v;
649+
}
650+
527651
/**
528652
* Invokes a binary function accepting a single-precision floating-point number and a signed 32-bit integer and returning a single-precision floating-point number.
529653
*

0 commit comments

Comments
 (0)