quickwin 3.0.0 → 3.0.2

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 (253) hide show
  1. package/README.en.md +3 -0
  2. package/README.md +2 -0
  3. package/lib/abort.d.ts +21 -0
  4. package/lib/assert.d.ts +2 -0
  5. package/lib/bitmap.d.ts +7 -0
  6. package/lib/cache-utils.d.ts +6 -0
  7. package/lib/certs.d.ts +1 -0
  8. package/lib/dpi.d.ts +9 -0
  9. package/lib/dumpRects.d.ts +2 -0
  10. package/lib/exec-log.d.ts +23 -0
  11. package/lib/fetch.d.ts +4 -0
  12. package/lib/ffi/bind.d.ts +70 -0
  13. package/lib/ffi/ctype.d.ts +117 -0
  14. package/lib/ffi/index.d.ts +2 -0
  15. package/lib/ffi/struct.d.ts +174 -0
  16. package/lib/http-common.d.ts +143 -0
  17. package/lib/http-server.d.ts +59 -0
  18. package/lib/polyfill.d.ts +22 -0
  19. package/lib/react-qw/components/Button.d.ts +9 -0
  20. package/lib/react-qw/components/CheckBox.d.ts +11 -0
  21. package/lib/react-qw/components/ComboBox.d.ts +11 -0
  22. package/lib/react-qw/components/DateTimePicker.d.ts +15 -0
  23. package/lib/react-qw/components/Input.d.ts +15 -0
  24. package/lib/react-qw/components/Link.d.ts +12 -0
  25. package/lib/react-qw/components/ListBox.d.ts +13 -0
  26. package/lib/react-qw/components/ListView.d.ts +37 -0
  27. package/lib/react-qw/components/PathPicker.d.ts +23 -0
  28. package/lib/react-qw/components/ProgressBar.d.ts +9 -0
  29. package/lib/react-qw/components/RadioButton.d.ts +11 -0
  30. package/lib/react-qw/components/ScrollView.d.ts +9 -0
  31. package/lib/react-qw/components/Slider.d.ts +12 -0
  32. package/lib/react-qw/components/Tab.d.ts +14 -0
  33. package/lib/react-qw/components/Tooltip.d.ts +7 -0
  34. package/lib/react-qw/components/TreeView.d.ts +18 -0
  35. package/lib/react-qw/components/index.d.ts +16 -0
  36. package/lib/react-qw/index.d.ts +24 -0
  37. package/lib/react-qw/layout.d.ts +29 -0
  38. package/lib/react-qw/nmhdr.d.ts +1 -0
  39. package/lib/react-qw/props.d.ts +2 -0
  40. package/lib/react-qw/reconciler.d.ts +21 -0
  41. package/lib/stream.d.ts +20 -0
  42. package/lib/text-codec.d.ts +39 -0
  43. package/lib/text-measure.d.ts +14 -0
  44. package/lib/url.d.ts +54 -0
  45. package/lib/websocket.d.ts +73 -0
  46. package/lib/windows/{advapi32.ts → advapi32.d.ts} +6 -21
  47. package/lib/windows/comctl32.d.ts +14 -0
  48. package/lib/windows/comdlg32.d.ts +4 -0
  49. package/lib/windows/{gdi32.ts → gdi32.d.ts} +161 -98
  50. package/lib/windows/{kernel32.ts → kernel32.d.ts} +138 -115
  51. package/lib/windows/{msimg32.ts → msimg32.d.ts} +11 -14
  52. package/lib/windows/ntdll.d.ts +5 -0
  53. package/lib/windows/ole32.d.ts +3 -0
  54. package/lib/windows/{psapi.ts → psapi.d.ts} +9 -18
  55. package/lib/windows/{shell32.ts → shell32.d.ts} +25 -24
  56. package/lib/windows/structs.d.ts +3141 -0
  57. package/lib/windows/{user32.ts → user32.d.ts} +196 -193
  58. package/lib/windows/version.d.ts +27 -0
  59. package/lib/windows/{winspool.ts → winspool.d.ts} +18 -32
  60. package/package.json +15 -8
  61. package/qwin-nowasm-x86.exe +0 -0
  62. package/qwin-nowasm.exe +0 -0
  63. package/qwin-x86.exe +0 -0
  64. package/qwin.exe +0 -0
  65. package/examples/PdfCanvas.js +0 -38
  66. package/examples/PdfCanvas.tsx +0 -54
  67. package/examples/execd/main.js +0 -23379
  68. package/examples/execd/tray.js +0 -192
  69. package/examples/execd/worker.js +0 -122
  70. package/examples/pdf_preview2.js +0 -398
  71. package/examples/pdf_preview2.ts +0 -353
  72. package/examples/pdf_viewer.js +0 -22515
  73. package/examples/pdf_viewer.tsx +0 -228
  74. package/examples/test_gallery.js +0 -18692
  75. package/examples/test_gallery.tsx +0 -291
  76. package/examples/test_react_lazy.js +0 -18560
  77. package/examples/test_react_lazy.tsx +0 -65
  78. package/examples/test_react_listbox.js +0 -18562
  79. package/examples/test_react_listbox.tsx +0 -73
  80. package/examples/test_root_window.js +0 -18532
  81. package/examples/test_root_window.tsx +0 -41
  82. package/examples/test_tab.js +0 -18563
  83. package/examples/test_tab.tsx +0 -77
  84. package/examples/tray_demo.js +0 -86
  85. package/examples/tray_demo.tsx +0 -89
  86. package/lib/abort.ts +0 -69
  87. package/lib/assert.ts +0 -7
  88. package/lib/bitmap.ts +0 -39
  89. package/lib/cache-utils.ts +0 -40
  90. package/lib/certs.ts +0 -258
  91. package/lib/dpi.ts +0 -31
  92. package/lib/dumpRects.ts +0 -34
  93. package/lib/exec-log.ts +0 -30
  94. package/lib/fetch.ts +0 -589
  95. package/lib/ffi/bind.ts +0 -540
  96. package/lib/ffi/ctype.ts +0 -371
  97. package/lib/ffi/index.ts +0 -6
  98. package/lib/ffi/struct.ts +0 -780
  99. package/lib/http-common.ts +0 -441
  100. package/lib/http-server.ts +0 -604
  101. package/lib/polyfill.ts +0 -171
  102. package/lib/react-qw/components/index.ts +0 -17
  103. package/lib/react-qw/index.ts +0 -96
  104. package/lib/react-qw/layout.ts +0 -111
  105. package/lib/react-qw/nmhdr.ts +0 -7
  106. package/lib/react-qw/props.ts +0 -41
  107. package/lib/react-qw/reconciler.ts +0 -379
  108. package/lib/stream.ts +0 -22
  109. package/lib/text-codec.ts +0 -171
  110. package/lib/text-measure.ts +0 -44
  111. package/lib/url.ts +0 -201
  112. package/lib/websocket.ts +0 -600
  113. package/lib/windows/comctl32.ts +0 -20
  114. package/lib/windows/comdlg32.ts +0 -10
  115. package/lib/windows/ntdll.ts +0 -11
  116. package/lib/windows/ole32.ts +0 -9
  117. package/lib/windows/structs.ts +0 -807
  118. package/lib/windows/version.ts +0 -35
  119. package/test/add.wasm +0 -0
  120. package/test/bench_ffi_callraw.js +0 -235
  121. package/test/bench_ffi_callraw.ts +0 -235
  122. package/test/bench_ffi_overhead.js +0 -139
  123. package/test/bench_ffi_overhead.ts +0 -141
  124. package/test/bench_sendmessage.js +0 -104
  125. package/test/bench_sendmessage.ts +0 -100
  126. package/test/bench_sendmessage_nonnull.js +0 -187
  127. package/test/bench_sendmessage_nonnull.ts +0 -188
  128. package/test/complex.wasm +0 -0
  129. package/test/complex_imports.wasm +0 -0
  130. package/test/debug/http_server_e2e.js +0 -31
  131. package/test/debug/minwasm.js +0 -52
  132. package/test/debug/mupdf_debug.js +0 -86
  133. package/test/debug/wasm.js +0 -122
  134. package/test/debug/wasm_export_debug.js +0 -54
  135. package/test/debug/wasm_names.js +0 -36
  136. package/test/example.pdf +0 -0
  137. package/test/global_imports.wasm +0 -0
  138. package/test/import_func.wasm +0 -0
  139. package/test/imports.wasm +0 -0
  140. package/test/mupdf-cdn.js +0 -40
  141. package/test/mupdf-cdn.ts +0 -43
  142. package/test/run.js +0 -111
  143. package/test/run.ts +0 -116
  144. package/test/sjlj.wasm +0 -0
  145. package/test/test_basic.js +0 -7
  146. package/test/test_basic.ts +0 -9
  147. package/test/test_basic_fetch.js +0 -63
  148. package/test/test_basic_fetch.ts +0 -59
  149. package/test/test_brotli.js +0 -48
  150. package/test/test_brotli.ts +0 -52
  151. package/test/test_fetch_cache.js +0 -169
  152. package/test/test_fetch_cache.ts +0 -187
  153. package/test/test_ffi.js +0 -157
  154. package/test/test_ffi.ts +0 -170
  155. package/test/test_ffi_abi.js +0 -290
  156. package/test/test_ffi_abi.ts +0 -274
  157. package/test/test_ffi_array.js +0 -116
  158. package/test/test_ffi_array.ts +0 -141
  159. package/test/test_ffi_bind.js +0 -656
  160. package/test/test_ffi_bind.ts +0 -670
  161. package/test/test_ffi_struct.js +0 -779
  162. package/test/test_ffi_struct.ts +0 -828
  163. package/test/test_ffi_struct_layout.js +0 -495
  164. package/test/test_ffi_struct_layout.ts +0 -543
  165. package/test/test_ffi_user32.js +0 -103
  166. package/test/test_ffi_user32.ts +0 -113
  167. package/test/test_ffi_windows.js +0 -172
  168. package/test/test_ffi_windows.ts +0 -187
  169. package/test/test_files.js +0 -102
  170. package/test/test_files.ts +0 -121
  171. package/test/test_frame_encoding.js +0 -128
  172. package/test/test_frame_encoding.ts +0 -132
  173. package/test/test_gdi_draw.js +0 -681
  174. package/test/test_gdi_draw.ts +0 -725
  175. package/test/test_helper.js +0 -91
  176. package/test/test_helper.ts +0 -88
  177. package/test/test_http_import.js +0 -96
  178. package/test/test_http_import.ts +0 -93
  179. package/test/test_http_server.js +0 -214
  180. package/test/test_http_server.ts +0 -211
  181. package/test/test_import_dir.js +0 -4
  182. package/test/test_import_dir.ts +0 -5
  183. package/test/test_ipv6.js +0 -134
  184. package/test/test_ipv6.ts +0 -130
  185. package/test/test_jsx.js +0 -1314
  186. package/test/test_meta_url.js +0 -3
  187. package/test/test_meta_url.ts +0 -4
  188. package/test/test_mupdf_render.js +0 -51
  189. package/test/test_mupdf_render.ts +0 -55
  190. package/test/test_mupdf_twice.js +0 -62
  191. package/test/test_mupdf_twice.ts +0 -65
  192. package/test/test_mupdf_wasm.js +0 -39
  193. package/test/test_mupdf_wasm.ts +0 -32
  194. package/test/test_net_event.js +0 -57
  195. package/test/test_net_event.ts +0 -56
  196. package/test/test_net_fetch.js +0 -384
  197. package/test/test_net_fetch.ts +0 -373
  198. package/test/test_net_websocket.js +0 -180
  199. package/test/test_net_websocket.ts +0 -164
  200. package/test/test_polyfill.js +0 -144
  201. package/test/test_polyfill.ts +0 -159
  202. package/test/test_printer.js +0 -137
  203. package/test/test_printer.ts +0 -148
  204. package/test/test_proc.js +0 -141
  205. package/test/test_proc.ts +0 -163
  206. package/test/test_react_complex.js +0 -18678
  207. package/test/test_react_counter.js +0 -18555
  208. package/test/test_react_flex.js +0 -18871
  209. package/test/test_react_hidden.js +0 -18560
  210. package/test/test_react_listview_link.js +0 -18590
  211. package/test/test_react_notify.js +0 -18797
  212. package/test/test_react_render.js +0 -18550
  213. package/test/test_registry.js +0 -74
  214. package/test/test_registry.ts +0 -95
  215. package/test/test_sysinfo.js +0 -122
  216. package/test/test_sysinfo.ts +0 -141
  217. package/test/test_url.js +0 -173
  218. package/test/test_url.ts +0 -183
  219. package/test/test_wasm_basic.js +0 -85
  220. package/test/test_wasm_basic.ts +0 -73
  221. package/test/test_wasm_import_global.js +0 -41
  222. package/test/test_wasm_import_global.ts +0 -45
  223. package/test/test_wasm_sjlj.js +0 -145
  224. package/test/test_wasm_sjlj.ts +0 -125
  225. package/test/test_wasm_types.js +0 -97
  226. package/test/test_wasm_types.ts +0 -124
  227. package/test/test_wolfssl_read.js +0 -203
  228. package/test/test_wolfssl_read.ts +0 -184
  229. package/test/test_worker.js +0 -21
  230. package/test/test_worker.ts +0 -24
  231. package/test/test_worker_concurrent.js +0 -45
  232. package/test/test_worker_concurrent.ts +0 -56
  233. package/test/test_worker_data_url.js +0 -46
  234. package/test/test_worker_data_url.ts +0 -51
  235. package/test/test_worker_fetch.js +0 -26
  236. package/test/test_worker_fetch.ts +0 -27
  237. package/test/test_worker_net.js +0 -69
  238. package/test/test_worker_net.ts +0 -66
  239. package/test/test_worker_wasm_concurrent.js +0 -44
  240. package/test/test_worker_wasm_concurrent.ts +0 -45
  241. package/test/types.wasm +0 -0
  242. package/test/worker_fetch.js +0 -13
  243. package/test/worker_fetch.ts +0 -13
  244. package/test/worker_helper.js +0 -1
  245. package/test/worker_helper.ts +0 -1
  246. package/test/worker_http.js +0 -18
  247. package/test/worker_http.ts +0 -20
  248. package/test/worker_import_lib.js +0 -12
  249. package/test/worker_import_lib.ts +0 -13
  250. package/test/worker_module.js +0 -16
  251. package/test/worker_module.ts +0 -17
  252. package/test/worker_wasm_concurrent.js +0 -22
  253. 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
- };