-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm32-modulewriter.js
267 lines (235 loc) · 8.83 KB
/
wasm32-modulewriter.js
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
var Wasm32ModuleWriter=function(){
this._types=[]; // these are real uint8arrays. the rest are writers
this._imports=[];
this._functions=[];
this._memory=[];
this._exports=[];
this._codes=[];
};
Wasm32ModuleWriter.sectionCode={
TYPE:0x01,
IMPORT:0x02,
FUNCTION:0x03,
TABLE:0x04,
MEMORY:0x05,
GLOBAL:0x06,
EXPORT:0x07,
START:0x08,
ELEMENT:0x09,
CODE:0x0A,
DATA:0x0B,
};
/*Wasm32ModuleWriter.prototype.addLogicalFunction=function(logical_function){
var typeIndex=this._types.length;
var funcIndex=this._functions.length;
this._types.push(logical_function._type);
this._functions.push(VLQEncoder.encodeUInt(typeIndex));
this._codes.push(logical_function._code);
if(logical_function._exportName){
var encodeUIntd_name=encodeUIntString(logical_function._exportName);
var export_def=new ResizableUint8Array();
export_def.append(VLQEncoder.encodeUInt(encodeUIntd_name.length));
export_def.append(encodeUIntd_name);
export_def.push(0);
export_def.append(VLQEncoder.encodeUInt(funcIndex));
this._exports.push(export_def.toUint8Array());
}
};*/
// memory: Wasm32MemoryWriter
Wasm32ModuleWriter.prototype.setMemory=function(memory){
// currently only 1 memory is allowed.
this._memory=[memory];
};
// name: string, field: string (external)
Wasm32ModuleWriter.prototype.exportFunction=function(name,field){
field=field||name;
var exportWriter=new Wasm32ExportWriter(field,Wasm32ExternalKind.function);
exportWriter.setName(name);
this._exports.push(exportWriter);
};
// name: string, module: string (external), field: string (external)
Wasm32ModuleWriter.prototype.importFunction=function(name,type,module,field){
var importWriter=new Wasm32ImportWriter(module,field,Wasm32ExternalKind.function);
importWriter.setName(name);
importWriter.setType(type);
this._imports.push(importWriter);
};
// name: string, type: Wasm32TypeWriter, code: Wasm32CodeWriter
Wasm32ModuleWriter.prototype.addFunction=function(name,type,codeWriter){
codeWriter.setName(name);
codeWriter.setType(type);
this._codes.push(codeWriter);
};
Wasm32ModuleWriter.prototype.generateModule=function(){
// TODO: fix imports
// func index should be imports then functions
// calling and exporting uses this combined func index
var funcTypes=[];
var funcTypesOffset=this._types.length;
var funcTypesEqComp=function(type_data){
return function(el){
if(el.length!=type_data.length)return false;
for(var i=0;i<el.length;++i){
if(el[i]!=type_data[i])return false;
}
return true;
};
};
var funcNames=[]; // {name:string, funcType:uint8array}
var funcNamesOffset=this._functions.length;
this._imports.forEach(function(obj){
var name=obj._functionname;
if(name){
if(funcNames.findIndex(function(el){return el.name===name;})===-1)funcNames.push({name:name,funcType:obj._functiontype});
else throw "Repeated function \""+name+"\".";
}
});
this._codes.forEach(function(obj){
var name=obj._functionname;
if(name){
if(funcNames.findIndex(function(el){return el.name===name;})===-1)funcNames.push({name:name,funcType:obj._functiontype});
else throw "Repeated function \""+name+"\".";
}
});
funcNames.forEach(function(el){
if(funcTypes.findIndex(funcTypesEqComp(el.funcType))===-1)funcTypes.push(el.funcType);
});
// generate the types (these are NOT writers, they are uint8arrays)
var that=this;
funcTypes.forEach(function(type){
that._types.push(type);
});
// generate the FunctionWriters
var that=this;
this._codes.forEach(function(obj){
var type=obj._functiontype;
if(type){
var typeIndex=funcTypes.findIndex(funcTypesEqComp(obj._functiontype))+funcTypesOffset;
if(typeIndex===-1)throw "Weird assembler bug.";
var functionWriter=new Wasm32FunctionWriter(typeIndex);
that._functions.push(functionWriter);
}
});
// resolve imports
this._imports.forEach(function(obj){
var type=obj._functiontype;
if(type){
var typeIndex=funcTypes.findIndex(funcTypesEqComp(type))+funcTypesOffset;
if(typeIndex===-1)throw "Weird assembler bug.";
obj._type=typeIndex;
}
});
// resolve functionlinks in code
this._codes.forEach(function(obj){
var functionLinks=obj._functionlinks;
functionLinks.sort(function(a,b){
return b.location-a.location; // sorts by location back to front
});
functionLinks.forEach(function(functionLink){
var funcIndex=funcNames.findIndex(function(el){
return el.name===functionLink.name;
})+funcNamesOffset;
if(funcIndex===-1)throw "Undeclared function \""+functionLink.name+"\".";
obj._data.insert_arr(functionLink.location,VLQEncoder.encodeUInt(funcIndex));
});
});
// resolve exports
this._exports.forEach(function(obj){
var name=obj._functionname;
if(name){
var funcIndex=funcNames.findIndex(function(el){
return el.name===name;
})+funcNamesOffset;
if(funcIndex===-1)throw "Undeclared function \""+functionLink.name+"\".";
obj._index=funcIndex;
}
});
// remove _functionname and _functiontype fields from CodeWriter and ImportWriter and ExportWriter
this._exports.forEach(function(obj){
if(obj._functionname)obj._functionname=undefined;
});
this._imports.forEach(function(obj){
if(obj._functionname){
obj._functionname=undefined;
obj._functiontype=undefined;
}
});
this._codes.forEach(function(obj){
if(obj._functionname){
obj._functionname=undefined;
obj._functiontype=undefined;
}
});
var output=new ResizableUint8Array();
var wasm_header=new Uint8Array(8);
wasm_header[0]=0x00;
wasm_header[1]=0x61;
wasm_header[2]=0x73;
wasm_header[3]=0x6d;
wasm_header[4]=0x01;
wasm_header[5]=0x00;
wasm_header[6]=0x00;
wasm_header[7]=0x00;
output.append(wasm_header);
// TYPE
if(this._types.length>0){
output.push(Wasm32ModuleWriter.sectionCode.TYPE);
var sizeloc=output.size();
output.append(VLQEncoder.encodeUInt(this._types.length));
for(var i=0;i<this._types.length;++i){
output.append(this._types[i]);
}
output.insert_arr(sizeloc,VLQEncoder.encodeUInt(output.size()-sizeloc));
}
// IMPORT
if(this._imports.length>0){
output.push(Wasm32ModuleWriter.sectionCode.IMPORT);
var sizeloc=output.size();
output.append(VLQEncoder.encodeUInt(this._imports.length));
for(var i=0;i<this._imports.length;++i){
output.append(this._imports[i].toUint8Array());
}
output.insert_arr(sizeloc,VLQEncoder.encodeUInt(output.size()-sizeloc));
}
// FUNCTION
if(this._functions.length>0){
output.push(Wasm32ModuleWriter.sectionCode.FUNCTION);
var sizeloc=output.size();
output.append(VLQEncoder.encodeUInt(this._functions.length));
for(var i=0;i<this._functions.length;++i){
output.append(this._functions[i].toUint8Array());
}
output.insert_arr(sizeloc,VLQEncoder.encodeUInt(output.size()-sizeloc));
}
// MEMORY
if(this._memory.length>0){
output.push(Wasm32ModuleWriter.sectionCode.MEMORY);
var sizeloc=output.size();
output.append(VLQEncoder.encodeUInt(this._memory.length));
for(var i=0;i<this._memory.length;++i){
output.append(this._memory[i].toUint8Array());
}
output.insert_arr(sizeloc,VLQEncoder.encodeUInt(output.size()-sizeloc));
}
// EXPORT
if(this._exports.length>0){
output.push(Wasm32ModuleWriter.sectionCode.EXPORT);
var sizeloc=output.size();
output.append(VLQEncoder.encodeUInt(this._exports.length));
for(var i=0;i<this._exports.length;++i){
output.append(this._exports[i].toUint8Array());
}
output.insert_arr(sizeloc,VLQEncoder.encodeUInt(output.size()-sizeloc));
}
// CODE
if(this._codes.length>0){
output.push(Wasm32ModuleWriter.sectionCode.CODE);
var sizeloc=output.size();
output.append(VLQEncoder.encodeUInt(this._codes.length));
for(var i=0;i<this._codes.length;++i){
output.append(this._codes[i].toUint8Array());
}
output.insert_arr(sizeloc,VLQEncoder.encodeUInt(output.size()-sizeloc));
}
return output.toUint8Array();
};