@node-ctypes/comtypes 0.1.0 → 0.2.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 (57) hide show
  1. package/README.md +114 -47
  2. package/dist/automation/dispatch.d.ts +6 -0
  3. package/dist/automation/dispatch.js +78 -29
  4. package/dist/automation/members.js +31 -22
  5. package/dist/automation/variant.d.ts +2 -0
  6. package/dist/automation/variant.js +38 -1
  7. package/dist/cli.js +7 -2
  8. package/dist/client/best.d.ts +6 -0
  9. package/dist/client/best.js +89 -45
  10. package/dist/client/constants.js +2 -2
  11. package/dist/client/dual.d.ts +14 -0
  12. package/dist/client/dual.js +122 -0
  13. package/dist/client/dynamic.d.ts +21 -9
  14. package/dist/client/dynamic.js +123 -18
  15. package/dist/client/events.js +11 -11
  16. package/dist/client/index.d.ts +39 -19
  17. package/dist/client/index.js +124 -17
  18. package/dist/commethod.js +67 -18
  19. package/dist/dispmethod.js +26 -5
  20. package/dist/executor/apartment.d.ts +32 -0
  21. package/dist/executor/apartment.js +226 -0
  22. package/dist/executor/classes.d.ts +10 -0
  23. package/dist/executor/classes.js +18 -0
  24. package/dist/executor/executor.d.ts +76 -0
  25. package/dist/executor/executor.js +208 -0
  26. package/dist/executor/operations.d.ts +29 -0
  27. package/dist/executor/operations.js +95 -0
  28. package/dist/index.d.ts +6 -2
  29. package/dist/index.js +3 -1
  30. package/dist/interface.d.ts +43 -3
  31. package/dist/interface.js +28 -8
  32. package/dist/interfaces/servprov.js +2 -2
  33. package/dist/low-level/index.d.ts +2 -0
  34. package/dist/low-level/index.js +1 -0
  35. package/dist/low-level/native.d.ts +6 -0
  36. package/dist/low-level/native.js +13 -1
  37. package/dist/pointer.d.ts +57 -6
  38. package/dist/pointer.js +233 -9
  39. package/dist/server/comobject.js +9 -3
  40. package/dist/server/errorinfo.js +6 -6
  41. package/dist/server/register.js +5 -0
  42. package/dist/threads.js +4 -4
  43. package/dist/tracking.d.ts +2 -0
  44. package/dist/tracking.js +4 -0
  45. package/dist/typelib/build.d.ts +5 -0
  46. package/dist/typelib/build.js +8 -1
  47. package/dist/typelib/generate.js +14 -11
  48. package/dist/typelib/getmodule.d.ts +14 -3
  49. package/dist/typelib/getmodule.js +61 -19
  50. package/dist/typelib/index.d.ts +1 -1
  51. package/dist/typelib/index.js +1 -1
  52. package/dist/typelib/load.js +32 -8
  53. package/dist/typelib/model.d.ts +3 -0
  54. package/dist/typelib/model.js +87 -66
  55. package/dist/typelib/steps.d.ts +12 -0
  56. package/dist/typelib/steps.js +51 -0
  57. package/package.json +2 -2
@@ -70,6 +70,8 @@ export declare const comReference: unique symbol;
70
70
  export interface BorrowedReference {
71
71
  readonly address: bigint;
72
72
  readonly interface: InterfaceDef<any>;
73
+ /** Objects implemented in JavaScript: whether other threads may call them (`free`). */
74
+ readonly threading?: "apartment" | "free";
73
75
  }
74
76
  /** The interface pointer behind a COM pointer, a dynamic object or a JavaScript COM object, if any. */
75
77
  export declare function borrowReference(value: unknown): BorrowedReference | null;
@@ -6,7 +6,8 @@ import { derivesFrom, findInterface } from "../interface.js";
6
6
  import { allocBSTR, oleaut32, POINTER_SIZE, readBSTR, readMemory, writeMemory } from "../low-level/native.js";
7
7
  import { rawAddRef, rawQueryInterface, rawRelease } from "../low-level/vtable.js";
8
8
  import { RecordInfo, recordParts } from "./record.js";
9
- import { COMPointer } from "../pointer.js";
9
+ import { COMPointer, setArgumentInspector, sharedWithExecutor } from "../pointer.js";
10
+ import { NamedArguments } from "./named.js";
10
11
  import { VT_ARRAY, VT_BOOL, VT_BSTR, VT_BYREF, VT_CY, VT_DATE, VT_DECIMAL, VT_DISPATCH, VT_EMPTY, VT_ERROR, VT_I1, VT_I2, VT_I4, VT_I8, VT_INT, VT_INT_PTR, VT_NULL, VT_R4, VT_R8, VT_RECORD, VT_TYPEMASK, VT_UI1, VT_UI2, VT_UI4, VT_UI8, VT_UINT, VT_UINT_PTR, VT_UNKNOWN, VT_VARIANT, vartypeName, } from "./constants.js";
11
12
  import { dateToOADate, oaDateToDate } from "./date.js";
12
13
  import { currencyToString, readDecimal, writeDecimal } from "./decimal.js";
@@ -774,3 +775,39 @@ export class VARIANT extends ctypes.Structure {
774
775
  return this.toString();
775
776
  }
776
777
  }
778
+ // ── asynchronous calls ────────────────────────────────────────────────
779
+ /**
780
+ * Whether `value`, passed to a call that runs on the COM executor, is usable
781
+ * there: COM objects must be shared with the executor (home thread in the
782
+ * multithreaded apartment, or agile objects); objects implemented in
783
+ * JavaScript must be free-threaded, since the executor (or the objects it
784
+ * calls) would call them from another thread.
785
+ */
786
+ function transferable(value, homeMultithreaded) {
787
+ if (value === null || (typeof value !== "object" && typeof value !== "function"))
788
+ return true;
789
+ if (value instanceof COMPointer)
790
+ return homeMultithreaded || sharedWithExecutor(value);
791
+ if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer || value instanceof Date)
792
+ return true;
793
+ const record = value;
794
+ const pointer = record[comPointer];
795
+ if (pointer instanceof COMPointer)
796
+ return homeMultithreaded || sharedWithExecutor(pointer);
797
+ const lend = record[comReference];
798
+ if (typeof lend === "function")
799
+ return lend.call(value)?.threading === "free";
800
+ if (value instanceof VARIANT) {
801
+ // An object inside a VARIANT belongs to the home apartment.
802
+ const vt = value.vt;
803
+ return homeMultithreaded || ((vt & VT_TYPEMASK) !== VT_DISPATCH && (vt & VT_TYPEMASK) !== VT_UNKNOWN && (vt & VT_TYPEMASK) !== VT_VARIANT && !(vt & (VT_ARRAY | VT_BYREF)));
804
+ }
805
+ if (value instanceof ByRef || value instanceof Typed)
806
+ return transferable(value.value, homeMultithreaded);
807
+ if (value instanceof NamedArguments)
808
+ return Object.values(value.values).every((item) => transferable(item, homeMultithreaded));
809
+ if (Array.isArray(value))
810
+ return value.every((item) => transferable(item, homeMultithreaded));
811
+ return true;
812
+ }
813
+ setArgumentInspector((args, homeMultithreaded) => args.every((value) => transferable(value, homeMultithreaded)));
package/dist/cli.js CHANGED
@@ -168,9 +168,14 @@ async function main(argv) {
168
168
  CoUninitialize();
169
169
  }
170
170
  }
171
- main(process.argv.slice(2)).then((code) => {
171
+ main(process.argv.slice(2))
172
+ .then((code) => {
172
173
  process.exitCode = code;
173
174
  }, (error) => {
174
175
  console.error(error instanceof Error ? error.message : error);
175
176
  process.exitCode = 1;
176
- });
177
+ })
178
+ // One more event loop turn before exiting: exiting right after the work
179
+ // (and CoUninitialize) races Node's teardown with tasks posted by V8's
180
+ // concurrent GC, and can abort with "!(handle->flags & UV_HANDLE_CLOSING)".
181
+ .then(() => new Promise((resolve) => setTimeout(resolve, 0)));
@@ -1,4 +1,10 @@
1
1
  import { COMPointer } from "../pointer.ts";
2
+ /**
3
+ * Load the type library {@link GetBestInterface} needs for `object` with
4
+ * `await GetModule` (in slices, without blocking the event loop), so that it
5
+ * finds the module cached. Objects without type information need nothing.
6
+ */
7
+ export declare function loadBestModule(object: COMPointer): Promise<void>;
2
8
  /**
3
9
  * The most specific interface of an object, typed from its type library
4
10
  * (loaded with `GetModule`): the default interface of its coclass
@@ -58,15 +58,16 @@ import { COMError } from "../errors.js";
58
58
  import { derivesFrom, findInterface } from "../interface.js";
59
59
  import { IDispatch } from "../interfaces/dispatch.js";
60
60
  import { IProvideClassInfo, ITypeInfo, ITypeLib } from "../interfaces/typeinfo.js";
61
- import { COMPointer } from "../pointer.js";
62
- import { GetModule } from "../typelib/getmodule.js";
63
- import { adoptDispatch, iterateNewEnum } from "./dynamic.js";
61
+ import { COMPointer, sync } from "../pointer.js";
62
+ import { GetModule, getModuleSync } from "../typelib/getmodule.js";
63
+ import { queryOrDispatch } from "./dual.js";
64
+ import { adoptDispatch, iterateNewEnum, iterateNewEnumAsync } from "./dynamic.js";
64
65
  /** The type info of implemented interface `index` (`-1`: the vtable part of a dual interface). */
65
66
  function implementedType(info, index) {
66
67
  const href = new ctypes.c_uint32();
67
- info.GetRefTypeOfImplType(index >>> 0, ctypes.byref(href));
68
+ sync(info).GetRefTypeOfImplType(index >>> 0, ctypes.byref(href));
68
69
  const out = new ctypes.c_void_p();
69
- info.GetRefTypeInfo(href.value, ctypes.byref(out));
70
+ sync(info).GetRefTypeInfo(href.value, ctypes.byref(out));
70
71
  return COMPointer.fromAddress(out.value, ITypeInfo);
71
72
  }
72
73
  /** Default (non-source) interface of a coclass. */
@@ -74,7 +75,7 @@ function defaultInterface(classInfo) {
74
75
  const count = withTypeAttr(classInfo, (attr) => attr.cImplTypes);
75
76
  const flags = new ctypes.c_int32();
76
77
  for (let index = 0; index < count; index++) {
77
- classInfo.GetImplTypeFlags(index, ctypes.byref(flags));
78
+ sync(classInfo).GetImplTypeFlags(index, ctypes.byref(flags));
78
79
  if ((flags.value & (IMPLTYPEFLAG_FDEFAULT | IMPLTYPEFLAG_FSOURCE)) === IMPLTYPEFLAG_FDEFAULT)
79
80
  return implementedType(classInfo, index);
80
81
  }
@@ -92,7 +93,7 @@ function objectTypeInfo(pointer) {
92
93
  try {
93
94
  const env_3 = { stack: [], error: void 0, hasError: false };
94
95
  try {
95
- const classInfo = __addDisposableResource(env_3, owned.GetClassInfo(), false);
96
+ const classInfo = __addDisposableResource(env_3, sync(owned).GetClassInfo(), false);
96
97
  const info = classInfo ? defaultInterface(classInfo) : null;
97
98
  if (info)
98
99
  return info;
@@ -148,16 +149,19 @@ function dualVtable(info) {
148
149
  function isInterfaceDef(value) {
149
150
  return typeof value === "object" && value !== null && "iid" in value && "Pointer" in value;
150
151
  }
152
+ function containingLibrary(info) {
153
+ const out = new ctypes.c_void_p();
154
+ const index = new ctypes.c_uint32();
155
+ sync(info).GetContainingTypeLib(ctypes.byref(out), ctypes.byref(index));
156
+ return COMPointer.fromAddress(out.value, ITypeLib);
157
+ }
151
158
  /** Definition of the type described by `info`, built by `GetModule` from its type library. */
152
159
  function interfaceOf(info) {
153
160
  const env_4 = { stack: [], error: void 0, hasError: false };
154
161
  try {
155
162
  const iid = withTypeAttr(info, (attr) => attr.guid.toString());
156
- const out = new ctypes.c_void_p();
157
- const index = new ctypes.c_uint32();
158
- info.GetContainingTypeLib(ctypes.byref(out), ctypes.byref(index));
159
- const library = __addDisposableResource(env_4, COMPointer.fromAddress(out.value, ITypeLib), false);
160
- for (const value of Object.values(GetModule(library)))
163
+ const library = __addDisposableResource(env_4, containingLibrary(info), false);
164
+ for (const value of Object.values(getModuleSync(library)))
161
165
  if (isInterfaceDef(value) && value.iid.toString() === iid)
162
166
  return value;
163
167
  return findInterface(iid) ?? null;
@@ -170,6 +174,55 @@ function interfaceOf(info) {
170
174
  __disposeResources(env_4);
171
175
  }
172
176
  }
177
+ /**
178
+ * Load the type library {@link GetBestInterface} needs for `object` with
179
+ * `await GetModule` (in slices, without blocking the event loop), so that it
180
+ * finds the module cached. Objects without type information need nothing.
181
+ */
182
+ export async function loadBestModule(object) {
183
+ const env_5 = { stack: [], error: void 0, hasError: false };
184
+ try {
185
+ let library;
186
+ try {
187
+ const env_6 = { stack: [], error: void 0, hasError: false };
188
+ try {
189
+ const info = __addDisposableResource(env_6, objectTypeInfo(object), false);
190
+ if (!info)
191
+ return;
192
+ const vtable = __addDisposableResource(env_6, dualVtable(info), false);
193
+ library = containingLibrary(vtable ?? info);
194
+ }
195
+ catch (e_5) {
196
+ env_6.error = e_5;
197
+ env_6.hasError = true;
198
+ }
199
+ finally {
200
+ __disposeResources(env_6);
201
+ }
202
+ }
203
+ catch (error) {
204
+ if (error instanceof COMError)
205
+ return;
206
+ throw error;
207
+ }
208
+ const owned = __addDisposableResource(env_5, library, false);
209
+ try {
210
+ await GetModule(owned);
211
+ }
212
+ catch (error) {
213
+ // GetBestInterface falls back to the object's own interface.
214
+ if (!(error instanceof COMError))
215
+ throw error;
216
+ }
217
+ }
218
+ catch (e_6) {
219
+ env_5.error = e_6;
220
+ env_5.hasError = true;
221
+ }
222
+ finally {
223
+ __disposeResources(env_5);
224
+ }
225
+ }
173
226
  /**
174
227
  * The most specific interface of an object, typed from its type library
175
228
  * (loaded with `GetModule`): the default interface of its coclass
@@ -180,54 +233,37 @@ function interfaceOf(info) {
180
233
  */
181
234
  export function GetBestInterface(object) {
182
235
  try {
183
- const env_5 = { stack: [], error: void 0, hasError: false };
236
+ const env_7 = { stack: [], error: void 0, hasError: false };
184
237
  try {
185
- const info = __addDisposableResource(env_5, objectTypeInfo(object), false);
238
+ const info = __addDisposableResource(env_7, objectTypeInfo(object), false);
186
239
  if (info) {
187
- const env_6 = { stack: [], error: void 0, hasError: false };
240
+ const env_8 = { stack: [], error: void 0, hasError: false };
188
241
  try {
189
- const vtable = __addDisposableResource(env_6, dualVtable(info), false);
242
+ const vtable = __addDisposableResource(env_8, dualVtable(info), false);
190
243
  const iface = interfaceOf(vtable ?? info);
191
244
  if (iface && iface !== object.interface) {
192
- const typed = object.tryQueryInterface(iface);
245
+ // A dispinterface, or a dual interface whose vtable cannot be marshaled,
246
+ // is reached through IDispatch::Invoke.
247
+ const typed = queryOrDispatch(object, iface);
193
248
  if (typed)
194
249
  return typed;
195
- // A dispinterface is called through IDispatch::Invoke only: objects
196
- // may not answer QueryInterface for its DIID.
197
- if (iface.allMethods.length === IDispatch.allMethods.length && derivesFrom(iface, IDispatch.iid)) {
198
- const dispatch = object.tryQueryInterface(IDispatch);
199
- if (dispatch) {
200
- const env_7 = { stack: [], error: void 0, hasError: false };
201
- try {
202
- const owned = __addDisposableResource(env_7, dispatch, false);
203
- return COMPointer.fromAddress(owned.address, iface, { addRef: true });
204
- }
205
- catch (e_5) {
206
- env_7.error = e_5;
207
- env_7.hasError = true;
208
- }
209
- finally {
210
- __disposeResources(env_7);
211
- }
212
- }
213
- }
214
250
  }
215
251
  }
216
- catch (e_6) {
217
- env_6.error = e_6;
218
- env_6.hasError = true;
252
+ catch (e_7) {
253
+ env_8.error = e_7;
254
+ env_8.hasError = true;
219
255
  }
220
256
  finally {
221
- __disposeResources(env_6);
257
+ __disposeResources(env_8);
222
258
  }
223
259
  }
224
260
  }
225
- catch (e_7) {
226
- env_5.error = e_7;
227
- env_5.hasError = true;
261
+ catch (e_8) {
262
+ env_7.error = e_8;
263
+ env_7.hasError = true;
228
264
  }
229
265
  finally {
230
- __disposeResources(env_5);
266
+ __disposeResources(env_7);
231
267
  }
232
268
  }
233
269
  catch (error) {
@@ -256,7 +292,8 @@ function wrapOutparam(value) {
256
292
  return value;
257
293
  }
258
294
  setOutputWrapper(wrapOutparam);
259
- // Typed dispatch pointers iterate their `_NewEnum` collection (Python: `__iter__`).
295
+ // Typed dispatch pointers iterate their `_NewEnum` collection (Python: `__iter__`),
296
+ // also with `for await` (IEnumVARIANT::Next on the COM executor).
260
297
  Object.defineProperty(IDispatch.Pointer.prototype, Symbol.iterator, {
261
298
  configurable: true,
262
299
  writable: true,
@@ -264,3 +301,10 @@ Object.defineProperty(IDispatch.Pointer.prototype, Symbol.iterator, {
264
301
  return iterateNewEnum(this, wrapOutparam);
265
302
  },
266
303
  });
304
+ Object.defineProperty(IDispatch.Pointer.prototype, Symbol.asyncIterator, {
305
+ configurable: true,
306
+ writable: true,
307
+ value: function () {
308
+ return iterateNewEnumAsync(this, wrapOutparam);
309
+ },
310
+ });
@@ -50,7 +50,7 @@ var __disposeResources = (this && this.__disposeResources) || (function (Suppres
50
50
  var e = new Error(message);
51
51
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
52
  });
53
- import { GetModule } from "../typelib/getmodule.js";
53
+ import { getModuleSync } from "../typelib/getmodule.js";
54
54
  import { typeLibOf } from "../typelib/load.js";
55
55
  const cache = new WeakMap();
56
56
  function isEnum(value) {
@@ -112,7 +112,7 @@ export function Constants(object) {
112
112
  const env_1 = { stack: [], error: void 0, hasError: false };
113
113
  try {
114
114
  const library = __addDisposableResource(env_1, typeLibOf(object), false);
115
- return constantsOf(GetModule(library));
115
+ return constantsOf(getModuleSync(library));
116
116
  }
117
117
  catch (e_1) {
118
118
  env_1.error = e_1;
@@ -0,0 +1,14 @@
1
+ import type { InterfaceDef } from "../interface.ts";
2
+ import { COMPointer } from "../pointer.ts";
3
+ /**
4
+ * `iface` (a dual interface from a type library) as a dispinterface: its
5
+ * members called through `IDispatch::Invoke`. `null` when a member has no
6
+ * DISPID (not a dual interface).
7
+ */
8
+ export declare function dispatchVariant(iface: InterfaceDef<any>): InterfaceDef<any> | null;
9
+ /**
10
+ * `iface` of `object`: QueryInterface, else (dual interfaces whose vtable
11
+ * cannot be marshaled) its dispatch variant on the object's `IDispatch`.
12
+ * `null` when neither is available.
13
+ */
14
+ export declare function queryOrDispatch(object: COMPointer, iface: InterfaceDef<any>): COMPointer | null;
@@ -0,0 +1,122 @@
1
+ var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
2
+ if (value !== null && value !== void 0) {
3
+ if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
4
+ var dispose, inner;
5
+ if (async) {
6
+ if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
7
+ dispose = value[Symbol.asyncDispose];
8
+ }
9
+ if (dispose === void 0) {
10
+ if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
11
+ dispose = value[Symbol.dispose];
12
+ if (async) inner = dispose;
13
+ }
14
+ if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
15
+ if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
16
+ env.stack.push({ value: value, dispose: dispose, async: async });
17
+ }
18
+ else if (async) {
19
+ env.stack.push({ async: true });
20
+ }
21
+ return value;
22
+ };
23
+ var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
24
+ return function (env) {
25
+ function fail(e) {
26
+ env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
27
+ env.hasError = true;
28
+ }
29
+ var r, s = 0;
30
+ function next() {
31
+ while (r = env.stack.pop()) {
32
+ try {
33
+ if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
34
+ if (r.dispose) {
35
+ var result = r.dispose.call(r.value);
36
+ if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
37
+ }
38
+ else s |= 1;
39
+ }
40
+ catch (e) {
41
+ fail(e);
42
+ }
43
+ }
44
+ if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
45
+ if (env.hasError) throw env.error;
46
+ }
47
+ return next();
48
+ };
49
+ })(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
50
+ var e = new Error(message);
51
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
52
+ });
53
+ // Dual interfaces reached through IDispatch. Across apartments a dual
54
+ // interface needs a registered proxy/stub for its vtable; when QueryInterface
55
+ // fails there, the object is still typed: the same members, from the same
56
+ // type library, called through IDispatch::Invoke with their DISPIDs.
57
+ import { dispid } from "../commethod.js";
58
+ import { DISPMETHOD } from "../dispmethod.js";
59
+ import { COMInterface, derivesFrom } from "../interface.js";
60
+ import { IDispatch } from "../interfaces/dispatch.js";
61
+ import { COMPointer } from "../pointer.js";
62
+ const variants = new WeakMap();
63
+ function parameters(spec) {
64
+ return spec.params ?? null;
65
+ }
66
+ /** The dispatch member equivalent to a dual interface method, or `null` when it has no DISPID. */
67
+ function dispatchMember(spec) {
68
+ const params = parameters(spec);
69
+ if (spec.dispid === null || !params)
70
+ return null;
71
+ const inputs = params.filter((param) => param.input || param.lcid).map((param) => (param.hasDefault ? [param.flags, param.type, param.name, param.defaultValue] : [param.flags, param.type, param.name]));
72
+ const retval = params.find((param) => param.output && param.flags.includes("retval"));
73
+ const flags = spec.kind === "method" ? [dispid(spec.dispid)] : [dispid(spec.dispid), spec.kind];
74
+ return DISPMETHOD(flags, (retval?.type ?? null), spec.property, ...inputs);
75
+ }
76
+ /**
77
+ * `iface` (a dual interface from a type library) as a dispinterface: its
78
+ * members called through `IDispatch::Invoke`. `null` when a member has no
79
+ * DISPID (not a dual interface).
80
+ */
81
+ export function dispatchVariant(iface) {
82
+ if (variants.has(iface))
83
+ return variants.get(iface);
84
+ let variant = null;
85
+ if (iface !== IDispatch && derivesFrom(iface, IDispatch.iid)) {
86
+ const own = iface.allMethods.slice(IDispatch.allMethods.length);
87
+ const members = own.map(dispatchMember);
88
+ if (own.length && members.every((member) => member !== null)) {
89
+ variant = COMInterface({ name: iface.name, iid: iface.iid, extends: IDispatch, register: false, dispMethods: members });
90
+ }
91
+ }
92
+ variants.set(iface, variant);
93
+ return variant;
94
+ }
95
+ /**
96
+ * `iface` of `object`: QueryInterface, else (dual interfaces whose vtable
97
+ * cannot be marshaled) its dispatch variant on the object's `IDispatch`.
98
+ * `null` when neither is available.
99
+ */
100
+ export function queryOrDispatch(object, iface) {
101
+ const env_1 = { stack: [], error: void 0, hasError: false };
102
+ try {
103
+ const typed = object.tryQueryInterface(iface);
104
+ if (typed)
105
+ return typed;
106
+ const variant = iface.allMethods.length === IDispatch.allMethods.length && derivesFrom(iface, IDispatch.iid) ? iface : dispatchVariant(iface);
107
+ if (!variant)
108
+ return null;
109
+ const dispatch = object.tryQueryInterface(IDispatch);
110
+ if (!dispatch)
111
+ return null;
112
+ const owned = __addDisposableResource(env_1, dispatch, false);
113
+ return COMPointer.fromAddress(owned.address, variant, { addRef: true });
114
+ }
115
+ catch (e_1) {
116
+ env_1.error = e_1;
117
+ env_1.hasError = true;
118
+ }
119
+ finally {
120
+ __disposeResources(env_1);
121
+ }
122
+ }
@@ -8,20 +8,30 @@ export { NamedArguments, named } from "../automation/named.ts";
8
8
  * A late-bound automation object. Python equivalent: `comtypes.client.dynamic.Dispatch`.
9
9
  *
10
10
  * - `obj.Prop` reads a property; `obj.Prop = value` writes it (`PROPERTYPUTREF` for objects).
11
- * - `obj.Method(...args)` calls a method; parameterized properties are called too: `obj.Item(key)`.
12
- * - `obj(...args)` invokes the default member (`DISPID_VALUE`).
13
- * - `for (const item of obj)` enumerates through `_NewEnum`.
11
+ * - `await obj.Method(...args)` calls a method on the COM executor; parameterized
12
+ * properties are read synchronously: `obj.Item(key)`.
13
+ * - `obj(...args)` invokes the default member (`DISPID_VALUE`): synchronously
14
+ * when the type information declares it a property, else as a method.
15
+ * - `for (const item of obj)` / `for await (const item of obj)` enumerate through `_NewEnum`.
16
+ * - `sync(obj)`: a view whose methods are synchronous.
14
17
  * - `using obj = ...` / `release(obj)` releases the object.
15
18
  *
16
19
  * Member kinds come from the type information when available; otherwise a
17
- * read is attempted and a method caller is returned when the member is not a
18
- * readable property, as comtypes does.
20
+ * read is attempted (synchronously) and a method caller is returned when the
21
+ * member is not a readable property, as comtypes does.
19
22
  */
20
- export interface DispatchObject extends Disposable, Iterable<any> {
23
+ export interface DispatchObject extends Disposable, Iterable<any>, AsyncIterable<any> {
21
24
  (...args: any[]): any;
22
25
  [member: string]: any;
23
26
  readonly [comPointer]: DispatchPointer;
24
27
  }
28
+ /**
29
+ * Values of a collection through `_NewEnum` and `IEnumVARIANT::Next` on the
30
+ * COM executor, converted by `convert` (`for await`). Each call fetches one
31
+ * item (some enumerators, e.g. Shell `FolderItems`, repeat the same item when
32
+ * asked for more), a batch of calls queued at once.
33
+ */
34
+ export declare function iterateNewEnumAsync(pointer: DispatchPointer, convert: (value: unknown) => unknown): AsyncGenerator<any, void, undefined>;
25
35
  /** Values of a collection through `_NewEnum` (`DISPID_NEWENUM`) and `IEnumVARIANT`, converted by `convert`. */
26
36
  export declare function iterateNewEnum(pointer: DispatchPointer, convert: (value: unknown) => unknown): Generator<any, void, undefined>;
27
37
  /**
@@ -44,10 +54,12 @@ export type DispatchTarget = DispatchObject | COMPointer;
44
54
  * `GetActiveObject` without `interface` or `dynamic`, and by typed calls
45
55
  * declared to return `IDispatch`.
46
56
  */
47
- export type AutomationObject = (COMPointer & Iterable<any> & {
57
+ export type AutomationObject = (COMPointer & Iterable<any> & AsyncIterable<any> & {
48
58
  [member: string]: any;
49
59
  }) | DispatchObject;
50
60
  export declare function adoptDispatch(pointer: DispatchPointer): DispatchObject;
61
+ /** The synchronous view of a dispatch object: methods called synchronously, same reference. */
62
+ export declare function syncDispatch(object: DispatchObject): DispatchObject;
51
63
  export declare function isDispatch(value: unknown): value is DispatchObject;
52
64
  /** The `IDispatch` pointer of a dispatch object (still owned by the object). */
53
65
  export declare function dispatchPointer(object: DispatchTarget): DispatchPointer;
@@ -61,8 +73,8 @@ export declare function getProperty(object: DispatchTarget, name: string, ...arg
61
73
  export declare function putProperty(object: DispatchTarget, name: string, ...argsAndValue: unknown[]): void;
62
74
  /** Property put by reference (`Set obj.Name = value`). */
63
75
  export declare function putRefProperty(object: DispatchTarget, name: string, ...argsAndValue: unknown[]): void;
64
- /** Method call (`DISPATCH_METHOD` only). */
65
- export declare function invokeMethod(object: DispatchTarget, name: string, ...args: unknown[]): any;
76
+ /** Method call (`DISPATCH_METHOD` only), on the COM executor. */
77
+ export declare function invokeMethod(object: DispatchTarget, name: string, ...args: unknown[]): Promise<any>;
66
78
  /** Member information from the type library, when available. */
67
79
  export declare function memberInfo(object: DispatchTarget, name: string): MemberInfo | null;
68
80
  /** Name of the object's type from its type information, or `null`. */