Skip to content

Commit e83a528

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

File tree

8 files changed

+236
-3
lines changed

8 files changed

+236
-3
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
@@ -106,6 +106,69 @@ console.log( headerDir );
106106

107107
<!-- NOTE: please keep in alphabetical order according to suffix XX_X -->
108108

109+
#### STDLIB_MATH_BASE_NAPI_MODULE_BB_B( fcn )
110+
111+
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning unsigned 8-bit integers.
112+
113+
```c
114+
#include <stdint.h>
115+
116+
static uint8_t add( const uint8_t x, const uint8_t y ) {
117+
return x + y;
118+
}
119+
120+
// ...
121+
122+
// Register a Node-API module:
123+
STDLIB_MATH_BASE_NAPI_MODULE_BB_B( add );
124+
```
125+
126+
The macro expects the following arguments:
127+
128+
- **fcn**: `uint8_t (*fcn)( uint8_t, uint8_t )` binary 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_bb_b( env, info, fcn )
133+
134+
Invokes a binary 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 add( const uint8_t x, const uint8_t y ) {
143+
return x + y;
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_bb_b( env, info, add );
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, uint8_t )` binary function.
167+
168+
```c
169+
void stdlib_math_base_napi_bb_b( napi_env env, napi_callback_info info, uint8_t (*fcn)( uint8_t, uint8_t ) );
170+
```
171+
109172
#### STDLIB_MATH_BASE_NAPI_MODULE_CC_C( fcn )
110173
111174
Macro for registering a Node-API module exporting an interface for invoking a binary function accepting and returning single-precision complex floating-point numbers.

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
@@ -20,6 +20,7 @@
2020
#define STDLIB_MATH_BASE_NAPI_BINARY_H
2121

2222
// NOTE: please keep in alphabetical order...
23+
#include "stdlib/math/base/napi/binary/bb_b.h"
2324
#include "stdlib/math/base/napi/binary/cc_c.h"
2425
#include "stdlib/math/base/napi/binary/cf_c.h"
2526
#include "stdlib/math/base/napi/binary/ci_c.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_BB_B_H
20+
#define STDLIB_MATH_BASE_NAPI_BINARY_BB_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 binary function accepting and returning unsigned 8-bit integers.
28+
*
29+
* @param fcn binary function
30+
*
31+
* @example
32+
* #include <stdint.h>
33+
*
34+
* static uint8_t add( const uint8_t x, const uint8_t y ) {
35+
* return x + y;
36+
* }
37+
*
38+
* // ...
39+
*
40+
* // Register a Node-API module:
41+
* STDLIB_MATH_BASE_NAPI_MODULE_BB_B( add );
42+
*/
43+
#define STDLIB_MATH_BASE_NAPI_MODULE_BB_B( fcn ) \
44+
static napi_value stdlib_math_base_napi_bb_b_wrapper( \
45+
napi_env env, \
46+
napi_callback_info info \
47+
) { \
48+
return stdlib_math_base_napi_bb_b( env, info, fcn ); \
49+
}; \
50+
static napi_value stdlib_math_base_napi_bb_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_bb_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_bb_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 binary function accepting and returning unsigned 8-bit integers.
77+
*/
78+
napi_value stdlib_math_base_napi_bb_b( napi_env env, napi_callback_info info, uint8_t (*fcn)( uint8_t, uint8_t ) );
79+
80+
#ifdef __cplusplus
81+
}
82+
#endif
83+
84+
#endif // !STDLIB_MATH_BASE_NAPI_BINARY_BB_B_H

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @example
3232
* #include <stdint.h>
3333
*
34-
* static double add( const int_32 x, const int_32 y ) {
34+
* static double add( const int32_t x, const int32_t y ) {
3535
* return x + y;
3636
* }
3737
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @example
3232
* #include <stdint.h>
3333
*
34-
* static float fcn( const int_32 x, const int_32 y ) {
34+
* static float fcn( const int32_t x, const int32_t y ) {
3535
* // ...
3636
* }
3737
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @example
3232
* #include <stdint.h>
3333
*
34-
* static int_32 add( const int_32 x, const int_32 y ) {
34+
* static int32_t add( const int32_t x, const int32_t y ) {
3535
* return x + y;
3636
* }
3737
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"confs": [
2626
{
2727
"src": [
28+
"./src/bb_b.c",
2829
"./src/cc_c.c",
2930
"./src/cf_c.c",
3031
"./src/ci_c.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) 2020 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/bb_b.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 8-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_bb_b( napi_env env, napi_callback_info info, uint8_t (*fcn)( uint8_t, uint8_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( (uint8_t)x, (uint8_t)y ), &v );
81+
assert( status == napi_ok );
82+
83+
return v;
84+
}

0 commit comments

Comments
 (0)