Skip to content

Commit 74f3c08

Browse files
gunjjoshikgryte
andauthored
feat: add dii_d in math/base/napi/ternary
PR-URL: #2546 Ref: #2544 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 3efb708 commit 74f3c08

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

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

+63
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,46 @@ The function accepts the following arguments:
182182
void stdlib_math_base_napi_fff_f( napi_env env, napi_callback_info info, float (*fcn)( float, float, float ) );
183183
```
184184

185+
#### stdlib_math_base_napi_dii_d( env, info, fcn )
186+
187+
Invokes a ternary function accepting a double-precision floating-point number and two signed 32-bit integers and returning a double-precision floating-point number.
188+
189+
```c
190+
#include <node_api.h>
191+
#include <stdint.h>
192+
193+
// ...
194+
195+
static double fcn( const double x, const int32_t y, const int32_t z ) {
196+
// ...
197+
}
198+
199+
// ...
200+
201+
/**
202+
* Receives JavaScript callback invocation data.
203+
*
204+
* @param env environment under which the function is invoked
205+
* @param info callback data
206+
* @return Node-API value
207+
*/
208+
napi_value addon( napi_env env, napi_callback_info info ) {
209+
return stdlib_math_base_napi_dii_d( env, info, fcn );
210+
}
211+
212+
// ...
213+
```
214+
215+
The function accepts the following arguments:
216+
217+
- **env**: `[in] napi_env` environment under which the function is invoked.
218+
- **info**: `[in] napi_callback_info` callback data.
219+
- **fcn**: `[in] double (*fcn)( double, int32_t, int32_t )` ternary function.
220+
221+
```c
222+
void stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, int32_t ) );
223+
```
224+
185225
#### STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( fcn )
186226

187227
Macro for registering a Node-API module exporting an interface for invoking a ternary function accepting and returning double-precision floating-point numbers.
@@ -224,6 +264,29 @@ The macro expects the following arguments:
224264

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

267+
#### STDLIB_MATH_BASE_NAPI_MODULE_DII_D( fcn )
268+
269+
Macro for registering a Node-API module exporting an interface for invoking a ternary function accepting a double-precision floating-point number and two signed 32-bit integers and returning a double-precision floating-point number.
270+
271+
```c
272+
#include <stdint.h>
273+
274+
static double fcn( const double x, const int32_t y, const int32_t z ) {
275+
// ...
276+
}
277+
278+
// ...
279+
280+
// Register a Node-API module:
281+
STDLIB_MATH_BASE_NAPI_MODULE_DII_D( fcn );
282+
```
283+
284+
The macro expects the following arguments:
285+
286+
- **fcn**: `double (*fcn)( double, int32_t, int32_t )` ternary function.
287+
288+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
289+
227290
</section>
228291
229292
<!-- /.usage -->

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

+47
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@
102102
}; \
103103
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_fff_f_init )
104104

105+
/**
106+
* Macro for registering a Node-API module exporting an interface invoking a ternary function accepting a double-precision floating-point number and two signed 32-bit integers and returning a double-precision floating-point number.
107+
*
108+
* @param fcn ternary function
109+
*
110+
* @example
111+
* #include <stdint.h>
112+
*
113+
* static double fcn( const double x, const int_32 y, const int_32 z ) {
114+
* // ...
115+
* }
116+
*
117+
* // ...
118+
*
119+
* // Register a Node-API module:
120+
* STDLIB_MATH_BASE_NAPI_MODULE_DII_D( fcn );
121+
*/
122+
#define STDLIB_MATH_BASE_NAPI_MODULE_DII_D( fcn ) \
123+
static napi_value stdlib_math_base_napi_dii_d_wrapper( \
124+
napi_env env, \
125+
napi_callback_info info \
126+
) { \
127+
return stdlib_math_base_napi_dii_d( env, info, fcn ); \
128+
}; \
129+
static napi_value stdlib_math_base_napi_dii_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_dii_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_dii_d_init )
146+
105147
/*
106148
* If C++, prevent name mangling so that the compiler emits a ternary file having undecorated names, thus mirroring the behavior of a C compiler.
107149
*/
@@ -119,6 +161,11 @@ napi_value stdlib_math_base_napi_ddd_d( napi_env env, napi_callback_info info, d
119161
*/
120162
napi_value stdlib_math_base_napi_fff_f( napi_env env, napi_callback_info info, float (*fcn)( float, float, float ) );
121163

164+
/**
165+
* Invokes a ternary function accepting a double-precision floating-point number and two signed 32-bit integers and returning a double-precision floating-point number.
166+
*/
167+
napi_value stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, int32_t ) );
168+
122169
#ifdef __cplusplus
123170
}
124171
#endif

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

+76
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,79 @@ napi_value stdlib_math_base_napi_fff_f( napi_env env, napi_callback_info info, f
172172

173173
return v;
174174
}
175+
176+
/**
177+
* Invokes a ternary function accepting a double-precision floating-point number and two signed 32-bit integers and returning a double-precision floating-point number.
178+
*
179+
* ## Notes
180+
*
181+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
182+
*
183+
* - `x`: input value.
184+
* - `y`: input value.
185+
* - `z`: input value.
186+
*
187+
* @param env environment under which the function is invoked
188+
* @param info callback data
189+
* @param fcn ternary function
190+
* @return function return value as a Node-API double-precision floating-point number
191+
*/
192+
napi_value stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, int32_t ) ) {
193+
napi_status status;
194+
195+
size_t argc = 3;
196+
napi_value argv[ 3 ];
197+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
198+
assert( status == napi_ok );
199+
200+
if ( argc < 3 ) {
201+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide three numbers." );
202+
assert( status == napi_ok );
203+
return NULL;
204+
}
205+
206+
napi_valuetype vtype0;
207+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
208+
assert( status == napi_ok );
209+
if ( vtype0 != napi_number ) {
210+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
211+
assert( status == napi_ok );
212+
return NULL;
213+
}
214+
215+
napi_valuetype vtype1;
216+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
217+
assert( status == napi_ok );
218+
if ( vtype1 != napi_number ) {
219+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
220+
assert( status == napi_ok );
221+
return NULL;
222+
}
223+
224+
napi_valuetype vtype2;
225+
status = napi_typeof( env, argv[ 2 ], &vtype2 );
226+
assert( status == napi_ok );
227+
if ( vtype2 != napi_number ) {
228+
status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
229+
assert( status == napi_ok );
230+
return NULL;
231+
}
232+
233+
double x;
234+
status = napi_get_value_double( env, argv[ 0 ], &x );
235+
assert( status == napi_ok );
236+
237+
int32_t y;
238+
status = napi_get_value_int32( env, argv[ 1 ], &y );
239+
assert( status == napi_ok );
240+
241+
int32_t z;
242+
status = napi_get_value_int32( env, argv[ 2 ], &z );
243+
assert( status == napi_ok );
244+
245+
napi_value v;
246+
status = napi_create_double( env, fcn( x, y, z ), &v );
247+
assert( status == napi_ok );
248+
249+
return v;
250+
}

0 commit comments

Comments
 (0)