-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.ts
More file actions
115 lines (101 loc) · 2.62 KB
/
builder.ts
File metadata and controls
115 lines (101 loc) · 2.62 KB
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
import {
Expr,
ExternalKind,
FuncType,
LocalVariables,
Module,
ValueType,
} from './wasm';
class BuilderError extends Error {
constructor(message: string) {
super(message);
}
}
// A function starts out Active until it's added to a module
// at which point it becomes NotActive. This is to prevent
// users from adding a function to a module, then editing the
// function further and wondering why the changes aren't in the module
enum FuncMode {
Active,
NotActive,
}
interface Func {
mode: FuncMode;
name?: string;
locals: LocalVariables;
localsCount: number;
type: FuncType;
code: Expr;
}
interface Local {
index: number;
}
export function createFunction(type: FuncType, code: Expr) {
return {
mode: FuncMode.Active,
locals: [],
localsCount: 0,
type,
code,
};
}
/**
* Creates new, empty module
*
* @returns Empty, initialized module.
*/
export function createModule(): Module {
return {
types: { id: 1, items: [] },
imports: { id: 2, items: [] },
functions: { id: 3, items: [] },
tables: { id: 4, items: [] },
memories: { id: 5, items: [] },
globals: { id: 6, items: [] },
exports: { id: 7, items: [] },
elements: { id: 9, items: [] },
code: { id: 10, items: [] },
data: { id: 11, items: [] },
customSections: [],
};
}
/**
* Takes in a module and adds a function to it.
*
* Note: Once a function is added to a module, you cannot reuse it.
* This is to prevent users from adding it to a module then editing
* the function further and wondering why the changes are not showing up.
*
* If a name is provided, we generate an export with that name
*
* @param module - A WebAssembly Module
* @param func - The function
* @returns Module with new function.
*/
export function addFunction(module: Module, func: Func): Module {
module.types.items.push(func.type);
module.functions.items.push(module.types.items.length - 1);
module.code.items.push({ locals: func.locals, code: [...func.code] });
if (func.name) {
// This works if we don't reuse type signatures. If we do, then this
// breaks.
const funcIndex = module.types.items.length - 1;
module.exports.items.push({
name: func.name,
index: funcIndex,
kind: ExternalKind.Function,
});
}
func.mode = FuncMode.NotActive;
return module;
}
export function addLocal(func: Func, type: ValueType): Local {
if (func.mode === FuncMode.NotActive) {
throw new BuilderError(
'Cannot add local to function that is no longer active'
);
}
func.locals.push({ count: 1, type });
func.localsCount += 1;
return { index: func.localsCount - 1 };
}