Skip to content

Commit d1b7fe1

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

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
@@ -1227,6 +1227,69 @@ The function accepts the following arguments:
12271227
void stdlib_math_base_napi_tt_t( napi_env env, napi_callback_info info, uint16_t (*fcn)( uint16_t, uint16_t ) );
12281228
```
12291229

1230+
#### STDLIB_MATH_BASE_NAPI_MODULE_UU_U( fcn )
1231+
1232+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning unsigned 32-bit integers.
1233+
1234+
```c
1235+
#include <stdint.h>
1236+
1237+
static uint32_t add( const uint32_t x, const uint32_t y ) {
1238+
return x + y;
1239+
}
1240+
1241+
// ...
1242+
1243+
// Register a Node-API module:
1244+
STDLIB_MATH_BASE_NAPI_MODULE_UU_U( add );
1245+
```
1246+
1247+
The macro expects the following arguments:
1248+
1249+
- **fcn**: `uint32_t (*fcn)( uint32_t, uint32_t )` binary function.
1250+
1251+
When used, this macro should be used **instead of** `NAPI_MODULE`. The macro includes `NAPI_MODULE`, thus ensuring Node-API module registration.
1252+
1253+
#### stdlib_math_base_napi_uu_u( env, info, fcn )
1254+
1255+
Invokes a binary function accepting and returning unsigned 32-bit integers.
1256+
1257+
```c
1258+
#include <node_api.h>
1259+
#include <stdint.h>
1260+
1261+
// ...
1262+
1263+
static uint32_t add( const uint32_t x, const uint32_t y ) {
1264+
return x + y;
1265+
}
1266+
1267+
// ...
1268+
1269+
/**
1270+
* Receives JavaScript callback invocation data.
1271+
*
1272+
* @param env environment under which the function is invoked
1273+
* @param info callback data
1274+
* @return Node-API value
1275+
*/
1276+
napi_value addon( napi_env env, napi_callback_info info ) {
1277+
return stdlib_math_base_napi_uu_u( env, info, add );
1278+
}
1279+
1280+
// ...
1281+
```
1282+
1283+
The function accepts the following arguments:
1284+
1285+
- **env**: `[in] napi_env` environment under which the function is invoked.
1286+
- **info**: `[in] napi_callback_info` callback data.
1287+
- **fcn**: `[in] uint32_t (*fcn)( uint32_t, uint32_t )` binary function.
1288+
1289+
```c
1290+
void stdlib_math_base_napi_uu_u( napi_env env, napi_callback_info info, uint32_t (*fcn)( uint32_t, uint32_t ) );
1291+
```
1292+
12301293
#### STDLIB_MATH_BASE_NAPI_MODULE_ZD_Z( fcn )
12311294
12321295
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
@@ -36,6 +36,7 @@
3636
#include "stdlib/math/base/napi/binary/ii_i.h"
3737
#include "stdlib/math/base/napi/binary/ll_d.h"
3838
#include "stdlib/math/base/napi/binary/tt_t.h"
39+
#include "stdlib/math/base/napi/binary/uu_u.h"
3940
#include "stdlib/math/base/napi/binary/zd_z.h"
4041
#include "stdlib/math/base/napi/binary/zi_z.h"
4142
#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_UU_U_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_UU_U_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 32-bit integers.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static uint32_t add( const uint32_t x, const uint32_t y ) {
35+
* return x + y;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_UU_U( add );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_UU_U( fcn ) \
44+
static napi_value stdlib_math_base_napi_uu_u_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_uu_u( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_uu_u_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_uu_u_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_uu_u_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 32-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_uu_u( napi_env env, napi_callback_info info, uint32_t (*fcn)( uint32_t, uint32_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_UU_U_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
@@ -40,6 +40,7 @@
4040
"./src/ii_f.c",
4141
"./src/ll_d.c",
4242
"./src/tt_t.c",
43+
"./src/uu_u.c",
4344
"./src/zd_z.c",
4445
"./src/zi_z.c",
4546
"./src/zz_z.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/uu_u.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 32-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 unsigned 32-bit integer
38+
*/
39+
napi_value stdlib_math_base_napi_uu_u( napi_env env, napi_callback_info info, uint32_t (*fcn)( uint32_t, uint32_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+
uint32_t x;
72+
status = napi_get_value_uint32( env, argv[ 0 ], &x );
73+
assert( status == napi_ok );
74+
75+
uint32_t y;
76+
status = napi_get_value_uint32( env, argv[ 1 ], &y );
77+
assert( status == napi_ok );
78+
79+
napi_value v;
80+
status = napi_create_uint32( env, fcn( x, y ), &v );
81+
assert( status == napi_ok );
82+
83+
return v;
84+
}

0 commit comments

Comments
 (0)