quickwin 1.3.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/README.en.md +1 -2
  2. package/README.md +19 -23
  3. package/examples/PdfCanvas.js +8 -37
  4. package/examples/PdfCanvas.tsx +11 -41
  5. package/examples/exec_server.js +20054 -1423
  6. package/examples/exec_server.tsx +489 -0
  7. package/examples/exec_server_worker.js +383 -33
  8. package/examples/exec_server_worker.ts +408 -30
  9. package/examples/pdf_preview2.js +25 -82
  10. package/examples/pdf_preview2.ts +27 -84
  11. package/examples/pdf_viewer.js +1318 -1178
  12. package/examples/pdf_viewer.tsx +16 -67
  13. package/examples/setres.js +22 -47
  14. package/examples/setres.ts +23 -57
  15. package/examples/test_gallery.js +1313 -1074
  16. package/examples/test_react_lazy.js +1284 -1056
  17. package/examples/test_react_listbox.js +1283 -1055
  18. package/examples/test_root_window.js +1279 -1051
  19. package/examples/test_tab.js +1284 -1056
  20. package/lib/abort.js +70 -0
  21. package/lib/abort.ts +69 -0
  22. package/lib/certs.js +214 -0
  23. package/lib/certs.ts +224 -0
  24. package/lib/exec-log.js +15 -0
  25. package/lib/exec-log.ts +30 -0
  26. package/lib/fetch.js +37 -4
  27. package/lib/fetch.ts +35 -4
  28. package/lib/ffi/bind.js +352 -0
  29. package/lib/ffi/bind.ts +442 -0
  30. package/lib/ffi/ctype.js +168 -0
  31. package/lib/ffi/ctype.ts +331 -0
  32. package/lib/ffi/index.js +6 -0
  33. package/lib/ffi/index.ts +6 -0
  34. package/lib/ffi/struct.js +412 -0
  35. package/lib/ffi/struct.ts +664 -0
  36. package/lib/http-common.js +43 -5
  37. package/lib/http-common.ts +48 -6
  38. package/lib/http-server.js +187 -29
  39. package/lib/http-server.ts +177 -26
  40. package/lib/react-qw/components/DateTimePicker.js +18 -37
  41. package/lib/react-qw/components/Input.js +3 -10
  42. package/lib/react-qw/components/Link.js +6 -19
  43. package/lib/react-qw/components/ListView.js +49 -154
  44. package/lib/react-qw/components/PathPicker.js +28 -88
  45. package/lib/react-qw/components/ScrollView.js +15 -8
  46. package/lib/react-qw/components/Slider.js +2 -1
  47. package/lib/react-qw/components/Tab.js +6 -22
  48. package/lib/react-qw/components/Tooltip.js +10 -21
  49. package/lib/react-qw/components/TreeView.js +8 -31
  50. package/lib/react-qw/index.js +4 -2
  51. package/lib/react-qw/index.ts +4 -2
  52. package/lib/react-qw/nmhdr.js +3 -24
  53. package/lib/react-qw/nmhdr.ts +5 -30
  54. package/lib/react-qw/props.js +7 -2
  55. package/lib/react-qw/props.ts +7 -3
  56. package/lib/react-qw/reconciler.js +23 -13
  57. package/lib/react-qw/reconciler.ts +22 -13
  58. package/lib/text-measure.js +18 -23
  59. package/lib/text-measure.ts +20 -25
  60. package/lib/websocket.js +12 -2
  61. package/lib/websocket.ts +15 -2
  62. package/lib/windows/comctl32.js +18 -0
  63. package/lib/windows/comctl32.ts +20 -0
  64. package/lib/windows/comdlg32.js +7 -0
  65. package/lib/windows/comdlg32.ts +9 -0
  66. package/lib/windows/gdi32.js +90 -0
  67. package/lib/windows/gdi32.ts +97 -0
  68. package/lib/windows/kernel32.js +66 -0
  69. package/lib/windows/kernel32.ts +76 -0
  70. package/lib/windows/ntdll.js +8 -0
  71. package/lib/windows/ntdll.ts +10 -0
  72. package/lib/windows/ole32.js +7 -0
  73. package/lib/windows/ole32.ts +9 -0
  74. package/lib/windows/shell32.js +11 -0
  75. package/lib/windows/shell32.ts +14 -0
  76. package/lib/windows/structs.js +384 -0
  77. package/lib/windows/structs.ts +422 -0
  78. package/lib/windows/user32.js +469 -0
  79. package/lib/windows/user32.ts +502 -0
  80. package/package.json +1 -1
  81. package/quickwin.d.ts +22 -37
  82. package/quickwin_const.d.ts +1 -0
  83. package/qwin-nowasm-x86.exe +0 -0
  84. package/qwin-nowasm.exe +0 -0
  85. package/qwin-x86.exe +0 -0
  86. package/qwin.exe +0 -0
  87. package/test/bench_ffi_callraw.js +236 -0
  88. package/test/bench_ffi_callraw.ts +236 -0
  89. package/test/bench_ffi_overhead.js +139 -0
  90. package/test/bench_ffi_overhead.ts +141 -0
  91. package/test/bench_sendmessage.js +104 -0
  92. package/test/bench_sendmessage.ts +100 -0
  93. package/test/bench_sendmessage_nonnull.js +187 -0
  94. package/test/bench_sendmessage_nonnull.ts +188 -0
  95. package/test/run.js +12 -4
  96. package/test/run.ts +13 -5
  97. package/test/test_basic_fetch.js +8 -0
  98. package/test/test_basic_fetch.ts +9 -0
  99. package/test/test_ffi.js +25 -43
  100. package/test/test_ffi.ts +33 -52
  101. package/test/test_ffi_abi.js +235 -0
  102. package/test/test_ffi_abi.ts +218 -0
  103. package/test/test_ffi_bind.js +538 -8
  104. package/test/test_ffi_bind.ts +559 -8
  105. package/test/test_ffi_struct.js +632 -88
  106. package/test/test_ffi_struct.ts +666 -91
  107. package/test/test_ffi_struct_layout.js +495 -0
  108. package/test/test_ffi_struct_layout.ts +543 -0
  109. package/test/test_ffi_user32.js +103 -0
  110. package/test/test_ffi_user32.ts +113 -0
  111. package/test/test_ffi_windows.js +101 -0
  112. package/test/test_ffi_windows.ts +113 -0
  113. package/test/test_helper.js +8 -1
  114. package/test/test_helper.ts +9 -1
  115. package/test/test_http_import.js +18 -0
  116. package/test/test_http_import.ts +19 -0
  117. package/test/test_http_server.js +73 -1
  118. package/test/test_http_server.ts +75 -1
  119. package/test/test_ipv6.js +2 -2
  120. package/test/test_ipv6.ts +2 -2
  121. package/test/test_net_fetch.js +70 -8
  122. package/test/test_net_fetch.ts +69 -7
  123. package/test/test_net_websocket.js +1 -1
  124. package/test/test_net_websocket.ts +1 -1
  125. package/test/test_react_complex.js +1294 -1066
  126. package/test/test_react_counter.js +1284 -1056
  127. package/test/test_react_flex.js +1304 -1069
  128. package/test/test_react_hidden.js +1287 -1059
  129. package/test/test_react_listview_link.js +1284 -1056
  130. package/test/test_react_notify.js +18534 -0
  131. package/test/test_react_render.js +1284 -1056
  132. package/test/test_wolfssl_read.js +3 -2
  133. package/test/test_wolfssl_read.ts +3 -1
  134. package/test/test_worker_net.js +35 -17
  135. package/test/test_worker_net.ts +27 -15
  136. package/tsconfig.json +1 -1
  137. package/examples/exec_server.ts +0 -80
  138. package/lib/ffi-bind.js +0 -85
  139. package/lib/ffi-bind.ts +0 -170
  140. package/lib/ffi-struct.js +0 -224
  141. package/lib/ffi-struct.ts +0 -239
  142. package/lib/win-common-structs.js +0 -39
  143. package/lib/win-common-structs.ts +0 -41
@@ -104,8 +104,9 @@ export const suite = {
104
104
  if (d instanceof ArrayBuffer) {
105
105
  sawArray = true;
106
106
  total += d.byteLength;
107
- if (total >= EXPECTED)
108
- break;
107
+ // Keep draining after the body is complete: the point of
108
+ // this suite is the no-data/EOF contract, so a full body
109
+ // must not short-circuit the read that would observe null.
109
110
  }
110
111
  }
111
112
  if (total >= EXPECTED)
@@ -99,7 +99,9 @@ export const suite = {
99
99
  if (d instanceof ArrayBuffer) {
100
100
  sawArray = true
101
101
  total += d.byteLength
102
- if (total >= EXPECTED) break
102
+ // Keep draining after the body is complete: the point of
103
+ // this suite is the no-data/EOF contract, so a full body
104
+ // must not short-circuit the read that would observe null.
103
105
  }
104
106
  }
105
107
  if (total >= EXPECTED) finish()
@@ -27,25 +27,43 @@ export const suite = {
27
27
  catch (e) {
28
28
  t.checkTrue('worker creation failed: ' + e, false);
29
29
  }
30
+ // Retry across jsdelivr's official CDN hosts: a dropped connection
31
+ // on a slow CI network must not be reported as a TLS regression.
30
32
  t.section('npm-package');
31
33
  let worker3;
32
- try {
33
- const { data: r3 } = await new Promise((resolve, reject) => {
34
- worker3 = new os.Worker('https://cdn.jsdelivr.net/npm/quickwin/test/worker_module.js');
35
- const timer = os.setTimeout(() => reject(new Error('timeout')), 15000);
36
- worker3.onmessage = (e) => {
37
- os.clearTimeout(timer);
38
- resolve({ worker: worker3, data: e.data });
39
- };
40
- worker3.postMessage({ type: 'start' });
41
- });
42
- t.check('npm worker message type', 'result', r3.type);
43
- t.check('npm worker imported value', 42, r3.value);
44
- worker3.postMessage({ type: 'done' });
45
- worker3.onmessage = null;
46
- }
47
- catch (e) {
48
- t.checkTrue('npm worker failed: ' + e, false);
34
+ const NPM_HOSTS = ['cdn.jsdelivr.net', 'fastly.jsdelivr.net', 'gcore.jsdelivr.net'];
35
+ let workerOk = false;
36
+ for (const host of NPM_HOSTS) {
37
+ for (let attempt = 0; attempt < 3 && !workerOk; attempt++) {
38
+ try {
39
+ const { data: r3 } = await new Promise((resolve, reject) => {
40
+ worker3 = new os.Worker('https://' + host + '/npm/quickwin/test/worker_module.js');
41
+ const timer = os.setTimeout(() => reject(new Error('timeout')), 15000);
42
+ worker3.onmessage = (e) => {
43
+ os.clearTimeout(timer);
44
+ resolve({ worker: worker3, data: e.data });
45
+ };
46
+ worker3.postMessage({ type: 'start' });
47
+ });
48
+ t.check('npm worker message type', 'result', r3.type);
49
+ t.check('npm worker imported value', 42, r3.value);
50
+ worker3.postMessage({ type: 'done' });
51
+ worker3.onmessage = null;
52
+ workerOk = true;
53
+ }
54
+ catch (e) {
55
+ if (worker3) {
56
+ worker3.onmessage = null;
57
+ worker3 = null;
58
+ }
59
+ if (attempt === 2 && host === NPM_HOSTS[NPM_HOSTS.length - 1])
60
+ t.checkTrue('npm worker failed: ' + e, false);
61
+ else
62
+ os.sleep(500);
63
+ }
64
+ }
65
+ if (workerOk)
66
+ break;
49
67
  }
50
68
  }
51
69
  };
@@ -31,24 +31,36 @@ export const suite = {
31
31
  t.checkTrue('worker creation failed: ' + e, false)
32
32
  }
33
33
 
34
+ // Retry across jsdelivr's official CDN hosts: a dropped connection
35
+ // on a slow CI network must not be reported as a TLS regression.
34
36
  t.section('npm-package')
35
37
  let worker3: any
36
- try {
37
- const { data: r3 } = await new Promise<any>((resolve, reject) => {
38
- worker3 = new os.Worker('https://cdn.jsdelivr.net/npm/quickwin/test/worker_module.js')
39
- const timer = os.setTimeout(() => reject(new Error('timeout')), 15000)
40
- worker3.onmessage = (e: any) => {
41
- os.clearTimeout(timer)
42
- resolve({ worker: worker3, data: e.data })
38
+ const NPM_HOSTS = ['cdn.jsdelivr.net', 'fastly.jsdelivr.net', 'gcore.jsdelivr.net']
39
+ let workerOk = false
40
+ for (const host of NPM_HOSTS) {
41
+ for (let attempt = 0; attempt < 3 && !workerOk; attempt++) {
42
+ try {
43
+ const { data: r3 } = await new Promise<any>((resolve, reject) => {
44
+ worker3 = new os.Worker('https://' + host + '/npm/quickwin/test/worker_module.js')
45
+ const timer = os.setTimeout(() => reject(new Error('timeout')), 15000)
46
+ worker3.onmessage = (e: any) => {
47
+ os.clearTimeout(timer)
48
+ resolve({ worker: worker3, data: e.data })
49
+ }
50
+ worker3.postMessage({ type: 'start' })
51
+ })
52
+ t.check('npm worker message type', 'result', r3.type)
53
+ t.check('npm worker imported value', 42, r3.value)
54
+ worker3.postMessage({ type: 'done' })
55
+ worker3.onmessage = null
56
+ workerOk = true
57
+ } catch (e) {
58
+ if (worker3) { worker3.onmessage = null; worker3 = null }
59
+ if (attempt === 2 && host === NPM_HOSTS[NPM_HOSTS.length - 1]) t.checkTrue('npm worker failed: ' + e, false)
60
+ else os.sleep(500)
43
61
  }
44
- worker3.postMessage({ type: 'start' })
45
- })
46
- t.check('npm worker message type', 'result', r3.type)
47
- t.check('npm worker imported value', 42, r3.value)
48
- worker3.postMessage({ type: 'done' })
49
- worker3.onmessage = null
50
- } catch (e) {
51
- t.checkTrue('npm worker failed: ' + e, false)
62
+ }
63
+ if (workerOk) break
52
64
  }
53
65
  }
54
66
  }
package/tsconfig.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "esModuleInterop": true,
16
16
  // "skipLibCheck": true,
17
17
  "allowJs": false,
18
- "lib": ["ES2020"],
18
+ "lib": ["ES2025"],
19
19
  "types": ["./quickwin.d.ts"],
20
20
  "rootDir": ".",
21
21
  "outDir": "_build",
@@ -1,80 +0,0 @@
1
- import { createServer } from '../lib/http-server.js'
2
- import * as os from 'os'
3
-
4
- // WORKER_DATA_URL 由 esbuild --define 在 build.ts 中注入(base64 data: URL)
5
- declare const WORKER_DATA_URL: string
6
-
7
- const EXEC_TIMEOUT_MS = 60_000
8
-
9
- interface ExecResult {
10
- out: Uint8Array
11
- code: number
12
- error: string | null
13
- }
14
-
15
- function runInWorker(cmd: string): Promise<ExecResult> {
16
- return new Promise((resolve, reject) => {
17
- const worker = new os.Worker(WORKER_DATA_URL)
18
- const id = 1
19
- let settled = false
20
- let timer: ReturnType<typeof os.setTimeout>
21
-
22
- const finish = (fn: () => void) => {
23
- if (settled) return
24
- settled = true
25
- os.clearTimeout(timer)
26
- worker.onmessage = null
27
- fn()
28
- }
29
-
30
- timer = os.setTimeout(() => {
31
- finish(() => reject(new Error('exec timeout')))
32
- }, EXEC_TIMEOUT_MS)
33
-
34
- worker.onmessage = (e) => {
35
- const msg = e.data as { type: string; id: number; out?: Uint8Array; code?: number; error?: string | null }
36
- if (msg.type !== 'result' || msg.id !== id) return
37
- finish(() => resolve({ out: msg.out ?? new Uint8Array(0), code: msg.code ?? -1, error: msg.error ?? null }))
38
- }
39
-
40
- try {
41
- worker.postMessage({ type: 'run', id, cmd })
42
- } catch (ex) {
43
- finish(() => reject(ex instanceof Error ? ex : new Error(String(ex))))
44
- }
45
- })
46
- }
47
-
48
- const server = createServer(async (req) => {
49
- const path = new URL(req.url).pathname
50
- if (req.method === 'GET' && (path === '/' || path === '/health')) {
51
- return new Response('ok', { headers: { 'Content-Type': 'text/plain' } })
52
- }
53
- if (req.method === 'POST' && path === '/exec') {
54
- const body = await req.json() as { cmd: string }
55
- try {
56
- const { out, code, error } = await runInWorker(body.cmd)
57
- if (error) {
58
- return new Response(JSON.stringify({ error }), {
59
- status: 500,
60
- headers: { 'Content-Type': 'application/json' }
61
- })
62
- }
63
- return new Response(out, {
64
- headers: {
65
- 'Content-Type': 'application/octet-stream',
66
- 'X-Exit-Code': String(code),
67
- }
68
- })
69
- } catch (ex) {
70
- return new Response(JSON.stringify({ error: String(ex) }), {
71
- status: 504,
72
- headers: { 'Content-Type': 'application/json' }
73
- })
74
- }
75
- }
76
- return new Response('Not Found', { status: 404 })
77
- })
78
-
79
- const rc = server.listen(8080, '0.0.0.0')
80
- console.log('listen rc=' + rc + ' addr=' + JSON.stringify(server.address()))
package/lib/ffi-bind.js DELETED
@@ -1,85 +0,0 @@
1
- import * as ffi from 'ffi';
2
- import * as win from 'win';
3
- import './text-codec.js';
4
- const KIND_TO_FFI = {
5
- void: ffi.FFI_TYPE_VOID,
6
- u8: ffi.FFI_TYPE_UINT8,
7
- i8: ffi.FFI_TYPE_SINT8,
8
- u16: ffi.FFI_TYPE_UINT16,
9
- i16: ffi.FFI_TYPE_SINT16,
10
- u32: ffi.FFI_TYPE_UINT32,
11
- i32: ffi.FFI_TYPE_SINT32,
12
- u64: ffi.FFI_TYPE_UINT64,
13
- i64: ffi.FFI_TYPE_SINT64,
14
- f32: ffi.FFI_TYPE_FLOAT,
15
- f64: ffi.FFI_TYPE_DOUBLE,
16
- ptr: ffi.FFI_TYPE_POINTER,
17
- wstr: ffi.FFI_TYPE_POINTER,
18
- };
19
- // 与类型层 CTypeOf 同步的运行时别名表
20
- const C_ALIAS = {
21
- int: 'i32', long: 'i32', short: 'i16', char: 'i8', float: 'f32', double: 'f64',
22
- DWORD: 'u32', UINT: 'u32', ULONG: 'u32', LONG: 'i32', BOOL: 'i32', HRESULT: 'i32',
23
- SHORT: 'i16', USHORT: 'u16', BYTE: 'u8',
24
- LONG_PTR: 'ptr', ULONG_PTR: 'ptr', INT_PTR: 'ptr', UINT_PTR: 'ptr', DWORD_PTR: 'ptr',
25
- SIZE_T: 'ptr', WPARAM: 'ptr', LPARAM: 'ptr',
26
- HANDLE: 'ptr', HWND: 'ptr', HDC: 'ptr', HMODULE: 'ptr', HFONT: 'ptr', HBRUSH: 'ptr',
27
- HICON: 'ptr', HBITMAP: 'ptr', LPVOID: 'ptr', LPCVOID: 'ptr', LPCWSTR: 'ptr',
28
- PCWSTR: 'ptr', LPWSTR: 'ptr',
29
- };
30
- export function normKind(t) {
31
- const k = C_ALIAS[t] ?? t;
32
- if (!(k in KIND_TO_FFI))
33
- throw new Error(`ffi-bind: invalid kind "${t}"`);
34
- return k;
35
- }
36
- const _dllCache = new Map();
37
- function loadDll(dll) {
38
- const cached = _dllCache.get(dll);
39
- if (cached !== undefined)
40
- return cached;
41
- const loaded = win.LoadLibrary(dll);
42
- if (!loaded)
43
- throw new Error(`ffi-bind: LoadLibrary("${dll}") failed`);
44
- _dllCache.set(dll, loaded);
45
- return loaded;
46
- }
47
- function parseSig(sig) {
48
- const parts = sig.split(' -> ');
49
- const ret = parts[1];
50
- if (ret === undefined || parts.length > 2) {
51
- throw new Error(`ffi-bind: invalid signature "${sig}" (expected "arg1 arg2 -> ret")`);
52
- }
53
- const retK = normKind(ret);
54
- if (retK === 'wstr')
55
- throw new Error('ffi-bind: "wstr" cannot be a return type');
56
- const tokens = (parts[0] ?? '') === '' ? [] : parts[0].split(' ');
57
- const args = tokens.map((t) => normKind(t));
58
- return { args, ret: retK };
59
- }
60
- function makeFn(proc, sig) {
61
- const { args, ret } = parseSig(sig);
62
- const argTypes = args.map((k) => KIND_TO_FFI[k]);
63
- const retType = KIND_TO_FFI[ret];
64
- return (...a) => {
65
- const vals = args.map((k, i) => k === 'wstr' ? new TextEncoder('utf-16le').encode(String(a[i]) + '\0').buffer : a[i]);
66
- return ffi.ffiCall(proc, argTypes, vals, retType);
67
- };
68
- }
69
- export function bind(dll, name, sig) {
70
- const proc = win.GetProcAddress(loadDll(dll), name);
71
- if (!proc)
72
- throw new Error(`ffi-bind: proc "${name}" not found in ${dll}`);
73
- return makeFn(proc, sig);
74
- }
75
- export function bindLib(dll, map) {
76
- const out = {};
77
- const h = loadDll(dll);
78
- for (const [name, sig] of Object.entries(map)) {
79
- const proc = win.GetProcAddress(h, name);
80
- if (!proc)
81
- throw new Error(`ffi-bind: proc "${name}" not found in ${dll}`);
82
- out[name] = makeFn(proc, sig);
83
- }
84
- return out;
85
- }
package/lib/ffi-bind.ts DELETED
@@ -1,170 +0,0 @@
1
- import * as ffi from 'ffi'
2
- import * as win from 'win'
3
- import './text-codec.js'
4
-
5
- // 声明式 FFI 绑定:把字符串签名('ptr wstr i32 ptr i32 -> i32')解析成
6
- // 一个可调用函数(bind)或一个 dll 的签名表(bindLib)。
7
- // kind:void u8 i8 u16 i16 u32 i32 u64 i64 f32 f64 ptr wstr
8
- // 可用 C/Windows typedef 别名:int long short char float double
9
- // + DWORD UINT LONG BOOL HRESULT ... + *_PTR WPARAM LPARAM SIZE_T
10
- // + HANDLE HWND HDC ... (+ LPVOID/LPCWSTR 等指针 typedef)
11
- // 别名在 token 层归一化到上面规范 kind(见 CTypeOf / C_ALIAS,两者须同步)
12
- // ptr 收 ArrayBuffer | number | null,槽宽=指针宽(ia32 4B / x64 8B)
13
- // wstr 收 string,自动 utf-16le+'\0' 编码后走 ptr 槽(仅参数,不能作返回)
14
-
15
- type Kind = 'void' | 'u8' | 'i8' | 'u16' | 'i16' | 'u32' | 'i32' | 'u64' | 'i64' | 'f32' | 'f64' | 'ptr' | 'wstr'
16
-
17
- type ArgTypeOf = {
18
- u8: number; i8: number; u16: number; i16: number
19
- u32: number; i32: number
20
- u64: number; i64: number
21
- f32: number; f64: number
22
- ptr: ArrayBuffer | number | null
23
- wstr: string
24
- }
25
- type RetTypeOf = {
26
- u8: number; i8: number; u16: number; i16: number
27
- u32: number; i32: number
28
- u64: number; i64: number
29
- f32: number; f64: number
30
- ptr: number | null
31
- }
32
-
33
- // C / Windows typedef → 规范 kind。Windows x86/x64 均 LLP64:int/long 恒 32 位,
34
- // long long 恒 64 位;LONG_PTR/WPARAM/SIZE_T 等指针宽随 arch 走 'ptr' 槽。
35
- // 运行时见 C_ALIAS(两表内容必须保持一致)。
36
- type CTypeOf = {
37
- int: 'i32'
38
- long: 'i32'
39
- short: 'i16'
40
- char: 'i8'
41
- float: 'f32'
42
- double: 'f64'
43
- DWORD: 'u32'
44
- UINT: 'u32'
45
- ULONG: 'u32'
46
- LONG: 'i32'
47
- BOOL: 'i32'
48
- HRESULT: 'i32'
49
- SHORT: 'i16'
50
- USHORT: 'u16'
51
- BYTE: 'u8'
52
- LONG_PTR: 'ptr'
53
- ULONG_PTR: 'ptr'
54
- INT_PTR: 'ptr'
55
- UINT_PTR: 'ptr'
56
- DWORD_PTR: 'ptr'
57
- SIZE_T: 'ptr'
58
- WPARAM: 'ptr'
59
- LPARAM: 'ptr'
60
- HANDLE: 'ptr'
61
- HWND: 'ptr'
62
- HDC: 'ptr'
63
- HMODULE: 'ptr'
64
- HFONT: 'ptr'
65
- HBRUSH: 'ptr'
66
- HICON: 'ptr'
67
- HBITMAP: 'ptr'
68
- LPVOID: 'ptr'
69
- LPCVOID: 'ptr'
70
- LPCWSTR: 'ptr'
71
- PCWSTR: 'ptr'
72
- LPWSTR: 'ptr'
73
- }
74
-
75
- export type Norm<T extends string> = T extends keyof CTypeOf ? CTypeOf[T] : T
76
-
77
- type ArgsOf<T extends string> =
78
- T extends '' ? []
79
- : T extends `${infer H} ${infer R}` ? [ArgTypeOf[Norm<H> & keyof ArgTypeOf], ...ArgsOf<R>]
80
- : [ArgTypeOf[Norm<T> & keyof ArgTypeOf]]
81
-
82
- type _Args<S extends string> = S extends `${infer A} -> ${string}` ? ArgsOf<A> : never
83
- type _Ret<S extends string> = S extends `${string} -> ${infer R}` ? RetTypeOf[Norm<R> & keyof RetTypeOf] : never
84
-
85
- const KIND_TO_FFI: Record<Kind, ffi.FfiType> = {
86
- void: ffi.FFI_TYPE_VOID,
87
- u8: ffi.FFI_TYPE_UINT8,
88
- i8: ffi.FFI_TYPE_SINT8,
89
- u16: ffi.FFI_TYPE_UINT16,
90
- i16: ffi.FFI_TYPE_SINT16,
91
- u32: ffi.FFI_TYPE_UINT32,
92
- i32: ffi.FFI_TYPE_SINT32,
93
- u64: ffi.FFI_TYPE_UINT64,
94
- i64: ffi.FFI_TYPE_SINT64,
95
- f32: ffi.FFI_TYPE_FLOAT,
96
- f64: ffi.FFI_TYPE_DOUBLE,
97
- ptr: ffi.FFI_TYPE_POINTER,
98
- wstr: ffi.FFI_TYPE_POINTER,
99
- }
100
-
101
- // 与类型层 CTypeOf 同步的运行时别名表
102
- const C_ALIAS: Record<string, Kind> = {
103
- int: 'i32', long: 'i32', short: 'i16', char: 'i8', float: 'f32', double: 'f64',
104
- DWORD: 'u32', UINT: 'u32', ULONG: 'u32', LONG: 'i32', BOOL: 'i32', HRESULT: 'i32',
105
- SHORT: 'i16', USHORT: 'u16', BYTE: 'u8',
106
- LONG_PTR: 'ptr', ULONG_PTR: 'ptr', INT_PTR: 'ptr', UINT_PTR: 'ptr', DWORD_PTR: 'ptr',
107
- SIZE_T: 'ptr', WPARAM: 'ptr', LPARAM: 'ptr',
108
- HANDLE: 'ptr', HWND: 'ptr', HDC: 'ptr', HMODULE: 'ptr', HFONT: 'ptr', HBRUSH: 'ptr',
109
- HICON: 'ptr', HBITMAP: 'ptr', LPVOID: 'ptr', LPCVOID: 'ptr', LPCWSTR: 'ptr',
110
- PCWSTR: 'ptr', LPWSTR: 'ptr',
111
- }
112
-
113
- export function normKind(t: string): Kind {
114
- const k = C_ALIAS[t] ?? t
115
- if (!(k in KIND_TO_FFI)) throw new Error(`ffi-bind: invalid kind "${t}"`)
116
- return k as Kind
117
- }
118
-
119
- const _dllCache: Map<string, win.HMODULE> = new Map()
120
-
121
- function loadDll(dll: string): win.HMODULE {
122
- const cached = _dllCache.get(dll)
123
- if (cached !== undefined) return cached
124
- const loaded = win.LoadLibrary(dll)
125
- if (!loaded) throw new Error(`ffi-bind: LoadLibrary("${dll}") failed`)
126
- _dllCache.set(dll, loaded)
127
- return loaded
128
- }
129
-
130
- function parseSig(sig: string): { args: Kind[]; ret: Kind } {
131
- const parts = sig.split(' -> ')
132
- const ret = parts[1]
133
- if (ret === undefined || parts.length > 2) {
134
- throw new Error(`ffi-bind: invalid signature "${sig}" (expected "arg1 arg2 -> ret")`)
135
- }
136
- const retK = normKind(ret)
137
- if (retK === 'wstr') throw new Error('ffi-bind: "wstr" cannot be a return type')
138
- const tokens = (parts[0] ?? '') === '' ? [] : (parts[0] as string).split(' ')
139
- const args = tokens.map((t) => normKind(t))
140
- return { args, ret: retK }
141
- }
142
-
143
- function makeFn(proc: number, sig: string): (...a: unknown[]) => unknown {
144
- const { args, ret } = parseSig(sig)
145
- const argTypes = args.map((k) => KIND_TO_FFI[k])
146
- const retType = KIND_TO_FFI[ret]
147
- return (...a: unknown[]) => {
148
- const vals = args.map((k, i) =>
149
- k === 'wstr' ? new TextEncoder('utf-16le').encode(String(a[i]) + '\0').buffer : a[i])
150
- return ffi.ffiCall(proc, argTypes as any, vals as any, retType)
151
- }
152
- }
153
-
154
- export function bind<const S extends string>(dll: string, name: string, sig: S): (...args: _Args<S>) => _Ret<S> {
155
- const proc = win.GetProcAddress(loadDll(dll), name)
156
- if (!proc) throw new Error(`ffi-bind: proc "${name}" not found in ${dll}`)
157
- return makeFn(proc, sig) as unknown as (...args: _Args<S>) => _Ret<S>
158
- }
159
-
160
- export function bindLib<const M extends Record<string, string>>(dll: string, map: M):
161
- { [K in keyof M]: (...args: _Args<M[K]>) => _Ret<M[K]> } {
162
- const out: Record<string, (...a: unknown[]) => unknown> = {}
163
- const h = loadDll(dll)
164
- for (const [name, sig] of Object.entries(map)) {
165
- const proc = win.GetProcAddress(h, name)
166
- if (!proc) throw new Error(`ffi-bind: proc "${name}" not found in ${dll}`)
167
- out[name] = makeFn(proc, sig)
168
- }
169
- return out as { [K in keyof M]: (...args: _Args<M[K]>) => _Ret<M[K]> }
170
- }