vba-interpreter 0.0.0-stage → 0.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/LICENSE +202 -0
- package/README.md +96 -2
- package/dist/contract-CBGNXslV.d.ts +226 -0
- package/dist/exec/index.d.ts +12 -0
- package/dist/exec/index.js +1 -0
- package/dist/exec-DYWJe-eU.js +306 -0
- package/dist/exec-DYWJe-eU.js.map +1 -0
- package/dist/index-DnrotnmB.d.ts +2015 -0
- package/dist/index-DtaSNo0V.d.ts +111 -0
- package/dist/index-DyoT9JOJ.d.ts +4 -0
- package/dist/index-gM7FYTaq.d.ts +1353 -0
- package/dist/index-zKYgEJdV.d.ts +900 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +3 -0
- package/dist/interp/index.d.ts +10 -0
- package/dist/interp/index.js +1 -0
- package/dist/interp-ChrsgBCf.js +3433 -0
- package/dist/interp-ChrsgBCf.js.map +1 -0
- package/dist/lang/index.d.ts +10 -0
- package/dist/lang/index.js +1 -0
- package/dist/lang-Bo63dJb9.js +4740 -0
- package/dist/lang-Bo63dJb9.js.map +1 -0
- package/dist/runtime/index.d.ts +33 -0
- package/dist/runtime/index.js +1 -0
- package/dist/runtime-C8-PPxmH.js +8284 -0
- package/dist/runtime-C8-PPxmH.js.map +1 -0
- package/dist/semantic/index.d.ts +12 -0
- package/dist/semantic/index.js +1 -0
- package/dist/semantic-CAVxm72C.js +2877 -0
- package/dist/semantic-CAVxm72C.js.map +1 -0
- package/package.json +91 -5
|
@@ -0,0 +1,3433 @@
|
|
|
1
|
+
import { a as completed, c as outcomeOf, d as CompileGate, f as DEFAULT_EXEC_LIMITS, i as compileFailure, l as LimitGuard, n as VbaCompileError, o as isTrappable, r as VbaStop, s as notRun, t as VbaCallbackFailure, u as normalizeDocumentObjects } from "./exec-DYWJe-eU.js";
|
|
2
|
+
import { n as listProcedures } from "./lang-Bo63dJb9.js";
|
|
3
|
+
import { $n as escape, $o as vLng, Ao as EMPTY$1, Ar as neg, Bo as given, C as debugPrint, Cn as letMember, Cs as toVbaError, Dr as idiv, En as collectionObject, Er as div, Fa as toStr, Fo as NOTHING, Gn as defineUdt, Hn as lsetRecord, Io as NULL, Ja as resolveDefaultMaybe, Jn as assignArray, Jo as vDbl, Kn as VbaArray, Ko as vCur, Kt as midAssign, Ma as toBool, Mr as sub, Nn as is, No as MISSING, Or as mod, Pn as noMember, Pr as cat, Qo as vLLng, Ro as VT, S as debugAssert, Sn as typeIs, Tr as add, Ua as toLng, Uo as vArr, Wo as vBool, Wt as lenOfType, Xn as TerminationQueue, Ya as scalar, Yn as defaultFor, Zn as counted, Zo as vInt, _a as createSession, _r as gt, _s as VbaRuntimeError, br as ne, cr as eqv, dr as or, er as isCountingStore, es as vObj, fr as xor, gr as ge, gs as VbaAbort, hr as eq, ir as like, ja as letAs, jr as pow, ka as fixLen, kr as mul, lr as imp, mr as cmp, no as curFromNumber, nr as retain, ns as vStr, or as and, qn as arrayOf, qo as vDate, rr as copyValue, rs as vUdt, t as runtime_exports, tr as release, ts as vSng, ur as not, ut as error, vr as le, wn as ForEachCursor, xs as notSupported, yr as lt, za as toDbl } from "./runtime-C8-PPxmH.js";
|
|
4
|
+
import { E as PropertyGroup, t as bindProgram, y as isVariant } from "./semantic-CAVxm72C.js";
|
|
5
|
+
// src/interp/async.ts
|
|
6
|
+
/**
|
|
7
|
+
* Applies `f` to a value now, or when its Promise settles. Not named `then`: a module exporting
|
|
8
|
+
* `then` is a thenable namespace, which dynamic `import()` would call.
|
|
9
|
+
*/
|
|
10
|
+
function andThen(x, f) {
|
|
11
|
+
return x instanceof Promise ? x.then(f) : f(x);
|
|
12
|
+
}
|
|
13
|
+
// src/interp/error-handler.ts
|
|
14
|
+
var ErrorHandlerState = class {
|
|
15
|
+
/** `goto0` errors propagate, `next` is Resume Next, `label` jumps to `target`. */
|
|
16
|
+
mode = "goto0";
|
|
17
|
+
/** The instruction of the handler label. */
|
|
18
|
+
target = -1;
|
|
19
|
+
/** True while the handler runs: a new error then propagates to the caller. */
|
|
20
|
+
handling = false;
|
|
21
|
+
/** The instruction that raised the error being handled. */
|
|
22
|
+
errorPc = -1;
|
|
23
|
+
/** `On Error GoTo label` / `Resume Next` / `GoTo 0`; any On Error clears Err. */
|
|
24
|
+
set(mode, target, err) {
|
|
25
|
+
this.mode = mode;
|
|
26
|
+
this.target = target;
|
|
27
|
+
err.clear();
|
|
28
|
+
}
|
|
29
|
+
/** `On Error GoTo -1`: leaves the handler without resuming, so a new error can be trapped. */
|
|
30
|
+
reset(err) {
|
|
31
|
+
this.handling = false;
|
|
32
|
+
err.clear();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Decides where an error raised by instruction `pc` goes: the instruction to continue at, or
|
|
36
|
+
* a rethrow when this activation does not trap it.
|
|
37
|
+
*/
|
|
38
|
+
trap(e, pc, err, next = pc + 1) {
|
|
39
|
+
if (!isTrappable(e) || this.mode === "goto0" || this.handling) throw e;
|
|
40
|
+
err.capture(e);
|
|
41
|
+
if (this.mode === "next") return next;
|
|
42
|
+
this.handling = true;
|
|
43
|
+
this.errorPc = pc;
|
|
44
|
+
return this.target;
|
|
45
|
+
}
|
|
46
|
+
/** `Resume` (`retry`), `Resume Next` or `Resume label`: leaves the handler and clears Err. */
|
|
47
|
+
resume(kind, label, err, nextOf = (pc) => pc + 1) {
|
|
48
|
+
if (!this.handling) throw new VbaRuntimeError(20);
|
|
49
|
+
this.handling = false;
|
|
50
|
+
err.clear();
|
|
51
|
+
return kind === "retry" ? this.errorPc : kind === "next" ? nextOf(this.errorPc) : label;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/** The instruction Resume Next continues at after an error in instruction `pc`. */
|
|
55
|
+
const nextOf = (code, pc) => code[pc]?.skip?.pc ?? pc + 1;
|
|
56
|
+
// src/interp/executor.ts
|
|
57
|
+
/** Adds this frame to the call stack of an error that leaves it. */
|
|
58
|
+
function note(f, e, pc) {
|
|
59
|
+
if (typeof e !== "object" || e === null) return;
|
|
60
|
+
const instr = f.code.code[pc];
|
|
61
|
+
let stack = f.run.stacks.get(e);
|
|
62
|
+
if (!stack) {
|
|
63
|
+
stack = [];
|
|
64
|
+
f.run.stacks.set(e, stack);
|
|
65
|
+
}
|
|
66
|
+
if (stack.length >= 64) return;
|
|
67
|
+
stack.push({
|
|
68
|
+
module: f.code.bound.module.name,
|
|
69
|
+
procedure: f.code.bound.name,
|
|
70
|
+
line: instr?.line ?? 0,
|
|
71
|
+
column: instr?.column ?? 0
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/** The instruction to continue at after an error, or a rethrow. */
|
|
75
|
+
function recover(f, e, pc) {
|
|
76
|
+
if (!isTrappable(e)) throw e;
|
|
77
|
+
try {
|
|
78
|
+
if (f.handler.mode === "goto0" || f.handler.handling) throw e;
|
|
79
|
+
if (typeof e === "object" && e !== null) {
|
|
80
|
+
const only = f.run.untrappable.get(e);
|
|
81
|
+
if (only === null || only === f) throw e;
|
|
82
|
+
}
|
|
83
|
+
f.erl = f.line;
|
|
84
|
+
return f.handler.trap(e instanceof RangeError ? toVbaError(e) : e, pc, f.session.err, nextOf(f.code.code, pc));
|
|
85
|
+
} catch (thrown) {
|
|
86
|
+
note(f, thrown, pc);
|
|
87
|
+
throw thrown;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Runs Class_Terminate of the objects whose last reference went during the statement that just
|
|
92
|
+
* ended in a frame at call depth `depth` (or in its callees). A Terminate that waits on its host
|
|
93
|
+
* makes the caller wait too; its errors do not reach the caller.
|
|
94
|
+
*/
|
|
95
|
+
function collectGarbage(run, depth = 0, since = 0) {
|
|
96
|
+
const due = run.terminations.take(depth, since);
|
|
97
|
+
if (due.length === 0) return void 0;
|
|
98
|
+
const err = run.session.err;
|
|
99
|
+
const swallow = (restore, e) => {
|
|
100
|
+
if (!isTrappable(e)) throw e;
|
|
101
|
+
restore();
|
|
102
|
+
};
|
|
103
|
+
const next = (i) => {
|
|
104
|
+
for (; i < due.length; i++) {
|
|
105
|
+
const restore = err.save();
|
|
106
|
+
let r;
|
|
107
|
+
try {
|
|
108
|
+
r = due[i].terminate(run);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
swallow(restore, e);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (r instanceof Promise) return r.then(() => next(i + 1), (e) => {
|
|
114
|
+
swallow(restore, e);
|
|
115
|
+
return next(i + 1);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return next(0);
|
|
120
|
+
}
|
|
121
|
+
/** Runs a frame's instructions from `from`. */
|
|
122
|
+
function execute(f, from = 0) {
|
|
123
|
+
const code = f.code.code;
|
|
124
|
+
const run = f.run;
|
|
125
|
+
const guard = run.guard;
|
|
126
|
+
let pc = from;
|
|
127
|
+
while (pc < code.length) {
|
|
128
|
+
if (guard.step()) return resume(f, pc);
|
|
129
|
+
let r;
|
|
130
|
+
try {
|
|
131
|
+
r = code[pc].run(f);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
pc = recover(f, e, pc);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (r instanceof Promise) return continueAsync(f, pc, r);
|
|
137
|
+
if (run.terminations.size > 0) {
|
|
138
|
+
const g = collectGarbage(run, f.depth, f.since);
|
|
139
|
+
if (g instanceof Promise) return continueAsync(f, pc, g.then(() => r));
|
|
140
|
+
}
|
|
141
|
+
pc = r === void 0 ? pc + 1 : r;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** Yields to the event loop, then goes on at `pc`. */
|
|
145
|
+
async function resume(f, pc) {
|
|
146
|
+
await pause(f.run);
|
|
147
|
+
return execute(f, pc);
|
|
148
|
+
}
|
|
149
|
+
async function continueAsync(f, at, pending) {
|
|
150
|
+
const code = f.code.code;
|
|
151
|
+
const run = f.run;
|
|
152
|
+
const guard = run.guard;
|
|
153
|
+
let pc = at;
|
|
154
|
+
let r = pending;
|
|
155
|
+
for (;;) {
|
|
156
|
+
try {
|
|
157
|
+
const next = r instanceof Promise ? await run.park(guard.wait(r)) : r;
|
|
158
|
+
if (run.terminations.size > 0) await collectGarbage(run, f.depth, f.since);
|
|
159
|
+
pc = next === void 0 ? pc + 1 : next;
|
|
160
|
+
} catch (e) {
|
|
161
|
+
pc = recover(f, e, pc);
|
|
162
|
+
}
|
|
163
|
+
if (pc >= code.length) return;
|
|
164
|
+
try {
|
|
165
|
+
if (guard.step()) await pause(run);
|
|
166
|
+
r = code[pc].run(f);
|
|
167
|
+
} catch (e) {
|
|
168
|
+
r = Promise.reject(e);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Yields to the event loop; cancel and the time limit end the run (On Error cannot trap them). */
|
|
173
|
+
function pause(run) {
|
|
174
|
+
return run.park(run.guard.pause());
|
|
175
|
+
}
|
|
176
|
+
// src/interp/frame.ts
|
|
177
|
+
var Frame = class {
|
|
178
|
+
code;
|
|
179
|
+
run;
|
|
180
|
+
slots;
|
|
181
|
+
me;
|
|
182
|
+
instance;
|
|
183
|
+
statics;
|
|
184
|
+
/** The subject of each With block, by block id. */
|
|
185
|
+
with = [];
|
|
186
|
+
/** The storage of a With block over a user-defined type: `.X` reads and writes the variable. */
|
|
187
|
+
withRefs = [];
|
|
188
|
+
/** Per-loop state (For limits and step, For Each iterators), by loop id. */
|
|
189
|
+
loops = [];
|
|
190
|
+
/** Return points of GoSub. */
|
|
191
|
+
gosub = [];
|
|
192
|
+
handler = new ErrorHandlerState();
|
|
193
|
+
/** The call depth of this activation (Class_Terminate checks use it). */
|
|
194
|
+
depth;
|
|
195
|
+
/** The termination queue's mark when the activation began. */
|
|
196
|
+
since;
|
|
197
|
+
/** The last line number passed (Erl reads it after an error). */
|
|
198
|
+
line = 0;
|
|
199
|
+
/** Erl: the line number before the statement that raised the trapped error. */
|
|
200
|
+
erl = 0;
|
|
201
|
+
constructor(code, run, slots, me, instance, statics) {
|
|
202
|
+
this.code = code;
|
|
203
|
+
this.run = run;
|
|
204
|
+
this.slots = slots;
|
|
205
|
+
this.me = me;
|
|
206
|
+
this.instance = instance;
|
|
207
|
+
this.statics = statics;
|
|
208
|
+
this.depth = run.guard.depth;
|
|
209
|
+
this.since = run.terminations.mark;
|
|
210
|
+
}
|
|
211
|
+
get session() {
|
|
212
|
+
return this.run.session;
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
/** Ends the For Each loop of block `id`, if one is open (unlocks its array). */
|
|
216
|
+
function closeCursor(f, id) {
|
|
217
|
+
const c = f.loops[id];
|
|
218
|
+
if (c instanceof ForEachCursor) c.close();
|
|
219
|
+
f.loops[id] = void 0;
|
|
220
|
+
}
|
|
221
|
+
/** Ends every open For Each loop of a frame (the procedure is left). */
|
|
222
|
+
function closeCursors(f) {
|
|
223
|
+
for (const c of f.loops) if (c instanceof ForEachCursor) c.close();
|
|
224
|
+
}
|
|
225
|
+
// src/interp/library.ts
|
|
226
|
+
const exports = runtime_exports;
|
|
227
|
+
const optional = (v) => v && given(v) ? v : void 0;
|
|
228
|
+
/** `LBound`/`UBound`: the bound of dimension `dim` (1 by default) of an array value. */
|
|
229
|
+
function bound(which, args, s) {
|
|
230
|
+
const a = args[0];
|
|
231
|
+
if (!a || a.vt !== 8192) throw new VbaRuntimeError(13);
|
|
232
|
+
const d = optional(args[1]);
|
|
233
|
+
const dim = d ? toLng(scalar(d), s) : 1;
|
|
234
|
+
return vLng(which === "lbound" ? a.v.lbound(dim) : a.v.ubound(dim));
|
|
235
|
+
}
|
|
236
|
+
function special(entry, module) {
|
|
237
|
+
switch (entry.name.toLowerCase()) {
|
|
238
|
+
case "array": {
|
|
239
|
+
const base = module.options.base;
|
|
240
|
+
return (_args, rest, f) => vArr(arrayOf(rest, base, f.session));
|
|
241
|
+
}
|
|
242
|
+
case "lbound": return (args, _rest, f) => bound("lbound", args, f.session);
|
|
243
|
+
case "ubound": return (args, _rest, f) => bound("ubound", args, f.session);
|
|
244
|
+
case "erl": return (_args, _rest, f) => vLng(f.erl);
|
|
245
|
+
case "error":
|
|
246
|
+
case "error$": {
|
|
247
|
+
const text = entry.name.endsWith("$");
|
|
248
|
+
return (args, _rest, f) => {
|
|
249
|
+
const n = optional(args[0]) ?? vLng(f.session.err.number);
|
|
250
|
+
const v = error(n, f.session);
|
|
251
|
+
return text && v.vt !== 8 ? vStrOf(v, f.session) : v;
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
default: return () => {
|
|
255
|
+
throw new VbaRuntimeError(445, `${entry.name} is not supported in this environment`);
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const vStrOf = (v, s) => vStr(toStr(scalar(v), s));
|
|
260
|
+
var LibraryDispatch = class {
|
|
261
|
+
cache = /* @__PURE__ */ new Map();
|
|
262
|
+
/** `typed`: a conversion whose argument has a static type (the invocation's `typedArgument`). */
|
|
263
|
+
compile(entry, module, typed = false) {
|
|
264
|
+
const key = `${module.name}\u0000${entry.name}\u0000${typed}`;
|
|
265
|
+
let call = this.cache.get(key);
|
|
266
|
+
if (!call) {
|
|
267
|
+
call = this.build(entry, module, typed);
|
|
268
|
+
this.cache.set(key, call);
|
|
269
|
+
}
|
|
270
|
+
return call;
|
|
271
|
+
}
|
|
272
|
+
build(entry, module, typed) {
|
|
273
|
+
if (entry.convention === "special") return special(entry, module);
|
|
274
|
+
const fn = entry.runtime ? exports[entry.runtime] : void 0;
|
|
275
|
+
if (typeof fn !== "function") return () => {
|
|
276
|
+
throw new VbaRuntimeError(445, `${entry.name} is not supported in this environment`);
|
|
277
|
+
};
|
|
278
|
+
const f = fn;
|
|
279
|
+
const n = entry.paramArray ? entry.params.length - 1 : entry.params.length;
|
|
280
|
+
const pad = (args) => {
|
|
281
|
+
const out = args.slice(0, n);
|
|
282
|
+
while (out.length < n) out.push(void 0);
|
|
283
|
+
return out;
|
|
284
|
+
};
|
|
285
|
+
const result = (r) => r ?? EMPTY$1;
|
|
286
|
+
const compareText = module.options.compareText ? 1 : 0;
|
|
287
|
+
switch (entry.convention) {
|
|
288
|
+
case "env": return (args, _rest, fr) => result(f(...pad(args), fr.session));
|
|
289
|
+
case "conversion": return (args, _rest, fr) => result(f(args[0], fr.session, typed));
|
|
290
|
+
case "compare": return (args, _rest, fr) => result(f(...pad(args), compareText, fr.session));
|
|
291
|
+
case "session": return (args, _rest, fr) => result(f(fr.session, ...pad(args)));
|
|
292
|
+
case "pure": return (args) => result(f(...pad(args)));
|
|
293
|
+
case "list": return (args, rest, fr) => result(f(...pad(args), rest, fr.session));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
// src/interp/objects/lifetime.ts
|
|
298
|
+
/** Marks an interface view; its value is the instance it views. */
|
|
299
|
+
const VIEW = Symbol("vba.view");
|
|
300
|
+
/**
|
|
301
|
+
* The object as a Variant or `As Object` variable holds it: an interface view becomes the
|
|
302
|
+
* instance's default interface ([MS-VBAL] 5.3.1.3), anything else is unchanged.
|
|
303
|
+
*/
|
|
304
|
+
function defaultInterface(v) {
|
|
305
|
+
if (v.vt !== 9 || v.v === null) return v;
|
|
306
|
+
const instance = v.v[VIEW];
|
|
307
|
+
return instance ? {
|
|
308
|
+
vt: 9,
|
|
309
|
+
v: instance
|
|
310
|
+
} : v;
|
|
311
|
+
}
|
|
312
|
+
// src/interp/storage.ts
|
|
313
|
+
/** A Variant variable. Holds any value; object references are counted. */
|
|
314
|
+
var VariantCell = class {
|
|
315
|
+
value;
|
|
316
|
+
constructor(value = EMPTY$1) {
|
|
317
|
+
this.value = value;
|
|
318
|
+
}
|
|
319
|
+
get() {
|
|
320
|
+
return this.value;
|
|
321
|
+
}
|
|
322
|
+
set(v) {
|
|
323
|
+
if (v.vt === 9 || this.value.vt === 9) {
|
|
324
|
+
retain(v);
|
|
325
|
+
release(this.value);
|
|
326
|
+
v = defaultInterface(v);
|
|
327
|
+
} else if (v.vt === 8192 || v.vt === 36) v = copyValue(v);
|
|
328
|
+
this.value = v;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
/** A temporary passed for a ByRef parameter given an expression: writes go nowhere else. */
|
|
332
|
+
var TempCell = class extends VariantCell {};
|
|
333
|
+
/** A variable of a scalar type: stores are Let-coerced to it. */
|
|
334
|
+
var ScalarCell = class {
|
|
335
|
+
vt;
|
|
336
|
+
env;
|
|
337
|
+
value;
|
|
338
|
+
constructor(vt, env) {
|
|
339
|
+
this.vt = vt;
|
|
340
|
+
this.env = env;
|
|
341
|
+
this.value = defaultFor(vt);
|
|
342
|
+
}
|
|
343
|
+
get() {
|
|
344
|
+
return this.value;
|
|
345
|
+
}
|
|
346
|
+
set(v) {
|
|
347
|
+
this.value = v.vt === this.vt ? v : letAs(this.vt, v, this.env);
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
/** `String * n`: padded with spaces or cut to n characters. */
|
|
351
|
+
var FixedStringCell = class {
|
|
352
|
+
length;
|
|
353
|
+
env;
|
|
354
|
+
value;
|
|
355
|
+
constructor(length, env) {
|
|
356
|
+
this.length = length;
|
|
357
|
+
this.env = env;
|
|
358
|
+
this.value = vStr("\0".repeat(length));
|
|
359
|
+
}
|
|
360
|
+
get() {
|
|
361
|
+
return this.value;
|
|
362
|
+
}
|
|
363
|
+
set(v) {
|
|
364
|
+
const s = letAs(8, v, this.env);
|
|
365
|
+
this.value = vStr(fixLen(s.v, this.length));
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
/** An object variable (`As Object`, `As Class`): Set stores, converted to the declared interface. */
|
|
369
|
+
var ObjectCell = class {
|
|
370
|
+
cast;
|
|
371
|
+
value = NOTHING;
|
|
372
|
+
constructor(cast = (v) => v) {
|
|
373
|
+
this.cast = cast;
|
|
374
|
+
}
|
|
375
|
+
get() {
|
|
376
|
+
return this.value;
|
|
377
|
+
}
|
|
378
|
+
set(v) {
|
|
379
|
+
if (v.vt !== 9) throw new VbaRuntimeError(v.vt === 0 ? 91 : 13);
|
|
380
|
+
const next = this.cast(v);
|
|
381
|
+
retain(next);
|
|
382
|
+
release(this.value);
|
|
383
|
+
this.value = next;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
/** `Dim x As New Class`: a new object is created whenever the variable is used while Nothing. */
|
|
387
|
+
var AutoNewCell = class extends ObjectCell {
|
|
388
|
+
create;
|
|
389
|
+
createLater;
|
|
390
|
+
constructor(create, cast, createLater) {
|
|
391
|
+
super(cast);
|
|
392
|
+
this.create = create;
|
|
393
|
+
this.createLater = createLater;
|
|
394
|
+
}
|
|
395
|
+
/** The object, created first when Nothing; a Promise only for an asynchronous Class_Initialize. */
|
|
396
|
+
ensure(run) {
|
|
397
|
+
if (this.value.v !== null || !this.createLater) return this.get();
|
|
398
|
+
const r = this.createLater(run);
|
|
399
|
+
const keep = (v) => {
|
|
400
|
+
if (this.value.v === null) this.set(v);
|
|
401
|
+
return this.value;
|
|
402
|
+
};
|
|
403
|
+
return r instanceof Promise ? r.then(keep) : keep(r);
|
|
404
|
+
}
|
|
405
|
+
get() {
|
|
406
|
+
if (this.value.v === null) this.set(this.create());
|
|
407
|
+
return this.value;
|
|
408
|
+
}
|
|
409
|
+
/** The value without creating an object (`x Is Nothing` is never True for As New). */
|
|
410
|
+
peek() {
|
|
411
|
+
return this.value;
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
/** An array variable: whole-array assignment copies ([MS-VBAL] 5.5.1.2.6). */
|
|
415
|
+
var ArrayCell = class {
|
|
416
|
+
array;
|
|
417
|
+
env;
|
|
418
|
+
value;
|
|
419
|
+
constructor(array, env) {
|
|
420
|
+
this.array = array;
|
|
421
|
+
this.env = env;
|
|
422
|
+
this.value = vArr(array);
|
|
423
|
+
}
|
|
424
|
+
get() {
|
|
425
|
+
return this.value;
|
|
426
|
+
}
|
|
427
|
+
set(v) {
|
|
428
|
+
const current = this.value.v;
|
|
429
|
+
if (current.fixed) throw new VbaRuntimeError(13, "Can't assign to array");
|
|
430
|
+
const next = vArr(assignArray(current.elemVt, v, this.env));
|
|
431
|
+
current.releaseAll();
|
|
432
|
+
this.value = next;
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
/** A user-defined type variable: assignment copies the record. */
|
|
436
|
+
var UdtCell = class {
|
|
437
|
+
type;
|
|
438
|
+
value;
|
|
439
|
+
constructor(type) {
|
|
440
|
+
this.type = type;
|
|
441
|
+
this.value = vUdt(type.create());
|
|
442
|
+
}
|
|
443
|
+
get() {
|
|
444
|
+
return this.value;
|
|
445
|
+
}
|
|
446
|
+
set(v) {
|
|
447
|
+
if (v.vt !== 36 || v.v.type !== this.type) throw new VbaRuntimeError(13);
|
|
448
|
+
this.value = copyValue(v);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
/** Builds a fresh array of an element type (a declared `Dim a(1 To 3) As Long`). */
|
|
452
|
+
function newArray(elemVt, bounds, fixed, env, init, counting = false) {
|
|
453
|
+
return new VbaArray(elemVt, bounds, {
|
|
454
|
+
fixed,
|
|
455
|
+
env,
|
|
456
|
+
counting,
|
|
457
|
+
...init ? { init } : {}
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
/** An element of an array, as a ByRef argument or the target of a store. */
|
|
461
|
+
var ElementRef = class {
|
|
462
|
+
array;
|
|
463
|
+
at;
|
|
464
|
+
constructor(array, at) {
|
|
465
|
+
this.array = array;
|
|
466
|
+
this.at = at;
|
|
467
|
+
}
|
|
468
|
+
get() {
|
|
469
|
+
return this.array.get(this.at);
|
|
470
|
+
}
|
|
471
|
+
set(v) {
|
|
472
|
+
if (!this.array.counting) escape(v);
|
|
473
|
+
this.array.set(this.at, v);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
/** A field of a user-defined type value. */
|
|
477
|
+
var FieldRef = class {
|
|
478
|
+
udt;
|
|
479
|
+
index;
|
|
480
|
+
constructor(udt, index) {
|
|
481
|
+
this.udt = udt;
|
|
482
|
+
this.index = index;
|
|
483
|
+
}
|
|
484
|
+
get() {
|
|
485
|
+
return this.udt.values[this.index];
|
|
486
|
+
}
|
|
487
|
+
set(v) {
|
|
488
|
+
escape(v);
|
|
489
|
+
this.udt.set(this.udt.type.fields[this.index].name, v);
|
|
490
|
+
}
|
|
491
|
+
};
|
|
492
|
+
/** `Dim a(n) As New Class`: an element read while Nothing gets a new object first. */
|
|
493
|
+
var AutoNewArray = class extends VbaArray {
|
|
494
|
+
create;
|
|
495
|
+
constructor(elemVt, bounds, options, create) {
|
|
496
|
+
super(elemVt, bounds, options);
|
|
497
|
+
this.create = create;
|
|
498
|
+
}
|
|
499
|
+
get(indices) {
|
|
500
|
+
const v = super.get(indices);
|
|
501
|
+
if (v.vt !== 9 || v.v !== null) return v;
|
|
502
|
+
const o = this.create();
|
|
503
|
+
if (!this.counting) escape(o);
|
|
504
|
+
super.set(indices, o);
|
|
505
|
+
return o;
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
// src/interp/compile/cells.ts
|
|
509
|
+
/** The element `VarType` of arrays of a static type. */
|
|
510
|
+
function elementVt(t) {
|
|
511
|
+
switch (t.kind) {
|
|
512
|
+
case "scalar": return t.vt;
|
|
513
|
+
case "object": return VT.Object;
|
|
514
|
+
case "udt": return VT.UserDefinedType;
|
|
515
|
+
default: return VT.Variant;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
var CellFactories = class {
|
|
519
|
+
services;
|
|
520
|
+
consts;
|
|
521
|
+
constructor(services, consts) {
|
|
522
|
+
this.services = services;
|
|
523
|
+
this.consts = consts;
|
|
524
|
+
}
|
|
525
|
+
/** The factory for a variable of `type`. */
|
|
526
|
+
make(type, options) {
|
|
527
|
+
const env = this.services.session;
|
|
528
|
+
switch (type.kind) {
|
|
529
|
+
case "variant": return () => new VariantCell();
|
|
530
|
+
case "scalar": {
|
|
531
|
+
if (type.fixedLength) {
|
|
532
|
+
const n = this.consts.number(type.fixedLength);
|
|
533
|
+
return () => new FixedStringCell(n, env);
|
|
534
|
+
}
|
|
535
|
+
const vt = type.vt;
|
|
536
|
+
return () => new ScalarCell(vt, env);
|
|
537
|
+
}
|
|
538
|
+
case "object": {
|
|
539
|
+
const cast = this.services.castObject(type);
|
|
540
|
+
if (options.isNew) {
|
|
541
|
+
const later = (run) => this.services.createObject(type, run);
|
|
542
|
+
return () => new AutoNewCell(this.creator(type), cast, later);
|
|
543
|
+
}
|
|
544
|
+
return () => new ObjectCell(cast);
|
|
545
|
+
}
|
|
546
|
+
case "udt": {
|
|
547
|
+
const udt = this.services.udtType(type.udt);
|
|
548
|
+
return () => new UdtCell(udt);
|
|
549
|
+
}
|
|
550
|
+
case "array": {
|
|
551
|
+
const element = type.element;
|
|
552
|
+
const vt = elementVt(element);
|
|
553
|
+
const init = this.elementInit(element);
|
|
554
|
+
const bounds = (options.bounds ?? []).map((b) => ({
|
|
555
|
+
lo: b.lower ? this.consts.number(b.lower) : options.base,
|
|
556
|
+
hi: this.consts.number(b.upper)
|
|
557
|
+
}));
|
|
558
|
+
const fixed = bounds.length > 0;
|
|
559
|
+
const counting = element.kind === "object" || element.kind === "variant";
|
|
560
|
+
if (options.isNew && element.kind === "object") {
|
|
561
|
+
const create = this.creator(element);
|
|
562
|
+
return () => new ArrayCell(new AutoNewArray(vt, bounds, {
|
|
563
|
+
fixed,
|
|
564
|
+
env,
|
|
565
|
+
counting
|
|
566
|
+
}, create), env);
|
|
567
|
+
}
|
|
568
|
+
return () => new ArrayCell(newArray(vt, bounds, fixed, env, init, counting), env);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
/** Creates a new object of `type` (for As New), synchronously. */
|
|
573
|
+
creator(type) {
|
|
574
|
+
return () => {
|
|
575
|
+
const r = this.services.createObject(type, this.services.activeRun());
|
|
576
|
+
if (r instanceof Promise) throw new VbaRuntimeError(51, "As New with an asynchronous Class_Initialize");
|
|
577
|
+
return r;
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
/** A fresh element of an array or field of `t` (UDTs and objects need more than a default). */
|
|
581
|
+
elementInit(t) {
|
|
582
|
+
if (t.kind === "udt") {
|
|
583
|
+
const udt = this.services.udtType(t.udt);
|
|
584
|
+
return () => ({
|
|
585
|
+
vt: 36,
|
|
586
|
+
v: udt.create()
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
if (t.kind === "object") return () => vObj(null);
|
|
590
|
+
if (t.kind === "scalar" && t.fixedLength) {
|
|
591
|
+
const n = this.consts.number(t.fixedLength);
|
|
592
|
+
return () => ({
|
|
593
|
+
vt: 8,
|
|
594
|
+
v: "\0".repeat(n)
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
return t.kind === "scalar" ? () => defaultFor(t.vt) : void 0;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
// src/interp/compile/context.ts
|
|
601
|
+
var CompileContext = class {
|
|
602
|
+
services;
|
|
603
|
+
module;
|
|
604
|
+
procedure;
|
|
605
|
+
consts;
|
|
606
|
+
cells;
|
|
607
|
+
program;
|
|
608
|
+
constructor(services, module, procedure, consts, cells) {
|
|
609
|
+
this.services = services;
|
|
610
|
+
this.module = module;
|
|
611
|
+
this.procedure = procedure;
|
|
612
|
+
this.consts = consts;
|
|
613
|
+
this.cells = cells;
|
|
614
|
+
this.program = services.program;
|
|
615
|
+
}
|
|
616
|
+
binding(e) {
|
|
617
|
+
return this.program.binding(e);
|
|
618
|
+
}
|
|
619
|
+
invocation(n) {
|
|
620
|
+
return this.program.invocation(n);
|
|
621
|
+
}
|
|
622
|
+
typeOf(e) {
|
|
623
|
+
return this.program.typeOf(e);
|
|
624
|
+
}
|
|
625
|
+
withBlock(e) {
|
|
626
|
+
return this.program.withBlock(e);
|
|
627
|
+
}
|
|
628
|
+
get compareText() {
|
|
629
|
+
return this.module.options.compareText;
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
// src/interp/compile/const-eval.ts
|
|
633
|
+
var ConstEvaluator = class {
|
|
634
|
+
evaluate;
|
|
635
|
+
env;
|
|
636
|
+
memo = /* @__PURE__ */ new Map();
|
|
637
|
+
active = /* @__PURE__ */ new Set();
|
|
638
|
+
constructor(evaluate, env) {
|
|
639
|
+
this.evaluate = evaluate;
|
|
640
|
+
this.env = env;
|
|
641
|
+
}
|
|
642
|
+
value(e) {
|
|
643
|
+
const known = this.memo.get(e);
|
|
644
|
+
if (known) return known;
|
|
645
|
+
if (this.active.has(e)) throw new VbaRuntimeError(5, "Circular reference in a constant expression");
|
|
646
|
+
this.active.add(e);
|
|
647
|
+
try {
|
|
648
|
+
const v = this.evaluate(e);
|
|
649
|
+
if (v instanceof Promise) throw new VbaRuntimeError(5, "Constant expression required");
|
|
650
|
+
this.memo.set(e, v);
|
|
651
|
+
return v;
|
|
652
|
+
} finally {
|
|
653
|
+
this.active.delete(e);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/** A constant used as a count or bound: Let-coerced to Long. */
|
|
657
|
+
number(e) {
|
|
658
|
+
return toLng(scalar(this.value(e)), this.env);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
// src/interp/ops.ts
|
|
662
|
+
/** The function of a binary operator (`Is` compares objects and needs no default values). */
|
|
663
|
+
function binaryOperator(op, flags) {
|
|
664
|
+
const { strict, typed } = flags;
|
|
665
|
+
const compare = flags.compareText ? 1 : 0;
|
|
666
|
+
switch (op) {
|
|
667
|
+
case "+": return (a, b, s) => add(a, b, strict, s);
|
|
668
|
+
case "-": return (a, b, s) => sub(a, b, strict, s);
|
|
669
|
+
case "*": return (a, b, s) => mul(a, b, strict, s);
|
|
670
|
+
case "/": return (a, b, s) => div(a, b, strict, s);
|
|
671
|
+
case "\\": return (a, b, s) => idiv(a, b, strict, s);
|
|
672
|
+
case "Mod": return (a, b, s) => mod(a, b, strict, s);
|
|
673
|
+
case "^": return (a, b, s) => pow(a, b, s);
|
|
674
|
+
case "&": return (a, b, s) => cat(a, b, s);
|
|
675
|
+
case "=": return (a, b, s) => eq(a, b, compare, typed, s);
|
|
676
|
+
case "<>": return (a, b, s) => ne(a, b, compare, typed, s);
|
|
677
|
+
case "<": return (a, b, s) => lt(a, b, compare, typed, s);
|
|
678
|
+
case ">": return (a, b, s) => gt(a, b, compare, typed, s);
|
|
679
|
+
case "<=": return (a, b, s) => le(a, b, compare, typed, s);
|
|
680
|
+
case ">=": return (a, b, s) => ge(a, b, compare, typed, s);
|
|
681
|
+
case "Like": return (a, b, s) => like(a, b, compare, s);
|
|
682
|
+
case "Is": return (a, b) => is(a, b);
|
|
683
|
+
case "And": return (a, b, s) => and(a, b, strict, s);
|
|
684
|
+
case "Or": return (a, b, s) => or(a, b, strict, s);
|
|
685
|
+
case "Xor": return (a, b, s) => xor(a, b, strict, s);
|
|
686
|
+
case "Eqv": return (a, b, s) => eqv(a, b, strict, s);
|
|
687
|
+
case "Imp": return (a, b, s) => imp(a, b, strict, s);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
function unaryOperator(op, strict) {
|
|
691
|
+
switch (op) {
|
|
692
|
+
case "-": return (a, s) => neg(a, strict, s);
|
|
693
|
+
case "Not": return (a, s) => not(a, strict, s);
|
|
694
|
+
case "+": return (a) => a;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
/** The simple data value (an object's default member), synchronous unless a host answers later. */
|
|
698
|
+
const scalarOf = (v) => v.vt === 9 ? resolveDefaultMaybe(v) : v;
|
|
699
|
+
// src/interp/objects/class-instance.ts
|
|
700
|
+
const temps = (args) => args.map((a) => new TempCell(a));
|
|
701
|
+
const EMPTY = { vt: 0 };
|
|
702
|
+
var ClassInstance = class {
|
|
703
|
+
module;
|
|
704
|
+
services;
|
|
705
|
+
vars = [];
|
|
706
|
+
statics = /* @__PURE__ */ new Map();
|
|
707
|
+
sinks = /* @__PURE__ */ new Set();
|
|
708
|
+
refs = 0;
|
|
709
|
+
escaped = false;
|
|
710
|
+
terminated = false;
|
|
711
|
+
constructor(module, services) {
|
|
712
|
+
this.module = module;
|
|
713
|
+
this.services = services;
|
|
714
|
+
counted(this);
|
|
715
|
+
}
|
|
716
|
+
typeName() {
|
|
717
|
+
return this.module.name;
|
|
718
|
+
}
|
|
719
|
+
identity() {
|
|
720
|
+
return this;
|
|
721
|
+
}
|
|
722
|
+
get(name, args, named) {
|
|
723
|
+
return this.external(name, "get", args, named, void 0, false);
|
|
724
|
+
}
|
|
725
|
+
let(name, args, value) {
|
|
726
|
+
return andThen(this.external(name, "let", args, void 0, value, false), () => void 0);
|
|
727
|
+
}
|
|
728
|
+
set(name, args, value) {
|
|
729
|
+
return andThen(this.external(name, "set", args, void 0, value, false), () => void 0);
|
|
730
|
+
}
|
|
731
|
+
call(name, args, named) {
|
|
732
|
+
return this.external(name, "call", args, named, void 0, false);
|
|
733
|
+
}
|
|
734
|
+
enumerate() {
|
|
735
|
+
const g = this.module.newEnum;
|
|
736
|
+
if (!g) throw new VbaRuntimeError(438);
|
|
737
|
+
const r = this.external(g.name, "get", [], void 0, void 0, true);
|
|
738
|
+
const items = (v) => {
|
|
739
|
+
if (v.vt !== 9 || v.v === null || !v.v.enumerate) throw new VbaRuntimeError(438);
|
|
740
|
+
return v.v.enumerate();
|
|
741
|
+
};
|
|
742
|
+
if (!(r instanceof Promise)) return items(r);
|
|
743
|
+
return { async *[Symbol.asyncIterator]() {
|
|
744
|
+
yield* items(await r);
|
|
745
|
+
} };
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* A call through the host object protocol (by the host, a runtime operation or late binding):
|
|
749
|
+
* it joins the run in progress or runs as a callback of its own (`Services.enter`).
|
|
750
|
+
* `internal` reaches private members (an interface's implementation).
|
|
751
|
+
*/
|
|
752
|
+
external(name, access, args, named, value, internal) {
|
|
753
|
+
return this.services.enter((run) => this.dispatch(name, access, temps(args), named, value, run, internal), EMPTY);
|
|
754
|
+
}
|
|
755
|
+
connect(sink) {
|
|
756
|
+
this.sinks.add(sink);
|
|
757
|
+
return () => this.sinks.delete(sink);
|
|
758
|
+
}
|
|
759
|
+
/** `RaiseEvent`: runs every connected handler in turn; arguments are references. */
|
|
760
|
+
raise(event, args) {
|
|
761
|
+
const sinks = [...this.sinks];
|
|
762
|
+
const next = (i) => {
|
|
763
|
+
for (; i < sinks.length; i++) {
|
|
764
|
+
const r = sinks[i].fire(this, event, args);
|
|
765
|
+
if (r instanceof Promise) return r.then(() => next(i + 1));
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
return next(0);
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Calls a member with argument references (the interpreter's own calls pass ByRef arguments
|
|
772
|
+
* through). `value` is the right-hand side of a Let or Set. Private members are reachable only
|
|
773
|
+
* with `internal` (an interface implementation, `Class_Initialize`).
|
|
774
|
+
*/
|
|
775
|
+
dispatch(name, access, args, named, value, run, internal = false) {
|
|
776
|
+
const group = name === "" ? this.module.defaultMember : this.module.members.get(name.toLowerCase());
|
|
777
|
+
if (!group) throw new VbaRuntimeError(438);
|
|
778
|
+
if (!(group instanceof PropertyGroup)) {
|
|
779
|
+
if (group.kind !== "variable" || !group.isPublic && !internal) throw new VbaRuntimeError(438);
|
|
780
|
+
return this.field(group.slot, access, args, value, run);
|
|
781
|
+
}
|
|
782
|
+
const proc = group.forAccess(access);
|
|
783
|
+
if (!proc) throw new VbaRuntimeError(451);
|
|
784
|
+
if (proc.visibility !== "public" && !internal) throw new VbaRuntimeError(438);
|
|
785
|
+
const reading = access === "get" || access === "call";
|
|
786
|
+
const takes = proc.params.length;
|
|
787
|
+
if (reading && takes === 0 && args.length > 0) return andThen(this.services.invoke(proc, [], [], this, run), (v) => applyArgs(v, args, run));
|
|
788
|
+
const placed = place(proc, args, named, value);
|
|
789
|
+
return this.services.invoke(proc, placed.args, placed.rest, this, run);
|
|
790
|
+
}
|
|
791
|
+
field(slot, access, args, value, run) {
|
|
792
|
+
const ref = this.vars[slot];
|
|
793
|
+
if (access === "let" || access === "set") {
|
|
794
|
+
if (args.length > 0) throw new VbaRuntimeError(450);
|
|
795
|
+
ref.set(value ?? MISSING);
|
|
796
|
+
return { vt: 0 };
|
|
797
|
+
}
|
|
798
|
+
const v = ref.get();
|
|
799
|
+
return args.length === 0 ? v : applyArgs(v, args, run);
|
|
800
|
+
}
|
|
801
|
+
retain() {
|
|
802
|
+
this.refs++;
|
|
803
|
+
}
|
|
804
|
+
release() {
|
|
805
|
+
if (--this.refs > 0 || this.escaped || this.terminated) return;
|
|
806
|
+
this.services.lastReleased(this);
|
|
807
|
+
}
|
|
808
|
+
escape() {
|
|
809
|
+
this.escaped = true;
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Runs Class_Terminate once no counted reference is left (checked at the end of a statement,
|
|
813
|
+
* so a function's result on its way to a variable is not terminated), then releases the
|
|
814
|
+
* objects the instance's own fields hold.
|
|
815
|
+
*/
|
|
816
|
+
terminate(run) {
|
|
817
|
+
if (this.terminated || this.escaped || this.refs > 0) return void 0;
|
|
818
|
+
this.terminated = true;
|
|
819
|
+
const p = this.module.groups.get("class_terminate")?.method;
|
|
820
|
+
const done = () => {
|
|
821
|
+
for (const ref of this.vars) {
|
|
822
|
+
const v = ref.get();
|
|
823
|
+
if (v.vt === 9 && v.v !== null) ref.set({
|
|
824
|
+
vt: 9,
|
|
825
|
+
v: null
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
if (!p) return done();
|
|
830
|
+
return andThen(this.services.invoke(p, [], [], this, run), done);
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
/** Places positional and named arguments (and a Let/Set value) on a procedure's parameters. */
|
|
834
|
+
function place(proc, args, named, value) {
|
|
835
|
+
const params = proc.params;
|
|
836
|
+
const fixed = params.at(-1)?.paramArray ? params.length - 1 : params.length;
|
|
837
|
+
const valueSlot = value === void 0 ? -1 : fixed - 1;
|
|
838
|
+
const limit = valueSlot >= 0 ? valueSlot : fixed;
|
|
839
|
+
const out = params.slice(0, fixed).map(() => null);
|
|
840
|
+
const rest = [];
|
|
841
|
+
args.forEach((a, i) => {
|
|
842
|
+
if (i < limit) out[i] = a;
|
|
843
|
+
else if (fixed < params.length) rest.push(a?.get() ?? MISSING);
|
|
844
|
+
else throw new VbaRuntimeError(450);
|
|
845
|
+
});
|
|
846
|
+
if (named) for (const [n, v] of named) {
|
|
847
|
+
const i = params.findIndex((p) => p.name.toLowerCase() === n.toLowerCase());
|
|
848
|
+
if (i < 0 || i >= limit) throw new VbaRuntimeError(448);
|
|
849
|
+
out[i] = new TempCell(v);
|
|
850
|
+
}
|
|
851
|
+
if (valueSlot >= 0) out[valueSlot] = new TempCell(value);
|
|
852
|
+
return {
|
|
853
|
+
args: out,
|
|
854
|
+
rest
|
|
855
|
+
};
|
|
856
|
+
}
|
|
857
|
+
/** Applies arguments to a value: an array element, or the default member of an object. */
|
|
858
|
+
function applyArgs(v, args, run) {
|
|
859
|
+
const values = args.map((a) => a?.get() ?? MISSING);
|
|
860
|
+
if (v.vt === 8192) return v.v.get(values.map((x) => toLng(scalar(x), run.session)));
|
|
861
|
+
if (v.vt !== 9) throw new VbaRuntimeError(450);
|
|
862
|
+
if (v.v === null) throw new VbaRuntimeError(91);
|
|
863
|
+
if (v.v instanceof ClassInstance) return v.v.dispatch("", "get", args, void 0, void 0, run);
|
|
864
|
+
return v.v.get("", values);
|
|
865
|
+
}
|
|
866
|
+
/** The class instance a value holds (directly or through an interface view), else null. */
|
|
867
|
+
function classInstanceOf(v) {
|
|
868
|
+
if (v.vt !== 9 || v.v === null) return null;
|
|
869
|
+
const o = v.v;
|
|
870
|
+
if (o instanceof ClassInstance) return o;
|
|
871
|
+
const viewed = o.instance;
|
|
872
|
+
return viewed instanceof ClassInstance ? viewed : null;
|
|
873
|
+
}
|
|
874
|
+
// src/interp/objects/interface-view.ts
|
|
875
|
+
var InterfaceView = class {
|
|
876
|
+
instance;
|
|
877
|
+
iface;
|
|
878
|
+
constructor(instance, iface) {
|
|
879
|
+
this.instance = instance;
|
|
880
|
+
this.iface = iface;
|
|
881
|
+
counted(this);
|
|
882
|
+
this[VIEW] = instance;
|
|
883
|
+
}
|
|
884
|
+
typeName() {
|
|
885
|
+
return this.instance.typeName();
|
|
886
|
+
}
|
|
887
|
+
identity() {
|
|
888
|
+
return this.instance;
|
|
889
|
+
}
|
|
890
|
+
/** The implementing member's name: `IShape_Area`, or the default member's. */
|
|
891
|
+
member(name) {
|
|
892
|
+
const n = name === "" ? this.iface.defaultMember?.name : name;
|
|
893
|
+
return n === void 0 ? "" : `${this.iface.name}_${n}`;
|
|
894
|
+
}
|
|
895
|
+
get(name, args, named) {
|
|
896
|
+
return this.instance.external(this.member(name), "get", args, named, void 0, true);
|
|
897
|
+
}
|
|
898
|
+
let(name, args, value) {
|
|
899
|
+
return andThen(this.instance.external(this.member(name), "let", args, void 0, value, true), () => void 0);
|
|
900
|
+
}
|
|
901
|
+
set(name, args, value) {
|
|
902
|
+
return andThen(this.instance.external(this.member(name), "set", args, void 0, value, true), () => void 0);
|
|
903
|
+
}
|
|
904
|
+
call(name, args, named) {
|
|
905
|
+
return this.instance.external(this.member(name), "call", args, named, void 0, true);
|
|
906
|
+
}
|
|
907
|
+
retain() {
|
|
908
|
+
this.instance.retain();
|
|
909
|
+
}
|
|
910
|
+
release() {
|
|
911
|
+
this.instance.release();
|
|
912
|
+
}
|
|
913
|
+
escape() {
|
|
914
|
+
this.instance.escape();
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
// src/interp/objects/type-of.ts
|
|
918
|
+
const FACTS = /* @__PURE__ */ new WeakMap();
|
|
919
|
+
/** The class facts of an interpreter object (an instance, or one seen through an interface). */
|
|
920
|
+
function classOf(o) {
|
|
921
|
+
const instance = o instanceof ClassInstance ? o : o instanceof InterfaceView ? o.instance : null;
|
|
922
|
+
if (!instance) return null;
|
|
923
|
+
const m = instance.module;
|
|
924
|
+
let facts = FACTS.get(m);
|
|
925
|
+
if (!facts) {
|
|
926
|
+
facts = {
|
|
927
|
+
name: m.name,
|
|
928
|
+
implements: m.implements.map((i) => i.name)
|
|
929
|
+
};
|
|
930
|
+
FACTS.set(m, facts);
|
|
931
|
+
}
|
|
932
|
+
return facts;
|
|
933
|
+
}
|
|
934
|
+
function typeOfTest(ctx, type) {
|
|
935
|
+
const name = type.parts.at(-1)?.name ?? type.name;
|
|
936
|
+
const qualified = type.parts.length > 1 ? (type.parts[0]?.name ?? "").toLowerCase() : null;
|
|
937
|
+
const facts = qualified !== null && qualified !== ctx.services.session.projectName.toLowerCase() ? (o) => classOf(o) ? {
|
|
938
|
+
name: "",
|
|
939
|
+
implements: []
|
|
940
|
+
} : null : classOf;
|
|
941
|
+
return (v) => typeIs(v, name, facts);
|
|
942
|
+
}
|
|
943
|
+
// src/interp/objects/members.ts
|
|
944
|
+
/** The object a value holds: error 91 for Nothing, 424 for a value that is not an object. */
|
|
945
|
+
function objectOf(v) {
|
|
946
|
+
if (v.vt !== 9) throw new VbaRuntimeError(424);
|
|
947
|
+
if (v.v === null) throw new VbaRuntimeError(91);
|
|
948
|
+
return v.v;
|
|
949
|
+
}
|
|
950
|
+
/** Argument values for a host member: objects escape, unless the host counts its references. */
|
|
951
|
+
const values = (args, counting) => args.map((a) => {
|
|
952
|
+
if (!a) return MISSING;
|
|
953
|
+
const v = a.get();
|
|
954
|
+
if (!counting) escape(v);
|
|
955
|
+
return v;
|
|
956
|
+
});
|
|
957
|
+
/** Calls a member of an object with argument references (`value` for Let and Set). */
|
|
958
|
+
function member(o, name, access, args, named, run, value) {
|
|
959
|
+
if (o instanceof ClassInstance) return o.dispatch(name, access, args, named, value, run);
|
|
960
|
+
if (o instanceof InterfaceView) return o.instance.dispatch(o.member(name), access, args, named, value, run, true);
|
|
961
|
+
if (name.toLowerCase() === "_newenum" && o.enumerate && args.length === 0) return vObj(enumerator(o));
|
|
962
|
+
const list = values(args, isCountingStore(o));
|
|
963
|
+
switch (access) {
|
|
964
|
+
case "get": return o.get(name, list, named);
|
|
965
|
+
case "call": return o.call(name, list, named);
|
|
966
|
+
case "let": return done(letMember(o, name, list, value, (t, v) => andThen(member(t, "", "let", [], void 0, run, v), () => void 0)));
|
|
967
|
+
case "set":
|
|
968
|
+
escape(value);
|
|
969
|
+
return done(o.set(name, list, value));
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
const EMPTY_RESULT = { vt: 0 };
|
|
973
|
+
const done = (r) => andThen(r, () => EMPTY_RESULT);
|
|
974
|
+
/** `obj.[_NewEnum]`: an object that enumerates what `obj` enumerates. */
|
|
975
|
+
function enumerator(o) {
|
|
976
|
+
return {
|
|
977
|
+
typeName: () => "IEnumVARIANT",
|
|
978
|
+
get: () => {
|
|
979
|
+
throw new VbaRuntimeError(438);
|
|
980
|
+
},
|
|
981
|
+
let: () => {
|
|
982
|
+
throw new VbaRuntimeError(438);
|
|
983
|
+
},
|
|
984
|
+
set: () => {
|
|
985
|
+
throw new VbaRuntimeError(438);
|
|
986
|
+
},
|
|
987
|
+
call: () => {
|
|
988
|
+
throw new VbaRuntimeError(438);
|
|
989
|
+
},
|
|
990
|
+
enumerate: () => o.enumerate()
|
|
991
|
+
};
|
|
992
|
+
}
|
|
993
|
+
/** An array index: each index Let-coerced to Long (error 9 out of range, 13 for a non-number). */
|
|
994
|
+
function indexOf(v, s) {
|
|
995
|
+
if (v.vt === 2 || v.vt === 3) return v.v;
|
|
996
|
+
return toLng(v, s);
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* `value(args)`: an array element, else the default member called with the arguments. With
|
|
1000
|
+
* `access` let or set, stores `newValue` there instead.
|
|
1001
|
+
*/
|
|
1002
|
+
function indexValue(value, args, refs, f, access = "get", newValue) {
|
|
1003
|
+
if (value.vt === 8192) {
|
|
1004
|
+
const idx = [];
|
|
1005
|
+
for (let i = 0; i < args.length; i++) {
|
|
1006
|
+
const s = scalarOf(args[i]);
|
|
1007
|
+
if (s instanceof Promise) return s.then(() => indexValue(value, args, refs, f, access, newValue));
|
|
1008
|
+
idx.push(indexOf(s, f.session));
|
|
1009
|
+
}
|
|
1010
|
+
if (access === "let" || access === "set") {
|
|
1011
|
+
new ElementRef(value.v, idx).set(newValue);
|
|
1012
|
+
return EMPTY_RESULT;
|
|
1013
|
+
}
|
|
1014
|
+
return value.v.get(idx);
|
|
1015
|
+
}
|
|
1016
|
+
if (value.vt === 9) return member(objectOf(value), "", access, refs, void 0, f.run, newValue);
|
|
1017
|
+
throw new VbaRuntimeError(13);
|
|
1018
|
+
}
|
|
1019
|
+
// src/interp/compile/variables.ts
|
|
1020
|
+
function variableRef(ctx, sym) {
|
|
1021
|
+
const i = sym.slot;
|
|
1022
|
+
if (sym.kind === "param") return (f) => f.slots[i];
|
|
1023
|
+
switch (sym.scope) {
|
|
1024
|
+
case "local":
|
|
1025
|
+
case "result": return (f) => f.slots[i];
|
|
1026
|
+
case "static": return (f) => f.statics[i];
|
|
1027
|
+
case "instance": return (f) => {
|
|
1028
|
+
if (!f.instance) throw new VbaRuntimeError(91);
|
|
1029
|
+
return f.instance.vars[i];
|
|
1030
|
+
};
|
|
1031
|
+
case "module": {
|
|
1032
|
+
const m = ctx.program.module(sym.module);
|
|
1033
|
+
if (!m) throw new Error(`unknown module ${sym.module}`);
|
|
1034
|
+
const state = ctx.services.moduleState(m);
|
|
1035
|
+
return () => state.vars[i];
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
/** A temporary for an expression passed ByRef: arrays and records are copied, as VBA does. */
|
|
1040
|
+
const temp = (v) => new TempCell(v.vt === 8192 || v.vt === 36 ? copyValue(v) : v);
|
|
1041
|
+
// src/interp/compile/args.ts
|
|
1042
|
+
/** Runs parts in order; the result is synchronous unless a part returns a Promise. */
|
|
1043
|
+
function collect(parts, f) {
|
|
1044
|
+
const out = new Array(parts.length);
|
|
1045
|
+
for (let i = 0; i < parts.length; i++) {
|
|
1046
|
+
const r = parts[i](f);
|
|
1047
|
+
if (r instanceof Promise) return finish(parts, f, out, i, r);
|
|
1048
|
+
out[i] = r;
|
|
1049
|
+
}
|
|
1050
|
+
return out;
|
|
1051
|
+
}
|
|
1052
|
+
async function finish(parts, f, out, at, pending) {
|
|
1053
|
+
out[at] = await pending;
|
|
1054
|
+
for (let i = at + 1; i < parts.length; i++) out[i] = await parts[i](f);
|
|
1055
|
+
return out;
|
|
1056
|
+
}
|
|
1057
|
+
const NONE = () => null;
|
|
1058
|
+
function tempOf(x, e, copy) {
|
|
1059
|
+
const v = x.value(e);
|
|
1060
|
+
return (f) => {
|
|
1061
|
+
const r = v(f);
|
|
1062
|
+
if (r instanceof Promise) return r.then((y) => copy ? temp(y) : new TempCell(y));
|
|
1063
|
+
return copy ? temp(r) : new TempCell(r);
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
/** The arguments of a call to a compiled procedure, as the binder matched them. */
|
|
1067
|
+
function siteArgs(x, site) {
|
|
1068
|
+
const parts = site.params.map((a) => {
|
|
1069
|
+
if (!a || !a.expression) return NONE;
|
|
1070
|
+
if (a.passing === "ref") return x.ref(a.expression);
|
|
1071
|
+
return tempOf(x, a.expression, a.passing === "temp");
|
|
1072
|
+
});
|
|
1073
|
+
const rest = site.rest.map((e) => e ? x.value(e) : () => MISSING);
|
|
1074
|
+
if (rest.length === 0) {
|
|
1075
|
+
if (parts.length === 0) return () => ({
|
|
1076
|
+
refs: [],
|
|
1077
|
+
rest: []
|
|
1078
|
+
});
|
|
1079
|
+
return (f) => {
|
|
1080
|
+
const r = collect(parts, f);
|
|
1081
|
+
return r instanceof Promise ? r.then((refs) => ({
|
|
1082
|
+
refs,
|
|
1083
|
+
rest: []
|
|
1084
|
+
})) : {
|
|
1085
|
+
refs: r,
|
|
1086
|
+
rest: []
|
|
1087
|
+
};
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
const all = [...parts, ...rest];
|
|
1091
|
+
const n = parts.length;
|
|
1092
|
+
return (f) => {
|
|
1093
|
+
const r = collect(all, f);
|
|
1094
|
+
const split = (list) => ({
|
|
1095
|
+
refs: list.slice(0, n),
|
|
1096
|
+
rest: list.slice(n)
|
|
1097
|
+
});
|
|
1098
|
+
return r instanceof Promise ? r.then(split) : split(r);
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
/** The arguments of a library call: values, undefined where omitted. */
|
|
1102
|
+
function libraryArgs(x, site) {
|
|
1103
|
+
const parts = site.params.map((a) => a && a.expression ? x.value(a.expression) : () => void 0);
|
|
1104
|
+
const all = [...parts, ...site.rest.map((e) => e ? x.value(e) : () => MISSING)];
|
|
1105
|
+
const n = parts.length;
|
|
1106
|
+
return (f) => {
|
|
1107
|
+
const r = collect(all, f);
|
|
1108
|
+
const split = (list) => ({
|
|
1109
|
+
args: list.slice(0, n),
|
|
1110
|
+
rest: list.slice(n)
|
|
1111
|
+
});
|
|
1112
|
+
return r instanceof Promise ? r.then(split) : split(r);
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
/** Arguments of a late-bound member: variables by reference, the rest as temporaries. */
|
|
1116
|
+
function lateArgs(x, args) {
|
|
1117
|
+
const positional = args.filter((a) => !a.name);
|
|
1118
|
+
const named = args.filter((a) => a.name);
|
|
1119
|
+
const parts = positional.map((a) => {
|
|
1120
|
+
if (!a.value) return NONE;
|
|
1121
|
+
if (!a.byVal && x.isVariable(a.value)) return x.ref(a.value);
|
|
1122
|
+
return tempOf(x, a.value, true);
|
|
1123
|
+
});
|
|
1124
|
+
const names = named.map((a) => (a.name?.name ?? "").toLowerCase());
|
|
1125
|
+
const values = named.map((a) => a.value ? x.value(a.value) : () => MISSING);
|
|
1126
|
+
if (named.length === 0) {
|
|
1127
|
+
if (parts.length === 0) return () => ({
|
|
1128
|
+
refs: [],
|
|
1129
|
+
named: void 0
|
|
1130
|
+
});
|
|
1131
|
+
return (f) => {
|
|
1132
|
+
const r = collect(parts, f);
|
|
1133
|
+
return r instanceof Promise ? r.then((refs) => ({
|
|
1134
|
+
refs,
|
|
1135
|
+
named: void 0
|
|
1136
|
+
})) : {
|
|
1137
|
+
refs: r,
|
|
1138
|
+
named: void 0
|
|
1139
|
+
};
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
const all = [...parts, ...values];
|
|
1143
|
+
const n = parts.length;
|
|
1144
|
+
return (f) => {
|
|
1145
|
+
const r = collect(all, f);
|
|
1146
|
+
const split = (list) => ({
|
|
1147
|
+
refs: list.slice(0, n),
|
|
1148
|
+
named: new Map(names.map((name, i) => [name, list[n + i]]))
|
|
1149
|
+
});
|
|
1150
|
+
return r instanceof Promise ? r.then(split) : split(r);
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
// src/interp/compile/types.ts
|
|
1154
|
+
/** An expression that always raises error `n` (a member that cannot run, an unbound call). */
|
|
1155
|
+
const fail = (n, what) => () => {
|
|
1156
|
+
throw new VbaRuntimeError(n, what);
|
|
1157
|
+
};
|
|
1158
|
+
// src/interp/compile/calls.ts
|
|
1159
|
+
/**
|
|
1160
|
+
* Compiles a call or index. `value` (for Let and Set) is the right-hand side, appended as the
|
|
1161
|
+
* last argument of a Property Let/Set or stored into the element or default member.
|
|
1162
|
+
*/
|
|
1163
|
+
function invocationValue(x, node, target, args, access, value) {
|
|
1164
|
+
const inv = x.ctx.invocation(node);
|
|
1165
|
+
switch (inv?.kind) {
|
|
1166
|
+
case "call":
|
|
1167
|
+
if (!inv.indexResult) return procedureCall(x, target, inv.group, inv.site, access, value);
|
|
1168
|
+
return indexed(x, procedureCall(x, target, inv.group, inv.site, "get", void 0), args, access, value);
|
|
1169
|
+
case "declare": return () => notSupported(`Calling the DLL procedure ${inv.symbol.name} (Declare)`);
|
|
1170
|
+
case "library": {
|
|
1171
|
+
if (inv.typedLength !== void 0) {
|
|
1172
|
+
const size = lenOfType(inv.typedLength);
|
|
1173
|
+
return () => size;
|
|
1174
|
+
}
|
|
1175
|
+
const call = x.ctx.services.library.compile(inv.entry, x.ctx.module, inv.typedArgument ?? false);
|
|
1176
|
+
const values = libraryArgs(x, inv.site);
|
|
1177
|
+
return (f) => {
|
|
1178
|
+
const a = values(f);
|
|
1179
|
+
if (a instanceof Promise) return a.then((r) => call(r.args, r.rest, f));
|
|
1180
|
+
return call(a.args, a.rest, f);
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
case "index": return indexCall(x, target, args, access, value);
|
|
1184
|
+
case "dynamic": return dynamicCall(x, target, args, access, value);
|
|
1185
|
+
case "member": return memberCall(x, target, args, inv.name, access, value);
|
|
1186
|
+
default: return fail(51, "Internal error: an unbound call");
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
function procedureCall(x, target, group, site, access, value) {
|
|
1190
|
+
const proc = group.forAccess(access);
|
|
1191
|
+
if (!proc) return fail(access === "get" ? 451 : 438);
|
|
1192
|
+
const services = x.ctx.services;
|
|
1193
|
+
const argsOf = siteArgs(x, site);
|
|
1194
|
+
const me = receiverOf(x, target);
|
|
1195
|
+
return (f) => {
|
|
1196
|
+
const call = (instance) => {
|
|
1197
|
+
const withArgs = (r, rhs) => services.invoke(proc, rhs ? [...r.refs, new TempCell(rhs)] : r.refs, r.rest, instance, f.run);
|
|
1198
|
+
if (!value) return andThen(argsOf(f), (r) => withArgs(r));
|
|
1199
|
+
return andThen(value(f), (rhs) => andThen(argsOf(f), (r) => withArgs(r, rhs)));
|
|
1200
|
+
};
|
|
1201
|
+
const instance = me(f);
|
|
1202
|
+
return instance instanceof Promise ? instance.then(call) : call(instance);
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
/** Where `Me` of the called procedure comes from: none, the caller's own, or the member's object. */
|
|
1206
|
+
function receiverOf(x, target) {
|
|
1207
|
+
const b = x.ctx.binding(target);
|
|
1208
|
+
if (b?.kind !== "procedure" || b.receiver === "none") return () => null;
|
|
1209
|
+
if (b.receiver === "self" || target.kind !== "MemberAccess") return (f) => f.instance;
|
|
1210
|
+
const object = x.qualifier(target);
|
|
1211
|
+
return (f) => {
|
|
1212
|
+
const o = object(f);
|
|
1213
|
+
return o instanceof Promise ? o.then(instanceOf) : instanceOf(o);
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
/** Values of index arguments, Let-coerced to Long when used on an array. */
|
|
1217
|
+
function indexParts(x, args) {
|
|
1218
|
+
return args.map((a) => {
|
|
1219
|
+
if (!a.value) return fail(449);
|
|
1220
|
+
return x.value(a.value);
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
function indexCall(x, target, args, access, value) {
|
|
1224
|
+
const base = x.value(target);
|
|
1225
|
+
const parts = indexParts(x, args);
|
|
1226
|
+
if (parts.length === 1 && !value && access !== "let" && access !== "set") {
|
|
1227
|
+
const only = parts[0];
|
|
1228
|
+
return (f) => {
|
|
1229
|
+
const a = base(f);
|
|
1230
|
+
const i = only(f);
|
|
1231
|
+
if (a instanceof Promise || i instanceof Promise) return Promise.all([a, i]).then(([av, iv]) => indexValue(av, [iv], [], f));
|
|
1232
|
+
if (a.vt === 8192 && (i.vt === 2 || i.vt === 3)) return a.v.get([i.v]);
|
|
1233
|
+
if (a.vt === 8192) {
|
|
1234
|
+
const s = scalarOf(i);
|
|
1235
|
+
if (!(s instanceof Promise)) return a.v.get([indexOf(s, f.session)]);
|
|
1236
|
+
}
|
|
1237
|
+
return indexValue(a, [i], [], f);
|
|
1238
|
+
};
|
|
1239
|
+
}
|
|
1240
|
+
const rhsFirst = value !== void 0 && x.ctx.typeOf(target).kind === "array";
|
|
1241
|
+
return indexed(x, x.value(target), args, access, value, rhsFirst);
|
|
1242
|
+
}
|
|
1243
|
+
function dynamicCall(x, target, args, access, value) {
|
|
1244
|
+
return indexed(x, x.value(target), args, access, value);
|
|
1245
|
+
}
|
|
1246
|
+
/** Indexes a value at run time: an array element, or the default member with the arguments. */
|
|
1247
|
+
function indexed(x, base, args, access, value, rhsFirst = false) {
|
|
1248
|
+
const parts = indexParts(x, args);
|
|
1249
|
+
if (rhsFirst && value) return (f) => {
|
|
1250
|
+
const r = value(f);
|
|
1251
|
+
const store = (rv) => {
|
|
1252
|
+
const a = base(f);
|
|
1253
|
+
const on = (av) => {
|
|
1254
|
+
const list = collect(parts, f);
|
|
1255
|
+
const put = (vals) => indexValue(av, vals, [], f, access, rv);
|
|
1256
|
+
return list instanceof Promise ? list.then(put) : put(list);
|
|
1257
|
+
};
|
|
1258
|
+
return a instanceof Promise ? a.then(on) : on(a);
|
|
1259
|
+
};
|
|
1260
|
+
return r instanceof Promise ? r.then(store) : store(r);
|
|
1261
|
+
};
|
|
1262
|
+
return (f) => {
|
|
1263
|
+
const a = base(f);
|
|
1264
|
+
const go = (av) => {
|
|
1265
|
+
const list = collect(parts, f);
|
|
1266
|
+
const withArgs = (vals) => {
|
|
1267
|
+
const refs = av.vt === 9 ? vals.map((v) => new TempCell(v)) : [];
|
|
1268
|
+
if (!value) return indexValue(av, vals, refs, f, access === "call" ? "get" : access);
|
|
1269
|
+
const rhs = value(f);
|
|
1270
|
+
return rhs instanceof Promise ? rhs.then((r) => indexValue(av, vals, refs, f, access, r)) : indexValue(av, vals, refs, f, access, rhs);
|
|
1271
|
+
};
|
|
1272
|
+
return list instanceof Promise ? list.then(withArgs) : withArgs(list);
|
|
1273
|
+
};
|
|
1274
|
+
return a instanceof Promise ? a.then(go) : go(a);
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
function memberCall(x, target, args, name, access, value) {
|
|
1278
|
+
const argsOf = lateArgs(x, args);
|
|
1279
|
+
const rhsOf = value ?? (() => void 0);
|
|
1280
|
+
const b = x.ctx.binding(target);
|
|
1281
|
+
if (target.kind === "Name" && b?.kind === "dynamic") {
|
|
1282
|
+
const n = x.dynamics.of(b.name, b.document, b.fallback);
|
|
1283
|
+
return (f) => {
|
|
1284
|
+
const a = argsOf(f);
|
|
1285
|
+
const go = (r) => {
|
|
1286
|
+
const rhs = value ? rhsOf(f) : void 0;
|
|
1287
|
+
if (rhs instanceof Promise) return rhs.then((v) => x.dynamics.access(n, f, access, r.refs, r.named, v));
|
|
1288
|
+
return x.dynamics.access(n, f, access, r.refs, r.named, rhs);
|
|
1289
|
+
};
|
|
1290
|
+
return a instanceof Promise ? a.then(go) : go(a);
|
|
1291
|
+
};
|
|
1292
|
+
}
|
|
1293
|
+
if (target.kind !== "MemberAccess" && target.kind !== "DictionaryAccess") return fail(51, "Internal error: a member call without an object");
|
|
1294
|
+
const object = x.qualifier(target);
|
|
1295
|
+
return (f) => {
|
|
1296
|
+
const o = object(f);
|
|
1297
|
+
const go = (ov) => {
|
|
1298
|
+
const obj = objectOf(ov);
|
|
1299
|
+
const a = argsOf(f);
|
|
1300
|
+
const call = (r) => {
|
|
1301
|
+
const rhs = value ? rhsOf(f) : void 0;
|
|
1302
|
+
if (rhs instanceof Promise) return rhs.then((v) => member(obj, name, access, r.refs, r.named, f.run, v));
|
|
1303
|
+
return member(obj, name, access, r.refs, r.named, f.run, rhs);
|
|
1304
|
+
};
|
|
1305
|
+
return a instanceof Promise ? a.then(call) : call(a);
|
|
1306
|
+
};
|
|
1307
|
+
return o instanceof Promise ? o.then(go) : go(o);
|
|
1308
|
+
};
|
|
1309
|
+
}
|
|
1310
|
+
/** The instance an early-bound member is called on: error 91 for Nothing, 13 for another object. */
|
|
1311
|
+
function instanceOf(v) {
|
|
1312
|
+
if (v.vt !== 9 || v.v === null) throw new VbaRuntimeError(91);
|
|
1313
|
+
const o = classInstanceOf(v);
|
|
1314
|
+
if (!o) throw new VbaRuntimeError(13);
|
|
1315
|
+
return o;
|
|
1316
|
+
}
|
|
1317
|
+
// src/interp/compile/names.ts
|
|
1318
|
+
function libraryConstantValue(entry) {
|
|
1319
|
+
if (typeof entry.value === "string") return vStr(entry.value);
|
|
1320
|
+
if (entry.type.kind === "scalar") return letAs(entry.type.vt, vDbl(entry.value));
|
|
1321
|
+
return vLng(entry.value);
|
|
1322
|
+
}
|
|
1323
|
+
/** The value of a bound name-like node; `access` is `call` when it stands alone as a statement. */
|
|
1324
|
+
function bindingValue(x, node, b, access) {
|
|
1325
|
+
const services = x.ctx.services;
|
|
1326
|
+
switch (b?.kind) {
|
|
1327
|
+
case "variable":
|
|
1328
|
+
case "param":
|
|
1329
|
+
case "implicit":
|
|
1330
|
+
case "result": {
|
|
1331
|
+
const ref = variableRef(x.ctx, b.symbol);
|
|
1332
|
+
if (b.symbol.kind === "variable" && b.symbol.isNew && b.symbol.bounds === null) return (f) => {
|
|
1333
|
+
const cell = ref(f);
|
|
1334
|
+
return cell instanceof AutoNewCell ? cell.ensure(f.run) : cell.get();
|
|
1335
|
+
};
|
|
1336
|
+
return (f) => ref(f).get();
|
|
1337
|
+
}
|
|
1338
|
+
case "const": {
|
|
1339
|
+
const symbol = b.symbol;
|
|
1340
|
+
let v;
|
|
1341
|
+
return () => v ??= services.constValue(symbol);
|
|
1342
|
+
}
|
|
1343
|
+
case "enumMember": {
|
|
1344
|
+
const symbol = b.symbol;
|
|
1345
|
+
let v;
|
|
1346
|
+
return () => v ??= services.enumValue(symbol);
|
|
1347
|
+
}
|
|
1348
|
+
case "libraryConstant": {
|
|
1349
|
+
const v = libraryConstantValue(b.entry);
|
|
1350
|
+
return () => v;
|
|
1351
|
+
}
|
|
1352
|
+
case "procedure":
|
|
1353
|
+
if (!x.ctx.invocation(node)) return procedureCall(x, node, b.group, {
|
|
1354
|
+
params: [],
|
|
1355
|
+
rest: []
|
|
1356
|
+
}, access === "call" ? "get" : access, void 0);
|
|
1357
|
+
return x.invoke(node, node, [], access);
|
|
1358
|
+
case "libraryFunction":
|
|
1359
|
+
case "declare": return x.invoke(node, node, [], access);
|
|
1360
|
+
case "libraryObject": {
|
|
1361
|
+
if (b.entry.name === "VBA") return fail(424);
|
|
1362
|
+
const o = b.entry.name === "Err" ? services.err : services.debug;
|
|
1363
|
+
const v = vObj(o);
|
|
1364
|
+
return () => v;
|
|
1365
|
+
}
|
|
1366
|
+
case "hostGlobal": {
|
|
1367
|
+
const o = services.host.globals.get(b.name.toLowerCase());
|
|
1368
|
+
if (!o) return fail(424);
|
|
1369
|
+
const v = vObj(o);
|
|
1370
|
+
return () => v;
|
|
1371
|
+
}
|
|
1372
|
+
case "documentObject": {
|
|
1373
|
+
const o = services.host.documentObjects.get(b.module.name.toLowerCase());
|
|
1374
|
+
return o ? () => vObj(o) : fail(424);
|
|
1375
|
+
}
|
|
1376
|
+
case "class": {
|
|
1377
|
+
const m = b.module;
|
|
1378
|
+
if (!m.predeclared) return fail(424);
|
|
1379
|
+
return (f) => services.defaultInstance(m, f.run);
|
|
1380
|
+
}
|
|
1381
|
+
case "me": return (f) => f.me ? vObj(f.me) : NOTHING;
|
|
1382
|
+
case "dynamic": {
|
|
1383
|
+
const n = x.dynamics.of(b.name, b.document, b.fallback);
|
|
1384
|
+
return (f) => x.dynamics.access(n, f, access, [], void 0);
|
|
1385
|
+
}
|
|
1386
|
+
case "member":
|
|
1387
|
+
case "errMember":
|
|
1388
|
+
case "debugMember":
|
|
1389
|
+
case "udtField":
|
|
1390
|
+
if (node.kind === "MemberAccess" || node.kind === "DictionaryAccess") return memberValue(x, node, access);
|
|
1391
|
+
return fail(51);
|
|
1392
|
+
case "unresolved": return fail(b.number, b.number === 35 ? `Sub, Function or variable not defined: ${b.name}` : void 0);
|
|
1393
|
+
case "module":
|
|
1394
|
+
case "enum": return fail(424);
|
|
1395
|
+
default: return fail(51, "Internal error: an unbound name");
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
function nameValue(x, e, access) {
|
|
1399
|
+
return bindingValue(x, e, x.ctx.binding(e), access);
|
|
1400
|
+
}
|
|
1401
|
+
/** `object.member`, `.member` in a With block, `object!key`. */
|
|
1402
|
+
function memberValue(x, e, access) {
|
|
1403
|
+
const b = x.ctx.binding(e);
|
|
1404
|
+
switch (b?.kind) {
|
|
1405
|
+
case "udtField": {
|
|
1406
|
+
const object = x.qualifier(e);
|
|
1407
|
+
const i = b.index;
|
|
1408
|
+
return (f) => {
|
|
1409
|
+
const u = object(f);
|
|
1410
|
+
if (u instanceof Promise) return u.then((y) => udtField(y, i));
|
|
1411
|
+
return udtField(u, i);
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
case "member":
|
|
1415
|
+
case "errMember":
|
|
1416
|
+
case "debugMember": {
|
|
1417
|
+
const object = x.qualifier(e);
|
|
1418
|
+
if (e.kind === "DictionaryAccess" || b.kind === "member" && b.dictionary) {
|
|
1419
|
+
const key = vStr(e.kind === "DictionaryAccess" ? e.key.name : b.name);
|
|
1420
|
+
return (f) => {
|
|
1421
|
+
const o = object(f);
|
|
1422
|
+
const read = (v) => member(objectOf(v), "", "get", [new TempCell(key)], void 0, f.run);
|
|
1423
|
+
return o instanceof Promise ? o.then(read) : read(o);
|
|
1424
|
+
};
|
|
1425
|
+
}
|
|
1426
|
+
const name = b.name;
|
|
1427
|
+
const how = access === "call" ? "call" : "get";
|
|
1428
|
+
return (f) => {
|
|
1429
|
+
const o = object(f);
|
|
1430
|
+
if (o instanceof Promise) return o.then((v) => member(objectOf(v), name, how, [], void 0, f.run));
|
|
1431
|
+
return member(objectOf(o), name, how, [], void 0, f.run);
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
default: return bindingValue(x, e, b, access);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
function udtField(v, i) {
|
|
1438
|
+
if (v.vt !== 36) throw new VbaRuntimeError(13);
|
|
1439
|
+
return v.v.values[i];
|
|
1440
|
+
}
|
|
1441
|
+
// src/interp/compile/refs.ts
|
|
1442
|
+
function valueTemp(x, e) {
|
|
1443
|
+
const v = x.value(e);
|
|
1444
|
+
return (f) => {
|
|
1445
|
+
const r = v(f);
|
|
1446
|
+
return r instanceof Promise ? r.then(temp) : temp(r);
|
|
1447
|
+
};
|
|
1448
|
+
}
|
|
1449
|
+
function referenceOf(x, e) {
|
|
1450
|
+
const b = x.ctx.binding(e);
|
|
1451
|
+
switch (e.kind) {
|
|
1452
|
+
case "Name":
|
|
1453
|
+
if (b?.kind === "variable" || b?.kind === "param" || b?.kind === "implicit" || b?.kind === "result") return variableRef(x.ctx, b.symbol);
|
|
1454
|
+
if (b?.kind === "dynamic" && b.fallback) {
|
|
1455
|
+
const n = x.dynamics.of(b.name, b.document, b.fallback);
|
|
1456
|
+
const v = x.value(e);
|
|
1457
|
+
return (f) => {
|
|
1458
|
+
const r = v(f);
|
|
1459
|
+
const pick = (value) => n.isLocal(f.run) && n.local ? n.local(f) : temp(value);
|
|
1460
|
+
return r instanceof Promise ? r.then(pick) : pick(r);
|
|
1461
|
+
};
|
|
1462
|
+
}
|
|
1463
|
+
return valueTemp(x, e);
|
|
1464
|
+
case "CallOrIndex": {
|
|
1465
|
+
const inv = x.ctx.invocation(e);
|
|
1466
|
+
if (inv?.kind !== "index" && inv?.kind !== "dynamic") return valueTemp(x, e);
|
|
1467
|
+
const base = x.ref(e.target);
|
|
1468
|
+
const parts = e.args.map((a) => a.value ? x.value(a.value) : () => ({ vt: 0 }));
|
|
1469
|
+
const whole = x.value(e);
|
|
1470
|
+
return (f) => {
|
|
1471
|
+
const r = base(f);
|
|
1472
|
+
const go = (ref) => {
|
|
1473
|
+
const v = ref.get();
|
|
1474
|
+
if (v.vt !== 8192) {
|
|
1475
|
+
const w = whole(f);
|
|
1476
|
+
return w instanceof Promise ? w.then(temp) : temp(w);
|
|
1477
|
+
}
|
|
1478
|
+
const list = collect(parts, f);
|
|
1479
|
+
const at = (vals) => {
|
|
1480
|
+
const idx = [];
|
|
1481
|
+
for (const a of vals) {
|
|
1482
|
+
const s = scalarOf(a);
|
|
1483
|
+
if (s instanceof Promise) return s.then(() => at(vals));
|
|
1484
|
+
idx.push(indexOf(s, f.session));
|
|
1485
|
+
}
|
|
1486
|
+
v.v.offset(idx);
|
|
1487
|
+
return new ElementRef(v.v, idx);
|
|
1488
|
+
};
|
|
1489
|
+
return list instanceof Promise ? list.then(at) : at(list);
|
|
1490
|
+
};
|
|
1491
|
+
return r instanceof Promise ? r.then(go) : go(r);
|
|
1492
|
+
};
|
|
1493
|
+
}
|
|
1494
|
+
case "MemberAccess": {
|
|
1495
|
+
if (b?.kind !== "udtField") return valueTemp(x, e);
|
|
1496
|
+
const object = x.qualifier(e);
|
|
1497
|
+
const i = b.index;
|
|
1498
|
+
return (f) => {
|
|
1499
|
+
const o = object(f);
|
|
1500
|
+
const field = (v) => {
|
|
1501
|
+
if (v.vt !== 36) throw new VbaRuntimeError(13);
|
|
1502
|
+
return new FieldRef(v.v, i);
|
|
1503
|
+
};
|
|
1504
|
+
return o instanceof Promise ? o.then(field) : field(o);
|
|
1505
|
+
};
|
|
1506
|
+
}
|
|
1507
|
+
default: return valueTemp(x, e);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
// src/interp/compile/dynamic.ts
|
|
1511
|
+
const is438 = (e) => e instanceof VbaRuntimeError && e.number === 438;
|
|
1512
|
+
/** What is known about one undeclared name in one procedure. */
|
|
1513
|
+
var DynamicName = class {
|
|
1514
|
+
name;
|
|
1515
|
+
onDocument;
|
|
1516
|
+
onGlobal;
|
|
1517
|
+
/** The run in which the document object, or the global object, answered 438 for the name. */
|
|
1518
|
+
documentMiss = null;
|
|
1519
|
+
globalMiss = null;
|
|
1520
|
+
local;
|
|
1521
|
+
constructor(ctx, name, document, fallback) {
|
|
1522
|
+
this.name = name;
|
|
1523
|
+
this.onDocument = document;
|
|
1524
|
+
this.onGlobal = ctx.services.host.globalObject !== void 0;
|
|
1525
|
+
this.local = fallback ? variableRef(ctx, fallback) : null;
|
|
1526
|
+
}
|
|
1527
|
+
/** Whether to look on the document's host object in `run`. */
|
|
1528
|
+
document(run) {
|
|
1529
|
+
return this.onDocument && this.documentMiss !== run;
|
|
1530
|
+
}
|
|
1531
|
+
global(run) {
|
|
1532
|
+
return this.onGlobal && this.globalMiss !== run;
|
|
1533
|
+
}
|
|
1534
|
+
/** Whether the name is the implicit local in `run` (no host object has it). */
|
|
1535
|
+
isLocal(run) {
|
|
1536
|
+
return !this.document(run) && !this.global(run) && this.local !== null;
|
|
1537
|
+
}
|
|
1538
|
+
};
|
|
1539
|
+
var DynamicNames = class {
|
|
1540
|
+
ctx;
|
|
1541
|
+
names = /* @__PURE__ */ new Map();
|
|
1542
|
+
constructor(ctx) {
|
|
1543
|
+
this.ctx = ctx;
|
|
1544
|
+
}
|
|
1545
|
+
of(name, document, fallback) {
|
|
1546
|
+
const key = name.toLowerCase();
|
|
1547
|
+
let n = this.names.get(key);
|
|
1548
|
+
if (!n) {
|
|
1549
|
+
n = new DynamicName(this.ctx, name, document, fallback);
|
|
1550
|
+
this.names.set(key, n);
|
|
1551
|
+
}
|
|
1552
|
+
return n;
|
|
1553
|
+
}
|
|
1554
|
+
/** Reads, calls or assigns the name, with arguments (`Range("A1")`) or without. */
|
|
1555
|
+
access(n, f, access, refs, named, value) {
|
|
1556
|
+
return this.attempt(n, f, access, refs, named, value, 0);
|
|
1557
|
+
}
|
|
1558
|
+
attempt(n, f, access, refs, named, value, stage) {
|
|
1559
|
+
const host = this.ctx.services.host;
|
|
1560
|
+
if (stage === 0 && n.document(f.run) && f.me) {
|
|
1561
|
+
const r = this.tryOn(f.me, n, f, access, refs, named, value, () => n.documentMiss = f.run);
|
|
1562
|
+
if (r !== MISS) return r instanceof Promise ? r.then((v) => v === MISS ? this.attempt(n, f, access, refs, named, value, 1) : v) : r;
|
|
1563
|
+
}
|
|
1564
|
+
if (stage <= 1 && n.global(f.run) && host.globalObject) {
|
|
1565
|
+
const r = this.tryOn(host.globalObject, n, f, access, refs, named, value, () => n.globalMiss = f.run);
|
|
1566
|
+
if (r !== MISS) return r instanceof Promise ? r.then((v) => v === MISS ? this.local(n, f, access, refs, value) : v) : r;
|
|
1567
|
+
}
|
|
1568
|
+
return this.local(n, f, access, refs, value);
|
|
1569
|
+
}
|
|
1570
|
+
tryOn(o, n, f, access, refs, named, value, miss) {
|
|
1571
|
+
try {
|
|
1572
|
+
const r = member(o, n.name, access, refs, named, f.run, value);
|
|
1573
|
+
if (r instanceof Promise) return r.catch((e) => {
|
|
1574
|
+
if (!is438(e)) throw e;
|
|
1575
|
+
miss();
|
|
1576
|
+
return MISS;
|
|
1577
|
+
});
|
|
1578
|
+
return r;
|
|
1579
|
+
} catch (e) {
|
|
1580
|
+
if (!is438(e)) throw e;
|
|
1581
|
+
miss();
|
|
1582
|
+
return MISS;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
local(n, f, access, refs, value) {
|
|
1586
|
+
if (!n.local) throw new VbaRuntimeError(438, `Variable not defined (${n.name})`);
|
|
1587
|
+
const cell = n.local(f);
|
|
1588
|
+
if (access === "let" || access === "set") {
|
|
1589
|
+
if (refs.length > 0) return andThen(indexValue(cell.get(), refs.map((r) => r?.get() ?? { vt: 0 }), refs, f, access, value), (v) => v);
|
|
1590
|
+
cell.set(value ?? { vt: 0 });
|
|
1591
|
+
return { vt: 0 };
|
|
1592
|
+
}
|
|
1593
|
+
const v = cell.get();
|
|
1594
|
+
if (refs.length === 0) return v;
|
|
1595
|
+
return indexValue(v, refs.map((r) => r?.get() ?? { vt: 0 }), refs, f);
|
|
1596
|
+
}
|
|
1597
|
+
};
|
|
1598
|
+
const MISS = Object.freeze({ vt: -1 });
|
|
1599
|
+
// src/interp/compile/expressions.ts
|
|
1600
|
+
const constant = (v) => () => v;
|
|
1601
|
+
function numberValue(e) {
|
|
1602
|
+
switch (e.literalType) {
|
|
1603
|
+
case "Integer": return vInt(e.value);
|
|
1604
|
+
case "Long": return vLng(e.value);
|
|
1605
|
+
case "LongLong": return vLLng(BigInt(e.bigValue ?? Math.trunc(e.value)));
|
|
1606
|
+
case "Single": return vSng(e.value);
|
|
1607
|
+
case "Currency": return vCur(e.exact ? BigInt(e.exact.mantissa) : curFromNumber(e.value));
|
|
1608
|
+
default: return vDbl(e.value);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
/** The simple value of an operand: an object's default member (`Is` keeps the object). */
|
|
1612
|
+
function operand(ev) {
|
|
1613
|
+
return (f) => {
|
|
1614
|
+
const v = ev(f);
|
|
1615
|
+
if (v instanceof Promise) return v.then((x) => x.vt === 9 ? scalarOf(x) : x);
|
|
1616
|
+
return v.vt === 9 ? scalarOf(v) : v;
|
|
1617
|
+
};
|
|
1618
|
+
}
|
|
1619
|
+
const HANDLERS$1 = {
|
|
1620
|
+
NumberLiteral: (_x, e) => constant(numberValue(e)),
|
|
1621
|
+
StringLiteral: (_x, e) => constant(vStr(e.value)),
|
|
1622
|
+
DateLiteral: (_x, e) => constant(vDate(e.value)),
|
|
1623
|
+
BooleanLiteral: (_x, e) => constant(vBool(e.value)),
|
|
1624
|
+
NothingLiteral: () => constant(NOTHING),
|
|
1625
|
+
EmptyLiteral: () => constant(EMPTY$1),
|
|
1626
|
+
NullLiteral: () => constant(NULL),
|
|
1627
|
+
Name: (x, e) => nameValue(x, e, "get"),
|
|
1628
|
+
Me: () => (f) => f.me ? {
|
|
1629
|
+
vt: 9,
|
|
1630
|
+
v: f.me
|
|
1631
|
+
} : NOTHING,
|
|
1632
|
+
MemberAccess: (x, e) => memberValue(x, e, "get"),
|
|
1633
|
+
DictionaryAccess: (x, e) => memberValue(x, e, "get"),
|
|
1634
|
+
CallOrIndex: (x, e) => invocationValue(x, e, e.target, e.args, "get"),
|
|
1635
|
+
Paren: (x, e) => x.value(e.expression),
|
|
1636
|
+
Unary: (x, e) => {
|
|
1637
|
+
const strict = !isVariant(x.ctx.typeOf(e.operand));
|
|
1638
|
+
const op = unaryOperator(e.operator, strict);
|
|
1639
|
+
const a = operand(x.value(e.operand));
|
|
1640
|
+
return (f) => {
|
|
1641
|
+
const v = a(f);
|
|
1642
|
+
return v instanceof Promise ? v.then((y) => op(y, f.session)) : op(v, f.session);
|
|
1643
|
+
};
|
|
1644
|
+
},
|
|
1645
|
+
Binary: (x, e) => {
|
|
1646
|
+
const lt = x.ctx.typeOf(e.left);
|
|
1647
|
+
const rt = x.ctx.typeOf(e.right);
|
|
1648
|
+
const op = binaryOperator(e.operator, {
|
|
1649
|
+
strict: !isVariant(lt) && !isVariant(rt),
|
|
1650
|
+
compareText: x.ctx.compareText,
|
|
1651
|
+
typed: x.ctx.program.isTypedOperand(e.left) || x.ctx.program.isTypedOperand(e.right)
|
|
1652
|
+
});
|
|
1653
|
+
const raw = e.operator === "Is";
|
|
1654
|
+
const l = raw ? x.value(e.left) : operand(x.value(e.left));
|
|
1655
|
+
const r = raw ? x.value(e.right) : operand(x.value(e.right));
|
|
1656
|
+
return (f) => {
|
|
1657
|
+
const a = l(f);
|
|
1658
|
+
if (a instanceof Promise) return a.then((av) => binaryRest(av, r, op, f));
|
|
1659
|
+
return binaryRest(a, r, op, f);
|
|
1660
|
+
};
|
|
1661
|
+
},
|
|
1662
|
+
TypeOf: (x, e) => {
|
|
1663
|
+
const test = typeOfTest(x.ctx, e.type);
|
|
1664
|
+
const v = x.value(e.expression);
|
|
1665
|
+
return (f) => {
|
|
1666
|
+
const o = v(f);
|
|
1667
|
+
return o instanceof Promise ? o.then((y) => vBool(test(y))) : vBool(test(o));
|
|
1668
|
+
};
|
|
1669
|
+
},
|
|
1670
|
+
New: (x, e) => {
|
|
1671
|
+
const type = x.ctx.typeOf(e);
|
|
1672
|
+
return (f) => x.ctx.services.createObject(type, f.run);
|
|
1673
|
+
},
|
|
1674
|
+
AddressOf: () => () => notSupported("AddressOf"),
|
|
1675
|
+
FileNumber: () => () => notSupported("File I/O"),
|
|
1676
|
+
InvalidExpression: () => () => {
|
|
1677
|
+
throw new VbaRuntimeError(5, "Syntax error");
|
|
1678
|
+
}
|
|
1679
|
+
};
|
|
1680
|
+
function binaryRest(a, r, op, f) {
|
|
1681
|
+
const b = r(f);
|
|
1682
|
+
return b instanceof Promise ? b.then((bv) => op(a, bv, f.session)) : op(a, b, f.session);
|
|
1683
|
+
}
|
|
1684
|
+
var ExpressionCompiler = class {
|
|
1685
|
+
ctx;
|
|
1686
|
+
dynamics;
|
|
1687
|
+
constructor(ctx) {
|
|
1688
|
+
this.ctx = ctx;
|
|
1689
|
+
this.dynamics = new DynamicNames(ctx);
|
|
1690
|
+
}
|
|
1691
|
+
/** The value of an expression. */
|
|
1692
|
+
value(e) {
|
|
1693
|
+
return HANDLERS$1[e.kind](this, e);
|
|
1694
|
+
}
|
|
1695
|
+
/** The simple data value (an object's default member). */
|
|
1696
|
+
scalar(e) {
|
|
1697
|
+
return operand(this.value(e));
|
|
1698
|
+
}
|
|
1699
|
+
/** A reference to the storage an expression names, or a temporary holding its value. */
|
|
1700
|
+
ref(e) {
|
|
1701
|
+
return referenceOf(this, e);
|
|
1702
|
+
}
|
|
1703
|
+
isVariable(e) {
|
|
1704
|
+
if (e.kind !== "Name") return false;
|
|
1705
|
+
const k = this.ctx.binding(e)?.kind;
|
|
1706
|
+
return k === "variable" || k === "param" || k === "implicit" || k === "result";
|
|
1707
|
+
}
|
|
1708
|
+
invoke(node, target, args, access) {
|
|
1709
|
+
return invocationValue(this, node, target, args, access);
|
|
1710
|
+
}
|
|
1711
|
+
/** A name or member standing alone as a statement (`DoIt`, `obj.Refresh`). */
|
|
1712
|
+
statement(e) {
|
|
1713
|
+
if (e.kind === "Name") return nameValue(this, e, "call");
|
|
1714
|
+
if (e.kind === "MemberAccess" || e.kind === "DictionaryAccess") return memberValue(this, e, "call");
|
|
1715
|
+
return this.value(e);
|
|
1716
|
+
}
|
|
1717
|
+
/**
|
|
1718
|
+
* The object of a member access, seen through its static type: an object declared as an
|
|
1719
|
+
* interface the class implements is viewed through that interface, wherever it came from (an
|
|
1720
|
+
* array element, a field, a function result).
|
|
1721
|
+
*/
|
|
1722
|
+
qualifier(e) {
|
|
1723
|
+
let raw;
|
|
1724
|
+
let type;
|
|
1725
|
+
if (e.object) {
|
|
1726
|
+
raw = this.value(e.object);
|
|
1727
|
+
type = this.ctx.typeOf(e.object);
|
|
1728
|
+
} else {
|
|
1729
|
+
const block = this.ctx.withBlock(e);
|
|
1730
|
+
if (!block) return () => {
|
|
1731
|
+
throw new VbaRuntimeError(91);
|
|
1732
|
+
};
|
|
1733
|
+
const id = block.id;
|
|
1734
|
+
type = block.subjectType;
|
|
1735
|
+
raw = type.kind === "udt" ? (f) => f.withRefs[id].get() : (f) => f.with[id] ?? NOTHING;
|
|
1736
|
+
}
|
|
1737
|
+
if (type.kind !== "object" || !type.classModule) return raw;
|
|
1738
|
+
const cast = this.ctx.services.castObject(type);
|
|
1739
|
+
return (f) => {
|
|
1740
|
+
const v = raw(f);
|
|
1741
|
+
return v instanceof Promise ? v.then(cast) : cast(v);
|
|
1742
|
+
};
|
|
1743
|
+
}
|
|
1744
|
+
};
|
|
1745
|
+
// src/interp/compile/builder.ts
|
|
1746
|
+
/** A jump target: its instruction index is known once `mark` is called. */
|
|
1747
|
+
var Target = class {
|
|
1748
|
+
pc = -1;
|
|
1749
|
+
};
|
|
1750
|
+
var CodeBuilder = class {
|
|
1751
|
+
code = [];
|
|
1752
|
+
labels = /* @__PURE__ */ new Map();
|
|
1753
|
+
loops = [];
|
|
1754
|
+
/** The end of the procedure (Exit Sub/Function/Property). */
|
|
1755
|
+
end = new Target();
|
|
1756
|
+
/** The 1-based line of the statement being compiled. */
|
|
1757
|
+
line = 0;
|
|
1758
|
+
/** The 1-based column where that statement starts. */
|
|
1759
|
+
column = 0;
|
|
1760
|
+
nextLoop = 0;
|
|
1761
|
+
get pc() {
|
|
1762
|
+
return this.code.length;
|
|
1763
|
+
}
|
|
1764
|
+
emit(run) {
|
|
1765
|
+
this.code.push({
|
|
1766
|
+
run,
|
|
1767
|
+
line: this.line,
|
|
1768
|
+
column: this.column
|
|
1769
|
+
});
|
|
1770
|
+
return this.code.length - 1;
|
|
1771
|
+
}
|
|
1772
|
+
/** An instruction that evaluates an expression for its effect. */
|
|
1773
|
+
effect(ev) {
|
|
1774
|
+
return this.emit((f) => {
|
|
1775
|
+
const r = ev(f);
|
|
1776
|
+
return r instanceof Promise ? r.then(() => void 0) : void 0;
|
|
1777
|
+
});
|
|
1778
|
+
}
|
|
1779
|
+
jump(t) {
|
|
1780
|
+
return this.emit(() => t.pc);
|
|
1781
|
+
}
|
|
1782
|
+
mark(t) {
|
|
1783
|
+
t.pc = this.code.length;
|
|
1784
|
+
}
|
|
1785
|
+
label(name) {
|
|
1786
|
+
const key = name.toLowerCase();
|
|
1787
|
+
let t = this.labels.get(key);
|
|
1788
|
+
if (!t) {
|
|
1789
|
+
t = new Target();
|
|
1790
|
+
this.labels.set(key, t);
|
|
1791
|
+
}
|
|
1792
|
+
return t;
|
|
1793
|
+
}
|
|
1794
|
+
/** A new id for a loop, Select Case or With: its slot in the frame's per-block state. */
|
|
1795
|
+
block() {
|
|
1796
|
+
return this.nextLoop++;
|
|
1797
|
+
}
|
|
1798
|
+
/**
|
|
1799
|
+
* The innermost open loop of a kind (`Exit For` also leaves a For Each), with the For Each
|
|
1800
|
+
* cursors the exit leaves (that loop's own, and those of loops nested inside it).
|
|
1801
|
+
*/
|
|
1802
|
+
loop(kind) {
|
|
1803
|
+
const cursors = [];
|
|
1804
|
+
for (let i = this.loops.length - 1; i >= 0; i--) {
|
|
1805
|
+
const l = this.loops[i];
|
|
1806
|
+
if (l.cursor !== void 0) cursors.push(l.cursor);
|
|
1807
|
+
if (l.kind === kind) return {
|
|
1808
|
+
loop: l,
|
|
1809
|
+
cursors
|
|
1810
|
+
};
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
};
|
|
1814
|
+
// src/interp/compile/stores.ts
|
|
1815
|
+
/** The right-hand side as Let or Set sees it. */
|
|
1816
|
+
function rhs(x, e, kind) {
|
|
1817
|
+
const v = x.value(e);
|
|
1818
|
+
if (kind === "let") return (f) => {
|
|
1819
|
+
const r = v(f);
|
|
1820
|
+
if (r instanceof Promise) return r.then((y) => y.vt === 9 ? scalarOf(y) : y);
|
|
1821
|
+
return r.vt === 9 ? scalarOf(r) : r;
|
|
1822
|
+
};
|
|
1823
|
+
return (f) => {
|
|
1824
|
+
const r = v(f);
|
|
1825
|
+
if (r instanceof Promise) return r.then(requireObject);
|
|
1826
|
+
return requireObject(r);
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
function requireObject(v) {
|
|
1830
|
+
if (v.vt !== 9) throw new VbaRuntimeError(424);
|
|
1831
|
+
return v;
|
|
1832
|
+
}
|
|
1833
|
+
/** Stores into a reference once the value is known. */
|
|
1834
|
+
function storeRef(ref, value) {
|
|
1835
|
+
return (f) => {
|
|
1836
|
+
const v = value(f);
|
|
1837
|
+
const put = (val) => {
|
|
1838
|
+
const r = ref(f);
|
|
1839
|
+
if (r instanceof Promise) return r.then((cell) => {
|
|
1840
|
+
cell.set(val);
|
|
1841
|
+
return EMPTY$1;
|
|
1842
|
+
});
|
|
1843
|
+
r.set(val);
|
|
1844
|
+
return EMPTY$1;
|
|
1845
|
+
};
|
|
1846
|
+
return v instanceof Promise ? v.then(put) : put(v);
|
|
1847
|
+
};
|
|
1848
|
+
}
|
|
1849
|
+
function variableStore(x, sym, kind, value) {
|
|
1850
|
+
const ref = variableRef(x.ctx, sym);
|
|
1851
|
+
if (kind === "let" && sym.type.kind === "object") return (f) => {
|
|
1852
|
+
const v = value(f);
|
|
1853
|
+
const put = (val) => member(objectOf(ref(f).get()), "", "let", [], void 0, f.run, val);
|
|
1854
|
+
return v instanceof Promise ? v.then(put) : put(v);
|
|
1855
|
+
};
|
|
1856
|
+
return (f) => {
|
|
1857
|
+
const v = value(f);
|
|
1858
|
+
if (v instanceof Promise) return v.then((val) => {
|
|
1859
|
+
ref(f).set(val);
|
|
1860
|
+
return EMPTY$1;
|
|
1861
|
+
});
|
|
1862
|
+
ref(f).set(v);
|
|
1863
|
+
return EMPTY$1;
|
|
1864
|
+
};
|
|
1865
|
+
}
|
|
1866
|
+
/** Compiles `target = value` (kind `let`) or `Set target = value` (kind `set`). */
|
|
1867
|
+
function assignment(x, target, kind, source) {
|
|
1868
|
+
return assignTo(x, target, kind, rhs(x, source, kind));
|
|
1869
|
+
}
|
|
1870
|
+
/** Compiles a store of an already prepared value (a For counter, a For Each element). */
|
|
1871
|
+
function assignTo(x, target, kind, value) {
|
|
1872
|
+
const b = x.ctx.binding(target);
|
|
1873
|
+
switch (target.kind) {
|
|
1874
|
+
case "Name": switch (b?.kind) {
|
|
1875
|
+
case "variable":
|
|
1876
|
+
case "param":
|
|
1877
|
+
case "implicit":
|
|
1878
|
+
case "result": return variableStore(x, b.symbol, kind, value);
|
|
1879
|
+
case "procedure": return invocationValue(x, target, target, [], kind, value);
|
|
1880
|
+
case "dynamic": {
|
|
1881
|
+
const n = x.dynamics.of(b.name, b.document, b.fallback);
|
|
1882
|
+
return (f) => {
|
|
1883
|
+
const v = value(f);
|
|
1884
|
+
if (v instanceof Promise) return v.then((val) => x.dynamics.access(n, f, kind, [], void 0, val));
|
|
1885
|
+
return x.dynamics.access(n, f, kind, [], void 0, v);
|
|
1886
|
+
};
|
|
1887
|
+
}
|
|
1888
|
+
case "unresolved": return () => {
|
|
1889
|
+
throw new VbaRuntimeError(b.number);
|
|
1890
|
+
};
|
|
1891
|
+
default: return () => {
|
|
1892
|
+
throw new VbaRuntimeError(5, "Invalid assignment target");
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1895
|
+
case "CallOrIndex": {
|
|
1896
|
+
const inv = x.ctx.invocation(target);
|
|
1897
|
+
if (inv?.kind === "library" || inv?.kind === "declare") return () => {
|
|
1898
|
+
throw new VbaRuntimeError(5, "Function call on left-hand side of assignment");
|
|
1899
|
+
};
|
|
1900
|
+
return invocationValue(x, target, target.target, target.args, kind, value);
|
|
1901
|
+
}
|
|
1902
|
+
case "MemberAccess":
|
|
1903
|
+
case "DictionaryAccess": return memberStore(x, target, kind, value);
|
|
1904
|
+
default: return storeRef(x.ref(target), value);
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
function memberStore(x, target, kind, value) {
|
|
1908
|
+
const b = x.ctx.binding(target);
|
|
1909
|
+
switch (b?.kind) {
|
|
1910
|
+
case "variable": return variableStore(x, b.symbol, kind, value);
|
|
1911
|
+
case "udtField": return storeRef(x.ref(target), value);
|
|
1912
|
+
case "procedure": return invocationValue(x, target, target, [], kind, value);
|
|
1913
|
+
case "member":
|
|
1914
|
+
case "errMember":
|
|
1915
|
+
case "debugMember": {
|
|
1916
|
+
const object = x.qualifier(target);
|
|
1917
|
+
const dictionary = target.kind === "DictionaryAccess" || b.kind === "member" && b.dictionary;
|
|
1918
|
+
const name = dictionary ? "" : b.name;
|
|
1919
|
+
const key = target.kind === "DictionaryAccess" ? target.key.name : b.name;
|
|
1920
|
+
return (f) => {
|
|
1921
|
+
const o = object(f);
|
|
1922
|
+
const put = (ov) => {
|
|
1923
|
+
const v = value(f);
|
|
1924
|
+
const refs = dictionary ? [new TempCell(vStr(key))] : [];
|
|
1925
|
+
const go = (val) => member(objectOf(ov), name, kind, refs, void 0, f.run, val);
|
|
1926
|
+
return v instanceof Promise ? v.then(go) : go(v);
|
|
1927
|
+
};
|
|
1928
|
+
return o instanceof Promise ? o.then(put) : put(o);
|
|
1929
|
+
};
|
|
1930
|
+
}
|
|
1931
|
+
default: return () => {
|
|
1932
|
+
throw new VbaRuntimeError(b?.kind === "unresolved" ? b.number : 5);
|
|
1933
|
+
};
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
// src/interp/compile/for-each.ts
|
|
1937
|
+
function forEachInit(c, s, id) {
|
|
1938
|
+
const group = c.x.value(s.collection);
|
|
1939
|
+
c.at(s);
|
|
1940
|
+
c.b.emit((f) => {
|
|
1941
|
+
closeCursor(f, id);
|
|
1942
|
+
const v = group(f);
|
|
1943
|
+
const begin = (g) => {
|
|
1944
|
+
try {
|
|
1945
|
+
f.loops[id] = new ForEachCursor(g);
|
|
1946
|
+
} catch (e) {
|
|
1947
|
+
f.loops[id] = ForEachCursor.broken();
|
|
1948
|
+
throw e;
|
|
1949
|
+
}
|
|
1950
|
+
};
|
|
1951
|
+
return v instanceof Promise ? v.then(begin) : begin(v);
|
|
1952
|
+
});
|
|
1953
|
+
}
|
|
1954
|
+
function forEachNext(c, s, id, exit) {
|
|
1955
|
+
const current = (f) => f.loops[id].current;
|
|
1956
|
+
const asObject = assignTo(c.x, s.element, "set", current);
|
|
1957
|
+
const asValue = assignTo(c.x, s.element, "let", current);
|
|
1958
|
+
const store = (f, cursor) => {
|
|
1959
|
+
if (cursor.broken) return void 0;
|
|
1960
|
+
const r = (cursor.current.vt === 9 ? asObject : asValue)(f);
|
|
1961
|
+
return r instanceof Promise ? r.then(() => void 0) : void 0;
|
|
1962
|
+
};
|
|
1963
|
+
c.b.emit((f) => {
|
|
1964
|
+
const cursor = f.loops[id];
|
|
1965
|
+
if (!cursor) return exit.pc;
|
|
1966
|
+
const r = cursor.next();
|
|
1967
|
+
const step = (more) => more ? store(f, cursor) : exit.pc;
|
|
1968
|
+
return r instanceof Promise ? r.then(step) : step(r);
|
|
1969
|
+
});
|
|
1970
|
+
}
|
|
1971
|
+
// src/interp/compile/for-loop.ts
|
|
1972
|
+
var ForState = class {
|
|
1973
|
+
end;
|
|
1974
|
+
step;
|
|
1975
|
+
/** The value being stored into the counter. */
|
|
1976
|
+
value;
|
|
1977
|
+
up;
|
|
1978
|
+
endNum;
|
|
1979
|
+
stepNum;
|
|
1980
|
+
constructor(start, end, step, s) {
|
|
1981
|
+
this.end = end;
|
|
1982
|
+
this.step = step;
|
|
1983
|
+
this.value = start;
|
|
1984
|
+
this.up = toDbl(step, s) >= 0;
|
|
1985
|
+
this.endNum = end.vt === 2 || end.vt === 3 || end.vt === 5 ? end.v : null;
|
|
1986
|
+
this.stepNum = step.vt === 2 || step.vt === 3 ? step.v : null;
|
|
1987
|
+
}
|
|
1988
|
+
/** True once the counter has passed the end. */
|
|
1989
|
+
done(v, s) {
|
|
1990
|
+
const c = cmp(v, this.end, 0, true, s);
|
|
1991
|
+
if (c === null) return true;
|
|
1992
|
+
return this.up ? c > 0 : c < 0;
|
|
1993
|
+
}
|
|
1994
|
+
};
|
|
1995
|
+
const ONE = () => vInt(1);
|
|
1996
|
+
function readBack(counter, f, then) {
|
|
1997
|
+
const v = counter(f);
|
|
1998
|
+
return v instanceof Promise ? v.then(then) : then(v);
|
|
1999
|
+
}
|
|
2000
|
+
function forInit(c, s, id, exit) {
|
|
2001
|
+
const parts = [
|
|
2002
|
+
c.x.scalar(s.start),
|
|
2003
|
+
c.x.scalar(s.end),
|
|
2004
|
+
s.step ? c.x.scalar(s.step) : ONE
|
|
2005
|
+
];
|
|
2006
|
+
const counter = c.x.value(s.counter);
|
|
2007
|
+
const store = assignTo(c.x, s.counter, "let", (f) => f.loops[id].value);
|
|
2008
|
+
c.at(s);
|
|
2009
|
+
c.b.emit((f) => {
|
|
2010
|
+
const list = collect(parts, f);
|
|
2011
|
+
const begin = (vals) => {
|
|
2012
|
+
const st = new ForState(vals[0], vals[1], vals[2], f.session);
|
|
2013
|
+
f.loops[id] = st;
|
|
2014
|
+
const check = (v) => st.done(v, f.session) ? exit.pc : void 0;
|
|
2015
|
+
const r = store(f);
|
|
2016
|
+
return r instanceof Promise ? r.then(() => readBack(counter, f, check)) : readBack(counter, f, check);
|
|
2017
|
+
};
|
|
2018
|
+
return list instanceof Promise ? list.then(begin) : begin(list);
|
|
2019
|
+
});
|
|
2020
|
+
}
|
|
2021
|
+
function forNext(c, s, id, body) {
|
|
2022
|
+
const counter = c.x.value(s.counter);
|
|
2023
|
+
const strict = !isVariant(c.x.ctx.typeOf(s.counter));
|
|
2024
|
+
const store = assignTo(c.x, s.counter, "let", (f) => f.loops[id].value);
|
|
2025
|
+
const b = c.x.ctx.binding(s.counter);
|
|
2026
|
+
const local = s.counter.kind === "Name" && (b?.kind === "variable" || b?.kind === "implicit" || b?.kind === "param") ? variableRef(c.x.ctx, b.symbol) : null;
|
|
2027
|
+
const slow = (f, st, cur) => {
|
|
2028
|
+
st.value = add(cur, st.step, strict, f.session);
|
|
2029
|
+
const check = (v) => st.done(v, f.session) ? void 0 : body.pc;
|
|
2030
|
+
const r = store(f);
|
|
2031
|
+
return r instanceof Promise ? r.then(() => readBack(counter, f, check)) : readBack(counter, f, check);
|
|
2032
|
+
};
|
|
2033
|
+
c.b.emit((f) => {
|
|
2034
|
+
const st = f.loops[id];
|
|
2035
|
+
if (local && st.stepNum !== null && st.endNum !== null) {
|
|
2036
|
+
const cell = local(f);
|
|
2037
|
+
const cur = cell.get();
|
|
2038
|
+
if (cur.vt === 3 || cur.vt === 2) {
|
|
2039
|
+
const next = cur.v + st.stepNum;
|
|
2040
|
+
const long = cur.vt === 3 || st.step.vt === 3;
|
|
2041
|
+
if (long ? next >= -0x80000000 && next <= 0x7fffffff : next >= -32768 && next <= 32767) {
|
|
2042
|
+
cell.set(long ? vLng(next) : vInt(next));
|
|
2043
|
+
const now = cell.get();
|
|
2044
|
+
if (now.vt === 2 || now.vt === 3) return (st.up ? now.v > st.endNum : now.v < st.endNum) ? void 0 : body.pc;
|
|
2045
|
+
return st.done(now, f.session) ? void 0 : body.pc;
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
return slow(f, st, cur);
|
|
2049
|
+
}
|
|
2050
|
+
const cur = counter(f);
|
|
2051
|
+
return cur instanceof Promise ? cur.then((v) => slow(f, st, v)) : slow(f, st, cur);
|
|
2052
|
+
});
|
|
2053
|
+
}
|
|
2054
|
+
// src/interp/compile/truth.ts
|
|
2055
|
+
const isTrue = (v, s) => v.vt === 11 ? v.v : v.vt === 1 ? false : toBool(v, s);
|
|
2056
|
+
/** A condition: `scalar` must already give the simple value. */
|
|
2057
|
+
function condition(scalar) {
|
|
2058
|
+
return (f) => {
|
|
2059
|
+
const v = scalar(f);
|
|
2060
|
+
return v instanceof Promise ? v.then((y) => isTrue(y, f.session)) : isTrue(v, f.session);
|
|
2061
|
+
};
|
|
2062
|
+
}
|
|
2063
|
+
// src/interp/compile/control.ts
|
|
2064
|
+
/** Emits `if not cond goto target`. */
|
|
2065
|
+
function jumpUnless(b, cond, target) {
|
|
2066
|
+
b.emit((f) => {
|
|
2067
|
+
const r = cond(f);
|
|
2068
|
+
return r instanceof Promise ? r.then((ok) => ok ? void 0 : target.pc) : r ? void 0 : target.pc;
|
|
2069
|
+
});
|
|
2070
|
+
}
|
|
2071
|
+
function jumpIf(b, cond, target) {
|
|
2072
|
+
b.emit((f) => {
|
|
2073
|
+
const r = cond(f);
|
|
2074
|
+
return r instanceof Promise ? r.then((ok) => ok ? target.pc : void 0) : r ? target.pc : void 0;
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
const COMPARE = {
|
|
2078
|
+
"=": eq,
|
|
2079
|
+
"<>": ne,
|
|
2080
|
+
"<": lt,
|
|
2081
|
+
">": gt,
|
|
2082
|
+
"<=": le,
|
|
2083
|
+
">=": ge
|
|
2084
|
+
};
|
|
2085
|
+
const CONTROL = {
|
|
2086
|
+
If: (c, s) => {
|
|
2087
|
+
const end = new Target();
|
|
2088
|
+
for (const branch of s.branches) {
|
|
2089
|
+
const next = new Target();
|
|
2090
|
+
c.at(branch);
|
|
2091
|
+
jumpUnless(c.b, condition(c.x.scalar(branch.condition)), next);
|
|
2092
|
+
c.block(branch.body);
|
|
2093
|
+
c.b.jump(end);
|
|
2094
|
+
c.b.mark(next);
|
|
2095
|
+
}
|
|
2096
|
+
if (s.elseBody) c.block(s.elseBody);
|
|
2097
|
+
c.b.mark(end);
|
|
2098
|
+
},
|
|
2099
|
+
SelectCase: (c, s) => {
|
|
2100
|
+
const id = c.b.block();
|
|
2101
|
+
const subject = c.x.scalar(s.subject);
|
|
2102
|
+
const subjectTyped = c.x.ctx.program.isTypedOperand(s.subject);
|
|
2103
|
+
const evaluate = c.b.effect((f) => {
|
|
2104
|
+
const v = subject(f);
|
|
2105
|
+
if (v instanceof Promise) return v.then((y) => {
|
|
2106
|
+
f.loops[id] = y;
|
|
2107
|
+
return y;
|
|
2108
|
+
});
|
|
2109
|
+
f.loops[id] = v;
|
|
2110
|
+
return v;
|
|
2111
|
+
});
|
|
2112
|
+
const end = new Target();
|
|
2113
|
+
for (const clause of s.cases) {
|
|
2114
|
+
const next = new Target();
|
|
2115
|
+
c.at(clause);
|
|
2116
|
+
const tests = clause.tests.map((t) => caseTest(c, t, id, subjectTyped));
|
|
2117
|
+
jumpUnless(c.b, anyOf(tests), next);
|
|
2118
|
+
c.block(clause.body);
|
|
2119
|
+
c.b.jump(end);
|
|
2120
|
+
c.b.mark(next);
|
|
2121
|
+
}
|
|
2122
|
+
if (s.elseBody) c.block(s.elseBody);
|
|
2123
|
+
c.b.mark(end);
|
|
2124
|
+
c.b.code[evaluate].skip = end;
|
|
2125
|
+
},
|
|
2126
|
+
For: (c, s) => {
|
|
2127
|
+
const exit = new Target();
|
|
2128
|
+
const body = new Target();
|
|
2129
|
+
const id = c.b.block();
|
|
2130
|
+
forInit(c, s, id, exit);
|
|
2131
|
+
c.b.mark(body);
|
|
2132
|
+
c.b.loops.push({
|
|
2133
|
+
kind: "For",
|
|
2134
|
+
exit
|
|
2135
|
+
});
|
|
2136
|
+
c.block(s.body);
|
|
2137
|
+
c.b.loops.pop();
|
|
2138
|
+
c.at(s);
|
|
2139
|
+
forNext(c, s, id, body);
|
|
2140
|
+
c.b.mark(exit);
|
|
2141
|
+
},
|
|
2142
|
+
ForEach: (c, s) => {
|
|
2143
|
+
const exit = new Target();
|
|
2144
|
+
const fetch = new Target();
|
|
2145
|
+
const id = c.b.block();
|
|
2146
|
+
forEachInit(c, s, id);
|
|
2147
|
+
c.b.mark(fetch);
|
|
2148
|
+
forEachNext(c, s, id, exit);
|
|
2149
|
+
c.b.loops.push({
|
|
2150
|
+
kind: "For",
|
|
2151
|
+
exit,
|
|
2152
|
+
cursor: id
|
|
2153
|
+
});
|
|
2154
|
+
c.block(s.body);
|
|
2155
|
+
c.b.loops.pop();
|
|
2156
|
+
c.b.jump(fetch);
|
|
2157
|
+
c.b.mark(exit);
|
|
2158
|
+
c.b.emit((f) => {
|
|
2159
|
+
closeCursor(f, id);
|
|
2160
|
+
});
|
|
2161
|
+
},
|
|
2162
|
+
DoLoop: (c, s) => {
|
|
2163
|
+
const top = new Target();
|
|
2164
|
+
const exit = new Target();
|
|
2165
|
+
const cond = s.condition;
|
|
2166
|
+
const test = cond ? condition(c.x.scalar(cond.expression)) : null;
|
|
2167
|
+
c.b.mark(top);
|
|
2168
|
+
if (cond && test && cond.position === "pre") {
|
|
2169
|
+
c.at(cond);
|
|
2170
|
+
if (cond.mode === "While") jumpUnless(c.b, test, exit);
|
|
2171
|
+
else jumpIf(c.b, test, exit);
|
|
2172
|
+
}
|
|
2173
|
+
c.b.loops.push({
|
|
2174
|
+
kind: "Do",
|
|
2175
|
+
exit
|
|
2176
|
+
});
|
|
2177
|
+
c.block(s.body);
|
|
2178
|
+
c.b.loops.pop();
|
|
2179
|
+
if (cond && test && cond.position === "post") {
|
|
2180
|
+
c.at(cond);
|
|
2181
|
+
if (cond.mode === "While") jumpIf(c.b, test, top);
|
|
2182
|
+
else jumpUnless(c.b, test, top);
|
|
2183
|
+
} else c.b.jump(top);
|
|
2184
|
+
c.b.mark(exit);
|
|
2185
|
+
},
|
|
2186
|
+
While: (c, s) => {
|
|
2187
|
+
const top = new Target();
|
|
2188
|
+
const exit = new Target();
|
|
2189
|
+
c.b.mark(top);
|
|
2190
|
+
jumpUnless(c.b, condition(c.x.scalar(s.condition)), exit);
|
|
2191
|
+
c.block(s.body);
|
|
2192
|
+
c.b.jump(top);
|
|
2193
|
+
c.b.mark(exit);
|
|
2194
|
+
},
|
|
2195
|
+
With: (c, s) => {
|
|
2196
|
+
const block = c.x.ctx.procedure?.withBlocks.find((w) => w.statement === s);
|
|
2197
|
+
const id = block?.id ?? -1;
|
|
2198
|
+
if (block?.subjectType.kind === "udt") {
|
|
2199
|
+
const storage = c.x.ref(s.object);
|
|
2200
|
+
c.b.effect((f) => {
|
|
2201
|
+
const r = storage(f);
|
|
2202
|
+
const keep = (ref) => {
|
|
2203
|
+
f.withRefs[id] = ref;
|
|
2204
|
+
return EMPTY$1;
|
|
2205
|
+
};
|
|
2206
|
+
return r instanceof Promise ? r.then(keep) : keep(r);
|
|
2207
|
+
});
|
|
2208
|
+
c.block(s.body);
|
|
2209
|
+
return;
|
|
2210
|
+
}
|
|
2211
|
+
const subject = c.x.value(s.object);
|
|
2212
|
+
c.b.effect((f) => {
|
|
2213
|
+
const v = subject(f);
|
|
2214
|
+
const keep = (y) => {
|
|
2215
|
+
retain(y);
|
|
2216
|
+
const old = f.with[id];
|
|
2217
|
+
f.with[id] = y;
|
|
2218
|
+
if (old) release(old);
|
|
2219
|
+
return y;
|
|
2220
|
+
};
|
|
2221
|
+
return v instanceof Promise ? v.then(keep) : keep(v);
|
|
2222
|
+
});
|
|
2223
|
+
c.block(s.body);
|
|
2224
|
+
c.b.emit((f) => {
|
|
2225
|
+
const v = f.with[id];
|
|
2226
|
+
f.with[id] = EMPTY$1;
|
|
2227
|
+
if (v) release(v);
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
};
|
|
2231
|
+
function anyOf(tests) {
|
|
2232
|
+
return (f) => {
|
|
2233
|
+
for (let i = 0; i < tests.length; i++) {
|
|
2234
|
+
const r = tests[i](f);
|
|
2235
|
+
if (r instanceof Promise) return r.then((ok) => ok ? true : anyOf(tests.slice(i + 1))(f));
|
|
2236
|
+
if (r) return true;
|
|
2237
|
+
}
|
|
2238
|
+
return false;
|
|
2239
|
+
};
|
|
2240
|
+
}
|
|
2241
|
+
function caseTest(c, t, id, subjectTyped) {
|
|
2242
|
+
const compare = c.x.ctx.compareText ? 1 : 0;
|
|
2243
|
+
const test = (e, op) => {
|
|
2244
|
+
const v = c.x.scalar(e);
|
|
2245
|
+
const typed = subjectTyped || c.x.ctx.program.isTypedOperand(e);
|
|
2246
|
+
return (f) => {
|
|
2247
|
+
const r = v(f);
|
|
2248
|
+
const check = (y) => isTrue(op(f.loops[id], y, compare, typed, f.session), f.session);
|
|
2249
|
+
return r instanceof Promise ? r.then(check) : check(r);
|
|
2250
|
+
};
|
|
2251
|
+
};
|
|
2252
|
+
switch (t.kind) {
|
|
2253
|
+
case "CaseValue": return test(t.value, eq);
|
|
2254
|
+
case "CaseIs": return test(t.value, COMPARE[t.operator]);
|
|
2255
|
+
case "CaseRange": {
|
|
2256
|
+
const from = test(t.from, ge);
|
|
2257
|
+
const to = test(t.to, le);
|
|
2258
|
+
return (f) => {
|
|
2259
|
+
const a = from(f);
|
|
2260
|
+
if (a instanceof Promise) return a.then((ok) => ok ? to(f) : false);
|
|
2261
|
+
return a ? to(f) : false;
|
|
2262
|
+
};
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
// src/interp/compile/flow.ts
|
|
2267
|
+
/**
|
|
2268
|
+
* Pushes a GoSub return point. Recursive GoSub runs out of stack space (error 28) at the call
|
|
2269
|
+
* depth limit; the procedure's own On Error cannot trap it (its callers can), as in Office.
|
|
2270
|
+
*/
|
|
2271
|
+
function pushReturn(f, back) {
|
|
2272
|
+
if (f.gosub.length >= f.run.guard.limits.maxCallDepth) {
|
|
2273
|
+
const e = new VbaRuntimeError(28);
|
|
2274
|
+
f.run.untrappable.set(e, f);
|
|
2275
|
+
throw e;
|
|
2276
|
+
}
|
|
2277
|
+
f.gosub.push(back);
|
|
2278
|
+
}
|
|
2279
|
+
const FLOW = {
|
|
2280
|
+
Label: (c, s) => {
|
|
2281
|
+
c.b.mark(c.b.label(s.label.name));
|
|
2282
|
+
if (s.label.isLineNumber) {
|
|
2283
|
+
const n = Number(s.label.name);
|
|
2284
|
+
c.b.emit((f) => {
|
|
2285
|
+
f.line = n;
|
|
2286
|
+
});
|
|
2287
|
+
}
|
|
2288
|
+
},
|
|
2289
|
+
GoTo: (c, s) => {
|
|
2290
|
+
const t = c.b.label(s.label.name);
|
|
2291
|
+
if (s.kind === "GoSub") {
|
|
2292
|
+
const back = c.b.pc + 1;
|
|
2293
|
+
c.b.emit((f) => {
|
|
2294
|
+
pushReturn(f, back);
|
|
2295
|
+
return t.pc;
|
|
2296
|
+
});
|
|
2297
|
+
} else c.b.jump(t);
|
|
2298
|
+
},
|
|
2299
|
+
GoSub: (c, s) => FLOW.GoTo(c, s),
|
|
2300
|
+
Return: (c) => {
|
|
2301
|
+
c.b.emit((f) => {
|
|
2302
|
+
const back = f.gosub.pop();
|
|
2303
|
+
if (back === void 0) throw new VbaRuntimeError(3);
|
|
2304
|
+
return back;
|
|
2305
|
+
});
|
|
2306
|
+
},
|
|
2307
|
+
OnGoTo: (c, s) => {
|
|
2308
|
+
const selector = c.x.scalar(s.selector);
|
|
2309
|
+
const targets = s.labels.map((l) => c.b.label(l.name));
|
|
2310
|
+
const gosub = s.gosub;
|
|
2311
|
+
const back = c.b.pc + 1;
|
|
2312
|
+
c.b.emit((f) => {
|
|
2313
|
+
const pick = (v) => {
|
|
2314
|
+
const n = toLng(scalar(v), f.session);
|
|
2315
|
+
if (n < 0 || n > 255) throw new VbaRuntimeError(5);
|
|
2316
|
+
const t = targets[n - 1];
|
|
2317
|
+
if (n === 0 || !t) return void 0;
|
|
2318
|
+
if (gosub) pushReturn(f, back);
|
|
2319
|
+
return t.pc;
|
|
2320
|
+
};
|
|
2321
|
+
const v = selector(f);
|
|
2322
|
+
return v instanceof Promise ? v.then(pick) : pick(v);
|
|
2323
|
+
});
|
|
2324
|
+
},
|
|
2325
|
+
Exit: (c, s) => {
|
|
2326
|
+
if (s.target === "For" || s.target === "Do") {
|
|
2327
|
+
const found = c.b.loop(s.target);
|
|
2328
|
+
if (!found) return;
|
|
2329
|
+
const { loop, cursors } = found;
|
|
2330
|
+
if (cursors.length > 0) c.b.emit((f) => {
|
|
2331
|
+
for (const id of cursors) closeCursor(f, id);
|
|
2332
|
+
return loop.exit.pc;
|
|
2333
|
+
});
|
|
2334
|
+
else c.b.jump(loop.exit);
|
|
2335
|
+
return;
|
|
2336
|
+
}
|
|
2337
|
+
const end = c.b.end;
|
|
2338
|
+
c.b.emit((f) => {
|
|
2339
|
+
if (f.handler.handling) f.session.err.clear();
|
|
2340
|
+
return end.pc;
|
|
2341
|
+
});
|
|
2342
|
+
},
|
|
2343
|
+
End: (c) => {
|
|
2344
|
+
c.b.emit(() => {
|
|
2345
|
+
throw new VbaAbort("end", "End statement");
|
|
2346
|
+
});
|
|
2347
|
+
},
|
|
2348
|
+
Stop: (c) => {
|
|
2349
|
+
c.b.emit(() => {
|
|
2350
|
+
throw new VbaStop();
|
|
2351
|
+
});
|
|
2352
|
+
},
|
|
2353
|
+
OnError: (c, s) => {
|
|
2354
|
+
switch (s.action) {
|
|
2355
|
+
case "goto": {
|
|
2356
|
+
const label = s.label;
|
|
2357
|
+
if (!label || label.isLineNumber && label.name === "0") {
|
|
2358
|
+
c.b.emit((f) => {
|
|
2359
|
+
f.handler.set("goto0", -1, f.session.err);
|
|
2360
|
+
});
|
|
2361
|
+
return;
|
|
2362
|
+
}
|
|
2363
|
+
const t = c.b.label(label.name);
|
|
2364
|
+
c.b.emit((f) => {
|
|
2365
|
+
f.handler.set("label", t.pc, f.session.err);
|
|
2366
|
+
});
|
|
2367
|
+
return;
|
|
2368
|
+
}
|
|
2369
|
+
case "resumeNext":
|
|
2370
|
+
c.b.emit((f) => {
|
|
2371
|
+
f.handler.set("next", -1, f.session.err);
|
|
2372
|
+
});
|
|
2373
|
+
return;
|
|
2374
|
+
case "disable":
|
|
2375
|
+
c.b.emit((f) => {
|
|
2376
|
+
f.handler.set("goto0", -1, f.session.err);
|
|
2377
|
+
});
|
|
2378
|
+
return;
|
|
2379
|
+
case "clear":
|
|
2380
|
+
c.b.emit((f) => {
|
|
2381
|
+
f.handler.reset(f.session.err);
|
|
2382
|
+
});
|
|
2383
|
+
return;
|
|
2384
|
+
}
|
|
2385
|
+
},
|
|
2386
|
+
Resume: (c, s) => {
|
|
2387
|
+
const t = s.action === "label" && s.label ? c.b.label(s.label.name) : null;
|
|
2388
|
+
const kind = s.action;
|
|
2389
|
+
c.b.emit((f) => {
|
|
2390
|
+
const pc = f.handler.resume(kind, t ? t.pc : -1, f.session.err, (at) => nextOf(f.code.code, at));
|
|
2391
|
+
f.erl = 0;
|
|
2392
|
+
return pc;
|
|
2393
|
+
});
|
|
2394
|
+
}
|
|
2395
|
+
};
|
|
2396
|
+
// src/interp/compile/simple.ts
|
|
2397
|
+
const unsupported = (what) => (c) => {
|
|
2398
|
+
c.b.emit(() => notSupported(what));
|
|
2399
|
+
};
|
|
2400
|
+
function redimClause(c, clause, preserve) {
|
|
2401
|
+
const ref = c.x.ref(clause.target);
|
|
2402
|
+
const base = c.x.ctx.module.options.base;
|
|
2403
|
+
const bounds = [];
|
|
2404
|
+
for (const b of clause.bounds) {
|
|
2405
|
+
bounds.push(b.lower ? c.x.scalar(b.lower) : () => ({
|
|
2406
|
+
vt: 3,
|
|
2407
|
+
v: base
|
|
2408
|
+
}));
|
|
2409
|
+
bounds.push(c.x.scalar(b.upper));
|
|
2410
|
+
}
|
|
2411
|
+
const t = c.x.ctx.typeOf(clause.target);
|
|
2412
|
+
const element = t.kind === "array" ? t.element : { kind: "variant" };
|
|
2413
|
+
const vt = elementVt(element);
|
|
2414
|
+
const init = c.x.ctx.cells.elementInit(element);
|
|
2415
|
+
c.b.emit((f) => {
|
|
2416
|
+
const list = collect(bounds, f);
|
|
2417
|
+
const apply = (vals) => {
|
|
2418
|
+
const dims = [];
|
|
2419
|
+
for (let i = 0; i < vals.length; i += 2) dims.push({
|
|
2420
|
+
lo: toLng(scalar(vals[i]), f.session),
|
|
2421
|
+
hi: toLng(scalar(vals[i + 1]), f.session)
|
|
2422
|
+
});
|
|
2423
|
+
const r = ref(f);
|
|
2424
|
+
const put = (cell) => {
|
|
2425
|
+
const cur = cell.get();
|
|
2426
|
+
if (cur.vt === 8192 && (preserve || cell instanceof ArrayCell)) cur.v.redim(dims, preserve, !(cell instanceof ArrayCell));
|
|
2427
|
+
else {
|
|
2428
|
+
const a = new VbaArray(cur.vt === 8192 ? cur.v.elemVt : vt, dims, {
|
|
2429
|
+
env: f.session,
|
|
2430
|
+
...init ? { init } : {}
|
|
2431
|
+
});
|
|
2432
|
+
if (cell instanceof VariantCell) cell.value = vArr(a);
|
|
2433
|
+
else cell.set(vArr(a));
|
|
2434
|
+
}
|
|
2435
|
+
};
|
|
2436
|
+
return r instanceof Promise ? r.then(put) : put(r);
|
|
2437
|
+
};
|
|
2438
|
+
return list instanceof Promise ? list.then(apply) : apply(list);
|
|
2439
|
+
});
|
|
2440
|
+
}
|
|
2441
|
+
const SIMPLE = {
|
|
2442
|
+
Assignment: (c, s) => {
|
|
2443
|
+
c.b.effect(assignment(c.x, s.target, s.assignment === "Set" ? "set" : "let", s.value));
|
|
2444
|
+
},
|
|
2445
|
+
Call: (c, s) => {
|
|
2446
|
+
c.b.effect(c.x.invoke(s, s.callee, s.args, "call"));
|
|
2447
|
+
},
|
|
2448
|
+
ReDim: (c, s) => {
|
|
2449
|
+
for (const clause of s.clauses) redimClause(c, clause, s.preserve);
|
|
2450
|
+
},
|
|
2451
|
+
Erase: (c, s) => {
|
|
2452
|
+
for (const target of s.targets) {
|
|
2453
|
+
const ref = c.x.ref(target);
|
|
2454
|
+
c.b.emit((f) => {
|
|
2455
|
+
const erase = (cell) => {
|
|
2456
|
+
const v = cell.get();
|
|
2457
|
+
if (v.vt !== 8192) throw new VbaRuntimeError(13);
|
|
2458
|
+
v.v.erase();
|
|
2459
|
+
};
|
|
2460
|
+
const r = ref(f);
|
|
2461
|
+
return r instanceof Promise ? r.then(erase) : erase(r);
|
|
2462
|
+
});
|
|
2463
|
+
}
|
|
2464
|
+
},
|
|
2465
|
+
RaiseEvent: (c, s) => {
|
|
2466
|
+
const name = s.name.name;
|
|
2467
|
+
const parts = s.args.map((a) => {
|
|
2468
|
+
if (!a.value) return () => new TempCell(MISSING);
|
|
2469
|
+
return c.x.ref(a.value);
|
|
2470
|
+
});
|
|
2471
|
+
c.b.emit((f) => {
|
|
2472
|
+
const instance = f.instance;
|
|
2473
|
+
if (!instance) throw new VbaRuntimeError(91);
|
|
2474
|
+
const list = collect(parts, f);
|
|
2475
|
+
const raise = (refs) => {
|
|
2476
|
+
const r = instance.raise(name, refs);
|
|
2477
|
+
return r instanceof Promise ? r.then(() => void 0) : void 0;
|
|
2478
|
+
};
|
|
2479
|
+
return list instanceof Promise ? list.then(raise) : raise(list);
|
|
2480
|
+
});
|
|
2481
|
+
},
|
|
2482
|
+
MidAssignment: (c, s) => {
|
|
2483
|
+
if (s.function === "MidB") return unsupported("MidB statement")(c);
|
|
2484
|
+
const ref = c.x.ref(s.target);
|
|
2485
|
+
const parts = [
|
|
2486
|
+
c.x.scalar(s.start),
|
|
2487
|
+
s.length ? c.x.scalar(s.length) : () => MISSING,
|
|
2488
|
+
c.x.scalar(s.value)
|
|
2489
|
+
];
|
|
2490
|
+
c.b.emit((f) => {
|
|
2491
|
+
const list = collect(parts, f);
|
|
2492
|
+
const apply = (vals) => {
|
|
2493
|
+
const r = ref(f);
|
|
2494
|
+
const put = (cell) => {
|
|
2495
|
+
const [start, length, value] = vals;
|
|
2496
|
+
cell.set(midAssign(cell.get(), start, length, value, f.session));
|
|
2497
|
+
};
|
|
2498
|
+
return r instanceof Promise ? r.then(put) : put(r);
|
|
2499
|
+
};
|
|
2500
|
+
return list instanceof Promise ? list.then(apply) : apply(list);
|
|
2501
|
+
});
|
|
2502
|
+
},
|
|
2503
|
+
LSet: (c, s) => padStatement(c, s.target, s.value, "left"),
|
|
2504
|
+
RSet: (c, s) => padStatement(c, s.target, s.value, "right"),
|
|
2505
|
+
Name: unsupported("Name (renaming files)"),
|
|
2506
|
+
Error: (c, s) => {
|
|
2507
|
+
const n = c.x.scalar(s.number);
|
|
2508
|
+
c.b.emit((f) => {
|
|
2509
|
+
const raise = (v) => {
|
|
2510
|
+
throw new VbaRuntimeError(toLng(v, f.session));
|
|
2511
|
+
};
|
|
2512
|
+
const v = n(f);
|
|
2513
|
+
return v instanceof Promise ? v.then(raise) : raise(v);
|
|
2514
|
+
});
|
|
2515
|
+
},
|
|
2516
|
+
Print: (c, s) => {
|
|
2517
|
+
if (s.channel === "file") return unsupported("File I/O (Print #)")(c);
|
|
2518
|
+
const parts = s.items.map((item) => {
|
|
2519
|
+
const value = item.value ? c.x.scalar(item.value) : null;
|
|
2520
|
+
const sep = item.separator ? [{ sep: item.separator }] : [];
|
|
2521
|
+
return (f) => {
|
|
2522
|
+
const make = (v) => {
|
|
2523
|
+
if (item.form === "spc") return [{ spc: v ? toLng(v, f.session) : 0 }, ...sep];
|
|
2524
|
+
if (item.form === "tab") return [{ tab: v ? toLng(v, f.session) : void 0 }, ...sep];
|
|
2525
|
+
return v ? [{ value: v }, ...sep] : sep;
|
|
2526
|
+
};
|
|
2527
|
+
if (!value) return make(null);
|
|
2528
|
+
const v = value(f);
|
|
2529
|
+
return v instanceof Promise ? v.then(make) : make(v);
|
|
2530
|
+
};
|
|
2531
|
+
});
|
|
2532
|
+
c.b.emit((f) => {
|
|
2533
|
+
const list = collect(parts, f);
|
|
2534
|
+
const print = (groups) => {
|
|
2535
|
+
debugPrint(f.session, groups.flat());
|
|
2536
|
+
};
|
|
2537
|
+
return list instanceof Promise ? list.then(print) : print(list);
|
|
2538
|
+
});
|
|
2539
|
+
},
|
|
2540
|
+
Open: unsupported("File I/O (Open)"),
|
|
2541
|
+
Close: unsupported("File I/O (Close)"),
|
|
2542
|
+
Input: unsupported("File I/O (Input #)"),
|
|
2543
|
+
LineInput: unsupported("File I/O (Line Input #)"),
|
|
2544
|
+
Write: unsupported("File I/O (Write #)"),
|
|
2545
|
+
Get: unsupported("File I/O (Get #)"),
|
|
2546
|
+
Put: unsupported("File I/O (Put #)"),
|
|
2547
|
+
Seek: unsupported("File I/O (Seek)"),
|
|
2548
|
+
Lock: unsupported("File I/O (Lock)"),
|
|
2549
|
+
Unlock: unsupported("File I/O (Unlock)"),
|
|
2550
|
+
Width: unsupported("File I/O (Width #)"),
|
|
2551
|
+
VariableDeclaration: () => void 0,
|
|
2552
|
+
ConstDeclaration: () => void 0,
|
|
2553
|
+
AttributeStatement: () => void 0,
|
|
2554
|
+
InvalidStatement: (c) => {
|
|
2555
|
+
c.b.emit(() => {
|
|
2556
|
+
throw new VbaRuntimeError(5, "Syntax error");
|
|
2557
|
+
});
|
|
2558
|
+
}
|
|
2559
|
+
};
|
|
2560
|
+
/** `LSet s = v` / `RSet s = v`: the value padded or cut to the target's current length. */
|
|
2561
|
+
function padStatement(c, target, source, side) {
|
|
2562
|
+
const ref = c.x.ref(target);
|
|
2563
|
+
const value = c.x.scalar(source);
|
|
2564
|
+
c.b.emit((f) => {
|
|
2565
|
+
const apply = (v) => {
|
|
2566
|
+
const r = ref(f);
|
|
2567
|
+
const put = (cell) => {
|
|
2568
|
+
const cur = cell.get();
|
|
2569
|
+
if (cur.vt === 36) {
|
|
2570
|
+
lsetRecord(cur.v, v);
|
|
2571
|
+
return;
|
|
2572
|
+
}
|
|
2573
|
+
const n = toStr(scalar(cur), f.session).length;
|
|
2574
|
+
const s = toStr(v, f.session);
|
|
2575
|
+
cell.set(vStr(side === "left" ? fixLen(s, n) : s.length >= n ? s.slice(0, n) : s.padStart(n, " ")));
|
|
2576
|
+
};
|
|
2577
|
+
return r instanceof Promise ? r.then(put) : put(r);
|
|
2578
|
+
};
|
|
2579
|
+
const v = value(f);
|
|
2580
|
+
return v instanceof Promise ? v.then(apply) : apply(v);
|
|
2581
|
+
});
|
|
2582
|
+
}
|
|
2583
|
+
// src/interp/compile/statements.ts
|
|
2584
|
+
const HANDLERS = {
|
|
2585
|
+
...SIMPLE,
|
|
2586
|
+
...CONTROL,
|
|
2587
|
+
...FLOW
|
|
2588
|
+
};
|
|
2589
|
+
var StatementCompiler = class {
|
|
2590
|
+
x;
|
|
2591
|
+
b = new CodeBuilder();
|
|
2592
|
+
constructor(x) {
|
|
2593
|
+
this.x = x;
|
|
2594
|
+
}
|
|
2595
|
+
block(body) {
|
|
2596
|
+
for (const s of body) this.statement(s);
|
|
2597
|
+
}
|
|
2598
|
+
statement(s) {
|
|
2599
|
+
this.at(s);
|
|
2600
|
+
HANDLERS[s.kind](this, s);
|
|
2601
|
+
}
|
|
2602
|
+
at(node) {
|
|
2603
|
+
this.b.line = node.span.start.line;
|
|
2604
|
+
this.b.column = node.span.start.column;
|
|
2605
|
+
}
|
|
2606
|
+
/** Compiles a body and closes it: the procedure end is the instruction after the last. */
|
|
2607
|
+
body(body) {
|
|
2608
|
+
this.block(body);
|
|
2609
|
+
this.b.mark(this.b.end);
|
|
2610
|
+
for (const [name, t] of this.b.labels) if (t.pc < 0) throw new Error(`label ${name} was never placed`);
|
|
2611
|
+
return this.b;
|
|
2612
|
+
}
|
|
2613
|
+
};
|
|
2614
|
+
// src/interp/compile/procedure.ts
|
|
2615
|
+
function paramPlan(ctx, p) {
|
|
2616
|
+
let omitted = null;
|
|
2617
|
+
if (p.optional) {
|
|
2618
|
+
if (p.defaultValue) omitted = ctx.consts.value(p.defaultValue);
|
|
2619
|
+
else if (p.type.kind === "variant") omitted = MISSING;
|
|
2620
|
+
}
|
|
2621
|
+
return {
|
|
2622
|
+
byRef: p.passing === "ByRef",
|
|
2623
|
+
optional: p.optional,
|
|
2624
|
+
paramArray: p.paramArray,
|
|
2625
|
+
cell: ctx.cells.make(p.type, {
|
|
2626
|
+
bounds: null,
|
|
2627
|
+
isNew: false,
|
|
2628
|
+
base: ctx.module.options.base
|
|
2629
|
+
}),
|
|
2630
|
+
omitted
|
|
2631
|
+
};
|
|
2632
|
+
}
|
|
2633
|
+
function compileProcedure(ctx) {
|
|
2634
|
+
const p = ctx.procedure;
|
|
2635
|
+
if (!p) throw new Error("compileProcedure needs a procedure");
|
|
2636
|
+
const base = ctx.module.options.base;
|
|
2637
|
+
const builder = new StatementCompiler(new ExpressionCompiler(ctx)).body(p.declaration.body);
|
|
2638
|
+
const make = (v) => ctx.cells.make(v.type, {
|
|
2639
|
+
bounds: v.bounds,
|
|
2640
|
+
isNew: v.isNew,
|
|
2641
|
+
base
|
|
2642
|
+
});
|
|
2643
|
+
return {
|
|
2644
|
+
bound: p,
|
|
2645
|
+
code: builder.code,
|
|
2646
|
+
locals: p.locals.map(make),
|
|
2647
|
+
statics: p.statics.map(make),
|
|
2648
|
+
result: p.result ? make(p.result) : null,
|
|
2649
|
+
params: p.params.map((param) => paramPlan(ctx, param))
|
|
2650
|
+
};
|
|
2651
|
+
}
|
|
2652
|
+
// src/interp/objects/events.ts
|
|
2653
|
+
var WithEventsCell = class extends ObjectCell {
|
|
2654
|
+
sink;
|
|
2655
|
+
connections;
|
|
2656
|
+
disconnect = null;
|
|
2657
|
+
constructor(cast, sink, connections) {
|
|
2658
|
+
super(cast);
|
|
2659
|
+
this.sink = sink;
|
|
2660
|
+
this.connections = connections;
|
|
2661
|
+
}
|
|
2662
|
+
set(v) {
|
|
2663
|
+
super.set(v);
|
|
2664
|
+
this.detach();
|
|
2665
|
+
const o = this.value.v;
|
|
2666
|
+
if (o && o.connect) {
|
|
2667
|
+
this.disconnect = o.connect(this.sink);
|
|
2668
|
+
this.connections.add(this);
|
|
2669
|
+
}
|
|
2670
|
+
}
|
|
2671
|
+
/** Disconnects without changing the value. */
|
|
2672
|
+
detach() {
|
|
2673
|
+
this.disconnect?.();
|
|
2674
|
+
this.disconnect = null;
|
|
2675
|
+
this.connections.delete(this);
|
|
2676
|
+
}
|
|
2677
|
+
};
|
|
2678
|
+
/** The sink that runs `handlers` (by lower-case event name) on `owner` (null in a module). */
|
|
2679
|
+
function handlerSink(services, handlers, owner) {
|
|
2680
|
+
return { fire(_source, event, args) {
|
|
2681
|
+
const h = handlers.get(event.toLowerCase());
|
|
2682
|
+
if (!h) return void 0;
|
|
2683
|
+
const params = args.slice(0, h.params.length);
|
|
2684
|
+
return services.enter((run) => {
|
|
2685
|
+
const unhandled = (e) => {
|
|
2686
|
+
if (isTrappable(e) && typeof e === "object" && e !== null) run.untrappable.set(e, null);
|
|
2687
|
+
throw e;
|
|
2688
|
+
};
|
|
2689
|
+
let r;
|
|
2690
|
+
try {
|
|
2691
|
+
r = services.invoke(h, params, [], owner, run);
|
|
2692
|
+
} catch (e) {
|
|
2693
|
+
return unhandled(e);
|
|
2694
|
+
}
|
|
2695
|
+
return r instanceof Promise ? r.then(() => void 0, unhandled) : void 0;
|
|
2696
|
+
}, void 0);
|
|
2697
|
+
} };
|
|
2698
|
+
}
|
|
2699
|
+
// src/interp/module-runtime.ts
|
|
2700
|
+
var ModuleRuntime = class {
|
|
2701
|
+
services;
|
|
2702
|
+
module;
|
|
2703
|
+
consts;
|
|
2704
|
+
cells;
|
|
2705
|
+
ctx;
|
|
2706
|
+
compiler;
|
|
2707
|
+
factories = null;
|
|
2708
|
+
constructor(services, module) {
|
|
2709
|
+
this.services = services;
|
|
2710
|
+
this.module = module;
|
|
2711
|
+
this.consts = new ConstEvaluator((e) => {
|
|
2712
|
+
return this.compiler.value(e)(this.constFrame());
|
|
2713
|
+
}, services.session);
|
|
2714
|
+
this.cells = new CellFactories(services, this.consts);
|
|
2715
|
+
this.ctx = new CompileContext(services, module, null, this.consts, this.cells);
|
|
2716
|
+
this.compiler = new ExpressionCompiler(this.ctx);
|
|
2717
|
+
}
|
|
2718
|
+
/** A frame without a procedure, for constant expressions. */
|
|
2719
|
+
constFrame() {
|
|
2720
|
+
return new Frame(EMPTY_CODE, this.services.activeRun(), [], null, null, []);
|
|
2721
|
+
}
|
|
2722
|
+
compile(p) {
|
|
2723
|
+
return compileProcedure(new CompileContext(this.services, this.module, p, this.consts, this.cells));
|
|
2724
|
+
}
|
|
2725
|
+
/** A fresh variable of the module (or of an instance, for a class), with its events connected. */
|
|
2726
|
+
variable(v, owner) {
|
|
2727
|
+
if (v.withEvents) {
|
|
2728
|
+
const handlers = this.module.eventHandlers.get(v.name.toLowerCase()) ?? /* @__PURE__ */ new Map();
|
|
2729
|
+
return new WithEventsCell(this.services.castObject(v.type), handlerSink(this.services, handlers, owner), this.services.connections);
|
|
2730
|
+
}
|
|
2731
|
+
return this.factory(v)();
|
|
2732
|
+
}
|
|
2733
|
+
factory(v) {
|
|
2734
|
+
this.factories ??= this.module.variables.map((s) => this.cells.make(s.type, {
|
|
2735
|
+
bounds: s.bounds,
|
|
2736
|
+
isNew: s.isNew,
|
|
2737
|
+
base: this.module.options.base
|
|
2738
|
+
}));
|
|
2739
|
+
return this.factories[v.slot];
|
|
2740
|
+
}
|
|
2741
|
+
/** Every variable of the module, for its module state or a new instance. */
|
|
2742
|
+
variables(owner) {
|
|
2743
|
+
return this.module.variables.map((v) => this.variable(v, owner));
|
|
2744
|
+
}
|
|
2745
|
+
};
|
|
2746
|
+
const EMPTY_CODE = {
|
|
2747
|
+
bound: null,
|
|
2748
|
+
code: [],
|
|
2749
|
+
locals: [],
|
|
2750
|
+
statics: [],
|
|
2751
|
+
result: null,
|
|
2752
|
+
params: []
|
|
2753
|
+
};
|
|
2754
|
+
// src/interp/module-state.ts
|
|
2755
|
+
var ModuleState = class {
|
|
2756
|
+
module;
|
|
2757
|
+
init;
|
|
2758
|
+
vars = [];
|
|
2759
|
+
statics = /* @__PURE__ */ new Map();
|
|
2760
|
+
initialized = false;
|
|
2761
|
+
constructor(module, init) {
|
|
2762
|
+
this.module = module;
|
|
2763
|
+
this.init = init;
|
|
2764
|
+
}
|
|
2765
|
+
/** Creates the variables on first use (array bounds may name constants of other modules). */
|
|
2766
|
+
ensure() {
|
|
2767
|
+
if (!this.initialized) {
|
|
2768
|
+
this.initialized = true;
|
|
2769
|
+
this.vars = this.init();
|
|
2770
|
+
}
|
|
2771
|
+
return this;
|
|
2772
|
+
}
|
|
2773
|
+
reset() {
|
|
2774
|
+
this.initialized = false;
|
|
2775
|
+
this.vars = [];
|
|
2776
|
+
this.statics.clear();
|
|
2777
|
+
}
|
|
2778
|
+
};
|
|
2779
|
+
// src/interp/objects/library-objects.ts
|
|
2780
|
+
const RAISE_PARAMS = [
|
|
2781
|
+
"number",
|
|
2782
|
+
"source",
|
|
2783
|
+
"description",
|
|
2784
|
+
"helpfile",
|
|
2785
|
+
"helpcontext"
|
|
2786
|
+
];
|
|
2787
|
+
/** Positional and named arguments placed on a parameter list (named by lower-case name). */
|
|
2788
|
+
function placed(params, args, named) {
|
|
2789
|
+
const out = params.map((_, i) => args[i]);
|
|
2790
|
+
if (named) for (const [n, v] of named) {
|
|
2791
|
+
const i = params.indexOf(n.toLowerCase());
|
|
2792
|
+
if (i < 0) throw new VbaRuntimeError(448);
|
|
2793
|
+
out[i] = v;
|
|
2794
|
+
}
|
|
2795
|
+
return out.map((v) => v && given(v) ? v : void 0);
|
|
2796
|
+
}
|
|
2797
|
+
function errObject(s) {
|
|
2798
|
+
const err = s.err;
|
|
2799
|
+
const read = (name) => {
|
|
2800
|
+
switch (name.toLowerCase()) {
|
|
2801
|
+
case "":
|
|
2802
|
+
case "number": return vLng(err.number);
|
|
2803
|
+
case "description": return vStr(err.description);
|
|
2804
|
+
case "source": return vStr(err.source);
|
|
2805
|
+
case "helpfile": return vStr(err.helpFile);
|
|
2806
|
+
case "helpcontext": return vLng(err.helpContext);
|
|
2807
|
+
case "lastdllerror": return vLng(err.lastDllError);
|
|
2808
|
+
default: return noMember(name);
|
|
2809
|
+
}
|
|
2810
|
+
};
|
|
2811
|
+
const invoke = (name, args, named) => {
|
|
2812
|
+
switch (name.toLowerCase()) {
|
|
2813
|
+
case "raise": {
|
|
2814
|
+
const [n, source, description, helpFile, helpContext] = placed(RAISE_PARAMS, args, named);
|
|
2815
|
+
if (n === void 0) throw new VbaRuntimeError(449);
|
|
2816
|
+
return err.raise(n, source, description, helpFile, helpContext, s);
|
|
2817
|
+
}
|
|
2818
|
+
case "clear":
|
|
2819
|
+
err.clear();
|
|
2820
|
+
return EMPTY$1;
|
|
2821
|
+
default:
|
|
2822
|
+
if (args.length > 0) throw new VbaRuntimeError(450);
|
|
2823
|
+
return read(name);
|
|
2824
|
+
}
|
|
2825
|
+
};
|
|
2826
|
+
return {
|
|
2827
|
+
typeName: () => "ErrObject",
|
|
2828
|
+
get: invoke,
|
|
2829
|
+
call: invoke,
|
|
2830
|
+
let: (name, args, value) => {
|
|
2831
|
+
if (args.length > 0) throw new VbaRuntimeError(450);
|
|
2832
|
+
const v = scalar(value);
|
|
2833
|
+
switch (name.toLowerCase()) {
|
|
2834
|
+
case "":
|
|
2835
|
+
case "number":
|
|
2836
|
+
err.number = toLng(v, s);
|
|
2837
|
+
if (!err.description) err.description = new VbaRuntimeError(err.number).description;
|
|
2838
|
+
return;
|
|
2839
|
+
case "description":
|
|
2840
|
+
err.description = toStr(v, s);
|
|
2841
|
+
return;
|
|
2842
|
+
case "source":
|
|
2843
|
+
err.source = toStr(v, s);
|
|
2844
|
+
return;
|
|
2845
|
+
case "helpfile":
|
|
2846
|
+
err.helpFile = toStr(v, s);
|
|
2847
|
+
return;
|
|
2848
|
+
case "helpcontext":
|
|
2849
|
+
err.helpContext = toLng(v, s);
|
|
2850
|
+
return;
|
|
2851
|
+
default: noMember(name);
|
|
2852
|
+
}
|
|
2853
|
+
},
|
|
2854
|
+
set: (name) => noMember(name)
|
|
2855
|
+
};
|
|
2856
|
+
}
|
|
2857
|
+
function debugObject(s) {
|
|
2858
|
+
const call = (name, args) => {
|
|
2859
|
+
if (name.toLowerCase() !== "assert") return noMember(name);
|
|
2860
|
+
const v = args[0];
|
|
2861
|
+
if (v === void 0) throw new VbaRuntimeError(449);
|
|
2862
|
+
debugAssert(s, toBool(scalar(v), s));
|
|
2863
|
+
return EMPTY$1;
|
|
2864
|
+
};
|
|
2865
|
+
return {
|
|
2866
|
+
typeName: () => "Debug",
|
|
2867
|
+
get: call,
|
|
2868
|
+
call,
|
|
2869
|
+
let: (name) => noMember(name),
|
|
2870
|
+
set: (name) => noMember(name)
|
|
2871
|
+
};
|
|
2872
|
+
}
|
|
2873
|
+
// src/interp/run-context.ts
|
|
2874
|
+
var RunContext = class {
|
|
2875
|
+
session;
|
|
2876
|
+
guard;
|
|
2877
|
+
services;
|
|
2878
|
+
/** Objects to check for Class_Terminate at the end of the statement that dropped them. */
|
|
2879
|
+
terminations = new TerminationQueue();
|
|
2880
|
+
/** The procedures each unhandled error left, innermost first. */
|
|
2881
|
+
stacks = /* @__PURE__ */ new WeakMap();
|
|
2882
|
+
/**
|
|
2883
|
+
* Errors a handler may not trap: `null` for every frame (an error that left an event handler),
|
|
2884
|
+
* else only the frame that raised it (recursive GoSub out of stack space).
|
|
2885
|
+
*/
|
|
2886
|
+
untrappable = /* @__PURE__ */ new WeakMap();
|
|
2887
|
+
/**
|
|
2888
|
+
* True while the run is parked: waiting on its host or yielding (`yieldMillis`). A call into
|
|
2889
|
+
* the program then queues behind the run; a call made while the run computes (synchronously,
|
|
2890
|
+
* from a host member it called) runs inside it.
|
|
2891
|
+
*/
|
|
2892
|
+
suspended = false;
|
|
2893
|
+
/** Awaits `p` with the run marked suspended. */
|
|
2894
|
+
async park(p) {
|
|
2895
|
+
this.suspended = true;
|
|
2896
|
+
try {
|
|
2897
|
+
return await p;
|
|
2898
|
+
} finally {
|
|
2899
|
+
this.suspended = false;
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
constructor(session, guard, services) {
|
|
2903
|
+
this.session = session;
|
|
2904
|
+
this.guard = guard;
|
|
2905
|
+
this.services = services;
|
|
2906
|
+
}
|
|
2907
|
+
/** Queues an object to be checked when the current statement ends. */
|
|
2908
|
+
queue(o) {
|
|
2909
|
+
this.terminations.push(o, this.guard.depth);
|
|
2910
|
+
}
|
|
2911
|
+
};
|
|
2912
|
+
// src/interp/scheduler.ts
|
|
2913
|
+
const isValue = (v) => typeof v === "object" && v !== null && "vt" in v && v.vt === 9;
|
|
2914
|
+
var RunScheduler = class {
|
|
2915
|
+
core;
|
|
2916
|
+
cancel;
|
|
2917
|
+
queue = Promise.resolve();
|
|
2918
|
+
/** Queued or running work. */
|
|
2919
|
+
pending = 0;
|
|
2920
|
+
disposed = false;
|
|
2921
|
+
constructor(core, cancel) {
|
|
2922
|
+
this.core = core;
|
|
2923
|
+
this.cancel = cancel;
|
|
2924
|
+
}
|
|
2925
|
+
get isRunning() {
|
|
2926
|
+
return this.pending > 0 || this.core.current !== null;
|
|
2927
|
+
}
|
|
2928
|
+
/** The run a newcomer joins: the one computing now (the newcomer is on its stack). */
|
|
2929
|
+
get joinable() {
|
|
2930
|
+
const cur = this.core.current;
|
|
2931
|
+
return cur && !cur.suspended ? cur : null;
|
|
2932
|
+
}
|
|
2933
|
+
/** Runs `work` after everything queued before it. */
|
|
2934
|
+
serial(work) {
|
|
2935
|
+
this.pending++;
|
|
2936
|
+
const settled = this.queue.then(work).finally(() => this.pending--);
|
|
2937
|
+
this.queue = settled.catch(() => void 0);
|
|
2938
|
+
return settled;
|
|
2939
|
+
}
|
|
2940
|
+
/** A program run: `body` in a run of its own, its ending mapped to a result. */
|
|
2941
|
+
async execute(body, isFunction) {
|
|
2942
|
+
if (this.disposed) return notRun(51, "The program was disposed");
|
|
2943
|
+
const run = this.start();
|
|
2944
|
+
try {
|
|
2945
|
+
run.guard.checkCancel();
|
|
2946
|
+
this.core.ensureModules();
|
|
2947
|
+
const value = await body(run);
|
|
2948
|
+
if (isFunction) escape(value);
|
|
2949
|
+
await collectGarbage(run);
|
|
2950
|
+
return completed(isFunction ? value : void 0, run.guard.executed);
|
|
2951
|
+
} catch (e) {
|
|
2952
|
+
return this.outcome(e, run);
|
|
2953
|
+
} finally {
|
|
2954
|
+
this.finish(run);
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
/** `run` called from inside a run (as `Application.Run`): runs in that run. */
|
|
2958
|
+
async nested(run, body, isFunction) {
|
|
2959
|
+
try {
|
|
2960
|
+
const value = await body(run);
|
|
2961
|
+
if (isFunction) escape(value);
|
|
2962
|
+
return completed(isFunction ? value : void 0, run.guard.executed);
|
|
2963
|
+
} catch (e) {
|
|
2964
|
+
return this.outcome(e, run);
|
|
2965
|
+
}
|
|
2966
|
+
}
|
|
2967
|
+
/**
|
|
2968
|
+
* A host's call into an object of the program (a member of a class instance it holds, a
|
|
2969
|
+
* WithEvents handler): inside the current run when it can join it, else a run of its own,
|
|
2970
|
+
* queued behind the work in progress. Failures of a run of its own reach the host as a
|
|
2971
|
+
* `VbaCallbackFailure`; `halted` is what the call answers after `End` or `Stop`.
|
|
2972
|
+
*/
|
|
2973
|
+
enter(work, halted) {
|
|
2974
|
+
const cur = this.joinable;
|
|
2975
|
+
if (cur) return work(cur);
|
|
2976
|
+
if (this.disposed) throw new VbaCallbackFailure(notRun(51, "The program was disposed"));
|
|
2977
|
+
if (this.pending === 0 && this.core.current === null) return this.callback(work, halted);
|
|
2978
|
+
return this.serial(() => this.callback(work, halted));
|
|
2979
|
+
}
|
|
2980
|
+
callback(work, halted) {
|
|
2981
|
+
const run = this.start();
|
|
2982
|
+
this.pending++;
|
|
2983
|
+
const done = (value) => {
|
|
2984
|
+
if (isValue(value)) escape(value);
|
|
2985
|
+
const g = collectGarbage(run);
|
|
2986
|
+
if (g instanceof Promise) return g.then(() => this.close(run, value), (e) => this.fail(run, e, halted));
|
|
2987
|
+
return this.close(run, value);
|
|
2988
|
+
};
|
|
2989
|
+
let r;
|
|
2990
|
+
try {
|
|
2991
|
+
run.guard.checkCancel();
|
|
2992
|
+
this.core.ensureModules();
|
|
2993
|
+
r = work(run);
|
|
2994
|
+
} catch (e) {
|
|
2995
|
+
return this.fail(run, e, halted);
|
|
2996
|
+
}
|
|
2997
|
+
if (r instanceof Promise) return r.then(done, (e) => this.fail(run, e, halted));
|
|
2998
|
+
try {
|
|
2999
|
+
return done(r);
|
|
3000
|
+
} catch (e) {
|
|
3001
|
+
return this.fail(run, e, halted);
|
|
3002
|
+
}
|
|
3003
|
+
}
|
|
3004
|
+
close(run, value) {
|
|
3005
|
+
this.finish(run);
|
|
3006
|
+
this.pending--;
|
|
3007
|
+
return value;
|
|
3008
|
+
}
|
|
3009
|
+
fail(run, e, halted) {
|
|
3010
|
+
const result = this.outcome(e, run);
|
|
3011
|
+
this.close(run, void 0);
|
|
3012
|
+
if (result.ok) return halted;
|
|
3013
|
+
throw new VbaCallbackFailure(result);
|
|
3014
|
+
}
|
|
3015
|
+
start() {
|
|
3016
|
+
const run = this.core.newRun(this.cancel);
|
|
3017
|
+
this.core.current = run;
|
|
3018
|
+
return run;
|
|
3019
|
+
}
|
|
3020
|
+
finish(run) {
|
|
3021
|
+
run.guard.close();
|
|
3022
|
+
if (this.core.current === run) this.core.current = null;
|
|
3023
|
+
}
|
|
3024
|
+
outcome(e, run) {
|
|
3025
|
+
if (e instanceof VbaAbort && e.reason === "end") this.core.reset();
|
|
3026
|
+
const stack = typeof e === "object" && e !== null ? run.stacks.get(e) : void 0;
|
|
3027
|
+
return outcomeOf(e, {
|
|
3028
|
+
steps: run.guard.executed,
|
|
3029
|
+
projectName: this.core.session.projectName,
|
|
3030
|
+
stack
|
|
3031
|
+
});
|
|
3032
|
+
}
|
|
3033
|
+
};
|
|
3034
|
+
// src/interp/core.ts
|
|
3035
|
+
var InterpreterCore = class {
|
|
3036
|
+
program;
|
|
3037
|
+
session;
|
|
3038
|
+
host;
|
|
3039
|
+
limits;
|
|
3040
|
+
gate;
|
|
3041
|
+
library = new LibraryDispatch();
|
|
3042
|
+
err;
|
|
3043
|
+
debug;
|
|
3044
|
+
/** The run in progress (runs are serialised by the scheduler). */
|
|
3045
|
+
current = null;
|
|
3046
|
+
scheduler;
|
|
3047
|
+
/** Connected WithEvents variables, disconnected by `End`, Reset and dispose. */
|
|
3048
|
+
connections = /* @__PURE__ */ new Set();
|
|
3049
|
+
runtimes = /* @__PURE__ */ new Map();
|
|
3050
|
+
states = /* @__PURE__ */ new Map();
|
|
3051
|
+
code = /* @__PURE__ */ new Map();
|
|
3052
|
+
constants = /* @__PURE__ */ new Map();
|
|
3053
|
+
enums = /* @__PURE__ */ new Map();
|
|
3054
|
+
udts = /* @__PURE__ */ new Map();
|
|
3055
|
+
defaults = /* @__PURE__ */ new Map();
|
|
3056
|
+
constructor(program, session, host, limits, cancel = {}, gate = null) {
|
|
3057
|
+
this.program = program;
|
|
3058
|
+
this.session = session;
|
|
3059
|
+
this.host = host;
|
|
3060
|
+
this.limits = limits;
|
|
3061
|
+
this.gate = gate;
|
|
3062
|
+
this.scheduler = new RunScheduler(this, cancel);
|
|
3063
|
+
this.err = errObject(session);
|
|
3064
|
+
this.debug = debugObject(session);
|
|
3065
|
+
}
|
|
3066
|
+
runtime(m) {
|
|
3067
|
+
let r = this.runtimes.get(m);
|
|
3068
|
+
if (!r) {
|
|
3069
|
+
r = new ModuleRuntime(this, m);
|
|
3070
|
+
this.runtimes.set(m, r);
|
|
3071
|
+
}
|
|
3072
|
+
return r;
|
|
3073
|
+
}
|
|
3074
|
+
moduleState(m) {
|
|
3075
|
+
let s = this.states.get(m);
|
|
3076
|
+
if (!s) {
|
|
3077
|
+
s = new ModuleState(m, () => this.runtime(m).variables(null));
|
|
3078
|
+
this.states.set(m, s);
|
|
3079
|
+
}
|
|
3080
|
+
return s;
|
|
3081
|
+
}
|
|
3082
|
+
/** Creates every module's variables (after a reset, before a run). */
|
|
3083
|
+
ensureModules() {
|
|
3084
|
+
for (const m of this.program.modules) if (!m.perInstance) this.moduleState(m).ensure();
|
|
3085
|
+
}
|
|
3086
|
+
/**
|
|
3087
|
+
* `End` and the VBE's Reset: every module variable and Static local back to its initial value,
|
|
3088
|
+
* default instances dropped, WithEvents disconnected. No Class_Terminate runs.
|
|
3089
|
+
*/
|
|
3090
|
+
reset() {
|
|
3091
|
+
for (const c of [...this.connections]) c.detach();
|
|
3092
|
+
this.connections.clear();
|
|
3093
|
+
for (const s of this.states.values()) s.reset();
|
|
3094
|
+
this.defaults.clear();
|
|
3095
|
+
}
|
|
3096
|
+
compiled(p) {
|
|
3097
|
+
let c = this.code.get(p);
|
|
3098
|
+
if (!c) {
|
|
3099
|
+
c = this.runtime(p.module).compile(p);
|
|
3100
|
+
this.code.set(p, c);
|
|
3101
|
+
}
|
|
3102
|
+
return c;
|
|
3103
|
+
}
|
|
3104
|
+
newRun(cancel = {}) {
|
|
3105
|
+
return new RunContext(this.session, new LimitGuard(this.limits, cancel), this);
|
|
3106
|
+
}
|
|
3107
|
+
activeRun() {
|
|
3108
|
+
return this.current ?? this.newRun();
|
|
3109
|
+
}
|
|
3110
|
+
enter(work, halted) {
|
|
3111
|
+
return this.scheduler.enter(work, halted);
|
|
3112
|
+
}
|
|
3113
|
+
lastReleased(o) {
|
|
3114
|
+
if (this.current) {
|
|
3115
|
+
this.current.queue(o);
|
|
3116
|
+
return;
|
|
3117
|
+
}
|
|
3118
|
+
const r = this.scheduler.enter((run) => run.queue(o), void 0);
|
|
3119
|
+
if (r instanceof Promise) r.catch(() => void 0);
|
|
3120
|
+
}
|
|
3121
|
+
invoke(p, args, rest, instance, run) {
|
|
3122
|
+
const broken = this.gate?.moduleError(p.module);
|
|
3123
|
+
if (broken) throw new VbaCompileError(broken);
|
|
3124
|
+
const code = this.compiled(p);
|
|
3125
|
+
run.guard.enter();
|
|
3126
|
+
const n = p.params.length;
|
|
3127
|
+
const slots = new Array(p.frameSize);
|
|
3128
|
+
const owned = [];
|
|
3129
|
+
const held = [];
|
|
3130
|
+
const locks = [];
|
|
3131
|
+
const unhold = () => {
|
|
3132
|
+
for (const v of held) release(v);
|
|
3133
|
+
for (const a of locks) a.locked--;
|
|
3134
|
+
};
|
|
3135
|
+
try {
|
|
3136
|
+
for (let i = 0; i < n; i++) {
|
|
3137
|
+
const plan = code.params[i];
|
|
3138
|
+
const a = args[i] ?? null;
|
|
3139
|
+
let cell;
|
|
3140
|
+
if (plan.paramArray) cell = new VariantCell(vArr(arrayOf(rest, 0, this.session)));
|
|
3141
|
+
else if (a === null) {
|
|
3142
|
+
if (!plan.optional) throw new VbaRuntimeError(449);
|
|
3143
|
+
cell = plan.cell();
|
|
3144
|
+
if (plan.omitted) cell.set(plan.omitted);
|
|
3145
|
+
} else if (plan.byRef && !(a instanceof TempCell)) cell = a;
|
|
3146
|
+
else {
|
|
3147
|
+
cell = plan.cell();
|
|
3148
|
+
cell.set(a.get());
|
|
3149
|
+
}
|
|
3150
|
+
if (cell !== a) owned.push(cell);
|
|
3151
|
+
if (a instanceof TempCell && a.value.vt === 9) {
|
|
3152
|
+
retain(a.value);
|
|
3153
|
+
held.push(a.value);
|
|
3154
|
+
} else if (cell === a && a instanceof ElementRef) {
|
|
3155
|
+
a.array.locked++;
|
|
3156
|
+
locks.push(a.array);
|
|
3157
|
+
}
|
|
3158
|
+
slots[i] = cell;
|
|
3159
|
+
}
|
|
3160
|
+
const result = code.result ? code.result() : new VariantCell();
|
|
3161
|
+
slots[n] = result;
|
|
3162
|
+
owned.push(result);
|
|
3163
|
+
for (let j = 0; j < code.locals.length; j++) {
|
|
3164
|
+
const cell = code.locals[j]();
|
|
3165
|
+
slots[n + 1 + j] = cell;
|
|
3166
|
+
owned.push(cell);
|
|
3167
|
+
}
|
|
3168
|
+
} catch (e) {
|
|
3169
|
+
unhold();
|
|
3170
|
+
run.guard.leave();
|
|
3171
|
+
throw e;
|
|
3172
|
+
}
|
|
3173
|
+
const statics = code.statics.length > 0 ? this.statics(p, code, instance) : [];
|
|
3174
|
+
const f = new Frame(code, run, slots, instance ?? (p.module.kind === "document" ? this.host.documentObjects.get(p.module.name.toLowerCase()) ?? null : null), instance, statics);
|
|
3175
|
+
instance?.retain();
|
|
3176
|
+
const leave = () => {
|
|
3177
|
+
if (f.handler.handling) this.session.err.clear();
|
|
3178
|
+
const value = slots[n].get();
|
|
3179
|
+
for (let i = owned.length - 1; i >= 0; i--) {
|
|
3180
|
+
const cell = owned[i];
|
|
3181
|
+
const v = cell instanceof ArrayCell ? cell.value : cell.get();
|
|
3182
|
+
if (v.vt === 9) release(v);
|
|
3183
|
+
else if (v.vt === 8192 && cell instanceof ArrayCell) v.v.releaseAll();
|
|
3184
|
+
}
|
|
3185
|
+
for (const v of f.with) if (v) release(v);
|
|
3186
|
+
closeCursors(f);
|
|
3187
|
+
unhold();
|
|
3188
|
+
instance?.release();
|
|
3189
|
+
run.guard.leave();
|
|
3190
|
+
return value;
|
|
3191
|
+
};
|
|
3192
|
+
/** An error leaves the procedure: its locals terminate before any caller's handler runs. */
|
|
3193
|
+
const fail = (e) => {
|
|
3194
|
+
leave();
|
|
3195
|
+
const g = collectGarbage(run, f.depth, f.since);
|
|
3196
|
+
if (g instanceof Promise) return g.then(() => {
|
|
3197
|
+
throw e;
|
|
3198
|
+
});
|
|
3199
|
+
throw e;
|
|
3200
|
+
};
|
|
3201
|
+
let r;
|
|
3202
|
+
try {
|
|
3203
|
+
r = execute(f);
|
|
3204
|
+
} catch (e) {
|
|
3205
|
+
return fail(e);
|
|
3206
|
+
}
|
|
3207
|
+
if (r instanceof Promise) return r.then(leave, fail);
|
|
3208
|
+
return leave();
|
|
3209
|
+
}
|
|
3210
|
+
statics(p, code, instance) {
|
|
3211
|
+
const store = instance ? instance.statics : this.moduleState(p.module).statics;
|
|
3212
|
+
let s = store.get(p);
|
|
3213
|
+
if (!s) {
|
|
3214
|
+
s = code.statics.map((make) => make());
|
|
3215
|
+
store.set(p, s);
|
|
3216
|
+
}
|
|
3217
|
+
return s;
|
|
3218
|
+
}
|
|
3219
|
+
constValue(c) {
|
|
3220
|
+
let v = this.constants.get(c);
|
|
3221
|
+
if (!v) {
|
|
3222
|
+
const m = this.program.module(c.module);
|
|
3223
|
+
if (!m) throw new VbaRuntimeError(51);
|
|
3224
|
+
v = this.runtime(m).consts.value(c.value);
|
|
3225
|
+
if (c.type?.kind === "scalar") v = letAs(c.type.vt, scalar(v), this.session);
|
|
3226
|
+
this.constants.set(c, v);
|
|
3227
|
+
}
|
|
3228
|
+
return v;
|
|
3229
|
+
}
|
|
3230
|
+
enumValue(e) {
|
|
3231
|
+
let v = this.enums.get(e);
|
|
3232
|
+
if (!v) {
|
|
3233
|
+
const m = this.program.module(e.module);
|
|
3234
|
+
if (!m) throw new VbaRuntimeError(51);
|
|
3235
|
+
if (e.value) v = vLng(this.runtime(m).consts.number(e.value));
|
|
3236
|
+
else if (e.previous) v = vLng(toLng(this.enumValue(e.previous)) + 1);
|
|
3237
|
+
else v = vLng(0);
|
|
3238
|
+
this.enums.set(e, v);
|
|
3239
|
+
}
|
|
3240
|
+
return v;
|
|
3241
|
+
}
|
|
3242
|
+
udtType(u) {
|
|
3243
|
+
let t = this.udts.get(u);
|
|
3244
|
+
if (t) return t;
|
|
3245
|
+
const m = this.program.module(u.module);
|
|
3246
|
+
if (!m) throw new VbaRuntimeError(51);
|
|
3247
|
+
const rt = this.runtime(m);
|
|
3248
|
+
const fields = u.fields.map((f) => this.udtField(rt, f.name, f.type, f.bounds));
|
|
3249
|
+
t = defineUdt(u.name, fields);
|
|
3250
|
+
this.udts.set(u, t);
|
|
3251
|
+
return t;
|
|
3252
|
+
}
|
|
3253
|
+
udtField(rt, name, type, bounds) {
|
|
3254
|
+
switch (type.kind) {
|
|
3255
|
+
case "scalar": return type.fixedLength ? {
|
|
3256
|
+
name,
|
|
3257
|
+
vt: type.vt,
|
|
3258
|
+
fixedLength: rt.consts.number(type.fixedLength)
|
|
3259
|
+
} : {
|
|
3260
|
+
name,
|
|
3261
|
+
vt: type.vt
|
|
3262
|
+
};
|
|
3263
|
+
case "object": return {
|
|
3264
|
+
name,
|
|
3265
|
+
vt: VT.Object
|
|
3266
|
+
};
|
|
3267
|
+
case "udt": return {
|
|
3268
|
+
name,
|
|
3269
|
+
vt: VT.UserDefinedType,
|
|
3270
|
+
type: this.udtType(type.udt)
|
|
3271
|
+
};
|
|
3272
|
+
case "array": {
|
|
3273
|
+
const make = rt.cells.make(type, {
|
|
3274
|
+
bounds,
|
|
3275
|
+
isNew: false,
|
|
3276
|
+
base: rt.module.options.base
|
|
3277
|
+
});
|
|
3278
|
+
return {
|
|
3279
|
+
name,
|
|
3280
|
+
vt: VT.Array,
|
|
3281
|
+
array: () => make().get().v
|
|
3282
|
+
};
|
|
3283
|
+
}
|
|
3284
|
+
default: return {
|
|
3285
|
+
name,
|
|
3286
|
+
vt: VT.Variant
|
|
3287
|
+
};
|
|
3288
|
+
}
|
|
3289
|
+
}
|
|
3290
|
+
createObject(type, run) {
|
|
3291
|
+
if (type.kind !== "object") throw new VbaRuntimeError(429);
|
|
3292
|
+
const cls = type.classModule ? this.program.module(type.classModule) : void 0;
|
|
3293
|
+
if (cls && cls.kind === "class") return this.instantiate(cls, run);
|
|
3294
|
+
if ((type.className ?? "").toLowerCase().replace(/^vba\./, "") === "collection") return vObj(collectionObject(this.session));
|
|
3295
|
+
throw new VbaRuntimeError(429, `ActiveX component can't create object (${type.className ?? "Object"})`);
|
|
3296
|
+
}
|
|
3297
|
+
instantiate(m, run) {
|
|
3298
|
+
const instance = new ClassInstance(m, this);
|
|
3299
|
+
instance.vars.push(...this.runtime(m).variables(instance));
|
|
3300
|
+
run.queue(instance);
|
|
3301
|
+
const init = m.groups.get("class_initialize")?.method;
|
|
3302
|
+
const value = vObj(instance);
|
|
3303
|
+
if (!init) return value;
|
|
3304
|
+
return andThen(this.invoke(init, [], [], instance, run), () => value);
|
|
3305
|
+
}
|
|
3306
|
+
defaultInstance(m, run) {
|
|
3307
|
+
const known = this.defaults.get(m);
|
|
3308
|
+
if (known) return known;
|
|
3309
|
+
return andThen(this.instantiate(m, run), (v) => {
|
|
3310
|
+
classInstanceOf(v)?.escape();
|
|
3311
|
+
this.defaults.set(m, v);
|
|
3312
|
+
return v;
|
|
3313
|
+
});
|
|
3314
|
+
}
|
|
3315
|
+
castObject(type) {
|
|
3316
|
+
if (type.kind !== "object" || !type.classModule) return defaultInterface;
|
|
3317
|
+
const target = this.program.module(type.classModule);
|
|
3318
|
+
if (!target) return defaultInterface;
|
|
3319
|
+
return (v) => {
|
|
3320
|
+
if (v.vt !== 9 || v.v === null) return v;
|
|
3321
|
+
const o = v.v;
|
|
3322
|
+
const instance = classInstanceOf(v);
|
|
3323
|
+
if (!instance) throw new VbaRuntimeError(13);
|
|
3324
|
+
if (instance.module === target) return vObj(instance);
|
|
3325
|
+
if (!instance.module.implements.includes(target)) throw new VbaRuntimeError(13);
|
|
3326
|
+
if (o instanceof InterfaceView && o.iface === target) return v;
|
|
3327
|
+
return vObj(new InterfaceView(instance, target));
|
|
3328
|
+
};
|
|
3329
|
+
}
|
|
3330
|
+
};
|
|
3331
|
+
// src/interp/program.ts
|
|
3332
|
+
const DEFAULT_YIELD_MILLIS = 100;
|
|
3333
|
+
var Interpreter = class {
|
|
3334
|
+
diagnostics;
|
|
3335
|
+
bound;
|
|
3336
|
+
core;
|
|
3337
|
+
gate;
|
|
3338
|
+
constructor(options) {
|
|
3339
|
+
const limits = {
|
|
3340
|
+
...DEFAULT_EXEC_LIMITS,
|
|
3341
|
+
...options.limits
|
|
3342
|
+
};
|
|
3343
|
+
this.bound = bindProgram(options.modules, {
|
|
3344
|
+
...options.constants ? { constants: options.constants } : {},
|
|
3345
|
+
globals: Object.keys(options.globals ?? {}),
|
|
3346
|
+
globalObject: options.globalObject !== void 0,
|
|
3347
|
+
documentObjects: Object.keys(options.documentObjects ?? {})
|
|
3348
|
+
});
|
|
3349
|
+
this.diagnostics = this.bound.diagnostics.map((d) => ({ ...d }));
|
|
3350
|
+
this.gate = new CompileGate(this.bound, this.diagnostics);
|
|
3351
|
+
const s = options.session ?? {};
|
|
3352
|
+
const session = createSession({
|
|
3353
|
+
limits,
|
|
3354
|
+
meter: { charge: (work) => this.core.current?.guard.charge(work) },
|
|
3355
|
+
...s.locale ? { locale: s.locale } : {},
|
|
3356
|
+
...s.ui ? { ui: s.ui } : {},
|
|
3357
|
+
...s.debug ? { debug: s.debug } : {},
|
|
3358
|
+
...s.environ ? { environ: s.environ } : {},
|
|
3359
|
+
...s.clock ? { clock: s.clock } : {},
|
|
3360
|
+
...s.projectName ? { projectName: s.projectName } : {}
|
|
3361
|
+
});
|
|
3362
|
+
this.core = new InterpreterCore(this.bound, session, {
|
|
3363
|
+
globals: normalizeDocumentObjects(options.globals),
|
|
3364
|
+
globalObject: options.globalObject,
|
|
3365
|
+
documentObjects: normalizeDocumentObjects(options.documentObjects)
|
|
3366
|
+
}, limits, {
|
|
3367
|
+
signal: options.signal,
|
|
3368
|
+
isCancelled: options.isCancelled,
|
|
3369
|
+
yieldMillis: options.yieldMillis ?? DEFAULT_YIELD_MILLIS
|
|
3370
|
+
}, this.gate);
|
|
3371
|
+
}
|
|
3372
|
+
get scheduler() {
|
|
3373
|
+
return this.core.scheduler;
|
|
3374
|
+
}
|
|
3375
|
+
get isRunning() {
|
|
3376
|
+
return this.scheduler.isRunning;
|
|
3377
|
+
}
|
|
3378
|
+
procedures() {
|
|
3379
|
+
return this.bound.modules.flatMap((m) => listProcedures(m.ast, {
|
|
3380
|
+
moduleKind: m.kind,
|
|
3381
|
+
moduleName: m.name
|
|
3382
|
+
}).map((p) => {
|
|
3383
|
+
const bound = m.procedures.find((b) => b.name.toLowerCase() === p.name.toLowerCase() && b.kind === p.kind);
|
|
3384
|
+
return {
|
|
3385
|
+
...p,
|
|
3386
|
+
module: m.name,
|
|
3387
|
+
moduleKind: m.kind,
|
|
3388
|
+
canRun: bound ? this.gate.blocking(bound) === null : this.gate.moduleError(m) === null
|
|
3389
|
+
};
|
|
3390
|
+
}));
|
|
3391
|
+
}
|
|
3392
|
+
run(name, args = []) {
|
|
3393
|
+
const found = this.bound.findGroup(name);
|
|
3394
|
+
if (found === "ambiguous") return Promise.resolve(notRun(35, `Ambiguous name detected: ${name}`));
|
|
3395
|
+
const proc = found ? found.method ?? found.get : null;
|
|
3396
|
+
if (!proc || proc.module.perInstance) return Promise.resolve(notRun(35, `Sub or Function not defined: ${name}`));
|
|
3397
|
+
return this.start(proc, () => args.map((v) => new TempCell(v)), proc.kind !== "Sub");
|
|
3398
|
+
}
|
|
3399
|
+
fireEvent(objectName, event, args) {
|
|
3400
|
+
const m = this.bound.module(objectName);
|
|
3401
|
+
const host = this.core.host.documentObjects.get(objectName.toLowerCase());
|
|
3402
|
+
const proc = m && host ? m.handler(host.typeName(), event) : null;
|
|
3403
|
+
if (!proc) return Promise.resolve({
|
|
3404
|
+
status: "completed",
|
|
3405
|
+
ok: true,
|
|
3406
|
+
steps: 0
|
|
3407
|
+
});
|
|
3408
|
+
return this.start(proc, () => args.slice(0, proc.params.length), false);
|
|
3409
|
+
}
|
|
3410
|
+
reset() {
|
|
3411
|
+
return this.scheduler.serial(() => this.core.reset());
|
|
3412
|
+
}
|
|
3413
|
+
dispose() {
|
|
3414
|
+
this.scheduler.disposed = true;
|
|
3415
|
+
this.core.reset();
|
|
3416
|
+
}
|
|
3417
|
+
/** Runs `proc` in a run of its own, or inside the run in progress when called from it. */
|
|
3418
|
+
start(proc, args, isFunction) {
|
|
3419
|
+
const blocked = this.gate.blocking(proc);
|
|
3420
|
+
if (blocked) return Promise.resolve(compileFailure(blocked));
|
|
3421
|
+
const body = (run) => this.core.invoke(proc, args(), [], null, run);
|
|
3422
|
+
const joined = this.scheduler.joinable;
|
|
3423
|
+
if (joined) return this.scheduler.nested(joined, body, isFunction);
|
|
3424
|
+
return this.scheduler.serial(() => this.scheduler.execute(body, isFunction));
|
|
3425
|
+
}
|
|
3426
|
+
};
|
|
3427
|
+
/** The tree-walking interpreter: binds `options.modules` and returns the runnable program. */
|
|
3428
|
+
function createInterpreter(options) {
|
|
3429
|
+
return new Interpreter(options);
|
|
3430
|
+
}
|
|
3431
|
+
export { createInterpreter as t };
|
|
3432
|
+
|
|
3433
|
+
//# sourceMappingURL=interp-ChrsgBCf.js.map
|