quickwin 1.3.0 → 2.1.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 (167) hide show
  1. package/README.en.md +18 -24
  2. package/README.md +17 -23
  3. package/examples/PdfCanvas.js +8 -37
  4. package/examples/PdfCanvas.tsx +11 -41
  5. package/examples/exec_server.js +20155 -1420
  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 +1424 -1180
  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 +1418 -1075
  16. package/examples/test_react_lazy.js +1391 -1059
  17. package/examples/test_react_listbox.js +1390 -1058
  18. package/examples/test_root_window.js +1384 -1052
  19. package/examples/test_tab.js +1391 -1059
  20. package/lib/abort.js +70 -0
  21. package/lib/abort.ts +69 -0
  22. package/lib/certs.js +219 -0
  23. package/lib/certs.ts +229 -0
  24. package/lib/exec-log.js +15 -0
  25. package/lib/exec-log.ts +30 -0
  26. package/lib/fetch.js +88 -7
  27. package/lib/fetch.ts +73 -7
  28. package/lib/ffi/bind.js +441 -0
  29. package/lib/ffi/bind.ts +539 -0
  30. package/lib/ffi/ctype.js +194 -0
  31. package/lib/ffi/ctype.ts +369 -0
  32. package/lib/ffi/index.js +6 -0
  33. package/lib/ffi/index.ts +6 -0
  34. package/lib/ffi/struct.js +490 -0
  35. package/lib/ffi/struct.ts +753 -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 +50 -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 +5 -3
  51. package/lib/react-qw/index.ts +8 -3
  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 +20 -23
  59. package/lib/text-measure.ts +22 -26
  60. package/lib/websocket.js +44 -4
  61. package/lib/websocket.ts +40 -4
  62. package/lib/windows/advapi32.js +53 -0
  63. package/lib/windows/advapi32.ts +64 -0
  64. package/lib/windows/comctl32.js +18 -0
  65. package/lib/windows/comctl32.ts +19 -0
  66. package/lib/windows/comdlg32.js +8 -0
  67. package/lib/windows/comdlg32.ts +10 -0
  68. package/lib/windows/gdi32.js +392 -0
  69. package/lib/windows/gdi32.ts +420 -0
  70. package/lib/windows/kernel32.js +262 -0
  71. package/lib/windows/kernel32.ts +291 -0
  72. package/lib/windows/msimg32.js +38 -0
  73. package/lib/windows/msimg32.ts +45 -0
  74. package/lib/windows/ntdll.js +9 -0
  75. package/lib/windows/ntdll.ts +11 -0
  76. package/lib/windows/ole32.js +7 -0
  77. package/lib/windows/ole32.ts +9 -0
  78. package/lib/windows/psapi.js +32 -0
  79. package/lib/windows/psapi.ts +37 -0
  80. package/lib/windows/shell32.js +13 -0
  81. package/lib/windows/shell32.ts +16 -0
  82. package/lib/windows/structs.js +519 -0
  83. package/lib/windows/structs.ts +566 -0
  84. package/lib/windows/user32.js +572 -0
  85. package/lib/windows/user32.ts +607 -0
  86. package/lib/windows/version.js +29 -0
  87. package/lib/windows/version.ts +35 -0
  88. package/lib/windows/winspool.js +71 -0
  89. package/lib/windows/winspool.ts +80 -0
  90. package/package.json +1 -1
  91. package/quickwin.d.ts +31 -40
  92. package/quickwin_const.d.ts +137 -0
  93. package/qwin-nowasm-x86.exe +0 -0
  94. package/qwin-nowasm.exe +0 -0
  95. package/qwin-x86.exe +0 -0
  96. package/qwin.exe +0 -0
  97. package/test/bench_ffi_callraw.js +236 -0
  98. package/test/bench_ffi_callraw.ts +236 -0
  99. package/test/bench_ffi_overhead.js +139 -0
  100. package/test/bench_ffi_overhead.ts +141 -0
  101. package/test/bench_sendmessage.js +104 -0
  102. package/test/bench_sendmessage.ts +100 -0
  103. package/test/bench_sendmessage_nonnull.js +187 -0
  104. package/test/bench_sendmessage_nonnull.ts +188 -0
  105. package/test/run.js +19 -4
  106. package/test/run.ts +20 -5
  107. package/test/test_basic_fetch.js +29 -0
  108. package/test/test_basic_fetch.ts +26 -0
  109. package/test/test_ffi.js +27 -45
  110. package/test/test_ffi.ts +35 -54
  111. package/test/test_ffi_abi.js +290 -0
  112. package/test/test_ffi_abi.ts +274 -0
  113. package/test/test_ffi_array.js +116 -0
  114. package/test/test_ffi_array.ts +141 -0
  115. package/test/test_ffi_bind.js +620 -8
  116. package/test/test_ffi_bind.ts +630 -8
  117. package/test/test_ffi_struct.js +632 -88
  118. package/test/test_ffi_struct.ts +666 -91
  119. package/test/test_ffi_struct_layout.js +495 -0
  120. package/test/test_ffi_struct_layout.ts +543 -0
  121. package/test/test_ffi_user32.js +103 -0
  122. package/test/test_ffi_user32.ts +113 -0
  123. package/test/test_ffi_windows.js +101 -0
  124. package/test/test_ffi_windows.ts +113 -0
  125. package/test/test_files.js +102 -0
  126. package/test/test_files.ts +121 -0
  127. package/test/test_gdi_draw.js +681 -0
  128. package/test/test_gdi_draw.ts +725 -0
  129. package/test/test_helper.js +8 -1
  130. package/test/test_helper.ts +9 -1
  131. package/test/test_http_import.js +18 -0
  132. package/test/test_http_import.ts +19 -0
  133. package/test/test_http_server.js +73 -1
  134. package/test/test_http_server.ts +75 -1
  135. package/test/test_ipv6.js +2 -2
  136. package/test/test_ipv6.ts +2 -2
  137. package/test/test_net_fetch.js +74 -11
  138. package/test/test_net_fetch.ts +73 -10
  139. package/test/test_net_websocket.js +1 -1
  140. package/test/test_net_websocket.ts +1 -1
  141. package/test/test_printer.js +137 -0
  142. package/test/test_printer.ts +148 -0
  143. package/test/test_proc.js +141 -0
  144. package/test/test_proc.ts +163 -0
  145. package/test/test_react_complex.js +1401 -1069
  146. package/test/test_react_counter.js +1391 -1059
  147. package/test/test_react_flex.js +1411 -1072
  148. package/test/test_react_hidden.js +1394 -1062
  149. package/test/test_react_listview_link.js +1391 -1059
  150. package/test/test_react_notify.js +18638 -0
  151. package/test/test_react_render.js +1391 -1059
  152. package/test/test_registry.js +74 -0
  153. package/test/test_registry.ts +95 -0
  154. package/test/test_sysinfo.js +122 -0
  155. package/test/test_sysinfo.ts +141 -0
  156. package/test/test_wolfssl_read.js +45 -43
  157. package/test/test_wolfssl_read.ts +42 -40
  158. package/test/test_worker_net.js +35 -17
  159. package/test/test_worker_net.ts +27 -15
  160. package/tsconfig.json +1 -1
  161. package/examples/exec_server.ts +0 -80
  162. package/lib/ffi-bind.js +0 -85
  163. package/lib/ffi-bind.ts +0 -170
  164. package/lib/ffi-struct.js +0 -224
  165. package/lib/ffi-struct.ts +0 -239
  166. package/lib/win-common-structs.js +0 -39
  167. package/lib/win-common-structs.ts +0 -41
@@ -0,0 +1,74 @@
1
+ import * as os from 'os';
2
+ import { NULL, PtrArrayBuffer } from '../lib/ffi/ctype.js';
3
+ import { RegCloseKey, RegCreateKeyEx, RegDeleteKey, RegOpenKeyEx, RegQueryValueEx, RegSetValueEx, } from '../lib/windows/advapi32.js';
4
+ // 跨架构读指针宽句柄(ia32 4B / x64 8B)—— 照 test_ffi.ts 的 readPtr 先例
5
+ function readPtr(dv, offset) {
6
+ if (os.arch === 'ia32') {
7
+ return dv.getUint32(offset, true);
8
+ }
9
+ const low = dv.getUint32(offset, true);
10
+ const high = dv.getUint32(offset + 4, true);
11
+ return low + high * 4294967296;
12
+ }
13
+ // REG_SZ 数据 = UTF-16LE 含结尾 NUL;读回按字节数截到 NUL
14
+ function wbuf(s) {
15
+ const p = new PtrArrayBuffer((s.length + 1) * 2);
16
+ const dv = new DataView(p);
17
+ for (let i = 0; i <= s.length; i++)
18
+ dv.setUint16(i * 2, s.charCodeAt(i), true);
19
+ return p;
20
+ }
21
+ function wstr(dv, bytes) {
22
+ let out = '';
23
+ for (let i = 0; i + 1 < bytes; i += 2) {
24
+ const ch = dv.getUint16(i, true);
25
+ if (!ch)
26
+ break;
27
+ out += String.fromCharCode(ch);
28
+ }
29
+ return out;
30
+ }
31
+ // 测试键:HKCU\Software\QuickWinTest(suite 结束时删除;中途 FAIL 会在下次 run 时复用)
32
+ const KEY = 'Software\\QuickWinTest';
33
+ const MSG = 'hello registry';
34
+ export const suite = {
35
+ name: 'registry',
36
+ run: (t) => {
37
+ t.section('RegCreateKeyEx: create test key under HKCU');
38
+ const phk = new PtrArrayBuffer(8);
39
+ // hKey 位双来源:预定义键常量(HKEY_CURRENT_USER = 0x80000001 的 i32 形)
40
+ // 或真实句柄——裸 <>ptr 位收两者;符号/零扩展语义由本调用实测检验
41
+ const rcCreate = RegCreateKeyEx(-2147483647 /* gui.HKey.CURRENT_USER */, KEY, 0, NULL, 0, 983103 /* gui.RegAccess.ALL_ACCESS */, NULL, phk, NULL);
42
+ t.check('create key = ERROR_SUCCESS', 0, rcCreate);
43
+ if (rcCreate !== 0)
44
+ return; // 基座失败,后续无意义
45
+ const hkey = readPtr(new DataView(phk), 0);
46
+ t.checkTrue(`key handle non-null (${hkey})`, hkey !== 0);
47
+ t.section('RegSetValueEx + RegQueryValueEx: REG_SZ roundtrip');
48
+ const data = wbuf(MSG);
49
+ t.check('set REG_SZ = ERROR_SUCCESS', 0, RegSetValueEx(hkey, 'greeting', 0, 1 /* gui.RegType.SZ */, data, (MSG.length + 1) * 2));
50
+ const pType = new PtrArrayBuffer(4);
51
+ const pCb = new PtrArrayBuffer(4);
52
+ const pData = new PtrArrayBuffer(64);
53
+ new DataView(pCb).setUint32(0, 64, true); // 双向 lpcbData:入 = 缓冲大小须预写
54
+ t.check('query = ERROR_SUCCESS', 0, RegQueryValueEx(hkey, 'greeting', NULL, pType, pData, pCb));
55
+ t.check('type = REG_SZ', 1, new DataView(pType).getUint32(0, true));
56
+ t.check('size = (len+1)*2', (MSG.length + 1) * 2, new DataView(pCb).getUint32(0, true));
57
+ t.check('data roundtrip', MSG, wstr(new DataView(pData), (MSG.length + 1) * 2));
58
+ t.section('RegQueryValueEx: size probe (lpData = NULL)');
59
+ new DataView(pCb).setUint32(0, 0, true); // 预写 0:只查类型/尺寸
60
+ t.check('probe = ERROR_SUCCESS', 0, RegQueryValueEx(hkey, 'greeting', NULL, NULL, NULL, pCb));
61
+ t.check('probe size', (MSG.length + 1) * 2, new DataView(pCb).getUint32(0, true));
62
+ t.section('RegOpenKeyEx: reopen + missings');
63
+ const phk2 = new PtrArrayBuffer(8);
64
+ t.check('reopen = ERROR_SUCCESS', 0, RegOpenKeyEx(-2147483647 /* gui.HKey.CURRENT_USER */, KEY, 0, 131097 /* gui.RegAccess.READ */, phk2));
65
+ const hkey2 = readPtr(new DataView(phk2), 0);
66
+ t.check('missing value → ERROR_FILE_NOT_FOUND', 2, RegQueryValueEx(hkey2, 'no-such-value', NULL, NULL, NULL, NULL));
67
+ t.check('missing key → ERROR_FILE_NOT_FOUND', 2, RegOpenKeyEx(-2147483647 /* gui.HKey.CURRENT_USER */, KEY + 'X', 0, 131097 /* gui.RegAccess.READ */, new PtrArrayBuffer(8)));
68
+ RegCloseKey(hkey2);
69
+ t.section('RegDeleteKey + RegCloseKey: cleanup');
70
+ t.check('delete key = ERROR_SUCCESS', 0, RegDeleteKey(-2147483647 /* gui.HKey.CURRENT_USER */, KEY));
71
+ t.check('close = ERROR_SUCCESS', 0, RegCloseKey(hkey));
72
+ t.check('open deleted → ERROR_FILE_NOT_FOUND', 2, RegOpenKeyEx(-2147483647 /* gui.HKey.CURRENT_USER */, KEY, 0, 131097 /* gui.RegAccess.READ */, new PtrArrayBuffer(8)));
73
+ },
74
+ };
@@ -0,0 +1,95 @@
1
+ import * as os from 'os'
2
+ import * as gui from 'gui'
3
+ import { NULL, PtrArrayBuffer } from '../lib/ffi/ctype.js'
4
+ import {
5
+ RegCloseKey, RegCreateKeyEx, RegDeleteKey, RegOpenKeyEx,
6
+ RegQueryValueEx, RegSetValueEx,
7
+ } from '../lib/windows/advapi32.js'
8
+ import { Tester } from './test_helper.js'
9
+
10
+ // 跨架构读指针宽句柄(ia32 4B / x64 8B)—— 照 test_ffi.ts 的 readPtr 先例
11
+ function readPtr(dv: DataView, offset: number): number {
12
+ if (os.arch === 'ia32') {
13
+ return dv.getUint32(offset, true)
14
+ }
15
+ const low = dv.getUint32(offset, true)
16
+ const high = dv.getUint32(offset + 4, true)
17
+ return low + high * 4294967296
18
+ }
19
+
20
+ // REG_SZ 数据 = UTF-16LE 含结尾 NUL;读回按字节数截到 NUL
21
+ function wbuf(s: string) {
22
+ const p = new PtrArrayBuffer((s.length + 1) * 2)
23
+ const dv = new DataView(p)
24
+ for (let i = 0; i <= s.length; i++) dv.setUint16(i * 2, s.charCodeAt(i), true)
25
+ return p
26
+ }
27
+ function wstr(dv: DataView, bytes: number): string {
28
+ let out = ''
29
+ for (let i = 0; i + 1 < bytes; i += 2) {
30
+ const ch = dv.getUint16(i, true)
31
+ if (!ch) break
32
+ out += String.fromCharCode(ch)
33
+ }
34
+ return out
35
+ }
36
+
37
+ // 测试键:HKCU\Software\QuickWinTest(suite 结束时删除;中途 FAIL 会在下次 run 时复用)
38
+ const KEY = 'Software\\QuickWinTest'
39
+ const MSG = 'hello registry'
40
+
41
+ export const suite = {
42
+ name: 'registry',
43
+ run: (t: Tester) => {
44
+ t.section('RegCreateKeyEx: create test key under HKCU')
45
+ const phk = new PtrArrayBuffer(8)
46
+ // hKey 位双来源:预定义键常量(HKEY_CURRENT_USER = 0x80000001 的 i32 形)
47
+ // 或真实句柄——裸 <>ptr 位收两者;符号/零扩展语义由本调用实测检验
48
+ const rcCreate = RegCreateKeyEx(gui.HKey.CURRENT_USER, KEY, 0, NULL, 0,
49
+ gui.RegAccess.ALL_ACCESS, NULL, phk, NULL)
50
+ t.check('create key = ERROR_SUCCESS', 0, rcCreate)
51
+ if (rcCreate !== 0) return // 基座失败,后续无意义
52
+ const hkey = readPtr(new DataView(phk), 0)
53
+ t.checkTrue(`key handle non-null (${hkey})`, hkey !== 0)
54
+
55
+ t.section('RegSetValueEx + RegQueryValueEx: REG_SZ roundtrip')
56
+ const data = wbuf(MSG)
57
+ t.check('set REG_SZ = ERROR_SUCCESS', 0,
58
+ RegSetValueEx(hkey, 'greeting', 0, gui.RegType.SZ, data, (MSG.length + 1) * 2))
59
+ const pType = new PtrArrayBuffer(4)
60
+ const pCb = new PtrArrayBuffer(4)
61
+ const pData = new PtrArrayBuffer(64)
62
+ new DataView(pCb).setUint32(0, 64, true) // 双向 lpcbData:入 = 缓冲大小须预写
63
+ t.check('query = ERROR_SUCCESS', 0,
64
+ RegQueryValueEx(hkey, 'greeting', NULL, pType, pData, pCb))
65
+ t.check('type = REG_SZ', 1, new DataView(pType).getUint32(0, true))
66
+ t.check('size = (len+1)*2', (MSG.length + 1) * 2, new DataView(pCb).getUint32(0, true))
67
+ t.check('data roundtrip', MSG, wstr(new DataView(pData), (MSG.length + 1) * 2))
68
+
69
+ t.section('RegQueryValueEx: size probe (lpData = NULL)')
70
+ new DataView(pCb).setUint32(0, 0, true) // 预写 0:只查类型/尺寸
71
+ t.check('probe = ERROR_SUCCESS', 0,
72
+ RegQueryValueEx(hkey, 'greeting', NULL, NULL, NULL, pCb))
73
+ t.check('probe size', (MSG.length + 1) * 2, new DataView(pCb).getUint32(0, true))
74
+
75
+ t.section('RegOpenKeyEx: reopen + missings')
76
+ const phk2 = new PtrArrayBuffer(8)
77
+ t.check('reopen = ERROR_SUCCESS', 0,
78
+ RegOpenKeyEx(gui.HKey.CURRENT_USER, KEY, 0, gui.RegAccess.READ, phk2))
79
+ const hkey2 = readPtr(new DataView(phk2), 0)
80
+ t.check('missing value → ERROR_FILE_NOT_FOUND', 2,
81
+ RegQueryValueEx(hkey2, 'no-such-value', NULL, NULL, NULL, NULL))
82
+ t.check('missing key → ERROR_FILE_NOT_FOUND', 2,
83
+ RegOpenKeyEx(gui.HKey.CURRENT_USER, KEY + 'X', 0, gui.RegAccess.READ,
84
+ new PtrArrayBuffer(8)))
85
+ RegCloseKey(hkey2)
86
+
87
+ t.section('RegDeleteKey + RegCloseKey: cleanup')
88
+ t.check('delete key = ERROR_SUCCESS', 0,
89
+ RegDeleteKey(gui.HKey.CURRENT_USER, KEY))
90
+ t.check('close = ERROR_SUCCESS', 0, RegCloseKey(hkey))
91
+ t.check('open deleted → ERROR_FILE_NOT_FOUND', 2,
92
+ RegOpenKeyEx(gui.HKey.CURRENT_USER, KEY, 0, gui.RegAccess.READ,
93
+ new PtrArrayBuffer(8)))
94
+ },
95
+ }
@@ -0,0 +1,122 @@
1
+ import * as ffi from 'ffi';
2
+ import * as os from 'os';
3
+ import { WCHAR } from '../lib/ffi/bind.js';
4
+ import { NULL, PtrArrayBuffer } from '../lib/ffi/ctype.js';
5
+ import { FormatMessage, GetCommandLineW, GetComputerName, GetEnvironmentVariable, GetLocalTime, GetModuleFileName, GetSystemDirectory, GetSystemTimeAsFileTime, GlobalAlloc, GlobalLock, GlobalUnlock, QueryPerformanceCounter, QueryPerformanceFrequency, RtlMoveMemory, Sleep, } from '../lib/windows/kernel32.js';
6
+ import { CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard, RegisterClipboardFormat, SetClipboardData, } from '../lib/windows/user32.js';
7
+ import { GetFileVersionInfoSize, GetFileVersionInfo, VerQueryValue, } from '../lib/windows/version.js';
8
+ import { SYSTEMTIME } from '../lib/windows/structs.js';
9
+ // 进程内存跟进读:ffi 只有 readByte(无 DataView/写通道),u32 小端拼值 / 指针随位宽
10
+ // (| 是有符号 32 位运算,>>> 0 归一无符号——0xFEEF04BD 类高位值否则读成负数)
11
+ function readU32At(addr) {
12
+ return (ffi.readByte(addr) | (ffi.readByte(addr + 1) << 8) |
13
+ (ffi.readByte(addr + 2) << 16) | (ffi.readByte(addr + 3) << 24)) >>> 0;
14
+ }
15
+ function readPtr(dv, offset) {
16
+ if (os.arch === 'ia32')
17
+ return dv.getUint32(offset, true);
18
+ return dv.getUint32(offset, true) + dv.getUint32(offset + 4, true) * 4294967296;
19
+ }
20
+ export const suite = {
21
+ name: 'sysinfo',
22
+ run: (t) => {
23
+ t.section('QueryPerformanceCounter: frequency + monotonic');
24
+ const freqB = new PtrArrayBuffer(8);
25
+ t.checkTrue('QueryPerformanceFrequency ok', QueryPerformanceFrequency(freqB) !== 0);
26
+ const freq = new DataView(freqB).getBigInt64(0, true);
27
+ t.checkTrue(`frequency sane (${freq})`, freq > 0n);
28
+ const c1 = new PtrArrayBuffer(8);
29
+ const c2 = new PtrArrayBuffer(8);
30
+ QueryPerformanceCounter(c1);
31
+ QueryPerformanceCounter(c2);
32
+ const v1 = new DataView(c1).getBigInt64(0, true);
33
+ const v2 = new DataView(c2).getBigInt64(0, true);
34
+ t.checkTrue(`counter monotonic (${v1} <= ${v2})`, v1 <= v2);
35
+ t.section('Sleep: QPC-measured elapsed lands in interval');
36
+ QueryPerformanceCounter(c1);
37
+ Sleep(50);
38
+ QueryPerformanceCounter(c2);
39
+ const elapsedMs = Number((new DataView(c2).getBigInt64(0, true) -
40
+ new DataView(c1).getBigInt64(0, true)) * 1000n / freq);
41
+ t.checkTrue(`Sleep(50) -> ${elapsedMs}ms in [30, 3000)`, elapsedMs >= 30 && elapsedMs < 3000);
42
+ t.section('GetLocalTime / GetSystemTimeAsFileTime');
43
+ const st = SYSTEMTIME.alloc();
44
+ GetLocalTime(st.ptr);
45
+ const got = SYSTEMTIME.decode(st);
46
+ t.checkTrue(`year sane (${got.wYear})`, got.wYear >= 2024 && got.wYear <= 2100);
47
+ t.checkTrue(`month sane (${got.wMonth})`, got.wMonth >= 1 && got.wMonth <= 12);
48
+ const ftB = new PtrArrayBuffer(8);
49
+ GetSystemTimeAsFileTime(ftB);
50
+ const ft = new DataView(ftB).getBigUint64(0, true);
51
+ t.checkTrue(`FILETIME > 0 (${ft})`, ft > 0n);
52
+ t.section('env / computer name / module path');
53
+ const pathW = WCHAR.alloc(4096);
54
+ const pathLen = GetEnvironmentVariable('PATH', pathW, 4096);
55
+ t.checkTrue(`PATH non-empty (${pathLen} chars)`, pathLen > 0);
56
+ const nameW = WCHAR.alloc(260);
57
+ const nameLenB = new PtrArrayBuffer(4);
58
+ new Uint32Array(nameLenB)[0] = 260;
59
+ t.checkTrue('GetComputerName ok', GetComputerName(nameW, nameLenB) !== 0);
60
+ const compName = WCHAR.decode(nameW);
61
+ t.checkTrue(`computer name non-empty (${compName})`, compName.length > 0);
62
+ const modW = WCHAR.alloc(520);
63
+ const modLen = GetModuleFileName(NULL, modW, 520);
64
+ const modPath = WCHAR.decode(modW);
65
+ t.checkTrue(`exe path has qwin (${modLen > 0})`, /qwin/i.test(modPath));
66
+ // 生产绑定的返回位 `<WCHAR>ptr!`:内建 WCHAR 自动解码 + `!` 非空担保(直接 string)
67
+ const cmdLine = GetCommandLineW();
68
+ t.checkTrue(`GetCommandLineW decodes to non-empty string (${cmdLine.length} chars)`, typeof cmdLine === 'string' && cmdLine.length > 0);
69
+ t.section('FormatMessage: error 2 -> readable text');
70
+ const msgW = WCHAR.alloc(512);
71
+ const msgLen = FormatMessage(0x1200 /* FROM_SYSTEM | IGNORE_INSERTS */, NULL, 2 /* ERROR_FILE_NOT_FOUND */, 0, msgW, 512, NULL);
72
+ // 语言无关断言(VM 中/英文系统都可过):错误 2 的系统文字必然 > 4 字符
73
+ t.checkTrue(`error 2 message non-empty (${msgLen} chars)`, msgLen > 4);
74
+ t.section('clipboard roundtrip: CF_UNICODETEXT (GlobalAlloc/RtlMoveMemory)');
75
+ t.checkTrue('OpenClipboard', OpenClipboard(NULL) !== 0);
76
+ t.checkTrue('EmptyClipboard', EmptyClipboard() !== 0);
77
+ const clipText = 'QuickWin clip roundtrip 你好';
78
+ const clipSrc = WCHAR.encode(clipText); // 自带 NUL 终止
79
+ const hMem = GlobalAlloc(0x2 /* GMEM_MOVEABLE */, clipSrc.byteLength);
80
+ t.checkTrue(`GlobalAlloc valid (${hMem})`, hMem !== 0);
81
+ const clipDst = GlobalLock(hMem);
82
+ t.checkTrue(`GlobalLock address (${clipDst})`, clipDst !== 0);
83
+ RtlMoveMemory(clipDst, clipSrc, clipSrc.byteLength);
84
+ GlobalUnlock(hMem);
85
+ const setR = SetClipboardData(13 /* CF_UNICODETEXT */, hMem);
86
+ t.checkTrue('SetClipboardData (ownership -> system)', setR !== 0);
87
+ CloseClipboard();
88
+ t.checkTrue('OpenClipboard (read back)', OpenClipboard(NULL) !== 0);
89
+ const hRead = GetClipboardData(13);
90
+ t.checkTrue('GetClipboardData handle', hRead !== 0);
91
+ const pRead = hRead ? GlobalLock(hRead) : 0;
92
+ const back = pRead ? WCHAR.decode(pRead) : '';
93
+ if (pRead)
94
+ GlobalUnlock(hRead);
95
+ CloseClipboard();
96
+ t.check('clip text roundtrip', clipText, back);
97
+ t.checkTrue('RegisterClipboardFormat', RegisterClipboardFormat('QuickWinClipFmt') !== 0);
98
+ t.section('version resource of kernel32.dll');
99
+ const sysW = WCHAR.alloc(300);
100
+ GetSystemDirectory(sysW, 300); // 尾不带反斜杠
101
+ const k32Path = `${WCHAR.decode(sysW)}\\kernel32.dll`;
102
+ const hB = new PtrArrayBuffer(4);
103
+ const vSize = GetFileVersionInfoSize(k32Path, hB);
104
+ t.checkTrue(`version size > 0 (${vSize})`, vSize > 0);
105
+ if (vSize > 0) {
106
+ const vHandle = new Uint32Array(hB)[0];
107
+ const vBuf = new PtrArrayBuffer(vSize);
108
+ t.checkTrue('GetFileVersionInfo', GetFileVersionInfo(k32Path, vHandle, vSize, vBuf) !== 0);
109
+ const pBuf = new PtrArrayBuffer(8);
110
+ const pLen = new PtrArrayBuffer(4);
111
+ t.checkTrue('VerQueryValue root block', VerQueryValue(vBuf, '\\', pBuf, pLen) !== 0);
112
+ const rootAddr = readPtr(new DataView(pBuf), 0);
113
+ const rootLen = new Uint32Array(pLen)[0];
114
+ t.checkTrue(`VS_FIXEDFILEINFO len >= 52 (${rootLen})`, rootLen >= 52);
115
+ t.check('VS_FIXEDFILEINFO signature', 0xfeef04bd, readU32At(rootAddr));
116
+ const fms = readU32At(rootAddr + 8);
117
+ const fls = readU32At(rootAddr + 12);
118
+ const ver = `${fms >>> 16}.${fms & 0xffff}.${fls >>> 16}.${fls & 0xffff}`;
119
+ t.checkTrue(`file version sane (${ver})`, fms !== 0 || fls !== 0);
120
+ }
121
+ },
122
+ };
@@ -0,0 +1,141 @@
1
+ import * as ffi from 'ffi'
2
+ import * as os from 'os'
3
+ import { WCHAR } from '../lib/ffi/bind.js'
4
+ import { NULL, PtrArrayBuffer, type Ptr } from '../lib/ffi/ctype.js'
5
+ import {
6
+ FormatMessage, GetCommandLineW, GetComputerName, GetEnvironmentVariable,
7
+ GetLocalTime, GetModuleFileName, GetSystemDirectory, GetSystemTimeAsFileTime,
8
+ GlobalAlloc, GlobalLock, GlobalUnlock, QueryPerformanceCounter,
9
+ QueryPerformanceFrequency, RtlMoveMemory, Sleep,
10
+ } from '../lib/windows/kernel32.js'
11
+ import {
12
+ CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard,
13
+ RegisterClipboardFormat, SetClipboardData,
14
+ } from '../lib/windows/user32.js'
15
+ import {
16
+ GetFileVersionInfoSize, GetFileVersionInfo, VerQueryValue,
17
+ } from '../lib/windows/version.js'
18
+ import { SYSTEMTIME } from '../lib/windows/structs.js'
19
+ import { Tester } from './test_helper.js'
20
+
21
+ // 进程内存跟进读:ffi 只有 readByte(无 DataView/写通道),u32 小端拼值 / 指针随位宽
22
+ // (| 是有符号 32 位运算,>>> 0 归一无符号——0xFEEF04BD 类高位值否则读成负数)
23
+ function readU32At(addr: number): number {
24
+ return (ffi.readByte(addr) | (ffi.readByte(addr + 1) << 8) |
25
+ (ffi.readByte(addr + 2) << 16) | (ffi.readByte(addr + 3) << 24)) >>> 0
26
+ }
27
+ function readPtr(dv: DataView, offset: number): number {
28
+ if (os.arch === 'ia32') return dv.getUint32(offset, true)
29
+ return dv.getUint32(offset, true) + dv.getUint32(offset + 4, true) * 4294967296
30
+ }
31
+
32
+ export const suite = {
33
+ name: 'sysinfo',
34
+ run: (t: Tester) => {
35
+ t.section('QueryPerformanceCounter: frequency + monotonic')
36
+ const freqB = new PtrArrayBuffer(8)
37
+ t.checkTrue('QueryPerformanceFrequency ok', QueryPerformanceFrequency(freqB) !== 0)
38
+ const freq = new DataView(freqB).getBigInt64(0, true)
39
+ t.checkTrue(`frequency sane (${freq})`, freq > 0n)
40
+ const c1 = new PtrArrayBuffer(8)
41
+ const c2 = new PtrArrayBuffer(8)
42
+ QueryPerformanceCounter(c1)
43
+ QueryPerformanceCounter(c2)
44
+ const v1 = new DataView(c1).getBigInt64(0, true)
45
+ const v2 = new DataView(c2).getBigInt64(0, true)
46
+ t.checkTrue(`counter monotonic (${v1} <= ${v2})`, v1 <= v2)
47
+
48
+ t.section('Sleep: QPC-measured elapsed lands in interval')
49
+ QueryPerformanceCounter(c1)
50
+ Sleep(50)
51
+ QueryPerformanceCounter(c2)
52
+ const elapsedMs = Number((new DataView(c2).getBigInt64(0, true) -
53
+ new DataView(c1).getBigInt64(0, true)) * 1000n / freq)
54
+ t.checkTrue(`Sleep(50) -> ${elapsedMs}ms in [30, 3000)`, elapsedMs >= 30 && elapsedMs < 3000)
55
+
56
+ t.section('GetLocalTime / GetSystemTimeAsFileTime')
57
+ const st = SYSTEMTIME.alloc()
58
+ GetLocalTime(st.ptr)
59
+ const got = SYSTEMTIME.decode(st)
60
+ t.checkTrue(`year sane (${got.wYear})`, got.wYear >= 2024 && got.wYear <= 2100)
61
+ t.checkTrue(`month sane (${got.wMonth})`, got.wMonth >= 1 && got.wMonth <= 12)
62
+ const ftB = new PtrArrayBuffer(8)
63
+ GetSystemTimeAsFileTime(ftB)
64
+ const ft = new DataView(ftB).getBigUint64(0, true)
65
+ t.checkTrue(`FILETIME > 0 (${ft})`, ft > 0n)
66
+
67
+ t.section('env / computer name / module path')
68
+ const pathW = WCHAR.alloc(4096)
69
+ const pathLen = GetEnvironmentVariable('PATH', pathW, 4096)
70
+ t.checkTrue(`PATH non-empty (${pathLen} chars)`, pathLen > 0)
71
+ const nameW = WCHAR.alloc(260)
72
+ const nameLenB = new PtrArrayBuffer(4)
73
+ new Uint32Array(nameLenB)[0] = 260
74
+ t.checkTrue('GetComputerName ok', GetComputerName(nameW, nameLenB) !== 0)
75
+ const compName = WCHAR.decode(nameW)
76
+ t.checkTrue(`computer name non-empty (${compName})`, compName.length > 0)
77
+ const modW = WCHAR.alloc(520)
78
+ const modLen = GetModuleFileName(NULL, modW, 520)
79
+ const modPath = WCHAR.decode(modW)
80
+ t.checkTrue(`exe path has qwin (${modLen > 0})`, /qwin/i.test(modPath))
81
+ // 生产绑定的返回位 `<WCHAR>ptr!`:内建 WCHAR 自动解码 + `!` 非空担保(直接 string)
82
+ const cmdLine = GetCommandLineW()
83
+ t.checkTrue(`GetCommandLineW decodes to non-empty string (${cmdLine.length} chars)`,
84
+ typeof cmdLine === 'string' && cmdLine.length > 0)
85
+
86
+ t.section('FormatMessage: error 2 -> readable text')
87
+ const msgW = WCHAR.alloc(512)
88
+ const msgLen = FormatMessage(0x1200 /* FROM_SYSTEM | IGNORE_INSERTS */,
89
+ NULL, 2 /* ERROR_FILE_NOT_FOUND */, 0, msgW, 512, NULL)
90
+ // 语言无关断言(VM 中/英文系统都可过):错误 2 的系统文字必然 > 4 字符
91
+ t.checkTrue(`error 2 message non-empty (${msgLen} chars)`, msgLen > 4)
92
+
93
+ t.section('clipboard roundtrip: CF_UNICODETEXT (GlobalAlloc/RtlMoveMemory)')
94
+ t.checkTrue('OpenClipboard', OpenClipboard(NULL) !== 0)
95
+ t.checkTrue('EmptyClipboard', EmptyClipboard() !== 0)
96
+ const clipText = 'QuickWin clip roundtrip 你好'
97
+ const clipSrc = WCHAR.encode(clipText) // 自带 NUL 终止
98
+ const hMem = GlobalAlloc(0x2 /* GMEM_MOVEABLE */, clipSrc.byteLength)
99
+ t.checkTrue(`GlobalAlloc valid (${hMem})`, hMem !== 0)
100
+ const clipDst = GlobalLock(hMem)
101
+ t.checkTrue(`GlobalLock address (${clipDst})`, clipDst !== 0)
102
+ RtlMoveMemory(clipDst, clipSrc, clipSrc.byteLength)
103
+ GlobalUnlock(hMem)
104
+ const setR = SetClipboardData(13 /* CF_UNICODETEXT */, hMem)
105
+ t.checkTrue('SetClipboardData (ownership -> system)', setR !== 0)
106
+ CloseClipboard()
107
+ t.checkTrue('OpenClipboard (read back)', OpenClipboard(NULL) !== 0)
108
+ const hRead = GetClipboardData(13)
109
+ t.checkTrue('GetClipboardData handle', hRead !== 0)
110
+ const pRead = hRead ? GlobalLock(hRead) : 0
111
+ const back = pRead ? WCHAR.decode(pRead as Ptr<'WCHAR'>) : ''
112
+ if (pRead) GlobalUnlock(hRead)
113
+ CloseClipboard()
114
+ t.check('clip text roundtrip', clipText, back)
115
+ t.checkTrue('RegisterClipboardFormat', RegisterClipboardFormat('QuickWinClipFmt') !== 0)
116
+
117
+ t.section('version resource of kernel32.dll')
118
+ const sysW = WCHAR.alloc(300)
119
+ GetSystemDirectory(sysW, 300) // 尾不带反斜杠
120
+ const k32Path = `${WCHAR.decode(sysW)}\\kernel32.dll`
121
+ const hB = new PtrArrayBuffer(4)
122
+ const vSize = GetFileVersionInfoSize(k32Path, hB)
123
+ t.checkTrue(`version size > 0 (${vSize})`, vSize > 0)
124
+ if (vSize > 0) {
125
+ const vHandle = new Uint32Array(hB)[0]!
126
+ const vBuf = new PtrArrayBuffer(vSize)
127
+ t.checkTrue('GetFileVersionInfo', GetFileVersionInfo(k32Path, vHandle, vSize, vBuf) !== 0)
128
+ const pBuf = new PtrArrayBuffer(8)
129
+ const pLen = new PtrArrayBuffer(4)
130
+ t.checkTrue('VerQueryValue root block', VerQueryValue(vBuf, '\\', pBuf, pLen) !== 0)
131
+ const rootAddr = readPtr(new DataView(pBuf), 0)
132
+ const rootLen = new Uint32Array(pLen)[0]
133
+ t.checkTrue(`VS_FIXEDFILEINFO len >= 52 (${rootLen})`, rootLen! >= 52)
134
+ t.check('VS_FIXEDFILEINFO signature', 0xfeef04bd, readU32At(rootAddr))
135
+ const fms = readU32At(rootAddr + 8)
136
+ const fls = readU32At(rootAddr + 12)
137
+ const ver = `${fms >>> 16}.${fms & 0xffff}.${fls >>> 16}.${fls & 0xffff}`
138
+ t.checkTrue(`file version sane (${ver})`, fms !== 0 || fls !== 0)
139
+ }
140
+ },
141
+ }
@@ -19,7 +19,8 @@ export const suite = {
19
19
  std.printf(' FAIL: %s\n', name);
20
20
  }
21
21
  }
22
- // ── wolfSSL_read return contract: ArrayBuffer | null, never number ──
22
+ // ── wolfSSL_read return contract: raw int, data lands in the
23
+ // caller-owned buffer (thin wrapper — errors stay numbers) ──
23
24
  t.section('wolfSSL_read contract (HTTPS /large/65536)');
24
25
  await new Promise((resolve) => {
25
26
  const s = sock.socket();
@@ -35,10 +36,9 @@ export const suite = {
35
36
  let sentRequest = false;
36
37
  let total = 0;
37
38
  const EXPECTED = 65536;
38
- let sawNumber = false;
39
- let sawArray = false;
40
- let sawNull = false;
41
- let nullErrOk = true;
39
+ let sawNonNumber = false;
40
+ let sawEof = false;
41
+ let errOk = true;
42
42
  let done = false;
43
43
  const finish = () => {
44
44
  if (done)
@@ -48,10 +48,10 @@ export const suite = {
48
48
  assert('connected', connected);
49
49
  assert('handshake ok', handshakeDone);
50
50
  assert('request sent', sentRequest);
51
- assert('received some data', sawArray && total > 0);
52
- assert('wolfSSL_read never returned number', !sawNumber);
53
- assert('wolfSSL_read returned null on no-data/EOF', sawNull);
54
- assert('null path get_error in {WANT_READ,WANT_WRITE,ZERO_RETURN}', nullErrOk);
51
+ assert('received full body', total >= EXPECTED);
52
+ assert('wolfSSL_read always returned number', !sawNonNumber);
53
+ assert('no-data/EOF observed via int (0 or ZERO_RETURN)', sawEof);
54
+ assert('n<=0 get_error in {WANT_READ,WANT_WRITE,ZERO_RETURN}', errOk);
55
55
  if (ssl) {
56
56
  wolfssl.wolfSSL_free(ssl);
57
57
  ssl = null;
@@ -77,38 +77,39 @@ export const suite = {
77
77
  sentRequest = true;
78
78
  };
79
79
  const onReadable = () => {
80
- // Drain until content complete or null
80
+ // Drain until EOF (n===0 / ZERO_RETURN) or WANT_* hands back
81
+ // to the event loop.
81
82
  for (let guard = 0; guard < 10000; guard++) {
82
- const d = wolfssl.wolfSSL_read(ssl, 8192);
83
- if (d === null) {
84
- sawNull = true;
85
- const err = wolfssl.wolfSSL_get_error(ssl, -1);
86
- if (err !== WANT_READ && err !== WANT_WRITE && err !== ZERO_RETURN) {
87
- nullErrOk = false;
88
- std.printf(' null get_error=%d\n', err);
89
- }
90
- if (err === ZERO_RETURN || err === 1 || err === 5) {
91
- // EOF / fatal — done draining
92
- break;
93
- }
94
- // WANT_*: wait for next FD_READ/WRITE
83
+ const buf = new ArrayBuffer(8192);
84
+ const n = wolfssl.wolfSSL_read(ssl, buf);
85
+ if (typeof n !== 'number') {
86
+ sawNonNumber = true;
87
+ std.printf(' FAIL got non-number %s (want int)\n', String(n));
88
+ finish();
95
89
  return;
96
90
  }
97
- if (typeof d === 'number') {
98
- // Pre-fix C API returns number 0/-1 — record and stop
99
- sawNumber = true;
100
- std.printf(' FAIL got number %d (want ArrayBuffer|null)\n', d);
91
+ if (n > 0) {
92
+ total += n;
93
+ // Keep draining after the body is complete: the point of
94
+ // this suite is the no-data/EOF contract, so a full body
95
+ // must not short-circuit the read that would observe it.
96
+ continue;
97
+ }
98
+ const err = wolfssl.wolfSSL_get_error(ssl, n);
99
+ if (err !== WANT_READ && err !== WANT_WRITE && err !== ZERO_RETURN) {
100
+ errOk = false;
101
+ std.printf(' n=%d get_error=%d outside {WANT_READ,WANT_WRITE,ZERO_RETURN}\n', n, err);
101
102
  finish();
102
103
  return;
103
104
  }
104
- if (d instanceof ArrayBuffer) {
105
- sawArray = true;
106
- total += d.byteLength;
107
- if (total >= EXPECTED)
108
- break;
105
+ if (n === 0 || err === ZERO_RETURN) {
106
+ sawEof = true;
107
+ break;
109
108
  }
109
+ // WANT_*: wait for next FD_READ/WRITE
110
+ return;
110
111
  }
111
- if (total >= EXPECTED)
112
+ if (sawEof)
112
113
  finish();
113
114
  };
114
115
  sock.set_on_event(s, (event) => {
@@ -177,21 +178,22 @@ export const suite = {
177
178
  }
178
179
  if (event.lNetworkEvents & 32 /* sock.FdEvent.FD_CLOSE */) {
179
180
  // drain remaining then finish
180
- if (ssl && sentRequest && !sawNumber) {
181
+ if (ssl && sentRequest && !sawNonNumber) {
181
182
  onReadable();
182
183
  }
183
- if (!sawNumber) {
184
- // Even if short, require null contract seen OR full body
185
- if (!sawNull && total < EXPECTED && total > 0) {
186
- // got data but no null — still OK if full body; if short, null expected
187
- sawNull = true; // server close without extra null read is edge; don't fail
184
+ if (!sawNonNumber) {
185
+ // Server close without an observed EOF read is an edge —
186
+ // don't fail if the body already completed.
187
+ if (!sawEof && total < EXPECTED && total > 0) {
188
+ sawEof = true;
188
189
  }
189
- if (total >= EXPECTED || sawNull)
190
+ if (total >= EXPECTED || sawEof)
190
191
  finish();
191
- else
192
+ else {
192
193
  assert('data before close', false);
193
- if (!done)
194
- finish();
194
+ if (!done)
195
+ finish();
196
+ }
195
197
  }
196
198
  }
197
199
  });