Skip to content

Commit 5ff1e27

Browse files
committed
feat: add unsigned 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 e83a528 commit 5ff1e27

File tree

6 files changed

+234
-1
lines changed

6 files changed

+234
-1
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
@@ -1164,6 +1164,69 @@ The function accepts the following arguments:
11641164
void stdlib_math_base_napi_ll_d( napi_env env, napi_callback_info info, double (*fcn)( int64_t, int64_t ) );
11651165
```
11661166
1167+
#### STDLIB_MATH_BASE_NAPI_MODULE_TT_T( fcn )
1168+
1169+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning unsigned 16-bit integers.
1170+
1171+
```c
1172+
#include <stdint.h>
1173+
1174+
static uint16_t add( const uint16_t x, const uint16_t y ) {
1175+
return x + y;
1176+
}
1177+
1178+
// ...
1179+
1180+
// Register a Node-API module:
1181+
STDLIB_MATH_BASE_NAPI_MODULE_TT_T( add );
1182+
```
1183+
1184+
The macro expects the following arguments:
1185+
1186+
- **fcn**: `uint16_t (*fcn)( uint16_t, uint16_t )` binary function.
1187+
1188+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
1189+
1190+
#### stdlib_math_base_napi_tt_t( env, info, fcn )
1191+
1192+
Invokes a binary function accepting and returning unsigned 16-bit integers.
1193+
1194+
```c
1195+
#include <node_api.h>
1196+
#include <stdint.h>
1197+
1198+
// ...
1199+
1200+
static uint16_t add( const uint16_t x, const uint16_t y ) {
1201+
return x + y;
1202+
}
1203+
1204+
// ...
1205+
1206+
/**
1207+
* Receives JavaScript callback invocation data.
1208+
*
1209+
* @param env environment under which the function is invoked
1210+
* @param info callback data
1211+
* @return Node-API value
1212+
*/
1213+
napi_value addon( napi_env env, napi_callback_info info ) {
1214+
return stdlib_math_base_napi_tt_t( env, info, add );
1215+
}
1216+
1217+
// ...
1218+
```
1219+
1220+
The function accepts the following arguments:
1221+
1222+
- **env**: `[in] napi_env` environment under which the function is invoked.
1223+
- **info**: `[in] napi_callback_info` callback data.
1224+
- **fcn**: `[in] uint16_t (*fcn)( uint16_t, uint16_t )` binary function.
1225+
1226+
```c
1227+
void stdlib_math_base_napi_tt_t( napi_env env, napi_callback_info info, uint16_t (*fcn)( uint16_t, uint16_t ) );
1228+
```
1229+
11671230
#### STDLIB_MATH_BASE_NAPI_MODULE_ZD_Z( fcn )
11681231

11691232
Macro for registering a Node-API module exporting an interface invoking a binary function accepting a double-precision complex floating-point number and a double-precision floating-point number and returning a double-precision complex 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
@@ -35,6 +35,7 @@
3535
#include "stdlib/math/base/napi/binary/ii_f.h"
3636
#include "stdlib/math/base/napi/binary/ii_i.h"
3737
#include "stdlib/math/base/napi/binary/ll_d.h"
38+
#include "stdlib/math/base/napi/binary/tt_t.h"
3839
#include "stdlib/math/base/napi/binary/zd_z.h"
3940
#include "stdlib/math/base/napi/binary/zi_z.h"
4041
#include "stdlib/math/base/napi/binary/zz_z.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_TT_T_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_TT_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 binary function accepting and returning unsigned 16-bit integers.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static uint16_t add( const uint16_t x, const uint16_t y ) {
35+
* return x + y;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_TT_T( add );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_TT_T( fcn ) \
44+
static napi_value stdlib_math_base_napi_tt_t_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_tt_t( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_tt_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_tt_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_tt_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 binary function accepting and returning unsigned 16-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_tt_t( napi_env env, napi_callback_info info, uint16_t (*fcn)( uint16_t, uint16_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_TT_T_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
@@ -39,6 +39,7 @@
3939
"./src/ii_d.c",
4040
"./src/ii_f.c",
4141
"./src/ll_d.c",
42+
"./src/tt_t.c",
4243
"./src/zd_z.c",
4344
"./src/zi_z.c",
4445
"./src/zz_z.c"

lib/node_modules/@stdlib/math/base/napi/binary/src/bb_b.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2020 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
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/tt_t.h"
20+
#include <node_api.h>
21+
#include <stdint.h>
22+
#include <assert.h>
23+
24+
/**
25+
* Invokes a binary function accepting and returning unsigned 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_tt_t( napi_env env, napi_callback_info info, uint16_t (*fcn)( uint16_t, uint16_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( (uint16_t)x, (uint16_t)y ), &v );
81+
assert( status == napi_ok );
82+
83+
return v;
84+
}

0 commit comments

Comments
 (0)