Skip to content

Commit 641fe14

Browse files
committed
Add weak args
1 parent 2f55822 commit 641fe14

21 files changed

+486
-141
lines changed

.github/workflows/cpplint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Install Node.js
2727
uses: actions/setup-node@v4
2828
with:
29-
node-version: 22.9.0
29+
node-version: 22.14.0
3030
cache: 'npm'
3131

3232
- name: Install Modules

.github/workflows/eslint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Install Node.js
2727
uses: actions/setup-node@v4
2828
with:
29-
node-version: 22.9.0
29+
node-version: 22.14.0
3030
cache: 'npm'
3131

3232
- name: Install Modules

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install Node.js
2222
uses: actions/setup-node@v4
2323
with:
24-
node-version: 22.9.0
24+
node-version: 22.14.0
2525
cache: 'npm'
2626

2727
- name: Get Package Version

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Install Node.js
3131
uses: actions/setup-node@v4
3232
with:
33-
node-version: 22.9.0
33+
node-version: 22.14.0
3434
cache: 'npm'
3535

3636
- name: Install Modules

.vscode/c_cpp_properties.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "Win32",
55
"includePath": [
66
"${workspaceFolder}/**",
7-
"${LocalAppData}/node-gyp/Cache/22.9.0/include/node"
7+
"${LocalAppData}/node-gyp/Cache/22.14.0/include/node"
88
],
99
"windowsSdkVersion": "10.0.19041.0",
1010
"cStandard": "c17",

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ Copy the addon file, for example, from `./src/build/Release/glfw.node`
137137
to `./bin-windows/glfw.node`, but each platform uses a different folder.
138138

139139
```json
140-
"build": "cd src && node-gyp rebuild -j max --silent && node -e \"require('addon-tools-raub').cpbin('glfw')\" && cd ..",
141-
"build-only": "cd src && node-gyp build -j max --silent && node -e \"require('addon-tools-raub').cpbin('glfw')\" && cd ..",
140+
"build": "cd src && node-gyp rebuild -j max --silent && node -e \"require('addon-tools-raub').cpbin('glfw')\"",
141+
"build-only": "cd src && node-gyp build -j max --silent && node -e \"require('addon-tools-raub').cpbin('glfw')\"",
142142
```
143143

144144
### Example of `cpcpplint` in **cpplint.yml**:

doc/README.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ passed as an argument, and `Napi::Value` is to be returned.
5959
Other 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

113114
Following macros convert JS arguments into C++ variables.
114115
Three 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

120122
What 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; }
127131
double 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
133143
That 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()`.
225244
Returns `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()`.
229248
Returns `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.
235254
Returns `nullptr` in other cases.
236255
237256

0 commit comments

Comments
 (0)