|
| 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 --> |
0 commit comments