1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// The AssemblyScript every code-mode program compiles against, as abi.ts: Json (from socialweb-computer-emdash's
// studio abi.as), print, and Run (one atlibs step-protocol event). A string, so browsers and Node load it alike.
export const ABI = String.raw`
export enum Kind {
Null,
Bool,
Number,
String,
Array,
Object,
}
export class Json {
kind: Kind = Kind.Null;
bool: bool = false;
num: f64 = 0;
str: string = "";
items: Json[] = [];
keys: string[] = [];
values: Json[] = [];
static null(): Json {
return new Json();
}
static bool(value: bool): Json {
const json = new Json();
json.kind = Kind.Bool;
json.bool = value;
return json;
}
static number(value: f64): Json {
const json = new Json();
json.kind = Kind.Number;
json.num = value;
return json;
}
static string(value: string): Json {
const json = new Json();
json.kind = Kind.String;
json.str = value;
return json;
}
static array(items: Json[]): Json {
const json = new Json();
json.kind = Kind.Array;
json.items = items;
return json;
}
static object(): Json {
const json = new Json();
json.kind = Kind.Object;
return json;
}
static parse(text: string): Json {
const parser = new Parser(text);
const value = parser.value();
parser.space();
if (parser.at < text.length) throw new Error("trailing characters in JSON");
return value;
}
get isNull(): bool {
return this.kind == Kind.Null;
}
get isBool(): bool {
return this.kind == Kind.Bool;
}
get isNumber(): bool {
return this.kind == Kind.Number;
}
get isString(): bool {
return this.kind == Kind.String;
}
get isArray(): bool {
return this.kind == Kind.Array;
}
get isObject(): bool {
return this.kind == Kind.Object;
}
// an array's items, an object's keys, a string's characters; 0 for the rest
get length(): i32 {
if (this.kind == Kind.Array) return this.items.length;
if (this.kind == Kind.Object) return this.keys.length;
if (this.kind == Kind.String) return this.str.length;
return 0;
}
has(key: string): bool {
return this.keys.includes(key);
}
get(key: string): Json {
for (let i = 0; i < this.keys.length; i++) if (this.keys[i] == key) return this.values[i];
return new Json();
}
at(index: i32): Json {
return index >= 0 && index < this.items.length ? this.items[index] : new Json();
}
set(key: string, value: Json): Json {
for (let i = 0; i < this.keys.length; i++) {
if (this.keys[i] == key) {
this.values[i] = value;
return this;
}
}
this.keys.push(key);
this.values.push(value);
return this;
}
push(value: Json): Json {
this.items.push(value);
return this;
}
toString(): string {
switch (this.kind) {
case Kind.Bool:
return this.bool ? "true" : "false";
case Kind.Number:
return isFinite(this.num) ? (this.num == Math.floor(this.num) && Math.abs(this.num) < 9007199254740992 ? i64(this.num).toString() : this.num.toString()) : "null";
case Kind.String:
return quote(this.str);
case Kind.Array: {
const parts = new Array<string>(this.items.length);
for (let i = 0; i < this.items.length; i++) parts[i] = this.items[i].toString();
return "[" + parts.join(",") + "]";
}
case Kind.Object: {
const parts = new Array<string>(this.keys.length);
for (let i = 0; i < this.keys.length; i++) parts[i] = quote(this.keys[i]) + ":" + this.values[i].toString();
return "{" + parts.join(",") + "}";
}
default:
return "null";
}
}
}
// Runs of plain characters are copied whole (substring), escapes added between them: the stub runtime
// never frees, so building a string a character at a time would cost memory quadratic in its length.
function quote(value: string): string {
const parts: string[] = ['"'];
let from = 0;
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i);
if (code != 0x22 && code != 0x5c && code >= 0x20) continue;
if (i > from) parts.push(value.substring(from, i));
from = i + 1;
if (code == 0x22) parts.push('\\"');
else if (code == 0x5c) parts.push("\\\\");
else if (code == 0x0a) parts.push("\\n");
else if (code == 0x0d) parts.push("\\r");
else if (code == 0x09) parts.push("\\t");
else parts.push("\\u" + code.toString(16).padStart(4, "0"));
}
if (value.length > from) parts.push(value.substring(from));
parts.push('"');
return parts.join("");
}
class Parser {
at: i32 = 0;
constructor(public text: string) {}
space(): void {
while (this.at < this.text.length) {
const code = this.text.charCodeAt(this.at);
if (code != 0x20 && code != 0x0a && code != 0x0d && code != 0x09) return;
this.at++;
}
}
expect(word: string): void {
if (this.text.substr(this.at, word.length) != word) throw new Error("invalid JSON at " + this.at.toString());
this.at += word.length;
}
value(): Json {
this.space();
if (this.at >= this.text.length) throw new Error("unexpected end of JSON");
const code = this.text.charCodeAt(this.at);
if (code == 0x7b) return this.object();
if (code == 0x5b) return this.array();
if (code == 0x22) return Json.string(this.string());
if (code == 0x74) {
this.expect("true");
return Json.bool(true);
}
if (code == 0x66) {
this.expect("false");
return Json.bool(false);
}
if (code == 0x6e) {
this.expect("null");
return Json.null();
}
return this.number();
}
take(code: i32): bool {
this.space();
if (this.at < this.text.length && this.text.charCodeAt(this.at) == code) {
this.at++;
return true;
}
return false;
}
object(): Json {
const json = Json.object();
this.at++;
if (this.take(0x7d)) return json;
do {
this.space();
const key = this.string();
this.space();
this.expect(":");
json.set(key, this.value());
} while (this.take(0x2c));
this.space();
this.expect("}");
return json;
}
array(): Json {
const json = Json.array([]);
this.at++;
if (this.take(0x5d)) return json;
do {
json.push(this.value());
} while (this.take(0x2c));
this.space();
this.expect("]");
return json;
}
string(): string {
this.expect('"');
const parts: string[] = [];
let from = this.at;
while (this.at < this.text.length) {
const code = this.text.charCodeAt(this.at);
if (code != 0x22 && code != 0x5c) {
this.at++;
continue;
}
if (this.at > from) parts.push(this.text.substring(from, this.at));
this.at++;
if (code == 0x22) return parts.join("");
const escape = this.text.charCodeAt(this.at++);
if (escape == 0x6e) parts.push("\n");
else if (escape == 0x72) parts.push("\r");
else if (escape == 0x74) parts.push("\t");
else if (escape == 0x62) parts.push("\b");
else if (escape == 0x66) parts.push("\f");
else if (escape == 0x75) {
parts.push(String.fromCharCode(<i32>parseInt(this.text.substr(this.at, 4), 16)));
this.at += 4;
} else parts.push(String.fromCharCode(escape));
from = this.at;
}
throw new Error("unterminated string in JSON");
}
number(): Json {
const start = this.at;
while (this.at < this.text.length) {
const code = this.text.charCodeAt(this.at);
if ((code >= 0x30 && code <= 0x39) || code == 0x2d || code == 0x2b || code == 0x2e || code == 0x65 || code == 0x45) this.at++;
else break;
}
if (start == this.at) throw new Error("invalid JSON at " + start.toString());
return Json.number(parseFloat(this.text.substring(start, this.at)));
}
}
export function alloc(len: i32): usize {
return heap.alloc(<usize>len);
}
export function input(ptr: usize, len: i32): Json {
return Json.parse(String.UTF8.decodeUnsafe(ptr, <usize>len));
}
export function output(value: Json): i64 {
const bytes = String.UTF8.encode(value.toString());
return (i64(changetype<usize>(bytes)) << 32) | i64(bytes.byteLength);
}
// stdout: print() appends a line; kept across events in the run's state, at most OUT_MAX characters.
const OUT_MAX = 65536;
let OUT: string[] = [];
let OUT_LEN = 0;
export function print(text: string): void {
if (OUT_LEN >= OUT_MAX) return;
const line = text.length + 1 > OUT_MAX - OUT_LEN ? text.substring(0, OUT_MAX - OUT_LEN - 1) : text;
OUT.push(line + "\n");
OUT_LEN += line.length + 1;
}
// One event of the run: the tool's input, the event (start, or a call's result) and the state main kept.
export class Run {
input: Json;
event: Json;
state: Json;
now: string;
calls: Json[] = [];
value: Json = Json.null();
finished: bool = false;
constructor(step: Json) {
this.input = step.get("input");
this.event = step.get("event");
this.now = step.get("now").str;
const saved = step.get("state");
this.state = saved.get("s");
const out = saved.get("out").str;
OUT = out.length ? [out] : [];
OUT_LEN = out.length;
}
get start(): bool {
return this.event.get("type").str == "start";
}
// a call's result: id, ok, output or error
get id(): string {
return this.event.get("id").str;
}
get ok(): bool {
return this.event.get("ok").bool;
}
get output(): Json {
return this.event.get("output");
}
get error(): string {
return this.event.get("error").str;
}
// a procedure (input); service: an https origin or a DID, "" for the session's PDS
call(id: string, nsid: string, input: Json, service: string = ""): void {
const c = Json.object().set("id", Json.string(id)).set("nsid", Json.string(nsid)).set("input", input);
if (service.length) c.set("service", Json.string(service));
this.calls.push(c);
}
// a query (params, string values)
query(id: string, nsid: string, params: Json, service: string = ""): void {
const c = Json.object().set("id", Json.string(id)).set("nsid", Json.string(nsid)).set("params", params);
if (service.length) c.set("service", Json.string(service));
this.calls.push(c);
}
finish(value: Json): void {
this.value = value;
this.finished = true;
}
out(): i64 {
const stdout = Json.string(OUT.join(""));
const out = Json.object()
.set("calls", Json.array(this.calls))
.set("state", Json.object().set("s", this.state).set("out", stdout))
.set("result", Json.object().set("stdout", stdout).set("value", this.value));
if (this.finished) out.set("done", Json.bool(true));
return output(out);
}
}
`;
// The entry the host runs: one event in, main(run), the run's output out.
export const ENTRY = String.raw`import { Run, input } from "./abi";
import { main } from "./code";
export { alloc } from "./abi";
export function run(ptr: usize, len: i32): i64 {
const r = new Run(input(ptr, len));
main(r);
return r.out();
}
`;