quickwin 3.0.1 → 3.0.3

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 (163) hide show
  1. package/README.en.md +3 -0
  2. package/README.md +2 -0
  3. package/package.json +2 -3
  4. package/qwin-nowasm-x86.exe +0 -0
  5. package/qwin-nowasm.exe +0 -0
  6. package/qwin-x86.exe +0 -0
  7. package/qwin.exe +0 -0
  8. package/examples/PdfCanvas.js +0 -38
  9. package/examples/PdfCanvas.tsx +0 -54
  10. package/examples/execd/main.js +0 -23379
  11. package/examples/execd/tray.js +0 -192
  12. package/examples/execd/worker.js +0 -122
  13. package/examples/pdf_preview2.js +0 -398
  14. package/examples/pdf_preview2.ts +0 -353
  15. package/examples/pdf_viewer.js +0 -22515
  16. package/examples/pdf_viewer.tsx +0 -228
  17. package/examples/test_gallery.js +0 -18692
  18. package/examples/test_gallery.tsx +0 -291
  19. package/examples/test_react_lazy.js +0 -18560
  20. package/examples/test_react_lazy.tsx +0 -65
  21. package/examples/test_react_listbox.js +0 -18562
  22. package/examples/test_react_listbox.tsx +0 -73
  23. package/examples/test_root_window.js +0 -18532
  24. package/examples/test_root_window.tsx +0 -41
  25. package/examples/test_tab.js +0 -18563
  26. package/examples/test_tab.tsx +0 -77
  27. package/examples/tray_demo.js +0 -86
  28. package/examples/tray_demo.tsx +0 -89
  29. package/test/add.wasm +0 -0
  30. package/test/bench_ffi_callraw.js +0 -235
  31. package/test/bench_ffi_callraw.ts +0 -235
  32. package/test/bench_ffi_overhead.js +0 -139
  33. package/test/bench_ffi_overhead.ts +0 -141
  34. package/test/bench_sendmessage.js +0 -104
  35. package/test/bench_sendmessage.ts +0 -100
  36. package/test/bench_sendmessage_nonnull.js +0 -187
  37. package/test/bench_sendmessage_nonnull.ts +0 -188
  38. package/test/complex.wasm +0 -0
  39. package/test/complex_imports.wasm +0 -0
  40. package/test/debug/http_server_e2e.js +0 -31
  41. package/test/debug/minwasm.js +0 -52
  42. package/test/debug/mupdf_debug.js +0 -86
  43. package/test/debug/wasm.js +0 -122
  44. package/test/debug/wasm_export_debug.js +0 -54
  45. package/test/debug/wasm_names.js +0 -36
  46. package/test/example.pdf +0 -0
  47. package/test/global_imports.wasm +0 -0
  48. package/test/import_func.wasm +0 -0
  49. package/test/imports.wasm +0 -0
  50. package/test/mupdf-cdn.js +0 -40
  51. package/test/mupdf-cdn.ts +0 -43
  52. package/test/run.js +0 -111
  53. package/test/run.ts +0 -116
  54. package/test/sjlj.wasm +0 -0
  55. package/test/test_basic.js +0 -7
  56. package/test/test_basic.ts +0 -9
  57. package/test/test_basic_fetch.js +0 -63
  58. package/test/test_basic_fetch.ts +0 -59
  59. package/test/test_brotli.js +0 -48
  60. package/test/test_brotli.ts +0 -52
  61. package/test/test_fetch_cache.js +0 -169
  62. package/test/test_fetch_cache.ts +0 -187
  63. package/test/test_ffi.js +0 -157
  64. package/test/test_ffi.ts +0 -170
  65. package/test/test_ffi_abi.js +0 -290
  66. package/test/test_ffi_abi.ts +0 -274
  67. package/test/test_ffi_array.js +0 -116
  68. package/test/test_ffi_array.ts +0 -141
  69. package/test/test_ffi_bind.js +0 -656
  70. package/test/test_ffi_bind.ts +0 -670
  71. package/test/test_ffi_struct.js +0 -779
  72. package/test/test_ffi_struct.ts +0 -828
  73. package/test/test_ffi_struct_layout.js +0 -495
  74. package/test/test_ffi_struct_layout.ts +0 -543
  75. package/test/test_ffi_user32.js +0 -103
  76. package/test/test_ffi_user32.ts +0 -113
  77. package/test/test_ffi_windows.js +0 -172
  78. package/test/test_ffi_windows.ts +0 -187
  79. package/test/test_files.js +0 -102
  80. package/test/test_files.ts +0 -121
  81. package/test/test_frame_encoding.js +0 -128
  82. package/test/test_frame_encoding.ts +0 -132
  83. package/test/test_gdi_draw.js +0 -681
  84. package/test/test_gdi_draw.ts +0 -725
  85. package/test/test_helper.js +0 -91
  86. package/test/test_helper.ts +0 -88
  87. package/test/test_http_import.js +0 -96
  88. package/test/test_http_import.ts +0 -93
  89. package/test/test_http_server.js +0 -214
  90. package/test/test_http_server.ts +0 -211
  91. package/test/test_import_dir.js +0 -4
  92. package/test/test_import_dir.ts +0 -5
  93. package/test/test_ipv6.js +0 -134
  94. package/test/test_ipv6.ts +0 -130
  95. package/test/test_jsx.js +0 -1314
  96. package/test/test_meta_url.js +0 -3
  97. package/test/test_meta_url.ts +0 -4
  98. package/test/test_mupdf_render.js +0 -51
  99. package/test/test_mupdf_render.ts +0 -55
  100. package/test/test_mupdf_twice.js +0 -62
  101. package/test/test_mupdf_twice.ts +0 -65
  102. package/test/test_mupdf_wasm.js +0 -39
  103. package/test/test_mupdf_wasm.ts +0 -32
  104. package/test/test_net_event.js +0 -57
  105. package/test/test_net_event.ts +0 -56
  106. package/test/test_net_fetch.js +0 -384
  107. package/test/test_net_fetch.ts +0 -373
  108. package/test/test_net_websocket.js +0 -180
  109. package/test/test_net_websocket.ts +0 -164
  110. package/test/test_polyfill.js +0 -144
  111. package/test/test_polyfill.ts +0 -159
  112. package/test/test_printer.js +0 -137
  113. package/test/test_printer.ts +0 -148
  114. package/test/test_proc.js +0 -141
  115. package/test/test_proc.ts +0 -163
  116. package/test/test_react_complex.js +0 -18678
  117. package/test/test_react_counter.js +0 -18555
  118. package/test/test_react_flex.js +0 -18871
  119. package/test/test_react_hidden.js +0 -18560
  120. package/test/test_react_listview_link.js +0 -18590
  121. package/test/test_react_notify.js +0 -18797
  122. package/test/test_react_render.js +0 -18550
  123. package/test/test_registry.js +0 -74
  124. package/test/test_registry.ts +0 -95
  125. package/test/test_sysinfo.js +0 -122
  126. package/test/test_sysinfo.ts +0 -141
  127. package/test/test_url.js +0 -173
  128. package/test/test_url.ts +0 -183
  129. package/test/test_wasm_basic.js +0 -85
  130. package/test/test_wasm_basic.ts +0 -73
  131. package/test/test_wasm_import_global.js +0 -41
  132. package/test/test_wasm_import_global.ts +0 -45
  133. package/test/test_wasm_sjlj.js +0 -145
  134. package/test/test_wasm_sjlj.ts +0 -125
  135. package/test/test_wasm_types.js +0 -97
  136. package/test/test_wasm_types.ts +0 -124
  137. package/test/test_wolfssl_read.js +0 -203
  138. package/test/test_wolfssl_read.ts +0 -184
  139. package/test/test_worker.js +0 -21
  140. package/test/test_worker.ts +0 -24
  141. package/test/test_worker_concurrent.js +0 -45
  142. package/test/test_worker_concurrent.ts +0 -56
  143. package/test/test_worker_data_url.js +0 -46
  144. package/test/test_worker_data_url.ts +0 -51
  145. package/test/test_worker_fetch.js +0 -26
  146. package/test/test_worker_fetch.ts +0 -27
  147. package/test/test_worker_net.js +0 -69
  148. package/test/test_worker_net.ts +0 -66
  149. package/test/test_worker_wasm_concurrent.js +0 -44
  150. package/test/test_worker_wasm_concurrent.ts +0 -45
  151. package/test/types.wasm +0 -0
  152. package/test/worker_fetch.js +0 -13
  153. package/test/worker_fetch.ts +0 -13
  154. package/test/worker_helper.js +0 -1
  155. package/test/worker_helper.ts +0 -1
  156. package/test/worker_http.js +0 -18
  157. package/test/worker_http.ts +0 -20
  158. package/test/worker_import_lib.js +0 -12
  159. package/test/worker_import_lib.ts +0 -13
  160. package/test/worker_module.js +0 -16
  161. package/test/worker_module.ts +0 -17
  162. package/test/worker_wasm_concurrent.js +0 -22
  163. package/test/worker_wasm_concurrent.ts +0 -22
@@ -1,543 +0,0 @@
1
- import { Tester } from './test_helper.js'
2
- import { computeStructLayout, computeArray } from '../lib/ffi/struct.js'
3
- import type { Fields } from '../lib/ffi/struct.js'
4
- import type { C_Struct, C_Union, C_Array } from '../lib/ffi/ctype.js'
5
-
6
- export const suite = {
7
- name: 'ffi-struct-layout',
8
- run: (t: Tester) => {
9
- // === 基础布局 ===
10
-
11
- t.section('Basic struct')
12
- {
13
- const s: C_Struct = {
14
- '#': 'struct',
15
- a: 'u32',
16
- b: 'u32',
17
- c: 'u32',
18
- }
19
- const r = computeStructLayout(s)
20
- t.check('size', 12, r.size)
21
- t.check('align', 4, r.maxEffectiveAlign)
22
- t.check('offset a', 0, r.fields[0]!.offset)
23
- t.check('offset b', 4, r.fields[1]!.offset)
24
- t.check('offset c', 8, r.fields[2]!.offset)
25
- }
26
-
27
- t.section('Padding')
28
- {
29
- const s: C_Struct = {
30
- '#': 'struct',
31
- a: 'u8',
32
- b: 'u32',
33
- c: 'u8',
34
- }
35
- const r = computeStructLayout(s)
36
- t.check('size', 12, r.size) // 1 + 3pad + 4 + 1 + 3pad
37
- t.check('offset a', 0, r.fields[0]!.offset)
38
- t.check('offset b', 4, r.fields[1]!.offset) // aligned to 4
39
- t.check('offset c', 8, r.fields[2]!.offset)
40
- }
41
-
42
- // === 嵌套 struct ===
43
-
44
- t.section('Nested struct')
45
- {
46
- const inner: C_Struct = {
47
- '#': 'struct',
48
- x: 'u32',
49
- y: 'u32',
50
- }
51
- const s: C_Struct = {
52
- '#': 'struct',
53
- a: 'u8',
54
- b: inner,
55
- }
56
- const r = computeStructLayout(s)
57
- t.check('size', 12, r.size) // 1 + 3pad + 8
58
- t.check('offset a', 0, r.fields[0]!.offset)
59
- t.check('offset b', 4, r.fields[1]!.offset) // b 自身偏移
60
- const b = r.fields[1]!
61
- t.checkTrue('b 是 struct 成员', b.type.tag === 'struct')
62
- if (b.type.tag === 'struct') {
63
- t.check('b.x (相对子起点)', 0, b.type.fields[0]!.offset)
64
- t.check('b.y (相对子起点)', 4, b.type.fields[1]!.offset)
65
- }
66
- }
67
-
68
- // === Union ===
69
-
70
- t.section('Union')
71
- {
72
- const u: C_Union = {
73
- '#': 'union',
74
- a: 'u8',
75
- b: 'u32',
76
- c: 'u64',
77
- }
78
- const r = computeStructLayout(u)
79
- t.check('size', 8, r.size) // max member size
80
- t.check('align', 8, r.maxEffectiveAlign) // max member align
81
- t.check('offset a', 0, r.fields[0]!.offset)
82
- t.check('offset b', 0, r.fields[1]!.offset)
83
- t.check('offset c', 0, r.fields[2]!.offset)
84
- }
85
-
86
- t.section('Union in struct')
87
- {
88
- const u: C_Union = {
89
- '#': 'union',
90
- a: 'u8',
91
- b: 'u32',
92
- }
93
- const s: C_Struct = {
94
- '#': 'struct',
95
- a: 'u8',
96
- u: u,
97
- c: 'u8',
98
- }
99
- const r = computeStructLayout(s)
100
- t.check('size', 12, r.size) // 1 + 3pad + 4 + 1 + 3pad
101
- t.check('offset a', 0, r.fields[0]!.offset)
102
- t.check('offset u', 4, r.fields[1]!.offset)
103
- const uf = r.fields[1]!
104
- t.checkTrue('u 是 union 成员', uf.type.tag === 'union')
105
- if (uf.type.tag === 'union') {
106
- t.check('u.a', 0, uf.type.fields[0]!.offset) // union member 同偏移
107
- t.check('u.b', 0, uf.type.fields[1]!.offset)
108
- }
109
- t.check('offset c', 8, r.fields[2]!.offset)
110
- }
111
-
112
- // === 匿名成员提升 ===
113
-
114
- t.section('Anonymous struct promotion')
115
- {
116
- const inner: C_Struct = {
117
- '#': 'struct',
118
- x: 'u32',
119
- y: 'u32',
120
- }
121
- const s: C_Struct = {
122
- '#': 'struct',
123
- a: 'u8',
124
- $anon1: inner, // 匿名:字段被提升
125
- }
126
- const r = computeStructLayout(s)
127
- t.check('size', 12, r.size) // 1 + 3pad + 8
128
- t.check('提升后字段数', 3, r.fields.length)
129
- t.check('offset a', 0, r.fields[0]!.offset)
130
- t.check('offset x (提升)', 4, r.fields[1]!.offset)
131
- t.check('offset y (提升)', 8, r.fields[2]!.offset)
132
- }
133
-
134
- // === Array ===
135
-
136
- t.section('Array of basic')
137
- {
138
- const arr: C_Array = { '#': 'array', element: 'u32', length: 4 }
139
- const r = computeArray(arr)
140
- t.check('size', 16, r.size)
141
- t.check('align', 4, r.align)
142
- }
143
-
144
- t.section('Array of struct')
145
- {
146
- const inner: C_Struct = {
147
- '#': 'struct',
148
- x: 'u32',
149
- y: 'u32',
150
- }
151
- const arr: C_Array = { '#': 'array', element: inner, length: 3 }
152
- const r = computeArray(arr)
153
- t.check('size', 24, r.size) // 3 * 8
154
- t.check('align', 4, r.align)
155
- }
156
-
157
- t.section('Array in struct(糖写法)')
158
- {
159
- const s: C_Struct = {
160
- '#': 'struct',
161
- a: 'u8',
162
- arr: 'u32[4]', // 数组糖(元素是 token 时的简写)
163
- }
164
- const r = computeStructLayout(s)
165
- t.check('size', 20, r.size) // 1 + 3pad + 16
166
- t.check('offset a', 0, r.fields[0]!.offset)
167
- t.check('offset arr', 4, r.fields[1]!.offset)
168
- }
169
-
170
- t.section('Array in struct(# 形式 + 聚合元素)')
171
- {
172
- const inner: C_Struct = { '#': 'struct', x: 'u16', y: 'u16' }
173
- const s: C_Struct = {
174
- '#': 'struct',
175
- a: 'u8',
176
- arr: { '#': 'array', element: inner, length: 3 },
177
- }
178
- const r = computeStructLayout(s)
179
- t.check('size == 14 (a@0 + 1pad + 3*4,align 取 inner 的 2)', 14, r.size)
180
- t.check('offset arr == 2 (inner align 2)', 2, r.fields[1]!.offset)
181
- }
182
-
183
- // === String ===
184
-
185
- // === String(字符串糖:布局 unit[length] + 解释 @encoding 正交) ===
186
-
187
- t.section('CString sugar(u16[10]@utf-16le)')
188
- {
189
- const r = computeStructLayout({ '#': 'struct', name: 'u16[10]@utf-16le' })
190
- t.check('size == 20 (typesize × length)', 20, r.size)
191
- t.check('align == 2 (u16 槽)', 2, r.maxEffectiveAlign)
192
- t.check('offset', 0, r.fields[0]!.offset)
193
- }
194
-
195
- t.section('CString sugar: 布局与解释正交(size 只看 unit×length)')
196
- {
197
- const r = computeStructLayout({ '#': 'struct', a: 'u8', s: 'u8[8]@utf-16le' })
198
- t.check('错配 u8[8]@utf-16le size == 9', 9, r.size) // 1 + 8,encoding 不参与布局
199
- t.check('align == 1 (u8 槽)', 1, r.maxEffectiveAlign)
200
- t.check('offset s == 1 (无 padding)', 1, r.fields[1]!.offset)
201
- const r2 = computeStructLayout({ '#': 'struct', s: 'u16[4]@utf-8' })
202
- t.check('错配 u16[4]@utf-8 size == 8', 8, r2.size) // 2 × 4
203
- t.check('align == 2 (u16 槽)', 2, r2.maxEffectiveAlign)
204
- }
205
-
206
- t.section('CString sugar validation')
207
- {
208
- const rejected = (s: C_Struct): boolean => {
209
- try { computeStructLayout(s); return false } catch { return true }
210
- }
211
- t.check('length 0 rejected', true,
212
- rejected({ '#': 'struct', x: 'u8[0]@utf-8' }))
213
- t.check('unit 非 u8/u16 rejected(落 token 分支抛)', true,
214
- rejected({ '#': 'struct', x: 'u32[4]@utf-8' as any }))
215
- t.check('unknown encoding rejected', true,
216
- rejected({ '#': 'struct', x: 'u16[4]@utf-16be' as any }))
217
- t.check('旧写法 {\'#\':\'string\'} rejected(词汇已删)', true,
218
- rejected({ '#': 'struct', x: { '#': 'string', unit: 'u16', length: 4, encoding: 'utf-16le' } } as any))
219
- }
220
-
221
- // === Pack ===
222
-
223
- t.section('Pack(1)')
224
- {
225
- const s: C_Struct = {
226
- '#': 'struct', '#pack': 1,
227
- a: 'u8',
228
- b: 'u32',
229
- c: 'u8',
230
- }
231
- const r = computeStructLayout(s)
232
- t.check('size', 6, r.size) // 1 + 4 + 1, no padding
233
- t.check('offset a', 0, r.fields[0]!.offset)
234
- t.check('offset b', 1, r.fields[1]!.offset) // no alignment
235
- t.check('offset c', 5, r.fields[2]!.offset)
236
- }
237
-
238
- t.section('Pack(4)')
239
- {
240
- const s: C_Struct = {
241
- '#': 'struct', '#pack': 4,
242
- a: 'u8',
243
- b: 'u64', // natural align 8, capped to 4
244
- }
245
- const r = computeStructLayout(s)
246
- t.check('offset a', 0, r.fields[0]!.offset)
247
- t.check('offset b', 4, r.fields[1]!.offset) // aligned to 4, not 8
248
- }
249
-
250
- // === Alignas(键尾 @N) ===
251
-
252
- t.section('Alignas')
253
- {
254
- const s: C_Struct = {
255
- '#': 'struct',
256
- a: 'u8',
257
- 'b@8': 'u32',
258
- }
259
- const r = computeStructLayout(s)
260
- t.check('offset a', 0, r.fields[0]!.offset)
261
- t.check('offset b', 8, r.fields[1]!.offset) // aligned to 8
262
- t.check('字段名剥离 @N', 'b', r.fields[1]!.name)
263
- }
264
-
265
- t.section('Alignas(2) < natural(4)')
266
- {
267
- const s: C_Struct = {
268
- '#': 'struct',
269
- a: 'u8',
270
- 'b@2': 'u32', // natural is 4, alignas is 2
271
- }
272
- const r = computeStructLayout(s)
273
- t.check('offset a', 0, r.fields[0]!.offset)
274
- t.check('offset b', 4, r.fields[1]!.offset) // natural align wins (4 > 2)
275
- }
276
-
277
- // === Pack + Alignas interaction ===
278
-
279
- t.section('Pack(1) + Alignas(8)')
280
- {
281
- const s: C_Struct = {
282
- '#': 'struct', '#pack': 1,
283
- a: 'u8',
284
- 'b@8': 'u32', // alignas wins over pack
285
- }
286
- const r = computeStructLayout(s)
287
- t.check('offset a', 0, r.fields[0]!.offset)
288
- t.check('offset b', 8, r.fields[1]!.offset) // alignas(8) > pack(1)
289
- }
290
-
291
- t.section('Pack(4) + Alignas(2)')
292
- {
293
- const s: C_Struct = {
294
- '#': 'struct', '#pack': 4,
295
- a: 'u8',
296
- 'b@2': 'u64', // natural 8, pack 4, alignas 2
297
- }
298
- const r = computeStructLayout(s)
299
- t.check('offset a', 0, r.fields[0]!.offset)
300
- t.check('offset b', 4, r.fields[1]!.offset) // min(pack=4, natural=8)=4, max(4, alignas=2)=4
301
- }
302
-
303
- t.section('Pack(4) + Alignas(16)')
304
- {
305
- const s: C_Struct = {
306
- '#': 'struct', '#pack': 4,
307
- a: 'u8',
308
- 'b@16': 'u32', // alignas(16) > pack(4)
309
- }
310
- const r = computeStructLayout(s)
311
- t.check('offset a', 0, r.fields[0]!.offset)
312
- t.check('offset b', 16, r.fields[1]!.offset) // alignas wins
313
- }
314
-
315
- // === 位域 ===
316
- // ground truth: mingw x86_64 交叉编译 sizeof/offsetof(含 windows.h 自带的
317
- // DCB/COMSTAT)。i686 同结果(DCB/COMSTAT 无指针字段,32/64 位布局一致)。
318
-
319
- const bitOf = (fields: Fields, n: string): { offset: number, bit: number } => {
320
- const f = fields.find(x => x.name === n)!
321
- t.checkTrue(`${n} is bitfield`, f.type.tag === 'bitfield')
322
- return { offset: f.offset, bit: (f.type as { bit: number }).bit }
323
- }
324
- const offOf = (fields: Fields, n: string): number => fields.find(x => x.name === n)!.offset
325
-
326
- t.section('Bitfield: 同单元打包 + 溢出开新单元(MS 文档例子)')
327
- {
328
- // 9+7=16 塞满第一单元,30 放不下开第二,18 开第三。sizeof 应为 12。
329
- const s: C_Struct = {
330
- '#': 'struct',
331
- first: 'u32:9',
332
- second: 'u32:7',
333
- may_straddle: 'u32:30',
334
- last: 'u32:18',
335
- }
336
- const r = computeStructLayout(s)
337
- t.check('sizeof == 12', 12, r.size)
338
- t.check('first.offset == 0', 0, bitOf(r.fields, 'first').offset)
339
- t.check('second.offset == 0 (共单元)', 0, bitOf(r.fields, 'second').offset)
340
- t.check('second.bit == 9', 9, bitOf(r.fields, 'second').bit)
341
- t.check('may_straddle.offset == 4 (新单元)', 4, bitOf(r.fields, 'may_straddle').offset)
342
- t.check('may_straddle.bit == 0', 0, bitOf(r.fields, 'may_straddle').bit)
343
- t.check('last.offset == 8 (再新单元)', 8, bitOf(r.fields, 'last').offset)
344
- }
345
-
346
- t.section('Bitfield: 精确填充算放得下')
347
- {
348
- // 15+17=32 恰好塞满一个 32 位单元 → 不开新单元(mingw 实测 sizeof=8, w@4)
349
- const s: C_Struct = {
350
- '#': 'struct',
351
- a: 'u32:15',
352
- b: 'u32:17',
353
- w: 'u16',
354
- }
355
- const r = computeStructLayout(s)
356
- t.check('sizeof == 8 (单元 0-3 + w@4, pad 到 8)', 8, r.size)
357
- t.check('b.offset == 0 (共单元)', 0, bitOf(r.fields, 'b').offset)
358
- t.check('b.bit == 15', 15, bitOf(r.fields, 'b').bit)
359
- t.check('w.offset == 4 (紧跟单元)', 4, offOf(r.fields, 'w'))
360
- }
361
-
362
- t.section('Bitfield: 非位域成员打断组 → 游标跳完整单元边界')
363
- {
364
- // u32 单元用 12 位 = 2 字节,但 char 落在单元边界 4(不是按字节截断的 2)
365
- const s: C_Struct = {
366
- '#': 'struct',
367
- a: 'u32:12',
368
- z: 'u8',
369
- }
370
- const r = computeStructLayout(s)
371
- t.check('sizeof == 8 (char@4, pad 到 8)', 8, r.size)
372
- t.check('z.offset == 4 (单元边界)', 4, offOf(r.fields, 'z'))
373
-
374
- // u16 单元用 12 位 = 2 字节 = 完整单元,char 紧随其后
375
- const s2: C_Struct = {
376
- '#': 'struct',
377
- a: 'u16:12',
378
- z: 'u8',
379
- }
380
- const r2 = computeStructLayout(s2)
381
- t.check('u16 单元 sizeof == 4', 4, r2.size)
382
- t.check('z.offset == 2 (u16 单元边界)', 2, offOf(r2.fields, 'z'))
383
- }
384
-
385
- t.section('Bitfield: 同宽不分签别,共单元')
386
- {
387
- const s: C_Struct = {
388
- '#': 'struct',
389
- a: 'u32:8',
390
- b: 'i32:8',
391
- }
392
- const r = computeStructLayout(s)
393
- t.check('sizeof == 4 (有/无符号共单元)', 4, r.size)
394
- t.check('b.bit == 8', 8, bitOf(r.fields, 'b').bit)
395
- }
396
-
397
- t.section('Bitfield: DCB(mingw windows.h sizeof=28)')
398
- {
399
- const s: C_Struct = {
400
- '#': 'struct',
401
- DCBlength: 'u32',
402
- BaudRate: 'u32',
403
- fBinary: 'u32:1',
404
- fParity: 'u32:1',
405
- fOutxCtsFlow: 'u32:1',
406
- fOutxDsrFlow: 'u32:1',
407
- fDtrControl: 'u32:2',
408
- fDsrSensitivity: 'u32:1',
409
- fTXContinueOnXoff: 'u32:1',
410
- fOutX: 'u32:1',
411
- fInX: 'u32:1',
412
- fErrorChar: 'u32:1',
413
- fNull: 'u32:1',
414
- fRtsControl: 'u32:2',
415
- fAbortOnError: 'u32:1',
416
- fDummy2: 'u32:17',
417
- wReserved: 'u16',
418
- XonLim: 'u16',
419
- XoffLim: 'u16',
420
- ByteSize: 'u8',
421
- Parity: 'u8',
422
- StopBits: 'u8',
423
- XonChar: 'i8',
424
- XoffChar: 'i8',
425
- ErrorChar: 'i8',
426
- EofChar: 'i8',
427
- EvtChar: 'i8',
428
- wReserved1: 'u16',
429
- }
430
- const r = computeStructLayout(s)
431
- t.check('DCB.size == 28', 28, r.size)
432
- t.check('fBinary.offset == 8 (单元基址)', 8, bitOf(r.fields, 'fBinary').offset)
433
- // 前 13 个位域 15 位 + fDummy2:17 = 32 位,恰好塞满一个 4 字节单元
434
- t.check('fDummy2.offset == 8 (共单元,未溢出)', 8, bitOf(r.fields, 'fDummy2').offset)
435
- t.check('fDummy2.bit == 15', 15, bitOf(r.fields, 'fDummy2').bit)
436
- t.check('wReserved.offset == 12', 12, offOf(r.fields, 'wReserved'))
437
- t.check('XonLim.offset == 14', 14, offOf(r.fields, 'XonLim'))
438
- t.check('ByteSize.offset == 18', 18, offOf(r.fields, 'ByteSize'))
439
- t.check('EofChar.offset == 24', 24, offOf(r.fields, 'EofChar'))
440
- t.check('wReserved1.offset == 26', 26, offOf(r.fields, 'wReserved1'))
441
- }
442
-
443
- t.section('Bitfield: COMSTAT(mingw windows.h sizeof=12)')
444
- {
445
- const s: C_Struct = {
446
- '#': 'struct',
447
- fCtsHold: 'u32:1',
448
- fDsrHold: 'u32:1',
449
- fRlsdHold: 'u32:1',
450
- fXoffHold: 'u32:1',
451
- fXoffSent: 'u32:1',
452
- fEof: 'u32:1',
453
- fTxim: 'u32:1',
454
- fReserved: 'u32:25',
455
- cbInQue: 'u32',
456
- cbOutQue: 'u32',
457
- }
458
- const r = computeStructLayout(s)
459
- t.check('COMSTAT.size == 12', 12, r.size)
460
- t.check('fReserved.bit == 7', 7, bitOf(r.fields, 'fReserved').bit)
461
- t.check('cbInQue.offset == 4', 4, offOf(r.fields, 'cbInQue'))
462
- t.check('cbOutQue.offset == 8', 8, offOf(r.fields, 'cbOutQue'))
463
- }
464
-
465
- t.section('Bitfield: union 位域完全别名(mingw 实测:每位域成员独立 offset 0/bit 0)')
466
- {
467
- const u: C_Union = {
468
- '#': 'union',
469
- a: 'u32:1',
470
- b: 'u32:1',
471
- }
472
- const r = computeStructLayout(u)
473
- t.check('sizeof == 4', 4, r.size)
474
- t.check('a.offset == 0', 0, bitOf(r.fields, 'a').offset)
475
- t.check('a.bit == 0', 0, bitOf(r.fields, 'a').bit)
476
- t.check('b.offset == 0 (同址别名)', 0, bitOf(r.fields, 'b').offset)
477
- t.check('b.bit == 0 (同位别名,实测 a=1→b=1)', 0, bitOf(r.fields, 'b').bit)
478
- }
479
-
480
- t.section('Bitfield: union 溢出/混合形状不越界(regression:曾推进游标致 offset 越出 size)')
481
- {
482
- // 溢出:struct 里 30+4>32 开新单元;union 每成员独立回 offset 0
483
- const u2: C_Union = { '#': 'union', a: 'u32:30', b: 'u32:4' }
484
- const r2 = computeStructLayout(u2)
485
- t.check('overflow sizeof == 4', 4, r2.size)
486
- t.check('overflow a.offset == 0', 0, bitOf(r2.fields, 'a').offset)
487
- t.check('overflow b.offset == 0(回 offset 0 重叠,曾为 4)', 0, bitOf(r2.fields, 'b').offset)
488
- t.check('overflow b.bit == 0', 0, bitOf(r2.fields, 'b').bit)
489
-
490
- // 位域后跟普通成员:flushUnit 不得推进 union 游标
491
- const u3: C_Union = { '#': 'union', a: 'u32:1', b: 'u8' }
492
- const r3 = computeStructLayout(u3)
493
- t.check('mixed sizeof == 4', 4, r3.size)
494
- t.check('mixed a.offset == 0', 0, bitOf(r3.fields, 'a').offset)
495
- t.check('mixed b.offset == 0(曾错位到 4 → 越出 size 4)', 0, offOf(r3.fields, 'b'))
496
- }
497
-
498
- t.section('Bitfield validation')
499
- {
500
- const rejected = (s: C_Struct): boolean => {
501
- try { computeStructLayout(s); return false } catch { return true }
502
- }
503
- t.check('width 0 rejected (v1 不支持 :0 填充)', true,
504
- rejected({ '#': 'struct', x: 'u32:0' }))
505
- t.check('width > unit bits rejected', true,
506
- rejected({ '#': 'struct', x: 'u32:33' }))
507
- t.check('width > u8 bits rejected', true,
508
- rejected({ '#': 'struct', x: 'u8:9' }))
509
- t.check('negative width rejected(非 \\d+ → 落 token 分支抛)', true,
510
- rejected({ '#': 'struct', x: 'u32:-1' }))
511
- t.check('non-integer width rejected', true,
512
- rejected({ '#': 'struct', x: 'u32:2.5' }))
513
- t.check('array of bitfields rejected(# 形式)', true,
514
- rejected({ '#': 'struct', x: { '#': 'array', element: 'u32:1', length: 2 } }))
515
- t.check('array of bitfields rejected(数组糖,类型层拒绝故 as any)', true,
516
- rejected({ '#': 'struct', x: 'u32:1[2]' as any }))
517
- t.check('anonymous bitfield rejected', true,
518
- rejected({ '#': 'struct', $anon1: 'u32:1' } as any))
519
- t.check('f32 bitfield unit rejected(归一后非整数档)', true,
520
- rejected({ '#': 'struct', x: 'f32:3' as any }))
521
- t.check('unknown bitfield unit rejected', true,
522
- rejected({ '#': 'struct', x: 'i3z:3' as any }))
523
- t.check('array with #pack rejected(#pack 仅 struct/union)', true,
524
- rejected({ '#': 'struct', x: { '#': 'array', element: 'u8', length: 2, '#pack': 4 } as any }))
525
- }
526
-
527
- t.section('# declaration validation')
528
- {
529
- const rejected = (s: C_Struct): boolean => {
530
- try { computeStructLayout(s); return false } catch { return true }
531
- }
532
- // '#' 必填:裸嵌套 map(漏写 '#')不再默认 struct,直接报错
533
- t.check('bare nested map rejected(漏写 #)', true,
534
- rejected({ '#': 'struct', x: { mask: 'u32' } } as any))
535
- t.check('unknown # tag rejected', true,
536
- rejected({ '#': 'struct', x: { '#': 'oops' } } as any))
537
- t.check('void token rejected(无大小)', true,
538
- rejected({ '#': 'struct', x: 'void' } as any))
539
- t.check('bare ptr token rejected', true,
540
- rejected({ '#': 'struct', x: 'ptr' } as any))
541
- }
542
- }
543
- }
@@ -1,103 +0,0 @@
1
- import { closure, WCHAR } from '../lib/ffi/bind.js';
2
- import { NULL, PtrArrayBuffer } from '../lib/ffi/ctype.js';
3
- import { GetDesktopWindow, IsWindow, IsWindowVisible, GetSystemMetrics, GetClientRect, GetWindowTextLength, GetClassName, FindWindow, SetTimer, KillTimer, GetKeyState, LoadCursor, SetCursor, GetMenu, DestroyMenu, GetForegroundWindow, GetDC, ReleaseDC, EnumWindows, FindWindowEx, } from '../lib/windows/user32.js';
4
- import { RECT } from '../lib/windows/structs.js';
5
- export const suite = {
6
- run(t) {
7
- t.section('user32: 窗口 / 度量');
8
- const desk = GetDesktopWindow();
9
- t.checkTrue('GetDesktopWindow 非 0', desk !== 0);
10
- if (desk === 0)
11
- return;
12
- t.check('IsWindow(desktop)', 1, IsWindow(desk));
13
- t.check('IsWindowVisible(desktop)', 1, IsWindowVisible(desk));
14
- t.checkTrue('GetSystemMetrics(SM_CXSCREEN) > 0', GetSystemMetrics(0) > 0);
15
- t.checkTrue('GetForegroundWindow 形态 number', typeof GetForegroundWindow() === 'number');
16
- t.section('user32: RECT 出参(encode → call → decode 字段直读)');
17
- const rectOut = RECT.alloc();
18
- t.check('GetClientRect(desktop)', 1, GetClientRect(desk, rectOut.ptr));
19
- const rect = RECT.decode(rectOut);
20
- t.check('客户区宽 = SM_CXSCREEN', GetSystemMetrics(0), rect.right - rect.left);
21
- t.check('客户区高 = SM_CYSCREEN', GetSystemMetrics(1), rect.bottom - rect.top);
22
- t.section('user32: 字符串出参(宽字符 buffer 读回)');
23
- t.checkTrue('GetWindowTextLength ≥ 0', GetWindowTextLength(desk) >= 0);
24
- const cls = new PtrArrayBuffer(64);
25
- const n = GetClassName(desk, cls, 32);
26
- t.checkTrue('GetClassName 写入长度 > 0', n > 0);
27
- const cdv = new DataView(cls);
28
- let clsName = '';
29
- for (let i = 0; i < n; i++)
30
- clsName += String.fromCharCode(cdv.getUint16(i * 2, true));
31
- t.checkTrue('类名非空', clsName.length > 0);
32
- // JSDoc 文档化流(L4):WCHAR.alloc(n) 当 out buffer、WCHAR.decode(buf) 读回
33
- const clsH = WCHAR.alloc(32);
34
- t.checkTrue('WCHAR.alloc out 写入长度 > 0', GetClassName(desk, clsH, 32) > 0);
35
- t.check('WCHAR.decode(buf) 读回类名', clsName, WCHAR.decode(clsH));
36
- t.section('user32: FindWindow(<WCHAR>ptr 的 NULL 与 string 双形态)');
37
- const fw = FindWindow(NULL, NULL);
38
- t.checkTrue('FindWindow(NULL, NULL) 形态 number(NULL 实参不 throw)', typeof fw === 'number');
39
- t.check('FindWindow(不存在类名, NULL)', 0, FindWindow('qw_no_such_class_xyz', NULL));
40
- t.section('user32: 计时器');
41
- const tid = SetTimer(NULL, 0, 50, NULL);
42
- t.checkTrue('SetTimer 返回非 0 id', tid !== 0);
43
- if (tid !== 0)
44
- t.checkTrue('KillTimer 成功', KillTimer(NULL, tid) !== 0);
45
- t.section('user32: 输入 / 光标 / 菜单');
46
- t.checkTrue('GetKeyState 返回整数', Number.isInteger(GetKeyState(0x1b)));
47
- t.checkTrue('LoadCursor(NULL, IDC_ARROW=32512) 非 0', LoadCursor(NULL, 32512) !== 0);
48
- // 品牌流动(正例):LoadCursor 返回 MaybePtr<'HCURSOR'> 直接喂 SetCursor 形参(先收窄空位)
49
- const arrow = LoadCursor(NULL, 32512);
50
- const prevCur = arrow !== 0 ? SetCursor(arrow) : null;
51
- t.checkTrue('SetCursor(LoadCursor(...)) 返回句柄形态', arrow !== 0 && (prevCur === null || typeof prevCur === 'number'));
52
- t.check('GetMenu(desktop) 无菜单 → 0', 0, GetMenu(desk));
53
- t.section('user32: GetDC / ReleaseDC 配对');
54
- const hdc = GetDC(NULL);
55
- t.checkTrue('GetDC(NULL) 非 0', hdc !== 0);
56
- t.checkTrue('ReleaseDC(NULL, hdc) 成功', hdc !== 0 && ReleaseDC(NULL, hdc) !== 0);
57
- // 仅类型层反例(不执行):异种句柄互传、<>ptr 误传品牌位都应编译不过
58
- const _typeOnly = () => {
59
- // @ts-expect-error Ptr<>(number)不是 Ptr<'HDC'>,误传桌面句柄应编译不过
60
- ReleaseDC(NULL, GetDesktopWindow());
61
- // @ts-expect-error Ptr<'HWND'> 不是 Ptr<'HCURSOR'>,窗口句柄喂 SetCursor 应编译不过
62
- SetCursor(GetDesktopWindow());
63
- // @ts-expect-error Ptr<'HWND'> 不是 Ptr<'HMENU'>,窗口句柄喂 DestroyMenu 应编译不过
64
- DestroyMenu(GetDesktopWindow());
65
- // @ts-expect-error Ptr<'HMENU'> 不是 Ptr<'HWND'>,菜单句柄喂 IsWindow 应编译不过
66
- IsWindow(GetMenu(desk));
67
- // @ts-expect-error 出参位未注册 encoder:<RECT>ptr 只收 MaybePtr<'RECT'>(NULL|Ptr),裸 ArrayBuffer 编译不过
68
- GetClientRect(desk, new ArrayBuffer(16));
69
- // @ts-expect-error 裸 number 缺 Ptr<'RECT'> 品牌
70
- GetClientRect(desk, 123);
71
- // 正例:同种品牌流动(GetMenu → DestroyMenu)通过——上面反例的成立依赖这层区分
72
- const m = GetMenu(desk);
73
- if (m !== 0)
74
- DestroyMenu(m);
75
- };
76
- void _typeOnly;
77
- t.section('user32: EnumWindows(<>ptr 回调端到端)');
78
- let count = 0;
79
- const enumClos = closure('<>ptr <>ptr -> i32', () => { count++; return 1; });
80
- t.check('EnumWindows 返回 1(枚举完成)', 1, EnumWindows(enumClos.ptr, NULL));
81
- t.checkTrue('回调计数 ≥ 1', count >= 1);
82
- enumClos.dispose();
83
- // EnumWindows(快照)与 FindWindowEx(NULL, prev) 链式遍历应得到同一组顶层窗口(实测两者顺序不同,
84
- // 所以只比集合,不比顺序)。
85
- // 回调只收集:回调内抛异常会被吞成返回 0(枚举静默提前结束),所以不在回调里做别的。
86
- t.section('user32: EnumWindows ≡ FindWindowEx 链遍历(顶层窗口集合)');
87
- const viaEnum = [];
88
- const collect = closure('<HWND>ptr <>ptr -> i32', (h) => { viaEnum.push(h); return 1; });
89
- try {
90
- EnumWindows(collect.ptr, NULL);
91
- }
92
- finally {
93
- collect.dispose();
94
- }
95
- const viaChain = [];
96
- for (let h = FindWindowEx(NULL, NULL, NULL, NULL); h && viaChain.length < 100000; h = FindWindowEx(NULL, h, NULL, NULL))
97
- viaChain.push(h);
98
- t.checkTrue('两种遍历都非空', viaEnum.length > 0 && viaChain.length > 0);
99
- t.check('窗口数量一致', viaEnum.length, viaChain.length);
100
- const sorted = (a) => a.slice().sort((x, y) => x - y).join(',');
101
- t.check('窗口集合一致(忽略顺序)', sorted(viaEnum), sorted(viaChain));
102
- },
103
- };