Skip to content

Commit cecfdfc

Browse files
committed
Add Node-API utility to assert that a value is a typed array
1 parent b9f1b88 commit cecfdfc

File tree

19 files changed

+1174
-0
lines changed

19 files changed

+1174
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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+
# Assert Typed Array
22+
23+
> Assert that a Node-API value is a typed 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/assert/napi/is-typedarray' );
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/assert/napi/is-typedarray' );
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/assert/napi/is_typedarray.h"
105+
```
106+
107+
#### stdlib_assert_napi_value_is_typedarray( env, value, \*message, \*err )
108+
109+
Asserts that a Node-API value is a typed array.
110+
111+
```c
112+
#include "stdlib/assert/napi/is_typedarray.h"
113+
#include <node_api.h>
114+
115+
static napi_value addon( napi_env env, napi_callback_info info ) {
116+
napi_value value;
117+
118+
// ...
119+
120+
napi_value err;
121+
napi_status status = stdlib_assert_napi_value_is_typedarray( env, value, "Must be a typed array.", &err );
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] napi_value` Node-API value.
136+
- **message**: `[in] char*` error message.
137+
- **err**: `[out] napi_value*` pointer for storing a JavaScript error.
138+
139+
```c
140+
napi_status stdlib_assert_napi_value_is_typedarray( const napi_env env, const napi_value value, const char *message, napi_value *err );
141+
```
142+
143+
The function returns a `napi_status` status code indicating success or failure (returns `napi_ok` if success).
144+
145+
</section>
146+
147+
<!-- /.usage -->
148+
149+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
150+
151+
<section class="notes">
152+
153+
### Notes
154+
155+
- If not provided a typed array, the function sets `err` with a JavaScript error; otherwise, `err` is set to `NULL`.
156+
157+
</section>
158+
159+
<!-- /.notes -->
160+
161+
<!-- C API usage examples. -->
162+
163+
<section class="examples">
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
</section>
170+
171+
<!-- /.c -->
172+
173+
<!-- 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. -->
174+
175+
<section class="references">
176+
177+
</section>
178+
179+
<!-- /.references -->
180+
181+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
182+
183+
<section class="related">
184+
185+
</section>
186+
187+
<!-- /.related -->
188+
189+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
190+
191+
<section class="links">
192+
193+
</section>
194+
195+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# @license Apache-2.0
2+
#
3+
# Copyright (c) 2022 The Stdlib Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# A `.gyp` file for building a Node.js native add-on.
18+
#
19+
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
20+
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
21+
{
22+
# List of files to include in this file:
23+
'includes': [
24+
'./include.gypi',
25+
],
26+
27+
# Define variables to be used throughout the configuration for all targets:
28+
'variables': {
29+
# Target name should match the add-on export name:
30+
'addon_target_name%': 'addon',
31+
32+
# Set variables based on the host OS:
33+
'conditions': [
34+
[
35+
'OS=="win"',
36+
{
37+
# Define the object file suffix:
38+
'obj': 'obj',
39+
},
40+
{
41+
# Define the object file suffix:
42+
'obj': 'o',
43+
}
44+
], # end condition (OS=="win")
45+
], # end conditions
46+
}, # end variables
47+
48+
# Define compile targets:
49+
'targets': [
50+
51+
# Target to generate an add-on:
52+
{
53+
# The target name should match the add-on export name:
54+
'target_name': '<(addon_target_name)',
55+
56+
# Define dependencies:
57+
'dependencies': [],
58+
59+
# Define directories which contain relevant include headers:
60+
'include_dirs': [
61+
# Local include directory:
62+
'<@(include_dirs)',
63+
],
64+
65+
# List of source files:
66+
'sources': [
67+
'<@(src_files)',
68+
],
69+
70+
# Settings which should be applied when a target's object files are used as linker input:
71+
'link_settings': {
72+
# Define libraries:
73+
'libraries': [
74+
'<@(libraries)',
75+
],
76+
77+
# Define library directories:
78+
'library_dirs': [
79+
'<@(library_dirs)',
80+
],
81+
},
82+
83+
# C/C++ compiler flags:
84+
'cflags': [
85+
# Enable commonly used warning options:
86+
'-Wall',
87+
88+
# Aggressive optimization:
89+
'-O3',
90+
],
91+
92+
# C specific compiler flags:
93+
'cflags_c': [
94+
# Specify the C standard to which a program is expected to conform:
95+
'-std=c99',
96+
],
97+
98+
# C++ specific compiler flags:
99+
'cflags_cpp': [
100+
# Specify the C++ standard to which a program is expected to conform:
101+
'-std=c++11',
102+
],
103+
104+
# Linker flags:
105+
'ldflags': [],
106+
107+
# Apply conditions based on the host OS:
108+
'conditions': [
109+
[
110+
'OS=="mac"',
111+
{
112+
# Linker flags:
113+
'ldflags': [
114+
'-undefined dynamic_lookup',
115+
'-Wl,-no-pie',
116+
'-Wl,-search_paths_first',
117+
],
118+
},
119+
], # end condition (OS=="mac")
120+
[
121+
'OS!="win"',
122+
{
123+
# C/C++ flags:
124+
'cflags': [
125+
# Generate platform-independent code:
126+
'-fPIC',
127+
],
128+
},
129+
], # end condition (OS!="win")
130+
], # end conditions
131+
}, # end target <(addon_target_name)
132+
133+
# Target to copy a generated add-on to a standard location:
134+
{
135+
'target_name': 'copy_addon',
136+
137+
# Declare that the output of this target is not linked:
138+
'type': 'none',
139+
140+
# Define dependencies:
141+
'dependencies': [
142+
# Require that the add-on be generated before building this target:
143+
'<(addon_target_name)',
144+
],
145+
146+
# Define a list of actions:
147+
'actions': [
148+
{
149+
'action_name': 'copy_addon',
150+
'message': 'Copying addon...',
151+
152+
# Explicitly list the inputs in the command-line invocation below:
153+
'inputs': [],
154+
155+
# Declare the expected outputs:
156+
'outputs': [
157+
'<(addon_output_dir)/<(addon_target_name).node',
158+
],
159+
160+
# Define the command-line invocation:
161+
'action': [
162+
'cp',
163+
'<(PRODUCT_DIR)/<(addon_target_name).node',
164+
'<(addon_output_dir)/<(addon_target_name).node',
165+
],
166+
},
167+
], # end actions
168+
}, # end target copy_addon
169+
], # end targets
170+
}

0 commit comments

Comments
 (0)