Skip to content

Commit 49904dd

Browse files
authored
feat: add macros for k_k and s_s
PR-URL: #7860 Reviewed-by: Athan Reines <[email protected]>
1 parent e006684 commit 49904dd

File tree

7 files changed

+439
-0
lines changed

7 files changed

+439
-0
lines changed

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,132 @@ The function accepts the following arguments:
736736
void stdlib_math_base_napi_i_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t ) );
737737
```
738738

739+
#### STDLIB_MATH_BASE_NAPI_MODULE_K_K( fcn )
740+
741+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning 16-bit signed integers.
742+
743+
```c
744+
#include <stdint.h>
745+
746+
static int16_t scale( const int16_t x ) {
747+
return x * 10;
748+
}
749+
750+
// ...
751+
752+
// Register a Node-API module:
753+
STDLIB_MATH_BASE_NAPI_MODULE_K_K( scale );
754+
```
755+
756+
The macro expects the following arguments:
757+
758+
- **fcn**: `int16_t (*fcn)( int16_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_k_k( env, info, fcn )
763+
764+
Invokes a unary function accepting and returning signed 16-bit integers.
765+
766+
```c
767+
#include <node_api.h>
768+
#include <stdint.h>
769+
770+
// ...
771+
772+
static int16_t identity( const int16_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_k_k( 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] int16_t (*fcn)( int16_t )` unary function.
797+
798+
```c
799+
void stdlib_math_base_napi_k_k( napi_env env, napi_callback_info info, int16_t (*fcn)( int16_t ) );
800+
```
801+
802+
#### STDLIB_MATH_BASE_NAPI_MODULE_S_S( fcn )
803+
804+
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning 8-bit signed integers.
805+
806+
```c
807+
#include <stdint.h>
808+
809+
static int8_t scale( const int8_t x ) {
810+
return x * 10;
811+
}
812+
813+
// ...
814+
815+
// Register a Node-API module:
816+
STDLIB_MATH_BASE_NAPI_MODULE_S_S( scale );
817+
```
818+
819+
The macro expects the following arguments:
820+
821+
- **fcn**: `int8_t (*fcn)( int8_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_s_s( env, info, fcn )
826+
827+
Invokes a unary function accepting and returning signed 8-bit integers.
828+
829+
```c
830+
#include <node_api.h>
831+
#include <stdint.h>
832+
833+
// ...
834+
835+
static int8_t identity( const int8_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_s_s( 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] int8_t (*fcn)( int8_t )` unary function.
860+
861+
```c
862+
void stdlib_math_base_napi_s_s( napi_env env, napi_callback_info info, int8_t (*fcn)( int8_t ) );
863+
```
864+
739865
#### STDLIB_MATH_BASE_NAPI_MODULE_T_T( fcn )
740866

741867
Macro for registering a Node-API module exporting an interface for invoking a unary function accepting and returning 16-bit unsigned integers.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "stdlib/math/base/napi/unary/i_d.h"
3131
#include "stdlib/math/base/napi/unary/i_f.h"
3232
#include "stdlib/math/base/napi/unary/i_i.h"
33+
#include "stdlib/math/base/napi/unary/k_k.h"
34+
#include "stdlib/math/base/napi/unary/s_s.h"
3335
#include "stdlib/math/base/napi/unary/t_t.h"
3436
#include "stdlib/math/base/napi/unary/u_u.h"
3537
#include "stdlib/math/base/napi/unary/z_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_UNARY_K_K_H
20+
#define STDLIB_MATH_BASE_NAPI_UNARY_K_K_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 signed integers.
28+
*
29+
* @param fcn unary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static int16_t scale( const int16_t x ) {
35+
* return x * 10;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_K_K( scale );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_K_K( fcn ) \
44+
static napi_value stdlib_math_base_napi_k_k_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_k_k( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_k_k_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_k_k_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_k_k_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 signed 16-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_k_k( napi_env env, napi_callback_info info, int16_t (*fcn)( int16_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_UNARY_K_K_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_UNARY_S_S_H
20+
#define STDLIB_MATH_BASE_NAPI_UNARY_S_S_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 signed integers.
28+
*
29+
* @param fcn unary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static int8_t scale( const int8_t x ) {
35+
* return x * 10;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_S_S( scale );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_S_S( fcn ) \
44+
static napi_value stdlib_math_base_napi_s_s_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_s_s( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_s_s_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_s_s_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_s_s_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 signed 8-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_s_s( napi_env env, napi_callback_info info, int8_t (*fcn)( int8_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_UNARY_S_S_H

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
"./src/i_d.c",
3636
"./src/i_f.c",
3737
"./src/i_i.c",
38+
"./src/k_k.c",
39+
"./src/s_s.c",
3840
"./src/t_t.c",
3941
"./src/u_u.c",
4042
"./src/z_d.c",
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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/unary/k_k.h"
20+
#include <node_api.h>
21+
#include <assert.h>
22+
#include <stdint.h>
23+
24+
/**
25+
* Invokes a unary function accepting and returning signed 16-bit integers.
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+
*
33+
* @param env environment under which the function is invoked
34+
* @param info callback data
35+
* @param fcn unary function
36+
* @return function return value as a Node-API signed 32-bit integer
37+
*/
38+
napi_value stdlib_math_base_napi_k_k( napi_env env, napi_callback_info info, int16_t (*fcn)( int16_t ) ) {
39+
napi_status status;
40+
41+
size_t argc = 1;
42+
napi_value argv[ 1 ];
43+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
44+
assert( status == napi_ok );
45+
46+
if ( argc < 1 ) {
47+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide a number." );
48+
assert( status == napi_ok );
49+
return NULL;
50+
}
51+
52+
napi_valuetype vtype0;
53+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
54+
assert( status == napi_ok );
55+
if ( vtype0 != napi_number ) {
56+
status = napi_throw_type_error( env, NULL, "invalid argument. Must provide a number." );
57+
assert( status == napi_ok );
58+
return NULL;
59+
}
60+
61+
int32_t x;
62+
status = napi_get_value_int32( env, argv[ 0 ], &x );
63+
assert( status == napi_ok );
64+
65+
napi_value v;
66+
status = napi_create_int32( env, (int32_t)fcn( (int16_t)x ), &v );
67+
assert( status == napi_ok );
68+
69+
return v;
70+
}

0 commit comments

Comments
 (0)