@kmamal/sdl 1.0.0-5 → 1.0.0-8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/binding.gyp +3 -0
  3. package/package.json +5 -12
  4. package/scripts/util/common.js +2 -2
  5. package/src/javascript/audio/audio-playback-stream.js +9 -10
  6. package/src/javascript/audio/audio-recording-stream.js +9 -8
  7. package/src/javascript/audio/audio-stream.js +100 -44
  8. package/src/javascript/audio/conversion.js +41 -0
  9. package/src/javascript/audio/devices.js +11 -0
  10. package/src/javascript/audio/events.js +65 -0
  11. package/src/javascript/audio/format-helpers.js +8 -4
  12. package/src/javascript/audio/index.js +25 -20
  13. package/src/javascript/audio/prevent-exit.js +12 -9
  14. package/src/javascript/audio/validate.js +9 -0
  15. package/src/javascript/cleanup.js +9 -6
  16. package/src/javascript/clipboard/index.js +8 -3
  17. package/src/javascript/device-instance.js +61 -0
  18. package/src/javascript/events/events-via-poll.js +33 -27
  19. package/src/javascript/events/index.js +75 -482
  20. package/src/javascript/events/try.js +22 -0
  21. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  22. package/src/javascript/gamepad/events.js +66 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  24. package/src/javascript/gamepad/index.js +15 -16
  25. package/src/javascript/globals.js +10 -1
  26. package/src/javascript/index.js +2 -5
  27. package/src/javascript/joystick/devices.js +43 -0
  28. package/src/javascript/joystick/events.js +129 -0
  29. package/src/javascript/joystick/index.js +9 -18
  30. package/src/javascript/joystick/joystick-device-instance.js +175 -0
  31. package/src/javascript/joystick/joystick-instance.js +12 -158
  32. package/src/javascript/keyboard/index.js +20 -10
  33. package/src/javascript/keyboard/key-mapping.js +46 -27
  34. package/src/javascript/mouse/index.js +36 -29
  35. package/src/javascript/sensor/events.js +21 -0
  36. package/src/javascript/sensor/index.js +13 -2
  37. package/src/javascript/sensor/sensor-instance.js +7 -47
  38. package/src/javascript/subsystems.js +56 -0
  39. package/src/javascript/touch/index.js +9 -4
  40. package/src/javascript/validate.js +114 -0
  41. package/src/javascript/video/conversion.js +63 -0
  42. package/src/javascript/video/events.js +179 -0
  43. package/src/javascript/video/format-helpers.js +10 -8
  44. package/src/javascript/video/index.js +28 -14
  45. package/src/javascript/video/validate.js +45 -0
  46. package/src/javascript/video/window.js +144 -161
  47. package/src/native/audio.cpp +457 -151
  48. package/src/native/audio.h +12 -6
  49. package/src/native/clipboard.cpp +8 -37
  50. package/src/native/enums.cpp +2 -0
  51. package/src/native/events.cpp +416 -354
  52. package/src/native/events.h +67 -65
  53. package/src/native/gamepad.cpp +86 -103
  54. package/src/native/gamepad.h +7 -8
  55. package/src/native/global.cpp +190 -85
  56. package/src/native/global.h +48 -2
  57. package/src/native/joystick.cpp +180 -274
  58. package/src/native/joystick.h +12 -6
  59. package/src/native/keyboard.cpp +60 -22
  60. package/src/native/keyboard.h +2 -1
  61. package/src/native/module.cpp +20 -3
  62. package/src/native/mouse.cpp +27 -58
  63. package/src/native/packed.cpp +106 -0
  64. package/src/native/packed.h +91 -0
  65. package/src/native/power.cpp +7 -1
  66. package/src/native/power.h +2 -2
  67. package/src/native/sensor.cpp +61 -69
  68. package/src/native/sensor.h +3 -3
  69. package/src/native/testing.cpp +131 -0
  70. package/src/native/testing.h +16 -0
  71. package/src/native/threads.cpp +300 -0
  72. package/src/native/threads.h +36 -0
  73. package/src/native/touch.cpp +32 -43
  74. package/src/native/touch.h +2 -1
  75. package/src/native/video.cpp +145 -95
  76. package/src/native/video.h +6 -3
  77. package/src/native/window.cpp +155 -218
  78. package/src/native/window.h +21 -20
  79. package/src/types/index.d.ts +141 -69
  80. package/src/javascript/audio/device.js +0 -37
  81. package/src/javascript/joystick/device.js +0 -13
  82. package/src/javascript/joystick/shared.js +0 -90
  83. package/src/javascript/worker.js +0 -88
  84. package/src/types/worker.d.ts +0 -71
@@ -1,8 +1,23 @@
1
1
  const Globals = require('../globals')
2
2
  const Bindings = require('../bindings')
3
3
  const Enums = require('../enums')
4
- const { VideoFormatHelpers } = require('./format-helpers')
5
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
+ const {
6
+ validateBoolean,
7
+ validateString,
8
+ validateObject,
9
+ validateInt32,
10
+ validateFloat,
11
+ validatePositiveFloat,
12
+ validatePositiveInt32,
13
+ validateNotDestroyed,
14
+ } = require('../validate')
15
+ const {
16
+ validateVsync,
17
+ validateImage,
18
+ validateRenderableFormat,
19
+ validateRgbFormat,
20
+ } = require('./validate')
6
21
 
7
22
  const validEvents = [
8
23
  'show',
@@ -11,6 +26,8 @@ const validEvents = [
11
26
  'minimize',
12
27
  'maximize',
13
28
  'restore',
29
+ 'enterFullscreen',
30
+ 'leaveFullscreen',
14
31
  'move',
15
32
  'resize',
16
33
  'displayChange',
@@ -41,13 +58,13 @@ class Window extends EventsViaPoll {
41
58
  constructor (options = {}) {
42
59
  super(validEvents)
43
60
 
44
- if (typeof options !== 'object' || options === null) { throw Object.assign(new Error("options must be an object"), { options }) }
61
+ validateObject('options', options)
45
62
 
46
63
  const {
47
64
  title = "",
48
- display = null,
49
- x = null, y = null,
50
- width = null, height = null,
65
+ display,
66
+ x, y,
67
+ width, height,
51
68
  visible = true,
52
69
  fullscreen = false,
53
70
  resizable = false,
@@ -62,52 +79,42 @@ class Window extends EventsViaPoll {
62
79
  const hasAccelerated = accelerated !== undefined
63
80
  const hasVsync = vsync !== undefined
64
81
 
65
- if (typeof title !== 'string') { throw Object.assign(new Error("title must be a string"), { title }) }
66
- if (display !== null && typeof display !== 'object') { throw Object.assign(new Error("display must be an object"), { display }) }
67
- if (x !== null && !Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
68
- if (x !== null && (x < -(2 ** 31) || x > 2 ** 31 - 1)) { throw Object.assign(new Error("x must be a 32-bit integer"), { x }) }
69
- if (y !== null && !Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
70
- if (y !== null && (y < -(2 ** 31) || y > 2 ** 31 - 1)) { throw Object.assign(new Error("y must be a 32-bit integer"), { y }) }
71
- if (width !== null) {
72
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
73
- if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
74
- }
75
- if (height !== null) {
76
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
77
- if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
78
- }
79
- if (typeof visible !== 'boolean') { throw Object.assign(new Error("visible must be a boolean"), { visible }) }
80
- if (typeof fullscreen !== 'boolean') { throw Object.assign(new Error("fullscreen must be a boolean"), { fullscreen }) }
81
- if (typeof resizable !== 'boolean') { throw Object.assign(new Error("resizable must be a boolean"), { resizable }) }
82
- if (typeof borderless !== 'boolean') { throw Object.assign(new Error("borderless must be a boolean"), { borderless }) }
83
- if (typeof alwaysOnTop !== 'boolean') { throw Object.assign(new Error("alwaysOnTop must be a boolean"), { alwaysOnTop }) }
84
- if (hasAccelerated && typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
85
- if (hasVsync) {
86
- if (!Number.isInteger(vsync)) { throw Object.assign(new Error("vsync must be an integer"), { vsync }) }
87
- if (vsync < -1 || vsync > 2 ** 31 - 1) { throw Object.assign(new Error("vsync must be -1 or a non-negative 32-bit integer"), { vsync }) }
88
- }
89
- if (typeof opengl !== 'boolean') { throw Object.assign(new Error("opengl must be a boolean"), { opengl }) }
90
- if (typeof webgpu !== 'boolean') { throw Object.assign(new Error("webgpu must be a boolean"), { webgpu }) }
91
- if (display !== null && (x !== null || y !== null)) { throw Object.assign(new Error("display and x/y are mutually exclusive"), { display, x, y }) }
82
+ validateString('title', title)
83
+ if (display !== undefined) { validateObject('display', display) }
84
+ if (x !== undefined) { validateInt32('x', x) }
85
+ if (y !== undefined) { validateInt32('y', y) }
86
+ if (width !== undefined) { validatePositiveInt32('width', width) }
87
+ if (height !== undefined) { validatePositiveInt32('height', height) }
88
+ validateBoolean('visible', visible)
89
+ validateBoolean('fullscreen', fullscreen)
90
+ validateBoolean('resizable', resizable)
91
+ validateBoolean('borderless', borderless)
92
+ validateBoolean('alwaysOnTop', alwaysOnTop)
93
+ if (hasAccelerated) { validateBoolean('accelerated', accelerated) }
94
+ if (hasVsync) { validateVsync(vsync) }
95
+ validateBoolean('opengl', opengl)
96
+ validateBoolean('webgpu', webgpu)
97
+ if (display !== undefined && (x !== undefined || y !== undefined)) { throw Object.assign(new Error("display and x/y are mutually exclusive"), { display, x, y }) }
92
98
  if (resizable && borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless }) }
93
99
  if (opengl && webgpu) { throw Object.assign(new Error("opengl and webgpu are mutually exclusive"), { opengl, webgpu }) }
94
100
  if (opengl && (hasAccelerated || hasVsync)) { throw Object.assign(new Error("opengl and accelerated/vsync are mutually exclusive"), { opengl, accelerated, vsync }) }
95
101
  if (webgpu && (hasAccelerated || hasVsync)) { throw Object.assign(new Error("webgpu and accelerated/vsync are mutually exclusive"), { webgpu, accelerated, vsync }) }
96
102
 
97
103
  let displayId = 0
98
- if (display) {
104
+ if (display !== undefined) {
99
105
  Globals.events.poll()
100
- if (!Globals.displays.includes(display)) { throw Object.assign(new Error("display must be from sdl.video.displays"), { display }) }
101
- displayId = display.id
106
+ const found = Globals.displays.find(({ id }) => id === display.id)
107
+ if (!found) { throw Object.assign(new Error("display must be from sdl.video.displays"), { display }) }
108
+ displayId = found.id
102
109
  }
103
110
 
104
111
  const result = Bindings.window_create(
105
112
  title,
106
113
  displayId,
107
- x,
108
- y,
109
- width,
110
- height,
114
+ x ?? null,
115
+ y ?? null,
116
+ width ?? null,
117
+ height ?? null,
111
118
  visible,
112
119
  fullscreen,
113
120
  resizable,
@@ -120,6 +127,8 @@ class Window extends EventsViaPoll {
120
127
  )
121
128
 
122
129
  this._id = result.id
130
+ this._title = result.title
131
+ this._visible = result.visible
123
132
  this._x = result.x
124
133
  this._y = result.y
125
134
  this._width = result.width
@@ -135,13 +144,11 @@ class Window extends EventsViaPoll {
135
144
  this._vsync = result.vsync
136
145
  this._native = result.native
137
146
 
138
- this._title = title
139
- this._visible = visible
140
147
  this._opengl = opengl
141
148
  this._webgpu = webgpu
142
149
 
143
- this._minimized = false
144
- this._maximized = false
150
+ this._minimized = result.minimized
151
+ this._maximized = result.maximized
145
152
  this._relativeMouseMode = false
146
153
  this._destroyed = false
147
154
 
@@ -150,11 +157,14 @@ class Window extends EventsViaPoll {
150
157
  // Keeps Node.js alive while windows are open
151
158
  const keepAlive = () => {}
152
159
  this.on('close', keepAlive)
153
- this.on('removeListener', (type, listener) => {
160
+ this._addInternalListener('removeListener', (type, listener) => {
154
161
  if (listener === keepAlive && !this._destroyed) { this.on('close', keepAlive) }
155
162
  })
156
163
 
157
- // Manually emit some initial events for convenience
164
+ // Manually emit some initial events for convenience.
165
+ // Note that these can duplicate real events.
166
+ // Something may have polled synchronously before this tick runs, e.g. a getter read right after createWindow().
167
+ // In that case the real move/resize/expose events have already fired and the ones below repeat them with the same values.
158
168
  process.nextTick(() => {
159
169
  if (this._destroyed) { return }
160
170
  Globals.events.withoutPolling(() => {
@@ -190,181 +200,177 @@ class Window extends EventsViaPoll {
190
200
 
191
201
  get id () { return this._id }
192
202
 
193
- get title () { return this._title }
203
+ get title () {
204
+ validateNotDestroyed(this)
205
+
206
+ return this._title
207
+ }
208
+
194
209
  setTitle (title) {
195
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
210
+ validateNotDestroyed(this)
196
211
 
197
- if (typeof title !== 'string') { throw Object.assign(new Error("title must be a string"), { title }) }
212
+ validateString('title', title)
198
213
 
199
- Bindings.window_setTitle(this._id, title)
200
- this._title = title
214
+ this._title = Bindings.window_setTitle(this._id, title)
201
215
  }
202
216
 
203
217
  get x () {
204
218
  Globals.events.poll()
205
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
219
+ validateNotDestroyed(this)
206
220
 
207
221
  return this._x
208
222
  }
209
223
 
210
224
  get y () {
211
225
  Globals.events.poll()
212
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
226
+ validateNotDestroyed(this)
213
227
 
214
228
  return this._y
215
229
  }
216
230
 
217
231
  setPosition (x, y) {
218
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
232
+ validateNotDestroyed(this)
219
233
 
220
- if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
221
- if (x < -(2 ** 31) || x > 2 ** 31 - 1) { throw Object.assign(new Error("x must be a 32-bit integer"), { x }) }
222
- if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
223
- if (y < -(2 ** 31) || y > 2 ** 31 - 1) { throw Object.assign(new Error("y must be a 32-bit integer"), { y }) }
234
+ validateInt32('x', x)
235
+ validateInt32('y', y)
224
236
 
225
237
  Bindings.window_setPosition(this._id, x, y)
226
238
  }
227
239
 
228
240
  get width () {
229
241
  Globals.events.poll()
230
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
242
+ validateNotDestroyed(this)
231
243
 
232
244
  return this._width
233
245
  }
234
246
 
235
247
  get height () {
236
248
  Globals.events.poll()
237
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
249
+ validateNotDestroyed(this)
238
250
 
239
251
  return this._height
240
252
  }
241
253
 
242
254
  setSize (width, height) {
243
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
255
+ validateNotDestroyed(this)
244
256
 
245
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
246
- if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
247
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
248
- if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
257
+ validatePositiveInt32('width', width)
258
+ validatePositiveInt32('height', height)
249
259
 
250
260
  Bindings.window_setSize(this._id, width, height, false)
251
261
  }
252
262
 
253
263
  setSizeInPixels (pixelWidth, pixelHeight) {
254
- Globals.events.poll()
255
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
264
+ validateNotDestroyed(this)
256
265
 
257
- if (!Number.isInteger(pixelWidth)) { throw Object.assign(new Error("pixelWidth must be an integer"), { pixelWidth }) }
258
- if (pixelWidth <= 0 || pixelWidth > 2 ** 31 - 1) { throw Object.assign(new Error("pixelWidth must be a positive 32-bit integer"), { pixelWidth }) }
259
- if (!Number.isInteger(pixelHeight)) { throw Object.assign(new Error("pixelHeight must be an integer"), { pixelHeight }) }
260
- if (pixelHeight <= 0 || pixelHeight > 2 ** 31 - 1) { throw Object.assign(new Error("pixelHeight must be a positive 32-bit integer"), { pixelHeight }) }
266
+ validatePositiveInt32('pixelWidth', pixelWidth)
267
+ validatePositiveInt32('pixelHeight', pixelHeight)
261
268
 
262
269
  Bindings.window_setSize(this._id, pixelWidth, pixelHeight, true)
263
270
  }
264
271
 
265
272
  get pixelWidth () {
266
273
  Globals.events.poll()
267
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
274
+ validateNotDestroyed(this)
268
275
 
269
276
  return this._pixelWidth
270
277
  }
271
278
 
272
279
  get pixelHeight () {
273
280
  Globals.events.poll()
274
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
281
+ validateNotDestroyed(this)
275
282
 
276
283
  return this._pixelHeight
277
284
  }
278
285
 
279
286
  get display () {
280
287
  Globals.events.poll()
281
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
288
+ validateNotDestroyed(this)
282
289
 
283
290
  return Globals.displays.find((a) => a.id === this._displayId) ?? null
284
291
  }
285
292
 
286
293
  get visible () {
287
294
  Globals.events.poll()
288
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
295
+ validateNotDestroyed(this)
289
296
 
290
297
  return this._visible
291
298
  }
292
299
 
293
300
  show (show = true) {
294
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
301
+ validateNotDestroyed(this)
295
302
 
296
- if (typeof show !== 'boolean') { throw Object.assign(new Error("show must be a boolean"), { show }) }
303
+ validateBoolean('show', show)
297
304
 
298
305
  show ? Bindings.window_show(this._id) : Bindings.window_hide(this._id)
299
- this._visible = show
300
306
  }
301
307
 
302
308
  hide () { this.show(false) }
303
309
 
304
310
  get fullscreen () {
305
311
  Globals.events.poll()
306
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
312
+ validateNotDestroyed(this)
307
313
 
308
314
  return this._fullscreen
309
315
  }
310
316
 
311
317
  setFullscreen (fullscreen) {
312
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
318
+ validateNotDestroyed(this)
313
319
 
314
- if (typeof fullscreen !== 'boolean') { throw Object.assign(new Error("fullscreen must be a boolean"), { fullscreen }) }
320
+ validateBoolean('fullscreen', fullscreen)
315
321
 
316
322
  Bindings.window_setFullscreen(this._id, fullscreen)
317
323
  }
318
324
 
319
325
  get resizable () {
320
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
326
+ validateNotDestroyed(this)
321
327
 
322
328
  return this._resizable
323
329
  }
324
330
 
325
331
  setResizable (resizable) {
326
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
332
+ validateNotDestroyed(this)
327
333
 
328
- if (typeof resizable !== 'boolean') { throw Object.assign(new Error("resizable must be a boolean"), { resizable }) }
334
+ validateBoolean('resizable', resizable)
329
335
  if (resizable && this._borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless: this._borderless }) }
330
336
 
331
337
  this._resizable = Bindings.window_setResizable(this._id, resizable)
332
338
  }
333
339
 
334
340
  get borderless () {
335
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
341
+ validateNotDestroyed(this)
336
342
 
337
343
  return this._borderless
338
344
  }
339
345
 
340
346
  setBorderless (borderless) {
341
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
347
+ validateNotDestroyed(this)
342
348
 
343
- if (typeof borderless !== 'boolean') { throw Object.assign(new Error("borderless must be a boolean"), { borderless }) }
349
+ validateBoolean('borderless', borderless)
344
350
  if (borderless && this._resizable) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable: this._resizable, borderless }) }
345
351
 
346
352
  this._borderless = Bindings.window_setBorderless(this._id, borderless)
347
353
  }
348
354
 
349
355
  get alwaysOnTop () {
350
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
356
+ validateNotDestroyed(this)
351
357
 
352
358
  return this._alwaysOnTop
353
359
  }
354
360
 
355
361
  get accelerated () {
356
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
362
+ validateNotDestroyed(this)
357
363
 
358
364
  return this._accelerated
359
365
  }
360
366
 
361
367
  setAccelerated (accelerated) {
362
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
368
+ validateNotDestroyed(this)
363
369
 
364
370
  if (this._opengl) { throw new Error("can't call setAccelerated in opengl mode") }
365
371
  if (this._webgpu) { throw new Error("can't call setAccelerated in webgpu mode") }
366
372
 
367
- if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
373
+ validateBoolean('accelerated', accelerated)
368
374
 
369
375
  const result = Bindings.window_setAcceleratedAndVsync(this._id, accelerated, this._vsync)
370
376
  this._accelerated = result.accelerated
@@ -372,19 +378,18 @@ class Window extends EventsViaPoll {
372
378
  }
373
379
 
374
380
  get vsync () {
375
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
381
+ validateNotDestroyed(this)
376
382
 
377
383
  return this._vsync
378
384
  }
379
385
 
380
386
  setVsync (vsync) {
381
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
387
+ validateNotDestroyed(this)
382
388
 
383
389
  if (this._opengl) { throw new Error("can't call setVsync in opengl mode") }
384
390
  if (this._webgpu) { throw new Error("can't call setVsync in webgpu mode") }
385
391
 
386
- if (!Number.isInteger(vsync)) { throw Object.assign(new Error("vsync must be an integer"), { vsync }) }
387
- if (vsync < -1 || vsync > 2 ** 31 - 1) { throw Object.assign(new Error("vsync must be -1 or a non-negative 32-bit integer"), { vsync }) }
392
+ validateVsync(vsync)
388
393
 
389
394
  const result = Bindings.window_setAcceleratedAndVsync(this._id, this._accelerated, vsync)
390
395
  this._accelerated = result.accelerated
@@ -392,45 +397,45 @@ class Window extends EventsViaPoll {
392
397
  }
393
398
 
394
399
  get opengl () {
395
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
400
+ validateNotDestroyed(this)
396
401
 
397
402
  return this._opengl
398
403
  }
399
404
 
400
405
  get webgpu () {
401
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
406
+ validateNotDestroyed(this)
402
407
 
403
408
  return this._webgpu
404
409
  }
405
410
 
406
411
  get native () {
407
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
412
+ validateNotDestroyed(this)
408
413
 
409
414
  return this._native
410
415
  }
411
416
 
412
417
  get minimized () {
413
418
  Globals.events.poll()
414
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
419
+ validateNotDestroyed(this)
415
420
 
416
421
  return this._minimized
417
422
  }
418
423
 
419
424
  minimize () {
420
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
425
+ validateNotDestroyed(this)
421
426
 
422
427
  Bindings.window_minimize(this._id)
423
428
  }
424
429
 
425
430
  get maximized () {
426
431
  Globals.events.poll()
427
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
432
+ validateNotDestroyed(this)
428
433
 
429
434
  return this._maximized
430
435
  }
431
436
 
432
437
  maximize () {
433
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
438
+ validateNotDestroyed(this)
434
439
 
435
440
  if (!this._resizable) { throw new Error("can't maximize a non-resizable window") }
436
441
 
@@ -438,48 +443,48 @@ class Window extends EventsViaPoll {
438
443
  }
439
444
 
440
445
  restore () {
441
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
446
+ validateNotDestroyed(this)
442
447
 
443
448
  Bindings.window_restore(this._id)
444
449
  }
445
450
 
446
451
  get focused () {
447
452
  Globals.events.poll()
448
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
453
+ validateNotDestroyed(this)
449
454
 
450
455
  return Globals.windows.focused === this
451
456
  }
452
457
 
453
458
  focus () {
454
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
459
+ validateNotDestroyed(this)
455
460
 
456
461
  Bindings.window_focus(this._id)
457
462
  }
458
463
 
459
464
  get hovered () {
460
465
  Globals.events.poll()
461
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
466
+ validateNotDestroyed(this)
462
467
 
463
468
  return Globals.windows.hovered === this
464
469
  }
465
470
 
466
471
  get mouseCaptured () {
467
472
  Globals.events.poll()
468
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
473
+ validateNotDestroyed(this)
469
474
 
470
475
  return Bindings.window_getMouseCaptured(this._id)
471
476
  }
472
477
 
473
478
  get relativeMouseMode () {
474
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
479
+ validateNotDestroyed(this)
475
480
 
476
481
  return this._relativeMouseMode
477
482
  }
478
483
 
479
484
  setRelativeMouseMode (relative = true) {
480
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
485
+ validateNotDestroyed(this)
481
486
 
482
- if (typeof relative !== 'boolean') { throw Object.assign(new Error("relative must be a boolean"), { relative }) }
487
+ validateBoolean('relative', relative)
483
488
 
484
489
  this._relativeMouseMode = Bindings.window_setRelativeMouseMode(this._id, relative)
485
490
  }
@@ -487,41 +492,27 @@ class Window extends EventsViaPoll {
487
492
  unsetRelativeMouseMode () { this.setRelativeMouseMode(false) }
488
493
 
489
494
  render (width, height, stride, format, buffer, options = {}) {
490
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
495
+ validateNotDestroyed(this)
491
496
 
492
497
  if (this._opengl) { throw new Error("can't call render in opengl mode") }
493
498
  if (this._webgpu) { throw new Error("can't call render in webgpu mode") }
494
499
 
495
- if (typeof options !== 'object' || options === null) { throw Object.assign(new Error("options must be an object"), { options }) }
500
+ validateObject('options', options)
496
501
 
497
502
  const {
498
503
  scaling = 'nearest',
499
- dstRect = null,
504
+ dstRect,
500
505
  } = options
501
506
 
502
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
503
- if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
504
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
505
- if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
506
- if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
507
- if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
508
- const helpers = VideoFormatHelpers[format]
509
- if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
510
- if (!helpers.isRenderable) { throw Object.assign(new Error("format can't be rendered"), { format }) }
511
- if (stride < helpers.minStride(width)) { throw Object.assign(new Error("stride is smaller than expected"), { stride, width, format }) }
512
- if (stride > 2 ** 31 - 1) { throw Object.assign(new Error("stride must be a 32-bit integer"), { stride }) }
513
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
514
- if (buffer.length < helpers.minBufferSize(stride, height)) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height, format }) }
515
- if (typeof scaling !== 'string') { throw Object.assign(new Error("scaling must be a string"), { scaling }) }
516
-
517
- if (dstRect !== null) {
518
- if (typeof dstRect !== 'object') { throw Object.assign(new Error("dstRect must be an object"), { dstRect }) }
519
- if (!Number.isFinite(dstRect.x)) { throw Object.assign(new Error("dstRect.x must be a number"), { dstRect }) }
520
- if (!Number.isFinite(dstRect.y)) { throw Object.assign(new Error("dstRect.y must be a number"), { dstRect }) }
521
- if (!Number.isFinite(dstRect.width)) { throw Object.assign(new Error("dstRect.width must be a number"), { dstRect }) }
522
- if (dstRect.width <= 0) { throw Object.assign(new Error("dstRect.width must be positive"), { dstRect }) }
523
- if (!Number.isFinite(dstRect.height)) { throw Object.assign(new Error("dstRect.height must be a number"), { dstRect }) }
524
- if (dstRect.height <= 0) { throw Object.assign(new Error("dstRect.height must be positive"), { dstRect }) }
507
+ validateImage(width, height, stride, format, buffer, validateRenderableFormat)
508
+ validateString('scaling', scaling)
509
+
510
+ if (dstRect !== undefined) {
511
+ validateObject('dstRect', dstRect)
512
+ validateFloat('dstRect.x', dstRect.x)
513
+ validateFloat('dstRect.y', dstRect.y)
514
+ validatePositiveFloat('dstRect.width', dstRect.width)
515
+ validatePositiveFloat('dstRect.height', dstRect.height)
525
516
  }
526
517
 
527
518
  const _format = Enums.pixelFormat[format]
@@ -529,25 +520,13 @@ class Window extends EventsViaPoll {
529
520
  const _scaling = Enums.scaleMode[scaling]
530
521
  if (_scaling === undefined) { throw Object.assign(new Error("scaling must be 'nearest' or 'linear'"), { scaling }) }
531
522
 
532
- Bindings.window_render(this._id, width, height, stride, _format, buffer, _scaling, dstRect)
523
+ Bindings.window_render(this._id, width, height, stride, _format, buffer, _scaling, dstRect ?? null)
533
524
  }
534
525
 
535
526
  setIcon (width, height, stride, format, buffer) {
536
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
537
-
538
- if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
539
- if (width <= 0 || width > 2 ** 31 - 1) { throw Object.assign(new Error("width must be a positive 32-bit integer"), { width }) }
540
- if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
541
- if (height <= 0 || height > 2 ** 31 - 1) { throw Object.assign(new Error("height must be a positive 32-bit integer"), { height }) }
542
- if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
543
- if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
544
- const helpers = VideoFormatHelpers[format]
545
- if (helpers === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
546
- if (helpers.isYuv) { throw Object.assign(new Error("format must be an RGB format"), { format }) }
547
- if (stride < helpers.minStride(width)) { throw Object.assign(new Error("stride is smaller than expected"), { stride, width, format }) }
548
- if (stride > 2 ** 31 - 1) { throw Object.assign(new Error("stride must be a 32-bit integer"), { stride }) }
549
- if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
550
- if (buffer.length < helpers.minBufferSize(stride, height)) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height, format }) }
527
+ validateNotDestroyed(this)
528
+
529
+ validateImage(width, height, stride, format, buffer, validateRgbFormat)
551
530
 
552
531
  const _format = Enums.pixelFormat[format]
553
532
 
@@ -555,36 +534,40 @@ class Window extends EventsViaPoll {
555
534
  }
556
535
 
557
536
  flash (untilFocused = false) {
558
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
537
+ validateNotDestroyed(this)
559
538
 
560
- if (typeof untilFocused !== 'boolean') { throw Object.assign(new Error("untilFocused must be a boolean"), { untilFocused }) }
539
+ validateBoolean('untilFocused', untilFocused)
561
540
 
562
541
  Bindings.window_flash(this._id, untilFocused ? 2 : 1)
563
542
  }
564
543
 
565
544
  stopFlashing () {
566
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
545
+ validateNotDestroyed(this)
567
546
 
568
547
  Bindings.window_flash(this._id, 0)
569
548
  }
570
549
 
571
550
  get destroyed () { return this._destroyed }
572
551
  destroy () {
573
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
552
+ validateNotDestroyed(this)
574
553
 
554
+ this._destroyed = true
555
+ Globals.windows.all.delete(this._id)
575
556
  if (Globals.windows.hovered === this) { Globals.windows.hovered = null }
576
557
  if (Globals.windows.focused === this) { Globals.windows.focused = null }
577
558
 
559
+ const destroyNative = () => {
560
+ try { Bindings.window_destroy(this._id) }
561
+ catch {}
562
+ }
563
+
578
564
  // Defer the native destruction until the pump has unwound.
579
565
  if (Bindings.events_isDispatchingFromWatch()) {
580
- process.nextTick(() => { Bindings.window_destroy(this._id) })
566
+ process.nextTick(destroyNative)
581
567
  }
582
568
  else {
583
- Bindings.window_destroy(this._id)
569
+ destroyNative()
584
570
  }
585
- this._destroyed = true
586
-
587
- Globals.windows.all.delete(this._id)
588
571
 
589
572
  // We might be inside an event listener
590
573
  this._retire(() => Object.assign(new Error("window is destroyed"), { id: this._id }))
@@ -596,7 +579,7 @@ class Window extends EventsViaPoll {
596
579
  }
597
580
 
598
581
  destroyGently () {
599
- if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
582
+ validateNotDestroyed(this)
600
583
 
601
584
  let shouldPrevent = false
602
585
  Globals.events.withoutPolling(() => {