@node-ctypes/comtypes 0.3.0 → 0.4.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.md +50 -624
- package/dist/automation/dispatch.js +10 -3
- package/dist/automation/record.js +3 -1
- package/dist/automation/variant.d.ts +4 -4
- package/dist/automation/variant.js +102 -29
- package/dist/client/dynamic.js +7 -8
- package/dist/client/events.js +18 -2
- package/dist/client/index.js +3 -1
- package/dist/commethod.d.ts +1 -1
- package/dist/commethod.js +151 -57
- package/dist/errorinfo.d.ts +10 -3
- package/dist/errorinfo.js +66 -18
- package/dist/errors.d.ts +7 -1
- package/dist/executor/apartment.d.ts +7 -3
- package/dist/executor/apartment.js +65 -29
- package/dist/executor/executor.d.ts +2 -0
- package/dist/executor/executor.js +65 -14
- package/dist/guid.d.ts +1 -1
- package/dist/guid.js +15 -10
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/interfaces/agile.d.ts +52 -0
- package/dist/interfaces/agile.js +59 -0
- package/dist/interfaces/errorinfo.d.ts +8 -0
- package/dist/interfaces/errorinfo.js +10 -0
- package/dist/interfaces/index.d.ts +2 -1
- package/dist/interfaces/index.js +2 -1
- package/dist/interfaces/typeinfo.js +3 -0
- package/dist/low-level/iids.d.ts +14 -0
- package/dist/low-level/iids.js +14 -0
- package/dist/low-level/index.d.ts +2 -2
- package/dist/low-level/index.js +1 -1
- package/dist/low-level/native.d.ts +23 -0
- package/dist/low-level/native.js +53 -0
- package/dist/low-level/registry.js +4 -2
- package/dist/low-level/vtable.d.ts +10 -0
- package/dist/low-level/vtable.js +32 -5
- package/dist/pointer.d.ts +2 -0
- package/dist/pointer.js +154 -84
- package/dist/pump.d.ts +32 -8
- package/dist/pump.js +87 -15
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.js +15 -1
- package/dist/server/automation.js +3 -3
- package/dist/server/comobject.d.ts +53 -0
- package/dist/server/comobject.js +319 -27
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/marshal.d.ts +1 -0
- package/dist/server/marshal.js +16 -8
- package/dist/server/register.js +8 -6
- package/dist/tracking.d.ts +2 -0
- package/dist/tracking.js +13 -0
- package/package.json +6 -2
package/dist/low-level/native.js
CHANGED
|
@@ -2,6 +2,15 @@ import * as ctypes from "node-ctypes";
|
|
|
2
2
|
const { c_int32, c_uint16, c_uint32, c_void_p, c_wchar_p, c_size_t } = ctypes;
|
|
3
3
|
export const POINTER_SIZE = ctypes.sizeof(c_void_p);
|
|
4
4
|
export const isWindows = process.platform === "win32";
|
|
5
|
+
/** Whether `type` is a `node-ctypes` `Structure` or `Union` class. */
|
|
6
|
+
export function isAggregateClass(type) {
|
|
7
|
+
return typeof type === "function" && (type.prototype instanceof ctypes.Structure || type.prototype instanceof ctypes.Union);
|
|
8
|
+
}
|
|
9
|
+
/** Whether `type` is a `node-ctypes` `POINTER(T)` class/definition. */
|
|
10
|
+
export function isPointerType(type) {
|
|
11
|
+
// POINTER(T) is a class (as in CPython); _pointerTo is undefined on other types.
|
|
12
|
+
return (typeof type === "function" || (typeof type === "object" && type !== null)) && type._pointerTo !== undefined;
|
|
13
|
+
}
|
|
5
14
|
// HRESULT-returning functions are bound with `c_int32` (not `HRESULT`): the
|
|
6
15
|
// raw value is returned and checked by the caller, which builds a COMError.
|
|
7
16
|
function bindOle32(library) {
|
|
@@ -26,6 +35,14 @@ function bindOle32(library) {
|
|
|
26
35
|
CoWaitForMultipleHandles: library.func("CoWaitForMultipleHandles", c_int32, [c_uint32, c_uint32, c_uint32, c_void_p, c_void_p]),
|
|
27
36
|
CoRegisterClassObject: library.func("CoRegisterClassObject", c_int32, [c_void_p, c_void_p, c_uint32, c_uint32, c_void_p]),
|
|
28
37
|
CoRevokeClassObject: library.func("CoRevokeClassObject", c_int32, [c_uint32]),
|
|
38
|
+
CoCreateFreeThreadedMarshaler: library.func("CoCreateFreeThreadedMarshaler", c_int32, [c_void_p, c_void_p]),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function bindCombase(library) {
|
|
42
|
+
return {
|
|
43
|
+
GetRestrictedErrorInfo: library.func("GetRestrictedErrorInfo", c_int32, [c_void_p]),
|
|
44
|
+
SetRestrictedErrorInfo: library.func("SetRestrictedErrorInfo", c_int32, [c_void_p]),
|
|
45
|
+
RoOriginateErrorW: library.func("RoOriginateErrorW", c_int32, [c_int32, c_uint32, c_wchar_p]),
|
|
29
46
|
};
|
|
30
47
|
}
|
|
31
48
|
function bindOleAut32(library) {
|
|
@@ -67,6 +84,8 @@ function bindOleAut32(library) {
|
|
|
67
84
|
}
|
|
68
85
|
let ole32Functions = null;
|
|
69
86
|
let oleaut32Functions = null;
|
|
87
|
+
let combaseFunctions = null;
|
|
88
|
+
let combaseUnavailable = false;
|
|
70
89
|
function requireWindows() {
|
|
71
90
|
if (!isWindows)
|
|
72
91
|
throw new Error(`COM is only available on Windows (current platform: ${process.platform})`);
|
|
@@ -81,6 +100,20 @@ export function oleaut32() {
|
|
|
81
100
|
requireWindows();
|
|
82
101
|
return (oleaut32Functions ??= bindOleAut32(new ctypes.WinDLL("oleaut32.dll")));
|
|
83
102
|
}
|
|
103
|
+
/** combase.dll functions (WinRT / restricted error info), loaded on first use; `null` if unavailable. */
|
|
104
|
+
export function combase() {
|
|
105
|
+
if (!isWindows || combaseUnavailable)
|
|
106
|
+
return null;
|
|
107
|
+
if (combaseFunctions)
|
|
108
|
+
return combaseFunctions;
|
|
109
|
+
try {
|
|
110
|
+
return (combaseFunctions = bindCombase(new ctypes.WinDLL("combase.dll")));
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
combaseUnavailable = true;
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
84
117
|
/** Allocate a BSTR (embedded NULs preserved). Free it with `SysFreeString`. */
|
|
85
118
|
export function allocBSTR(text) {
|
|
86
119
|
const pointer = oleaut32().SysAllocStringLen(Buffer.from(text, "utf16le"), text.length);
|
|
@@ -129,6 +162,26 @@ export function writeMemory(address, data) {
|
|
|
129
162
|
if (data.length)
|
|
130
163
|
ctypes.memmove(address, data, data.length);
|
|
131
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Copy current native bytes into `buffer` when external buffers are disabled
|
|
167
|
+
* (`NODE_CTYPES_NO_EXTERNAL_BUFFERS=1` / Electron). No-op when `EXTERNAL_BUFFERS` is true.
|
|
168
|
+
*/
|
|
169
|
+
export function refreshMirror(buffer) {
|
|
170
|
+
if (ctypes.EXTERNAL_BUFFERS || !buffer.length)
|
|
171
|
+
return;
|
|
172
|
+
const copy = Buffer.allocUnsafe(buffer.length);
|
|
173
|
+
ctypes.memmove(copy, buffer, buffer.length);
|
|
174
|
+
copy.copy(buffer);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Copy bytes from `buffer` back to native memory when external buffers are disabled
|
|
178
|
+
* (`NODE_CTYPES_NO_EXTERNAL_BUFFERS=1` / Electron). No-op when `EXTERNAL_BUFFERS` is true.
|
|
179
|
+
*/
|
|
180
|
+
export function flushMirror(buffer) {
|
|
181
|
+
if (ctypes.EXTERNAL_BUFFERS || !buffer.length)
|
|
182
|
+
return;
|
|
183
|
+
ctypes.memmove(buffer, Buffer.from(buffer), buffer.length);
|
|
184
|
+
}
|
|
132
185
|
/** Read a CoTaskMem-allocated wide string and free it. */
|
|
133
186
|
export function takeCoTaskMemString(pointer) {
|
|
134
187
|
if (!pointer)
|
|
@@ -53,9 +53,11 @@ export function subkeys(root, key) {
|
|
|
53
53
|
try {
|
|
54
54
|
const names = [];
|
|
55
55
|
const name = Buffer.alloc(512);
|
|
56
|
+
const length = new ctypes.c_uint32();
|
|
57
|
+
const lengthRef = ctypes.byref(length);
|
|
56
58
|
for (let index = 0;; index++) {
|
|
57
|
-
|
|
58
|
-
const result = api().RegEnumKeyExW(handle.value, index, name,
|
|
59
|
+
length.value = name.length / 2;
|
|
60
|
+
const result = api().RegEnumKeyExW(handle.value, index, name, lengthRef, null, null, null, null);
|
|
59
61
|
if (result === ERROR_NO_MORE_ITEMS)
|
|
60
62
|
return names;
|
|
61
63
|
if (result !== ERROR_SUCCESS)
|
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import * as ctypes from "node-ctypes";
|
|
2
|
+
/** Read the vtable pointer of the COM object at `object`. */
|
|
3
|
+
export declare function vtableOf(object: bigint): bigint;
|
|
4
|
+
/** Address of the function in vtable slot `slot` of `vtable`. */
|
|
5
|
+
export declare function vtableEntryFromTable(vtable: bigint, slot: number): bigint;
|
|
2
6
|
/** Address of the function in vtable slot `slot` of the COM object at `object`. */
|
|
3
7
|
export declare function vtableEntry(object: bigint, slot: number): bigint;
|
|
8
|
+
/** Format a native 16-byte GUID at `address` directly into `{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}` without allocating a `GUID` Structure. */
|
|
9
|
+
export declare function readGuidString(address: bigint): string;
|
|
4
10
|
/**
|
|
5
11
|
* Bind function pointers of one prototype, cached by address: binding costs
|
|
6
12
|
* far more than calling, and vtables are shared by all objects of a class.
|
|
13
|
+
* Bounded to prevent unbounded growth in long-running processes with dynamic DLLs.
|
|
7
14
|
*/
|
|
8
15
|
export declare function functionCache<TRet extends ctypes.FFIType | null, const TArgs extends readonly ctypes.FFIType[]>(prototype: () => ctypes.FuncPtrFactory<TRet, TArgs>): (address: bigint) => ctypes.TypedFFIFunction<TArgs, TRet>;
|
|
16
|
+
export declare const QueryInterfaceAt: (address: bigint) => ctypes.TypedFFIFunction<readonly [ctypes.SimpleCDataConstructor<"pointer">, ctypes.SimpleCDataConstructor<"pointer">, ctypes.SimpleCDataConstructor<"pointer">], ctypes.SimpleCDataConstructor<"int32">>;
|
|
17
|
+
export declare const RefCountAt: (address: bigint) => ctypes.TypedFFIFunction<readonly [ctypes.SimpleCDataConstructor<"pointer">], ctypes.SimpleCDataConstructor<"uint32">>;
|
|
18
|
+
export declare const PointerArgAt: (address: bigint) => ctypes.TypedFFIFunction<readonly [ctypes.SimpleCDataConstructor<"pointer">, ctypes.SimpleCDataConstructor<"pointer">], ctypes.SimpleCDataConstructor<"int32">>;
|
|
9
19
|
export declare function rawQueryInterface(object: bigint, iid: {
|
|
10
20
|
readonly _buffer: Buffer;
|
|
11
21
|
}): [hr: number, pointer: bigint | null];
|
package/dist/low-level/vtable.js
CHANGED
|
@@ -1,34 +1,61 @@
|
|
|
1
1
|
import * as ctypes from "node-ctypes";
|
|
2
2
|
import { POINTER_SIZE } from "./native.js";
|
|
3
3
|
const { c_int32, c_uint32, c_void_p } = ctypes;
|
|
4
|
-
/**
|
|
5
|
-
export function
|
|
4
|
+
/** Read the vtable pointer of the COM object at `object`. */
|
|
5
|
+
export function vtableOf(object) {
|
|
6
6
|
if (!object)
|
|
7
7
|
throw new TypeError("NULL COM pointer");
|
|
8
8
|
const vtable = ctypes.readValue(object, c_void_p);
|
|
9
9
|
if (!vtable)
|
|
10
10
|
throw new TypeError("COM object has a NULL vtable");
|
|
11
|
+
return vtable;
|
|
12
|
+
}
|
|
13
|
+
/** Address of the function in vtable slot `slot` of `vtable`. */
|
|
14
|
+
export function vtableEntryFromTable(vtable, slot) {
|
|
11
15
|
const entry = ctypes.readValue(vtable, c_void_p, slot * POINTER_SIZE);
|
|
12
16
|
if (!entry)
|
|
13
17
|
throw new TypeError(`COM vtable slot ${slot} is NULL`);
|
|
14
18
|
return entry;
|
|
15
19
|
}
|
|
20
|
+
/** Address of the function in vtable slot `slot` of the COM object at `object`. */
|
|
21
|
+
export function vtableEntry(object, slot) {
|
|
22
|
+
return vtableEntryFromTable(vtableOf(object), slot);
|
|
23
|
+
}
|
|
24
|
+
const guidScratch = Buffer.allocUnsafe(16);
|
|
25
|
+
/** Format a native 16-byte GUID at `address` directly into `{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}` without allocating a `GUID` Structure. */
|
|
26
|
+
export function readGuidString(address) {
|
|
27
|
+
ctypes.memmove(guidScratch, address, 16);
|
|
28
|
+
const d1 = guidScratch.readUInt32LE(0).toString(16).padStart(8, "0");
|
|
29
|
+
const d2 = guidScratch.readUInt16LE(4).toString(16).padStart(4, "0");
|
|
30
|
+
const d3 = guidScratch.readUInt16LE(6).toString(16).padStart(4, "0");
|
|
31
|
+
const tail = guidScratch.subarray(8, 16).toString("hex");
|
|
32
|
+
return `{${d1}-${d2}-${d3}-${tail.slice(0, 4)}-${tail.slice(4)}}`.toUpperCase();
|
|
33
|
+
}
|
|
34
|
+
const MAX_FUNCTION_CACHE = 4096;
|
|
16
35
|
/**
|
|
17
36
|
* Bind function pointers of one prototype, cached by address: binding costs
|
|
18
37
|
* far more than calling, and vtables are shared by all objects of a class.
|
|
38
|
+
* Bounded to prevent unbounded growth in long-running processes with dynamic DLLs.
|
|
19
39
|
*/
|
|
20
40
|
export function functionCache(prototype) {
|
|
21
41
|
let factory = null;
|
|
22
42
|
const cache = new Map();
|
|
23
43
|
return (address) => {
|
|
24
44
|
let fn = cache.get(address);
|
|
25
|
-
if (!fn)
|
|
45
|
+
if (!fn) {
|
|
46
|
+
if (cache.size >= MAX_FUNCTION_CACHE) {
|
|
47
|
+
const oldest = cache.keys().next().value;
|
|
48
|
+
if (oldest !== undefined)
|
|
49
|
+
cache.delete(oldest);
|
|
50
|
+
}
|
|
26
51
|
cache.set(address, (fn = (factory ??= prototype())(address)));
|
|
52
|
+
}
|
|
27
53
|
return fn;
|
|
28
54
|
};
|
|
29
55
|
}
|
|
30
|
-
const QueryInterfaceAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p, c_void_p));
|
|
31
|
-
const RefCountAt = functionCache(() => ctypes.WINFUNCTYPE(c_uint32, c_void_p));
|
|
56
|
+
export const QueryInterfaceAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p, c_void_p));
|
|
57
|
+
export const RefCountAt = functionCache(() => ctypes.WINFUNCTYPE(c_uint32, c_void_p));
|
|
58
|
+
export const PointerArgAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p));
|
|
32
59
|
// Raw IUnknown calls without errcheck: used by error handling, finalizers
|
|
33
60
|
// and lifetime tracking, where throwing or recursion is not acceptable.
|
|
34
61
|
export function rawQueryInterface(object, iid) {
|
package/dist/pointer.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { Operation } from "./executor/operations.ts";
|
|
|
3
3
|
import { GUID } from "./guid.ts";
|
|
4
4
|
import type { InterfaceDef, InterfacePointer, MethodSpec, SyncPointer } from "./interface.ts";
|
|
5
5
|
type Callable = (...args: any[]) => any;
|
|
6
|
+
declare const COM_POINTER_BRAND: unique symbol;
|
|
6
7
|
/** How an asynchronous call reaches the executor. */
|
|
7
8
|
export interface AsyncContext {
|
|
8
9
|
/** The home thread is single-threaded: interface results are handed back to it. */
|
|
@@ -92,6 +93,7 @@ export declare function sync<I extends InterfaceDef<any>>(pointer: COMPointer<I>
|
|
|
92
93
|
*/
|
|
93
94
|
export declare class COMPointer<I extends InterfaceDef = InterfaceDef> implements Disposable {
|
|
94
95
|
#private;
|
|
96
|
+
readonly [COM_POINTER_BRAND] = true;
|
|
95
97
|
readonly interface: I;
|
|
96
98
|
constructor(address: bigint, iface: I);
|
|
97
99
|
/** Wrap a raw interface pointer. */
|
package/dist/pointer.js
CHANGED
|
@@ -6,9 +6,32 @@ import { executorAlive, executorCall, executorGeneration } from "./executor/exec
|
|
|
6
6
|
import { operation } from "./executor/operations.js";
|
|
7
7
|
import { GUID } from "./guid.js";
|
|
8
8
|
import { E_NOINTERFACE } from "./hresult.js";
|
|
9
|
-
import {
|
|
9
|
+
import { IID_IUnknown } from "./low-level/iids.js";
|
|
10
|
+
import { isAggregateClass, refreshMirror } from "./low-level/native.js";
|
|
11
|
+
import { functionCache, rawQueryInterface, rawRelease, vtableEntry, vtableEntryFromTable, vtableOf } from "./low-level/vtable.js";
|
|
10
12
|
import { track, untrack } from "./tracking.js";
|
|
11
|
-
const
|
|
13
|
+
const COM_POINTER_BRAND = Symbol.for("node-comtypes.COMPointer");
|
|
14
|
+
function isCOMPointer(value) {
|
|
15
|
+
return value instanceof COMPointer || (typeof value === "object" && value !== null && value[COM_POINTER_BRAND] === true);
|
|
16
|
+
}
|
|
17
|
+
const MAX_VTABLE_CACHES_PER_INTERFACE = 1024;
|
|
18
|
+
// Per-interface, per-vtable-pointer bound-slot cache shared across all COMPointer instances of a COM class.
|
|
19
|
+
const vtableMethodCaches = new WeakMap();
|
|
20
|
+
function vtableSlotsFor(iface, vtable) {
|
|
21
|
+
let byVtable = vtableMethodCaches.get(iface);
|
|
22
|
+
if (!byVtable)
|
|
23
|
+
vtableMethodCaches.set(iface, (byVtable = new Map()));
|
|
24
|
+
let slots = byVtable.get(vtable);
|
|
25
|
+
if (!slots) {
|
|
26
|
+
if (byVtable.size >= MAX_VTABLE_CACHES_PER_INTERFACE) {
|
|
27
|
+
const oldest = byVtable.keys().next().value;
|
|
28
|
+
if (oldest !== undefined)
|
|
29
|
+
byVtable.delete(oldest);
|
|
30
|
+
}
|
|
31
|
+
byVtable.set(vtable, (slots = []));
|
|
32
|
+
}
|
|
33
|
+
return slots;
|
|
34
|
+
}
|
|
12
35
|
/**
|
|
13
36
|
* Create the slot for `spec`, declared by interface `owner`. HRESULT methods
|
|
14
37
|
* are bound with a `c_int32` restype and an errcheck raising {@link COMError},
|
|
@@ -17,22 +40,41 @@ const IID_IUnknown = new GUID("{00000000-0000-0000-C000-000000000046}");
|
|
|
17
40
|
export function createSlot(spec, owner) {
|
|
18
41
|
const label = `${owner.name}::${spec.name}`;
|
|
19
42
|
const hresult = spec.restype === ctypes.HRESULT;
|
|
43
|
+
// MSVC C++ member-function ABI (x86/x64/ARM64): COM virtual methods returning
|
|
44
|
+
// a struct/union by value always receive a hidden return buffer pointer (`sret`)
|
|
45
|
+
// as the second argument right after `this`, even for 8-byte POD structs
|
|
46
|
+
// (e.g. Direct2D `GetSize()`, `GetPixelFormat()`, Direct3D `GetDesc()`).
|
|
47
|
+
const structReturn = !hresult && isAggregateClass(spec.restype) ? spec.restype : null;
|
|
20
48
|
// IUnknown methods never set error info (and QueryInterface failures are common).
|
|
21
49
|
const errorInfo = !owner.iid.equals(IID_IUnknown);
|
|
22
|
-
const nativeRestype = hresult ? ctypes.c_int32 : spec.restype;
|
|
50
|
+
const nativeRestype = hresult ? ctypes.c_int32 : structReturn ? ctypes.c_void_p : spec.restype;
|
|
23
51
|
// One array per slot: the executor caches its bindings by it.
|
|
24
|
-
const nativeArgtypes = [ctypes.c_void_p, ...spec.argtypes];
|
|
25
|
-
const cache = functionCache(() => ctypes.WINFUNCTYPE(nativeRestype,
|
|
52
|
+
const nativeArgtypes = structReturn ? [ctypes.c_void_p, ctypes.c_void_p, ...spec.argtypes] : [ctypes.c_void_p, ...spec.argtypes];
|
|
53
|
+
const cache = functionCache(() => ctypes.WINFUNCTYPE(nativeRestype, ...nativeArgtypes));
|
|
26
54
|
const errcheck = (result, _fn, args) => {
|
|
27
55
|
if (result < 0)
|
|
28
|
-
throw new COMError(result, { method: label, details: errorInfo ? errorInfoFor(args[0], owner.iid) : null });
|
|
56
|
+
throw new COMError(result, { method: label, details: errorInfo ? errorInfoFor(args[0], owner.iid, result) : null });
|
|
29
57
|
return result;
|
|
30
58
|
};
|
|
31
59
|
const { marshal, marshalAsync } = spec;
|
|
60
|
+
const invokeStructSync = structReturn
|
|
61
|
+
? (fn, address, nativeArgs) => {
|
|
62
|
+
const out = new structReturn();
|
|
63
|
+
fn(address, out._buffer, ...nativeArgs);
|
|
64
|
+
refreshMirror(out._buffer);
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
: null;
|
|
32
68
|
return {
|
|
33
69
|
spec,
|
|
34
70
|
label,
|
|
35
|
-
call:
|
|
71
|
+
call: invokeStructSync
|
|
72
|
+
? marshal
|
|
73
|
+
? (fn, address, args) => marshal((addr, ...nativeArgs) => invokeStructSync(fn, addr, nativeArgs), address, args, owner.definition)
|
|
74
|
+
: (fn, address, args) => invokeStructSync(fn, address, args)
|
|
75
|
+
: marshal
|
|
76
|
+
? (fn, address, args) => marshal(fn, address, args, owner.definition)
|
|
77
|
+
: (fn, address, args) => fn(address, ...args),
|
|
36
78
|
bind(address) {
|
|
37
79
|
const fn = cache(address);
|
|
38
80
|
if (hresult && fn.errcheck !== errcheck)
|
|
@@ -41,12 +83,21 @@ export function createSlot(spec, owner) {
|
|
|
41
83
|
},
|
|
42
84
|
callAsync(entry, target, args, context) {
|
|
43
85
|
// Promise steps instead of an async function: this runs for every method call.
|
|
44
|
-
const run =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
86
|
+
const run = structReturn
|
|
87
|
+
? (native, results) => {
|
|
88
|
+
const out = new structReturn();
|
|
89
|
+
return executorCall({ fn: entry, restype: nativeRestype, argtypes: nativeArgtypes, args: [target, out._buffer, ...native] }, { operation: context.operation }).then(() => {
|
|
90
|
+
refreshMirror(out._buffer);
|
|
91
|
+
const locations = context.results?.length ? [...results, ...context.results] : results;
|
|
92
|
+
return context.handOver && locations.length ? handOver(locations).then(() => out) : out;
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
: (native, results) => executorCall({ fn: entry, restype: nativeRestype, argtypes: nativeArgtypes, args: [target, ...native], ...(hresult && errorInfo ? { errorInfo: { object: target, iid: owner.iid } } : {}) }, { operation: context.operation }).then(({ value, details }) => {
|
|
96
|
+
if (hresult && value < 0)
|
|
97
|
+
throw new COMError(value, { method: label, details });
|
|
98
|
+
const locations = context.results?.length ? [...results, ...context.results] : results;
|
|
99
|
+
return context.handOver && locations.length ? handOver(locations).then(() => value) : value;
|
|
100
|
+
});
|
|
50
101
|
return marshalAsync ? marshalAsync(run, args, owner.definition) : run(args, []);
|
|
51
102
|
},
|
|
52
103
|
};
|
|
@@ -97,7 +148,7 @@ export function sharedWithExecutor(pointer) {
|
|
|
97
148
|
* handlers, COM servers and code that needs the lowest latency.
|
|
98
149
|
*/
|
|
99
150
|
export function sync(pointer) {
|
|
100
|
-
if (!(pointer
|
|
151
|
+
if (!isCOMPointer(pointer))
|
|
101
152
|
throw new TypeError("sync() expects a COM pointer");
|
|
102
153
|
return createView(pointer);
|
|
103
154
|
}
|
|
@@ -116,10 +167,12 @@ let viewOf = null;
|
|
|
116
167
|
* or {@link COMPointer.fromAddress}.
|
|
117
168
|
*/
|
|
118
169
|
export class COMPointer {
|
|
170
|
+
[COM_POINTER_BRAND] = true;
|
|
119
171
|
interface;
|
|
120
172
|
#reference;
|
|
121
|
-
#
|
|
122
|
-
#
|
|
173
|
+
#vtable = 0n;
|
|
174
|
+
#bound = null;
|
|
175
|
+
#state = null;
|
|
123
176
|
// A view keeps its pointer (the owner tracked for release) alive.
|
|
124
177
|
#owner = null;
|
|
125
178
|
static {
|
|
@@ -127,12 +180,13 @@ export class COMPointer {
|
|
|
127
180
|
invokeAsync = (pointer, slot, args, options) => pointer.#invokeAsync(slot, args, options);
|
|
128
181
|
shared = (pointer) => pointer.#shared();
|
|
129
182
|
createView = (pointer) => {
|
|
130
|
-
|
|
131
|
-
|
|
183
|
+
const state = pointer.#ensureState();
|
|
184
|
+
if (state.view)
|
|
185
|
+
return state.view;
|
|
132
186
|
viewOf = pointer;
|
|
133
187
|
try {
|
|
134
188
|
const SyncClass = pointer.interface.SyncPointer;
|
|
135
|
-
return (
|
|
189
|
+
return (state.view = new SyncClass(pointer.#reference.address, pointer.interface));
|
|
136
190
|
}
|
|
137
191
|
finally {
|
|
138
192
|
viewOf = null;
|
|
@@ -146,13 +200,17 @@ export class COMPointer {
|
|
|
146
200
|
if (viewOf) {
|
|
147
201
|
// A synchronous view: same reference, same state.
|
|
148
202
|
this.#reference = viewOf.#reference;
|
|
149
|
-
this.#
|
|
203
|
+
this.#vtable = viewOf.#vtable;
|
|
204
|
+
this.#bound = viewOf.#bound;
|
|
150
205
|
this.#state = viewOf.#state;
|
|
151
206
|
this.#owner = viewOf;
|
|
152
207
|
return;
|
|
153
208
|
}
|
|
154
209
|
this.#reference = track(address, this);
|
|
155
210
|
}
|
|
211
|
+
#ensureState() {
|
|
212
|
+
return (this.#state ??= { route: null, inflight: 0, view: null, entries: null });
|
|
213
|
+
}
|
|
156
214
|
/** Wrap a raw interface pointer. */
|
|
157
215
|
static fromAddress(address, iface, options = {}) {
|
|
158
216
|
if (typeof address !== "bigint" || address === 0n)
|
|
@@ -176,69 +234,72 @@ export class COMPointer {
|
|
|
176
234
|
#invoke(slot, args) {
|
|
177
235
|
const { address } = this.#live();
|
|
178
236
|
const binding = this.#slot(slot);
|
|
179
|
-
let
|
|
237
|
+
let bound = this.#bound;
|
|
238
|
+
if (bound === null) {
|
|
239
|
+
const vtable = (this.#vtable = vtableOf(address));
|
|
240
|
+
bound = this.#bound = vtableSlotsFor(this.interface, vtable);
|
|
241
|
+
if (this.#owner && this.#owner.#bound === null) {
|
|
242
|
+
this.#owner.#vtable = vtable;
|
|
243
|
+
this.#owner.#bound = bound;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
let fn = bound[slot];
|
|
180
247
|
if (fn === undefined)
|
|
181
|
-
fn =
|
|
248
|
+
fn = bound[slot] = binding.bind(vtableEntryFromTable(this.#vtable, slot));
|
|
182
249
|
return binding.call(fn, address, args);
|
|
183
250
|
}
|
|
184
|
-
/**
|
|
185
|
-
#
|
|
186
|
-
const cached = this.#state.route;
|
|
187
|
-
if (cached !== null && (cached instanceof Promise || cached.kind !== "proxy" || executorAlive(cached.generation)))
|
|
188
|
-
return cached;
|
|
189
|
-
const address = this.#live().address;
|
|
251
|
+
/** Classify the non-proxy route of `address`, or `null` when it is a cross-apartment proxy. */
|
|
252
|
+
#classifyRoute(address) {
|
|
190
253
|
const local = localThreading(address);
|
|
191
254
|
const home = homeApartment();
|
|
192
|
-
let route;
|
|
193
255
|
// Objects implemented in JavaScript run their callbacks on the home thread
|
|
194
256
|
// unless free-threaded; without COM on the home thread, calls stay there.
|
|
195
257
|
if (local === "apartment" || home === "none")
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
258
|
+
return HOME;
|
|
259
|
+
if (home === "mta")
|
|
260
|
+
return SHARED_MTA;
|
|
261
|
+
if (local === "free")
|
|
262
|
+
return SHARED_AGILE;
|
|
201
263
|
// A proxy answers IClientSecurity locally; QueryInterface(IAgileObject) would go to the server.
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
264
|
+
if (isProxy(address))
|
|
265
|
+
return null;
|
|
266
|
+
return isAgile(address) ? SHARED_AGILE : HOME;
|
|
267
|
+
}
|
|
268
|
+
/** The route for asynchronous calls, computed once (a `proxy` route again for a new executor). */
|
|
269
|
+
#route() {
|
|
270
|
+
const state = this.#ensureState();
|
|
271
|
+
const cached = state.route;
|
|
272
|
+
if (cached !== null && (cached instanceof Promise || cached.kind !== "proxy" || executorAlive(cached.generation)))
|
|
273
|
+
return cached;
|
|
274
|
+
const address = this.#live().address;
|
|
275
|
+
const classified = this.#classifyRoute(address);
|
|
276
|
+
const route = classified ?? this.#proxyRoute(address, state);
|
|
277
|
+
state.entries = null;
|
|
278
|
+
state.route = route;
|
|
210
279
|
return route;
|
|
211
280
|
}
|
|
212
|
-
#proxyRoute(address) {
|
|
213
|
-
return referenceForExecutor(address, this.interface.iid).then((
|
|
214
|
-
if (!
|
|
215
|
-
return (
|
|
216
|
-
const route = { kind: "proxy", address:
|
|
217
|
-
if (this.#reference.released) {
|
|
218
|
-
releaseOnExecutor(
|
|
219
|
-
return (
|
|
281
|
+
#proxyRoute(address, state) {
|
|
282
|
+
return referenceForExecutor(address, this.interface.iid).then((resolved) => {
|
|
283
|
+
if (!resolved)
|
|
284
|
+
return (state.route = HOME);
|
|
285
|
+
const route = { kind: "proxy", address: resolved.address, generation: resolved.generation };
|
|
286
|
+
if (this.#reference.released || !executorAlive(route.generation)) {
|
|
287
|
+
releaseOnExecutor(resolved.address, route.generation);
|
|
288
|
+
return (state.route = HOME);
|
|
220
289
|
}
|
|
221
|
-
executorReferences.register(this, { address:
|
|
222
|
-
return (
|
|
223
|
-
}, () => (
|
|
290
|
+
executorReferences.register(this, { address: resolved.address, generation: route.generation }, state);
|
|
291
|
+
return (state.route = route);
|
|
292
|
+
}, () => (state.route = HOME));
|
|
224
293
|
}
|
|
225
294
|
#shared() {
|
|
226
|
-
const route = this.#state
|
|
227
|
-
if (route !== null && !(route instanceof Promise))
|
|
228
|
-
return route.kind === "shared";
|
|
295
|
+
const route = this.#state?.route ?? null;
|
|
229
296
|
if (route !== null)
|
|
297
|
+
return !(route instanceof Promise) && route.kind === "shared";
|
|
298
|
+
const classified = this.#classifyRoute(this.#live().address);
|
|
299
|
+
if (classified === null)
|
|
230
300
|
return false;
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const home = homeApartment();
|
|
234
|
-
if (local === "apartment" || home === "none")
|
|
235
|
-
return false;
|
|
236
|
-
if (home === "mta") {
|
|
237
|
-
this.#state.route = SHARED_MTA;
|
|
238
|
-
return true;
|
|
239
|
-
}
|
|
240
|
-
if (local === "free" || (!isProxy(address) && isAgile(address))) {
|
|
241
|
-
this.#state.route = SHARED_AGILE;
|
|
301
|
+
if (classified.kind === "shared") {
|
|
302
|
+
this.#ensureState().route = classified;
|
|
242
303
|
return true;
|
|
243
304
|
}
|
|
244
305
|
return false;
|
|
@@ -260,11 +321,12 @@ export class COMPointer {
|
|
|
260
321
|
}
|
|
261
322
|
#dispatch(route, slot, binding, args, options, current) {
|
|
262
323
|
const reference = this.#live();
|
|
324
|
+
const state = this.#ensureState();
|
|
263
325
|
// A reference of an executor stopped meanwhile (finalize): route again on the current one.
|
|
264
326
|
if (route.kind === "proxy" && !executorAlive(route.generation)) {
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
|
|
327
|
+
if (state.route === route) {
|
|
328
|
+
state.route = null;
|
|
329
|
+
state.entries = null;
|
|
268
330
|
}
|
|
269
331
|
const fresh = this.#route();
|
|
270
332
|
return fresh instanceof Promise ? fresh.then((settled) => this.#dispatch(settled, slot, binding, args, options, current)) : this.#dispatch(fresh, slot, binding, args, options, current);
|
|
@@ -282,13 +344,13 @@ export class COMPointer {
|
|
|
282
344
|
const target = route.kind === "proxy" ? route.address : reference.address;
|
|
283
345
|
const handOverResults = route.kind === "proxy" || route.handOver;
|
|
284
346
|
// The vtable of an address does not change: read each entry once.
|
|
285
|
-
let entries =
|
|
347
|
+
let entries = state.entries;
|
|
286
348
|
if (entries?.address !== target)
|
|
287
|
-
entries =
|
|
349
|
+
entries = state.entries = { address: target, slots: [] };
|
|
288
350
|
const entry = (entries.slots[slot] ??= vtableEntry(target, slot));
|
|
289
|
-
|
|
351
|
+
state.inflight++;
|
|
290
352
|
return binding.callAsync(entry, target, args, { handOver: handOverResults, results, operation: current }).finally(() => {
|
|
291
|
-
|
|
353
|
+
state.inflight--;
|
|
292
354
|
});
|
|
293
355
|
}
|
|
294
356
|
/** Interface pointer value; throws once released. */
|
|
@@ -335,13 +397,16 @@ export class COMPointer {
|
|
|
335
397
|
}
|
|
336
398
|
/** Forget the executor's reference of a `proxy` route. */
|
|
337
399
|
#dropExecutorReference() {
|
|
338
|
-
const
|
|
400
|
+
const state = this.#state;
|
|
401
|
+
if (!state)
|
|
402
|
+
return;
|
|
403
|
+
const route = state.route;
|
|
339
404
|
if (route !== null && !(route instanceof Promise) && route.kind === "proxy") {
|
|
340
|
-
executorReferences.unregister(
|
|
405
|
+
executorReferences.unregister(state);
|
|
341
406
|
releaseOnExecutor(route.address, route.generation);
|
|
342
407
|
}
|
|
343
|
-
|
|
344
|
-
|
|
408
|
+
state.route = null;
|
|
409
|
+
state.entries = null;
|
|
345
410
|
}
|
|
346
411
|
/**
|
|
347
412
|
* Release the owned reference and invalidate this pointer. Returns the new
|
|
@@ -350,10 +415,11 @@ export class COMPointer {
|
|
|
350
415
|
*/
|
|
351
416
|
Release() {
|
|
352
417
|
const reference = this.#live();
|
|
353
|
-
const
|
|
418
|
+
const state = this.#state;
|
|
419
|
+
const route = state?.route ?? null;
|
|
354
420
|
untrack(reference);
|
|
355
421
|
this.#dropExecutorReference();
|
|
356
|
-
if (
|
|
422
|
+
if (state && state.inflight > 0 && route !== null && !(route instanceof Promise) && route.kind === "shared") {
|
|
357
423
|
releaseOnExecutor(reference.address, executorGeneration());
|
|
358
424
|
return 0;
|
|
359
425
|
}
|
|
@@ -361,16 +427,20 @@ export class COMPointer {
|
|
|
361
427
|
}
|
|
362
428
|
/** COM identity: both pointers reach the same `IUnknown`. */
|
|
363
429
|
isSameObject(other) {
|
|
364
|
-
if (!(other
|
|
430
|
+
if (!isCOMPointer(other))
|
|
365
431
|
throw new TypeError("isSameObject expects a COMPointer");
|
|
366
|
-
const
|
|
367
|
-
|
|
432
|
+
const a = this.address;
|
|
433
|
+
const b = other.address;
|
|
434
|
+
if (a === b)
|
|
435
|
+
return true;
|
|
436
|
+
const identity = (addr) => {
|
|
437
|
+
const [hr, address] = rawQueryInterface(addr, IID_IUnknown);
|
|
368
438
|
if (hr < 0 || !address)
|
|
369
439
|
throw new COMError(hr, { method: "IUnknown::QueryInterface" });
|
|
370
440
|
rawRelease(address);
|
|
371
441
|
return address;
|
|
372
442
|
};
|
|
373
|
-
return identity(
|
|
443
|
+
return identity(a) === identity(b);
|
|
374
444
|
}
|
|
375
445
|
/** Give up ownership without releasing: the caller becomes responsible for the reference. */
|
|
376
446
|
detach() {
|
package/dist/pump.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Dispatch the messages waiting in the thread's queue, without blocking.
|
|
3
|
-
* Returns how many were dispatched (at most `limit`).
|
|
3
|
+
* Returns how many were dispatched (at most `limit`). Re-entrant safe: nested
|
|
4
|
+
* `pumpMessages()` calls from inside `DispatchMessageW` use their own MSG buffer.
|
|
4
5
|
*/
|
|
5
6
|
export declare function pumpMessages(limit?: number): number;
|
|
6
7
|
/**
|
|
@@ -8,21 +9,44 @@ export declare function pumpMessages(limit?: number): number;
|
|
|
8
9
|
* event loop does not run meanwhile). Python equivalent: `comtypes.client.PumpEvents`.
|
|
9
10
|
*/
|
|
10
11
|
export declare function PumpEvents(milliseconds: number): void;
|
|
12
|
+
/**
|
|
13
|
+
* Drain all queued Win32 messages, or if the queue is empty sleep in the
|
|
14
|
+
* Windows kernel via `MsgWaitForMultipleObjectsEx` (~0% CPU) until any input
|
|
15
|
+
* or COM STA message arrives or `timeoutMs` elapses, then drain immediately.
|
|
16
|
+
*/
|
|
17
|
+
export declare function waitAndPumpMessages(timeoutMs?: number, limit?: number): number;
|
|
11
18
|
export interface MessagePump extends Disposable {
|
|
12
19
|
stop(): void;
|
|
13
20
|
readonly running: boolean;
|
|
14
21
|
}
|
|
22
|
+
export interface StartMessagePumpOptions {
|
|
23
|
+
/**
|
|
24
|
+
* Polling interval in ms when `mode === "interval"` (default `10`), or kernel
|
|
25
|
+
* idle yield in ms when `mode === "wait"` (default `8`).
|
|
26
|
+
*/
|
|
27
|
+
readonly interval?: number;
|
|
28
|
+
/**
|
|
29
|
+
* - `"interval"` (default): `setInterval` pump (non-blocking, ~15.6 ms timer granularity on Windows).
|
|
30
|
+
* - `"wait"`: `MsgWaitForMultipleObjectsEx` + `setImmediate` pump — wakes in < 0.1 ms on incoming
|
|
31
|
+
* Win32/COM/UI messages, sleeps at ~0% CPU when idle, and yields every `interval` ms to libuv.
|
|
32
|
+
*/
|
|
33
|
+
readonly mode?: "interval" | "wait";
|
|
34
|
+
/** Whether the pump keeps the Node.js event loop alive (default `false` for `"interval"`, `true` for `"wait"`). */
|
|
35
|
+
readonly ref?: boolean;
|
|
36
|
+
}
|
|
15
37
|
/**
|
|
16
|
-
* Pump messages
|
|
17
|
-
*
|
|
18
|
-
*
|
|
38
|
+
* Pump Win32 messages from the Node.js event loop.
|
|
39
|
+
*
|
|
40
|
+
* - Default (`mode: "interval"`): periodic unref'd `setInterval` — leaves the
|
|
41
|
+
* JS thread completely idle between ticks.
|
|
42
|
+
* - Low-latency (`mode: "wait"`): `MsgWaitForMultipleObjectsEx` + `setImmediate`
|
|
43
|
+
* — sub-millisecond wakeup for interactive UI / STA COM callbacks.
|
|
19
44
|
*/
|
|
20
|
-
export declare function startMessagePump(options?:
|
|
21
|
-
readonly interval?: number;
|
|
22
|
-
}): MessagePump;
|
|
45
|
+
export declare function startMessagePump(options?: StartMessagePumpOptions): MessagePump;
|
|
23
46
|
/**
|
|
24
47
|
* Pump messages until `condition()` holds or `timeout` elapses; resolves to
|
|
25
|
-
* the last value of the condition.
|
|
48
|
+
* the last value of the condition. Uses `waitAndPumpMessages` so STA COM events
|
|
49
|
+
* wake immediately (< 0.1 ms) while still yielding to the Node.js event loop.
|
|
26
50
|
*/
|
|
27
51
|
export declare function waitFor(condition: () => boolean, options?: {
|
|
28
52
|
readonly timeout?: number;
|