quickwin 2.0.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 (102) hide show
  1. package/README.en.md +17 -22
  2. package/README.md +1 -3
  3. package/examples/exec_server.js +133 -29
  4. package/examples/exec_server_worker.js +1 -1
  5. package/examples/exec_server_worker.ts +2 -2
  6. package/examples/pdf_preview2.js +1 -1
  7. package/examples/pdf_preview2.ts +1 -1
  8. package/examples/pdf_viewer.js +132 -28
  9. package/examples/pdf_viewer.tsx +1 -1
  10. package/examples/test_gallery.js +131 -27
  11. package/examples/test_react_lazy.js +131 -27
  12. package/examples/test_react_listbox.js +131 -27
  13. package/examples/test_root_window.js +131 -27
  14. package/examples/test_tab.js +131 -27
  15. package/lib/certs.js +13 -8
  16. package/lib/certs.ts +13 -8
  17. package/lib/fetch.js +51 -3
  18. package/lib/fetch.ts +38 -3
  19. package/lib/ffi/bind.js +94 -5
  20. package/lib/ffi/bind.ts +107 -10
  21. package/lib/ffi/ctype.js +33 -7
  22. package/lib/ffi/ctype.ts +48 -10
  23. package/lib/ffi/struct.js +92 -14
  24. package/lib/ffi/struct.ts +108 -19
  25. package/lib/react-qw/components/ListView.js +1 -0
  26. package/lib/react-qw/components/PathPicker.js +2 -2
  27. package/lib/react-qw/index.js +1 -1
  28. package/lib/react-qw/index.ts +4 -1
  29. package/lib/text-measure.js +3 -1
  30. package/lib/text-measure.ts +4 -3
  31. package/lib/websocket.js +32 -2
  32. package/lib/websocket.ts +25 -2
  33. package/lib/windows/advapi32.js +53 -0
  34. package/lib/windows/advapi32.ts +64 -0
  35. package/lib/windows/comctl32.js +15 -15
  36. package/lib/windows/comctl32.ts +15 -16
  37. package/lib/windows/comdlg32.js +3 -2
  38. package/lib/windows/comdlg32.ts +3 -2
  39. package/lib/windows/gdi32.js +328 -26
  40. package/lib/windows/gdi32.ts +349 -26
  41. package/lib/windows/kernel32.js +206 -10
  42. package/lib/windows/kernel32.ts +225 -10
  43. package/lib/windows/msimg32.js +38 -0
  44. package/lib/windows/msimg32.ts +45 -0
  45. package/lib/windows/ntdll.js +3 -2
  46. package/lib/windows/ntdll.ts +3 -2
  47. package/lib/windows/ole32.js +1 -1
  48. package/lib/windows/ole32.ts +1 -1
  49. package/lib/windows/psapi.js +32 -0
  50. package/lib/windows/psapi.ts +37 -0
  51. package/lib/windows/shell32.js +5 -3
  52. package/lib/windows/shell32.ts +5 -3
  53. package/lib/windows/structs.js +135 -0
  54. package/lib/windows/structs.ts +144 -0
  55. package/lib/windows/user32.js +202 -99
  56. package/lib/windows/user32.ts +204 -99
  57. package/lib/windows/version.js +29 -0
  58. package/lib/windows/version.ts +35 -0
  59. package/lib/windows/winspool.js +71 -0
  60. package/lib/windows/winspool.ts +80 -0
  61. package/package.json +1 -1
  62. package/quickwin.d.ts +11 -5
  63. package/quickwin_const.d.ts +136 -0
  64. package/qwin-nowasm-x86.exe +0 -0
  65. package/qwin-nowasm.exe +0 -0
  66. package/qwin-x86.exe +0 -0
  67. package/qwin.exe +0 -0
  68. package/test/run.js +7 -0
  69. package/test/run.ts +7 -0
  70. package/test/test_basic_fetch.js +21 -0
  71. package/test/test_basic_fetch.ts +17 -0
  72. package/test/test_ffi.js +6 -6
  73. package/test/test_ffi.ts +6 -6
  74. package/test/test_ffi_abi.js +55 -0
  75. package/test/test_ffi_abi.ts +56 -0
  76. package/test/test_ffi_array.js +116 -0
  77. package/test/test_ffi_array.ts +141 -0
  78. package/test/test_ffi_bind.js +82 -0
  79. package/test/test_ffi_bind.ts +71 -0
  80. package/test/test_files.js +102 -0
  81. package/test/test_files.ts +121 -0
  82. package/test/test_gdi_draw.js +681 -0
  83. package/test/test_gdi_draw.ts +725 -0
  84. package/test/test_net_fetch.js +4 -3
  85. package/test/test_net_fetch.ts +4 -3
  86. package/test/test_printer.js +137 -0
  87. package/test/test_printer.ts +148 -0
  88. package/test/test_proc.js +141 -0
  89. package/test/test_proc.ts +163 -0
  90. package/test/test_react_complex.js +131 -27
  91. package/test/test_react_counter.js +131 -27
  92. package/test/test_react_flex.js +131 -27
  93. package/test/test_react_hidden.js +131 -27
  94. package/test/test_react_listview_link.js +131 -27
  95. package/test/test_react_notify.js +131 -27
  96. package/test/test_react_render.js +131 -27
  97. package/test/test_registry.js +74 -0
  98. package/test/test_registry.ts +95 -0
  99. package/test/test_sysinfo.js +122 -0
  100. package/test/test_sysinfo.ts +141 -0
  101. package/test/test_wolfssl_read.js +45 -44
  102. package/test/test_wolfssl_read.ts +42 -42
@@ -15600,10 +15600,18 @@ var C_BASIC_TYPE = [
15600
15600
  "f64",
15601
15601
  "ptr"
15602
15602
  ];
15603
- var PTR_TOKEN_RE = /^<([^<>\s]*)>ptr(!|@[A-Za-z_]\w*)?$/;
15603
+ var PTR_TOKEN_RE = /^<([A-Za-z_]\w*|)>ptr(!|@[A-Za-z_]\w*)?$/;
15604
15604
  function isCPtrToken(t) {
15605
15605
  return PTR_TOKEN_RE.test(t);
15606
15606
  }
15607
+ var VAL_TOKEN_RE = /^<([A-Za-z_]\w*)>$/;
15608
+ function isCValToken(t) {
15609
+ return VAL_TOKEN_RE.test(t);
15610
+ }
15611
+ function valName(t) {
15612
+ const m = VAL_TOKEN_RE.exec(t);
15613
+ return m?.[1] || "";
15614
+ }
15607
15615
  function ptrName(t) {
15608
15616
  const m = PTR_TOKEN_RE.exec(t);
15609
15617
  return m?.[1] || "";
@@ -15611,8 +15619,9 @@ function ptrName(t) {
15611
15619
  var cTokenToType = (t) => {
15612
15620
  if (isCPtrToken(t))
15613
15621
  return "ptr";
15614
- else
15615
- return t;
15622
+ if (isCValToken(t))
15623
+ throw new Error(`ffi: cTokenToType: by-value token "${t}" has no scalar slot type`);
15624
+ return t;
15616
15625
  };
15617
15626
  var PTR_SIZE = os.arch === "x64" ? 8 : 4;
15618
15627
  var SizeAlign = {
@@ -15631,8 +15640,12 @@ var NULL = 0;
15631
15640
  function normToken(t) {
15632
15641
  if (isCPtrToken(t)) {
15633
15642
  const m = PTR_TOKEN_RE.exec(t);
15634
- return m?.[2]?.startsWith("@") ? `<${m[1]}>ptr` : t;
15643
+ const name = m?.[1] || "";
15644
+ const mod = m?.[2];
15645
+ return mod?.startsWith("@") ? `<${name}>ptr` : t;
15635
15646
  }
15647
+ if (isCValToken(t))
15648
+ return t;
15636
15649
  const at = t.indexOf("@");
15637
15650
  const base = at >= 0 ? t.slice(0, at) : t;
15638
15651
  const suffixOk = at < 0 || /^[A-Za-z_]\w*$/.test(t.slice(at + 1));
@@ -15728,6 +15741,9 @@ var PtrArrayBuffer = class extends ArrayBuffer {
15728
15741
  };
15729
15742
 
15730
15743
  // _build/lib/ffi/bind.js
15744
+ function valCodec(name, encoders) {
15745
+ return encoders[name];
15746
+ }
15731
15747
  var _dllCache = /* @__PURE__ */ new Map();
15732
15748
  function loadDll(dll) {
15733
15749
  const cached = _dllCache.get(dll);
@@ -15753,14 +15769,27 @@ function parseSig(sig) {
15753
15769
  }
15754
15770
  argTokens.push(tok);
15755
15771
  }
15772
+ const retToken = normToken(parts[1]);
15773
+ if (isCValToken(retToken))
15774
+ throw new Error(`ffi-bind: "${sig}": by-value struct at the return position (<${valName(retToken)}>) is not supported \u2014 argument positions only; pass a struct out through an out-param slot instead (<N>ptr + caller buffer)`);
15756
15775
  return {
15757
15776
  argTokens,
15758
- retToken: normToken(parts[1]),
15777
+ retToken,
15759
15778
  sig
15760
15779
  };
15761
15780
  }
15762
15781
  function makeFn(proc, sig, encoders, decoders) {
15763
15782
  const ps = parseSig(sig);
15783
+ for (const t of ps.argTokens) {
15784
+ if (!isCValToken(t))
15785
+ continue;
15786
+ const name = valName(t);
15787
+ const codec = valCodec(name, encoders);
15788
+ if (!codec || typeof codec.encode !== "function")
15789
+ throw new Error(`ffi-bind: ${sig}: by-value position <${name}> has no registered layout (available: ${Object.keys(encoders).join(", ") || "none"})`);
15790
+ if (typeof codec.size !== "number" || codec.size < 1)
15791
+ throw new Error(`ffi-bind: ${sig}: by-value position <${name}> requires a struct layout with size >= 1 (got ${codec.size}; non-struct codecs expose no size)`);
15792
+ }
15764
15793
  return (...a) => callPacked(proc, ps, a, encoders, decoders);
15765
15794
  }
15766
15795
  var IA32_ARG_SIZE = {
@@ -15780,15 +15809,29 @@ var X64_MIN_SLOTS = 4;
15780
15809
  function callPacked(proc, { argTokens, retToken, sig }, args, encoders, decoders) {
15781
15810
  const is64 = os2.arch === "x64";
15782
15811
  const getWidth = (t, is642) => {
15812
+ if (isCValToken(t))
15813
+ throw new Error(`ffi-bind: getWidth: by-value token "${t}" (dispatched by widthOf)`);
15783
15814
  if (is642)
15784
15815
  return 8;
15785
15816
  if (isCPtrToken(t))
15786
15817
  return PTR_SIZE;
15787
15818
  return IA32_ARG_SIZE[t];
15788
15819
  };
15820
+ const widthOf = (t) => {
15821
+ if (isCValToken(t)) {
15822
+ if (is64)
15823
+ return 8;
15824
+ const layout = valCodec(valName(t), encoders);
15825
+ if (!layout)
15826
+ throw new Error(`ffi-bind: <${valName(t)}> by-value position has no registered layout`);
15827
+ const size = layout.size;
15828
+ return size % 4 === 0 ? size : size + 4 - size % 4;
15829
+ }
15830
+ return getWidth(t, is64);
15831
+ };
15789
15832
  let total = 0;
15790
15833
  for (const t of argTokens)
15791
- total += getWidth(t, is64);
15834
+ total += widthOf(t);
15792
15835
  if (is64 && argTokens.length < X64_MIN_SLOTS)
15793
15836
  total = X64_MIN_SLOTS * 8;
15794
15837
  const argFrame = new ArrayBuffer(total);
@@ -15825,10 +15868,28 @@ function callPacked(proc, { argTokens, retToken, sig }, args, encoders, decoders
15825
15868
  throw new Error(`ffi-bind: ${sig}: arg#${i + 1} <${name}>ptr! encode produced 0 (null pointer forbidden here)`);
15826
15869
  writeSlot(dv, off, { k: "ptr", v: p });
15827
15870
  }
15871
+ } else if (isCValToken(argToken)) {
15872
+ const name = valName(argToken);
15873
+ const codec = valCodec(name, encoders);
15874
+ if (!codec)
15875
+ throw new Error(`ffi-bind: ${sig}: arg#${i + 1} <${name}> by-value position has no registered layout`);
15876
+ if (arg === null || arg === void 0)
15877
+ throw new Error(`ffi-bind: <${name}> value position got ${String(arg)}; pass a struct object (use <${name}>ptr to pass a pointer)`);
15878
+ const size = codec.size;
15879
+ if (!is64 || size <= 8 && (size & size - 1) === 0) {
15880
+ codec.encode(arg, argFrame, off);
15881
+ } else {
15882
+ const buf = new ArrayBuffer(size + 15);
15883
+ const base = ffi.bufferPtr(buf);
15884
+ const pad = (16 - base % 16) % 16;
15885
+ codec.encode(arg, buf, pad);
15886
+ held.push(buf);
15887
+ writeSlot(dv, off, { k: "ptr", v: base + pad });
15888
+ }
15828
15889
  } else {
15829
15890
  writeSlot(dv, off, { k: argToken, v: arg });
15830
15891
  }
15831
- off += getWidth(argToken, is64);
15892
+ off += widthOf(argToken);
15832
15893
  }
15833
15894
  const retBuf = new ArrayBuffer(8);
15834
15895
  const retIsFp = retToken === "f32" || retToken === "f64";
@@ -16219,21 +16280,20 @@ function createStruct(t) {
16219
16280
  size,
16220
16281
  structAlign: maxEffectiveAlign,
16221
16282
  decode: (p) => {
16222
- if (typeof p === "number") {
16223
- const buf = new ArrayBuffer(size);
16224
- const u8 = new Uint8Array(buf);
16225
- for (let i = 0; i < size; i++)
16226
- u8[i] = ffi2.readByte(p + i);
16227
- return doDecode(new DataView(buf), 0, fields);
16228
- }
16283
+ if (typeof p === "number")
16284
+ return doDecode(new DataView(ffi2.readBytes(p, size)), 0, fields);
16229
16285
  return doDecode(new DataView(p), 0, fields);
16230
16286
  },
16231
- alloc: (() => new PtrArrayBuffer(size)),
16232
- encode: ((v, buf) => {
16287
+ alloc: ((n) => {
16288
+ if (n !== void 0 && (!Number.isInteger(n) || n < 1))
16289
+ throw new Error(`ffi-struct: alloc(n) requires an integer n >= 1 (PAB(0) never escapes \u2014 pass NULL for an empty pointer position)`);
16290
+ return new PtrArrayBuffer(size * (n ?? 1));
16291
+ }),
16292
+ encode: ((v, buf, offset) => {
16233
16293
  if (v === void 0 || v === null)
16234
16294
  throw new Error(`ffi-struct: encode(v) requires an argument; use alloc() for a zeroed out-param slot`);
16235
16295
  const out = buf ?? new PtrArrayBuffer(size);
16236
- doEncode(new DataView(out), 0, fields, v);
16296
+ doEncode(new DataView(out), offset ?? 0, fields, v);
16237
16297
  return out;
16238
16298
  }),
16239
16299
  offsetOf: (name) => {
@@ -16253,6 +16313,49 @@ function buildDecl(kind, member, pack) {
16253
16313
  decl[k] = v;
16254
16314
  return decl;
16255
16315
  }
16316
+ function structArray(def) {
16317
+ const { fields, size } = computeStructLayout(def.__struct);
16318
+ const readN = (dv, n) => {
16319
+ const out = [];
16320
+ for (let i = 0; i < n; i++)
16321
+ out.push(doDecode(dv, i * size, fields));
16322
+ return out;
16323
+ };
16324
+ return {
16325
+ encode: (els) => {
16326
+ if (!Array.isArray(els) || els.length === 0)
16327
+ throw new Error(`ffi-struct: structArray(def).encode(els) requires a non-empty element array (pass NULL for an empty pointer position)`);
16328
+ const out = def.alloc(els.length);
16329
+ for (let i = 0; i < els.length; i++)
16330
+ def.encode(els[i], out, i * def.size);
16331
+ return out;
16332
+ },
16333
+ decode: ((p, count) => {
16334
+ if (count !== void 0 && (!Number.isInteger(count) || count < 0))
16335
+ throw new Error(`ffi-struct: structArray.decode(p, count) requires a non-negative integer count, got ${count}`);
16336
+ if (typeof p === "number") {
16337
+ if (count === void 0)
16338
+ throw new Error(`ffi-struct: structArray.decode(address, count) requires an explicit count (a bare address carries no length)`);
16339
+ if (count === 0)
16340
+ return [];
16341
+ return readN(new DataView(ffi2.readBytes(p, count * size)), count);
16342
+ }
16343
+ let n;
16344
+ if (count !== void 0) {
16345
+ n = count;
16346
+ if (n * size > p.byteLength)
16347
+ throw new Error(`ffi-struct: structArray.decode: count ${n} \xD7 size ${size} = ${n * size} exceeds buffer byteLength ${p.byteLength}`);
16348
+ } else {
16349
+ if (p.byteLength % size !== 0)
16350
+ throw new Error(`ffi-struct: structArray.decode: byteLength ${p.byteLength} is not a multiple of size ${size} (mixed buffer? pass count explicitly)`);
16351
+ n = p.byteLength / size;
16352
+ }
16353
+ if (n === 0)
16354
+ return [];
16355
+ return readN(new DataView(p), n);
16356
+ })
16357
+ };
16358
+ }
16256
16359
  function struct(a, b6, c) {
16257
16360
  const isNamed = typeof a === "string";
16258
16361
  const raw = isNamed ? b6 : a;
@@ -16502,11 +16605,12 @@ import * as os4 from "os";
16502
16605
 
16503
16606
  // _build/lib/windows/gdi32.js
16504
16607
  var b2 = (name, sig, encoders) => bind("gdi32.dll", name, sig, encoders);
16505
- var DeleteObject = /* @__PURE__ */ b2("DeleteObject", "<>ptr -> i32");
16608
+ var POINTArr = structArray(POINT);
16609
+ var DeleteObject = /* @__PURE__ */ b2("DeleteObject", "<HGDIOBJ>ptr -> i32");
16506
16610
  var GetDeviceCaps = /* @__PURE__ */ b2("GetDeviceCaps", "<HDC>ptr! i32@DeviceCap -> i32");
16507
- var CreateFontIndirect = /* @__PURE__ */ b2("CreateFontIndirectW", "<LOGFONTW>ptr -> <>ptr", { LOGFONTW });
16508
- var SelectObject = /* @__PURE__ */ b2("SelectObject", "<HDC>ptr! <>ptr -> <>ptr");
16509
- var GetObject = /* @__PURE__ */ b2("GetObjectW", "<>ptr i32 <BYTE>ptr -> i32");
16611
+ var CreateFontIndirect = /* @__PURE__ */ b2("CreateFontIndirectW", "<LOGFONTW>ptr -> <HGDIOBJ>ptr", { LOGFONTW });
16612
+ var SelectObject = /* @__PURE__ */ b2("SelectObject", "<HDC>ptr! <HGDIOBJ>ptr -> <HGDIOBJ>ptr");
16613
+ var GetObject = /* @__PURE__ */ b2("GetObjectW", "<HGDIOBJ>ptr i32 <BYTE>ptr -> i32");
16510
16614
 
16511
16615
  // _build/lib/react-qw/props.js
16512
16616
  import * as gui from "gui";
@@ -16653,7 +16757,7 @@ function measureTextForHwnd(hwnd2, text) {
16653
16757
  if (!hdc)
16654
16758
  return { width: 0, height: 0 };
16655
16759
  const hFont = gui2.SendMessage(hwnd2, 49, 0, 0);
16656
- const oldFont = hFont ? SelectObject(hdc, hFont) : 0;
16760
+ const oldFont = hFont ? SelectObject(hdc, hFont) : NULL;
16657
16761
  const result = measureText(hdc, text, 0);
16658
16762
  if (hFont)
16659
16763
  SelectObject(hdc, oldFont);
@@ -18038,11 +18142,11 @@ var import_react17 = __toESM(require_react(), 1);
18038
18142
 
18039
18143
  // _build/lib/windows/comdlg32.js
18040
18144
  var b3 = (name, sig) => bind("comdlg32.dll", name, sig);
18041
- var GetOpenFileName = /* @__PURE__ */ b3("GetOpenFileNameW", "<BYTE>ptr -> u32");
18145
+ var GetOpenFileName = /* @__PURE__ */ b3("GetOpenFileNameW", "<OPENFILENAMEW>ptr -> u32");
18042
18146
 
18043
18147
  // _build/lib/windows/shell32.js
18044
18148
  var b4 = (name, sig) => bind("shell32.dll", name, sig);
18045
- var SHBrowseForFolder = /* @__PURE__ */ b4("SHBrowseForFolderW", "<BYTE>ptr -> <>ptr");
18149
+ var SHBrowseForFolder = /* @__PURE__ */ b4("SHBrowseForFolderW", "<BROWSEINFOW>ptr -> <>ptr");
18046
18150
  var SHGetPathFromIDList = /* @__PURE__ */ b4("SHGetPathFromIDListW", "<>ptr <BYTE>ptr -> u32");
18047
18151
 
18048
18152
  // _build/lib/windows/ole32.js
@@ -18067,7 +18171,7 @@ function openFileDialog(owner, filter, title, multiple) {
18067
18171
  lpstrTitle: titleWide?.ptr ?? 0,
18068
18172
  Flags: flags
18069
18173
  });
18070
- const ret = GetOpenFileName(ofn);
18174
+ const ret = GetOpenFileName(ofn.ptr);
18071
18175
  if (!ret)
18072
18176
  return null;
18073
18177
  if (!multiple) {
@@ -18085,7 +18189,7 @@ function openFolderDialog(owner, title) {
18085
18189
  lpszTitle: titleWide?.ptr ?? 0,
18086
18190
  ulFlags: 65
18087
18191
  });
18088
- const pidl = SHBrowseForFolder(bi);
18192
+ const pidl = SHBrowseForFolder(bi.ptr);
18089
18193
  if (!pidl)
18090
18194
  return null;
18091
18195
  const pathBuf = WCHAR.alloc(260);
@@ -18180,7 +18284,7 @@ function createRoot(container) {
18180
18284
  } else {
18181
18285
  hwnd2 = container;
18182
18286
  }
18183
- const root = reconciler_default.createContainer(hwnd2, 0, null, false, null, "", noop, noop, noop, noop);
18287
+ const root = reconciler_default.createContainer(hwnd2, 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);
18184
18288
  const rootInst = { hwnd: hwnd2, type: "_root", props: { type: "_root", style: { flexDirection: "column", alignItems: "stretch" } }, children: [] };
18185
18289
  instancesByHwnd.set(hwnd2, rootInst);
18186
18290
  if (typeof container === "object")
@@ -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
+ };