@tinyactors/wasm 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -2
- package/index.d.ts +1530 -0
- package/index.mjs +10 -0
- package/init.d.ts +16 -0
- package/init.mjs +6 -0
- package/lib/address.mjs +112 -0
- package/lib/cbor.mjs +262 -0
- package/lib/checks.mjs +56 -0
- package/lib/compile.mjs +390 -0
- package/lib/convenience.mjs +876 -0
- package/lib/datamodel.mjs +1002 -0
- package/lib/direct.mjs +14 -0
- package/lib/errors.mjs +94 -0
- package/lib/host.mjs +2206 -0
- package/lib/http.mjs +104 -0
- package/lib/module.mjs +324 -0
- package/lib/quickjs.mjs +78 -0
- package/lib/realm-script.mjs +2 -0
- package/lib/realms.mjs +122 -0
- package/lib/state.mjs +32 -0
- package/lib/values.mjs +31 -0
- package/lib/version.mjs +5 -0
- package/package.json +51 -4
- package/tinyactors.wasm +0 -0
|
@@ -0,0 +1,1002 @@
|
|
|
1
|
+
// The native "ecmascript" datamodel of @tinyactors/wasm (docs/browser.md,
|
|
2
|
+
// "The native datamodel without node:vm"; the addon's src/datamodel/,
|
|
3
|
+
// translated): the vtable's entries, which wasm/src/datamodel.c forwards
|
|
4
|
+
// to the import ta_web.datamodel, done in JavaScript with the host's realm
|
|
5
|
+
// (realms.mjs) and the realm's script (the addon's src/datamodel/realm.js).
|
|
6
|
+
//
|
|
7
|
+
// Rows are the host's tables in C (charts, sessions, payloads, loops); the
|
|
8
|
+
// value of each is at its row's slot in an array of this file's: a
|
|
9
|
+
// chart's record, a session's scope, a payload's value, a loop's items.
|
|
10
|
+
//
|
|
11
|
+
// Where a chart evaluates: a scope object per session, made by the chart's
|
|
12
|
+
// constructor in the host's realm, and each expression one function of
|
|
13
|
+
// the scope, compiled at its first evaluation, sloppy outside (`with
|
|
14
|
+
// (scope)`) and strict inside: the addon's code path. What a chart's
|
|
15
|
+
// scripts declare is asked of the engine by the kit (realm.js, `script`):
|
|
16
|
+
// a chart-level script's functions are the chart's helpers, on its scopes'
|
|
17
|
+
// prototype; a script's top-level vars are the session's variables; its
|
|
18
|
+
// let, const and class are refused, the script's error.execution.
|
|
19
|
+
import * as cbor from './cbor.mjs';
|
|
20
|
+
import { TinyactorsError } from './errors.mjs';
|
|
21
|
+
import { application, makeRealm } from './realms.mjs';
|
|
22
|
+
|
|
23
|
+
// The entries (wasm/src/datamodel.c).
|
|
24
|
+
const DM = {
|
|
25
|
+
ATTACH: 1, DETACH: 2, CREATE: 3, DESERIALIZE: 4, DESTROY: 5, INIT_DATA: 6, BIND_EVENT: 7, DETACH_EVENT: 8,
|
|
26
|
+
EVAL_CONDITION: 9, EVAL_STRING: 10, ASSIGN: 11, ASSIGN_STRING: 12, SCRIPT: 13, LOG: 14, FOREACH_BEGIN: 15,
|
|
27
|
+
FOREACH_ITEM: 16, FOREACH_END: 17, CAPTURE: 18, DROP: 19, CUSTOM: 20, SERIALIZE: 21, MIGRATE: 22, TO_WIRE: 23,
|
|
28
|
+
FROM_WIRE: 24, PAYLOAD_SERIALIZE: 25, PAYLOAD_DESERIALIZE: 26, VARIABLE: 27, FAILURE: 28,
|
|
29
|
+
};
|
|
30
|
+
const TA_OK = 0, TA_ERROR_EXECUTION = -1, TA_ERROR_INVALID = -3, TA_ERROR_NO_MEMORY = -4, TA_ERROR_STALE = -5;
|
|
31
|
+
const TA_ERROR_STATE = -6, TA_ERROR_UNSUPPORTED = -7;
|
|
32
|
+
const NIL = 0xFFFFFFFF;
|
|
33
|
+
const FAILURE_NONE = 0, FAILURE_EXCEPTION = 1;
|
|
34
|
+
// TA_EXPRESSION_* (tinyactors/image.h)
|
|
35
|
+
const KIND = { CONDITION: 1, VALUE: 2, LOCATION: 3, SCRIPT: 4, STRING: 5, CONTENT: 6, ITEM: 7, GLOBAL_SCRIPT: 8 };
|
|
36
|
+
const TYPES = ['platform', 'internal', 'external'];
|
|
37
|
+
const EVENT_FIELDS = ['name', 'type', 'sendid', 'origin', 'origintype', 'invokeid', 'data', 'raw'];
|
|
38
|
+
const SPACE = /[ \t\n\r]/;
|
|
39
|
+
|
|
40
|
+
// The shapes an expression is compiled in (the addon's expressions.c).
|
|
41
|
+
const SHARED_VALUE = ["(function(__s){with(__s){return (function(){'use strict';return (\n", "\n);})();}})"];
|
|
42
|
+
const SHARED_SCRIPT = ["(function(__s){with(__s){(function(){'use strict';\n", '\n})();}})'];
|
|
43
|
+
const SHARED_SETTER = ["(function(__s,__v){with(__s){(function(){'use strict';(\n", '\n) = __v;})();}})'];
|
|
44
|
+
|
|
45
|
+
const encoder = new TextEncoder();
|
|
46
|
+
|
|
47
|
+
// A value's source without a trailing `;` (expr="new Foo();").
|
|
48
|
+
function trimmed(source) {
|
|
49
|
+
let end = source.length;
|
|
50
|
+
while (end > 0 && (source[end - 1] === ';' || SPACE.test(source[end - 1]))) end--;
|
|
51
|
+
return source.slice(0, end);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// The identifier a location starts with (ASCII; empty for anything else).
|
|
55
|
+
function leadingName(source) {
|
|
56
|
+
const match = /^[ \t\n\r]*([A-Za-z_$][A-Za-z0-9_$]*)/.exec(source);
|
|
57
|
+
return match === null ? '' : match[1];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function trim(text) { return text.replace(/^[ \t\n\r]+|[ \t\n\r]+$/g, ''); }
|
|
61
|
+
|
|
62
|
+
// A refusal the datamodel answers core with, carrying its status.
|
|
63
|
+
class Refused extends Error {
|
|
64
|
+
constructor(status, message) {
|
|
65
|
+
super(message);
|
|
66
|
+
this.status = status;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ---- the datamodel -----------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
export class NativeDatamodel {
|
|
73
|
+
constructor(state) {
|
|
74
|
+
this.state = state;
|
|
75
|
+
this.module = state.module;
|
|
76
|
+
this.fn = state.fn;
|
|
77
|
+
this.realm = null;
|
|
78
|
+
this.charts = [];
|
|
79
|
+
this.sessions = [];
|
|
80
|
+
// per session slot: its chart's slot and core's ids of its _event's name, invoke and origintype
|
|
81
|
+
this.meta = new Uint32Array(64);
|
|
82
|
+
this.values = [];
|
|
83
|
+
this.loops = [];
|
|
84
|
+
this.strings = null;
|
|
85
|
+
this.failed = false;
|
|
86
|
+
this.message = '';
|
|
87
|
+
this.evaluating = -1; // the chart slot of the session evaluating, for In()
|
|
88
|
+
this.current = -1; // the slot of the session evaluating
|
|
89
|
+
this.attachError = null; // why the last chart was refused at attach
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// The host's pointer and arena: during the host's making (a restore
|
|
93
|
+
// attaches its charts) JavaScript has neither yet.
|
|
94
|
+
get pointer() { return this.state.pointer || (this.fn.ta_web_host_making(this.module.env) >>> 0); }
|
|
95
|
+
get scratch() { return this.state.scratch || (this.fn.ta_web_host_scratch(this.pointer) >>> 0); }
|
|
96
|
+
|
|
97
|
+
// ---- the realm ----
|
|
98
|
+
|
|
99
|
+
kit() { return this.ensureRealm().kit; }
|
|
100
|
+
|
|
101
|
+
ensureRealm() {
|
|
102
|
+
if (this.realm === null) this.realm = makeRealm((state) => this.in(state));
|
|
103
|
+
return this.realm;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
free() {
|
|
107
|
+
const realm = this.realm;
|
|
108
|
+
this.realm = null;
|
|
109
|
+
this.charts = [];
|
|
110
|
+
this.sessions = [];
|
|
111
|
+
this.values = [];
|
|
112
|
+
this.loops = [];
|
|
113
|
+
realm?.free();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// SCXML In() of the evaluating session (5.9.1).
|
|
117
|
+
in(state) {
|
|
118
|
+
if (this.evaluating < 0 || this.pointer === 0) return false;
|
|
119
|
+
const name = `${state}`;
|
|
120
|
+
const module = this.module;
|
|
121
|
+
return module.scoped(this.scratch, () => {
|
|
122
|
+
const [at, length] = module.text(this.scratch, name);
|
|
123
|
+
module.setWord(0, at);
|
|
124
|
+
module.setWord(1, length);
|
|
125
|
+
const index = this.fn.ta_web_chart_state(this.pointer, this.evaluating) >>> 0;
|
|
126
|
+
return index !== NIL && this.fn.ta_web_dm_in(this.pointer, index) !== 0;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ---- the record and the output ----
|
|
131
|
+
|
|
132
|
+
words(n) { return this.module.words().slice(this.module.record, this.module.record + n); }
|
|
133
|
+
|
|
134
|
+
text(pointer, length) { return this.module.string(pointer >>> 0, length); }
|
|
135
|
+
|
|
136
|
+
// The output room of at least `size` bytes: its address.
|
|
137
|
+
room(size) {
|
|
138
|
+
const at = this.fn.ta_web_dm_output(this.pointer, size) >>> 0;
|
|
139
|
+
if (at === 0) throw new Refused(TA_ERROR_NO_MEMORY, 'no memory for the datamodel\'s output');
|
|
140
|
+
return at;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// A string in the output room; its length in the record's word 0.
|
|
144
|
+
putText(text) {
|
|
145
|
+
const at = this.room(3 * text.length + 1);
|
|
146
|
+
const { written } = encoder.encodeInto(text, this.module.bytes().subarray(at, at + 3 * text.length + 1));
|
|
147
|
+
this.module.setWord(0, written);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Bytes at [pointer, length] of linear memory copied into the output room;
|
|
151
|
+
// the length in the record's word 0.
|
|
152
|
+
putBytes(pointer, length) {
|
|
153
|
+
const at = this.room(length || 1);
|
|
154
|
+
const bytes = this.module.bytes();
|
|
155
|
+
bytes.copyWithin(at, pointer, pointer + length);
|
|
156
|
+
this.module.setWord(0, length);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// `value` as CBOR (the value table's, or to_wire's with `wire`) in the
|
|
160
|
+
// output room.
|
|
161
|
+
putValue(value, wire) {
|
|
162
|
+
const module = this.module, scratch = this.scratch;
|
|
163
|
+
module.scoped(scratch, () => {
|
|
164
|
+
const [at, length] = cbor.write(module, scratch, value, { realms: true, wire });
|
|
165
|
+
this.putBytes(at, length);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// The value of the CBOR at [pointer, length], made in the application's realm.
|
|
170
|
+
readValue(pointer, length) {
|
|
171
|
+
const module = this.module;
|
|
172
|
+
return cbor.read(module, this.scratch, pointer, length);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---- the import ----
|
|
176
|
+
|
|
177
|
+
// One entry, as the import ta_web.datamodel gives it: its status. What it
|
|
178
|
+
// throws is its error.execution (kept for `failure`), or the refusal's
|
|
179
|
+
// status.
|
|
180
|
+
entry(entry, slot) {
|
|
181
|
+
if (entry === DM.FAILURE) return this.failure();
|
|
182
|
+
if (entry === DM.DROP) {
|
|
183
|
+
this.values[slot] = undefined;
|
|
184
|
+
return TA_OK;
|
|
185
|
+
}
|
|
186
|
+
this.failed = false;
|
|
187
|
+
const outer = this.evaluating, current = this.current;
|
|
188
|
+
try {
|
|
189
|
+
return this.dispatch(entry, slot) ?? TA_OK;
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (error instanceof Refused) {
|
|
192
|
+
if (error.reason !== undefined) this.state.reason = error.reason;
|
|
193
|
+
if (entry === DM.ATTACH) this.attachError = error.message;
|
|
194
|
+
return error.status;
|
|
195
|
+
}
|
|
196
|
+
this.fail(error);
|
|
197
|
+
if (entry === DM.ATTACH) this.attachError = this.message;
|
|
198
|
+
return entry === DM.ATTACH ? TA_ERROR_INVALID : TA_ERROR_EXECUTION;
|
|
199
|
+
} finally {
|
|
200
|
+
this.evaluating = outer;
|
|
201
|
+
this.current = current;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
fail(error) {
|
|
206
|
+
this.failed = true;
|
|
207
|
+
try {
|
|
208
|
+
this.message = String(error);
|
|
209
|
+
} catch {
|
|
210
|
+
this.message = '';
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
failure() {
|
|
215
|
+
if (!this.failed) {
|
|
216
|
+
this.module.setWord(0, FAILURE_NONE);
|
|
217
|
+
return TA_OK;
|
|
218
|
+
}
|
|
219
|
+
this.failed = false;
|
|
220
|
+
this.putText(this.message);
|
|
221
|
+
const length = this.module.word(0);
|
|
222
|
+
this.module.setWord(0, FAILURE_EXCEPTION);
|
|
223
|
+
this.module.setWord(1, length);
|
|
224
|
+
return TA_OK;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
dispatch(entry, slot) {
|
|
228
|
+
switch (entry) {
|
|
229
|
+
case DM.ATTACH: return this.attach(slot);
|
|
230
|
+
case DM.DETACH: this.charts[slot] = undefined; return TA_OK;
|
|
231
|
+
case DM.CREATE: return this.create(slot);
|
|
232
|
+
case DM.DESERIALIZE: return this.deserialize(slot);
|
|
233
|
+
case DM.DESTROY: this.sessions[slot] = undefined; return TA_OK;
|
|
234
|
+
case DM.INIT_DATA: return this.initData(slot);
|
|
235
|
+
case DM.BIND_EVENT: return this.bindEvent(slot);
|
|
236
|
+
case DM.DETACH_EVENT: return TA_OK; // SCXML keeps _event bound to the last event (5.10)
|
|
237
|
+
case DM.EVAL_CONDITION: {
|
|
238
|
+
const e = this.module.word(0);
|
|
239
|
+
const value = this.evaluate(this.session(slot, true), e);
|
|
240
|
+
this.module.setWord(0, value ? 1 : 0);
|
|
241
|
+
return TA_OK;
|
|
242
|
+
}
|
|
243
|
+
case DM.EVAL_STRING: {
|
|
244
|
+
const e = this.module.word(0);
|
|
245
|
+
this.putText(`${this.evaluate(this.session(slot, true), e)}`);
|
|
246
|
+
return TA_OK;
|
|
247
|
+
}
|
|
248
|
+
case DM.ASSIGN: {
|
|
249
|
+
const [location, e] = this.words(2);
|
|
250
|
+
const session = this.session(slot, true);
|
|
251
|
+
this.assign(session, location, this.evaluate(session, e));
|
|
252
|
+
return TA_OK;
|
|
253
|
+
}
|
|
254
|
+
case DM.ASSIGN_STRING: {
|
|
255
|
+
const [location, at, length] = this.words(3);
|
|
256
|
+
const text = this.text(at, length);
|
|
257
|
+
this.assign(this.session(slot, true), location, text);
|
|
258
|
+
return TA_OK;
|
|
259
|
+
}
|
|
260
|
+
case DM.SCRIPT: {
|
|
261
|
+
const e = this.module.word(0);
|
|
262
|
+
this.evaluate(this.session(slot, true), e);
|
|
263
|
+
return TA_OK;
|
|
264
|
+
}
|
|
265
|
+
case DM.LOG: return this.log(slot);
|
|
266
|
+
case DM.FOREACH_BEGIN: return this.foreachBegin(slot);
|
|
267
|
+
case DM.FOREACH_ITEM: return this.foreachItem(slot);
|
|
268
|
+
case DM.FOREACH_END: this.loops[this.module.word(0)] = undefined; return TA_OK;
|
|
269
|
+
case DM.CAPTURE: return this.capture(slot);
|
|
270
|
+
case DM.CUSTOM: return TA_ERROR_UNSUPPORTED;
|
|
271
|
+
case DM.SERIALIZE: return this.serialize(slot);
|
|
272
|
+
case DM.MIGRATE: return this.migrate(slot);
|
|
273
|
+
case DM.TO_WIRE: return this.payloadOut(slot, true);
|
|
274
|
+
case DM.PAYLOAD_SERIALIZE: return this.payloadOut(slot, false);
|
|
275
|
+
case DM.FROM_WIRE:
|
|
276
|
+
case DM.PAYLOAD_DESERIALIZE: return this.fromWire(slot);
|
|
277
|
+
case DM.VARIABLE: return this.variable(slot);
|
|
278
|
+
default: return TA_ERROR_UNSUPPORTED;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// ---- charts ----
|
|
283
|
+
|
|
284
|
+
// Expression `e` of the chart at `slot`, from the image: {kind, line, column, source}.
|
|
285
|
+
expressionOf(chart, e) {
|
|
286
|
+
let info = chart.expressions[e];
|
|
287
|
+
if (info !== undefined) return info;
|
|
288
|
+
const status = this.fn.ta_web_chart_expression(this.pointer, chart.slot, e);
|
|
289
|
+
const w = this.words(6);
|
|
290
|
+
info = status === TA_OK ? { kind: w[1], line: w[2], column: w[3], source: this.text(w[4], w[5]) } : null;
|
|
291
|
+
chart.expressions[e] = info;
|
|
292
|
+
return info;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
attach(slot) {
|
|
296
|
+
const [index, segment, binding] = this.words(3);
|
|
297
|
+
const realm = this.ensureRealm();
|
|
298
|
+
const chart = {
|
|
299
|
+
slot, index, segment, binding, expressions: [], getters: [], setters: [], names: [], invokeids: [], origins: [],
|
|
300
|
+
leading: [], scripts: [], Scope: undefined, data: [],
|
|
301
|
+
};
|
|
302
|
+
const pointer = this.pointer;
|
|
303
|
+
const dataCount = this.fn.ta_web_chart_data_count(pointer, slot);
|
|
304
|
+
for (let i = 0; i < dataCount; i++) {
|
|
305
|
+
if (this.fn.ta_web_chart_data(pointer, slot, i) !== TA_OK) continue;
|
|
306
|
+
const w = this.words(5);
|
|
307
|
+
chart.data.push({ id: this.text(w[1], w[2]), expression: w[3] });
|
|
308
|
+
}
|
|
309
|
+
const count = this.fn.ta_web_chart_expression_count(pointer, slot);
|
|
310
|
+
const names = chart.data.map((d) => d.id);
|
|
311
|
+
const scripts = [];
|
|
312
|
+
for (let e = 0; e < count; e++) {
|
|
313
|
+
const info = this.expressionOf(chart, e);
|
|
314
|
+
if (info === null) continue;
|
|
315
|
+
if (info.kind === KIND.ITEM) {
|
|
316
|
+
const item = trim(info.source);
|
|
317
|
+
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(item) && !names.includes(item)) names.push(item);
|
|
318
|
+
} else if (info.kind === KIND.SCRIPT || info.kind === KIND.GLOBAL_SCRIPT) scripts.push([e, info]);
|
|
319
|
+
}
|
|
320
|
+
// the scripts, as the kit runs them: the vars they declare are the
|
|
321
|
+
// session's, a chart-level script's functions the chart's helpers
|
|
322
|
+
const helpers = [];
|
|
323
|
+
for (const [e, info] of scripts) {
|
|
324
|
+
const found = realm.kit.script(info.source);
|
|
325
|
+
if (found === null) continue; // it does not parse: its evaluation's SyntaxError
|
|
326
|
+
const [functions, vars, refusal, head, tail] = found;
|
|
327
|
+
if (refusal !== undefined) {
|
|
328
|
+
chart.scripts[e] = refusal;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
chart.scripts[e] = [head, tail];
|
|
332
|
+
names.push(...vars);
|
|
333
|
+
if (info.kind === KIND.GLOBAL_SCRIPT && functions.length) helpers.push([info.source, functions]);
|
|
334
|
+
}
|
|
335
|
+
chart.Scope = realm.kit.shape(names, helpers);
|
|
336
|
+
this.charts[slot] = chart;
|
|
337
|
+
return TA_OK;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// ---- sessions ----
|
|
341
|
+
|
|
342
|
+
// The session at `slot` (its scope), with the
|
|
343
|
+
// evaluating chart set for In() and, with `charge`, a fuel unit charged.
|
|
344
|
+
session(slot, charge) {
|
|
345
|
+
const session = this.sessions[slot];
|
|
346
|
+
if (session === undefined) throw new Refused(TA_ERROR_STALE, 'no such session');
|
|
347
|
+
this.evaluating = this.meta[4 * slot];
|
|
348
|
+
this.current = slot;
|
|
349
|
+
if (charge) this.fn.ta_web_dm_charge(this.pointer, 1);
|
|
350
|
+
return session;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
chartOf(slot) { return this.charts[this.meta[4 * slot]]; }
|
|
354
|
+
|
|
355
|
+
grow(slot) {
|
|
356
|
+
if (4 * slot + 4 <= this.meta.length) return;
|
|
357
|
+
let length = this.meta.length;
|
|
358
|
+
while (length < 4 * slot + 4) length *= 2;
|
|
359
|
+
const meta = new Uint32Array(length);
|
|
360
|
+
meta.set(this.meta);
|
|
361
|
+
this.meta = meta;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// The strings of _event the datamodel keeps: the three types, then the
|
|
365
|
+
// I/O processors' types (every session of a host has the same ones).
|
|
366
|
+
keepStrings(types) {
|
|
367
|
+
if (this.strings !== null) return;
|
|
368
|
+
this.strings = [...TYPES, ...types];
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// The start's system variables: [sessionid, name, processors].
|
|
372
|
+
systemOf(words) {
|
|
373
|
+
const pointer = this.pointer;
|
|
374
|
+
const sessionid = this.text(words[3], words[4]), name = this.text(words[5], words[6]);
|
|
375
|
+
const processors = [];
|
|
376
|
+
for (let i = 0; i < words[7]; i++) {
|
|
377
|
+
this.fn.ta_web_dm_processor(pointer, i);
|
|
378
|
+
const w = this.words(4);
|
|
379
|
+
processors.push([this.text(w[0], w[1]), this.text(w[2], w[3])]);
|
|
380
|
+
}
|
|
381
|
+
return [sessionid, name, processors];
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
make(slot, chart, link, init) {
|
|
385
|
+
link.init = init;
|
|
386
|
+
return new chart.Scope(link);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
create(slot) {
|
|
390
|
+
const words = this.words(8);
|
|
391
|
+
const init = (BigInt(words[1]) | (BigInt(words[2]) << 32n));
|
|
392
|
+
const chart = this.charts[words[0]];
|
|
393
|
+
const value = init !== 0n ? this.values[Number(init & 0xFFFFFFFFn) - 1] : undefined;
|
|
394
|
+
if (init !== 0n) this.fn.ta_web_payload_drop(this.pointer, words[1], words[2]); // the init's reference is ours
|
|
395
|
+
if (chart === undefined) throw new Refused(TA_ERROR_INVALID, 'no such chart');
|
|
396
|
+
const system = this.systemOf(words);
|
|
397
|
+
this.keepStrings(system[2].map(([type]) => type));
|
|
398
|
+
const link = this.realm.kit.system(...system);
|
|
399
|
+
const session = this.make(slot, chart, link, value);
|
|
400
|
+
this.grow(slot);
|
|
401
|
+
this.meta.set([words[0], NIL, NIL, NIL], 4 * slot);
|
|
402
|
+
this.sessions[slot] = session;
|
|
403
|
+
return TA_OK;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
linkOf(session) { return session[this.realm.kit.LINK]; }
|
|
407
|
+
|
|
408
|
+
initData(slot) {
|
|
409
|
+
const declaration = this.module.word(0);
|
|
410
|
+
const session = this.session(slot, true);
|
|
411
|
+
const chart = this.chartOf(slot);
|
|
412
|
+
const data = chart.data[declaration];
|
|
413
|
+
if (data === undefined) throw new Error('no such <data>');
|
|
414
|
+
const init = this.linkOf(session).init;
|
|
415
|
+
let value;
|
|
416
|
+
if (init !== null && typeof init === 'object' && Object.hasOwn(init, data.id)) value = init[data.id];
|
|
417
|
+
else if (data.expression !== NIL) value = this.evaluate(session, data.expression);
|
|
418
|
+
session[data.id] = value;
|
|
419
|
+
return TA_OK;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// ---- _event ----
|
|
423
|
+
|
|
424
|
+
// The string of `text` kept at `index` of a chart's list: the one there
|
|
425
|
+
// when its text is the same, else a new one, kept when the place was empty.
|
|
426
|
+
kept(list, index, text) {
|
|
427
|
+
if (index === NIL) return text;
|
|
428
|
+
const kept = list[index];
|
|
429
|
+
if (kept === text) return kept;
|
|
430
|
+
if (kept === undefined) list[index] = text;
|
|
431
|
+
return text;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
bindEvent(slot) {
|
|
435
|
+
const w = this.words(18);
|
|
436
|
+
const session = this.session(slot, true);
|
|
437
|
+
const chart = this.chartOf(slot);
|
|
438
|
+
const optional = (at) => (w[at + 1] === 0 ? undefined : this.text(w[at], w[at + 1]));
|
|
439
|
+
const strings = this.strings ?? TYPES;
|
|
440
|
+
const data = (BigInt(w[14]) | (BigInt(w[15]) << 32n));
|
|
441
|
+
const fields = [
|
|
442
|
+
this.kept(chart.names, w[1], this.text(w[4], w[5])),
|
|
443
|
+
strings[w[0] % 3],
|
|
444
|
+
optional(6),
|
|
445
|
+
w[9] ? this.kept(chart.origins, w[2], this.text(w[8], w[9])) : undefined,
|
|
446
|
+
w[11] === 0 ? undefined : w[3] !== NIL ? this.kept(strings, 3 + w[3], this.text(w[10], w[11])) : this.text(w[10], w[11]),
|
|
447
|
+
w[13] ? this.kept(chart.invokeids, w[2], this.text(w[12], w[13])) : undefined,
|
|
448
|
+
data !== 0n ? this.values[Number(data & 0xFFFFFFFFn) - 1] : undefined,
|
|
449
|
+
optional(16),
|
|
450
|
+
];
|
|
451
|
+
const event = this.realm.kit.event(fields);
|
|
452
|
+
Object.defineProperty(session, '_event', { value: event, enumerable: true, configurable: true, writable: false });
|
|
453
|
+
session[this.realm.kit.LINK].init = undefined; // an event has come: the params were read by init_data
|
|
454
|
+
this.meta[4 * slot + 1] = w[1];
|
|
455
|
+
this.meta[4 * slot + 2] = w[2];
|
|
456
|
+
this.meta[4 * slot + 3] = w[3];
|
|
457
|
+
return TA_OK;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// ---- evaluations ----
|
|
461
|
+
|
|
462
|
+
evaluate(session, e) {
|
|
463
|
+
const chart = this.chartOf(this.current);
|
|
464
|
+
const info = this.expressionOf(chart, e);
|
|
465
|
+
if (info === null) throw new Error('no such expression');
|
|
466
|
+
if (info.kind === KIND.CONTENT) return this.content(info.source);
|
|
467
|
+
if (info.kind === KIND.SCRIPT || info.kind === KIND.GLOBAL_SCRIPT) {
|
|
468
|
+
if (chart.getters[e] === undefined && typeof chart.scripts[e] === 'string') chart.getters[e] = chart.scripts[e];
|
|
469
|
+
const [head, tail] = Array.isArray(chart.scripts[e]) ? chart.scripts[e] : SHARED_SCRIPT;
|
|
470
|
+
return this.compiled(chart.getters, e, () => head + info.source + tail)(session);
|
|
471
|
+
}
|
|
472
|
+
return this.compiled(chart.getters, e, () => SHARED_VALUE[0] + trimmed(info.source) + SHARED_VALUE[1])(session);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// The compiled function of `e` in `cache`, compiled in the realm at its
|
|
476
|
+
// first use; null kept when it did not compile, so it is not compiled
|
|
477
|
+
// again; a string is a refusal, thrown at each evaluation.
|
|
478
|
+
compiled(cache, e, source) {
|
|
479
|
+
const kept = cache[e];
|
|
480
|
+
if (kept === null) throw new this.realm.global.Error('the expression did not compile');
|
|
481
|
+
if (typeof kept === 'string') throw new this.realm.global.Error(kept);
|
|
482
|
+
if (kept !== undefined) return kept;
|
|
483
|
+
let made;
|
|
484
|
+
try {
|
|
485
|
+
made = this.realm.evaluate(source(), `<expr:${e}>`);
|
|
486
|
+
} catch (error) {
|
|
487
|
+
cache[e] = null;
|
|
488
|
+
throw error;
|
|
489
|
+
}
|
|
490
|
+
cache[e] = made;
|
|
491
|
+
return made;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
assign(session, location, value) {
|
|
495
|
+
const chart = this.chartOf(this.current);
|
|
496
|
+
const info = this.expressionOf(chart, location);
|
|
497
|
+
if (info === null) throw new Error('no such location');
|
|
498
|
+
let name = chart.leading[location];
|
|
499
|
+
if (name === undefined) chart.leading[location] = name = leadingName(info.source);
|
|
500
|
+
if (name === '' || !Object.hasOwn(session, name)) throw new this.realm.global.Error('an assignment to a location the session does not declare');
|
|
501
|
+
const fn = this.compiled(chart.setters, location, () => SHARED_SETTER[0] + trimmed(info.source) + SHARED_SETTER[1]);
|
|
502
|
+
fn(session, value);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
log(slot) {
|
|
506
|
+
const e = this.module.word(0);
|
|
507
|
+
let value = this.evaluate(this.session(slot, true), e);
|
|
508
|
+
if (value !== null && typeof value === 'object') {
|
|
509
|
+
try {
|
|
510
|
+
const json = JSON.stringify(value);
|
|
511
|
+
if (typeof json === 'string') value = json;
|
|
512
|
+
} catch {
|
|
513
|
+
// a cycle: its text instead
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
this.putText(`${value}`);
|
|
517
|
+
return TA_OK;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// ---- SCXML B.2: <content> and <data> text ----
|
|
521
|
+
|
|
522
|
+
content(text) {
|
|
523
|
+
const kit = this.realm.kit;
|
|
524
|
+
try {
|
|
525
|
+
return kit.parse(text); // JSON
|
|
526
|
+
} catch {
|
|
527
|
+
// not JSON
|
|
528
|
+
}
|
|
529
|
+
if (/^[ \t\n\r]*</.test(text)) {
|
|
530
|
+
const document = this.dom(text);
|
|
531
|
+
if (document !== undefined) return document;
|
|
532
|
+
}
|
|
533
|
+
return text.replace(/[ \t\n\r]+/g, ' ').replace(/^ | $/g, '');
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// The XML document of `text`, made with the kit's XML nodes from the
|
|
537
|
+
// tree txml read in C; undefined when the text is not well-formed XML.
|
|
538
|
+
dom(text) {
|
|
539
|
+
const module = this.module, pointer = this.pointer, scratch = this.scratch;
|
|
540
|
+
return module.scoped(scratch, () => {
|
|
541
|
+
const [at, length] = module.copyIn(scratch, encoder.encode(text));
|
|
542
|
+
module.setWord(0, at);
|
|
543
|
+
module.setWord(1, length);
|
|
544
|
+
const tree = this.fn.ta_web_xml_parse(pointer) >>> 2;
|
|
545
|
+
try {
|
|
546
|
+
const status = module.word(0) | 0;
|
|
547
|
+
if (status === TA_ERROR_NO_MEMORY) throw new Refused(TA_ERROR_NO_MEMORY, 'no memory for the XML content');
|
|
548
|
+
if (status !== TA_OK || tree === 0) return undefined;
|
|
549
|
+
const words = module.words();
|
|
550
|
+
let i = tree;
|
|
551
|
+
const read = () => words[i++];
|
|
552
|
+
const string = () => {
|
|
553
|
+
const p = read(), n = read();
|
|
554
|
+
return module.string(p, n);
|
|
555
|
+
};
|
|
556
|
+
const qualified = (prefix, local) => (prefix ? `${prefix}:${local}` : local);
|
|
557
|
+
const node = () => {
|
|
558
|
+
const kind = read();
|
|
559
|
+
const prefix = string(), name = string(), value = string();
|
|
560
|
+
const attributes = read(), children = read();
|
|
561
|
+
const entry = [kind, undefined, undefined, undefined, []];
|
|
562
|
+
if (kind === 1) {
|
|
563
|
+
entry[1] = qualified(prefix, name);
|
|
564
|
+
const list = {};
|
|
565
|
+
for (let a = 0; a < attributes; a++) {
|
|
566
|
+
const ap = string(), an = string(), av = string();
|
|
567
|
+
Object.defineProperty(list, qualified(ap, an), { value: av, writable: true, enumerable: true, configurable: true });
|
|
568
|
+
}
|
|
569
|
+
entry[3] = list;
|
|
570
|
+
} else {
|
|
571
|
+
entry[1] = kind === 3 ? '#text' : kind === 4 ? '#cdata-section' : kind === 8 ? '#comment' : name;
|
|
572
|
+
entry[2] = value;
|
|
573
|
+
}
|
|
574
|
+
for (let c = 0; c < children; c++) entry[4].push(node());
|
|
575
|
+
return entry;
|
|
576
|
+
};
|
|
577
|
+
const count = read();
|
|
578
|
+
const children = [];
|
|
579
|
+
for (let c = 0; c < count; c++) children.push(node());
|
|
580
|
+
return this.realm.kit.dom(children, text);
|
|
581
|
+
} finally {
|
|
582
|
+
this.fn.ta_web_xml_end(pointer);
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// ---- <foreach> ----
|
|
588
|
+
|
|
589
|
+
foreachBegin(slot) {
|
|
590
|
+
const [array, loop] = this.words(2);
|
|
591
|
+
const value = this.evaluate(this.session(slot, true), array);
|
|
592
|
+
if (!Array.isArray(value)) throw new this.realm.global.TypeError('<foreach>\'s array is not an array');
|
|
593
|
+
const items = value.slice(); // the loop walks the items it had
|
|
594
|
+
this.loops[loop] = items;
|
|
595
|
+
this.module.setWord(0, items.length);
|
|
596
|
+
return TA_OK;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
foreachItem(slot) {
|
|
600
|
+
const [loop, i, item, index] = this.words(4);
|
|
601
|
+
const session = this.session(slot, true);
|
|
602
|
+
const items = this.loops[loop];
|
|
603
|
+
if (items === undefined) throw new Refused(TA_ERROR_STALE, 'no such loop');
|
|
604
|
+
this.assign(session, item, items[i]);
|
|
605
|
+
if (index !== NIL) this.assign(session, index, i);
|
|
606
|
+
return TA_OK;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// ---- payloads ----
|
|
610
|
+
|
|
611
|
+
capture(slot) {
|
|
612
|
+
const [session, kind, namelistCount, paramCount, content, namelist, params] = this.words(7);
|
|
613
|
+
void kind;
|
|
614
|
+
const s = this.session(session, true);
|
|
615
|
+
const chart = this.chartOf(session);
|
|
616
|
+
const kit = this.realm.kit;
|
|
617
|
+
let captured;
|
|
618
|
+
if (content !== NIL) captured = kit.copy(this.evaluate(s, content));
|
|
619
|
+
else {
|
|
620
|
+
const names = [], values = [];
|
|
621
|
+
const words = () => this.module.words();
|
|
622
|
+
for (let i = 0; i < namelistCount; i++) {
|
|
623
|
+
const location = words()[(namelist >>> 2) + i];
|
|
624
|
+
const info = this.expressionOf(chart, location);
|
|
625
|
+
if (info === null) throw new Error('no such location');
|
|
626
|
+
names.push(trim(info.source));
|
|
627
|
+
values.push(this.evaluate(s, location));
|
|
628
|
+
}
|
|
629
|
+
for (let i = 0; i < paramCount; i++) {
|
|
630
|
+
const sid = words()[(params >>> 2) + 2 * i], e = words()[(params >>> 2) + 2 * i + 1];
|
|
631
|
+
const at = this.fn.ta_web_chart_string(this.pointer, chart.slot, sid) >>> 0;
|
|
632
|
+
names.push(this.module.string(at, this.module.word(0)));
|
|
633
|
+
values.push(this.evaluate(s, e));
|
|
634
|
+
}
|
|
635
|
+
captured = kit.record(names, values);
|
|
636
|
+
}
|
|
637
|
+
this.values[slot] = captured;
|
|
638
|
+
this.module.setWord(0, 1);
|
|
639
|
+
return TA_OK;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// A payload's value (to_wire's or the value table's CBOR) in the output.
|
|
643
|
+
payloadOut(slot, wire) {
|
|
644
|
+
try {
|
|
645
|
+
this.putValue(this.values[slot], wire);
|
|
646
|
+
} catch (error) {
|
|
647
|
+
if (error instanceof Refused) throw error;
|
|
648
|
+
if (error instanceof TinyactorsError) {
|
|
649
|
+
const status = error.status === 'TA_ERROR_NO_MEMORY' ? TA_ERROR_NO_MEMORY : wire ? TA_ERROR_INVALID : TA_ERROR_UNSUPPORTED;
|
|
650
|
+
this.fail(error);
|
|
651
|
+
return status;
|
|
652
|
+
}
|
|
653
|
+
throw error;
|
|
654
|
+
}
|
|
655
|
+
return TA_OK;
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// A payload from CBOR (from_wire, payload_deserialize): read, then copied
|
|
659
|
+
// into the host's realm.
|
|
660
|
+
fromWire(slot) {
|
|
661
|
+
const [at, length] = this.words(2);
|
|
662
|
+
let value;
|
|
663
|
+
try {
|
|
664
|
+
value = this.kit().copy(this.readValue(at, length));
|
|
665
|
+
} catch (error) {
|
|
666
|
+
this.fail(error);
|
|
667
|
+
return TA_ERROR_INVALID;
|
|
668
|
+
}
|
|
669
|
+
this.values[slot] = value;
|
|
670
|
+
this.module.setWord(0, 1);
|
|
671
|
+
return TA_OK;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
// A <data> variable for inspection (actor.context): the value itself as a
|
|
675
|
+
// payload, which core writes out with to_wire at once and releases.
|
|
676
|
+
variable(slot) {
|
|
677
|
+
const [session, at, length] = this.words(3);
|
|
678
|
+
const name = this.text(at, length);
|
|
679
|
+
const s = this.session(session, false);
|
|
680
|
+
const chart = this.chartOf(session);
|
|
681
|
+
if (!chart.data.some((d) => d.id === name)) throw new Refused(TA_ERROR_INVALID, 'no such variable');
|
|
682
|
+
const value = s[name];
|
|
683
|
+
this.module.setWord(0, value === undefined ? 0 : 1);
|
|
684
|
+
if (value !== undefined) this.values[slot] = value;
|
|
685
|
+
return TA_OK;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// The value of payload `handle` (a bigint) in the application's realm.
|
|
689
|
+
exported(handle) {
|
|
690
|
+
const value = this.values[Number(handle & 0xFFFFFFFFn) - 1];
|
|
691
|
+
if (value === null || (typeof value !== 'object' && typeof value !== 'function')) return value;
|
|
692
|
+
return application().exported(value);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
// A payload of the host's realm for a value the application gives: one
|
|
696
|
+
// reference, its handle. A value that cannot cross throws a
|
|
697
|
+
// TinyactorsError of the kit's status.
|
|
698
|
+
payload(value) {
|
|
699
|
+
const kit = this.kit();
|
|
700
|
+
let copy;
|
|
701
|
+
try {
|
|
702
|
+
copy = kit.copy(value);
|
|
703
|
+
} catch (error) {
|
|
704
|
+
const refusal = kit.refusal(error);
|
|
705
|
+
if (refusal === undefined) throw error;
|
|
706
|
+
throw new TinyactorsError(String(error?.message ?? error), { status: refusal === 'invalid' ? 'TA_ERROR_INVALID' : 'TA_ERROR_UNSUPPORTED' });
|
|
707
|
+
}
|
|
708
|
+
const slot = this.fn.ta_web_payload_new(this.pointer);
|
|
709
|
+
if (slot === 0) throw new TinyactorsError('no memory for the payload', { status: 'TA_ERROR_NO_MEMORY' });
|
|
710
|
+
this.values[slot - 1] = copy;
|
|
711
|
+
return this.module.word64(0);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// ---- hibernation ----
|
|
715
|
+
|
|
716
|
+
// A session's variables as hibernation writes them: [names, values].
|
|
717
|
+
stored(session) { return this.realm.kit.stored(session); }
|
|
718
|
+
|
|
719
|
+
// _event as a blob holds it: its eight fields, then core's ids; null
|
|
720
|
+
// when no event came yet.
|
|
721
|
+
eventOut(session, slot) {
|
|
722
|
+
const event = session._event;
|
|
723
|
+
if (event === null || typeof event !== 'object') return null;
|
|
724
|
+
const fields = EVENT_FIELDS.map((field) => event[field]);
|
|
725
|
+
fields.push(this.meta[4 * slot + 1], this.meta[4 * slot + 2], this.meta[4 * slot + 3]);
|
|
726
|
+
return fields;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// The value-table refusal of a session's state as the reason the host
|
|
730
|
+
// gives: which variable, and why.
|
|
731
|
+
reason(what, name, error) {
|
|
732
|
+
const message = error instanceof Error || (error !== null && typeof error === 'object' && 'message' in error)
|
|
733
|
+
? String(error.message) : 'it is not a value of the value table';
|
|
734
|
+
return name !== undefined ? `${what}${name}' cannot be stored: ${message}` : `${what} cannot be stored: ${message}`;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// Writes one value's CBOR: [pointer, length] in the scratch; a Refused
|
|
738
|
+
// naming it when the value table refuses it.
|
|
739
|
+
written(value, what, name) {
|
|
740
|
+
try {
|
|
741
|
+
return cbor.write(this.module, this.scratch, value, { realms: true });
|
|
742
|
+
} catch (error) {
|
|
743
|
+
if (error instanceof TinyactorsError && error.status === 'TA_ERROR_NO_MEMORY') throw new Refused(TA_ERROR_NO_MEMORY, error.message);
|
|
744
|
+
const refused = new Refused(TA_ERROR_UNSUPPORTED, this.reason(what, name, error));
|
|
745
|
+
refused.reason = refused.message;
|
|
746
|
+
throw refused;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
serialize(slot) {
|
|
751
|
+
this.state.reason = '';
|
|
752
|
+
const session = this.session(slot, false);
|
|
753
|
+
const [names, values] = this.stored(session);
|
|
754
|
+
const vars = {};
|
|
755
|
+
for (let i = 0; i < names.length; i++) {
|
|
756
|
+
Object.defineProperty(vars, names[i], { value: values[i], enumerable: true, writable: true, configurable: true });
|
|
757
|
+
}
|
|
758
|
+
let event;
|
|
759
|
+
try {
|
|
760
|
+
event = this.eventOut(session, slot);
|
|
761
|
+
} catch (error) {
|
|
762
|
+
const refused = new Refused(TA_ERROR_UNSUPPORTED, this.reason('_event', undefined, error));
|
|
763
|
+
refused.reason = refused.message;
|
|
764
|
+
throw refused;
|
|
765
|
+
}
|
|
766
|
+
const module = this.module, scratch = this.scratch;
|
|
767
|
+
module.scoped(scratch, () => {
|
|
768
|
+
let written;
|
|
769
|
+
try {
|
|
770
|
+
written = cbor.write(module, scratch, { vars, event }, { realms: true });
|
|
771
|
+
} catch (error) {
|
|
772
|
+
if (error instanceof TinyactorsError && error.status === 'TA_ERROR_NO_MEMORY') throw new Refused(TA_ERROR_NO_MEMORY, error.message);
|
|
773
|
+
// which value: the variable it names, else _event
|
|
774
|
+
for (let i = 0; i < names.length; i++) module.scoped(scratch, () => this.written(values[i], 'variable \'', names[i]));
|
|
775
|
+
module.scoped(scratch, () => this.written(event, '_event', undefined));
|
|
776
|
+
throw error;
|
|
777
|
+
}
|
|
778
|
+
this.putBytes(written[0], written[1]);
|
|
779
|
+
});
|
|
780
|
+
return TA_OK;
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// A session from a blob serialize wrote, for its chart now: its
|
|
784
|
+
// variables (all of them kept in its link's init until the first event
|
|
785
|
+
// or migrate takes them) and its _event.
|
|
786
|
+
deserialize(slot) {
|
|
787
|
+
const words = this.words(8);
|
|
788
|
+
const chart = this.charts[words[0]];
|
|
789
|
+
if (chart === undefined) throw new Refused(TA_ERROR_INVALID, 'no such chart');
|
|
790
|
+
const state = this.readValue(words[1], words[2]);
|
|
791
|
+
if (state === null || typeof state !== 'object' || state.vars === null || typeof state.vars !== 'object') {
|
|
792
|
+
throw new Refused(TA_ERROR_INVALID, 'not a blob of the native datamodel');
|
|
793
|
+
}
|
|
794
|
+
const system = this.systemOf(words);
|
|
795
|
+
this.keepStrings(system[2].map(([type]) => type));
|
|
796
|
+
const session = this.revive(slot, { chart, link: this.realm.kit.system(...system), vars: state.vars, init: state.vars });
|
|
797
|
+
this.eventIn(session, slot, chart, state.event);
|
|
798
|
+
return TA_OK;
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// A session made again (a wake, a restore): its variables set, the blob's
|
|
802
|
+
// all kept in its link's init for migrate.
|
|
803
|
+
revive(slot, { chart, link, vars, init }) {
|
|
804
|
+
const session = this.make(slot, chart, link, init);
|
|
805
|
+
this.grow(slot);
|
|
806
|
+
this.meta.set([chart.slot, NIL, NIL, NIL], 4 * slot);
|
|
807
|
+
this.realm.kit.restore(session, vars);
|
|
808
|
+
this.sessions[slot] = session;
|
|
809
|
+
return session;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// _event from the fields a blob holds, its strings shared with the
|
|
813
|
+
// chart's (by core's ids, when the text is the same).
|
|
814
|
+
eventIn(session, slot, chart, fields) {
|
|
815
|
+
if (fields === null || fields === undefined) return;
|
|
816
|
+
if (!Array.isArray(fields) || fields.length !== 11) throw new Refused(TA_ERROR_INVALID, 'not a blob of the native datamodel');
|
|
817
|
+
const id = (v) => (typeof v === 'number' && Number.isInteger(v) && v >= 0 && v <= NIL ? v : NIL);
|
|
818
|
+
const [nameID, invoke, origintype] = [id(fields[8]), id(fields[9]), id(fields[10])];
|
|
819
|
+
const made = fields.slice(0, 8);
|
|
820
|
+
const keep = (list, index, text) => (typeof text === 'string' ? this.kept(list, index, text) : text);
|
|
821
|
+
made[0] = keep(chart.names, nameID, made[0]);
|
|
822
|
+
const types = this.strings ?? TYPES;
|
|
823
|
+
for (let i = 0; i < 3; i++) if (types[i] === made[1]) made[1] = types[i];
|
|
824
|
+
made[3] = keep(chart.origins, invoke, made[3]);
|
|
825
|
+
made[4] = origintype < NIL - 3 ? keep(types, 3 + origintype, made[4]) : made[4];
|
|
826
|
+
made[5] = keep(chart.invokeids, invoke, made[5]);
|
|
827
|
+
if (made[6] !== undefined) made[6] = this.realm.kit.copy(made[6]);
|
|
828
|
+
const event = this.realm.kit.event(made);
|
|
829
|
+
Object.defineProperty(session, '_event', { value: event, enumerable: true, configurable: true, writable: false });
|
|
830
|
+
this.meta[4 * slot + 1] = nameID;
|
|
831
|
+
this.meta[4 * slot + 2] = invoke;
|
|
832
|
+
this.meta[4 * slot + 3] = origintype;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// After deserialize, for a blob of an older version of the chart: the
|
|
836
|
+
// old bindings (exactly the old chart's variables) as one payload.
|
|
837
|
+
migrate(slot) {
|
|
838
|
+
const session = this.session(this.module.word(0), true);
|
|
839
|
+
const old = this.realm.kit.upgrade(session);
|
|
840
|
+
if (old !== null && typeof old === 'object') {
|
|
841
|
+
this.values[slot] = this.realm.kit.copy(old);
|
|
842
|
+
this.module.setWord(0, 1);
|
|
843
|
+
} else this.module.setWord(0, 0);
|
|
844
|
+
return TA_OK;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// ---- snapshots ----
|
|
848
|
+
|
|
849
|
+
// The native datamodel's state for a host snapshot: the tables'
|
|
850
|
+
// namespaces and every row, each value as CBOR bytes of its own (the
|
|
851
|
+
// addon's format). Throws a TinyactorsError when a session's state
|
|
852
|
+
// cannot be stored.
|
|
853
|
+
snapshot() {
|
|
854
|
+
const module = this.module, pointer = this.pointer, scratch = this.scratch;
|
|
855
|
+
const layout = (which) => {
|
|
856
|
+
const status = this.fn.ta_web_dm_layout(pointer, which);
|
|
857
|
+
if (status !== TA_OK) throw new TinyactorsError('no memory for the snapshot', { status: 'TA_ERROR_NO_MEMORY' });
|
|
858
|
+
return module.copyOut(module.word(0), module.word(1));
|
|
859
|
+
};
|
|
860
|
+
const rows = (which) => {
|
|
861
|
+
for (let room = 1024; ; room *= 4) {
|
|
862
|
+
const out = module.scoped(scratch, () => {
|
|
863
|
+
const at = module.room(scratch, 12 * room);
|
|
864
|
+
module.setWord(0, at);
|
|
865
|
+
module.setWord(1, room);
|
|
866
|
+
const n = this.fn.ta_web_dm_rows(pointer, which);
|
|
867
|
+
if (n === room) return null; // perhaps more: again with more room
|
|
868
|
+
const words = module.words(), base = at >>> 2, found = [];
|
|
869
|
+
for (let i = 0; i < n; i++) found.push([BigInt(words[base + 3 * i]) | (BigInt(words[base + 3 * i + 1]) << 32n), words[base + 3 * i + 2]]);
|
|
870
|
+
return found;
|
|
871
|
+
});
|
|
872
|
+
if (out !== null) return out;
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
const bytes = (value, what, name) => module.scoped(scratch, () => {
|
|
876
|
+
const [at, length] = this.writtenSnapshot(value, what, name);
|
|
877
|
+
return module.copyOut(at, length);
|
|
878
|
+
});
|
|
879
|
+
const state = { sessions: layout(0), payloads: layout(1), loops: layout(2) };
|
|
880
|
+
state.rows = rows(0).map(([handle]) => this.snapshotSession(handle, bytes));
|
|
881
|
+
state.values = rows(1).map(([handle, count]) => [handle, count, bytes(this.values[Number(handle & 0xFFFFFFFFn) - 1], 'a payload')]);
|
|
882
|
+
state.items = rows(2).map(([handle]) => [handle, bytes(this.loops[Number(handle & 0xFFFFFFFFn) - 1], 'a <foreach>\'s items')]);
|
|
883
|
+
return state;
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
writtenSnapshot(value, what, name) {
|
|
887
|
+
try {
|
|
888
|
+
return cbor.write(this.module, this.scratch, value, { realms: true });
|
|
889
|
+
} catch (error) {
|
|
890
|
+
if (error instanceof TinyactorsError && error.status === 'TA_ERROR_NO_MEMORY') throw error;
|
|
891
|
+
throw new TinyactorsError(this.reason(what, name, error), { status: 'TA_ERROR_UNSUPPORTED' });
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// One session row: [handle, segment, chart index, {name: bytes}, _event
|
|
896
|
+
// as bytes, the link's [sessionid, name, processors] as bytes, its init
|
|
897
|
+
// as bytes or null].
|
|
898
|
+
snapshotSession(handle, bytes) {
|
|
899
|
+
const slot = Number(handle & 0xFFFFFFFFn) - 1;
|
|
900
|
+
const session = this.sessions[slot];
|
|
901
|
+
const chart = this.chartOf(slot);
|
|
902
|
+
if (session === undefined || chart === undefined) throw new TinyactorsError('a session without its chart', { status: 'TA_ERROR_INVALID' });
|
|
903
|
+
const [names, values] = this.stored(session);
|
|
904
|
+
const vars = {};
|
|
905
|
+
for (let i = 0; i < names.length; i++) {
|
|
906
|
+
Object.defineProperty(vars, names[i], { value: bytes(values[i], 'variable \'', names[i]), enumerable: true, writable: true, configurable: true });
|
|
907
|
+
}
|
|
908
|
+
let event;
|
|
909
|
+
try {
|
|
910
|
+
event = this.eventOut(session, slot);
|
|
911
|
+
} catch (error) {
|
|
912
|
+
throw new TinyactorsError(this.reason('_event', undefined, error), { status: 'TA_ERROR_UNSUPPORTED' });
|
|
913
|
+
}
|
|
914
|
+
const linked = this.realm.kit.linked(session);
|
|
915
|
+
return [
|
|
916
|
+
handle, chart.segment, chart.index, vars, bytes(event, '_event'),
|
|
917
|
+
bytes([linked[0], linked[1], linked[2]], 'the system variables'),
|
|
918
|
+
linked[3] === undefined ? null : bytes(linked[3], 'the params'),
|
|
919
|
+
];
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
// The state a snapshot holds back into a fresh datamodel: the tables'
|
|
923
|
+
// namespaces, then every row at its handle (a row twice, or one the
|
|
924
|
+
// tables do not hold, refused).
|
|
925
|
+
restore(state) {
|
|
926
|
+
const module = this.module, pointer = this.pointer, scratch = this.scratch;
|
|
927
|
+
const invalid = (message) => new TinyactorsError(message, { status: 'TA_ERROR_INVALID' });
|
|
928
|
+
const keys = ['sessions', 'payloads', 'loops', 'rows', 'values', 'items'];
|
|
929
|
+
if (state === null || typeof state !== 'object' || Array.isArray(state) || Object.keys(state).join() !== keys.join()) {
|
|
930
|
+
throw invalid('not a snapshot of the native datamodel');
|
|
931
|
+
}
|
|
932
|
+
const bytesOf = (value) => {
|
|
933
|
+
if (!(value instanceof Uint8Array)) throw invalid('not a snapshot of the native datamodel');
|
|
934
|
+
return value;
|
|
935
|
+
};
|
|
936
|
+
const valueOf = (value) => module.scoped(scratch, () => {
|
|
937
|
+
const [at, length] = module.copyIn(scratch, bytesOf(value));
|
|
938
|
+
return cbor.read(module, scratch, at, length);
|
|
939
|
+
});
|
|
940
|
+
for (const [which, key] of [[0, 'sessions'], [1, 'payloads'], [2, 'loops']]) {
|
|
941
|
+
module.scoped(scratch, () => {
|
|
942
|
+
const [at, length] = module.copyIn(scratch, bytesOf(state[key]));
|
|
943
|
+
module.setWord(0, at);
|
|
944
|
+
module.setWord(1, length);
|
|
945
|
+
const status = this.fn.ta_web_dm_restore(pointer, which);
|
|
946
|
+
if (status === TA_ERROR_STATE) throw new TinyactorsError('the native datamodel is not a fresh one', { status: 'TA_ERROR_STATE' });
|
|
947
|
+
if (status !== TA_OK) throw new TinyactorsError('a table of the snapshot does not restore', { status: module.statusName(status) });
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
const handleOf = (value) => {
|
|
951
|
+
if (typeof value === 'number' && Number.isSafeInteger(value) && value > 0) return BigInt(value);
|
|
952
|
+
if (typeof value === 'bigint' && value > 0n) return value;
|
|
953
|
+
throw invalid('not a snapshot of the native datamodel');
|
|
954
|
+
};
|
|
955
|
+
const fill = (which, handle, value, what) => {
|
|
956
|
+
module.setWord64(0, handle);
|
|
957
|
+
module.setWord(2, value);
|
|
958
|
+
if (this.fn.ta_web_dm_fill(pointer, which) !== TA_OK) throw invalid(what);
|
|
959
|
+
};
|
|
960
|
+
const rows = state.rows, values = state.values, items = state.items;
|
|
961
|
+
if (!Array.isArray(rows) || !Array.isArray(values) || !Array.isArray(items)) throw invalid('not a snapshot of the native datamodel');
|
|
962
|
+
this.ensureRealm();
|
|
963
|
+
const kit = this.realm.kit;
|
|
964
|
+
for (const row of rows) {
|
|
965
|
+
if (!Array.isArray(row) || row.length !== 7) throw invalid('not a session of a snapshot');
|
|
966
|
+
const handle = handleOf(row[0]);
|
|
967
|
+
const chart = this.charts.find((c) => c !== undefined && c.segment === row[1] && c.index === row[2]);
|
|
968
|
+
if (chart === undefined) throw invalid('a session whose chart the restored host does not have');
|
|
969
|
+
fill(0, handle, chart.slot, 'a session the snapshot\'s tables do not hold, or twice');
|
|
970
|
+
const slot = Number(handle & 0xFFFFFFFFn) - 1;
|
|
971
|
+
const system = valueOf(row[5]);
|
|
972
|
+
if (!Array.isArray(system) || system.length !== 3 || !Array.isArray(system[2])) throw invalid('not a session of a snapshot');
|
|
973
|
+
this.keepStrings(system[2].map((pair) => pair[0]));
|
|
974
|
+
const init = row[6] === null ? undefined : kit.copy(valueOf(row[6]));
|
|
975
|
+
if (row[3] === null || typeof row[3] !== 'object') throw invalid('not a session of a snapshot');
|
|
976
|
+
const vars = {};
|
|
977
|
+
for (const name of Object.keys(row[3])) {
|
|
978
|
+
Object.defineProperty(vars, name, { value: valueOf(row[3][name]), enumerable: true, writable: true, configurable: true });
|
|
979
|
+
}
|
|
980
|
+
const session = this.revive(slot, { chart, link: kit.system(system[0], system[1], system[2]), vars, init });
|
|
981
|
+
this.eventIn(session, slot, chart, valueOf(row[4]));
|
|
982
|
+
}
|
|
983
|
+
for (const row of values) {
|
|
984
|
+
if (!Array.isArray(row) || row.length !== 3) throw invalid('a payload the snapshot\'s tables do not hold, or twice');
|
|
985
|
+
const handle = handleOf(row[0]);
|
|
986
|
+
const count = row[1];
|
|
987
|
+
if (typeof count !== 'number' || !Number.isInteger(count) || count < 1 || count > NIL) throw invalid('a payload the snapshot\'s tables do not hold, or twice');
|
|
988
|
+
fill(1, handle, count, 'a payload the snapshot\'s tables do not hold, or twice');
|
|
989
|
+
this.values[Number(handle & 0xFFFFFFFFn) - 1] = kit.copy(valueOf(row[2]));
|
|
990
|
+
}
|
|
991
|
+
for (const row of items) {
|
|
992
|
+
if (!Array.isArray(row) || row.length !== 2) throw invalid('a <foreach> the snapshot\'s tables do not hold, or twice');
|
|
993
|
+
const handle = handleOf(row[0]);
|
|
994
|
+
fill(2, handle, 0, 'a <foreach> the snapshot\'s tables do not hold, or twice');
|
|
995
|
+
const slot = Number(handle & 0xFFFFFFFFn) - 1;
|
|
996
|
+
if (this.loops[slot] !== undefined) throw invalid('a <foreach> the snapshot\'s tables do not hold, or twice');
|
|
997
|
+
const value = kit.copy(valueOf(row[1]));
|
|
998
|
+
if (!Array.isArray(value)) throw invalid('a <foreach>\'s items are not an array');
|
|
999
|
+
this.loops[slot] = value;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|