README.md (6523B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2020 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 # Add-on Arguments 22 23 > C API for validating, extracting, and transforming (to native C types) function arguments provided to a strided array Node-API add-on interface. 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/strided/napi/addon-arguments' ); 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/strided/napi/addon-arguments' ); 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/strided/napi/addon_arguments.h" 105 ``` 106 107 <!-- lint disable maximum-heading-length --> 108 109 #### stdlib_strided_napi_addon_arguments( env, argv, nargs, nin, \*arrays[], \*shape, \*strides, \*types, \*err ) 110 111 Validates, extracts, and transforms (to native C types) function arguments provided to a strided array Node-API add-on interface. 112 113 ```c 114 #include <node_api.h> 115 #include <stdint.h> 116 #include <assert.h> 117 118 // ... 119 120 /** 121 * Receives JavaScript callback invocation data. 122 * 123 * @param env environment under which the function is invoked 124 * @param info callback data 125 * @return Node-API value 126 */ 127 napi_value addon( napi_env env, napi_callback_info info ) { 128 napi_status status; 129 130 // ... 131 132 int64_t nargs = 7; 133 int64_t nin = 2; 134 135 // Get callback arguments: 136 size_t argc = 7; 137 napi_value argv[ 7 ]; 138 status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL ); 139 assert( status == napi_ok ); 140 141 // ... 142 143 // Initialize destination arrays: 144 uint8_t *arrays[ 3 ]; 145 int64_t strides[ 3 ]; 146 int64_t shape[ 1 ]; 147 int32_t types[ 3 ]; 148 149 // Process the provided arguments: 150 napi_value err; 151 status = stdlib_strided_napi_addon_arguments( env, argv, nargs, nin, arrays, shape, strides, types, &err ); 152 assert( status == napi_ok ); 153 154 // ... 155 156 } 157 158 // ... 159 ``` 160 161 The function accepts the following arguments: 162 163 - **env**: `[in] napi_env` environment under which the function is invoked. 164 - **argv**: `[in] napi_value*` strided function arguments. 165 - **nargs**: `[in] int64_t` total number of expected arguments. 166 - **nin**: `[in] int64_t` number of input strided array arguments. 167 - **arrays**: `[out] uint8_t**` destination array for storing pointers to both input and output strided byte arrays. 168 - **shape**: `[out] int64_t*` destination array for storing the array shape (dimensions). 169 - **strides**: `[out] int64_t*` destination array for storing array strides (in bytes) for each strided array. 170 - **types**: `[out] int32_t*` destination array for storing strided array argument data types. 171 - **err**: `[out] napi_value*` pointer for storing a JavaScript error. 172 173 ```c 174 napi_status stdlib_strided_napi_addon_arguments( const napi_env env, const napi_value *argv, const int64_t nargs, const int64_t nin, uint8_t *arrays[], int64_t *shape, int64_t *strides, int32_t *types, napi_value *err ); 175 ``` 176 177 The function returns a `napi_status` status code indicating success or failure (returns `napi_ok` if success). 178 179 </section> 180 181 <!-- /.usage --> 182 183 <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 184 185 <section class="notes"> 186 187 ### Notes 188 189 - The function assumes the following argument order: 190 191 ```text 192 [ N, ia1, is1, ia2, is2, ..., oa1, os1, oa2, os2, ... ] 193 ``` 194 195 where 196 197 - `N` is the number of elements over which to iterate. 198 - `ia#` is an input strided array. 199 - `is#` is a corresponding input strided array stride (in units of elements). 200 - `oa#` is an output strided array. 201 - `os#` is a corresponding output strided array stride (in units of elements). 202 203 - The function may return one of the following JavaScript errors: 204 205 - `TypeError`: first argument must be an integer. 206 - `TypeError`: input array stride argument must be an integer. 207 - `TypeError`: output array stride argument must be an integer. 208 - `TypeError`: input array argument must be a typed array. 209 - `TypeError`: output array argument must be a typed array. 210 - `RangeError`: input array argument must have sufficient elements based on the associated stride and the number of indexed elements. 211 - `RangeError`: output array argument must have sufficient elements based on the associated stride and the number of indexed elements. 212 213 </section> 214 215 <!-- /.notes --> 216 217 <!-- C API usage examples. --> 218 219 <section class="examples"> 220 221 </section> 222 223 <!-- /.examples --> 224 225 </section> 226 227 <!-- /.c --> 228 229 <!-- 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. --> 230 231 <section class="references"> 232 233 </section> 234 235 <!-- /.references --> 236 237 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 238 239 <section class="links"> 240 241 </section> 242 243 <!-- /.links -->