@tinyactors/wasm 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -6
- package/index.d.ts +899 -590
- package/index.mjs +3 -4
- package/init.mjs +0 -1
- package/lib/checks.mjs +3 -1
- package/lib/compile.mjs +27 -16
- package/lib/convenience.mjs +815 -101
- package/lib/datamodel.mjs +59 -398
- package/lib/direct.mjs +6 -4
- package/lib/errors.mjs +28 -0
- package/lib/host.mjs +191 -34
- package/lib/http.mjs +19 -0
- package/lib/realm-script.mjs +1 -1
- package/lib/version.mjs +5 -0
- package/package.json +1 -1
- package/tinyactors.wasm +0 -0
package/lib/datamodel.mjs
CHANGED
|
@@ -6,40 +6,16 @@
|
|
|
6
6
|
//
|
|
7
7
|
// Rows are the host's tables in C (charts, sessions, payloads, loops); the
|
|
8
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
|
|
10
|
-
// value, a loop's items.
|
|
9
|
+
// chart's record, a session's scope, a payload's value, a loop's items.
|
|
11
10
|
//
|
|
12
|
-
// Where a chart evaluates
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
// - a declaring chart: one session function per chart, made at attach
|
|
21
|
-
// from its chart-level scripts' text with the realm's Function (the
|
|
22
|
-
// engine compiles it; no parser of ours), a generator called once per
|
|
23
|
-
// session, whose bindings are the session's: its `var`, `function`,
|
|
24
|
-
// `let`, `const` and `class` declarations, the <data> ids, the names a
|
|
25
|
-
// <foreach> binds and the vars its scripts of executable content
|
|
26
|
-
// declare. It stops before each chart-level script and after the last
|
|
27
|
-
// one, and at each stop runs the session's evaluations as direct evals
|
|
28
|
-
// in its own scope, so closures and lexical bindings work as in the
|
|
29
|
-
// addon's context per session, at a few hundred bytes a session.
|
|
30
|
-
//
|
|
31
|
-
// Which kind a chart is, and the names it declares, is asked of the engine
|
|
32
|
-
// at attach: a candidate name (an identifier of the script's text) is
|
|
33
|
-
// declared when the script followed by `let <name>` does not parse, and
|
|
34
|
-
// declared lexically when the script followed by `var <name>` does not
|
|
35
|
-
// either. Where the session function cannot do what the addon's context
|
|
36
|
-
// does, it refuses at attach or reports, never differs silently:
|
|
37
|
-
// declaring `eval`, `arguments` or a `__tinyactors_` name, a chart-level
|
|
38
|
-
// script with a 'use strict' directive, or a strict script of executable
|
|
39
|
-
// content that declares a var, refuse the chart; a chart-level script that
|
|
40
|
-
// throws stops the ones after it (the session's error.execution says so);
|
|
41
|
-
// an undeclared assignment in a sloppy script lands on the host's realm,
|
|
42
|
-
// and hibernation and snapshots refuse naming it.
|
|
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.
|
|
43
19
|
import * as cbor from './cbor.mjs';
|
|
44
20
|
import { TinyactorsError } from './errors.mjs';
|
|
45
21
|
import { application, makeRealm } from './realms.mjs';
|
|
@@ -59,26 +35,15 @@ const FAILURE_NONE = 0, FAILURE_EXCEPTION = 1;
|
|
|
59
35
|
const KIND = { CONDITION: 1, VALUE: 2, LOCATION: 3, SCRIPT: 4, STRING: 5, CONTENT: 6, ITEM: 7, GLOBAL_SCRIPT: 8 };
|
|
60
36
|
const TYPES = ['platform', 'internal', 'external'];
|
|
61
37
|
const EVENT_FIELDS = ['name', 'type', 'sendid', 'origin', 'origintype', 'invokeid', 'data', 'raw'];
|
|
62
|
-
const PREFIX = '__tinyactors_';
|
|
63
|
-
const IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
64
|
-
// Candidate names of a script: what may be an identifier in its text.
|
|
65
|
-
const CANDIDATES = /[\p{ID_Start}$_][\p{ID_Continue}$]*/gu;
|
|
66
38
|
const SPACE = /[ \t\n\r]/;
|
|
67
39
|
|
|
68
40
|
// The shapes an expression is compiled in (the addon's expressions.c).
|
|
69
41
|
const SHARED_VALUE = ["(function(__s){with(__s){return (function(){'use strict';return (\n", "\n);})();}})"];
|
|
70
42
|
const SHARED_SCRIPT = ["(function(__s){with(__s){(function(){'use strict';\n", '\n})();}})'];
|
|
71
43
|
const SHARED_SETTER = ["(function(__s,__v){with(__s){(function(){'use strict';(\n", '\n) = __v;})();}})'];
|
|
72
|
-
const OWN_VALUE = ["(function(){'use strict';return (\n", '\n);})'];
|
|
73
|
-
const OWN_SETTER = ["(function(__tinyactors_v){'use strict';(\n", '\n) = __tinyactors_v;})'];
|
|
74
44
|
|
|
75
45
|
const encoder = new TextEncoder();
|
|
76
46
|
|
|
77
|
-
function strictDirective(source) {
|
|
78
|
-
const match = /^[ \t\n\r]*(['"])use strict\1/.exec(source);
|
|
79
|
-
return match !== null;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
47
|
// A value's source without a trailing `;` (expr="new Foo();").
|
|
83
48
|
function trimmed(source) {
|
|
84
49
|
let end = source.length;
|
|
@@ -102,54 +67,6 @@ class Refused extends Error {
|
|
|
102
67
|
}
|
|
103
68
|
}
|
|
104
69
|
|
|
105
|
-
// ---- a declaring chart's session function ------------------------------------------------------
|
|
106
|
-
|
|
107
|
-
// One stop of the session's generator: it runs each evaluation it is given
|
|
108
|
-
// as a direct eval in the generator's own scope, until it is told to go on
|
|
109
|
-
// to the next chart-level script.
|
|
110
|
-
const STOP = 'for (;;) { __tinyactors_in = yield __tinyactors_out; if (__tinyactors_in === __tinyactors_session.next) break; ' +
|
|
111
|
-
'try { __tinyactors_out = [true, eval(__tinyactors_in)]; } catch (__tinyactors_e) { __tinyactors_out = [false, __tinyactors_e]; } }';
|
|
112
|
-
|
|
113
|
-
// The factory of a declaring chart's sessions: called with a session's
|
|
114
|
-
// scope (its system variables and _event, read through `with`), it answers
|
|
115
|
-
// the session's generator function.
|
|
116
|
-
function factorySource({ vars, names, scripts }) {
|
|
117
|
-
const cases = names.map((name) => `case ${JSON.stringify(name)}: `);
|
|
118
|
-
const get = names.map((name, i) => `${cases[i]}return ${name};`).join(' ');
|
|
119
|
-
const set = names.map((name, i) => `${cases[i]}${name} = __tinyactors_v; return true;`).join(' ');
|
|
120
|
-
const body = [
|
|
121
|
-
'let arguments;',
|
|
122
|
-
vars.length ? `var ${vars.join(', ')};` : '',
|
|
123
|
-
`function __tinyactors_get(__tinyactors_n) { switch (__tinyactors_n) { ${get} } return __tinyactors_session.none; }`,
|
|
124
|
-
`function __tinyactors_set(__tinyactors_n, __tinyactors_v) { switch (__tinyactors_n) { ${set} } return false; }`,
|
|
125
|
-
'function* __tinyactors_loop() { var __tinyactors_in = yield; for (;;) { var __tinyactors_out; ' +
|
|
126
|
-
'try { __tinyactors_out = [true, eval(__tinyactors_in)]; } catch (__tinyactors_e) { __tinyactors_out = [false, __tinyactors_e]; } ' +
|
|
127
|
-
'__tinyactors_in = yield __tinyactors_out; } }',
|
|
128
|
-
'__tinyactors_session.get = __tinyactors_get; __tinyactors_session.set = __tinyactors_set;',
|
|
129
|
-
'__tinyactors_session.loop = __tinyactors_loop(); __tinyactors_session.loop.next();',
|
|
130
|
-
'var __tinyactors_in, __tinyactors_out;',
|
|
131
|
-
STOP,
|
|
132
|
-
];
|
|
133
|
-
for (const script of scripts) body.push(script, STOP);
|
|
134
|
-
return `with (__tinyactors_system) return function* (__tinyactors_session) {\n${body.join('\n')}\n};`;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// A declaring session: its scope (the system variables and _event, read
|
|
138
|
-
// through `with`), its generator, and the handle the generator fills (get,
|
|
139
|
-
// set, and the loop it runs on after a chart-level script threw).
|
|
140
|
-
class Declaring {
|
|
141
|
-
constructor(scope, handle, generator) {
|
|
142
|
-
this.scope = scope;
|
|
143
|
-
this.handle = handle;
|
|
144
|
-
this.generator = generator;
|
|
145
|
-
this.stop = 0;
|
|
146
|
-
this.dead = false;
|
|
147
|
-
this.getters = undefined;
|
|
148
|
-
this.setters = undefined;
|
|
149
|
-
this.odd = undefined;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
70
|
// ---- the datamodel -----------------------------------------------------------------------------
|
|
154
71
|
|
|
155
72
|
export class NativeDatamodel {
|
|
@@ -380,7 +297,7 @@ export class NativeDatamodel {
|
|
|
380
297
|
const realm = this.ensureRealm();
|
|
381
298
|
const chart = {
|
|
382
299
|
slot, index, segment, binding, expressions: [], getters: [], setters: [], names: [], invokeids: [], origins: [],
|
|
383
|
-
leading: [],
|
|
300
|
+
leading: [], scripts: [], Scope: undefined, data: [],
|
|
384
301
|
};
|
|
385
302
|
const pointer = this.pointer;
|
|
386
303
|
const dataCount = this.fn.ta_web_chart_data_count(pointer, slot);
|
|
@@ -400,104 +317,29 @@ export class NativeDatamodel {
|
|
|
400
317
|
if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(item) && !names.includes(item)) names.push(item);
|
|
401
318
|
} else if (info.kind === KIND.SCRIPT || info.kind === KIND.GLOBAL_SCRIPT) scripts.push([e, info]);
|
|
402
319
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
this.charts[slot] = chart;
|
|
407
|
-
return TA_OK;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
// Whether `source` parses as a function body of the realm, nothing run.
|
|
411
|
-
parses(source) {
|
|
412
|
-
try {
|
|
413
|
-
new this.realm.Function(source); // eslint-disable-line no-new
|
|
414
|
-
return true;
|
|
415
|
-
} catch (error) {
|
|
416
|
-
if (error instanceof this.realm.global.SyntaxError) return false;
|
|
417
|
-
throw error;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
// A declaring chart's session function and what it declares, or null for
|
|
422
|
-
// a chart that declares nothing (the shared path). Throws a Refused for
|
|
423
|
-
// a chart it cannot run as the addon does.
|
|
424
|
-
declares(chart, scripts, names) {
|
|
425
|
-
const global = []; // [e, source] of the chart-level scripts that parse
|
|
426
|
-
const lexical = new Set(), declared = new Set(), execVars = new Set();
|
|
427
|
-
let refused = null;
|
|
428
|
-
const refuse = (message) => {
|
|
429
|
-
if (refused === null) refused = message;
|
|
430
|
-
};
|
|
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 = [];
|
|
431
323
|
for (const [e, info] of scripts) {
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
const
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
for (const name of candidates) {
|
|
439
|
-
if (!this.parses(`let ${name};`) && name !== 'eval' && name !== 'arguments') continue; // not a binding name
|
|
440
|
-
if (this.parses(`${run}\n;let ${name};`)) continue; // not declared at the function's level
|
|
441
|
-
if (name === 'eval' || name === 'arguments' || name.startsWith(PREFIX)) {
|
|
442
|
-
refuse(`the chart's scripts declare ${name}, which @tinyactors/wasm's native datamodel cannot give a session ` +
|
|
443
|
-
'(it runs each session\'s evaluations through a direct eval): rename it');
|
|
444
|
-
continue;
|
|
445
|
-
}
|
|
446
|
-
if (top) {
|
|
447
|
-
declared.add(name);
|
|
448
|
-
if (!this.parses(`${run}\n;var ${name};`)) lexical.add(name);
|
|
449
|
-
} else if (strict) {
|
|
450
|
-
refuse(`a script of executable content that starts with 'use strict' declares var ${name}, which ` +
|
|
451
|
-
'@tinyactors/wasm\'s native datamodel cannot keep between its runs: declare it in a <data> or a chart-level script');
|
|
452
|
-
} else execVars.add(name);
|
|
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;
|
|
453
330
|
}
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
'for it alone (its chart-level scripts are one function\'s body): leave the directive out');
|
|
458
|
-
}
|
|
459
|
-
global.push([e, source]);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if (refused === null && declared.size === 0 && execVars.size === 0) return null;
|
|
463
|
-
if (refused !== null) throw new Refused(TA_ERROR_INVALID, refused);
|
|
464
|
-
const valid = (name) => IDENTIFIER.test(name) || /^[\p{ID_Start}$_][\p{ID_Continue}$]*$/u.test(name);
|
|
465
|
-
const vars = [...new Set([...names.filter(valid), ...execVars])].filter((name) => !lexical.has(name));
|
|
466
|
-
for (const name of vars) {
|
|
467
|
-
if (name === 'eval' || name === 'arguments' || name.startsWith(PREFIX)) {
|
|
468
|
-
throw new Refused(TA_ERROR_INVALID, `the chart declares ${name} (a <data> or a <foreach>), which @tinyactors/wasm's ` +
|
|
469
|
-
'native datamodel cannot give a session: rename it');
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
const all = [...new Set([...vars, ...declared])];
|
|
473
|
-
let factory;
|
|
474
|
-
try {
|
|
475
|
-
factory = this.realm.Function('__tinyactors_system', factorySource({ vars, names: all, scripts: global.map(([, s]) => s) }));
|
|
476
|
-
} catch (error) {
|
|
477
|
-
throw new Refused(TA_ERROR_INVALID, `the chart's scripts cannot be one session function: ${error}`);
|
|
331
|
+
chart.scripts[e] = [head, tail];
|
|
332
|
+
names.push(...vars);
|
|
333
|
+
if (info.kind === KIND.GLOBAL_SCRIPT && functions.length) helpers.push([info.source, functions]);
|
|
478
334
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
remade: new Set(), odd: names.filter((name) => !valid(name)),
|
|
483
|
-
};
|
|
484
|
-
// the names a fresh session holds a function in: its function declarations, made again at a wake
|
|
485
|
-
const probe = this.startDeclaring(record, this.ensureRealm().kit.system('', '', []));
|
|
486
|
-
for (const name of all) {
|
|
487
|
-
let value;
|
|
488
|
-
try {
|
|
489
|
-
value = probe.handle.get(name);
|
|
490
|
-
} catch {
|
|
491
|
-
continue; // in its TDZ: a lexical binding
|
|
492
|
-
}
|
|
493
|
-
if (typeof value === 'function') record.remade.add(name);
|
|
494
|
-
}
|
|
495
|
-
return record;
|
|
335
|
+
chart.Scope = realm.kit.shape(names, helpers);
|
|
336
|
+
this.charts[slot] = chart;
|
|
337
|
+
return TA_OK;
|
|
496
338
|
}
|
|
497
339
|
|
|
498
340
|
// ---- sessions ----
|
|
499
341
|
|
|
500
|
-
// The session at `slot` (its scope
|
|
342
|
+
// The session at `slot` (its scope), with the
|
|
501
343
|
// evaluating chart set for In() and, with `charge`, a fuel unit charged.
|
|
502
344
|
session(slot, charge) {
|
|
503
345
|
const session = this.sessions[slot];
|
|
@@ -539,33 +381,9 @@ export class NativeDatamodel {
|
|
|
539
381
|
return [sessionid, name, processors];
|
|
540
382
|
}
|
|
541
383
|
|
|
542
|
-
// A declaring session: its scope (the system variables and _event), its
|
|
543
|
-
// generator at its first stop, and the handle the generator fills (get,
|
|
544
|
-
// set, the loop after a chart-level script threw).
|
|
545
|
-
startDeclaring(record, link) {
|
|
546
|
-
const kit = this.realm.kit;
|
|
547
|
-
const scope = new (record.Shape ??= kit.shape([]))(link);
|
|
548
|
-
const handle = { next: record.next ??= Object.freeze({}), none: undefined, value: undefined, get: null, set: null, loop: null };
|
|
549
|
-
const generator = record.factory(scope)(handle);
|
|
550
|
-
generator.next();
|
|
551
|
-
return new Declaring(scope, handle, generator);
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
// Runs `code` as a direct eval in the declaring session's scope: its value.
|
|
555
|
-
run(session, code) {
|
|
556
|
-
const answer = session.dead ? session.handle.loop.next(code).value : session.generator.next(code).value;
|
|
557
|
-
if (!answer[0]) throw answer[1];
|
|
558
|
-
return answer[1];
|
|
559
|
-
}
|
|
560
|
-
|
|
561
384
|
make(slot, chart, link, init) {
|
|
562
|
-
if (chart.declaring === null) {
|
|
563
|
-
link.init = init;
|
|
564
|
-
return new chart.Scope(link);
|
|
565
|
-
}
|
|
566
|
-
if (init !== undefined) init = this.realm.kit.copy(init);
|
|
567
385
|
link.init = init;
|
|
568
|
-
return
|
|
386
|
+
return new chart.Scope(link);
|
|
569
387
|
}
|
|
570
388
|
|
|
571
389
|
create(slot) {
|
|
@@ -585,10 +403,7 @@ export class NativeDatamodel {
|
|
|
585
403
|
return TA_OK;
|
|
586
404
|
}
|
|
587
405
|
|
|
588
|
-
|
|
589
|
-
scopeOf(session) { return session instanceof Declaring ? session.scope : session; }
|
|
590
|
-
|
|
591
|
-
linkOf(session) { return this.scopeOf(session)[this.realm.kit.LINK]; }
|
|
406
|
+
linkOf(session) { return session[this.realm.kit.LINK]; }
|
|
592
407
|
|
|
593
408
|
initData(slot) {
|
|
594
409
|
const declaration = this.module.word(0);
|
|
@@ -600,24 +415,10 @@ export class NativeDatamodel {
|
|
|
600
415
|
let value;
|
|
601
416
|
if (init !== null && typeof init === 'object' && Object.hasOwn(init, data.id)) value = init[data.id];
|
|
602
417
|
else if (data.expression !== NIL) value = this.evaluate(session, data.expression);
|
|
603
|
-
|
|
418
|
+
session[data.id] = value;
|
|
604
419
|
return TA_OK;
|
|
605
420
|
}
|
|
606
421
|
|
|
607
|
-
setVariable(session, name, value) {
|
|
608
|
-
if (!(session instanceof Declaring)) {
|
|
609
|
-
session[name] = value;
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
if (!session.handle.set(name, value)) (session.odd ??= {})[name] = value;
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
getVariable(session, name) {
|
|
616
|
-
if (!(session instanceof Declaring)) return session[name];
|
|
617
|
-
if (session.odd !== undefined && Object.hasOwn(session.odd, name)) return session.odd[name];
|
|
618
|
-
return session.handle.get(name);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
422
|
// ---- _event ----
|
|
622
423
|
|
|
623
424
|
// The string of `text` kept at `index` of a chart's list: the one there
|
|
@@ -648,9 +449,8 @@ export class NativeDatamodel {
|
|
|
648
449
|
optional(16),
|
|
649
450
|
];
|
|
650
451
|
const event = this.realm.kit.event(fields);
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
scope[this.realm.kit.LINK].init = undefined; // an event has come: the params were read by init_data
|
|
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
|
|
654
454
|
this.meta[4 * slot + 1] = w[1];
|
|
655
455
|
this.meta[4 * slot + 2] = w[2];
|
|
656
456
|
this.meta[4 * slot + 3] = w[3];
|
|
@@ -664,31 +464,21 @@ export class NativeDatamodel {
|
|
|
664
464
|
const info = this.expressionOf(chart, e);
|
|
665
465
|
if (info === null) throw new Error('no such expression');
|
|
666
466
|
if (info.kind === KIND.CONTENT) return this.content(info.source);
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
const
|
|
670
|
-
|
|
671
|
-
return fn(session);
|
|
672
|
-
}
|
|
673
|
-
if (info.kind === KIND.GLOBAL_SCRIPT) return this.chartScript(session, chart, e, info);
|
|
674
|
-
if (info.kind === KIND.SCRIPT) {
|
|
675
|
-
const source = info.source;
|
|
676
|
-
return this.run(session, strictDirective(source) ? `'use strict';{\n${source}\n}` : `{\n${source}\n}`);
|
|
677
|
-
}
|
|
678
|
-
const getters = session.getters ??= [];
|
|
679
|
-
let fn = getters[e];
|
|
680
|
-
if (fn === undefined) {
|
|
681
|
-
fn = this.run(session, OWN_VALUE[0] + trimmed(info.source) + OWN_VALUE[1]);
|
|
682
|
-
getters[e] = fn;
|
|
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);
|
|
683
471
|
}
|
|
684
|
-
return
|
|
472
|
+
return this.compiled(chart.getters, e, () => SHARED_VALUE[0] + trimmed(info.source) + SHARED_VALUE[1])(session);
|
|
685
473
|
}
|
|
686
474
|
|
|
687
475
|
// The compiled function of `e` in `cache`, compiled in the realm at its
|
|
688
|
-
// first use; null kept when it did not compile, so it is not compiled
|
|
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.
|
|
689
478
|
compiled(cache, e, source) {
|
|
690
479
|
const kept = cache[e];
|
|
691
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);
|
|
692
482
|
if (kept !== undefined) return kept;
|
|
693
483
|
let made;
|
|
694
484
|
try {
|
|
@@ -701,59 +491,15 @@ export class NativeDatamodel {
|
|
|
701
491
|
return made;
|
|
702
492
|
}
|
|
703
493
|
|
|
704
|
-
// A declaring session's chart-level script `e`: the generator goes on to
|
|
705
|
-
// the stop after it. One that does not parse is its own error.execution;
|
|
706
|
-
// one that throws ends the session function, so the scripts after it do
|
|
707
|
-
// not run (the addon runs them in its context per session): its error
|
|
708
|
-
// says so, and the session goes on through its loop.
|
|
709
|
-
chartScript(session, chart, e, info) {
|
|
710
|
-
const stop = chart.declaring.stops.get(e);
|
|
711
|
-
if (stop === undefined) {
|
|
712
|
-
new this.realm.Function(`\n${info.source}`); // eslint-disable-line no-new
|
|
713
|
-
return undefined;
|
|
714
|
-
}
|
|
715
|
-
if (session.dead || session.stop >= stop) return undefined; // run already (a restored session: never)
|
|
716
|
-
while (session.stop < stop) {
|
|
717
|
-
session.stop++;
|
|
718
|
-
try {
|
|
719
|
-
session.generator.next(session.handle.next);
|
|
720
|
-
} catch (error) {
|
|
721
|
-
session.dead = true;
|
|
722
|
-
const later = chart.declaring.stops.size > stop;
|
|
723
|
-
if (later && error !== null && typeof error === 'object') {
|
|
724
|
-
try {
|
|
725
|
-
error.message = `${error.message} (the chart-level scripts after this one did not run: in ` +
|
|
726
|
-
'@tinyactors/wasm a chart\'s chart-level scripts are one function\'s body)';
|
|
727
|
-
} catch {
|
|
728
|
-
// a frozen error keeps its message
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
throw error;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
return undefined;
|
|
735
|
-
}
|
|
736
|
-
|
|
737
494
|
assign(session, location, value) {
|
|
738
495
|
const chart = this.chartOf(this.current);
|
|
739
496
|
const info = this.expressionOf(chart, location);
|
|
740
497
|
if (info === null) throw new Error('no such location');
|
|
741
498
|
let name = chart.leading[location];
|
|
742
499
|
if (name === undefined) chart.leading[location] = name = leadingName(info.source);
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
const fn = this.compiled(chart.setters, location, () => SHARED_SETTER[0] + trimmed(info.source) + SHARED_SETTER[1]);
|
|
747
|
-
fn(session, value);
|
|
748
|
-
return;
|
|
749
|
-
}
|
|
750
|
-
const setters = session.setters ??= [];
|
|
751
|
-
let fn = setters[location];
|
|
752
|
-
if (fn === undefined) {
|
|
753
|
-
fn = this.run(session, OWN_SETTER[0] + trimmed(info.source) + OWN_SETTER[1]);
|
|
754
|
-
setters[location] = fn;
|
|
755
|
-
}
|
|
756
|
-
fn(value);
|
|
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);
|
|
757
503
|
}
|
|
758
504
|
|
|
759
505
|
log(slot) {
|
|
@@ -933,7 +679,7 @@ export class NativeDatamodel {
|
|
|
933
679
|
const s = this.session(session, false);
|
|
934
680
|
const chart = this.chartOf(session);
|
|
935
681
|
if (!chart.data.some((d) => d.id === name)) throw new Refused(TA_ERROR_INVALID, 'no such variable');
|
|
936
|
-
const value =
|
|
682
|
+
const value = s[name];
|
|
937
683
|
this.module.setWord(0, value === undefined ? 0 : 1);
|
|
938
684
|
if (value !== undefined) this.values[slot] = value;
|
|
939
685
|
return TA_OK;
|
|
@@ -968,59 +714,12 @@ export class NativeDatamodel {
|
|
|
968
714
|
// ---- hibernation ----
|
|
969
715
|
|
|
970
716
|
// A session's variables as hibernation writes them: [names, values].
|
|
971
|
-
stored(session
|
|
972
|
-
if (chart.declaring === null) {
|
|
973
|
-
const names = [], values = [];
|
|
974
|
-
for (const name of Object.keys(session)) {
|
|
975
|
-
if (name === '_event') continue;
|
|
976
|
-
names.push(name);
|
|
977
|
-
values.push(session[name]);
|
|
978
|
-
}
|
|
979
|
-
return [names, values];
|
|
980
|
-
}
|
|
981
|
-
const record = chart.declaring;
|
|
982
|
-
const names = [], values = [];
|
|
983
|
-
for (const name of record.stored) {
|
|
984
|
-
let value;
|
|
985
|
-
try {
|
|
986
|
-
value = session.handle.get(name);
|
|
987
|
-
} catch {
|
|
988
|
-
continue; // in its TDZ
|
|
989
|
-
}
|
|
990
|
-
if (typeof value === 'function' && record.remade.has(name)) continue;
|
|
991
|
-
names.push(name);
|
|
992
|
-
values.push(value);
|
|
993
|
-
}
|
|
994
|
-
if (session.odd !== undefined) for (const name of Object.keys(session.odd)) {
|
|
995
|
-
names.push(name);
|
|
996
|
-
values.push(session.odd[name]);
|
|
997
|
-
}
|
|
998
|
-
return [names, values];
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
// Why a declaring session cannot be written out, or undefined: a chart
|
|
1002
|
-
// that declares with let, const or class, or names an undeclared
|
|
1003
|
-
// assignment put on the host's shared realm.
|
|
1004
|
-
unstorable(chart, snapshot) {
|
|
1005
|
-
if (chart.declaring === null) return undefined;
|
|
1006
|
-
if (chart.declaring.lexical) {
|
|
1007
|
-
return snapshot
|
|
1008
|
-
? 'a session of a chart whose scripts declare with let, const or class at their top level: a new context cannot be given those bindings again'
|
|
1009
|
-
: 'the chart\'s scripts declare with let, const or class at their top level: a new context cannot be given those bindings again, so its sessions stay resident';
|
|
1010
|
-
}
|
|
1011
|
-
const leaked = this.realm.kit.globals();
|
|
1012
|
-
if (leaked.length > 0) {
|
|
1013
|
-
return `an undeclared assignment in a chart's script put ${leaked.map((n) => `'${n}'`).join(', ')} on the host's realm, ` +
|
|
1014
|
-
'not on a session (in @tinyactors/wasm the host\'s charts share one realm), so it cannot be stored: declare it ' +
|
|
1015
|
-
'(a <data>, or var in a chart-level script)';
|
|
1016
|
-
}
|
|
1017
|
-
return undefined;
|
|
1018
|
-
}
|
|
717
|
+
stored(session) { return this.realm.kit.stored(session); }
|
|
1019
718
|
|
|
1020
719
|
// _event as a blob holds it: its eight fields, then core's ids; null
|
|
1021
720
|
// when no event came yet.
|
|
1022
721
|
eventOut(session, slot) {
|
|
1023
|
-
const event =
|
|
722
|
+
const event = session._event;
|
|
1024
723
|
if (event === null || typeof event !== 'object') return null;
|
|
1025
724
|
const fields = EVENT_FIELDS.map((field) => event[field]);
|
|
1026
725
|
fields.push(this.meta[4 * slot + 1], this.meta[4 * slot + 2], this.meta[4 * slot + 3]);
|
|
@@ -1051,14 +750,7 @@ export class NativeDatamodel {
|
|
|
1051
750
|
serialize(slot) {
|
|
1052
751
|
this.state.reason = '';
|
|
1053
752
|
const session = this.session(slot, false);
|
|
1054
|
-
const
|
|
1055
|
-
const why = this.unstorable(chart, false);
|
|
1056
|
-
if (why !== undefined) {
|
|
1057
|
-
const refused = new Refused(TA_ERROR_UNSUPPORTED, why);
|
|
1058
|
-
refused.reason = why;
|
|
1059
|
-
throw refused;
|
|
1060
|
-
}
|
|
1061
|
-
const [names, values] = this.stored(session, chart);
|
|
753
|
+
const [names, values] = this.stored(session);
|
|
1062
754
|
const vars = {};
|
|
1063
755
|
for (let i = 0; i < names.length; i++) {
|
|
1064
756
|
Object.defineProperty(vars, names[i], { value: values[i], enumerable: true, writable: true, configurable: true });
|
|
@@ -1106,33 +798,13 @@ export class NativeDatamodel {
|
|
|
1106
798
|
return TA_OK;
|
|
1107
799
|
}
|
|
1108
800
|
|
|
1109
|
-
// A session made again (a wake, a restore): its variables set, the
|
|
1110
|
-
//
|
|
1111
|
-
revive(slot, { chart, link, vars, init
|
|
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 }) {
|
|
1112
804
|
const session = this.make(slot, chart, link, init);
|
|
1113
805
|
this.grow(slot);
|
|
1114
806
|
this.meta.set([chart.slot, NIL, NIL, NIL], 4 * slot);
|
|
1115
|
-
|
|
1116
|
-
else {
|
|
1117
|
-
const kit = this.realm.kit;
|
|
1118
|
-
const extra = [];
|
|
1119
|
-
for (const name of Object.keys(vars)) {
|
|
1120
|
-
const value = kit.copy(vars[name]);
|
|
1121
|
-
if (chart.declaring.declared.has(name) && session.handle.set(name, value)) continue;
|
|
1122
|
-
if (chart.declaring.odd.includes(name)) {
|
|
1123
|
-
(session.odd ??= {})[name] = value;
|
|
1124
|
-
continue;
|
|
1125
|
-
}
|
|
1126
|
-
if (!IDENTIFIER.test(name) || name.startsWith(PREFIX)) continue;
|
|
1127
|
-
session.handle.value = value;
|
|
1128
|
-
this.run(session, `var ${name} = __tinyactors_session.value;`);
|
|
1129
|
-
session.handle.value = undefined;
|
|
1130
|
-
extra.push(name);
|
|
1131
|
-
}
|
|
1132
|
-
if (extra.length) link.added = extra;
|
|
1133
|
-
session.stop = chart.declaring.stops.size; // its chart-level scripts ran before it was written out
|
|
1134
|
-
}
|
|
1135
|
-
if (relink) link.added = Array.isArray(added) ? added.slice() : undefined;
|
|
807
|
+
this.realm.kit.restore(session, vars);
|
|
1136
808
|
this.sessions[slot] = session;
|
|
1137
809
|
return session;
|
|
1138
810
|
}
|
|
@@ -1154,25 +826,17 @@ export class NativeDatamodel {
|
|
|
1154
826
|
made[5] = keep(chart.invokeids, invoke, made[5]);
|
|
1155
827
|
if (made[6] !== undefined) made[6] = this.realm.kit.copy(made[6]);
|
|
1156
828
|
const event = this.realm.kit.event(made);
|
|
1157
|
-
Object.defineProperty(
|
|
829
|
+
Object.defineProperty(session, '_event', { value: event, enumerable: true, configurable: true, writable: false });
|
|
1158
830
|
this.meta[4 * slot + 1] = nameID;
|
|
1159
831
|
this.meta[4 * slot + 2] = invoke;
|
|
1160
832
|
this.meta[4 * slot + 3] = origintype;
|
|
1161
833
|
}
|
|
1162
834
|
|
|
1163
835
|
// After deserialize, for a blob of an older version of the chart: the
|
|
1164
|
-
// old bindings (exactly the old chart's variables) as one payload
|
|
1165
|
-
// names the blob added that the new chart does not declare go.
|
|
836
|
+
// old bindings (exactly the old chart's variables) as one payload.
|
|
1166
837
|
migrate(slot) {
|
|
1167
|
-
const
|
|
1168
|
-
const
|
|
1169
|
-
const link = this.linkOf(session);
|
|
1170
|
-
const old = link.init;
|
|
1171
|
-
link.init = undefined;
|
|
1172
|
-
if (session instanceof Declaring && link.added !== undefined) {
|
|
1173
|
-
for (const name of link.added) this.run(session, `delete ${name};`);
|
|
1174
|
-
link.added = undefined;
|
|
1175
|
-
} else if (!(session instanceof Declaring)) this.realm.kit.upgrade(session);
|
|
838
|
+
const session = this.session(this.module.word(0), true);
|
|
839
|
+
const old = this.realm.kit.upgrade(session);
|
|
1176
840
|
if (old !== null && typeof old === 'object') {
|
|
1177
841
|
this.values[slot] = this.realm.kit.copy(old);
|
|
1178
842
|
this.module.setWord(0, 1);
|
|
@@ -1230,15 +894,13 @@ export class NativeDatamodel {
|
|
|
1230
894
|
|
|
1231
895
|
// One session row: [handle, segment, chart index, {name: bytes}, _event
|
|
1232
896
|
// as bytes, the link's [sessionid, name, processors] as bytes, its init
|
|
1233
|
-
// as bytes or null
|
|
897
|
+
// as bytes or null].
|
|
1234
898
|
snapshotSession(handle, bytes) {
|
|
1235
899
|
const slot = Number(handle & 0xFFFFFFFFn) - 1;
|
|
1236
900
|
const session = this.sessions[slot];
|
|
1237
901
|
const chart = this.chartOf(slot);
|
|
1238
902
|
if (session === undefined || chart === undefined) throw new TinyactorsError('a session without its chart', { status: 'TA_ERROR_INVALID' });
|
|
1239
|
-
const
|
|
1240
|
-
if (why !== undefined) throw new TinyactorsError(why, { status: 'TA_ERROR_UNSUPPORTED' });
|
|
1241
|
-
const [names, values] = this.stored(session, chart);
|
|
903
|
+
const [names, values] = this.stored(session);
|
|
1242
904
|
const vars = {};
|
|
1243
905
|
for (let i = 0; i < names.length; i++) {
|
|
1244
906
|
Object.defineProperty(vars, names[i], { value: bytes(values[i], 'variable \'', names[i]), enumerable: true, writable: true, configurable: true });
|
|
@@ -1249,12 +911,11 @@ export class NativeDatamodel {
|
|
|
1249
911
|
} catch (error) {
|
|
1250
912
|
throw new TinyactorsError(this.reason('_event', undefined, error), { status: 'TA_ERROR_UNSUPPORTED' });
|
|
1251
913
|
}
|
|
1252
|
-
const linked = this.realm.kit.linked(
|
|
914
|
+
const linked = this.realm.kit.linked(session);
|
|
1253
915
|
return [
|
|
1254
916
|
handle, chart.segment, chart.index, vars, bytes(event, '_event'),
|
|
1255
917
|
bytes([linked[0], linked[1], linked[2]], 'the system variables'),
|
|
1256
918
|
linked[3] === undefined ? null : bytes(linked[3], 'the params'),
|
|
1257
|
-
bytes(linked[4], 'the session'),
|
|
1258
919
|
];
|
|
1259
920
|
}
|
|
1260
921
|
|
|
@@ -1301,7 +962,7 @@ export class NativeDatamodel {
|
|
|
1301
962
|
this.ensureRealm();
|
|
1302
963
|
const kit = this.realm.kit;
|
|
1303
964
|
for (const row of rows) {
|
|
1304
|
-
if (!Array.isArray(row) || row.length !==
|
|
965
|
+
if (!Array.isArray(row) || row.length !== 7) throw invalid('not a session of a snapshot');
|
|
1305
966
|
const handle = handleOf(row[0]);
|
|
1306
967
|
const chart = this.charts.find((c) => c !== undefined && c.segment === row[1] && c.index === row[2]);
|
|
1307
968
|
if (chart === undefined) throw invalid('a session whose chart the restored host does not have');
|
|
@@ -1316,7 +977,7 @@ export class NativeDatamodel {
|
|
|
1316
977
|
for (const name of Object.keys(row[3])) {
|
|
1317
978
|
Object.defineProperty(vars, name, { value: valueOf(row[3][name]), enumerable: true, writable: true, configurable: true });
|
|
1318
979
|
}
|
|
1319
|
-
const session = this.revive(slot, { chart, link: kit.system(system[0], system[1], system[2]), vars, init
|
|
980
|
+
const session = this.revive(slot, { chart, link: kit.system(system[0], system[1], system[2]), vars, init });
|
|
1320
981
|
this.eventIn(session, slot, chart, valueOf(row[4]));
|
|
1321
982
|
}
|
|
1322
983
|
for (const row of values) {
|