Skip to content

Commit 0d9e8ca

Browse files
feat: add STDLIB_MATH_BASE_NAPI_MODULE_DID_D macro in math/base/napi/ternary
PR-URL: #5281 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 5bd3138 commit 0d9e8ca

File tree

5 files changed

+247
-0
lines changed

5 files changed

+247
-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
@@ -263,6 +263,69 @@ The function accepts the following arguments:
263263
void stdlib_math_base_napi_ddd_d( napi_env env, napi_callback_info info, double (*fcn)( double, double, double ) );
264264
```
265265

266+
#### STDLIB_MATH_BASE_NAPI_MODULE_DID_D( fcn )
267+
268+
Macro for registering a Node-API module exporting an interface for invoking a ternary function accepting a double-precision floating-point number, a signed 32-bit integer, and a double-precision floating-point number and returning a double-precision floating-point number.
269+
270+
```c
271+
#include <stdint.h>
272+
273+
static double fcn( const double x, const int32_t y, const double z ) {
274+
// ...
275+
}
276+
277+
// ...
278+
279+
// Register a Node-API module:
280+
STDLIB_MATH_BASE_NAPI_MODULE_DID_D( fcn );
281+
```
282+
283+
The macro expects the following arguments:
284+
285+
- **fcn**: `double (*fcn)( double, int32_t, double )` ternary function.
286+
287+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
288+
289+
#### stdlib_math_base_napi_did_d( env, info, fcn )
290+
291+
Invokes a ternary function accepting a double-precision floating-point number, a signed 32-bit integer, and a double-precision floating-point number and returning a double-precision floating-point number.
292+
293+
```c
294+
#include <node_api.h>
295+
#include <stdint.h>
296+
297+
// ...
298+
299+
static double fcn( const double x, const int32_t y, const double z ) {
300+
// ...
301+
}
302+
303+
// ...
304+
305+
/**
306+
* Receives JavaScript callback invocation data.
307+
*
308+
* @param env environment under which the function is invoked
309+
* @param info callback data
310+
* @return Node-API value
311+
*/
312+
napi_value addon( napi_env env, napi_callback_info info ) {
313+
return stdlib_math_base_napi_did_d( env, info, fcn );
314+
}
315+
316+
// ...
317+
```
318+
319+
The function accepts the following arguments:
320+
321+
- **env**: `[in] napi_env` environment under which the function is invoked.
322+
- **info**: `[in] napi_callback_info` callback data.
323+
- **fcn**: `[in] double (*fcn)( double, int32_t, double )` ternary function.
324+
325+
```c
326+
void stdlib_math_base_napi_did_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, double ) );
327+
```
328+
266329
#### STDLIB_MATH_BASE_NAPI_MODULE_DII_D( fcn )
267330
268331
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.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// NOTE: keep in alphabetical order...
2323
#include "stdlib/math/base/napi/ternary/ccc_c.h"
2424
#include "stdlib/math/base/napi/ternary/ddd_d.h"
25+
#include "stdlib/math/base/napi/ternary/did_d.h"
2526
#include "stdlib/math/base/napi/ternary/dii_d.h"
2627
#include "stdlib/math/base/napi/ternary/fff_f.h"
2728
#include "stdlib/math/base/napi/ternary/iid_d.h"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef STDLIB_MATH_BASE_NAPI_TERNARY_DID_D_H
20+
#define STDLIB_MATH_BASE_NAPI_TERNARY_DID_D_H
21+
22+
#include <node_api.h>
23+
#include <assert.h>
24+
#include <stdint.h>
25+
26+
/**
27+
* Macro for registering a Node-API module exporting an interface invoking a ternary function accepting a double-precision floating-point number, a signed 32-bit integer, and a double-precision floating-point number and returning a double-precision floating-point number.
28+
*
29+
* @param fcn ternary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static double fcn( const double x, const int_32 y, const double z ) {
35+
* // ...
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_DID_D( fcn );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_DID_D( fcn ) \
44+
static napi_value stdlib_math_base_napi_did_d_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_did_d( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_did_d_init( \
51+
napi_env env, \
52+
napi_value exports \
53+
) { \
54+
napi_value fcn; \
55+
napi_status status = napi_create_function( \
56+
env, \
57+
"exports", \
58+
NAPI_AUTO_LENGTH, \
59+
stdlib_math_base_napi_did_d_wrapper, \
60+
NULL, \
61+
&fcn \
62+
); \
63+
assert( status == napi_ok ); \
64+
return fcn; \
65+
}; \
66+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_did_d_init )
67+
68+
/*
69+
* If C++, prevent name mangling so that the compiler emits a ternary file having undecorated names, thus mirroring the behavior of a C compiler.
70+
*/
71+
#ifdef __cplusplus
72+
extern "C" {
73+
#endif
74+
75+
/**
76+
* Invokes a ternary function accepting a double-precision floating-point number, a signed 32-bit integer, and a double-precision floating-point number and returning a double-precision floating-point number.
77+
*/
78+
napi_value stdlib_math_base_napi_did_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, double ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_TERNARY_DID_D_H

lib/node_modules/@stdlib/math/base/napi/ternary/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"src": [
2828
"./src/ccc_c.c",
2929
"./src/ddd_d.c",
30+
"./src/did_d.c",
3031
"./src/dii_d.c",
3132
"./src/fff_f.c",
3233
"./src/iid_d.c",
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "stdlib/math/base/napi/ternary/did_d.h"
20+
#include <node_api.h>
21+
#include <stdint.h>
22+
#include <assert.h>
23+
24+
/**
25+
* Invokes a ternary function accepting a double-precision floating-point number, a signed 32-bit integer, and a double-precision floating-point number and returning a double-precision floating-point number.
26+
*
27+
* ## Notes
28+
*
29+
* - This function expects that the callback `info` argument provides access to the following JavaScript arguments:
30+
*
31+
* - `x`: input value.
32+
* - `y`: input value.
33+
* - `z`: input value.
34+
*
35+
* @param env environment under which the function is invoked
36+
* @param info callback data
37+
* @param fcn ternary function
38+
* @return function return value as a Node-API double-precision floating-point number
39+
*/
40+
napi_value stdlib_math_base_napi_did_d( napi_env env, napi_callback_info info, double (*fcn)( double, int32_t, double ) ) {
41+
napi_status status;
42+
43+
size_t argc = 3;
44+
napi_value argv[ 3 ];
45+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
46+
assert( status == napi_ok );
47+
48+
if ( argc < 3 ) {
49+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide three numbers." );
50+
assert( status == napi_ok );
51+
return NULL;
52+
}
53+
54+
napi_valuetype vtype0;
55+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
56+
assert( status == napi_ok );
57+
if ( vtype0 != napi_number ) {
58+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
59+
assert( status == napi_ok );
60+
return NULL;
61+
}
62+
63+
napi_valuetype vtype1;
64+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
65+
assert( status == napi_ok );
66+
if ( vtype1 != napi_number ) {
67+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
68+
assert( status == napi_ok );
69+
return NULL;
70+
}
71+
72+
napi_valuetype vtype2;
73+
status = napi_typeof( env, argv[ 2 ], &vtype2 );
74+
assert( status == napi_ok );
75+
if ( vtype2 != napi_number ) {
76+
status = napi_throw_type_error( env, NULL, "invalid argument. Third argument must be a number." );
77+
assert( status == napi_ok );
78+
return NULL;
79+
}
80+
81+
double x;
82+
status = napi_get_value_double( env, argv[ 0 ], &x );
83+
assert( status == napi_ok );
84+
85+
int32_t y;
86+
status = napi_get_value_int32( env, argv[ 1 ], &y );
87+
assert( status == napi_ok );
88+
89+
double z;
90+
status = napi_get_value_double( env, argv[ 2 ], &z );
91+
assert( status == napi_ok );
92+
93+
napi_value v;
94+
status = napi_create_double( env, fcn( x, y, z ), &v );
95+
assert( status == napi_ok );
96+
97+
return v;
98+
}

0 commit comments

Comments
 (0)