Skip to content

Commit 665d028

Browse files
feat: add STDLIB_MATH_BASE_NAPI_MODULE_IID_D macro in math/base/napi/ternary
PR-URL: #4523 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 515162f commit 665d028

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,46 @@ The function accepts the following arguments:
222222
void stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, int32_t ) );
223223
```
224224

225+
#### stdlib_math_base_napi_iid_d( env, info, fcn )
226+
227+
Invokes a ternary function accepting two signed 32-bit integers and a double-precision floating-point number and returning a double-precision floating-point number.
228+
229+
```c
230+
#include <node_api.h>
231+
#include <stdint.h>
232+
233+
// ...
234+
235+
static double fcn( const int32_t x, const int32_t y, const double z ) {
236+
// ...
237+
}
238+
239+
// ...
240+
241+
/**
242+
* Receives JavaScript callback invocation data.
243+
*
244+
* @param env environment under which the function is invoked
245+
* @param info callback data
246+
* @return Node-API value
247+
*/
248+
napi_value addon( napi_env env, napi_callback_info info ) {
249+
return stdlib_math_base_napi_iid_d( env, info, fcn );
250+
}
251+
252+
// ...
253+
```
254+
255+
The function accepts the following arguments:
256+
257+
- **env**: `[in] napi_env` environment under which the function is invoked.
258+
- **info**: `[in] napi_callback_info` callback data.
259+
- **fcn**: `[in] double (*fcn)( int32_t, int32_t, double )` ternary function.
260+
261+
```c
262+
void stdlib_math_base_napi_iid_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, int32_t, double ) );
263+
```
264+
225265
#### STDLIB_MATH_BASE_NAPI_MODULE_DDD_D( fcn )
226266

227267
Macro for registering a Node-API module exporting an interface for invoking a ternary function accepting and returning double-precision floating-point numbers.
@@ -287,6 +327,29 @@ The macro expects the following arguments:
287327
288328
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
289329
330+
#### STDLIB_MATH_BASE_NAPI_MODULE_IID_D( fcn )
331+
332+
Macro for registering a Node-API module exporting an interface for invoking a ternary function accepting two signed 32-bit integers and a double-precision floating-point number and returning a double-precision floating-point number.
333+
334+
```c
335+
#include <stdint.h>
336+
337+
static double fcn( const int32_t x, const int32_t y, const double z ) {
338+
// ...
339+
}
340+
341+
// ...
342+
343+
// Register a Node-API module:
344+
STDLIB_MATH_BASE_NAPI_MODULE_IID_D( fcn );
345+
```
346+
347+
The macro expects the following arguments:
348+
349+
- **fcn**: `double (*fcn)( int32_t, int32_t, double )` ternary function.
350+
351+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
352+
290353
</section>
291354

292355
<!-- /.usage -->

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,48 @@
144144
}; \
145145
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_dii_d_init )
146146

147+
/**
148+
* Macro for registering a Node-API module exporting an interface invoking a ternary function accepting two signed 32-bit integers and a double-precision floating-point number and returning a double-precision floating-point number.
149+
*
150+
* @param fcn ternary function
151+
*
152+
* @example
153+
* #include <stdint.h>
154+
*
155+
* static double fcn( const int_32 x, const int_32 y, const double z ) {
156+
* // ...
157+
* }
158+
*
159+
* // ...
160+
*
161+
* // Register a Node-API module:
162+
* STDLIB_MATH_BASE_NAPI_MODULE_IID_D( fcn );
163+
*/
164+
#define STDLIB_MATH_BASE_NAPI_MODULE_IID_D( fcn ) \
165+
static napi_value stdlib_math_base_napi_iid_d_wrapper( \
166+
napi_env env, \
167+
napi_callback_info info \
168+
) { \
169+
return stdlib_math_base_napi_iid_d( env, info, fcn ); \
170+
}; \
171+
static napi_value stdlib_math_base_napi_iid_d_init( \
172+
napi_env env, \
173+
napi_value exports \
174+
) { \
175+
napi_value fcn; \
176+
napi_status status = napi_create_function( \
177+
env, \
178+
"exports", \
179+
NAPI_AUTO_LENGTH, \
180+
stdlib_math_base_napi_iid_d_wrapper, \
181+
NULL, \
182+
&fcn \
183+
); \
184+
assert( status == napi_ok ); \
185+
return fcn; \
186+
}; \
187+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_iid_d_init )
188+
147189
/*
148190
* If C++, prevent name mangling so that the compiler emits a ternary file having undecorated names, thus mirroring the behavior of a C compiler.
149191
*/
@@ -166,6 +208,11 @@ napi_value stdlib_math_base_napi_fff_f( napi_env env, napi_callback_info info, f
166208
*/
167209
napi_value stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, int32_t ) );
168210

211+
/**
212+
* Invokes a ternary function accepting two signed 32-bit integers and a double-precision floating-point number and returning a double-precision floating-point number.
213+
*/
214+
napi_value stdlib_math_base_napi_iid_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, int32_t, double ) );
215+
169216
#ifdef __cplusplus
170217
}
171218
#endif

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,79 @@ napi_value stdlib_math_base_napi_dii_d( napi_env env, napi_callback_info info, d
248248

249249
return v;
250250
}
251+
252+
/**
253+
* Invokes a ternary function accepting two signed 32-bit integers and a double-precision floating-point number and returning a double-precision floating-point number.
254+
*
255+
* ## Notes
256+
*
257+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
258+
*
259+
* - `x`: input value.
260+
* - `y`: input value.
261+
* - `z`: input value.
262+
*
263+
* @param env environment under which the function is invoked
264+
* @param info callback data
265+
* @param fcn ternary function
266+
* @return function return value as a Node-API double-precision floating-point number
267+
*/
268+
napi_value stdlib_math_base_napi_iid_d( napi_env env, napi_callback_info info, double (*fcn)( int32_t, int32_t, double ) ) {
269+
napi_status status;
270+
271+
size_t argc = 3;
272+
napi_value argv[ 3 ];
273+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
274+
assert( status == napi_ok );
275+
276+
if ( argc < 3 ) {
277+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide three numbers." );
278+
assert( status == napi_ok );
279+
return NULL;
280+
}
281+
282+
napi_valuetype vtype0;
283+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
284+
assert( status == napi_ok );
285+
if ( vtype0 != napi_number ) {
286+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
287+
assert( status == napi_ok );
288+
return NULL;
289+
}
290+
291+
napi_valuetype vtype1;
292+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
293+
assert( status == napi_ok );
294+
if ( vtype1 != napi_number ) {
295+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
296+
assert( status == napi_ok );
297+
return NULL;
298+
}
299+
300+
napi_valuetype vtype2;
301+
status = napi_typeof( env, argv[ 2 ], &vtype2 );
302+
assert( status == napi_ok );
303+
if ( vtype2 != napi_number ) {
304+
status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
305+
assert( status == napi_ok );
306+
return NULL;
307+
}
308+
309+
int32_t x;
310+
status = napi_get_value_int32( env, argv[ 1 ], &x );
311+
assert( status == napi_ok );
312+
313+
int32_t y;
314+
status = napi_get_value_int32( env, argv[ 2 ], &y );
315+
assert( status == napi_ok );
316+
317+
double z;
318+
status = napi_get_value_double( env, argv[ 0 ], &z );
319+
assert( status == napi_ok );
320+
321+
napi_value v;
322+
status = napi_create_double( env, fcn( x, y, z ), &v );
323+
assert( status == napi_ok );
324+
325+
return v;
326+
}

0 commit comments

Comments
 (0)