@@ -59,6 +59,7 @@ passed as an argument, and `Napi::Value` is to be returned.
5959Other global helpers:
6060* ` DBG_EXPORT ` - set symbol visibility (mainly for callstack traces). On Windows, that is
6161 equal to exporting a symbol: `__declspec(dllexport)`. On Unix it does nothing.
62+ * ` JS_THROW(text) ` - throws JS exception with the given text message.
6263
6364---
6465
@@ -112,22 +113,31 @@ destroyed by `destroy()`, and then fetch `env`.
112113
113114Following macros convert JS arguments into C++ variables.
114115Three types of argument retrieval are supported:
115- * ` REQ_ ` - 2 params, requires an argument to have a value
116- * ` USE_ ` - 3 params, allows the argument to be empty and have a default
116+ * ` REQ_ ` - 2 params, requires an argument to have a value of specific type.
117+ * ` USE_ ` - 3 params, allows the argument to be empty and have a default.
117118* ` LET_ ` - 2 params, is ` USE_ ` with a preset zero-default.
118119* ` SOFT_ ` - 2 params, is ` LET_ ` without type and arity checks.
120+ * ` WEAK_ ` - 2 params, uses type coercion, doesn't check if arg exists.
119121
120122What it does, basically:
121123
122124``` cpp
123125// REQ_DOUBLE_ARG(0, x)
124- double x = info[0 ].ToNumber().DoubleValue();
126+ if (info.Length() < 1 || !info[0 ].IsNumber()) { JS_THROW; RET_UNDEFINED; }
127+ double x = info[ 0] .As< Napi::Number > ().DoubleValue();
125128
126129// USE_DOUBLE_ARG(0, x, 5.7)
130+ if (info.Length() < 1 || !info[ 0] .IsNumber()) { JS_THROW; RET_UNDEFINED; }
127131double x = IS_ARG_EMPTY(0) ? 5.7 : info[ 0] .ToNumber().DoubleValue();
128132
129133// LET_DOUBLE_ARG(0, x)
130- double x = IS_ARG_EMPTY(0 ) ? 0.0 : info[0 ].ToNumber().DoubleValue();
134+ USE_DOUBLE_ARG (0, x, 0.0);
135+
136+ // SOFT_DOUBLE_ARG(0, x)
137+ double x = info.Length() < 1 ? 0.0 : info[ 0] .ToNumber().DoubleValue();
138+
139+ // WEAK_DOUBLE_ARG(0, x)
140+ double x = info[ 0] .ToNumber().DoubleValue();
131141```
132142
133143That extrapolates well to all the helpers below:
@@ -136,31 +146,40 @@ That extrapolates well to all the helpers below:
136146| :--- | :---: | :---: | :---: |
137147| `REQ_STR_ARG` | `string` | `std::string` | - |
138148| `USE_STR_ARG` | `string` | `std::string` | - |
149+ | `WEAK_STR_ARG` | `string` | `std::string` | - |
139150| `LET_STR_ARG` | `string` | `std::string` | `""` |
140151| `REQ_INT32_ARG` | `number` | `int32_t` | - |
141152| `USE_INT32_ARG` | `number` | `int32_t` | - |
153+ | `WEAK_INT32_ARG` | `number` | `int32_t` | - |
142154| `LET_INT32_ARG` | `number` | `int32_t` | `0` |
143155| `REQ_INT_ARG` | `number` | `int32_t` | - |
144156| `USE_INT_ARG` | `number` | `int32_t` | - |
157+ | `WEAK_INT_ARG` | `number` | `int32_t` | - |
145158| `LET_INT_ARG` | `number` | `int32_t` | `0` |
146159| `REQ_UINT32_ARG` | `number` | `uint32_t` | - |
147160| `USE_UINT32_ARG` | `number` | `uint32_t` | - |
161+ | `WEAK_UINT32_ARG`| `number` | `uint32_t` | - |
148162| `LET_UINT32_ARG` | `number` | `uint32_t` | `0` |
149163| `REQ_UINT_ARG` | `number` | `uint32_t` | - |
150164| `USE_UINT_ARG` | `number` | `uint32_t` | - |
165+ | `WEAK_UINT_ARG` | `number` | `uint32_t` | - |
151166| `LET_UINT_ARG` | `number` | `uint32_t` | `0` |
152167| `REQ_BOOL_ARG` | `Boolean` | `bool` | - |
153168| `USE_BOOL_ARG` | `Boolean` | `bool` | - |
169+ | `WEAK_BOOL_ARG` | `Boolean` | `bool` | - |
154170| `LET_BOOL_ARG` | `Boolean` | `bool` | `false` |
155171| `SOFT_BOOL_ARG` | `Boolean` | `bool` | `false` |
156172| `REQ_OFFS_ARG` | `number` | `size_t` | - |
157173| `USE_OFFS_ARG` | `number` | `size_t` | - |
174+ | `WEAK_OFFS_ARG` | `number` | `size_t` | - |
158175| `LET_OFFS_ARG` | `number` | `size_t` | `0` |
159176| `REQ_DOUBLE_ARG` | `number` | `double` | - |
160177| `USE_DOUBLE_ARG` | `number` | `double` | - |
178+ | `WEAK_DOUBLE_ARG`| `number` | `double` | - |
161179| `LET_DOUBLE_ARG` | `number` | `double` | `0.0` |
162180| `REQ_FLOAT_ARG` | `number` | `float` | - |
163181| `USE_FLOAT_ARG` | `number` | `float` | - |
182+ | `WEAK_FLOAT_ARG` | `number` | `float` | - |
164183| `LET_FLOAT_ARG` | `number` | `float` | `0.f` |
165184| `REQ_EXT_ARG` | `native` | `void*` | - |
166185| `USE_EXT_ARG` | `native` | `void*` | - |
@@ -171,7 +190,7 @@ That extrapolates well to all the helpers below:
171190| `REQ_ARRAY_ARG` | `object` | `Napi::Array` | - |
172191| `USE_ARRAY_ARG` | `object` | `Napi::Array` | - |
173192| `LET_ARRAY_ARG` | `object` | `Napi::Array` | `[]` |
174- | ` LET_ARRAY_STR_ARG ` | ` object ` | ` std::vector<std::string> ` | ` std::vector<std::string>() ` |
193+ | `LET_ARRAY_STR_ARG` | `object` | `std::vector<std::string>` | `std::vector<std::string>()` |
175194| `REQ_FUN_ARG` | `function` | `Napi::Function` | - |
176195| `REQ_ARRV_ARG` | `ArrayBuffer` | `Napi::ArrayBuffer` | - |
177196| `REQ_BUF_ARG` | `Buffer` | `Napi::Buffer<uint8_t>` | - |
@@ -220,18 +239,18 @@ See also: [Class Wrapping](class-wrapping.md)
220239
221240**JS Data to C++ Data**
222241
223- * ` T *getArrayData(value , num = NULL) ` - extracts TypedArray data of any type from
224- the given JS value . Does not accept ` Array ` . Checks with ` IsArrayBuffer() ` .
242+ * `T *getArrayData(env, obj , num = NULL)` - extracts TypedArray data of any type from
243+ the given JS object . Does not accept `Array`. Checks with `IsArrayBuffer()`.
225244Returns `nullptr` for empty JS values. For unacceptable values throws TypeError.
226245
227- * ` T *getBufferData(value , num = NULL) ` - extracts Buffer data from
228- the given JS value . Checks with ` IsBuffer() ` .
246+ * `T *getBufferData(env, obj , num = NULL)` - extracts Buffer data from
247+ the given JS object . Checks with `IsBuffer()`.
229248Returns `nullptr` for empty JS values. For unacceptable values throws TypeError.
230249
231- * ` void *getData(value ) ` - if ` value ` is a ` TypedArray|Buffer ` ,
232- calls ` getArrayData ` or ` getArrayData ` on it. Otherwise, if
233- ` value .data` is a ` TypedArray|Buffer ` ,
234- calls ` getArrayData ` or ` getArrayData ` on it.
250+ * `void *getData(env, obj )` - if `obj ` is a `TypedArray|Buffer`,
251+ calls `getArrayData` or `getBufferData ` on it. Otherwise, if
252+ `obj .data` is a `TypedArray|Buffer`,
253+ calls `getArrayData` or `getBufferData ` on it.
235254Returns `nullptr` in other cases.
236255
237256
0 commit comments