@litz-sim/litz 0.1.0 → 0.2.1
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 +176 -0
- package/README.md +168 -100
- package/index.d.ts +114 -2
- package/index.js +7 -652
- package/litz_wasm.wasm +0 -0
- package/package.json +4 -2
- package/src/bytes.js +73 -0
- package/src/constants.js +98 -0
- package/src/errors.js +9 -0
- package/src/host.js +423 -0
- package/src/jco.js +257 -0
- package/src/litz.js +261 -0
- package/src/session.js +432 -0
package/src/bytes.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Buffers and the byte boundary: host values in, typed arrays out.
|
|
2
|
+
|
|
3
|
+
import { DECODER } from "./constants.js";
|
|
4
|
+
import { LitzError } from "./errors.js";
|
|
5
|
+
|
|
6
|
+
export function hold(io, bytes) {
|
|
7
|
+
if (bytes.length === 0) return { ptr: 0, len: 0 };
|
|
8
|
+
const ptr = io.exports.litz_alloc(bytes.length);
|
|
9
|
+
if (ptr === 0) throw new LitzError(`could not allocate ${bytes.length} bytes in the module`);
|
|
10
|
+
io.view(ptr, bytes.length).set(bytes);
|
|
11
|
+
return { ptr, len: bytes.length };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function bytesOf(value, label) {
|
|
15
|
+
if (value instanceof Uint8Array) return value;
|
|
16
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
17
|
+
if (ArrayBuffer.isView(value)) {
|
|
18
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
19
|
+
}
|
|
20
|
+
throw new LitzError(`\`${label}\` must be a Uint8Array`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function optionalBytes(value, label) {
|
|
24
|
+
if (value === null || value === undefined) return new Uint8Array(0);
|
|
25
|
+
return bytesOf(value, label);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function decodeChunks(chunks) {
|
|
29
|
+
if (chunks.length === 0) return "";
|
|
30
|
+
if (chunks.length === 1) return DECODER.decode(chunks[0]);
|
|
31
|
+
const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
32
|
+
const joined = new Uint8Array(total);
|
|
33
|
+
let at = 0;
|
|
34
|
+
for (const chunk of chunks) {
|
|
35
|
+
joined.set(chunk, at);
|
|
36
|
+
at += chunk.length;
|
|
37
|
+
}
|
|
38
|
+
return DECODER.decode(joined);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function toBytes(source) {
|
|
42
|
+
if (source instanceof ArrayBuffer) return new Uint8Array(source.slice(0));
|
|
43
|
+
if (ArrayBuffer.isView(source)) {
|
|
44
|
+
return new Uint8Array(
|
|
45
|
+
source.buffer.slice(source.byteOffset, source.byteOffset + source.byteLength),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (typeof Response !== "undefined" && source instanceof Response) {
|
|
49
|
+
if (!source.ok) throw new LitzError(`fetching the wasm module failed: ${source.status}`);
|
|
50
|
+
return new Uint8Array(await source.arrayBuffer());
|
|
51
|
+
}
|
|
52
|
+
const url = source instanceof URL ? source : new URL(String(source), import.meta.url);
|
|
53
|
+
if (url.protocol === "file:") {
|
|
54
|
+
const { readFile } = await import("node:fs/promises");
|
|
55
|
+
try {
|
|
56
|
+
return new Uint8Array(await readFile(url));
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw new LitzError(
|
|
59
|
+
`could not read the wasm module at ${url.href}; run \`npm run build\` in the package, or pass \`wasm\` explicitly`,
|
|
60
|
+
{ cause: error },
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (typeof fetch !== "function") {
|
|
65
|
+
throw new LitzError("this host has no fetch; pass `wasm` as bytes or a URL");
|
|
66
|
+
}
|
|
67
|
+
const response = await fetch(url);
|
|
68
|
+
if (!response.ok) {
|
|
69
|
+
throw new LitzError(`fetching ${url.href} failed: ${response.status}`);
|
|
70
|
+
}
|
|
71
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
72
|
+
}
|
|
73
|
+
|
package/src/constants.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Values the wrapper and the module agree on.
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_STEPS = 400_000;
|
|
4
|
+
|
|
5
|
+
export const REQUIRED_EXPORTS = [
|
|
6
|
+
"memory",
|
|
7
|
+
"litz_alloc",
|
|
8
|
+
"litz_free",
|
|
9
|
+
"litz_new",
|
|
10
|
+
"litz_new_json",
|
|
11
|
+
"litz_drop",
|
|
12
|
+
"litz_last_error",
|
|
13
|
+
"litz_run",
|
|
14
|
+
"litz_ms",
|
|
15
|
+
"litz_conflict_count",
|
|
16
|
+
"litz_conflict",
|
|
17
|
+
"litz_serial_take",
|
|
18
|
+
"litz_serial_write",
|
|
19
|
+
"litz_display_info",
|
|
20
|
+
"litz_display_version",
|
|
21
|
+
"litz_display_rgba",
|
|
22
|
+
"litz_touch",
|
|
23
|
+
"litz_trace_start",
|
|
24
|
+
"litz_trace_len",
|
|
25
|
+
"litz_trace_vcd",
|
|
26
|
+
"litz_probe_add",
|
|
27
|
+
"litz_scope_len",
|
|
28
|
+
"litz_scope_json",
|
|
29
|
+
"litz_decode_add",
|
|
30
|
+
"litz_part_state",
|
|
31
|
+
"litz_part_value",
|
|
32
|
+
"litz_press",
|
|
33
|
+
"litz_set_control",
|
|
34
|
+
"litz_part_count",
|
|
35
|
+
"litz_part_id",
|
|
36
|
+
"litz_chip_info",
|
|
37
|
+
"litz_stop",
|
|
38
|
+
"litz_fault",
|
|
39
|
+
"litz_part_pin_level",
|
|
40
|
+
"litz_gpio_level",
|
|
41
|
+
"litz_set_named_control",
|
|
42
|
+
"litz_heap",
|
|
43
|
+
"litz_guest_console",
|
|
44
|
+
"litz_guest_fault",
|
|
45
|
+
"litz_guest_call",
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
// State codes from `litz_part_state`; `-1` means the diagram has no such id.
|
|
49
|
+
// The code carries both what the part is and what it is doing, which is why
|
|
50
|
+
// the wrapper reports a kind as well as a word.
|
|
51
|
+
export const STATE_CODES = [
|
|
52
|
+
{ kind: "none", state: "none" },
|
|
53
|
+
{ kind: "led", state: "off" },
|
|
54
|
+
{ kind: "led", state: "on" },
|
|
55
|
+
{ kind: "button", state: "up" },
|
|
56
|
+
{ kind: "button", state: "down" },
|
|
57
|
+
{ kind: "switch", state: "off" },
|
|
58
|
+
{ kind: "switch", state: "on" },
|
|
59
|
+
{ kind: "tone", state: "silent" },
|
|
60
|
+
{ kind: "tone", state: "sounding" },
|
|
61
|
+
{ kind: "servo", state: "position" },
|
|
62
|
+
{ kind: "segments", state: "mask" },
|
|
63
|
+
{ kind: "knob", state: "position" },
|
|
64
|
+
{ kind: "radio", state: "state" },
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
// How a run ended, from `litz_stop`.
|
|
68
|
+
export const STOPS = ["budget", "idle", "halted", "trap", "reset", "breakpoint"];
|
|
69
|
+
|
|
70
|
+
// A pin's level, from `litz_part_pin_level` and `litz_gpio_level`.
|
|
71
|
+
export const LEVELS = ["low", "high", "high-z"];
|
|
72
|
+
|
|
73
|
+
export const ENCODER = new TextEncoder();
|
|
74
|
+
export const DECODER = new TextDecoder();
|
|
75
|
+
|
|
76
|
+
// State codes the part bridge shares with the module: the same numbering the
|
|
77
|
+
// rest of the ABI uses for a part's state.
|
|
78
|
+
export const PART_STATE_KINDS = {
|
|
79
|
+
none: 0,
|
|
80
|
+
"led-off": 1,
|
|
81
|
+
"led-on": 2,
|
|
82
|
+
"button-up": 3,
|
|
83
|
+
"button-down": 4,
|
|
84
|
+
"switch-off": 5,
|
|
85
|
+
"switch-on": 6,
|
|
86
|
+
"tone-silent": 7,
|
|
87
|
+
"tone-sounding": 8,
|
|
88
|
+
servo: 9,
|
|
89
|
+
segments: 10,
|
|
90
|
+
knob: 11,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
export const LEVEL_WORDS = ["low", "high", "floating"];
|
|
95
|
+
|
|
96
|
+
// `litz:image` and `litz:stop`, in contract order.
|
|
97
|
+
export const IMAGE_KINDS = ["rom", "bootloader", "partition-table", "firmware"];
|
|
98
|
+
export const STOP_CODES = { budget: 0, idle: 1, halted: 2, trap: 3, reset: 4 };
|
package/src/errors.js
ADDED
package/src/host.js
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
// The JavaScript half of the `litz_host` import module: custom parts and
|
|
2
|
+
// chips run here, the simulator drives them through imports.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
DECODER,
|
|
6
|
+
ENCODER,
|
|
7
|
+
IMAGE_KINDS,
|
|
8
|
+
LEVEL_WORDS,
|
|
9
|
+
PART_STATE_KINDS,
|
|
10
|
+
STOP_CODES,
|
|
11
|
+
} from "./constants.js";
|
|
12
|
+
import { LitzError } from "./errors.js";
|
|
13
|
+
|
|
14
|
+
class HostRuntime {
|
|
15
|
+
#memory = null;
|
|
16
|
+
#exports = null;
|
|
17
|
+
#factories = new Map();
|
|
18
|
+
#chipFactories = new Map();
|
|
19
|
+
#instances = new Map();
|
|
20
|
+
#chips = new Map();
|
|
21
|
+
#nextId = 1;
|
|
22
|
+
#lastError = "";
|
|
23
|
+
#callInPtr = 0;
|
|
24
|
+
#callInSize = 0;
|
|
25
|
+
#callOutPtr = 0;
|
|
26
|
+
#callOutSize = 0;
|
|
27
|
+
|
|
28
|
+
constructor() {
|
|
29
|
+
const host = this;
|
|
30
|
+
this.imports = {
|
|
31
|
+
part_create: (namePtr, nameLen) => host.#create(namePtr, nameLen),
|
|
32
|
+
part_drop: (id) => host.#instances.delete(id),
|
|
33
|
+
part_set_pin: (id, pin, level, atNs) =>
|
|
34
|
+
host.#part(id)?.setPin(pin, LEVEL_WORDS[level] ?? "floating", Number(atNs)),
|
|
35
|
+
part_advance: (id, toNs) => host.#part(id)?.advance(Number(toNs)),
|
|
36
|
+
part_next_event: (id) => {
|
|
37
|
+
const at = host.#part(id)?.nextEvent();
|
|
38
|
+
return at === null || at === undefined ? -1n : BigInt(Math.round(at));
|
|
39
|
+
},
|
|
40
|
+
part_drive: (id, pin) => {
|
|
41
|
+
const drive = host.#part(id)?.drive(pin);
|
|
42
|
+
return packDrive(drive);
|
|
43
|
+
},
|
|
44
|
+
part_state: (id, ptr, cap) => {
|
|
45
|
+
const [kind, value] = packState(host.#part(id)?.state());
|
|
46
|
+
if (cap < 8) return 0;
|
|
47
|
+
const view = host.#view();
|
|
48
|
+
new DataView(view.buffer, ptr, 8).setUint32(0, kind, true);
|
|
49
|
+
new DataView(view.buffer, ptr, 8).setUint32(4, value, true);
|
|
50
|
+
return 8;
|
|
51
|
+
},
|
|
52
|
+
part_press: (id, pressed) => (host.#part(id)?.press(pressed === 1) ? 1 : 0),
|
|
53
|
+
part_set_control: (id, permille) => (host.#part(id)?.setControl(permille) ? 1 : 0),
|
|
54
|
+
part_analog: (id, pin) => {
|
|
55
|
+
const volts = host.#part(id)?.analog(pin);
|
|
56
|
+
return volts === null || volts === undefined ? Number.NaN : volts;
|
|
57
|
+
},
|
|
58
|
+
part_i2c_connect: (id, address, read) => host.#part(id)?.i2cConnect(address, read === 1),
|
|
59
|
+
part_i2c_read: (id) => host.#part(id)?.i2cRead() ?? 0xff,
|
|
60
|
+
part_i2c_write: (id, byte) => (host.#part(id)?.i2cWrite(byte) ? 1 : 0),
|
|
61
|
+
part_i2c_stop: (id) => host.#part(id)?.i2cStop(),
|
|
62
|
+
part_spi_select: (id, selected) => host.#part(id)?.spiSelect(selected === 1),
|
|
63
|
+
part_spi_transfer: (id, txPtr, txLen, rxLen, outPtr, outCap) => {
|
|
64
|
+
const tx = host.#read(txPtr, txLen);
|
|
65
|
+
const rx = host.#part(id)?.spiTransfer(tx, rxLen) ?? new Uint8Array(0);
|
|
66
|
+
return host.#write(outPtr, outCap, rx);
|
|
67
|
+
},
|
|
68
|
+
part_uart_receive: (id, ptr, len, atNs) =>
|
|
69
|
+
host.#part(id)?.uartReceive(host.#read(ptr, len), Number(atNs)),
|
|
70
|
+
part_uart_poll: (id, outPtr, outCap) => {
|
|
71
|
+
const bytes = host.#part(id)?.uartPoll() ?? new Uint8Array(0);
|
|
72
|
+
return host.#write(outPtr, outCap, bytes);
|
|
73
|
+
},
|
|
74
|
+
host_error: (ptr, cap) => host.#write(ptr, cap, ENCODER.encode(host.#lastError)),
|
|
75
|
+
|
|
76
|
+
// ------------------------------------------------------------ chips
|
|
77
|
+
chip_create: (namePtr, nameLen) => host.#createChip(namePtr, nameLen),
|
|
78
|
+
chip_drop: (id) => host.#chips.delete(id),
|
|
79
|
+
chip_info: (id, ptr, cap) => {
|
|
80
|
+
const chip = host.#chip(id);
|
|
81
|
+
if (!chip) return 0;
|
|
82
|
+
try {
|
|
83
|
+
return host.#write(ptr, cap, ENCODER.encode(JSON.stringify(chip.info())));
|
|
84
|
+
} catch (error) {
|
|
85
|
+
host.#lastError = `chip info: ${error?.message ?? error}`;
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
chip_load_image: (id, kind, offset, ptr, len) => {
|
|
90
|
+
const chip = host.#chip(id);
|
|
91
|
+
if (!chip) return 0;
|
|
92
|
+
try {
|
|
93
|
+
chip.loadImage(IMAGE_KINDS[kind] ?? "firmware", Number(offset), host.#read(ptr, len));
|
|
94
|
+
return 1;
|
|
95
|
+
} catch (error) {
|
|
96
|
+
host.#lastError = `chip load-image: ${error?.message ?? error}`;
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
chip_boot: (id) => {
|
|
101
|
+
const chip = host.#chip(id);
|
|
102
|
+
if (!chip) return -1n;
|
|
103
|
+
try {
|
|
104
|
+
return BigInt(chip.boot() >>> 0);
|
|
105
|
+
} catch (error) {
|
|
106
|
+
host.#lastError = `chip boot: ${error?.message ?? error}`;
|
|
107
|
+
return -1n;
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
chip_set_inputs: (id, ptr, count) => {
|
|
111
|
+
const chip = host.#chip(id);
|
|
112
|
+
if (!chip) return;
|
|
113
|
+
const inputs = [];
|
|
114
|
+
for (let index = 0; index < count; index += 1) {
|
|
115
|
+
const base = ptr + index * 8;
|
|
116
|
+
inputs.push({
|
|
117
|
+
pin: host.#u32(base),
|
|
118
|
+
level: LEVEL_WORDS[host.#u32(base + 4)] ?? "floating",
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
chip.setInputs(inputs);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
host.#lastError = `chip set-inputs: ${error?.message ?? error}`;
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
chip_run: (id, budget, outPtr) => {
|
|
128
|
+
const chip = host.#chip(id);
|
|
129
|
+
if (!chip) return 0;
|
|
130
|
+
let result;
|
|
131
|
+
try {
|
|
132
|
+
result = chip.run(Number(budget));
|
|
133
|
+
} catch (error) {
|
|
134
|
+
host.#lastError = `chip run: ${error?.message ?? error}`;
|
|
135
|
+
result = { cycles: 0, stop: "trap" };
|
|
136
|
+
}
|
|
137
|
+
const view = new DataView(host.#view().buffer, outPtr, 16);
|
|
138
|
+
view.setBigUint64(0, BigInt(Math.round(result.cycles ?? 0)), true);
|
|
139
|
+
view.setUint32(8, STOP_CODES[result.stop] ?? 0, true);
|
|
140
|
+
return 16;
|
|
141
|
+
},
|
|
142
|
+
chip_take_outputs: (id, ptr, cap) => {
|
|
143
|
+
const chip = host.#chip(id);
|
|
144
|
+
if (!chip) return 0;
|
|
145
|
+
let outputs;
|
|
146
|
+
try {
|
|
147
|
+
outputs = chip.takeOutputs();
|
|
148
|
+
} catch (error) {
|
|
149
|
+
host.#lastError = `chip take-outputs: ${error?.message ?? error}`;
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
const count = Math.min(outputs.length, Math.floor(cap / 12));
|
|
153
|
+
const view = new DataView(host.#view().buffer);
|
|
154
|
+
for (let index = 0; index < count; index += 1) {
|
|
155
|
+
const output = outputs[index];
|
|
156
|
+
const base = ptr + index * 12;
|
|
157
|
+
view.setUint32(base, output.pin, true);
|
|
158
|
+
view.setUint32(base + 4, LEVEL_WORDS.indexOf(output.level) < 0 ? 2 : LEVEL_WORDS.indexOf(output.level), true);
|
|
159
|
+
view.setUint32(base + 8, output.enabled === false ? 0 : 1, true);
|
|
160
|
+
}
|
|
161
|
+
return count * 12;
|
|
162
|
+
},
|
|
163
|
+
chip_write_console: (id, ptr, len) => {
|
|
164
|
+
try {
|
|
165
|
+
host.#chip(id)?.writeConsole(host.#read(ptr, len));
|
|
166
|
+
} catch (error) {
|
|
167
|
+
host.#lastError = `chip write-console: ${error?.message ?? error}`;
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The memory and exports of the module that imports this host. */
|
|
174
|
+
attach(memory, exports) {
|
|
175
|
+
this.#memory = memory;
|
|
176
|
+
this.#exports = exports;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Release the buffers the 0.2 host-call bridge keeps between calls. */
|
|
180
|
+
release() {
|
|
181
|
+
if (!this.#exports) return;
|
|
182
|
+
if (this.#callInPtr !== 0) this.#exports.litz_free(this.#callInPtr, this.#callInSize);
|
|
183
|
+
if (this.#callOutPtr !== 0) this.#exports.litz_free(this.#callOutPtr, this.#callOutSize);
|
|
184
|
+
this.#callInPtr = 0;
|
|
185
|
+
this.#callInSize = 0;
|
|
186
|
+
this.#callOutPtr = 0;
|
|
187
|
+
this.#callOutSize = 0;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Make module names loadable for one session. Returns what it replaced, so
|
|
192
|
+
* a failed boot can put the previous maps back.
|
|
193
|
+
*/
|
|
194
|
+
register({ parts = {}, boards = {} } = {}) {
|
|
195
|
+
const previous = { parts: new Map(), boards: new Map() };
|
|
196
|
+
for (const [name, factory] of Object.entries(parts)) {
|
|
197
|
+
if (typeof factory !== "function") {
|
|
198
|
+
throw new LitzError(`the factory for ${name} must be a function`);
|
|
199
|
+
}
|
|
200
|
+
previous.parts.set(name, this.#factories.get(name));
|
|
201
|
+
this.#factories.set(name, factory);
|
|
202
|
+
}
|
|
203
|
+
for (const [name, factory] of Object.entries(boards)) {
|
|
204
|
+
if (typeof factory !== "function") {
|
|
205
|
+
throw new LitzError(`the factory for ${name} must be a function`);
|
|
206
|
+
}
|
|
207
|
+
previous.boards.set(name, this.#chipFactories.get(name));
|
|
208
|
+
this.#chipFactories.set(name, factory);
|
|
209
|
+
}
|
|
210
|
+
return previous;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Put back the factories `register` replaced. Live instances are kept:
|
|
214
|
+
* sessions drop their own through `part_drop` and `chip_drop`. */
|
|
215
|
+
restore(previous) {
|
|
216
|
+
for (const [name, factory] of previous.parts) {
|
|
217
|
+
if (factory === undefined) this.#factories.delete(name);
|
|
218
|
+
else this.#factories.set(name, factory);
|
|
219
|
+
}
|
|
220
|
+
for (const [name, factory] of previous.boards) {
|
|
221
|
+
if (factory === undefined) this.#chipFactories.delete(name);
|
|
222
|
+
else this.#chipFactories.set(name, factory);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
#part(id) {
|
|
227
|
+
return this.#instances.get(id);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#chip(id) {
|
|
231
|
+
return this.#chips.get(id);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
#u32(ptr) {
|
|
235
|
+
return new DataView(this.#view().buffer).getUint32(ptr, true);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The services a chip adapter calls while it runs. `console` and `fault`
|
|
240
|
+
* are the guest's two host imports; both are buffered inside the module,
|
|
241
|
+
* which is where the session reads them from.
|
|
242
|
+
*/
|
|
243
|
+
#chipHost(id) {
|
|
244
|
+
return {
|
|
245
|
+
console: (bytes) => {
|
|
246
|
+
const data = typeof bytes === "string" ? ENCODER.encode(bytes) : bytes;
|
|
247
|
+
if (data.length === 0) return;
|
|
248
|
+
const ptr = this.#exports.litz_alloc(data.length);
|
|
249
|
+
if (ptr === 0) return;
|
|
250
|
+
try {
|
|
251
|
+
this.#view().set(data, ptr);
|
|
252
|
+
this.#exports.litz_guest_console(id, ptr, data.length);
|
|
253
|
+
} finally {
|
|
254
|
+
this.#exports.litz_free(ptr, data.length);
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
fault: (detail) => {
|
|
258
|
+
const data = ENCODER.encode(String(detail));
|
|
259
|
+
const ptr = this.#exports.litz_alloc(data.length);
|
|
260
|
+
if (ptr === 0) return;
|
|
261
|
+
try {
|
|
262
|
+
this.#view().set(data, ptr);
|
|
263
|
+
this.#exports.litz_guest_fault(id, ptr, data.length);
|
|
264
|
+
} finally {
|
|
265
|
+
this.#exports.litz_free(ptr, data.length);
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
/** A 0.2 host call, answered by the circuit inside the module. */
|
|
269
|
+
call: (op, args) => this.#guestCall(id, op, args),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Ask the module to answer one `litz:mcu@0.2.0` host call. The module
|
|
275
|
+
* returns the reply's full length; when the buffer was too small, it is
|
|
276
|
+
* grown and the call is repeated.
|
|
277
|
+
*/
|
|
278
|
+
#guestCall(id, op, args) {
|
|
279
|
+
const exports = this.#exports;
|
|
280
|
+
if (!exports || typeof exports.litz_guest_call !== "function") {
|
|
281
|
+
throw new LitzError("this module was built without the host-call bridge");
|
|
282
|
+
}
|
|
283
|
+
const opBytes = ENCODER.encode(op);
|
|
284
|
+
const argBytes = ENCODER.encode(JSON.stringify(args ?? {}));
|
|
285
|
+
const need = opBytes.length + argBytes.length;
|
|
286
|
+
if (this.#callInSize < need) {
|
|
287
|
+
if (this.#callInPtr !== 0) exports.litz_free(this.#callInPtr, this.#callInSize);
|
|
288
|
+
this.#callInSize = Math.max(need, 256);
|
|
289
|
+
this.#callInPtr = exports.litz_alloc(this.#callInSize);
|
|
290
|
+
if (this.#callInPtr === 0) {
|
|
291
|
+
this.#callInSize = 0;
|
|
292
|
+
throw new LitzError(`could not allocate ${need} bytes for a host call`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const input = this.#view();
|
|
296
|
+
input.set(opBytes, this.#callInPtr);
|
|
297
|
+
input.set(argBytes, this.#callInPtr + opBytes.length);
|
|
298
|
+
for (;;) {
|
|
299
|
+
if (this.#callOutSize === 0) {
|
|
300
|
+
this.#callOutSize = 4096;
|
|
301
|
+
this.#callOutPtr = exports.litz_alloc(this.#callOutSize);
|
|
302
|
+
if (this.#callOutPtr === 0) {
|
|
303
|
+
this.#callOutSize = 0;
|
|
304
|
+
throw new LitzError("could not allocate a host-call reply buffer");
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const length = exports.litz_guest_call(
|
|
308
|
+
id,
|
|
309
|
+
this.#callInPtr,
|
|
310
|
+
opBytes.length,
|
|
311
|
+
this.#callInPtr + opBytes.length,
|
|
312
|
+
argBytes.length,
|
|
313
|
+
this.#callOutPtr,
|
|
314
|
+
this.#callOutSize,
|
|
315
|
+
);
|
|
316
|
+
if (length > this.#callOutSize) {
|
|
317
|
+
exports.litz_free(this.#callOutPtr, this.#callOutSize);
|
|
318
|
+
this.#callOutSize = length;
|
|
319
|
+
this.#callOutPtr = exports.litz_alloc(length);
|
|
320
|
+
if (this.#callOutPtr === 0) {
|
|
321
|
+
this.#callOutSize = 0;
|
|
322
|
+
throw new LitzError(`could not allocate ${length} bytes for a host-call reply`);
|
|
323
|
+
}
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const text = DECODER.decode(
|
|
327
|
+
this.#view().subarray(this.#callOutPtr, this.#callOutPtr + length),
|
|
328
|
+
);
|
|
329
|
+
let reply;
|
|
330
|
+
try {
|
|
331
|
+
reply = JSON.parse(text);
|
|
332
|
+
} catch (error) {
|
|
333
|
+
throw new LitzError(`host call ${op} returned invalid JSON: ${error.message}`);
|
|
334
|
+
}
|
|
335
|
+
if (!reply.ok) throw new LitzError(reply.error);
|
|
336
|
+
return reply.value;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
#createChip(namePtr, nameLen) {
|
|
341
|
+
this.#lastError = "";
|
|
342
|
+
const name = DECODER.decode(this.#read(namePtr, nameLen));
|
|
343
|
+
const factory = this.#chipFactories.get(name);
|
|
344
|
+
if (!factory) {
|
|
345
|
+
this.#lastError = `the host has no runtime for module ${name}`;
|
|
346
|
+
return 0;
|
|
347
|
+
}
|
|
348
|
+
const id = this.#nextId;
|
|
349
|
+
this.#nextId += 1;
|
|
350
|
+
let chip;
|
|
351
|
+
try {
|
|
352
|
+
chip = factory(this.#chipHost(id));
|
|
353
|
+
} catch (error) {
|
|
354
|
+
this.#lastError = `instantiating ${name} failed: ${error?.message ?? error}`;
|
|
355
|
+
return 0;
|
|
356
|
+
}
|
|
357
|
+
if (!chip || typeof chip !== "object") {
|
|
358
|
+
this.#lastError = `the factory for ${name} did not return a chip`;
|
|
359
|
+
return 0;
|
|
360
|
+
}
|
|
361
|
+
this.#chips.set(id, chip);
|
|
362
|
+
return id;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
#create(namePtr, nameLen) {
|
|
366
|
+
this.#lastError = "";
|
|
367
|
+
const name = DECODER.decode(this.#read(namePtr, nameLen));
|
|
368
|
+
const factory = this.#factories.get(name);
|
|
369
|
+
if (!factory) {
|
|
370
|
+
this.#lastError = `the host has no runtime for module ${name}`;
|
|
371
|
+
return 0;
|
|
372
|
+
}
|
|
373
|
+
let instance;
|
|
374
|
+
try {
|
|
375
|
+
instance = factory();
|
|
376
|
+
} catch (error) {
|
|
377
|
+
this.#lastError = `instantiating ${name} failed: ${error?.message ?? error}`;
|
|
378
|
+
return 0;
|
|
379
|
+
}
|
|
380
|
+
if (!instance || typeof instance !== "object") {
|
|
381
|
+
this.#lastError = `the factory for ${name} did not return a part`;
|
|
382
|
+
return 0;
|
|
383
|
+
}
|
|
384
|
+
const id = this.#nextId;
|
|
385
|
+
this.#nextId += 1;
|
|
386
|
+
this.#instances.set(id, instance);
|
|
387
|
+
return id;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
#view() {
|
|
391
|
+
if (!this.#memory) throw new LitzError("the host bridge is not attached to a module");
|
|
392
|
+
return new Uint8Array(this.#memory.buffer);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
#read(ptr, len) {
|
|
396
|
+
return new Uint8Array(this.#view().slice(ptr, ptr + len));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
#write(ptr, cap, bytes) {
|
|
400
|
+
const n = Math.min(cap, bytes.length);
|
|
401
|
+
if (n > 0) this.#view().set(bytes.subarray(0, n), ptr);
|
|
402
|
+
return n;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function packDrive(drive) {
|
|
407
|
+
if (!drive) return 2 | (1 << 8);
|
|
408
|
+
if (typeof drive === "string") {
|
|
409
|
+
const level = LEVEL_WORDS.indexOf(drive);
|
|
410
|
+
return (level < 0 ? 2 : level) | (1 << 8);
|
|
411
|
+
}
|
|
412
|
+
const level = LEVEL_WORDS.indexOf(drive.level);
|
|
413
|
+
const strength = drive.strength === "weak" ? 1 : 0;
|
|
414
|
+
return (level < 0 ? 2 : level) | (strength << 8);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function packState(state) {
|
|
418
|
+
if (!state) return [0, 0];
|
|
419
|
+
const kind = PART_STATE_KINDS[state.kind] ?? 0;
|
|
420
|
+
return [kind, state.value ?? 0];
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export { HostRuntime };
|