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,116 +0,0 @@
1
- import { bind } from '../lib/ffi/bind.js';
2
- import { struct, structArray } from '../lib/ffi/struct.js';
3
- import { NULL, normToken, PtrArrayBuffer } from '../lib/ffi/ctype.js';
4
- function expectType(_value) { }
5
- // 运行时 throw 捕获:返回错误文本(无异常 → 空串)
6
- function throws(fn) {
7
- try {
8
- fn();
9
- return '';
10
- }
11
- catch (e) {
12
- return String(e);
13
- }
14
- }
15
- const POINT = struct('POINT', { x: 'i32', y: 'i32' });
16
- const RECT = struct('RECT', { l: 'i32', t: 'i32', r: 'i32', b: 'i32' });
17
- // 数组编解码对:encode 注册进 encoders 表后 <POINT>ptr 位收元素数组;
18
- // decode 是出参数组读回(双态:buffer 自算/显式 count、裸地址 count 必填)
19
- const POINTArr = structArray(POINT);
20
- export const suite = {
21
- name: 'ffi-array',
22
- run: (t) => {
23
- t.section("normToken: '*' is an ordinary out-of-lexicon char (same as '+')");
24
- t.checkTrue("'<X*>ptr' rejected", throws(() => normToken('<POINT*>ptr')).includes('Unknown token'));
25
- t.checkTrue("'*'+'!' rejected", throws(() => normToken('<POINT*>ptr!')).includes('Unknown token'));
26
- t.checkTrue("'*'+'@' rejected", throws(() => normToken('<POINT*>ptr@Foo')).includes('Unknown token'));
27
- t.checkTrue('bare <*>ptr rejected', throws(() => normToken('<*>ptr')).includes('Unknown token'));
28
- t.checkTrue("'+' not in lexicon: generic Unknown token", throws(() => normToken('<POINT+>ptr')).includes('Unknown token'));
29
- t.checkTrue('name charset tightened (non-identifier rejected)', throws(() => normToken('<1abc>ptr')).includes('Unknown token'));
30
- t.check('plain <X>ptr unchanged', '<X>ptr', normToken('<X>ptr'));
31
- t.check('!-form unchanged', '<X>ptr!', normToken('<X>ptr!'));
32
- t.check('@-suffix stripped (enum annotation is type-layer only)', '<X>ptr', normToken('<X>ptr@Foo'));
33
- t.section('type layer: registered encoder derives the array value domain');
34
- // 注册数组 encoder 后:<POINT>ptr 位 = 非空元素数组(值域由 encoder 推导)∪ number 直通
35
- const PolylineT = bind('gdi32.dll', 'Polyline', '<HDC>ptr! <POINT>ptr i32 -> i32', { POINT: POINTArr });
36
- expectType();
37
- expectType();
38
- // 类型专用块(只过 tsc、不执行)
39
- const typeOnly = () => {
40
- // @ts-expect-error 单对象不是元素数组(encoder 值域收数组,不收裸 POINT)
41
- PolylineT(0, { x: 1, y: 2 }, 1);
42
- // @ts-expect-error 品牌拦错布局直传(RECT PAB 不是 POINT 位的值域——传 .ptr 走 number 直通)
43
- PolylineT(0, RECT.alloc(), 1);
44
- // @ts-expect-error decode 的裸地址形 count 必填(地址无长度信息)
45
- POINTArr.decode(POINT.alloc().ptr);
46
- };
47
- t.checkTrue('type-only block wired', typeof typeOnly === 'function');
48
- t.section('structArray.encode: contiguous branded materialization');
49
- const pab = POINTArr.encode([{ x: 1, y: 2 }, { x: -3, y: 4 }]);
50
- t.check('byteLength = n * size', 16, pab.byteLength);
51
- const pdv = new DataView(pab);
52
- t.check('[0].x', 1, pdv.getInt32(0, true));
53
- t.check('[0].y', 2, pdv.getInt32(4, true));
54
- t.check('[1].x', -3, pdv.getInt32(8, true));
55
- t.check('[1].y', 4, pdv.getInt32(12, true));
56
- t.checkTrue('PAB .ptr non-null (branded)', pab.ptr !== 0);
57
- // fail-loud:值域刻意收普通数组(直传变量),空/非数组/单对象全靠运行时
58
- // throw —— 任何路径都不产出 PAB(0)(其 .ptr 是"非 0 的 1 字节堆指针",会骗过
59
- // C 的 NULL 防御)。
60
- t.checkTrue('[] fail-loud (never produces PAB(0))', throws(() => POINTArr.encode([])).includes('non-empty'));
61
- t.checkTrue('non-array fail-loud', throws(() => POINTArr.encode(123)).includes('non-empty'));
62
- t.section('structArray.decode: self-evident / explicit count / address form');
63
- // 自算形态:纯元素序列 buffer(alloc)byteLength/size 整除自证
64
- t.check('self-computed n = byteLength/size', 3, POINTArr.decode(POINT.alloc(3)).length);
65
- const rt = POINTArr.decode(POINTArr.encode([{ x: 1, y: -2 }, { x: 3, y: 4 }]));
66
- t.check('roundtrip [0].x', 1, rt[0].x);
67
- t.check('roundtrip [1].y', 4, rt[1].y);
68
- // 混合缓冲(byteLength 非整数倍,如 EnumPrinters 的结构 + 尾部字符串)自算 fail-loud
69
- t.checkTrue('non-multiple byteLength fail-loud', throws(() => POINTArr.decode(new PtrArrayBuffer(7))).includes('multiple'));
70
- // 显式 count 越界防线:count*size 超 byteLength → 拦死(写错 count 的越界读入口)
71
- t.checkTrue('count beyond byteLength fail-loud', throws(() => POINTArr.decode(new PtrArrayBuffer(12), 2)).includes('exceeds'));
72
- t.checkTrue('negative count fail-loud', throws(() => POINTArr.decode(new PtrArrayBuffer(16), -1)).includes('non-negative'));
73
- t.check('count = 0 → []', 0, POINTArr.decode(POINT.alloc(), 0).length);
74
- // 裸地址形(C 返回的 T* / 预建缓冲 .ptr):count 必填,逐字节拷读
75
- const addrBack = POINTArr.decode(pab.ptr, 2);
76
- t.check('address form count', 2, addrBack.length);
77
- t.check('address form [0].x', 1, addrBack[0].x);
78
- t.checkTrue('address without count throws at runtime (dynamic path)', throws(() => POINTArr.decode(pab.ptr)).includes('count'));
79
- t.section('StructDef.alloc(n): n-slot zero-filled (default 1)');
80
- t.check('alloc() default = 1 slot', POINT.size, POINT.alloc().byteLength);
81
- t.check('alloc(3) = 3 slots', POINT.size * 3, POINT.alloc(3).byteLength);
82
- t.checkTrue('alloc slots zero-filled', new Int32Array(POINT.alloc(2))[3] === 0);
83
- t.checkTrue('alloc(0) fail-loud (PAB(0) never escapes)', throws(() => POINT.alloc(0)).includes('n >= 1'));
84
- t.checkTrue('alloc(-1) fail-loud', throws(() => POINT.alloc(-1)).includes('n >= 1'));
85
- t.checkTrue('alloc(1.5) fail-loud', throws(() => POINT.alloc(1.5)).includes('n >= 1'));
86
- t.section('StructDef.encode(v, buf, offset): positioned write primitive');
87
- const big = POINT.alloc(2);
88
- POINT.encode({ x: 7, y: -8 }, big, POINT.size); // 写入第 2 槽
89
- const bdv = new DataView(big);
90
- t.check('slot 0 stays zero', 0, bdv.getInt32(0, true));
91
- t.check('slot 1.x written at offset', 7, bdv.getInt32(POINT.size, true));
92
- t.check('slot 1.y written at offset', -8, bdv.getInt32(POINT.size + 4, true));
93
- t.checkTrue('offset form returns the buf itself', POINT.encode({ x: 1, y: 2 }, big, 0) === big);
94
- t.section('registered encoder dispatch: kernel32!IsBadReadPtr observes the pointer C gets');
95
- const isBad = bind('kernel32.dll', 'IsBadReadPtr', '<POINT>ptr u64 -> i32', { POINT: POINTArr });
96
- t.check('3-point array: whole 24-byte region readable', 0, isBad([{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }], 24n));
97
- t.check('number passthrough: prebuilt PAB .ptr', 0, isBad(pab.ptr, 16n));
98
- t.check('number passthrough: NULL literal', 1, isBad(NULL, 1n));
99
- t.checkTrue('bare object rejected at encoder', throws(() => isBad({ x: 1, y: 2 }, 8n)).includes('non-empty'));
100
- t.checkTrue('[] rejected at encoder (NULL stays explicit)', throws(() => isBad([], 8n)).includes('non-empty'));
101
- t.checkTrue('null rejected with NULL hint', throws(() => isBad(null, 8n)).includes('NULL'));
102
- t.section('content fidelity: msvcrt!memcpy reads encoder array source');
103
- const memcpy = bind('msvcrt.dll', 'memcpy', '<POINT>ptr <POINT>ptr u64 -> <>ptr', { POINT: POINTArr });
104
- const dst = POINT.alloc();
105
- memcpy(dst.ptr, [{ x: 7, y: -8 }], 8n);
106
- const back = POINT.decode(dst.ptr);
107
- t.check('copied x', 7, back.x);
108
- t.check('copied y', -8, back.y);
109
- t.section("decode(alloc()): '!' unfilled field fails (C did not fill)");
110
- const OutH = struct('OutH', { h: '<HANDLE>ptr!' });
111
- t.checkTrue("'!' 0 slot throws on decode", throws(() => OutH.decode(OutH.alloc())).includes("'!'"));
112
- t.section('bind-time lexicon checks (fail at module load)');
113
- t.checkTrue('<POINT+>ptr rejected at bind (not in lexicon)', throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT+>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'));
114
- t.checkTrue('<POINT*>ptr rejected at bind (out-of-lexicon)', throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT*>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'));
115
- }
116
- };
@@ -1,141 +0,0 @@
1
- import { Tester } from './test_helper.js'
2
- import { bind } from '../lib/ffi/bind.js'
3
- import { struct, structArray } from '../lib/ffi/struct.js'
4
- import { NULL, normToken, PtrArrayBuffer, type MaybePtr, type Ptr } from '../lib/ffi/ctype.js'
5
-
6
- // 编译期断言工具(仅类型层,运行时无开销)
7
- type Equal<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false
8
- function expectType<T extends true>(_value?: T): void {}
9
-
10
- // 运行时 throw 捕获:返回错误文本(无异常 → 空串)
11
- function throws(fn: () => unknown): string {
12
- try { fn(); return '' } catch (e) { return String(e) }
13
- }
14
-
15
- const POINT = struct('POINT', { x: 'i32', y: 'i32' })
16
- const RECT = struct('RECT', { l: 'i32', t: 'i32', r: 'i32', b: 'i32' })
17
- // 数组编解码对:encode 注册进 encoders 表后 <POINT>ptr 位收元素数组;
18
- // decode 是出参数组读回(双态:buffer 自算/显式 count、裸地址 count 必填)
19
- const POINTArr = structArray(POINT)
20
-
21
- export const suite = {
22
- name: 'ffi-array',
23
- run: (t: Tester) => {
24
- t.section("normToken: '*' is an ordinary out-of-lexicon char (same as '+')")
25
- t.checkTrue("'<X*>ptr' rejected", throws(() => normToken('<POINT*>ptr')).includes('Unknown token'))
26
- t.checkTrue("'*'+'!' rejected", throws(() => normToken('<POINT*>ptr!')).includes('Unknown token'))
27
- t.checkTrue("'*'+'@' rejected", throws(() => normToken('<POINT*>ptr@Foo')).includes('Unknown token'))
28
- t.checkTrue('bare <*>ptr rejected', throws(() => normToken('<*>ptr')).includes('Unknown token'))
29
- t.checkTrue("'+' not in lexicon: generic Unknown token", throws(() => normToken('<POINT+>ptr')).includes('Unknown token'))
30
- t.checkTrue('name charset tightened (non-identifier rejected)',
31
- throws(() => normToken('<1abc>ptr')).includes('Unknown token'))
32
- t.check('plain <X>ptr unchanged', '<X>ptr', normToken('<X>ptr'))
33
- t.check('!-form unchanged', '<X>ptr!', normToken('<X>ptr!'))
34
- t.check('@-suffix stripped (enum annotation is type-layer only)', '<X>ptr', normToken('<X>ptr@Foo'))
35
-
36
- t.section('type layer: registered encoder derives the array value domain')
37
- // 注册数组 encoder 后:<POINT>ptr 位 = 非空元素数组(值域由 encoder 推导)∪ number 直通
38
- const PolylineT = bind('gdi32.dll', 'Polyline', '<HDC>ptr! <POINT>ptr i32 -> i32', { POINT: POINTArr })
39
- expectType<Equal<Parameters<typeof PolylineT>[1],
40
- readonly { readonly x?: number | undefined; readonly y?: number | undefined }[] | MaybePtr<'POINT'>>>()
41
- expectType<Equal<Parameters<typeof PolylineT>[0], Ptr<'HDC'>>>()
42
- // 类型专用块(只过 tsc、不执行)
43
- const typeOnly: unknown = () => {
44
- // @ts-expect-error 单对象不是元素数组(encoder 值域收数组,不收裸 POINT)
45
- PolylineT(0 as Ptr<'HDC'>, { x: 1, y: 2 }, 1)
46
- // @ts-expect-error 品牌拦错布局直传(RECT PAB 不是 POINT 位的值域——传 .ptr 走 number 直通)
47
- PolylineT(0 as Ptr<'HDC'>, RECT.alloc(), 1)
48
- // @ts-expect-error decode 的裸地址形 count 必填(地址无长度信息)
49
- POINTArr.decode(POINT.alloc().ptr)
50
- }
51
- t.checkTrue('type-only block wired', typeof typeOnly === 'function')
52
-
53
- t.section('structArray.encode: contiguous branded materialization')
54
- const pab = POINTArr.encode([{ x: 1, y: 2 }, { x: -3, y: 4 }])
55
- t.check('byteLength = n * size', 16, pab.byteLength)
56
- const pdv = new DataView(pab)
57
- t.check('[0].x', 1, pdv.getInt32(0, true))
58
- t.check('[0].y', 2, pdv.getInt32(4, true))
59
- t.check('[1].x', -3, pdv.getInt32(8, true))
60
- t.check('[1].y', 4, pdv.getInt32(12, true))
61
- t.checkTrue('PAB .ptr non-null (branded)', pab.ptr !== 0)
62
- // fail-loud:值域刻意收普通数组(直传变量),空/非数组/单对象全靠运行时
63
- // throw —— 任何路径都不产出 PAB(0)(其 .ptr 是"非 0 的 1 字节堆指针",会骗过
64
- // C 的 NULL 防御)。
65
- t.checkTrue('[] fail-loud (never produces PAB(0))',
66
- throws(() => POINTArr.encode([])).includes('non-empty'))
67
- t.checkTrue('non-array fail-loud',
68
- throws(() => POINTArr.encode(123 as never)).includes('non-empty'))
69
-
70
- t.section('structArray.decode: self-evident / explicit count / address form')
71
- // 自算形态:纯元素序列 buffer(alloc)byteLength/size 整除自证
72
- t.check('self-computed n = byteLength/size', 3, POINTArr.decode(POINT.alloc(3)).length)
73
- const rt = POINTArr.decode(POINTArr.encode([{ x: 1, y: -2 }, { x: 3, y: 4 }]))
74
- t.check('roundtrip [0].x', 1, rt[0]!.x)
75
- t.check('roundtrip [1].y', 4, rt[1]!.y)
76
- // 混合缓冲(byteLength 非整数倍,如 EnumPrinters 的结构 + 尾部字符串)自算 fail-loud
77
- t.checkTrue('non-multiple byteLength fail-loud',
78
- throws(() => POINTArr.decode(new PtrArrayBuffer(7))).includes('multiple'))
79
- // 显式 count 越界防线:count*size 超 byteLength → 拦死(写错 count 的越界读入口)
80
- t.checkTrue('count beyond byteLength fail-loud',
81
- throws(() => POINTArr.decode(new PtrArrayBuffer(12), 2)).includes('exceeds'))
82
- t.checkTrue('negative count fail-loud',
83
- throws(() => POINTArr.decode(new PtrArrayBuffer(16), -1)).includes('non-negative'))
84
- t.check('count = 0 → []', 0, POINTArr.decode(POINT.alloc(), 0).length)
85
- // 裸地址形(C 返回的 T* / 预建缓冲 .ptr):count 必填,逐字节拷读
86
- const addrBack = POINTArr.decode(pab.ptr, 2)
87
- t.check('address form count', 2, addrBack.length)
88
- t.check('address form [0].x', 1, addrBack[0]!.x)
89
- t.checkTrue('address without count throws at runtime (dynamic path)',
90
- throws(() => (POINTArr.decode as unknown as (p: number) => unknown[])(pab.ptr)).includes('count'))
91
-
92
- t.section('StructDef.alloc(n): n-slot zero-filled (default 1)')
93
- t.check('alloc() default = 1 slot', POINT.size, POINT.alloc().byteLength)
94
- t.check('alloc(3) = 3 slots', POINT.size * 3, POINT.alloc(3).byteLength)
95
- t.checkTrue('alloc slots zero-filled', new Int32Array(POINT.alloc(2))[3] === 0)
96
- t.checkTrue('alloc(0) fail-loud (PAB(0) never escapes)',
97
- throws(() => POINT.alloc(0)).includes('n >= 1'))
98
- t.checkTrue('alloc(-1) fail-loud', throws(() => POINT.alloc(-1)).includes('n >= 1'))
99
- t.checkTrue('alloc(1.5) fail-loud', throws(() => POINT.alloc(1.5)).includes('n >= 1'))
100
-
101
- t.section('StructDef.encode(v, buf, offset): positioned write primitive')
102
- const big = POINT.alloc(2)
103
- POINT.encode({ x: 7, y: -8 }, big, POINT.size) // 写入第 2 槽
104
- const bdv = new DataView(big)
105
- t.check('slot 0 stays zero', 0, bdv.getInt32(0, true))
106
- t.check('slot 1.x written at offset', 7, bdv.getInt32(POINT.size, true))
107
- t.check('slot 1.y written at offset', -8, bdv.getInt32(POINT.size + 4, true))
108
- t.checkTrue('offset form returns the buf itself', POINT.encode({ x: 1, y: 2 }, big, 0) === big)
109
-
110
- t.section('registered encoder dispatch: kernel32!IsBadReadPtr observes the pointer C gets')
111
- const isBad = bind('kernel32.dll', 'IsBadReadPtr', '<POINT>ptr u64 -> i32', { POINT: POINTArr })
112
- t.check('3-point array: whole 24-byte region readable', 0, isBad([{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }], 24n))
113
- t.check('number passthrough: prebuilt PAB .ptr', 0, isBad(pab.ptr, 16n))
114
- t.check('number passthrough: NULL literal', 1, isBad(NULL, 1n))
115
- t.checkTrue('bare object rejected at encoder',
116
- throws(() => isBad({ x: 1, y: 2 } as never, 8n)).includes('non-empty'))
117
- t.checkTrue('[] rejected at encoder (NULL stays explicit)',
118
- throws(() => isBad([], 8n)).includes('non-empty'))
119
- t.checkTrue('null rejected with NULL hint',
120
- throws(() => isBad(null as never, 8n)).includes('NULL'))
121
-
122
- t.section('content fidelity: msvcrt!memcpy reads encoder array source')
123
- const memcpy = bind('msvcrt.dll', 'memcpy', '<POINT>ptr <POINT>ptr u64 -> <>ptr', { POINT: POINTArr })
124
- const dst = POINT.alloc()
125
- memcpy(dst.ptr, [{ x: 7, y: -8 }], 8n)
126
- const back = POINT.decode(dst.ptr)
127
- t.check('copied x', 7, back.x)
128
- t.check('copied y', -8, back.y)
129
-
130
- t.section("decode(alloc()): '!' unfilled field fails (C did not fill)")
131
- const OutH = struct('OutH', { h: '<HANDLE>ptr!' })
132
- t.checkTrue("'!' 0 slot throws on decode",
133
- throws(() => OutH.decode(OutH.alloc())).includes("'!'"))
134
-
135
- t.section('bind-time lexicon checks (fail at module load)')
136
- t.checkTrue('<POINT+>ptr rejected at bind (not in lexicon)',
137
- throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT+>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'))
138
- t.checkTrue('<POINT*>ptr rejected at bind (out-of-lexicon)',
139
- throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT*>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'))
140
- }
141
- }