quickwin 2.0.0 → 2.1.1

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 (103) hide show
  1. package/README.en.md +21 -26
  2. package/README.md +3 -6
  3. package/examples/exec_server.js +139 -31
  4. package/examples/exec_server.tsx +7 -8
  5. package/examples/exec_server_worker.js +1 -1
  6. package/examples/exec_server_worker.ts +2 -2
  7. package/examples/pdf_preview2.js +1 -1
  8. package/examples/pdf_preview2.ts +1 -1
  9. package/examples/pdf_viewer.js +132 -28
  10. package/examples/pdf_viewer.tsx +1 -1
  11. package/examples/test_gallery.js +131 -27
  12. package/examples/test_react_lazy.js +131 -27
  13. package/examples/test_react_listbox.js +131 -27
  14. package/examples/test_root_window.js +131 -27
  15. package/examples/test_tab.js +131 -27
  16. package/lib/certs.js +13 -8
  17. package/lib/certs.ts +13 -8
  18. package/lib/fetch.js +51 -3
  19. package/lib/fetch.ts +38 -3
  20. package/lib/ffi/bind.js +94 -5
  21. package/lib/ffi/bind.ts +107 -10
  22. package/lib/ffi/ctype.js +33 -7
  23. package/lib/ffi/ctype.ts +48 -10
  24. package/lib/ffi/struct.js +92 -14
  25. package/lib/ffi/struct.ts +108 -19
  26. package/lib/react-qw/components/ListView.js +1 -0
  27. package/lib/react-qw/components/PathPicker.js +2 -2
  28. package/lib/react-qw/index.js +1 -1
  29. package/lib/react-qw/index.ts +4 -1
  30. package/lib/text-measure.js +3 -1
  31. package/lib/text-measure.ts +4 -3
  32. package/lib/websocket.js +32 -2
  33. package/lib/websocket.ts +25 -2
  34. package/lib/windows/advapi32.js +53 -0
  35. package/lib/windows/advapi32.ts +64 -0
  36. package/lib/windows/comctl32.js +15 -15
  37. package/lib/windows/comctl32.ts +15 -16
  38. package/lib/windows/comdlg32.js +3 -2
  39. package/lib/windows/comdlg32.ts +3 -2
  40. package/lib/windows/gdi32.js +328 -26
  41. package/lib/windows/gdi32.ts +349 -26
  42. package/lib/windows/kernel32.js +206 -10
  43. package/lib/windows/kernel32.ts +225 -10
  44. package/lib/windows/msimg32.js +38 -0
  45. package/lib/windows/msimg32.ts +45 -0
  46. package/lib/windows/ntdll.js +3 -2
  47. package/lib/windows/ntdll.ts +3 -2
  48. package/lib/windows/ole32.js +1 -1
  49. package/lib/windows/ole32.ts +1 -1
  50. package/lib/windows/psapi.js +32 -0
  51. package/lib/windows/psapi.ts +37 -0
  52. package/lib/windows/shell32.js +5 -3
  53. package/lib/windows/shell32.ts +5 -3
  54. package/lib/windows/structs.js +135 -0
  55. package/lib/windows/structs.ts +144 -0
  56. package/lib/windows/user32.js +202 -99
  57. package/lib/windows/user32.ts +204 -99
  58. package/lib/windows/version.js +29 -0
  59. package/lib/windows/version.ts +35 -0
  60. package/lib/windows/winspool.js +71 -0
  61. package/lib/windows/winspool.ts +80 -0
  62. package/package.json +1 -1
  63. package/quickwin.d.ts +11 -5
  64. package/quickwin_const.d.ts +136 -0
  65. package/qwin-nowasm-x86.exe +0 -0
  66. package/qwin-nowasm.exe +0 -0
  67. package/qwin-x86.exe +0 -0
  68. package/qwin.exe +0 -0
  69. package/test/run.js +7 -0
  70. package/test/run.ts +7 -0
  71. package/test/test_basic_fetch.js +21 -0
  72. package/test/test_basic_fetch.ts +17 -0
  73. package/test/test_ffi.js +6 -6
  74. package/test/test_ffi.ts +6 -6
  75. package/test/test_ffi_abi.js +55 -0
  76. package/test/test_ffi_abi.ts +56 -0
  77. package/test/test_ffi_array.js +116 -0
  78. package/test/test_ffi_array.ts +141 -0
  79. package/test/test_ffi_bind.js +82 -0
  80. package/test/test_ffi_bind.ts +71 -0
  81. package/test/test_files.js +102 -0
  82. package/test/test_files.ts +121 -0
  83. package/test/test_gdi_draw.js +681 -0
  84. package/test/test_gdi_draw.ts +725 -0
  85. package/test/test_net_fetch.js +4 -3
  86. package/test/test_net_fetch.ts +4 -3
  87. package/test/test_printer.js +137 -0
  88. package/test/test_printer.ts +148 -0
  89. package/test/test_proc.js +141 -0
  90. package/test/test_proc.ts +163 -0
  91. package/test/test_react_complex.js +131 -27
  92. package/test/test_react_counter.js +131 -27
  93. package/test/test_react_flex.js +131 -27
  94. package/test/test_react_hidden.js +131 -27
  95. package/test/test_react_listview_link.js +131 -27
  96. package/test/test_react_notify.js +131 -27
  97. package/test/test_react_render.js +131 -27
  98. package/test/test_registry.js +74 -0
  99. package/test/test_registry.ts +95 -0
  100. package/test/test_sysinfo.js +122 -0
  101. package/test/test_sysinfo.ts +141 -0
  102. package/test/test_wolfssl_read.js +45 -44
  103. package/test/test_wolfssl_read.ts +42 -42
package/lib/ffi/struct.ts CHANGED
@@ -214,13 +214,18 @@ export type StructDef<T extends { '#': 'struct' | 'union' }, N extends string =
214
214
  // 数组元素含必填 ⇒ 该键必给,值后序合成到内层)。带 buf = 写入调用方既有
215
215
  // PtrArrayBuffer(返回其本身,供链式读回)——encode 出口一律 PAB,裸 ArrayBuffer
216
216
  // 不出 codec(与 CodecMap 同约束)。动态调用方无参 encode 由运行时 throw 兜底。
217
- alloc(): PtrArrayBuffer<N>
217
+ /** n(默认 1)= 按本布局 size 步长连续分配 n 个元素的零填充缓冲(出参数组场景,
218
+ * 品牌 .ptr 直接喂 <N>ptr 位)。n < 1 / 非整数运行时 throw ——
219
+ * 任何路径都不产出 PAB(0)(其 .ptr 是"非 0 的 1 字节堆指针",会骗过 C 的 NULL
220
+ * 防御;空位语义由调用方显式传 NULL)。 */
221
+ alloc(n?: number): PtrArrayBuffer<N>
218
222
  encode(v: EncodeIn<T>): PtrArrayBuffer<N>
219
- // 带 buf = 写入调用方既有 PtrArrayBuffer(复用/预分配形态,返回其本身供链式读回)。
220
- // 刻意没有 offset —— 与 decode 同理(bind.ts CodecMap 注释):PAB 的 .ptr 恒指向起点,
221
- // 顶层再收 offset 只会掩盖"写错位置"且返回值 .ptr 对不上写入点;嵌套偏移由布局字段
222
- // (doEncode 的 base)承担。
223
- encode(v: EncodeIn<T>, buf: PtrArrayBuffer<any>): PtrArrayBuffer<any>
223
+ // 带 buf = 写入调用方既有 PtrArrayBuffer(复用/预分配形态);带 offset = 写入点
224
+ // 相对 buf 起点的字节偏移(数组物化/自定义内存管理的底层写入原语,内部即 doEncode
225
+ // 的 base)。返回值仍是 buf 本身 —— offset 形态下其 .ptr 对不上写入点,写入点地址
226
+ // = buf.ptr + offset(自算术,品牌随算术丢失同 CreatePipe 先例);越界写由 DataView
227
+ // RangeError 兜底,无需额外校验。
228
+ encode(v: EncodeIn<T>, buf: PtrArrayBuffer<any>, offset?: number): PtrArrayBuffer<any>
224
229
  offsetOf(name: string): number
225
230
  }
226
231
 
@@ -592,27 +597,31 @@ function createStruct(t: C_Struct | C_Union): any {
592
597
  size,
593
598
  structAlign: maxEffectiveAlign,
594
599
  decode: (p: ArrayBuffer | number) => {
595
- // number = native 拥有的品牌指针(Codec 的 decode(p) 形态):按布局 size
596
- // 逐字节拷进临时 buffer 再解码;ArrayBuffer = 调用方持有的 out buffer 直读。
600
+ // number = native 拥有的品牌指针(Codec 的 decode(p) 形态):readBytes 一次
601
+ // native memcpy 拷出 size 字节再解码(替代逐字节 readByte 循环,N 次跨语言
602
+ // 调用降为 1 次);ArrayBuffer = 调用方持有的 out buffer 直读。
597
603
  // 顶层不收 offset——out 参数恒从起点读,嵌套偏移由字段布局(doDecode 的 base)承担。
598
- if (typeof p === 'number') {
599
- const buf = new ArrayBuffer(size)
600
- const u8 = new Uint8Array(buf)
601
- for (let i = 0; i < size; i++) u8[i] = ffi.readByte(p + i)
602
- return doDecode(new DataView(buf), 0, fields)
603
- }
604
+ if (typeof p === 'number')
605
+ return doDecode(new DataView(ffi.readBytes(p, size)), 0, fields)
604
606
  return doDecode(new DataView(p), 0, fields)
605
607
  },
606
- alloc: (() => new PtrArrayBuffer(size)) as StructDef<any, any>['alloc'],
607
- encode: ((v: any, buf?: PtrArrayBuffer<any>) => {
608
+ alloc: ((n?: number) => {
609
+ // fail-loud:n < 1 / 非整数绝不静默产出 PAB(0)(其 .ptr 是"非 0 的 1 字节
610
+ // 堆指针",会骗过 C 的 NULL 防御)—— 空位语义由调用方显式传 NULL。
611
+ if (n !== undefined && (!Number.isInteger(n) || n < 1))
612
+ throw new Error(`ffi-struct: alloc(n) requires an integer n >= 1 ` +
613
+ `(PAB(0) never escapes — pass NULL for an empty pointer position)`)
614
+ return new PtrArrayBuffer(size * (n ?? 1))
615
+ }) as StructDef<any, any>['alloc'],
616
+ encode: ((v: any, buf?: PtrArrayBuffer<any>, offset?: number) => {
608
617
  // fail-loud:无参 = 分配语义,走 alloc();这里绝不静默零填充 —— 类型层由
609
- // EncodeIn 拦 '!' 字段省略,动态/JS 调用方由本 throw 兜住。顶层不收 offset
610
- // (与 decode 对齐,见 StructDef 注释),恒从 buffer 起点写。
618
+ // EncodeIn 拦 '!' 字段省略,动态/JS 调用方由本 throw 兜住。顶层默认从起点写,
619
+ // offset 显式给写入点(数组物化原语,见接口注释)。
611
620
  if (v === undefined || v === null)
612
621
  throw new Error(`ffi-struct: encode(v) requires an argument; ` +
613
622
  `use alloc() for a zeroed out-param slot`)
614
623
  const out = buf ?? new PtrArrayBuffer(size)
615
- doEncode(new DataView(out), 0, fields, v)
624
+ doEncode(new DataView(out), offset ?? 0, fields, v)
616
625
  return out
617
626
  }) as StructDef<any, any>['encode'],
618
627
  offsetOf: (name: string) => {
@@ -640,6 +649,86 @@ function buildDecl(kind: 'struct' | 'union', member: Record<string, unknown>, pa
640
649
  return decl as unknown as C_Struct | C_Union
641
650
  }
642
651
 
652
+ /** decode 双态重载:有 byteLength 的 buffer 形 count 可缺省(自算);裸地址无长度
653
+ * 信息,count 必填(类型层拦,缺省调用编译红)。 */
654
+ type StructArrayDecode<T extends { '#': 'struct' | 'union' }, N extends string> = {
655
+ (p: ArrayBuffer, count?: number): ShapeOfC<T>[]
656
+ (p: Ptr<N>, count: number): ShapeOfC<T>[]
657
+ }
658
+
659
+ /** 结构/联合体的数组编解码对工厂:返回 `{ encode, decode }`。
660
+ * encode —— 单值 encoder 的数组版:收元素数组 → 按本布局 size 步长紧密排列
661
+ * (stride = size,已含尾部对齐 padding),返回带品牌 .ptr 的 PAB —— 与 C 的
662
+ * `const T apt[]` 同构。值域刻意收普通数组(调用方持有的 `const pts = [...]` 变量
663
+ * 直传,非空元组会拒之);[] / 非数组 / 单对象运行时 throw —— 任何路径都不产出
664
+ * PAB(0)(其 .ptr 是"非 0 的 1 字节堆指针",会骗过 C 的 NULL 防御;空位语义由
665
+ * 调用方显式传 NULL)。注册进 bind 的 encoders 表(`{ POINT: structArray(POINT) }`)
666
+ * 后,普通 `<POINT>ptr` 位的值域即元素数组(值域由 encoder 推导,token/引擎/类型层
667
+ * 零数组概念);直接调用 `.encode(els).ptr` 亦可。
668
+ * decode —— 出参数组读回(与 def.decode 的 ArrayBuffer/地址双态对称):
669
+ * - buffer 形(含 PAB):count? 缺省自算 byteLength/size —— 要求整数倍,% ≠ 0
670
+ * throw(混合缓冲如 EnumPrinters 的"结构 + 尾部字符串"必须显式 count);显式
671
+ * count 校验 count*size ≤ byteLength(写错 count → 越界读的防线,buf 形可校才
672
+ * 校;不验品牌,与 def.decode 的 buffer 形同款)。
673
+ * - 裸地址形:count 必填(地址无长度信息),逐字节拷读(def.decode 的 number 形
674
+ * 同机制);长度不可校 → 信任 C 的 count。
675
+ * - count = 0 → [](读 0 个真空安全,与 encode 禁空的 PAB(0) 理由刻意不对称)。
676
+ * 实现:encode 走纯公开 API(alloc + encode offset 循环);decode 在本文件内复用
677
+ * computeStructLayout/doDecode。 */
678
+ export function structArray<T extends { '#': 'struct' | 'union' }, N extends string = ''>(
679
+ def: StructDef<T, N>,
680
+ ) {
681
+ const { fields, size } = computeStructLayout(def.__struct)
682
+ const readN = (dv: DataView, n: number): unknown[] => {
683
+ const out: unknown[] = []
684
+ for (let i = 0; i < n; i++) out.push(doDecode(dv, i * size, fields))
685
+ return out
686
+ }
687
+ return {
688
+ encode: (els: readonly EncodeIn<T>[]): PtrArrayBuffer<N> => {
689
+ if (!Array.isArray(els) || els.length === 0)
690
+ throw new Error(`ffi-struct: structArray(def).encode(els) requires a ` +
691
+ `non-empty element array (pass NULL for an empty pointer position)`)
692
+ const out = def.alloc(els.length)
693
+ for (let i = 0; i < els.length; i++) def.encode(els[i], out, i * def.size)
694
+ return out
695
+ },
696
+ decode: ((p: ArrayBuffer | number, count?: number): unknown[] => {
697
+ // fail-loud 校验矩阵(见 JSDoc):显式 count 信调用方但拦越界;缺省只对
698
+ // 有 byteLength 的 buffer 形自算;裸地址必带 count。
699
+ if (count !== undefined && (!Number.isInteger(count) || count < 0))
700
+ throw new Error(`ffi-struct: structArray.decode(p, count) requires a ` +
701
+ `non-negative integer count, got ${count}`)
702
+ if (typeof p === 'number') {
703
+ if (count === undefined)
704
+ throw new Error(`ffi-struct: structArray.decode(address, count) ` +
705
+ `requires an explicit count (a bare address carries no length)`)
706
+ if (count === 0) return []
707
+ // readBytes 一次 native memcpy 拷出整段再解码(def.decode 的 number 形同机制)。
708
+ return readN(new DataView(ffi.readBytes(p, count * size)), count)
709
+ }
710
+ let n: number
711
+ if (count !== undefined) {
712
+ n = count
713
+ // 越界防线:写错 count 是越界读的唯一入口,buf 形有 byteLength 就校死。
714
+ if (n * size > p.byteLength)
715
+ throw new Error(`ffi-struct: structArray.decode: count ${n} × ` +
716
+ `size ${size} = ${n * size} exceeds buffer byteLength ${p.byteLength}`)
717
+ } else {
718
+ // 自算形态:byteLength 必须是纯元素序列(size = 0 的退化布局同样落此
719
+ // throw —— 0 % 0 = NaN),混合缓冲显式传 count。
720
+ if (p.byteLength % size !== 0)
721
+ throw new Error(`ffi-struct: structArray.decode: byteLength ` +
722
+ `${p.byteLength} is not a multiple of size ${size} ` +
723
+ `(mixed buffer? pass count explicitly)`)
724
+ n = p.byteLength / size
725
+ }
726
+ if (n === 0) return []
727
+ return readN(new DataView(p), n)
728
+ }) as StructArrayDecode<T, N>,
729
+ }
730
+ }
731
+
643
732
  /** 平铺字段表定义结构体(token/`X[n]`/`unit:width`/`'#'` 声明均可,见 ctype.ts §4)。
644
733
  * 给 name 则 alloc().ptr 与 decode 入参带 Ptr<name> 品牌。 */
645
734
  type StructOf<M, N extends string = ''> = StructDef<{ '#': 'struct', '#pack'?: number } & M, N>
@@ -47,6 +47,7 @@ function getCellFont(hwnd, style) {
47
47
  const cur = gui.SendMessage(hwnd, 49 /* gui.WmMsg.GETFONT */, 0, 0);
48
48
  if (cur) {
49
49
  const lf = LOGFONTW.alloc();
50
+ // GETFONT 的 LRESULT 断言为 HFONT(gdi32 <HGDIOBJ>ptr 位)
50
51
  if (!GetObject(cur, LOGFONTW.size, lf))
51
52
  return null;
52
53
  base = LOGFONTW.decode(lf);
@@ -23,7 +23,7 @@ function openFileDialog(owner, filter, title, multiple) {
23
23
  lpstrTitle: titleWide?.ptr ?? 0,
24
24
  Flags: flags,
25
25
  });
26
- const ret = GetOpenFileName(ofn);
26
+ const ret = GetOpenFileName(ofn.ptr);
27
27
  if (!ret)
28
28
  return null;
29
29
  if (!multiple) {
@@ -42,7 +42,7 @@ function openFolderDialog(owner, title) {
42
42
  lpszTitle: titleWide?.ptr ?? 0,
43
43
  ulFlags: 0x00000041,
44
44
  });
45
- const pidl = SHBrowseForFolder(bi);
45
+ const pidl = SHBrowseForFolder(bi.ptr);
46
46
  if (!pidl)
47
47
  return null;
48
48
  const pathBuf = WCHAR.alloc(260);
@@ -50,7 +50,7 @@ export function createRoot(container) {
50
50
  else {
51
51
  hwnd = container;
52
52
  }
53
- const root = reconciler.createContainer(hwnd, 0, null, false, null, '', noop, noop, noop, noop);
53
+ const root = reconciler.createContainer(hwnd, 0, null, false, null, '', (error, info) => console.log('[React uncaught]', error, error.stack, info.componentStack), (error, info) => console.log('[React caught]', error, error.stack, info.componentStack), (error, info) => console.log('[React recoverable]', error, error.stack, info.componentStack), noop);
54
54
  const rootInst = { hwnd, type: '_root', props: { type: '_root', style: { flexDirection: 'column', alignItems: 'stretch' } }, children: [] };
55
55
  instancesByHwnd.set(hwnd, rootInst);
56
56
  if (typeof container === 'object')
@@ -64,7 +64,10 @@ export function createRoot(container: gui.HWND | RootWindowConfig) {
64
64
  }
65
65
  const root = reconciler.createContainer(
66
66
  hwnd, 0, null, false, null, '',
67
- noop, noop, noop, noop,
67
+ (error, info) => console.log('[React uncaught]', error, error.stack, info.componentStack),
68
+ (error, info) => console.log('[React caught]', error, error.stack, info.componentStack),
69
+ (error, info) => console.log('[React recoverable]', error, error.stack, info.componentStack),
70
+ noop,
68
71
  )
69
72
  const rootInst: Instance = { hwnd, type: '_root', props: { type: '_root', style: { flexDirection: 'column', alignItems: 'stretch' } }, children: [] }
70
73
  instancesByHwnd.set(hwnd, rootInst)
@@ -1,3 +1,4 @@
1
+ import { NULL } from './ffi/ctype.js';
1
2
  import * as gui from 'gui';
2
3
  import { DrawText, GetDC, ReleaseDC } from './windows/user32.js';
3
4
  import { SelectObject } from './windows/gdi32.js';
@@ -21,8 +22,9 @@ export function measureTextForHwnd(hwnd, text) {
21
22
  const hdc = GetDC(hwnd);
22
23
  if (!hdc)
23
24
  return { width: 0, height: 0 };
25
+ // SendMessage 返回 LRESULT 裸数;GETFONT 语义为 HFONT——断言进 GDI 对象品牌链
24
26
  const hFont = gui.SendMessage(hwnd, 49 /* gui.WmMsg.GETFONT */, 0, 0);
25
- const oldFont = hFont ? SelectObject(hdc, hFont) : 0;
27
+ const oldFont = hFont ? SelectObject(hdc, hFont) : NULL;
26
28
  const result = measureText(hdc, text, 0);
27
29
  if (hFont)
28
30
  SelectObject(hdc, oldFont);
@@ -1,4 +1,4 @@
1
- import type { Ptr } from './ffi/ctype.js'
1
+ import { NULL, type Ptr } from './ffi/ctype.js'
2
2
  import * as gui from 'gui'
3
3
  import { DrawText, GetDC, ReleaseDC } from './windows/user32.js'
4
4
  import { SelectObject } from './windows/gdi32.js'
@@ -24,8 +24,9 @@ export function getButtonIdealSize(hwnd: gui.HWND): { width: number; height: num
24
24
  export function measureTextForHwnd(hwnd: gui.HWND, text: string): { width: number; height: number } {
25
25
  const hdc = GetDC(hwnd)
26
26
  if (!hdc) return { width: 0, height: 0 }
27
- const hFont = gui.SendMessage(hwnd, gui.WmMsg.GETFONT, 0, 0)
28
- const oldFont = hFont ? SelectObject(hdc, hFont) : 0
27
+ // SendMessage 返回 LRESULT 裸数;GETFONT 语义为 HFONT——断言进 GDI 对象品牌链
28
+ const hFont = gui.SendMessage(hwnd, gui.WmMsg.GETFONT, 0, 0) as Ptr<'HGDIOBJ'>
29
+ const oldFont = hFont ? SelectObject(hdc, hFont) : NULL
29
30
  const result = measureText(hdc, text, 0)
30
31
  if (hFont) SelectObject(hdc, oldFont)
31
32
  ReleaseDC(hwnd, hdc)
package/lib/websocket.js CHANGED
@@ -345,7 +345,21 @@ class WebSocketImpl {
345
345
  while (true) {
346
346
  let data;
347
347
  if (isWSS && this._ssl) {
348
- data = wolfssl.wolfSSL_read(this._ssl, 8192);
348
+ const buf = new ArrayBuffer(8192);
349
+ const n = wolfssl.wolfSSL_read(this._ssl, buf);
350
+ if (n > 0) {
351
+ data = buf.slice(0, n);
352
+ }
353
+ else {
354
+ const err = wolfssl.wolfSSL_get_error(this._ssl, n);
355
+ if (err === 2 /* wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_READ */ ||
356
+ err === 3 /* wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_WRITE */)
357
+ break;
358
+ if (n === 0 || err === 6 /* wolfssl.ErrorCode.WOLFSSL_ERROR_ZERO_RETURN */)
359
+ break;
360
+ doError(new Error('TLS read failed: ' + err));
361
+ return;
362
+ }
349
363
  }
350
364
  else if (this._sock !== null && this._sock >= 0) {
351
365
  data = sock.recv(this._sock, 8192);
@@ -443,7 +457,23 @@ class WebSocketImpl {
443
457
  while (true) {
444
458
  let data;
445
459
  if (this._ssl) {
446
- data = wolfssl.wolfSSL_read(this._ssl, 8192);
460
+ const buf = new ArrayBuffer(8192);
461
+ const n = wolfssl.wolfSSL_read(this._ssl, buf);
462
+ if (n > 0) {
463
+ data = buf.slice(0, n);
464
+ }
465
+ else {
466
+ const err = wolfssl.wolfSSL_get_error(this._ssl, n);
467
+ if (err === 2 /* wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_READ */ ||
468
+ err === 3 /* wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_WRITE */)
469
+ break;
470
+ if (n === 0 || err === 6 /* wolfssl.ErrorCode.WOLFSSL_ERROR_ZERO_RETURN */)
471
+ break;
472
+ this._fireError(new Error('TLS read failed: ' + err));
473
+ if (this._state !== State.CLOSED)
474
+ this._setState(State.CLOSED);
475
+ return;
476
+ }
447
477
  }
448
478
  else if (this._sock !== null && this._sock >= 0) {
449
479
  data = sock.recv(this._sock, 8192);
package/lib/websocket.ts CHANGED
@@ -363,7 +363,18 @@ class WebSocketImpl {
363
363
  while (true) {
364
364
  let data: ArrayBuffer | null
365
365
  if (isWSS && this._ssl) {
366
- data = wolfssl.wolfSSL_read(this._ssl, 8192)
366
+ const buf = new ArrayBuffer(8192)
367
+ const n = wolfssl.wolfSSL_read(this._ssl, buf)
368
+ if (n > 0) {
369
+ data = buf.slice(0, n)
370
+ } else {
371
+ const err = wolfssl.wolfSSL_get_error(this._ssl, n)
372
+ if (err === wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_READ ||
373
+ err === wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_WRITE) break
374
+ if (n === 0 || err === wolfssl.ErrorCode.WOLFSSL_ERROR_ZERO_RETURN) break
375
+ doError(new Error('TLS read failed: ' + err))
376
+ return
377
+ }
367
378
  } else if (this._sock !== null && this._sock >= 0) {
368
379
  data = sock.recv(this._sock, 8192)
369
380
  } else { break }
@@ -460,7 +471,19 @@ class WebSocketImpl {
460
471
  while (true) {
461
472
  let data: ArrayBuffer | null
462
473
  if (this._ssl) {
463
- data = wolfssl.wolfSSL_read(this._ssl, 8192)
474
+ const buf = new ArrayBuffer(8192)
475
+ const n = wolfssl.wolfSSL_read(this._ssl, buf)
476
+ if (n > 0) {
477
+ data = buf.slice(0, n)
478
+ } else {
479
+ const err = wolfssl.wolfSSL_get_error(this._ssl, n)
480
+ if (err === wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_READ ||
481
+ err === wolfssl.ErrorCode.WOLFSSL_ERROR_WANT_WRITE) break
482
+ if (n === 0 || err === wolfssl.ErrorCode.WOLFSSL_ERROR_ZERO_RETURN) break
483
+ this._fireError(new Error('TLS read failed: ' + err))
484
+ if (this._state !== State.CLOSED) this._setState(State.CLOSED)
485
+ return
486
+ }
464
487
  } else if (this._sock !== null && this._sock >= 0) {
465
488
  data = sock.recv(this._sock, 8192)
466
489
  } else { break }
@@ -0,0 +1,53 @@
1
+ // advapi32 绑定(注册表 API;键名/值名一律 W 版 UTF-16)
2
+ import { bind } from '../ffi/bind.js';
3
+ // dll 名部分应用(工厂转调保泛型 infer,见 lib/windows/user32.ts 头注释)
4
+ const b = (name, sig) => bind('advapi32.dll', name, sig);
5
+ /** 创建/打开子键(子键不存在则创建,含中间层级)
6
+ * @returns 0 = ERROR_SUCCESS
7
+ * @param args_0 hKey 父键:gui.HKey.CURRENT_USER / LOCAL_MACHINE 预定义键,或本族
8
+ * Open/Create 返回的真实句柄(此位双来源,不标 @HKey——裸位收两种)
9
+ * @param args_1 lpSubKey 子键路径('Software\\X\\Y',可 NULL = 打开 hKey 自身副本)
10
+ * @param args_2 Reserved 保留,恒 0
11
+ * @param args_3 lpClass 键类名(一般 NULL)
12
+ * @param args_4 dwOptions 保留属性(恒 0)
13
+ * @param args_5 samDesired 访问掩码 gui.RegAccess.READ / SET_VALUE(标准组合常量按成员收录)
14
+ * @param args_6 lpSecurityAttributes 安全描述符(一般 NULL)
15
+ * @param args_7 phkResult 出参:键句柄(PtrArrayBuffer(8),readPtr 按架构读回)
16
+ * @param args_8 lpdwDisposition 出参:新建/已存在标记(可 NULL) */
17
+ export const RegCreateKeyEx = /*@__PURE__*/ b('RegCreateKeyExW', '<>ptr <WCHAR>ptr u32 <WCHAR>ptr u32 u32@RegAccess <>ptr <BYTE>ptr <BYTE>ptr -> i32');
18
+ /** 打开已有子键(不创建)
19
+ * @returns 0 = ERROR_SUCCESS;2 = ERROR_FILE_NOT_FOUND(子键不存在)
20
+ * @param args_0 hKey 父键(同 RegCreateKeyEx 的双来源语义)
21
+ * @param args_1 lpSubKey 子键路径(NULL = 打开 hKey 自身副本)
22
+ * @param args_2 ulOptions 保留,恒 0
23
+ * @param args_3 samDesired 访问掩码 gui.RegAccess.READ / SET_VALUE
24
+ * @param args_4 phkResult 出参:键句柄 */
25
+ export const RegOpenKeyEx = /*@__PURE__*/ b('RegOpenKeyExW', '<>ptr <WCHAR>ptr u32 u32@RegAccess <BYTE>ptr -> i32');
26
+ /** 关闭键句柄;只关 Open/Create 返回的真实句柄——预定义键(HKEY_CURRENT_USER 等)
27
+ * 不要关
28
+ * @returns 0 = ERROR_SUCCESS */
29
+ export const RegCloseKey = /*@__PURE__*/ b('RegCloseKey', '<>ptr -> i32');
30
+ /** 写键值
31
+ * @returns 0 = ERROR_SUCCESS
32
+ * @param args_0 hKey 键句柄(须 KEY_SET_VALUE 权限,含于 gui.RegAccess.SET_VALUE)
33
+ * @param args_1 lpValueName 值名(NULL = 键的默认值)
34
+ * @param args_2 Reserved 保留,恒 0
35
+ * @param args_3 dwType 值类型 gui.RegType.SZ(UTF-16 字符串)
36
+ * @param args_4 lpData 数据缓冲(REG_SZ:UTF-16LE 含结尾 NUL)
37
+ * @param args_5 cbData 数据字节数(REG_SZ = (字符数+1)*2) */
38
+ export const RegSetValueEx = /*@__PURE__*/ b('RegSetValueExW', '<>ptr <WCHAR>ptr u32 u32@RegType <BYTE>ptr u32 -> i32');
39
+ /** 读键值(两跳模式:第一跳 lpData=NULL 取尺寸/类型,第二跳按尺寸带缓冲读数据)
40
+ * @returns 0 = ERROR_SUCCESS;2 = ERROR_FILE_NOT_FOUND(值不存在)
41
+ * @param args_0 hKey 键句柄(须 KEY_QUERY_VALUE 权限,含于 gui.RegAccess.READ)
42
+ * @param args_1 lpValueName 值名(NULL = 键的默认值)
43
+ * @param args_2 lpReserved 保留,恒 NULL
44
+ * @param args_3 lpType 出参:值类型(可 NULL)
45
+ * @param args_4 lpData 数据缓冲(可 NULL = 只查尺寸)
46
+ * @param args_5 lpcbData 双向:入 = lpData 缓冲字节数(须预写)、出 = 实际字节数 */
47
+ export const RegQueryValueEx = /*@__PURE__*/ b('RegQueryValueExW', '<>ptr <WCHAR>ptr <>ptr <BYTE>ptr <BYTE>ptr <BYTE>ptr -> i32');
48
+ /** 删除子键(连值一起删);须父键 KEY_CREATE_SUB_KEY 权限——自己的 HKCU 直接传
49
+ * gui.HKey.CURRENT_USER 即可
50
+ * @returns 0 = ERROR_SUCCESS;2 = 子键不存在
51
+ * @param args_0 hKey 父键
52
+ * @param args_1 lpSubKey 要删的子键路径(不可 NULL) */
53
+ export const RegDeleteKey = /*@__PURE__*/ b('RegDeleteKeyW', '<>ptr <WCHAR>ptr -> i32');
@@ -0,0 +1,64 @@
1
+ // advapi32 绑定(注册表 API;键名/值名一律 W 版 UTF-16)
2
+ import { bind } from '../ffi/bind.js'
3
+
4
+ // dll 名部分应用(工厂转调保泛型 infer,见 lib/windows/user32.ts 头注释)
5
+ const b = <const S extends string>(name: string, sig: S) => bind('advapi32.dll', name, sig)
6
+
7
+ /** 创建/打开子键(子键不存在则创建,含中间层级)
8
+ * @returns 0 = ERROR_SUCCESS
9
+ * @param args_0 hKey 父键:gui.HKey.CURRENT_USER / LOCAL_MACHINE 预定义键,或本族
10
+ * Open/Create 返回的真实句柄(此位双来源,不标 @HKey——裸位收两种)
11
+ * @param args_1 lpSubKey 子键路径('Software\\X\\Y',可 NULL = 打开 hKey 自身副本)
12
+ * @param args_2 Reserved 保留,恒 0
13
+ * @param args_3 lpClass 键类名(一般 NULL)
14
+ * @param args_4 dwOptions 保留属性(恒 0)
15
+ * @param args_5 samDesired 访问掩码 gui.RegAccess.READ / SET_VALUE(标准组合常量按成员收录)
16
+ * @param args_6 lpSecurityAttributes 安全描述符(一般 NULL)
17
+ * @param args_7 phkResult 出参:键句柄(PtrArrayBuffer(8),readPtr 按架构读回)
18
+ * @param args_8 lpdwDisposition 出参:新建/已存在标记(可 NULL) */
19
+ export const RegCreateKeyEx = /*@__PURE__*/ b('RegCreateKeyExW',
20
+ '<>ptr <WCHAR>ptr u32 <WCHAR>ptr u32 u32@RegAccess <>ptr <BYTE>ptr <BYTE>ptr -> i32')
21
+
22
+ /** 打开已有子键(不创建)
23
+ * @returns 0 = ERROR_SUCCESS;2 = ERROR_FILE_NOT_FOUND(子键不存在)
24
+ * @param args_0 hKey 父键(同 RegCreateKeyEx 的双来源语义)
25
+ * @param args_1 lpSubKey 子键路径(NULL = 打开 hKey 自身副本)
26
+ * @param args_2 ulOptions 保留,恒 0
27
+ * @param args_3 samDesired 访问掩码 gui.RegAccess.READ / SET_VALUE
28
+ * @param args_4 phkResult 出参:键句柄 */
29
+ export const RegOpenKeyEx = /*@__PURE__*/ b('RegOpenKeyExW',
30
+ '<>ptr <WCHAR>ptr u32 u32@RegAccess <BYTE>ptr -> i32')
31
+
32
+ /** 关闭键句柄;只关 Open/Create 返回的真实句柄——预定义键(HKEY_CURRENT_USER 等)
33
+ * 不要关
34
+ * @returns 0 = ERROR_SUCCESS */
35
+ export const RegCloseKey = /*@__PURE__*/ b('RegCloseKey', '<>ptr -> i32')
36
+
37
+ /** 写键值
38
+ * @returns 0 = ERROR_SUCCESS
39
+ * @param args_0 hKey 键句柄(须 KEY_SET_VALUE 权限,含于 gui.RegAccess.SET_VALUE)
40
+ * @param args_1 lpValueName 值名(NULL = 键的默认值)
41
+ * @param args_2 Reserved 保留,恒 0
42
+ * @param args_3 dwType 值类型 gui.RegType.SZ(UTF-16 字符串)
43
+ * @param args_4 lpData 数据缓冲(REG_SZ:UTF-16LE 含结尾 NUL)
44
+ * @param args_5 cbData 数据字节数(REG_SZ = (字符数+1)*2) */
45
+ export const RegSetValueEx = /*@__PURE__*/ b('RegSetValueExW',
46
+ '<>ptr <WCHAR>ptr u32 u32@RegType <BYTE>ptr u32 -> i32')
47
+
48
+ /** 读键值(两跳模式:第一跳 lpData=NULL 取尺寸/类型,第二跳按尺寸带缓冲读数据)
49
+ * @returns 0 = ERROR_SUCCESS;2 = ERROR_FILE_NOT_FOUND(值不存在)
50
+ * @param args_0 hKey 键句柄(须 KEY_QUERY_VALUE 权限,含于 gui.RegAccess.READ)
51
+ * @param args_1 lpValueName 值名(NULL = 键的默认值)
52
+ * @param args_2 lpReserved 保留,恒 NULL
53
+ * @param args_3 lpType 出参:值类型(可 NULL)
54
+ * @param args_4 lpData 数据缓冲(可 NULL = 只查尺寸)
55
+ * @param args_5 lpcbData 双向:入 = lpData 缓冲字节数(须预写)、出 = 实际字节数 */
56
+ export const RegQueryValueEx = /*@__PURE__*/ b('RegQueryValueExW',
57
+ '<>ptr <WCHAR>ptr <>ptr <BYTE>ptr <BYTE>ptr <BYTE>ptr -> i32')
58
+
59
+ /** 删除子键(连值一起删);须父键 KEY_CREATE_SUB_KEY 权限——自己的 HKCU 直接传
60
+ * gui.HKey.CURRENT_USER 即可
61
+ * @returns 0 = ERROR_SUCCESS;2 = 子键不存在
62
+ * @param args_0 hKey 父键
63
+ * @param args_1 lpSubKey 要删的子键路径(不可 NULL) */
64
+ export const RegDeleteKey = /*@__PURE__*/ b('RegDeleteKeyW', '<>ptr <WCHAR>ptr -> i32')
@@ -1,18 +1,18 @@
1
- // comctl32 绑定(ImageList 系列;HIMAGELIST 为不透明句柄保持 <>ptr)
1
+ // comctl32 绑定(ImageList 系列;HIMAGELIST 不透明句柄走 <HIMAGELIST>ptr 自产自收,
2
+ // Add 的 hbm 位与 gdi32 的 <HGDIOBJ>ptr 同型互认——CreateBitmap 产物直传)
2
3
  import { bind } from '../ffi/bind.js';
3
4
  // dll 名部分应用(工厂转调保泛型 infer,见 lib/windows/user32.ts 头注释)
4
5
  const b = (name, sig) => bind('comctl32.dll', name, sig);
5
- /** 创建图像列表;失败 → 0
6
- * @param args_0 cx 图像宽度(像素)
7
- * @param args_1 cy 图像高度(像素)
8
- * @param args_2 flags ILC_* 标志(ILC_COLOR32 = 4)
9
- * @param args_3 initial 初始图像数,0 = 默认
10
- * @param args_4 grow 扩容步长,0 = 默认 */
11
- export const ImageListCreate = /*@__PURE__*/ b('ImageList_Create', 'i32 i32 u32 i32 i32 -> <>ptr');
12
- /** 追加位图到图像列表;返回新图像索引(-1 失败)
13
- * @param args_0 himl
14
- * @param args_1 hbmImage 源位图句柄(HBITMAP) */
15
- export const ImageListAdd = /*@__PURE__*/ b('ImageList_Add', '<>ptr <>ptr -> i32');
16
- /** 销毁图像列表;成功 → 非 0
17
- * @param args_0 himl */
18
- export const ImageListDestroy = /*@__PURE__*/ b('ImageList_Destroy', '<>ptr -> i32');
6
+ /** 建图像列表;配对 ImageListDestroy
7
+ * @returns 图像列表句柄;失败 → NULL
8
+ * @param args_0 cx/cy 图像尺寸 @param args_2 flags ILC_* 组合
9
+ * @param args_3 cInitial 初始容量 @param args_4 cGrow 增长步长 */
10
+ export const ImageListCreate = /*@__PURE__*/ b('ImageList_Create', 'i32 i32 u32 i32 i32 -> <HIMAGELIST>ptr');
11
+ /** 加位图进图像列表
12
+ * @returns 新索引(-1 失败)
13
+ * @param args_0 himl 图像列表句柄
14
+ * @param args_1 hbm 位图句柄(gdi32 <HGDIOBJ>ptr 产物,如 CreateCompatibleBitmap) */
15
+ export const ImageListAdd = /*@__PURE__*/ b('ImageList_Add', '<HIMAGELIST>ptr <HGDIOBJ>ptr -> i32');
16
+ /** 销毁图像列表
17
+ * @returns 成功 → 非 0 */
18
+ export const ImageListDestroy = /*@__PURE__*/ b('ImageList_Destroy', '<HIMAGELIST>ptr -> i32');
@@ -1,20 +1,19 @@
1
- // comctl32 绑定(ImageList 系列;HIMAGELIST 为不透明句柄保持 <>ptr)
1
+ // comctl32 绑定(ImageList 系列;HIMAGELIST 不透明句柄走 <HIMAGELIST>ptr 自产自收,
2
+ // Add 的 hbm 位与 gdi32 的 <HGDIOBJ>ptr 同型互认——CreateBitmap 产物直传)
2
3
  import { bind } from '../ffi/bind.js'
3
4
 
4
5
  // dll 名部分应用(工厂转调保泛型 infer,见 lib/windows/user32.ts 头注释)
5
6
  const b = <const S extends string>(name: string, sig: S) => bind('comctl32.dll', name, sig)
6
-
7
- /** 创建图像列表;失败 → 0
8
- * @param args_0 cx 图像宽度(像素)
9
- * @param args_1 cy 图像高度(像素)
10
- * @param args_2 flags ILC_* 标志(ILC_COLOR32 = 4)
11
- * @param args_3 initial 初始图像数,0 = 默认
12
- * @param args_4 grow 扩容步长,0 = 默认 */
13
- export const ImageListCreate = /*@__PURE__*/ b('ImageList_Create', 'i32 i32 u32 i32 i32 -> <>ptr')
14
- /** 追加位图到图像列表;返回新图像索引(-1 失败)
15
- * @param args_0 himl
16
- * @param args_1 hbmImage 源位图句柄(HBITMAP) */
17
- export const ImageListAdd = /*@__PURE__*/ b('ImageList_Add', '<>ptr <>ptr -> i32')
18
- /** 销毁图像列表;成功 → 非 0
19
- * @param args_0 himl */
20
- export const ImageListDestroy = /*@__PURE__*/ b('ImageList_Destroy', '<>ptr -> i32')
7
+ /** 建图像列表;配对 ImageListDestroy
8
+ * @returns 图像列表句柄;失败 → NULL
9
+ * @param args_0 cx/cy 图像尺寸 @param args_2 flags ILC_* 组合
10
+ * @param args_3 cInitial 初始容量 @param args_4 cGrow 增长步长 */
11
+ export const ImageListCreate = /*@__PURE__*/ b('ImageList_Create', 'i32 i32 u32 i32 i32 -> <HIMAGELIST>ptr')
12
+ /** 加位图进图像列表
13
+ * @returns 新索引(-1 失败)
14
+ * @param args_0 himl 图像列表句柄
15
+ * @param args_1 hbm 位图句柄(gdi32 <HGDIOBJ>ptr 产物,如 CreateCompatibleBitmap) */
16
+ export const ImageListAdd = /*@__PURE__*/ b('ImageList_Add', '<HIMAGELIST>ptr <HGDIOBJ>ptr -> i32')
17
+ /** 销毁图像列表
18
+ * @returns 成功 → 非 0 */
19
+ export const ImageListDestroy = /*@__PURE__*/ b('ImageList_Destroy', '<HIMAGELIST>ptr -> i32')
@@ -2,6 +2,7 @@
2
2
  import { bind } from '../ffi/bind.js';
3
3
  // dll 名部分应用(工厂转调保泛型 infer,见 lib/windows/user32.ts 头注释)
4
4
  const b = (name, sig) => bind('comdlg32.dll', name, sig);
5
- /** 打开/保存文件对话框;用户确认 → 非 0(lpstrFile 缓冲即选中路径),取消/出错 → 0
5
+ /** 打开/保存文件对话框
6
+ * @returns 用户确认 → 非 0(lpstrFile 缓冲即选中路径),取消/出错 → 0
6
7
  * @param args_0 pOpenfn OPENFILENAMEW.encode(...).ptr(lStructSize 必须 = OPENFILENAMEW.size,见 structs.ts) */
7
- export const GetOpenFileName = /*@__PURE__*/ b('GetOpenFileNameW', '<BYTE>ptr -> u32');
8
+ export const GetOpenFileName = /*@__PURE__*/ b('GetOpenFileNameW', '<OPENFILENAMEW>ptr -> u32');
@@ -4,6 +4,7 @@ import { bind } from '../ffi/bind.js'
4
4
  // dll 名部分应用(工厂转调保泛型 infer,见 lib/windows/user32.ts 头注释)
5
5
  const b = <const S extends string>(name: string, sig: S) => bind('comdlg32.dll', name, sig)
6
6
 
7
- /** 打开/保存文件对话框;用户确认 → 非 0(lpstrFile 缓冲即选中路径),取消/出错 → 0
7
+ /** 打开/保存文件对话框
8
+ * @returns 用户确认 → 非 0(lpstrFile 缓冲即选中路径),取消/出错 → 0
8
9
  * @param args_0 pOpenfn OPENFILENAMEW.encode(...).ptr(lStructSize 必须 = OPENFILENAMEW.size,见 structs.ts) */
9
- export const GetOpenFileName = /*@__PURE__*/ b('GetOpenFileNameW', '<BYTE>ptr -> u32')
10
+ export const GetOpenFileName = /*@__PURE__*/ b('GetOpenFileNameW', '<OPENFILENAMEW>ptr -> u32')