Skip to content

Commit 1470720

Browse files
feat: adds napi/create-bool
--- 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: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 194c6f9 commit 1470720

File tree

19 files changed

+1224
-0
lines changed

19 files changed

+1224
-0
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# create_bool
22+
23+
> Convert a boolean value to a Node-API value.
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/create-bool' );
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/create-bool' );
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/create_bool.h"
105+
```
106+
107+
#### stdlib_napi_create_bool( env, value, \*out )
108+
109+
Converts a boolean value to a Node-API value.
110+
111+
```c
112+
#include "stdlib/napi/create_bool.h"
113+
#include <node_api.h>
114+
#include <stdbool.h>
115+
116+
static napi_value addon( napi_env env, napi_callback_info info ) {
117+
118+
// ...
119+
120+
napi_value value;
121+
napi_status status = stdlib_napi_create_bool( env, true, &value );
122+
assert( status == napi_ok );
123+
if ( err != NULL ) {
124+
assert( napi_throw( env, err ) == napi_ok );
125+
return NULL;
126+
}
127+
128+
// ...
129+
}
130+
```
131+
132+
The function accepts the following arguments:
133+
134+
- **env**: `[in] napi_env` environment under which the function is invoked.
135+
- **value**: `[in] bool` boolean value.
136+
- **out**: `[out] napi_value*` destination for storing output value.
137+
138+
```c
139+
napi_status stdlib_napi_create_bool( const napi_env env, const bool value, napi_value *out );
140+
```
141+
142+
The function returns a `napi_status` status code indicating success or failure (returns `napi_ok` if success).
143+
144+
#### STDLIB_NAPI_CREATE_BOOL( env, expression, name )
145+
146+
Macro for converting a boolean value to a Node-API value.
147+
148+
```c
149+
#include "stdlib/napi/create_bool.h"
150+
#include "stdlib/napi/argv_bool.h"
151+
#include "stdlib/napi/argv.h"
152+
#include <node_api.h>
153+
#include <stdbool.h>
154+
155+
static bool fcn( const bool v ) {
156+
return v;
157+
}
158+
159+
// ...
160+
161+
static napi_value addon( napi_env env, napi_callback_info info ) {
162+
// Retrieve add-on callback arguments:
163+
STDLIB_NAPI_ARGV( env, info, argv, argc, 1 );
164+
165+
// Convert the first argument to a C type:
166+
STDLIB_NAPI_ARGV_BOOL( env, value, argv, 0 );
167+
168+
// ...
169+
170+
// Convert a value having a C type to a Node-API value:
171+
STDLIB_NAPI_CREATE_BOOL( env, fcn( value ), out );
172+
173+
return out;
174+
}
175+
```
176+
177+
The macro expects the following arguments:
178+
179+
- **env**: environment under which the callback is invoked.
180+
- **expression**: expression returning a boolean value.
181+
- **name**: output variable name.
182+
183+
</section>
184+
185+
<!-- /.usage -->
186+
187+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
188+
189+
<section class="notes">
190+
191+
</section>
192+
193+
<!-- /.notes -->
194+
195+
<!-- C API usage examples. -->
196+
197+
<section class="examples">
198+
199+
</section>
200+
201+
<!-- /.examples -->
202+
203+
</section>
204+
205+
<!-- /.c -->
206+
207+
<!-- 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. -->
208+
209+
<section class="references">
210+
211+
</section>
212+
213+
<!-- /.references -->
214+
215+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
216+
217+
<section class="related">
218+
219+
</section>
220+
221+
<!-- /.related -->
222+
223+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
224+
225+
<section class="links">
226+
227+
</section>
228+
229+
<!-- /.links -->

0 commit comments

Comments
 (0)