@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/lib/direct.mjs ADDED
@@ -0,0 +1,14 @@
1
+ // The direct layer of @tinyactors/wasm: the same names, shapes, errors and
2
+ // lifetimes as @tinyactors/node's (lib/direct.mjs, the addon's exports),
3
+ // in JavaScript over the module (docs/browser.md). The convenience layer
4
+ // (lib/convenience.mjs) imports it as `#direct` under the export condition
5
+ // `tinyactors-wasm`, and the package's entries (index.mjs, init.mjs) export
6
+ // what that layer exports: the direct Host and Send are its. Nothing here touches the module before a call: an entry loads it.
7
+ export { Address } from './address.mjs';
8
+ export { compileCharts, compileSystem } from './compile.mjs';
9
+ export {
10
+ CompileError, InvalidError, ResolveError, StaleError, StateError, TinyactorsError, UnsupportedError,
11
+ } from './errors.mjs';
12
+ export { Host, Send, SimulatedClock } from './host.mjs';
13
+ export { decodeTraces, encodeHTTPPost, parseHTTPRequest } from './http.mjs';
14
+ export { decode, encode } from './values.mjs';
package/lib/errors.mjs ADDED
@@ -0,0 +1,94 @@
1
+ // The errors, as the addon defines them (src/module.c, src/compile.c):
2
+ // TinyactorsError, an Error with read-only `code`, `reason`, `path` and
3
+ // `status` (core's name of a status, ta_status_name), each undefined when
4
+ // not given; CompileError and ResolveError, its subclasses. A poisoned
5
+ // module's error carries what poisoned it as its `cause`.
6
+ // One subclass per status an application branches on (StaleError,
7
+ // StateError, InvalidError, UnsupportedError): `new TinyactorsError(message,
8
+ // {status})` makes the subclass of that status itself, and a typed method's
9
+ // error the subclass of its code (-32001 stale, -32002 state, any other
10
+ // invalid), so no call that throws chooses a class.
11
+ let byStatus, byCode;
12
+
13
+ export class TinyactorsError extends Error {
14
+ constructor(message, options) {
15
+ const given = options ?? {};
16
+ if (new.target === TinyactorsError) {
17
+ const subclass = byStatus[given.status] ?? (given.code === undefined ? undefined : byCode(given.code));
18
+ if (subclass !== undefined) return new subclass(message, options);
19
+ }
20
+ super(message, given.cause !== undefined ? { cause: given.cause } : undefined);
21
+ Object.defineProperties(this, {
22
+ code: { value: given.code, enumerable: true },
23
+ reason: { value: given.reason, enumerable: true },
24
+ path: { value: given.path, enumerable: true },
25
+ status: { value: given.status, enumerable: true },
26
+ });
27
+ }
28
+
29
+ get name() { return 'TinyactorsError'; }
30
+ }
31
+
32
+ const of = (name, status) => ({ [name]: class extends TinyactorsError {
33
+ constructor(message, options) {
34
+ const given = options ?? {};
35
+ super(message, given.code === undefined ? { ...given, status: given.status ?? status } : given);
36
+ }
37
+
38
+ get name() { return name; }
39
+ } })[name];
40
+
41
+ export const StaleError = of('StaleError', 'TA_ERROR_STALE');
42
+ export const StateError = of('StateError', 'TA_ERROR_STATE');
43
+ export const InvalidError = of('InvalidError', 'TA_ERROR_INVALID');
44
+ export const UnsupportedError = of('UnsupportedError', 'TA_ERROR_UNSUPPORTED');
45
+ byStatus = { __proto__: null, TA_ERROR_STALE: StaleError, TA_ERROR_STATE: StateError,
46
+ TA_ERROR_INVALID: InvalidError, TA_ERROR_UNSUPPORTED: UnsupportedError };
47
+ byCode = (code) => code === -32001 ? StaleError : code === -32002 ? StateError : InvalidError;
48
+
49
+ // A compilation with an error: every diagnostic, the message the first
50
+ // error's as a compiler prints it.
51
+ export class CompileError extends TinyactorsError {
52
+ constructor(message, options) {
53
+ const given = options ?? {};
54
+ super(message, { status: given.status ?? 'TA_ERROR_INVALID' });
55
+ Object.defineProperty(this, 'diagnostics', { value: Object.freeze([...(given.diagnostics ?? [])]), enumerable: true });
56
+ }
57
+
58
+ get name() { return 'CompileError'; }
59
+ }
60
+
61
+ // A resolver's refusal: a diagnostic of the resolved file at its line and
62
+ // column.
63
+ export class ResolveError extends TinyactorsError {
64
+ constructor(message, options) {
65
+ super(message, { status: 'TA_ERROR_INVALID' });
66
+ const given = options ?? {};
67
+ Object.defineProperties(this, {
68
+ line: { value: given.line, enumerable: true },
69
+ column: { value: given.column, enumerable: true },
70
+ });
71
+ }
72
+
73
+ get name() { return 'ResolveError'; }
74
+ }
75
+
76
+ // A refusal of a call: core's status by its name.
77
+ export function refusal(status, message) {
78
+ return new TinyactorsError(message, { status });
79
+ }
80
+
81
+ // A reply's error (core wrote it: {code, message, data: {reason, path}}),
82
+ // or a refused config's: a TinyactorsError with `status` only when it is
83
+ // a call's (`status` given).
84
+ export function coreError(error, status) {
85
+ const options = {};
86
+ if (status !== undefined) options.status = status;
87
+ if (error === null || typeof error !== 'object') return new TinyactorsError(status ?? 'TA_ERROR_INVALID', options);
88
+ options.code = error.code;
89
+ if (error.data !== null && typeof error.data === 'object') {
90
+ options.reason = error.data.reason;
91
+ options.path = error.data.path;
92
+ }
93
+ return new TinyactorsError(error.message, options);
94
+ }