Skip to content

Commit 2e5601a

Browse files
committed
feat: add signed 16-bit integer APIs
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: passed - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 3e17026 commit 2e5601a

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,69 @@ The function accepts the following arguments:
11011101
void stdlib_math_base_napi_ii_i( napi_env env, napi_callback_info info, int32_t (*fcn)( int32_t, int32_t ) );
11021102
```
11031103

1104+
#### STDLIB_MATH_BASE_NAPI_MODULE_KK_K( fcn )
1105+
1106+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning signed 16-bit integers.
1107+
1108+
```c
1109+
#include <stdint.h>
1110+
1111+
static int16_t add( const int16_t x, const int16_t y ) {
1112+
return x + y;
1113+
}
1114+
1115+
// ...
1116+
1117+
// Register a Node-API module:
1118+
STDLIB_MATH_BASE_NAPI_MODULE_KK_K( add );
1119+
```
1120+
1121+
The macro expects the following arguments:
1122+
1123+
- **fcn**: `int16_t (*fcn)( int16_t, int16_t )` binary function.
1124+
1125+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
1126+
1127+
#### stdlib_math_base_napi_kk_k( env, info, fcn )
1128+
1129+
Invokes a binary function accepting and returning signed 16-bit integers.
1130+
1131+
```c
1132+
#include <node_api.h>
1133+
#include <stdint.h>
1134+
1135+
// ...
1136+
1137+
static int16_t add( const int16_t x, const int16_t y ) {
1138+
return x + y;
1139+
}
1140+
1141+
// ...
1142+
1143+
/**
1144+
* Receives JavaScript callback invocation data.
1145+
*
1146+
* @param env environment under which the function is invoked
1147+
* @param info callback data
1148+
* @return Node-API value
1149+
*/
1150+
napi_value addon( napi_env env, napi_callback_info info ) {
1151+
return stdlib_math_base_napi_kk_k( env, info, add );
1152+
}
1153+
1154+
// ...
1155+
```
1156+
1157+
The function accepts the following arguments:
1158+
1159+
- **env**: `[in] napi_env` environment under which the function is invoked.
1160+
- **info**: `[in] napi_callback_info` callback data.
1161+
- **fcn**: `[in] int16_t (*fcn)( int16_t, int16_t )` binary function.
1162+
1163+
```c
1164+
void stdlib_math_base_napi_kk_k( napi_env env, napi_callback_info info, int16_t (*fcn)( int16_t, int16_t ) );
1165+
```
1166+
11041167
#### STDLIB_MATH_BASE_NAPI_MODULE_LL_D( fcn )
11051168
11061169
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting signed 64-bit integers and returning a double-precision floating-point number.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "stdlib/math/base/napi/binary/ii_d.h"
3535
#include "stdlib/math/base/napi/binary/ii_f.h"
3636
#include "stdlib/math/base/napi/binary/ii_i.h"
37+
#include "stdlib/math/base/napi/binary/kk_k.h"
3738
#include "stdlib/math/base/napi/binary/ll_d.h"
3839
#include "stdlib/math/base/napi/binary/ss_s.h"
3940
#include "stdlib/math/base/napi/binary/tt_t.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_BINARY_KK_K_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_KK_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 binary function accepting and returning signed 16-bit integers.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static int16_t add( const int16_t x, const int16_t y ) {
35+
* return x + y;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_KK_K( add );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_KK_K( fcn ) \
44+
static napi_value stdlib_math_base_napi_kk_k_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_kk_k( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_kk_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_kk_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_kk_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 binary function accepting and returning signed 16-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_kk_k( napi_env env, napi_callback_info info, int16_t (*fcn)( int16_t, int16_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_KK_K_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"./src/id_d.c",
3939
"./src/ii_d.c",
4040
"./src/ii_f.c",
41+
"./src/kk_k.c",
4142
"./src/ll_d.c",
4243
"./src/ss_s.c",
4344
"./src/tt_t.c",
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+
#include "stdlib/math/base/napi/binary/kk_k.h"
20+
#include <node_api.h>
21+
#include <stdint.h>
22+
#include <assert.h>
23+
24+
/**
25+
* Invokes a binary 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+
* - `y`: input value.
33+
*
34+
* @param env environment under which the function is invoked
35+
* @param info callback data
36+
* @param fcn binary function
37+
* @return function return value as a Node-API signed 32-bit integer
38+
*/
39+
napi_value stdlib_math_base_napi_kk_k( napi_env env, napi_callback_info info, int16_t (*fcn)( int16_t, int16_t ) ) {
40+
napi_status status;
41+
42+
size_t argc = 2;
43+
napi_value argv[ 2 ];
44+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
45+
assert( status == napi_ok );
46+
47+
if ( argc < 2 ) {
48+
status = napi_throw_error( env, NULL, "invalid invocation. Must provide two numbers." );
49+
assert( status == napi_ok );
50+
return NULL;
51+
}
52+
53+
napi_valuetype vtype0;
54+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
55+
assert( status == napi_ok );
56+
if ( vtype0 != napi_number ) {
57+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
58+
assert( status == napi_ok );
59+
return NULL;
60+
}
61+
62+
napi_valuetype vtype1;
63+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
64+
assert( status == napi_ok );
65+
if ( vtype1 != napi_number ) {
66+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
67+
assert( status == napi_ok );
68+
return NULL;
69+
}
70+
71+
int32_t x;
72+
status = napi_get_value_int32( env, argv[ 0 ], &x );
73+
assert( status == napi_ok );
74+
75+
int32_t y;
76+
status = napi_get_value_int32( env, argv[ 1 ], &y );
77+
assert( status == napi_ok );
78+
79+
napi_value v;
80+
status = napi_create_int32( env, (int32_t)fcn( (int16_t)x, (int16_t)y ), &v );
81+
assert( status == napi_ok );
82+
83+
return v;
84+
}

0 commit comments

Comments
 (0)