-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
44 lines (37 loc) · 852 Bytes
/
Copy pathcommon.ts
File metadata and controls
44 lines (37 loc) · 852 Bytes
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
export type Output = {
errors: string;
warnings: string;
output: string;
bytecode: string;
trace: string;
ast: string;
};
export type EvaluatorOptions = {
strict?: boolean;
};
export interface Evaluator {
eval(source: string, location?: string): Promise<Output>;
}
const outputFields = [
"errors",
"warnings",
"output",
"bytecode",
"trace",
"ast",
] as const;
export function parseOutput(value: string): Output {
const output: unknown = JSON.parse(value);
if (!isOutput(output)) {
throw new Error("wasm evaluator returned invalid output");
}
return output;
}
function isOutput(value: unknown): value is Output {
const output = value as Record<string, unknown>;
return (
typeof value === "object" &&
value !== null &&
outputFields.every((field) => typeof output[field] === "string")
);
}