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.
- package/README.en.md +18 -24
- package/README.md +17 -23
- package/examples/PdfCanvas.js +8 -37
- package/examples/PdfCanvas.tsx +11 -41
- package/examples/exec_server.js +20155 -1420
- package/examples/exec_server.tsx +489 -0
- package/examples/exec_server_worker.js +383 -33
- package/examples/exec_server_worker.ts +408 -30
- package/examples/pdf_preview2.js +25 -82
- package/examples/pdf_preview2.ts +27 -84
- package/examples/pdf_viewer.js +1424 -1180
- package/examples/pdf_viewer.tsx +16 -67
- package/examples/setres.js +22 -47
- package/examples/setres.ts +23 -57
- package/examples/test_gallery.js +1418 -1075
- package/examples/test_react_lazy.js +1391 -1059
- package/examples/test_react_listbox.js +1390 -1058
- package/examples/test_root_window.js +1384 -1052
- package/examples/test_tab.js +1391 -1059
- package/lib/abort.js +70 -0
- package/lib/abort.ts +69 -0
- package/lib/certs.js +219 -0
- package/lib/certs.ts +229 -0
- package/lib/exec-log.js +15 -0
- package/lib/exec-log.ts +30 -0
- package/lib/fetch.js +88 -7
- package/lib/fetch.ts +73 -7
- package/lib/ffi/bind.js +441 -0
- package/lib/ffi/bind.ts +539 -0
- package/lib/ffi/ctype.js +194 -0
- package/lib/ffi/ctype.ts +369 -0
- package/lib/ffi/index.js +6 -0
- package/lib/ffi/index.ts +6 -0
- package/lib/ffi/struct.js +490 -0
- package/lib/ffi/struct.ts +753 -0
- package/lib/http-common.js +43 -5
- package/lib/http-common.ts +48 -6
- package/lib/http-server.js +187 -29
- package/lib/http-server.ts +177 -26
- package/lib/react-qw/components/DateTimePicker.js +18 -37
- package/lib/react-qw/components/Input.js +3 -10
- package/lib/react-qw/components/Link.js +6 -19
- package/lib/react-qw/components/ListView.js +50 -154
- package/lib/react-qw/components/PathPicker.js +28 -88
- package/lib/react-qw/components/ScrollView.js +15 -8
- package/lib/react-qw/components/Slider.js +2 -1
- package/lib/react-qw/components/Tab.js +6 -22
- package/lib/react-qw/components/Tooltip.js +10 -21
- package/lib/react-qw/components/TreeView.js +8 -31
- package/lib/react-qw/index.js +5 -3
- package/lib/react-qw/index.ts +8 -3
- package/lib/react-qw/nmhdr.js +3 -24
- package/lib/react-qw/nmhdr.ts +5 -30
- package/lib/react-qw/props.js +7 -2
- package/lib/react-qw/props.ts +7 -3
- package/lib/react-qw/reconciler.js +23 -13
- package/lib/react-qw/reconciler.ts +22 -13
- package/lib/text-measure.js +20 -23
- package/lib/text-measure.ts +22 -26
- package/lib/websocket.js +44 -4
- package/lib/websocket.ts +40 -4
- package/lib/windows/advapi32.js +53 -0
- package/lib/windows/advapi32.ts +64 -0
- package/lib/windows/comctl32.js +18 -0
- package/lib/windows/comctl32.ts +19 -0
- package/lib/windows/comdlg32.js +8 -0
- package/lib/windows/comdlg32.ts +10 -0
- package/lib/windows/gdi32.js +392 -0
- package/lib/windows/gdi32.ts +420 -0
- package/lib/windows/kernel32.js +262 -0
- package/lib/windows/kernel32.ts +291 -0
- package/lib/windows/msimg32.js +38 -0
- package/lib/windows/msimg32.ts +45 -0
- package/lib/windows/ntdll.js +9 -0
- package/lib/windows/ntdll.ts +11 -0
- package/lib/windows/ole32.js +7 -0
- package/lib/windows/ole32.ts +9 -0
- package/lib/windows/psapi.js +32 -0
- package/lib/windows/psapi.ts +37 -0
- package/lib/windows/shell32.js +13 -0
- package/lib/windows/shell32.ts +16 -0
- package/lib/windows/structs.js +519 -0
- package/lib/windows/structs.ts +566 -0
- package/lib/windows/user32.js +572 -0
- package/lib/windows/user32.ts +607 -0
- package/lib/windows/version.js +29 -0
- package/lib/windows/version.ts +35 -0
- package/lib/windows/winspool.js +71 -0
- package/lib/windows/winspool.ts +80 -0
- package/package.json +1 -1
- package/quickwin.d.ts +31 -40
- package/quickwin_const.d.ts +137 -0
- package/qwin-nowasm-x86.exe +0 -0
- package/qwin-nowasm.exe +0 -0
- package/qwin-x86.exe +0 -0
- package/qwin.exe +0 -0
- package/test/bench_ffi_callraw.js +236 -0
- package/test/bench_ffi_callraw.ts +236 -0
- package/test/bench_ffi_overhead.js +139 -0
- package/test/bench_ffi_overhead.ts +141 -0
- package/test/bench_sendmessage.js +104 -0
- package/test/bench_sendmessage.ts +100 -0
- package/test/bench_sendmessage_nonnull.js +187 -0
- package/test/bench_sendmessage_nonnull.ts +188 -0
- package/test/run.js +19 -4
- package/test/run.ts +20 -5
- package/test/test_basic_fetch.js +29 -0
- package/test/test_basic_fetch.ts +26 -0
- package/test/test_ffi.js +27 -45
- package/test/test_ffi.ts +35 -54
- package/test/test_ffi_abi.js +290 -0
- package/test/test_ffi_abi.ts +274 -0
- package/test/test_ffi_array.js +116 -0
- package/test/test_ffi_array.ts +141 -0
- package/test/test_ffi_bind.js +620 -8
- package/test/test_ffi_bind.ts +630 -8
- package/test/test_ffi_struct.js +632 -88
- package/test/test_ffi_struct.ts +666 -91
- package/test/test_ffi_struct_layout.js +495 -0
- package/test/test_ffi_struct_layout.ts +543 -0
- package/test/test_ffi_user32.js +103 -0
- package/test/test_ffi_user32.ts +113 -0
- package/test/test_ffi_windows.js +101 -0
- package/test/test_ffi_windows.ts +113 -0
- package/test/test_files.js +102 -0
- package/test/test_files.ts +121 -0
- package/test/test_gdi_draw.js +681 -0
- package/test/test_gdi_draw.ts +725 -0
- package/test/test_helper.js +8 -1
- package/test/test_helper.ts +9 -1
- package/test/test_http_import.js +18 -0
- package/test/test_http_import.ts +19 -0
- package/test/test_http_server.js +73 -1
- package/test/test_http_server.ts +75 -1
- package/test/test_ipv6.js +2 -2
- package/test/test_ipv6.ts +2 -2
- package/test/test_net_fetch.js +74 -11
- package/test/test_net_fetch.ts +73 -10
- package/test/test_net_websocket.js +1 -1
- package/test/test_net_websocket.ts +1 -1
- package/test/test_printer.js +137 -0
- package/test/test_printer.ts +148 -0
- package/test/test_proc.js +141 -0
- package/test/test_proc.ts +163 -0
- package/test/test_react_complex.js +1401 -1069
- package/test/test_react_counter.js +1391 -1059
- package/test/test_react_flex.js +1411 -1072
- package/test/test_react_hidden.js +1394 -1062
- package/test/test_react_listview_link.js +1391 -1059
- package/test/test_react_notify.js +18638 -0
- package/test/test_react_render.js +1391 -1059
- package/test/test_registry.js +74 -0
- package/test/test_registry.ts +95 -0
- package/test/test_sysinfo.js +122 -0
- package/test/test_sysinfo.ts +141 -0
- package/test/test_wolfssl_read.js +45 -43
- package/test/test_wolfssl_read.ts +42 -40
- package/test/test_worker_net.js +35 -17
- package/test/test_worker_net.ts +27 -15
- package/tsconfig.json +1 -1
- package/examples/exec_server.ts +0 -80
- package/lib/ffi-bind.js +0 -85
- package/lib/ffi-bind.ts +0 -170
- package/lib/ffi-struct.js +0 -224
- package/lib/ffi-struct.ts +0 -239
- package/lib/win-common-structs.js +0 -39
- package/lib/win-common-structs.ts +0 -41
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { bind } from '../lib/ffi/bind.js';
|
|
2
|
+
import { struct, structArray } from '../lib/ffi/struct.js';
|
|
3
|
+
import { NULL, normToken, PtrArrayBuffer } from '../lib/ffi/ctype.js';
|
|
4
|
+
function expectType(_value) { }
|
|
5
|
+
// 运行时 throw 捕获:返回错误文本(无异常 → 空串)
|
|
6
|
+
function throws(fn) {
|
|
7
|
+
try {
|
|
8
|
+
fn();
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
return String(e);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const POINT = struct('POINT', { x: 'i32', y: 'i32' });
|
|
16
|
+
const RECT = struct('RECT', { l: 'i32', t: 'i32', r: 'i32', b: 'i32' });
|
|
17
|
+
// 数组编解码对:encode 注册进 encoders 表后 <POINT>ptr 位收元素数组;
|
|
18
|
+
// decode 是出参数组读回(双态:buffer 自算/显式 count、裸地址 count 必填)
|
|
19
|
+
const POINTArr = structArray(POINT);
|
|
20
|
+
export const suite = {
|
|
21
|
+
name: 'ffi-array',
|
|
22
|
+
run: (t) => {
|
|
23
|
+
t.section("normToken: '*' is an ordinary out-of-lexicon char (same as '+')");
|
|
24
|
+
t.checkTrue("'<X*>ptr' rejected", throws(() => normToken('<POINT*>ptr')).includes('Unknown token'));
|
|
25
|
+
t.checkTrue("'*'+'!' rejected", throws(() => normToken('<POINT*>ptr!')).includes('Unknown token'));
|
|
26
|
+
t.checkTrue("'*'+'@' rejected", throws(() => normToken('<POINT*>ptr@Foo')).includes('Unknown token'));
|
|
27
|
+
t.checkTrue('bare <*>ptr rejected', throws(() => normToken('<*>ptr')).includes('Unknown token'));
|
|
28
|
+
t.checkTrue("'+' not in lexicon: generic Unknown token", throws(() => normToken('<POINT+>ptr')).includes('Unknown token'));
|
|
29
|
+
t.checkTrue('name charset tightened (non-identifier rejected)', throws(() => normToken('<1abc>ptr')).includes('Unknown token'));
|
|
30
|
+
t.check('plain <X>ptr unchanged', '<X>ptr', normToken('<X>ptr'));
|
|
31
|
+
t.check('!-form unchanged', '<X>ptr!', normToken('<X>ptr!'));
|
|
32
|
+
t.check('@-suffix stripped (enum annotation is type-layer only)', '<X>ptr', normToken('<X>ptr@Foo'));
|
|
33
|
+
t.section('type layer: registered encoder derives the array value domain');
|
|
34
|
+
// 注册数组 encoder 后:<POINT>ptr 位 = 非空元素数组(值域由 encoder 推导)∪ number 直通
|
|
35
|
+
const PolylineT = bind('gdi32.dll', 'Polyline', '<HDC>ptr! <POINT>ptr i32 -> i32', { POINT: POINTArr });
|
|
36
|
+
expectType();
|
|
37
|
+
expectType();
|
|
38
|
+
// 类型专用块(只过 tsc、不执行)
|
|
39
|
+
const typeOnly = () => {
|
|
40
|
+
// @ts-expect-error 单对象不是元素数组(encoder 值域收数组,不收裸 POINT)
|
|
41
|
+
PolylineT(0, { x: 1, y: 2 }, 1);
|
|
42
|
+
// @ts-expect-error 品牌拦错布局直传(RECT PAB 不是 POINT 位的值域——传 .ptr 走 number 直通)
|
|
43
|
+
PolylineT(0, RECT.alloc(), 1);
|
|
44
|
+
// @ts-expect-error decode 的裸地址形 count 必填(地址无长度信息)
|
|
45
|
+
POINTArr.decode(POINT.alloc().ptr);
|
|
46
|
+
};
|
|
47
|
+
t.checkTrue('type-only block wired', typeof typeOnly === 'function');
|
|
48
|
+
t.section('structArray.encode: contiguous branded materialization');
|
|
49
|
+
const pab = POINTArr.encode([{ x: 1, y: 2 }, { x: -3, y: 4 }]);
|
|
50
|
+
t.check('byteLength = n * size', 16, pab.byteLength);
|
|
51
|
+
const pdv = new DataView(pab);
|
|
52
|
+
t.check('[0].x', 1, pdv.getInt32(0, true));
|
|
53
|
+
t.check('[0].y', 2, pdv.getInt32(4, true));
|
|
54
|
+
t.check('[1].x', -3, pdv.getInt32(8, true));
|
|
55
|
+
t.check('[1].y', 4, pdv.getInt32(12, true));
|
|
56
|
+
t.checkTrue('PAB .ptr non-null (branded)', pab.ptr !== 0);
|
|
57
|
+
// fail-loud:值域刻意收普通数组(直传变量),空/非数组/单对象全靠运行时
|
|
58
|
+
// throw —— 任何路径都不产出 PAB(0)(其 .ptr 是"非 0 的 1 字节堆指针",会骗过
|
|
59
|
+
// C 的 NULL 防御)。
|
|
60
|
+
t.checkTrue('[] fail-loud (never produces PAB(0))', throws(() => POINTArr.encode([])).includes('non-empty'));
|
|
61
|
+
t.checkTrue('non-array fail-loud', throws(() => POINTArr.encode(123)).includes('non-empty'));
|
|
62
|
+
t.section('structArray.decode: self-evident / explicit count / address form');
|
|
63
|
+
// 自算形态:纯元素序列 buffer(alloc)byteLength/size 整除自证
|
|
64
|
+
t.check('self-computed n = byteLength/size', 3, POINTArr.decode(POINT.alloc(3)).length);
|
|
65
|
+
const rt = POINTArr.decode(POINTArr.encode([{ x: 1, y: -2 }, { x: 3, y: 4 }]));
|
|
66
|
+
t.check('roundtrip [0].x', 1, rt[0].x);
|
|
67
|
+
t.check('roundtrip [1].y', 4, rt[1].y);
|
|
68
|
+
// 混合缓冲(byteLength 非整数倍,如 EnumPrinters 的结构 + 尾部字符串)自算 fail-loud
|
|
69
|
+
t.checkTrue('non-multiple byteLength fail-loud', throws(() => POINTArr.decode(new PtrArrayBuffer(7))).includes('multiple'));
|
|
70
|
+
// 显式 count 越界防线:count*size 超 byteLength → 拦死(写错 count 的越界读入口)
|
|
71
|
+
t.checkTrue('count beyond byteLength fail-loud', throws(() => POINTArr.decode(new PtrArrayBuffer(12), 2)).includes('exceeds'));
|
|
72
|
+
t.checkTrue('negative count fail-loud', throws(() => POINTArr.decode(new PtrArrayBuffer(16), -1)).includes('non-negative'));
|
|
73
|
+
t.check('count = 0 → []', 0, POINTArr.decode(POINT.alloc(), 0).length);
|
|
74
|
+
// 裸地址形(C 返回的 T* / 预建缓冲 .ptr):count 必填,逐字节拷读
|
|
75
|
+
const addrBack = POINTArr.decode(pab.ptr, 2);
|
|
76
|
+
t.check('address form count', 2, addrBack.length);
|
|
77
|
+
t.check('address form [0].x', 1, addrBack[0].x);
|
|
78
|
+
t.checkTrue('address without count throws at runtime (dynamic path)', throws(() => POINTArr.decode(pab.ptr)).includes('count'));
|
|
79
|
+
t.section('StructDef.alloc(n): n-slot zero-filled (default 1)');
|
|
80
|
+
t.check('alloc() default = 1 slot', POINT.size, POINT.alloc().byteLength);
|
|
81
|
+
t.check('alloc(3) = 3 slots', POINT.size * 3, POINT.alloc(3).byteLength);
|
|
82
|
+
t.checkTrue('alloc slots zero-filled', new Int32Array(POINT.alloc(2))[3] === 0);
|
|
83
|
+
t.checkTrue('alloc(0) fail-loud (PAB(0) never escapes)', throws(() => POINT.alloc(0)).includes('n >= 1'));
|
|
84
|
+
t.checkTrue('alloc(-1) fail-loud', throws(() => POINT.alloc(-1)).includes('n >= 1'));
|
|
85
|
+
t.checkTrue('alloc(1.5) fail-loud', throws(() => POINT.alloc(1.5)).includes('n >= 1'));
|
|
86
|
+
t.section('StructDef.encode(v, buf, offset): positioned write primitive');
|
|
87
|
+
const big = POINT.alloc(2);
|
|
88
|
+
POINT.encode({ x: 7, y: -8 }, big, POINT.size); // 写入第 2 槽
|
|
89
|
+
const bdv = new DataView(big);
|
|
90
|
+
t.check('slot 0 stays zero', 0, bdv.getInt32(0, true));
|
|
91
|
+
t.check('slot 1.x written at offset', 7, bdv.getInt32(POINT.size, true));
|
|
92
|
+
t.check('slot 1.y written at offset', -8, bdv.getInt32(POINT.size + 4, true));
|
|
93
|
+
t.checkTrue('offset form returns the buf itself', POINT.encode({ x: 1, y: 2 }, big, 0) === big);
|
|
94
|
+
t.section('registered encoder dispatch: kernel32!IsBadReadPtr observes the pointer C gets');
|
|
95
|
+
const isBad = bind('kernel32.dll', 'IsBadReadPtr', '<POINT>ptr u64 -> i32', { POINT: POINTArr });
|
|
96
|
+
t.check('3-point array: whole 24-byte region readable', 0, isBad([{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }], 24n));
|
|
97
|
+
t.check('number passthrough: prebuilt PAB .ptr', 0, isBad(pab.ptr, 16n));
|
|
98
|
+
t.check('number passthrough: NULL literal', 1, isBad(NULL, 1n));
|
|
99
|
+
t.checkTrue('bare object rejected at encoder', throws(() => isBad({ x: 1, y: 2 }, 8n)).includes('non-empty'));
|
|
100
|
+
t.checkTrue('[] rejected at encoder (NULL stays explicit)', throws(() => isBad([], 8n)).includes('non-empty'));
|
|
101
|
+
t.checkTrue('null rejected with NULL hint', throws(() => isBad(null, 8n)).includes('NULL'));
|
|
102
|
+
t.section('content fidelity: msvcrt!memcpy reads encoder array source');
|
|
103
|
+
const memcpy = bind('msvcrt.dll', 'memcpy', '<POINT>ptr <POINT>ptr u64 -> <>ptr', { POINT: POINTArr });
|
|
104
|
+
const dst = POINT.alloc();
|
|
105
|
+
memcpy(dst.ptr, [{ x: 7, y: -8 }], 8n);
|
|
106
|
+
const back = POINT.decode(dst.ptr);
|
|
107
|
+
t.check('copied x', 7, back.x);
|
|
108
|
+
t.check('copied y', -8, back.y);
|
|
109
|
+
t.section("decode(alloc()): '!' unfilled field fails (C did not fill)");
|
|
110
|
+
const OutH = struct('OutH', { h: '<HANDLE>ptr!' });
|
|
111
|
+
t.checkTrue("'!' 0 slot throws on decode", throws(() => OutH.decode(OutH.alloc())).includes("'!'"));
|
|
112
|
+
t.section('bind-time lexicon checks (fail at module load)');
|
|
113
|
+
t.checkTrue('<POINT+>ptr rejected at bind (not in lexicon)', throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT+>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'));
|
|
114
|
+
t.checkTrue('<POINT*>ptr rejected at bind (out-of-lexicon)', throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT*>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'));
|
|
115
|
+
}
|
|
116
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Tester } from './test_helper.js'
|
|
2
|
+
import { bind } from '../lib/ffi/bind.js'
|
|
3
|
+
import { struct, structArray } from '../lib/ffi/struct.js'
|
|
4
|
+
import { NULL, normToken, PtrArrayBuffer, type MaybePtr, type Ptr } from '../lib/ffi/ctype.js'
|
|
5
|
+
|
|
6
|
+
// 编译期断言工具(仅类型层,运行时无开销)
|
|
7
|
+
type Equal<A, B> = (<G>() => G extends A ? 1 : 2) extends (<G>() => G extends B ? 1 : 2) ? true : false
|
|
8
|
+
function expectType<T extends true>(_value?: T): void {}
|
|
9
|
+
|
|
10
|
+
// 运行时 throw 捕获:返回错误文本(无异常 → 空串)
|
|
11
|
+
function throws(fn: () => unknown): string {
|
|
12
|
+
try { fn(); return '' } catch (e) { return String(e) }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const POINT = struct('POINT', { x: 'i32', y: 'i32' })
|
|
16
|
+
const RECT = struct('RECT', { l: 'i32', t: 'i32', r: 'i32', b: 'i32' })
|
|
17
|
+
// 数组编解码对:encode 注册进 encoders 表后 <POINT>ptr 位收元素数组;
|
|
18
|
+
// decode 是出参数组读回(双态:buffer 自算/显式 count、裸地址 count 必填)
|
|
19
|
+
const POINTArr = structArray(POINT)
|
|
20
|
+
|
|
21
|
+
export const suite = {
|
|
22
|
+
name: 'ffi-array',
|
|
23
|
+
run: (t: Tester) => {
|
|
24
|
+
t.section("normToken: '*' is an ordinary out-of-lexicon char (same as '+')")
|
|
25
|
+
t.checkTrue("'<X*>ptr' rejected", throws(() => normToken('<POINT*>ptr')).includes('Unknown token'))
|
|
26
|
+
t.checkTrue("'*'+'!' rejected", throws(() => normToken('<POINT*>ptr!')).includes('Unknown token'))
|
|
27
|
+
t.checkTrue("'*'+'@' rejected", throws(() => normToken('<POINT*>ptr@Foo')).includes('Unknown token'))
|
|
28
|
+
t.checkTrue('bare <*>ptr rejected', throws(() => normToken('<*>ptr')).includes('Unknown token'))
|
|
29
|
+
t.checkTrue("'+' not in lexicon: generic Unknown token", throws(() => normToken('<POINT+>ptr')).includes('Unknown token'))
|
|
30
|
+
t.checkTrue('name charset tightened (non-identifier rejected)',
|
|
31
|
+
throws(() => normToken('<1abc>ptr')).includes('Unknown token'))
|
|
32
|
+
t.check('plain <X>ptr unchanged', '<X>ptr', normToken('<X>ptr'))
|
|
33
|
+
t.check('!-form unchanged', '<X>ptr!', normToken('<X>ptr!'))
|
|
34
|
+
t.check('@-suffix stripped (enum annotation is type-layer only)', '<X>ptr', normToken('<X>ptr@Foo'))
|
|
35
|
+
|
|
36
|
+
t.section('type layer: registered encoder derives the array value domain')
|
|
37
|
+
// 注册数组 encoder 后:<POINT>ptr 位 = 非空元素数组(值域由 encoder 推导)∪ number 直通
|
|
38
|
+
const PolylineT = bind('gdi32.dll', 'Polyline', '<HDC>ptr! <POINT>ptr i32 -> i32', { POINT: POINTArr })
|
|
39
|
+
expectType<Equal<Parameters<typeof PolylineT>[1],
|
|
40
|
+
readonly { readonly x?: number | undefined; readonly y?: number | undefined }[] | MaybePtr<'POINT'>>>()
|
|
41
|
+
expectType<Equal<Parameters<typeof PolylineT>[0], Ptr<'HDC'>>>()
|
|
42
|
+
// 类型专用块(只过 tsc、不执行)
|
|
43
|
+
const typeOnly: unknown = () => {
|
|
44
|
+
// @ts-expect-error 单对象不是元素数组(encoder 值域收数组,不收裸 POINT)
|
|
45
|
+
PolylineT(0 as Ptr<'HDC'>, { x: 1, y: 2 }, 1)
|
|
46
|
+
// @ts-expect-error 品牌拦错布局直传(RECT PAB 不是 POINT 位的值域——传 .ptr 走 number 直通)
|
|
47
|
+
PolylineT(0 as Ptr<'HDC'>, RECT.alloc(), 1)
|
|
48
|
+
// @ts-expect-error decode 的裸地址形 count 必填(地址无长度信息)
|
|
49
|
+
POINTArr.decode(POINT.alloc().ptr)
|
|
50
|
+
}
|
|
51
|
+
t.checkTrue('type-only block wired', typeof typeOnly === 'function')
|
|
52
|
+
|
|
53
|
+
t.section('structArray.encode: contiguous branded materialization')
|
|
54
|
+
const pab = POINTArr.encode([{ x: 1, y: 2 }, { x: -3, y: 4 }])
|
|
55
|
+
t.check('byteLength = n * size', 16, pab.byteLength)
|
|
56
|
+
const pdv = new DataView(pab)
|
|
57
|
+
t.check('[0].x', 1, pdv.getInt32(0, true))
|
|
58
|
+
t.check('[0].y', 2, pdv.getInt32(4, true))
|
|
59
|
+
t.check('[1].x', -3, pdv.getInt32(8, true))
|
|
60
|
+
t.check('[1].y', 4, pdv.getInt32(12, true))
|
|
61
|
+
t.checkTrue('PAB .ptr non-null (branded)', pab.ptr !== 0)
|
|
62
|
+
// fail-loud:值域刻意收普通数组(直传变量),空/非数组/单对象全靠运行时
|
|
63
|
+
// throw —— 任何路径都不产出 PAB(0)(其 .ptr 是"非 0 的 1 字节堆指针",会骗过
|
|
64
|
+
// C 的 NULL 防御)。
|
|
65
|
+
t.checkTrue('[] fail-loud (never produces PAB(0))',
|
|
66
|
+
throws(() => POINTArr.encode([])).includes('non-empty'))
|
|
67
|
+
t.checkTrue('non-array fail-loud',
|
|
68
|
+
throws(() => POINTArr.encode(123 as never)).includes('non-empty'))
|
|
69
|
+
|
|
70
|
+
t.section('structArray.decode: self-evident / explicit count / address form')
|
|
71
|
+
// 自算形态:纯元素序列 buffer(alloc)byteLength/size 整除自证
|
|
72
|
+
t.check('self-computed n = byteLength/size', 3, POINTArr.decode(POINT.alloc(3)).length)
|
|
73
|
+
const rt = POINTArr.decode(POINTArr.encode([{ x: 1, y: -2 }, { x: 3, y: 4 }]))
|
|
74
|
+
t.check('roundtrip [0].x', 1, rt[0]!.x)
|
|
75
|
+
t.check('roundtrip [1].y', 4, rt[1]!.y)
|
|
76
|
+
// 混合缓冲(byteLength 非整数倍,如 EnumPrinters 的结构 + 尾部字符串)自算 fail-loud
|
|
77
|
+
t.checkTrue('non-multiple byteLength fail-loud',
|
|
78
|
+
throws(() => POINTArr.decode(new PtrArrayBuffer(7))).includes('multiple'))
|
|
79
|
+
// 显式 count 越界防线:count*size 超 byteLength → 拦死(写错 count 的越界读入口)
|
|
80
|
+
t.checkTrue('count beyond byteLength fail-loud',
|
|
81
|
+
throws(() => POINTArr.decode(new PtrArrayBuffer(12), 2)).includes('exceeds'))
|
|
82
|
+
t.checkTrue('negative count fail-loud',
|
|
83
|
+
throws(() => POINTArr.decode(new PtrArrayBuffer(16), -1)).includes('non-negative'))
|
|
84
|
+
t.check('count = 0 → []', 0, POINTArr.decode(POINT.alloc(), 0).length)
|
|
85
|
+
// 裸地址形(C 返回的 T* / 预建缓冲 .ptr):count 必填,逐字节拷读
|
|
86
|
+
const addrBack = POINTArr.decode(pab.ptr, 2)
|
|
87
|
+
t.check('address form count', 2, addrBack.length)
|
|
88
|
+
t.check('address form [0].x', 1, addrBack[0]!.x)
|
|
89
|
+
t.checkTrue('address without count throws at runtime (dynamic path)',
|
|
90
|
+
throws(() => (POINTArr.decode as unknown as (p: number) => unknown[])(pab.ptr)).includes('count'))
|
|
91
|
+
|
|
92
|
+
t.section('StructDef.alloc(n): n-slot zero-filled (default 1)')
|
|
93
|
+
t.check('alloc() default = 1 slot', POINT.size, POINT.alloc().byteLength)
|
|
94
|
+
t.check('alloc(3) = 3 slots', POINT.size * 3, POINT.alloc(3).byteLength)
|
|
95
|
+
t.checkTrue('alloc slots zero-filled', new Int32Array(POINT.alloc(2))[3] === 0)
|
|
96
|
+
t.checkTrue('alloc(0) fail-loud (PAB(0) never escapes)',
|
|
97
|
+
throws(() => POINT.alloc(0)).includes('n >= 1'))
|
|
98
|
+
t.checkTrue('alloc(-1) fail-loud', throws(() => POINT.alloc(-1)).includes('n >= 1'))
|
|
99
|
+
t.checkTrue('alloc(1.5) fail-loud', throws(() => POINT.alloc(1.5)).includes('n >= 1'))
|
|
100
|
+
|
|
101
|
+
t.section('StructDef.encode(v, buf, offset): positioned write primitive')
|
|
102
|
+
const big = POINT.alloc(2)
|
|
103
|
+
POINT.encode({ x: 7, y: -8 }, big, POINT.size) // 写入第 2 槽
|
|
104
|
+
const bdv = new DataView(big)
|
|
105
|
+
t.check('slot 0 stays zero', 0, bdv.getInt32(0, true))
|
|
106
|
+
t.check('slot 1.x written at offset', 7, bdv.getInt32(POINT.size, true))
|
|
107
|
+
t.check('slot 1.y written at offset', -8, bdv.getInt32(POINT.size + 4, true))
|
|
108
|
+
t.checkTrue('offset form returns the buf itself', POINT.encode({ x: 1, y: 2 }, big, 0) === big)
|
|
109
|
+
|
|
110
|
+
t.section('registered encoder dispatch: kernel32!IsBadReadPtr observes the pointer C gets')
|
|
111
|
+
const isBad = bind('kernel32.dll', 'IsBadReadPtr', '<POINT>ptr u64 -> i32', { POINT: POINTArr })
|
|
112
|
+
t.check('3-point array: whole 24-byte region readable', 0, isBad([{ x: 1, y: 2 }, { x: 3, y: 4 }, { x: 5, y: 6 }], 24n))
|
|
113
|
+
t.check('number passthrough: prebuilt PAB .ptr', 0, isBad(pab.ptr, 16n))
|
|
114
|
+
t.check('number passthrough: NULL literal', 1, isBad(NULL, 1n))
|
|
115
|
+
t.checkTrue('bare object rejected at encoder',
|
|
116
|
+
throws(() => isBad({ x: 1, y: 2 } as never, 8n)).includes('non-empty'))
|
|
117
|
+
t.checkTrue('[] rejected at encoder (NULL stays explicit)',
|
|
118
|
+
throws(() => isBad([], 8n)).includes('non-empty'))
|
|
119
|
+
t.checkTrue('null rejected with NULL hint',
|
|
120
|
+
throws(() => isBad(null as never, 8n)).includes('NULL'))
|
|
121
|
+
|
|
122
|
+
t.section('content fidelity: msvcrt!memcpy reads encoder array source')
|
|
123
|
+
const memcpy = bind('msvcrt.dll', 'memcpy', '<POINT>ptr <POINT>ptr u64 -> <>ptr', { POINT: POINTArr })
|
|
124
|
+
const dst = POINT.alloc()
|
|
125
|
+
memcpy(dst.ptr, [{ x: 7, y: -8 }], 8n)
|
|
126
|
+
const back = POINT.decode(dst.ptr)
|
|
127
|
+
t.check('copied x', 7, back.x)
|
|
128
|
+
t.check('copied y', -8, back.y)
|
|
129
|
+
|
|
130
|
+
t.section("decode(alloc()): '!' unfilled field fails (C did not fill)")
|
|
131
|
+
const OutH = struct('OutH', { h: '<HANDLE>ptr!' })
|
|
132
|
+
t.checkTrue("'!' 0 slot throws on decode",
|
|
133
|
+
throws(() => OutH.decode(OutH.alloc())).includes("'!'"))
|
|
134
|
+
|
|
135
|
+
t.section('bind-time lexicon checks (fail at module load)')
|
|
136
|
+
t.checkTrue('<POINT+>ptr rejected at bind (not in lexicon)',
|
|
137
|
+
throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT+>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'))
|
|
138
|
+
t.checkTrue('<POINT*>ptr rejected at bind (out-of-lexicon)',
|
|
139
|
+
throws(() => bind('kernel32.dll', 'IsBadReadPtr', '<POINT*>ptr u64 -> i32', { POINT: POINTArr })).includes('Unknown token'))
|
|
140
|
+
}
|
|
141
|
+
}
|