Skip to content

Commit 886684a

Browse files
headlessNodekgryte
andauthored
feat: add napi/argv-booleanarray
PR-URL: #11587 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#436
1 parent f3dfe11 commit 886684a

File tree

19 files changed

+1310
-0
lines changed

19 files changed

+1310
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# argv_booleanarray
22+
23+
> Convert a Node-API value to a boolean array.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var headerDir = require( '@stdlib/napi/argv-booleanarray' );
41+
```
42+
43+
#### headerDir
44+
45+
Absolute file path for the directory containing header files for C APIs.
46+
47+
```javascript
48+
var dir = headerDir;
49+
// returns <string>
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
```javascript
71+
var headerDir = require( '@stdlib/napi/argv-booleanarray' );
72+
73+
console.log( headerDir );
74+
// => <string>
75+
```
76+
77+
</section>
78+
79+
<!-- /.examples -->
80+
81+
<!-- C interface documentation. -->
82+
83+
* * *
84+
85+
<section class="c">
86+
87+
## C APIs
88+
89+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
90+
91+
<section class="intro">
92+
93+
</section>
94+
95+
<!-- /.intro -->
96+
97+
<!-- C usage documentation. -->
98+
99+
<section class="usage">
100+
101+
### Usage
102+
103+
```c
104+
#include "stdlib/napi/argv_booleanarray.h"
105+
```
106+
107+
#### stdlib_napi_argv_booleanarray( env, value, \*\*data, \*length, \*message, \*err )
108+
109+
Converts a Node-API value to a boolean array.
110+
111+
```c
112+
#include "stdlib/napi/argv_booleanarray.h"
113+
#include <node_api.h>
114+
#include <stdint.h>
115+
#include <stdbool.h>
116+
117+
static napi_value addon( napi_env env, napi_callback_info info ) {
118+
napi_value value;
119+
120+
// ...
121+
122+
bool *X;
123+
int64_t len;
124+
napi_value err;
125+
napi_status status = stdlib_napi_argv_booleanarray( env, value, &X, &len, "Must be a typed array.", &err );
126+
assert( status == napi_ok );
127+
if ( err != NULL ) {
128+
assert( napi_throw( env, err ) == napi_ok );
129+
return NULL;
130+
}
131+
132+
// ...
133+
}
134+
```
135+
136+
The function accepts the following arguments:
137+
138+
- **env**: `[in] napi_env` environment under which the function is invoked.
139+
- **value**: `[in] napi_value` Node-API value.
140+
- **data**: `[out] bool**` pointer for returning a reference to the output array.
141+
- **length**: `[out] int64_t*` pointer for returning the number of array elements.
142+
- **message**: `[in] char*` error message.
143+
- **err**: `[out] napi_value*` pointer for storing a JavaScript error. If not provided a `Uint8Array`, the function sets `err` with a JavaScript error; otherwise, `err` is set to `NULL`.
144+
145+
```c
146+
napi_status stdlib_napi_argv_booleanarray( const napi_env env, const napi_value value, bool **data, int64_t *length, const char *message, napi_value *err );
147+
```
148+
149+
The function returns a `napi_status` status code indicating success or failure (returns `napi_ok` if success).
150+
151+
#### STDLIB_NAPI_ARGV_BOOLEANARRAY( env, X, len, argv, index )
152+
153+
Macro for converting an add-on callback argument to a boolean array.
154+
155+
```c
156+
#include "stdlib/napi/argv_booleanarray.h"
157+
#include "stdlib/napi/argv.h"
158+
#include <node_api.h>
159+
#include <stdint.h>
160+
#include <stdbool.h>
161+
162+
static void fcn( const bool *X, const int64_t xlen, bool *Y, const int64_t ylen ) {
163+
int64_t len;
164+
int64_t i;
165+
166+
if ( xlen > ylen ) {
167+
len = ylen;
168+
} else {
169+
len = xlen;
170+
}
171+
for ( i = 0; i < len; i++ ) {
172+
Y[ i ] = X[ i ];
173+
}
174+
}
175+
176+
// ...
177+
178+
static napi_value addon( napi_env env, napi_callback_info info ) {
179+
// Retrieve add-on callback arguments:
180+
STDLIB_NAPI_ARGV( env, info, argv, argc, 2 );
181+
182+
// Convert the first argument to a C type:
183+
STDLIB_NAPI_ARGV_BOOLEANARRAY( env, X, xlen, argv, 0 );
184+
185+
// Convert the second argument to a C type:
186+
STDLIB_NAPI_ARGV_BOOLEANARRAY( env, Y, ylen, argv, 1 );
187+
188+
// ...
189+
190+
fcn( X, xlen, Y, ylen );
191+
}
192+
```
193+
194+
The macro expects the following arguments:
195+
196+
- **env**: environment under which the callback is invoked.
197+
- **X**: output variable name for the array.
198+
- **len**: output variable name for the array length.
199+
- **argv**: name of the variable containing add-on callback arguments.
200+
- **index**: argument index.
201+
202+
</section>
203+
204+
<!-- /.usage -->
205+
206+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
207+
208+
<section class="notes">
209+
210+
</section>
211+
212+
<!-- /.notes -->
213+
214+
<!-- C API usage examples. -->
215+
216+
<section class="examples">
217+
218+
</section>
219+
220+
<!-- /.examples -->
221+
222+
</section>
223+
224+
<!-- /.c -->
225+
226+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
227+
228+
<section class="references">
229+
230+
</section>
231+
232+
<!-- /.references -->
233+
234+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
235+
236+
<section class="related">
237+
238+
</section>
239+
240+
<!-- /.related -->
241+
242+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
243+
244+
<section class="links">
245+
246+
</section>
247+
248+
<!-- /.links -->

0 commit comments

Comments
 (0)