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,495 +0,0 @@
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
- };