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,495 @@
1
+ import { computeStructLayout, computeArray } from '../lib/ffi/struct.js';
2
+ export const suite = {
3
+ name: 'ffi-struct-layout',
4
+ run: (t) => {
5
+ // === 基础布局 ===
6
+ t.section('Basic struct');
7
+ {
8
+ const s = {
9
+ '#': 'struct',
10
+ a: 'u32',
11
+ b: 'u32',
12
+ c: 'u32',
13
+ };
14
+ const r = computeStructLayout(s);
15
+ t.check('size', 12, r.size);
16
+ t.check('align', 4, r.maxEffectiveAlign);
17
+ t.check('offset a', 0, r.fields[0].offset);
18
+ t.check('offset b', 4, r.fields[1].offset);
19
+ t.check('offset c', 8, r.fields[2].offset);
20
+ }
21
+ t.section('Padding');
22
+ {
23
+ const s = {
24
+ '#': 'struct',
25
+ a: 'u8',
26
+ b: 'u32',
27
+ c: 'u8',
28
+ };
29
+ const r = computeStructLayout(s);
30
+ t.check('size', 12, r.size); // 1 + 3pad + 4 + 1 + 3pad
31
+ t.check('offset a', 0, r.fields[0].offset);
32
+ t.check('offset b', 4, r.fields[1].offset); // aligned to 4
33
+ t.check('offset c', 8, r.fields[2].offset);
34
+ }
35
+ // === 嵌套 struct ===
36
+ t.section('Nested struct');
37
+ {
38
+ const inner = {
39
+ '#': 'struct',
40
+ x: 'u32',
41
+ y: 'u32',
42
+ };
43
+ const s = {
44
+ '#': 'struct',
45
+ a: 'u8',
46
+ b: inner,
47
+ };
48
+ const r = computeStructLayout(s);
49
+ t.check('size', 12, r.size); // 1 + 3pad + 8
50
+ t.check('offset a', 0, r.fields[0].offset);
51
+ t.check('offset b', 4, r.fields[1].offset); // b 自身偏移
52
+ const b = r.fields[1];
53
+ t.checkTrue('b 是 struct 成员', b.type.tag === 'struct');
54
+ if (b.type.tag === 'struct') {
55
+ t.check('b.x (相对子起点)', 0, b.type.fields[0].offset);
56
+ t.check('b.y (相对子起点)', 4, b.type.fields[1].offset);
57
+ }
58
+ }
59
+ // === Union ===
60
+ t.section('Union');
61
+ {
62
+ const u = {
63
+ '#': 'union',
64
+ a: 'u8',
65
+ b: 'u32',
66
+ c: 'u64',
67
+ };
68
+ const r = computeStructLayout(u);
69
+ t.check('size', 8, r.size); // max member size
70
+ t.check('align', 8, r.maxEffectiveAlign); // max member align
71
+ t.check('offset a', 0, r.fields[0].offset);
72
+ t.check('offset b', 0, r.fields[1].offset);
73
+ t.check('offset c', 0, r.fields[2].offset);
74
+ }
75
+ t.section('Union in struct');
76
+ {
77
+ const u = {
78
+ '#': 'union',
79
+ a: 'u8',
80
+ b: 'u32',
81
+ };
82
+ const s = {
83
+ '#': 'struct',
84
+ a: 'u8',
85
+ u: u,
86
+ c: 'u8',
87
+ };
88
+ const r = computeStructLayout(s);
89
+ t.check('size', 12, r.size); // 1 + 3pad + 4 + 1 + 3pad
90
+ t.check('offset a', 0, r.fields[0].offset);
91
+ t.check('offset u', 4, r.fields[1].offset);
92
+ const uf = r.fields[1];
93
+ t.checkTrue('u 是 union 成员', uf.type.tag === 'union');
94
+ if (uf.type.tag === 'union') {
95
+ t.check('u.a', 0, uf.type.fields[0].offset); // union member 同偏移
96
+ t.check('u.b', 0, uf.type.fields[1].offset);
97
+ }
98
+ t.check('offset c', 8, r.fields[2].offset);
99
+ }
100
+ // === 匿名成员提升 ===
101
+ t.section('Anonymous struct promotion');
102
+ {
103
+ const inner = {
104
+ '#': 'struct',
105
+ x: 'u32',
106
+ y: 'u32',
107
+ };
108
+ const s = {
109
+ '#': 'struct',
110
+ a: 'u8',
111
+ $anon1: inner, // 匿名:字段被提升
112
+ };
113
+ const r = computeStructLayout(s);
114
+ t.check('size', 12, r.size); // 1 + 3pad + 8
115
+ t.check('提升后字段数', 3, r.fields.length);
116
+ t.check('offset a', 0, r.fields[0].offset);
117
+ t.check('offset x (提升)', 4, r.fields[1].offset);
118
+ t.check('offset y (提升)', 8, r.fields[2].offset);
119
+ }
120
+ // === Array ===
121
+ t.section('Array of basic');
122
+ {
123
+ const arr = { '#': 'array', element: 'u32', length: 4 };
124
+ const r = computeArray(arr);
125
+ t.check('size', 16, r.size);
126
+ t.check('align', 4, r.align);
127
+ }
128
+ t.section('Array of struct');
129
+ {
130
+ const inner = {
131
+ '#': 'struct',
132
+ x: 'u32',
133
+ y: 'u32',
134
+ };
135
+ const arr = { '#': 'array', element: inner, length: 3 };
136
+ const r = computeArray(arr);
137
+ t.check('size', 24, r.size); // 3 * 8
138
+ t.check('align', 4, r.align);
139
+ }
140
+ t.section('Array in struct(糖写法)');
141
+ {
142
+ const s = {
143
+ '#': 'struct',
144
+ a: 'u8',
145
+ arr: 'u32[4]', // 数组糖(元素是 token 时的简写)
146
+ };
147
+ const r = computeStructLayout(s);
148
+ t.check('size', 20, r.size); // 1 + 3pad + 16
149
+ t.check('offset a', 0, r.fields[0].offset);
150
+ t.check('offset arr', 4, r.fields[1].offset);
151
+ }
152
+ t.section('Array in struct(# 形式 + 聚合元素)');
153
+ {
154
+ const inner = { '#': 'struct', x: 'u16', y: 'u16' };
155
+ const s = {
156
+ '#': 'struct',
157
+ a: 'u8',
158
+ arr: { '#': 'array', element: inner, length: 3 },
159
+ };
160
+ const r = computeStructLayout(s);
161
+ t.check('size == 14 (a@0 + 1pad + 3*4,align 取 inner 的 2)', 14, r.size);
162
+ t.check('offset arr == 2 (inner align 2)', 2, r.fields[1].offset);
163
+ }
164
+ // === String ===
165
+ // === String(字符串糖:布局 unit[length] + 解释 @encoding 正交) ===
166
+ t.section('CString sugar(u16[10]@utf-16le)');
167
+ {
168
+ const r = computeStructLayout({ '#': 'struct', name: 'u16[10]@utf-16le' });
169
+ t.check('size == 20 (typesize × length)', 20, r.size);
170
+ t.check('align == 2 (u16 槽)', 2, r.maxEffectiveAlign);
171
+ t.check('offset', 0, r.fields[0].offset);
172
+ }
173
+ t.section('CString sugar: 布局与解释正交(size 只看 unit×length)');
174
+ {
175
+ const r = computeStructLayout({ '#': 'struct', a: 'u8', s: 'u8[8]@utf-16le' });
176
+ t.check('错配 u8[8]@utf-16le size == 9', 9, r.size); // 1 + 8,encoding 不参与布局
177
+ t.check('align == 1 (u8 槽)', 1, r.maxEffectiveAlign);
178
+ t.check('offset s == 1 (无 padding)', 1, r.fields[1].offset);
179
+ const r2 = computeStructLayout({ '#': 'struct', s: 'u16[4]@utf-8' });
180
+ t.check('错配 u16[4]@utf-8 size == 8', 8, r2.size); // 2 × 4
181
+ t.check('align == 2 (u16 槽)', 2, r2.maxEffectiveAlign);
182
+ }
183
+ t.section('CString sugar validation');
184
+ {
185
+ const rejected = (s) => {
186
+ try {
187
+ computeStructLayout(s);
188
+ return false;
189
+ }
190
+ catch {
191
+ return true;
192
+ }
193
+ };
194
+ t.check('length 0 rejected', true, rejected({ '#': 'struct', x: 'u8[0]@utf-8' }));
195
+ t.check('unit 非 u8/u16 rejected(落 token 分支抛)', true, rejected({ '#': 'struct', x: 'u32[4]@utf-8' }));
196
+ t.check('unknown encoding rejected', true, rejected({ '#': 'struct', x: 'u16[4]@utf-16be' }));
197
+ t.check('旧写法 {\'#\':\'string\'} rejected(词汇已删)', true, rejected({ '#': 'struct', x: { '#': 'string', unit: 'u16', length: 4, encoding: 'utf-16le' } }));
198
+ }
199
+ // === Pack ===
200
+ t.section('Pack(1)');
201
+ {
202
+ const s = {
203
+ '#': 'struct', '#pack': 1,
204
+ a: 'u8',
205
+ b: 'u32',
206
+ c: 'u8',
207
+ };
208
+ const r = computeStructLayout(s);
209
+ t.check('size', 6, r.size); // 1 + 4 + 1, no padding
210
+ t.check('offset a', 0, r.fields[0].offset);
211
+ t.check('offset b', 1, r.fields[1].offset); // no alignment
212
+ t.check('offset c', 5, r.fields[2].offset);
213
+ }
214
+ t.section('Pack(4)');
215
+ {
216
+ const s = {
217
+ '#': 'struct', '#pack': 4,
218
+ a: 'u8',
219
+ b: 'u64', // natural align 8, capped to 4
220
+ };
221
+ const r = computeStructLayout(s);
222
+ t.check('offset a', 0, r.fields[0].offset);
223
+ t.check('offset b', 4, r.fields[1].offset); // aligned to 4, not 8
224
+ }
225
+ // === Alignas(键尾 @N) ===
226
+ t.section('Alignas');
227
+ {
228
+ const s = {
229
+ '#': 'struct',
230
+ a: 'u8',
231
+ 'b@8': 'u32',
232
+ };
233
+ const r = computeStructLayout(s);
234
+ t.check('offset a', 0, r.fields[0].offset);
235
+ t.check('offset b', 8, r.fields[1].offset); // aligned to 8
236
+ t.check('字段名剥离 @N', 'b', r.fields[1].name);
237
+ }
238
+ t.section('Alignas(2) < natural(4)');
239
+ {
240
+ const s = {
241
+ '#': 'struct',
242
+ a: 'u8',
243
+ 'b@2': 'u32', // natural is 4, alignas is 2
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); // natural align wins (4 > 2)
248
+ }
249
+ // === Pack + Alignas interaction ===
250
+ t.section('Pack(1) + Alignas(8)');
251
+ {
252
+ const s = {
253
+ '#': 'struct', '#pack': 1,
254
+ a: 'u8',
255
+ 'b@8': 'u32', // alignas wins over pack
256
+ };
257
+ const r = computeStructLayout(s);
258
+ t.check('offset a', 0, r.fields[0].offset);
259
+ t.check('offset b', 8, r.fields[1].offset); // alignas(8) > pack(1)
260
+ }
261
+ t.section('Pack(4) + Alignas(2)');
262
+ {
263
+ const s = {
264
+ '#': 'struct', '#pack': 4,
265
+ a: 'u8',
266
+ 'b@2': 'u64', // natural 8, pack 4, alignas 2
267
+ };
268
+ const r = computeStructLayout(s);
269
+ t.check('offset a', 0, r.fields[0].offset);
270
+ t.check('offset b', 4, r.fields[1].offset); // min(pack=4, natural=8)=4, max(4, alignas=2)=4
271
+ }
272
+ t.section('Pack(4) + Alignas(16)');
273
+ {
274
+ const s = {
275
+ '#': 'struct', '#pack': 4,
276
+ a: 'u8',
277
+ 'b@16': 'u32', // alignas(16) > pack(4)
278
+ };
279
+ const r = computeStructLayout(s);
280
+ t.check('offset a', 0, r.fields[0].offset);
281
+ t.check('offset b', 16, r.fields[1].offset); // alignas wins
282
+ }
283
+ // === 位域 ===
284
+ // ground truth: mingw x86_64 交叉编译 sizeof/offsetof(含 windows.h 自带的
285
+ // DCB/COMSTAT)。i686 同结果(DCB/COMSTAT 无指针字段,32/64 位布局一致)。
286
+ const bitOf = (fields, n) => {
287
+ const f = fields.find(x => x.name === n);
288
+ t.checkTrue(`${n} is bitfield`, f.type.tag === 'bitfield');
289
+ return { offset: f.offset, bit: f.type.bit };
290
+ };
291
+ const offOf = (fields, n) => fields.find(x => x.name === n).offset;
292
+ t.section('Bitfield: 同单元打包 + 溢出开新单元(MS 文档例子)');
293
+ {
294
+ // 9+7=16 塞满第一单元,30 放不下开第二,18 开第三。sizeof 应为 12。
295
+ const s = {
296
+ '#': 'struct',
297
+ first: 'u32:9',
298
+ second: 'u32:7',
299
+ may_straddle: 'u32:30',
300
+ last: 'u32:18',
301
+ };
302
+ const r = computeStructLayout(s);
303
+ t.check('sizeof == 12', 12, r.size);
304
+ t.check('first.offset == 0', 0, bitOf(r.fields, 'first').offset);
305
+ t.check('second.offset == 0 (共单元)', 0, bitOf(r.fields, 'second').offset);
306
+ t.check('second.bit == 9', 9, bitOf(r.fields, 'second').bit);
307
+ t.check('may_straddle.offset == 4 (新单元)', 4, bitOf(r.fields, 'may_straddle').offset);
308
+ t.check('may_straddle.bit == 0', 0, bitOf(r.fields, 'may_straddle').bit);
309
+ t.check('last.offset == 8 (再新单元)', 8, bitOf(r.fields, 'last').offset);
310
+ }
311
+ t.section('Bitfield: 精确填充算放得下');
312
+ {
313
+ // 15+17=32 恰好塞满一个 32 位单元 → 不开新单元(mingw 实测 sizeof=8, w@4)
314
+ const s = {
315
+ '#': 'struct',
316
+ a: 'u32:15',
317
+ b: 'u32:17',
318
+ w: 'u16',
319
+ };
320
+ const r = computeStructLayout(s);
321
+ t.check('sizeof == 8 (单元 0-3 + w@4, pad 到 8)', 8, r.size);
322
+ t.check('b.offset == 0 (共单元)', 0, bitOf(r.fields, 'b').offset);
323
+ t.check('b.bit == 15', 15, bitOf(r.fields, 'b').bit);
324
+ t.check('w.offset == 4 (紧跟单元)', 4, offOf(r.fields, 'w'));
325
+ }
326
+ t.section('Bitfield: 非位域成员打断组 → 游标跳完整单元边界');
327
+ {
328
+ // u32 单元用 12 位 = 2 字节,但 char 落在单元边界 4(不是按字节截断的 2)
329
+ const s = {
330
+ '#': 'struct',
331
+ a: 'u32:12',
332
+ z: 'u8',
333
+ };
334
+ const r = computeStructLayout(s);
335
+ t.check('sizeof == 8 (char@4, pad 到 8)', 8, r.size);
336
+ t.check('z.offset == 4 (单元边界)', 4, offOf(r.fields, 'z'));
337
+ // u16 单元用 12 位 = 2 字节 = 完整单元,char 紧随其后
338
+ const s2 = {
339
+ '#': 'struct',
340
+ a: 'u16:12',
341
+ z: 'u8',
342
+ };
343
+ const r2 = computeStructLayout(s2);
344
+ t.check('u16 单元 sizeof == 4', 4, r2.size);
345
+ t.check('z.offset == 2 (u16 单元边界)', 2, offOf(r2.fields, 'z'));
346
+ }
347
+ t.section('Bitfield: 同宽不分签别,共单元');
348
+ {
349
+ const s = {
350
+ '#': 'struct',
351
+ a: 'u32:8',
352
+ b: 'i32:8',
353
+ };
354
+ const r = computeStructLayout(s);
355
+ t.check('sizeof == 4 (有/无符号共单元)', 4, r.size);
356
+ t.check('b.bit == 8', 8, bitOf(r.fields, 'b').bit);
357
+ }
358
+ t.section('Bitfield: DCB(mingw windows.h sizeof=28)');
359
+ {
360
+ const s = {
361
+ '#': 'struct',
362
+ DCBlength: 'u32',
363
+ BaudRate: 'u32',
364
+ fBinary: 'u32:1',
365
+ fParity: 'u32:1',
366
+ fOutxCtsFlow: 'u32:1',
367
+ fOutxDsrFlow: 'u32:1',
368
+ fDtrControl: 'u32:2',
369
+ fDsrSensitivity: 'u32:1',
370
+ fTXContinueOnXoff: 'u32:1',
371
+ fOutX: 'u32:1',
372
+ fInX: 'u32:1',
373
+ fErrorChar: 'u32:1',
374
+ fNull: 'u32:1',
375
+ fRtsControl: 'u32:2',
376
+ fAbortOnError: 'u32:1',
377
+ fDummy2: 'u32:17',
378
+ wReserved: 'u16',
379
+ XonLim: 'u16',
380
+ XoffLim: 'u16',
381
+ ByteSize: 'u8',
382
+ Parity: 'u8',
383
+ StopBits: 'u8',
384
+ XonChar: 'i8',
385
+ XoffChar: 'i8',
386
+ ErrorChar: 'i8',
387
+ EofChar: 'i8',
388
+ EvtChar: 'i8',
389
+ wReserved1: 'u16',
390
+ };
391
+ const r = computeStructLayout(s);
392
+ t.check('DCB.size == 28', 28, r.size);
393
+ t.check('fBinary.offset == 8 (单元基址)', 8, bitOf(r.fields, 'fBinary').offset);
394
+ // 前 13 个位域 15 位 + fDummy2:17 = 32 位,恰好塞满一个 4 字节单元
395
+ t.check('fDummy2.offset == 8 (共单元,未溢出)', 8, bitOf(r.fields, 'fDummy2').offset);
396
+ t.check('fDummy2.bit == 15', 15, bitOf(r.fields, 'fDummy2').bit);
397
+ t.check('wReserved.offset == 12', 12, offOf(r.fields, 'wReserved'));
398
+ t.check('XonLim.offset == 14', 14, offOf(r.fields, 'XonLim'));
399
+ t.check('ByteSize.offset == 18', 18, offOf(r.fields, 'ByteSize'));
400
+ t.check('EofChar.offset == 24', 24, offOf(r.fields, 'EofChar'));
401
+ t.check('wReserved1.offset == 26', 26, offOf(r.fields, 'wReserved1'));
402
+ }
403
+ t.section('Bitfield: COMSTAT(mingw windows.h sizeof=12)');
404
+ {
405
+ const s = {
406
+ '#': 'struct',
407
+ fCtsHold: 'u32:1',
408
+ fDsrHold: 'u32:1',
409
+ fRlsdHold: 'u32:1',
410
+ fXoffHold: 'u32:1',
411
+ fXoffSent: 'u32:1',
412
+ fEof: 'u32:1',
413
+ fTxim: 'u32:1',
414
+ fReserved: 'u32:25',
415
+ cbInQue: 'u32',
416
+ cbOutQue: 'u32',
417
+ };
418
+ const r = computeStructLayout(s);
419
+ t.check('COMSTAT.size == 12', 12, r.size);
420
+ t.check('fReserved.bit == 7', 7, bitOf(r.fields, 'fReserved').bit);
421
+ t.check('cbInQue.offset == 4', 4, offOf(r.fields, 'cbInQue'));
422
+ t.check('cbOutQue.offset == 8', 8, offOf(r.fields, 'cbOutQue'));
423
+ }
424
+ t.section('Bitfield: union 位域完全别名(mingw 实测:每位域成员独立 offset 0/bit 0)');
425
+ {
426
+ const u = {
427
+ '#': 'union',
428
+ a: 'u32:1',
429
+ b: 'u32:1',
430
+ };
431
+ const r = computeStructLayout(u);
432
+ t.check('sizeof == 4', 4, r.size);
433
+ t.check('a.offset == 0', 0, bitOf(r.fields, 'a').offset);
434
+ t.check('a.bit == 0', 0, bitOf(r.fields, 'a').bit);
435
+ t.check('b.offset == 0 (同址别名)', 0, bitOf(r.fields, 'b').offset);
436
+ t.check('b.bit == 0 (同位别名,实测 a=1→b=1)', 0, bitOf(r.fields, 'b').bit);
437
+ }
438
+ t.section('Bitfield: union 溢出/混合形状不越界(regression:曾推进游标致 offset 越出 size)');
439
+ {
440
+ // 溢出:struct 里 30+4>32 开新单元;union 每成员独立回 offset 0
441
+ const u2 = { '#': 'union', a: 'u32:30', b: 'u32:4' };
442
+ const r2 = computeStructLayout(u2);
443
+ t.check('overflow sizeof == 4', 4, r2.size);
444
+ t.check('overflow a.offset == 0', 0, bitOf(r2.fields, 'a').offset);
445
+ t.check('overflow b.offset == 0(回 offset 0 重叠,曾为 4)', 0, bitOf(r2.fields, 'b').offset);
446
+ t.check('overflow b.bit == 0', 0, bitOf(r2.fields, 'b').bit);
447
+ // 位域后跟普通成员:flushUnit 不得推进 union 游标
448
+ const u3 = { '#': 'union', a: 'u32:1', b: 'u8' };
449
+ const r3 = computeStructLayout(u3);
450
+ t.check('mixed sizeof == 4', 4, r3.size);
451
+ t.check('mixed a.offset == 0', 0, bitOf(r3.fields, 'a').offset);
452
+ t.check('mixed b.offset == 0(曾错位到 4 → 越出 size 4)', 0, offOf(r3.fields, 'b'));
453
+ }
454
+ t.section('Bitfield validation');
455
+ {
456
+ const rejected = (s) => {
457
+ try {
458
+ computeStructLayout(s);
459
+ return false;
460
+ }
461
+ catch {
462
+ return true;
463
+ }
464
+ };
465
+ t.check('width 0 rejected (v1 不支持 :0 填充)', true, rejected({ '#': 'struct', x: 'u32:0' }));
466
+ t.check('width > unit bits rejected', true, rejected({ '#': 'struct', x: 'u32:33' }));
467
+ t.check('width > u8 bits rejected', true, rejected({ '#': 'struct', x: 'u8:9' }));
468
+ t.check('negative width rejected(非 \\d+ → 落 token 分支抛)', true, rejected({ '#': 'struct', x: 'u32:-1' }));
469
+ t.check('non-integer width rejected', true, rejected({ '#': 'struct', x: 'u32:2.5' }));
470
+ t.check('array of bitfields rejected(# 形式)', true, rejected({ '#': 'struct', x: { '#': 'array', element: 'u32:1', length: 2 } }));
471
+ t.check('array of bitfields rejected(数组糖,类型层拒绝故 as any)', true, rejected({ '#': 'struct', x: 'u32:1[2]' }));
472
+ t.check('anonymous bitfield rejected', true, rejected({ '#': 'struct', $anon1: 'u32:1' }));
473
+ t.check('f32 bitfield unit rejected(归一后非整数档)', true, rejected({ '#': 'struct', x: 'f32:3' }));
474
+ t.check('unknown bitfield unit rejected', true, rejected({ '#': 'struct', x: 'i3z:3' }));
475
+ t.check('array with #pack rejected(#pack 仅 struct/union)', true, rejected({ '#': 'struct', x: { '#': 'array', element: 'u8', length: 2, '#pack': 4 } }));
476
+ }
477
+ t.section('# declaration validation');
478
+ {
479
+ const rejected = (s) => {
480
+ try {
481
+ computeStructLayout(s);
482
+ return false;
483
+ }
484
+ catch {
485
+ return true;
486
+ }
487
+ };
488
+ // '#' 必填:裸嵌套 map(漏写 '#')不再默认 struct,直接报错
489
+ t.check('bare nested map rejected(漏写 #)', true, rejected({ '#': 'struct', x: { mask: 'u32' } }));
490
+ t.check('unknown # tag rejected', true, rejected({ '#': 'struct', x: { '#': 'oops' } }));
491
+ t.check('void token rejected(无大小)', true, rejected({ '#': 'struct', x: 'void' }));
492
+ t.check('bare ptr token rejected', true, rejected({ '#': 'struct', x: 'ptr' }));
493
+ }
494
+ }
495
+ };