Skip to content

Commit 70fa3ff

Browse files
committed
Add Node-API utilities for asserting that a Node-API call returns an "ok" status
1 parent acb0ef3 commit 70fa3ff

File tree

11 files changed

+627
-0
lines changed

11 files changed

+627
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# ok status
22+
23+
> C utilities for for asserting that a Node-API call returns an "ok" status.
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+
```c
40+
#include "stdlib/assert/napi/status_ok.h"
41+
```
42+
43+
#### STDLIB_ASSERT_NAPI_STATUS_OK_RET_VOID( env, call, message )
44+
45+
Macro for asserting that a Node-API API call returns an "ok" status.
46+
47+
```c
48+
#include <node_api.h>
49+
50+
static void foo( const napi_env env, const napi_value value, double *out ) {
51+
// ...
52+
53+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VOID( env, napi_get_value_double( env, value, out ), "" )
54+
55+
// ...
56+
57+
return;
58+
}
59+
```
60+
61+
The macro expects the following arguments:
62+
63+
- **env**: environment under which the function is invoked.
64+
- **call**: expression which returns a Node-API status.
65+
- **message**: error message.
66+
67+
If a status is not "ok", an exception is raised and `void` returned.
68+
69+
#### STDLIB_ASSERT_NAPI_STATUS_OK_RET_NULL( env, call, message )
70+
71+
Macro for asserting that a Node-API API call returns an "ok" status.
72+
73+
```c
74+
#include <node_api.h>
75+
76+
static napi_value foo( const napi_env env, const napi_value value, double *out ) {
77+
// ...
78+
79+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_NULL( env, napi_get_value_double( env, value, out ), "" )
80+
81+
// ...
82+
83+
return NULL;
84+
}
85+
```
86+
87+
The macro expects the following arguments:
88+
89+
- **env**: environment under which the function is invoked.
90+
- **call**: expression which returns a Node-API status.
91+
- **message**: error message.
92+
93+
If a status is not "ok", an exception is raised and `NULL` returned.
94+
95+
#### STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, call, message, value )
96+
97+
Macro for asserting that a Node-API API call returns an "ok" status.
98+
99+
```c
100+
#include <node_api.h>
101+
102+
static double foo( const napi_env env, const napi_value value ) {
103+
// ...
104+
105+
double *out;
106+
STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_double( env, value, out ), "", 0.0/0.0 )
107+
108+
// ...
109+
110+
return out;
111+
}
112+
```
113+
114+
The macro expects the following arguments:
115+
116+
- **env**: environment under which the function is invoked.
117+
- **call**: expression which returns a Node-API status.
118+
- **message**: error message.
119+
- **value**: return value.
120+
121+
If a status is not "ok", an exception is raised and the value specified by `value` returned.
122+
123+
</section>
124+
125+
<!-- /.usage -->
126+
127+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
128+
129+
<section class="notes">
130+
131+
</section>
132+
133+
<!-- /.notes -->
134+
135+
<!-- Package usage examples. -->
136+
137+
<section class="examples">
138+
139+
</section>
140+
141+
<!-- /.examples -->
142+
143+
<!-- 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. -->
144+
145+
<section class="references">
146+
147+
</section>
148+
149+
<!-- /.references -->
150+
151+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
152+
153+
<section class="related">
154+
155+
</section>
156+
157+
<!-- /.related -->
158+
159+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
160+
161+
<section class="links">
162+
163+
</section>
164+
165+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Absolute file path for the directory containing header files for C APIs.
23+
*
24+
* @example
25+
* var dir = headerDir;
26+
* // returns <string>
27+
*/
28+
declare const headerDir: string;
29+
30+
31+
// EXPORTS //
32+
33+
export = headerDir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
import headerDir = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The variable is a string...
25+
{
26+
// tslint:disable-next-line:no-unused-expression
27+
headerDir; // $ExpectType string
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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_ASSERT_NAPI_STATUS_OK_H
20+
#define STDLIB_ASSERT_NAPI_STATUS_OK_H
21+
22+
#include <node_api.h>
23+
#include <assert.h>
24+
25+
/**
26+
* Macro for asserting that a Node-API API call returns an "ok" status.
27+
*
28+
* ## Notes
29+
*
30+
* - If a status is not "ok", an exception is raised and `void` returned.
31+
*
32+
* @param env environment under which the function is invoked
33+
* @param call expression which returns a Node-API status
34+
* @param message error message
35+
*
36+
* @example
37+
* #include <node_api.h>
38+
*
39+
* static void foo( const napi_env env, const napi_value value, double *out ) {
40+
* // ...
41+
*
42+
* STDLIB_ASSERT_NAPI_STATUS_OK_RET_VOID( env, napi_get_value_double( env, value, out ), "" )
43+
*
44+
* // ...
45+
*
46+
* return;
47+
* }
48+
*/
49+
#define STDLIB_ASSERT_NAPI_STATUS_OK_RET_VOID( env, call, message ) \
50+
if ( (call) != napi_ok ) { \
51+
assert( napi_throw_error( env, NULL, message ) == napi_ok ); \
52+
return; \
53+
}
54+
55+
/**
56+
* Macro for asserting that a Node-API API call returns an "ok" status.
57+
*
58+
* ## Notes
59+
*
60+
* - If a status is not "ok", an exception is raised and `NULL` returned.
61+
*
62+
* @param env environment under which the function is invoked
63+
* @param call expression which returns a Node-API status
64+
* @param message error message
65+
*
66+
* @example
67+
* #include <node_api.h>
68+
*
69+
* static napi_value foo( const napi_env env, const napi_value value, double *out ) {
70+
* // ...
71+
*
72+
* STDLIB_ASSERT_NAPI_STATUS_OK_RET_NULL( env, napi_get_value_double( env, value, out ), "" )
73+
*
74+
* // ...
75+
*
76+
* return NULL;
77+
* }
78+
*/
79+
#define STDLIB_ASSERT_NAPI_STATUS_OK_RET_NULL( env, call, message ) \
80+
if ( (call) != napi_ok ) { \
81+
assert( napi_throw_error( env, NULL, message ) == napi_ok ); \
82+
return NULL; \
83+
}
84+
85+
/**
86+
* Macro for asserting that a Node-API API call returns an "ok" status.
87+
*
88+
* ## Notes
89+
*
90+
* - If a status is not "ok", an exception is raised and the value specified by `value` returned.
91+
*
92+
* @param env environment under which the function is invoked
93+
* @param call expression which returns a Node-API status
94+
* @param message error message
95+
* @param value return value
96+
*
97+
* @example
98+
* #include <node_api.h>
99+
*
100+
* static double foo( const napi_env env, const napi_value value ) {
101+
* // ...
102+
*
103+
* double *out;
104+
* STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, napi_get_value_double( env, value, out ), "", 0.0/0.0 )
105+
*
106+
* // ...
107+
*
108+
* return out;
109+
* }
110+
*/
111+
#define STDLIB_ASSERT_NAPI_STATUS_OK_RET_VALUE( env, call, message, value ) \
112+
if ( (call) != napi_ok ) { \
113+
assert( napi_throw_error( env, NULL, message ) == napi_ok ); \
114+
return value; \
115+
}
116+
117+
#endif // !STDLIB_ASSERT_NAPI_STATUS_OK_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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+
'use strict';
20+
21+
// MAIN //
22+
23+
var dir = null;
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = dir;

0 commit comments

Comments
 (0)