Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 function WasmFunctionBuilder(name, sig_index) { | 5 function WasmFunctionBuilder(name, sig_index) { |
| 6 this.name = name; | 6 this.name = name; |
| 7 this.sig_index = sig_index; | 7 this.sig_index = sig_index; |
| 8 this.exports = []; | 8 this.exports = []; |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 } | 142 } |
| 143 | 143 |
| 144 WasmModuleBuilder.prototype.toArray = function(debug) { | 144 WasmModuleBuilder.prototype.toArray = function(debug) { |
| 145 // Add header bytes | 145 // Add header bytes |
| 146 var bytes = []; | 146 var bytes = []; |
| 147 bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3, | 147 bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3, |
| 148 kWasmV0, kWasmV1, kWasmV2, kWasmV3]); | 148 kWasmV0, kWasmV1, kWasmV2, kWasmV3]); |
| 149 | 149 |
| 150 var wasm = this; | 150 var wasm = this; |
| 151 | 151 |
| 152 // Add memory section | |
| 153 if (wasm.memory != undefined) { | |
| 154 if (debug) print("emitting memory @ " + bytes.length); | |
| 155 emit_section(bytes, kDeclMemory, function(bytes) { | |
| 156 emit_varint(bytes, wasm.memory.min); | |
| 157 emit_varint(bytes, wasm.memory.max); | |
| 158 emit_u8(bytes, wasm.memory.exp ? 1 : 0); | |
| 159 }); | |
| 160 } | |
| 161 | |
| 162 // Add signatures section | 152 // Add signatures section |
| 163 if (wasm.signatures.length > 0) { | 153 if (wasm.signatures.length > 0) { |
| 164 if (debug) print("emitting signatures @ " + bytes.length); | 154 if (debug) print("emitting signatures @ " + bytes.length); |
| 165 emit_section(bytes, kDeclSignatures, function(bytes) { | 155 emit_section(bytes, kDeclSignatures, function(bytes) { |
| 166 emit_varint(bytes, wasm.signatures.length); | 156 emit_varint(bytes, wasm.signatures.length); |
| 167 for (sig of wasm.signatures) { | 157 for (sig of wasm.signatures) { |
| 168 var params = sig.length - 1; | 158 var params = sig.length - 1; |
| 169 emit_varint(bytes, params); | 159 emit_varint(bytes, params); |
| 170 for (var j = 0; j < sig.length; j++) { | 160 for (var j = 0; j < sig.length; j++) { |
| 171 emit_u8(bytes, sig[j]); | 161 emit_u8(bytes, sig[j]); |
| 172 } | 162 } |
| 173 } | 163 } |
| 174 }); | 164 }); |
| 175 } | 165 } |
| 176 | 166 |
| 177 // Add imports section | 167 // Add imports section |
| 178 if (wasm.imports.length > 0) { | 168 if (wasm.imports.length > 0) { |
| 179 if (debug) print("emitting imports @ " + bytes.length); | 169 if (debug) print("emitting imports @ " + bytes.length); |
| 180 emit_section(bytes, kDeclImportTable, function(bytes) { | 170 emit_section(bytes, kDeclImportTable, function(bytes) { |
| 181 emit_varint(bytes, wasm.imports.length); | 171 emit_varint(bytes, wasm.imports.length); |
| 182 for (imp of wasm.imports) { | 172 for (imp of wasm.imports) { |
| 183 emit_varint(bytes, imp.sig_index); | 173 emit_varint(bytes, imp.sig_index); |
| 184 emit_string(bytes, imp.module); | 174 emit_string(bytes, imp.module); |
| 185 emit_string(bytes, imp.name || ''); | 175 emit_string(bytes, imp.name || ''); |
| 186 } | 176 } |
| 187 }); | 177 }); |
| 188 } | 178 } |
| 189 | 179 |
| 190 // Add functions section | 180 // Add functions declaractions |
|
JF
2016/04/19 15:48:47
typo
titzer
2016/04/20 08:51:24
Done.
| |
| 191 var names = false; | 181 var names = false; |
| 192 var exports = 0; | 182 var exports = 0; |
| 193 if (wasm.functions.length > 0) { | 183 if (wasm.functions.length > 0) { |
| 194 var has_names = false; | 184 var has_names = false; |
| 195 | 185 |
| 196 // emit function signatures | 186 // emit function signatures |
| 197 if (debug) print("emitting function sigs @ " + bytes.length); | 187 if (debug) print("emitting function sigs @ " + bytes.length); |
| 198 emit_section(bytes, kDeclFunctionSignatures, function(bytes) { | 188 emit_section(bytes, kDeclFunctionSignatures, function(bytes) { |
| 199 emit_varint(bytes, wasm.functions.length); | 189 emit_varint(bytes, wasm.functions.length); |
| 200 for (func of wasm.functions) { | 190 for (func of wasm.functions) { |
| 201 has_names = has_names || (func.name != undefined && | 191 has_names = has_names || (func.name != undefined && |
| 202 func.name.length > 0); | 192 func.name.length > 0); |
| 203 exports += func.exports.length; | 193 exports += func.exports.length; |
| 204 | 194 |
| 205 emit_varint(bytes, func.sig_index); | 195 emit_varint(bytes, func.sig_index); |
| 206 } | 196 } |
| 207 }); | 197 }); |
| 208 | 198 |
| 199 } | |
| 200 | |
| 201 // Add function table. | |
| 202 if (wasm.function_table.length > 0) { | |
| 203 if (debug) print("emitting function table @ " + bytes.length); | |
| 204 emit_section(bytes, kDeclFunctionTable, function(bytes) { | |
| 205 emit_varint(bytes, wasm.function_table.length); | |
| 206 for (index of wasm.function_table) { | |
| 207 emit_varint(bytes, index); | |
| 208 } | |
| 209 }); | |
| 210 } | |
| 211 | |
| 212 // Add memory section | |
| 213 if (wasm.memory != undefined) { | |
| 214 if (debug) print("emitting memory @ " + bytes.length); | |
| 215 emit_section(bytes, kDeclMemory, function(bytes) { | |
| 216 emit_varint(bytes, wasm.memory.min); | |
| 217 emit_varint(bytes, wasm.memory.max); | |
| 218 emit_u8(bytes, wasm.memory.exp ? 1 : 0); | |
| 219 }); | |
| 220 } | |
| 221 | |
| 222 | |
| 223 // Add export table. | |
| 224 if (exports > 0) { | |
| 225 if (debug) print("emitting exports @ " + bytes.length); | |
| 226 emit_section(bytes, kDeclExportTable, function(bytes) { | |
| 227 emit_varint(bytes, exports); | |
| 228 for (func of wasm.functions) { | |
| 229 for (exp of func.exports) { | |
| 230 emit_varint(bytes, func.index); | |
| 231 emit_string(bytes, exp); | |
| 232 } | |
| 233 } | |
| 234 }); | |
| 235 } | |
| 236 | |
| 237 // Add start function section. | |
| 238 if (wasm.start_index != undefined) { | |
| 239 if (debug) print("emitting start function @ " + bytes.length); | |
| 240 emit_section(bytes, kDeclStartFunction, function(bytes) { | |
| 241 emit_varint(bytes, wasm.start_index); | |
| 242 }); | |
| 243 } | |
| 244 | |
| 245 // Add function bodies. | |
| 246 if (wasm.functions.length > 0) { | |
| 209 // emit function bodies | 247 // emit function bodies |
| 210 if (debug) print("emitting function bodies @ " + bytes.length); | 248 if (debug) print("emitting function bodies @ " + bytes.length); |
| 211 emit_section(bytes, kDeclFunctionBodies, function(bytes) { | 249 emit_section(bytes, kDeclFunctionBodies, function(bytes) { |
| 212 emit_varint(bytes, wasm.functions.length); | 250 emit_varint(bytes, wasm.functions.length); |
| 213 for (func of wasm.functions) { | 251 for (func of wasm.functions) { |
| 214 // Function body length will be patched later. | 252 // Function body length will be patched later. |
| 215 var local_decls = []; | 253 var local_decls = []; |
| 216 var l = func.locals; | 254 var l = func.locals; |
| 217 if (l != undefined) { | 255 if (l != undefined) { |
| 218 var local_decls_count = 0; | 256 var local_decls_count = 0; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 237 emit_u8(header, decl.type); | 275 emit_u8(header, decl.type); |
| 238 } | 276 } |
| 239 | 277 |
| 240 emit_varint(bytes, header.length + func.body.length); | 278 emit_varint(bytes, header.length + func.body.length); |
| 241 emit_bytes(bytes, header); | 279 emit_bytes(bytes, header); |
| 242 emit_bytes(bytes, func.body); | 280 emit_bytes(bytes, func.body); |
| 243 } | 281 } |
| 244 }); | 282 }); |
| 245 } | 283 } |
| 246 | 284 |
| 247 // emit function names | 285 // Add data segments. |
| 248 if (has_names) { | |
| 249 if (debug) print("emitting names @ " + bytes.length); | |
| 250 emit_section(bytes, kDeclNames, function(bytes) { | |
| 251 emit_varint(bytes, wasm.functions.length); | |
| 252 for (func of wasm.functions) { | |
| 253 var name = func.name == undefined ? "" : func.name; | |
| 254 emit_string(bytes, name); | |
| 255 emit_u8(bytes, 0); // local names count == 0 | |
| 256 } | |
| 257 }); | |
| 258 } | |
| 259 | |
| 260 // Add start function section. | |
| 261 if (wasm.start_index != undefined) { | |
| 262 if (debug) print("emitting start function @ " + bytes.length); | |
| 263 emit_section(bytes, kDeclStartFunction, function(bytes) { | |
| 264 emit_varint(bytes, wasm.start_index); | |
| 265 }); | |
| 266 } | |
| 267 | |
| 268 if (wasm.function_table.length > 0) { | |
| 269 if (debug) print("emitting function table @ " + bytes.length); | |
| 270 emit_section(bytes, kDeclFunctionTable, function(bytes) { | |
| 271 emit_varint(bytes, wasm.function_table.length); | |
| 272 for (index of wasm.function_table) { | |
| 273 emit_varint(bytes, index); | |
| 274 } | |
| 275 }); | |
| 276 } | |
| 277 | |
| 278 if (exports > 0) { | |
| 279 if (debug) print("emitting exports @ " + bytes.length); | |
| 280 emit_section(bytes, kDeclExportTable, function(bytes) { | |
| 281 emit_varint(bytes, exports); | |
| 282 for (func of wasm.functions) { | |
| 283 for (exp of func.exports) { | |
| 284 emit_varint(bytes, func.index); | |
| 285 emit_string(bytes, exp); | |
| 286 } | |
| 287 } | |
| 288 }); | |
| 289 } | |
| 290 | |
| 291 if (wasm.data_segments.length > 0) { | 286 if (wasm.data_segments.length > 0) { |
| 292 if (debug) print("emitting data segments @ " + bytes.length); | 287 if (debug) print("emitting data segments @ " + bytes.length); |
| 293 emit_section(bytes, kDeclDataSegments, function(bytes) { | 288 emit_section(bytes, kDeclDataSegments, function(bytes) { |
| 294 emit_varint(bytes, wasm.data_segments.length); | 289 emit_varint(bytes, wasm.data_segments.length); |
| 295 for (seg of wasm.data_segments) { | 290 for (seg of wasm.data_segments) { |
| 296 emit_varint(bytes, seg.addr); | 291 emit_varint(bytes, seg.addr); |
| 297 emit_varint(bytes, seg.data.length); | 292 emit_varint(bytes, seg.data.length); |
| 298 emit_bytes(bytes, seg.data); | 293 emit_bytes(bytes, seg.data); |
| 299 } | 294 } |
| 300 }); | 295 }); |
| 301 } | 296 } |
| 302 | 297 |
| 303 // Emit any explicitly added sections | 298 // Add any explicitly added sections |
| 304 for (exp of wasm.explicit) { | 299 for (exp of wasm.explicit) { |
| 305 if (debug) print("emitting explicit @ " + bytes.length); | 300 if (debug) print("emitting explicit @ " + bytes.length); |
| 306 emit_bytes(bytes, exp); | 301 emit_bytes(bytes, exp); |
| 307 } | 302 } |
| 308 | 303 |
| 304 // Add function names. | |
| 305 if (has_names) { | |
| 306 if (debug) print("emitting names @ " + bytes.length); | |
| 307 emit_section(bytes, kDeclNames, function(bytes) { | |
| 308 emit_varint(bytes, wasm.functions.length); | |
| 309 for (func of wasm.functions) { | |
| 310 var name = func.name == undefined ? "" : func.name; | |
| 311 emit_string(bytes, name); | |
| 312 emit_u8(bytes, 0); // local names count == 0 | |
| 313 } | |
| 314 }); | |
| 315 } | |
| 316 | |
| 309 // End the module. | 317 // End the module. |
| 310 if (debug) print("emitting end @ " + bytes.length); | 318 if (debug) print("emitting end @ " + bytes.length); |
| 311 emit_section(bytes, kDeclEnd, function(bytes) {}); | 319 emit_section(bytes, kDeclEnd, function(bytes) {}); |
| 312 | 320 |
| 313 return bytes; | 321 return bytes; |
| 314 } | 322 } |
| 315 | 323 |
| 316 WasmModuleBuilder.prototype.toBuffer = function(debug) { | 324 WasmModuleBuilder.prototype.toBuffer = function(debug) { |
| 317 var bytes = this.toArray(debug); | 325 var bytes = this.toArray(debug); |
| 318 var buffer = new ArrayBuffer(bytes.length); | 326 var buffer = new ArrayBuffer(bytes.length); |
| 319 var view = new Uint8Array(buffer); | 327 var view = new Uint8Array(buffer); |
| 320 for (var i = 0; i < bytes.length; i++) { | 328 for (var i = 0; i < bytes.length; i++) { |
| 321 var val = bytes[i]; | 329 var val = bytes[i]; |
| 322 if ((typeof val) == "string") val = val.charCodeAt(0); | 330 if ((typeof val) == "string") val = val.charCodeAt(0); |
| 323 view[i] = val | 0; | 331 view[i] = val | 0; |
| 324 } | 332 } |
| 325 return buffer; | 333 return buffer; |
| 326 } | 334 } |
| 327 | 335 |
| 328 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { | 336 WasmModuleBuilder.prototype.instantiate = function(ffi, memory) { |
| 329 var buffer = this.toBuffer(); | 337 var buffer = this.toBuffer(); |
| 330 if (memory != undefined) { | 338 if (memory != undefined) { |
| 331 return Wasm.instantiateModule(buffer, ffi, memory); | 339 return Wasm.instantiateModule(buffer, ffi, memory); |
| 332 } else { | 340 } else { |
| 333 return Wasm.instantiateModule(buffer, ffi); | 341 return Wasm.instantiateModule(buffer, ffi); |
| 334 } | 342 } |
| 335 } | 343 } |
| OLD | NEW |