Skip to content

Commit b0e6301

Browse files
gunjjoshikgryte
andauthored
feat: add macros for u_u, t_t and b_b
PR-URL: #7800 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent ede3d64 commit b0e6301

File tree

9 files changed

+660
-0
lines changed

9 files changed

+660
-0
lines changed

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

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,69 @@ console.log( headerDir );
106106

107107
<!-- NOTE: keep in alphabetical order according to the suffix X_X -->
108108

109+
#### STDLIB_MATH_BASE_NAPI_MODULE_B_B( fcn )
110+
111+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning 8-bit unsigned integers.
112+
113+
```c
114+
#include <stdint.h>
115+
116+
static uint8_t scale( const uint8_t x ) {
117+
return x * 10;
118+
}
119+
120+
// ...
121+
122+
// Register a Node-API module:
123+
STDLIB_MATH_BASE_NAPI_MODULE_B_B( scale );
124+
```
125+
126+
The macro expects the following arguments:
127+
128+
- **fcn**: `uint8_t (*fcn)( uint8_t )` unary function.
129+
130+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
131+
132+
#### stdlib_math_base_napi_b_b( env, info, fcn )
133+
134+
Invokes a unary function accepting and returning unsigned 8-bit integers.
135+
136+
```c
137+
#include <node_api.h>
138+
#include <stdint.h>
139+
140+
// ...
141+
142+
static uint8_t identity( const uint8_t x ) {
143+
return x;
144+
}
145+
146+
// ...
147+
148+
/**
149+
* Receives JavaScript callback invocation data.
150+
*
151+
* @param env environment under which the function is invoked
152+
* @param info callback data
153+
* @return Node-API value
154+
*/
155+
napi_value addon( napi_env env, napi_callback_info info ) {
156+
return stdlib_math_base_napi_b_b( env, info, identity );
157+
}
158+
159+
// ...
160+
```
161+
162+
The function accepts the following arguments:
163+
164+
- **env**: `[in] napi_env` environment under which the function is invoked.
165+
- **info**: `[in] napi_callback_info` callback data.
166+
- **fcn**: `[in] uint8_t (*fcn)( uint8_t )` unary function.
167+
168+
```c
169+
void stdlib_math_base_napi_b_b( napi_env env, napi_callback_info info, uint8_t (*fcn)( uint8_t ) );
170+
```
171+
109172
#### STDLIB_MATH_BASE_NAPI_MODULE_C_C( fcn )
110173
111174
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning single-precision complex floating-point numbers.
@@ -673,6 +736,132 @@ The function accepts the following arguments:
673736
void stdlib_math_base_napi_i_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t ) );
674737
```
675738

739+
#### STDLIB_MATH_BASE_NAPI_MODULE_T_T( fcn )
740+
741+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning 16-bit unsigned integers.
742+
743+
```c
744+
#include <stdint.h>
745+
746+
static uint16_t scale( const uint16_t x ) {
747+
return x * 10;
748+
}
749+
750+
// ...
751+
752+
// Register a Node-API module:
753+
STDLIB_MATH_BASE_NAPI_MODULE_T_T( scale );
754+
```
755+
756+
The macro expects the following arguments:
757+
758+
- **fcn**: `uint16_t (*fcn)( uint16_t )` unary function.
759+
760+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
761+
762+
#### stdlib_math_base_napi_t_t( env, info, fcn )
763+
764+
Invokes a unary function accepting and returning unsigned 16-bit integers.
765+
766+
```c
767+
#include <node_api.h>
768+
#include <stdint.h>
769+
770+
// ...
771+
772+
static uint16_t identity( const uint16_t x ) {
773+
return x;
774+
}
775+
776+
// ...
777+
778+
/**
779+
* Receives JavaScript callback invocation data.
780+
*
781+
* @param env environment under which the function is invoked
782+
* @param info callback data
783+
* @return Node-API value
784+
*/
785+
napi_value addon( napi_env env, napi_callback_info info ) {
786+
return stdlib_math_base_napi_t_t( env, info, identity );
787+
}
788+
789+
// ...
790+
```
791+
792+
The function accepts the following arguments:
793+
794+
- **env**: `[in] napi_env` environment under which the function is invoked.
795+
- **info**: `[in] napi_callback_info` callback data.
796+
- **fcn**: `[in] uint16_t (*fcn)( uint16_t )` unary function.
797+
798+
```c
799+
void stdlib_math_base_napi_t_t( napi_env env, napi_callback_info info, uint16_t (*fcn)( uint16_t ) );
800+
```
801+
802+
#### STDLIB_MATH_BASE_NAPI_MODULE_U_U( fcn )
803+
804+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning 32-bit unsigned integers.
805+
806+
```c
807+
#include <stdint.h>
808+
809+
static uint32_t scale( const uint32_t x ) {
810+
return x * 10;
811+
}
812+
813+
// ...
814+
815+
// Register a Node-API module:
816+
STDLIB_MATH_BASE_NAPI_MODULE_U_U( scale );
817+
```
818+
819+
The macro expects the following arguments:
820+
821+
- **fcn**: `uint32_t (*fcn)( uint32_t )` unary function.
822+
823+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
824+
825+
#### stdlib_math_base_napi_u_u( env, info, fcn )
826+
827+
Invokes a unary function accepting and returning unsigned 32-bit integers.
828+
829+
```c
830+
#include <node_api.h>
831+
#include <stdint.h>
832+
833+
// ...
834+
835+
static uint32_t identity( const uint32_t x ) {
836+
return x;
837+
}
838+
839+
// ...
840+
841+
/**
842+
* Receives JavaScript callback invocation data.
843+
*
844+
* @param env environment under which the function is invoked
845+
* @param info callback data
846+
* @return Node-API value
847+
*/
848+
napi_value addon( napi_env env, napi_callback_info info ) {
849+
return stdlib_math_base_napi_u_u( env, info, identity );
850+
}
851+
852+
// ...
853+
```
854+
855+
The function accepts the following arguments:
856+
857+
- **env**: `[in] napi_env` environment under which the function is invoked.
858+
- **info**: `[in] napi_callback_info` callback data.
859+
- **fcn**: `[in] uint32_t (*fcn)( uint32_t )` unary function.
860+
861+
```c
862+
void stdlib_math_base_napi_u_u( napi_env env, napi_callback_info info, uint32_t (*fcn)( uint32_t ) );
863+
```
864+
676865
#### STDLIB_MATH_BASE_NAPI_MODULE_Z_D( fcn )
677866

678867
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting a double-precision complex floating-point number and returning a double-precision floating-point number.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define STDLIB_MATH_BASE_NAPI_UNARY_H
2121

2222
// NOTE: keep in alphabetical order...
23+
#include "stdlib/math/base/napi/unary/b_b.h"
2324
#include "stdlib/math/base/napi/unary/c_c.h"
2425
#include "stdlib/math/base/napi/unary/c_f.h"
2526
#include "stdlib/math/base/napi/unary/d_d.h"
@@ -29,6 +30,8 @@
2930
#include "stdlib/math/base/napi/unary/i_d.h"
3031
#include "stdlib/math/base/napi/unary/i_f.h"
3132
#include "stdlib/math/base/napi/unary/i_i.h"
33+
#include "stdlib/math/base/napi/unary/t_t.h"
34+
#include "stdlib/math/base/napi/unary/u_u.h"
3235
#include "stdlib/math/base/napi/unary/z_d.h"
3336
#include "stdlib/math/base/napi/unary/z_z.h"
3437

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_UNARY_B_B_H
20+
#define STDLIB_MATH_BASE_NAPI_UNARY_B_B_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 unary function accepting and returning 8-bit unsigned integers.
28+
*
29+
* @param fcn unary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static uint8_t scale( const uint8_t x ) {
35+
* return x * 10;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_B_B( scale );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_B_B( fcn ) \
44+
static napi_value stdlib_math_base_napi_b_b_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_b_b( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_b_b_init( \
51+
napi_env env, \
52+
napi_value exports \
53+
) { \
54+
napi_value f; \
55+
napi_status status = napi_create_function( \
56+
env, \
57+
"exports", \
58+
NAPI_AUTO_LENGTH, \
59+
stdlib_math_base_napi_b_b_wrapper, \
60+
NULL, \
61+
&f \
62+
); \
63+
assert( status == napi_ok ); \
64+
return f; \
65+
}; \
66+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_b_b_init )
67+
68+
/*
69+
* If C++, prevent name mangling so that the compiler emits a binary 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 unary function accepting and returning unsigned 8-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_b_b( napi_env env, napi_callback_info info, uint8_t (*fcn)( uint8_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_UNARY_B_B_H
85+
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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_UNARY_T_T_H
20+
#define STDLIB_MATH_BASE_NAPI_UNARY_T_T_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 unary function accepting and returning 16-bit unsigned integers.
28+
*
29+
* @param fcn unary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static uint16_t scale( const uint16_t x ) {
35+
* return x * 10;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_T_T( scale );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_T_T( fcn ) \
44+
static napi_value stdlib_math_base_napi_t_t_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_t_t( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_t_t_init( \
51+
napi_env env, \
52+
napi_value exports \
53+
) { \
54+
napi_value f; \
55+
napi_status status = napi_create_function( \
56+
env, \
57+
"exports", \
58+
NAPI_AUTO_LENGTH, \
59+
stdlib_math_base_napi_t_t_wrapper, \
60+
NULL, \
61+
&f \
62+
); \
63+
assert( status == napi_ok ); \
64+
return f; \
65+
}; \
66+
NAPI_MODULE( NODE_GYP_MODULE_NAME, stdlib_math_base_napi_t_t_init )
67+
68+
/*
69+
* If C++, prevent name mangling so that the compiler emits a binary 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 unary function accepting and returning unsigned 16-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_t_t( napi_env env, napi_callback_info info, uint16_t (*fcn)( uint16_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_UNARY_T_T_H
85+

0 commit comments

Comments
 (0)