quickwin 1.3.0 → 2.0.0

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 (143) hide show
  1. package/README.en.md +1 -2
  2. package/README.md +19 -23
  3. package/examples/PdfCanvas.js +8 -37
  4. package/examples/PdfCanvas.tsx +11 -41
  5. package/examples/exec_server.js +20054 -1423
  6. package/examples/exec_server.tsx +489 -0
  7. package/examples/exec_server_worker.js +383 -33
  8. package/examples/exec_server_worker.ts +408 -30
  9. package/examples/pdf_preview2.js +25 -82
  10. package/examples/pdf_preview2.ts +27 -84
  11. package/examples/pdf_viewer.js +1318 -1178
  12. package/examples/pdf_viewer.tsx +16 -67
  13. package/examples/setres.js +22 -47
  14. package/examples/setres.ts +23 -57
  15. package/examples/test_gallery.js +1313 -1074
  16. package/examples/test_react_lazy.js +1284 -1056
  17. package/examples/test_react_listbox.js +1283 -1055
  18. package/examples/test_root_window.js +1279 -1051
  19. package/examples/test_tab.js +1284 -1056
  20. package/lib/abort.js +70 -0
  21. package/lib/abort.ts +69 -0
  22. package/lib/certs.js +214 -0
  23. package/lib/certs.ts +224 -0
  24. package/lib/exec-log.js +15 -0
  25. package/lib/exec-log.ts +30 -0
  26. package/lib/fetch.js +37 -4
  27. package/lib/fetch.ts +35 -4
  28. package/lib/ffi/bind.js +352 -0
  29. package/lib/ffi/bind.ts +442 -0
  30. package/lib/ffi/ctype.js +168 -0
  31. package/lib/ffi/ctype.ts +331 -0
  32. package/lib/ffi/index.js +6 -0
  33. package/lib/ffi/index.ts +6 -0
  34. package/lib/ffi/struct.js +412 -0
  35. package/lib/ffi/struct.ts +664 -0
  36. package/lib/http-common.js +43 -5
  37. package/lib/http-common.ts +48 -6
  38. package/lib/http-server.js +187 -29
  39. package/lib/http-server.ts +177 -26
  40. package/lib/react-qw/components/DateTimePicker.js +18 -37
  41. package/lib/react-qw/components/Input.js +3 -10
  42. package/lib/react-qw/components/Link.js +6 -19
  43. package/lib/react-qw/components/ListView.js +49 -154
  44. package/lib/react-qw/components/PathPicker.js +28 -88
  45. package/lib/react-qw/components/ScrollView.js +15 -8
  46. package/lib/react-qw/components/Slider.js +2 -1
  47. package/lib/react-qw/components/Tab.js +6 -22
  48. package/lib/react-qw/components/Tooltip.js +10 -21
  49. package/lib/react-qw/components/TreeView.js +8 -31
  50. package/lib/react-qw/index.js +4 -2
  51. package/lib/react-qw/index.ts +4 -2
  52. package/lib/react-qw/nmhdr.js +3 -24
  53. package/lib/react-qw/nmhdr.ts +5 -30
  54. package/lib/react-qw/props.js +7 -2
  55. package/lib/react-qw/props.ts +7 -3
  56. package/lib/react-qw/reconciler.js +23 -13
  57. package/lib/react-qw/reconciler.ts +22 -13
  58. package/lib/text-measure.js +18 -23
  59. package/lib/text-measure.ts +20 -25
  60. package/lib/websocket.js +12 -2
  61. package/lib/websocket.ts +15 -2
  62. package/lib/windows/comctl32.js +18 -0
  63. package/lib/windows/comctl32.ts +20 -0
  64. package/lib/windows/comdlg32.js +7 -0
  65. package/lib/windows/comdlg32.ts +9 -0
  66. package/lib/windows/gdi32.js +90 -0
  67. package/lib/windows/gdi32.ts +97 -0
  68. package/lib/windows/kernel32.js +66 -0
  69. package/lib/windows/kernel32.ts +76 -0
  70. package/lib/windows/ntdll.js +8 -0
  71. package/lib/windows/ntdll.ts +10 -0
  72. package/lib/windows/ole32.js +7 -0
  73. package/lib/windows/ole32.ts +9 -0
  74. package/lib/windows/shell32.js +11 -0
  75. package/lib/windows/shell32.ts +14 -0
  76. package/lib/windows/structs.js +384 -0
  77. package/lib/windows/structs.ts +422 -0
  78. package/lib/windows/user32.js +469 -0
  79. package/lib/windows/user32.ts +502 -0
  80. package/package.json +1 -1
  81. package/quickwin.d.ts +22 -37
  82. package/quickwin_const.d.ts +1 -0
  83. package/qwin-nowasm-x86.exe +0 -0
  84. package/qwin-nowasm.exe +0 -0
  85. package/qwin-x86.exe +0 -0
  86. package/qwin.exe +0 -0
  87. package/test/bench_ffi_callraw.js +236 -0
  88. package/test/bench_ffi_callraw.ts +236 -0
  89. package/test/bench_ffi_overhead.js +139 -0
  90. package/test/bench_ffi_overhead.ts +141 -0
  91. package/test/bench_sendmessage.js +104 -0
  92. package/test/bench_sendmessage.ts +100 -0
  93. package/test/bench_sendmessage_nonnull.js +187 -0
  94. package/test/bench_sendmessage_nonnull.ts +188 -0
  95. package/test/run.js +12 -4
  96. package/test/run.ts +13 -5
  97. package/test/test_basic_fetch.js +8 -0
  98. package/test/test_basic_fetch.ts +9 -0
  99. package/test/test_ffi.js +25 -43
  100. package/test/test_ffi.ts +33 -52
  101. package/test/test_ffi_abi.js +235 -0
  102. package/test/test_ffi_abi.ts +218 -0
  103. package/test/test_ffi_bind.js +538 -8
  104. package/test/test_ffi_bind.ts +559 -8
  105. package/test/test_ffi_struct.js +632 -88
  106. package/test/test_ffi_struct.ts +666 -91
  107. package/test/test_ffi_struct_layout.js +495 -0
  108. package/test/test_ffi_struct_layout.ts +543 -0
  109. package/test/test_ffi_user32.js +103 -0
  110. package/test/test_ffi_user32.ts +113 -0
  111. package/test/test_ffi_windows.js +101 -0
  112. package/test/test_ffi_windows.ts +113 -0
  113. package/test/test_helper.js +8 -1
  114. package/test/test_helper.ts +9 -1
  115. package/test/test_http_import.js +18 -0
  116. package/test/test_http_import.ts +19 -0
  117. package/test/test_http_server.js +73 -1
  118. package/test/test_http_server.ts +75 -1
  119. package/test/test_ipv6.js +2 -2
  120. package/test/test_ipv6.ts +2 -2
  121. package/test/test_net_fetch.js +70 -8
  122. package/test/test_net_fetch.ts +69 -7
  123. package/test/test_net_websocket.js +1 -1
  124. package/test/test_net_websocket.ts +1 -1
  125. package/test/test_react_complex.js +1294 -1066
  126. package/test/test_react_counter.js +1284 -1056
  127. package/test/test_react_flex.js +1304 -1069
  128. package/test/test_react_hidden.js +1287 -1059
  129. package/test/test_react_listview_link.js +1284 -1056
  130. package/test/test_react_notify.js +18534 -0
  131. package/test/test_react_render.js +1284 -1056
  132. package/test/test_wolfssl_read.js +3 -2
  133. package/test/test_wolfssl_read.ts +3 -1
  134. package/test/test_worker_net.js +35 -17
  135. package/test/test_worker_net.ts +27 -15
  136. package/tsconfig.json +1 -1
  137. package/examples/exec_server.ts +0 -80
  138. package/lib/ffi-bind.js +0 -85
  139. package/lib/ffi-bind.ts +0 -170
  140. package/lib/ffi-struct.js +0 -224
  141. package/lib/ffi-struct.ts +0 -239
  142. package/lib/win-common-structs.js +0 -39
  143. package/lib/win-common-structs.ts +0 -41
@@ -0,0 +1,543 @@
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
+ }
@@ -0,0 +1,103 @@
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
+ };