[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.decode = decode;
|
---|
| 7 |
|
---|
| 8 | var _helperApiError = require("@webassemblyjs/helper-api-error");
|
---|
| 9 |
|
---|
| 10 | var ieee754 = _interopRequireWildcard(require("@webassemblyjs/ieee754"));
|
---|
| 11 |
|
---|
| 12 | var utf8 = _interopRequireWildcard(require("@webassemblyjs/utf8"));
|
---|
| 13 |
|
---|
| 14 | var t = _interopRequireWildcard(require("@webassemblyjs/ast"));
|
---|
| 15 |
|
---|
| 16 | var _leb = require("@webassemblyjs/leb128");
|
---|
| 17 |
|
---|
| 18 | var _helperWasmBytecode = _interopRequireDefault(require("@webassemblyjs/helper-wasm-bytecode"));
|
---|
| 19 |
|
---|
| 20 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 21 |
|
---|
| 22 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
---|
| 23 |
|
---|
| 24 | function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
|
---|
| 25 |
|
---|
| 26 | function toHex(n) {
|
---|
| 27 | return "0x" + Number(n).toString(16);
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | function byteArrayEq(l, r) {
|
---|
| 31 | if (l.length !== r.length) {
|
---|
| 32 | return false;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | for (var i = 0; i < l.length; i++) {
|
---|
| 36 | if (l[i] !== r[i]) {
|
---|
| 37 | return false;
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | return true;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | function decode(ab, opts) {
|
---|
| 45 | var buf = new Uint8Array(ab);
|
---|
| 46 | var getUniqueName = t.getUniqueNameGenerator();
|
---|
| 47 | var offset = 0;
|
---|
| 48 |
|
---|
| 49 | function getPosition() {
|
---|
| 50 | return {
|
---|
| 51 | line: -1,
|
---|
| 52 | column: offset
|
---|
| 53 | };
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | function dump(b, msg) {
|
---|
| 57 | if (opts.dump === false) return;
|
---|
| 58 | var pad = "\t\t\t\t\t\t\t\t\t\t";
|
---|
| 59 | var str = "";
|
---|
| 60 |
|
---|
| 61 | if (b.length < 5) {
|
---|
| 62 | str = b.map(toHex).join(" ");
|
---|
| 63 | } else {
|
---|
| 64 | str = "...";
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | console.log(toHex(offset) + ":\t", str, pad, ";", msg);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | function dumpSep(msg) {
|
---|
| 71 | if (opts.dump === false) return;
|
---|
| 72 | console.log(";", msg);
|
---|
| 73 | }
|
---|
| 74 | /**
|
---|
| 75 | * TODO(sven): we can atually use a same structure
|
---|
| 76 | * we are adding incrementally new features
|
---|
| 77 | */
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | var state = {
|
---|
| 81 | elementsInFuncSection: [],
|
---|
| 82 | elementsInExportSection: [],
|
---|
| 83 | elementsInCodeSection: [],
|
---|
| 84 |
|
---|
| 85 | /**
|
---|
| 86 | * Decode memory from:
|
---|
| 87 | * - Memory section
|
---|
| 88 | */
|
---|
| 89 | memoriesInModule: [],
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Decoded types from:
|
---|
| 93 | * - Type section
|
---|
| 94 | */
|
---|
| 95 | typesInModule: [],
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Decoded functions from:
|
---|
| 99 | * - Function section
|
---|
| 100 | * - Import section
|
---|
| 101 | */
|
---|
| 102 | functionsInModule: [],
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * Decoded tables from:
|
---|
| 106 | * - Table section
|
---|
| 107 | */
|
---|
| 108 | tablesInModule: [],
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Decoded globals from:
|
---|
| 112 | * - Global section
|
---|
| 113 | */
|
---|
| 114 | globalsInModule: []
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | function isEOF() {
|
---|
| 118 | return offset >= buf.length;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | function eatBytes(n) {
|
---|
| 122 | offset = offset + n;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | function readBytesAtOffset(_offset, numberOfBytes) {
|
---|
| 126 | var arr = [];
|
---|
| 127 |
|
---|
| 128 | for (var i = 0; i < numberOfBytes; i++) {
|
---|
| 129 | arr.push(buf[_offset + i]);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | return arr;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | function readBytes(numberOfBytes) {
|
---|
| 136 | return readBytesAtOffset(offset, numberOfBytes);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | function readF64() {
|
---|
| 140 | var bytes = readBytes(ieee754.NUMBER_OF_BYTE_F64);
|
---|
| 141 | var value = ieee754.decodeF64(bytes);
|
---|
| 142 |
|
---|
| 143 | if (Math.sign(value) * value === Infinity) {
|
---|
| 144 | return {
|
---|
| 145 | value: Math.sign(value),
|
---|
| 146 | inf: true,
|
---|
| 147 | nextIndex: ieee754.NUMBER_OF_BYTE_F64
|
---|
| 148 | };
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | if (isNaN(value)) {
|
---|
| 152 | var sign = bytes[bytes.length - 1] >> 7 ? -1 : 1;
|
---|
| 153 | var mantissa = 0;
|
---|
| 154 |
|
---|
| 155 | for (var i = 0; i < bytes.length - 2; ++i) {
|
---|
| 156 | mantissa += bytes[i] * Math.pow(256, i);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | mantissa += bytes[bytes.length - 2] % 16 * Math.pow(256, bytes.length - 2);
|
---|
| 160 | return {
|
---|
| 161 | value: sign * mantissa,
|
---|
| 162 | nan: true,
|
---|
| 163 | nextIndex: ieee754.NUMBER_OF_BYTE_F64
|
---|
| 164 | };
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | return {
|
---|
| 168 | value: value,
|
---|
| 169 | nextIndex: ieee754.NUMBER_OF_BYTE_F64
|
---|
| 170 | };
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | function readF32() {
|
---|
| 174 | var bytes = readBytes(ieee754.NUMBER_OF_BYTE_F32);
|
---|
| 175 | var value = ieee754.decodeF32(bytes);
|
---|
| 176 |
|
---|
| 177 | if (Math.sign(value) * value === Infinity) {
|
---|
| 178 | return {
|
---|
| 179 | value: Math.sign(value),
|
---|
| 180 | inf: true,
|
---|
| 181 | nextIndex: ieee754.NUMBER_OF_BYTE_F32
|
---|
| 182 | };
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | if (isNaN(value)) {
|
---|
| 186 | var sign = bytes[bytes.length - 1] >> 7 ? -1 : 1;
|
---|
| 187 | var mantissa = 0;
|
---|
| 188 |
|
---|
| 189 | for (var i = 0; i < bytes.length - 2; ++i) {
|
---|
| 190 | mantissa += bytes[i] * Math.pow(256, i);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | mantissa += bytes[bytes.length - 2] % 128 * Math.pow(256, bytes.length - 2);
|
---|
| 194 | return {
|
---|
| 195 | value: sign * mantissa,
|
---|
| 196 | nan: true,
|
---|
| 197 | nextIndex: ieee754.NUMBER_OF_BYTE_F32
|
---|
| 198 | };
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | return {
|
---|
| 202 | value: value,
|
---|
| 203 | nextIndex: ieee754.NUMBER_OF_BYTE_F32
|
---|
| 204 | };
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | function readUTF8String() {
|
---|
| 208 | var lenu32 = readU32(); // Don't eat any bytes. Instead, peek ahead of the current offset using
|
---|
| 209 | // readBytesAtOffset below. This keeps readUTF8String neutral with respect
|
---|
| 210 | // to the current offset, just like the other readX functions.
|
---|
| 211 |
|
---|
| 212 | var strlen = lenu32.value;
|
---|
| 213 | dump([strlen], "string length");
|
---|
| 214 | var bytes = readBytesAtOffset(offset + lenu32.nextIndex, strlen);
|
---|
| 215 | var value = utf8.decode(bytes);
|
---|
| 216 | return {
|
---|
| 217 | value: value,
|
---|
| 218 | nextIndex: strlen + lenu32.nextIndex
|
---|
| 219 | };
|
---|
| 220 | }
|
---|
| 221 | /**
|
---|
| 222 | * Decode an unsigned 32bits integer
|
---|
| 223 | *
|
---|
| 224 | * The length will be handled by the leb librairy, we pass the max number of
|
---|
| 225 | * byte.
|
---|
| 226 | */
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | function readU32() {
|
---|
| 230 | var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U32);
|
---|
| 231 | var buffer = Buffer.from(bytes);
|
---|
| 232 | return (0, _leb.decodeUInt32)(buffer);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | function readVaruint32() {
|
---|
| 236 | // where 32 bits = max 4 bytes
|
---|
| 237 | var bytes = readBytes(4);
|
---|
| 238 | var buffer = Buffer.from(bytes);
|
---|
| 239 | return (0, _leb.decodeUInt32)(buffer);
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | function readVaruint7() {
|
---|
| 243 | // where 7 bits = max 1 bytes
|
---|
| 244 | var bytes = readBytes(1);
|
---|
| 245 | var buffer = Buffer.from(bytes);
|
---|
| 246 | return (0, _leb.decodeUInt32)(buffer);
|
---|
| 247 | }
|
---|
| 248 | /**
|
---|
| 249 | * Decode a signed 32bits interger
|
---|
| 250 | */
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | function read32() {
|
---|
| 254 | var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U32);
|
---|
| 255 | var buffer = Buffer.from(bytes);
|
---|
| 256 | return (0, _leb.decodeInt32)(buffer);
|
---|
| 257 | }
|
---|
| 258 | /**
|
---|
| 259 | * Decode a signed 64bits integer
|
---|
| 260 | */
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 | function read64() {
|
---|
| 264 | var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U64);
|
---|
| 265 | var buffer = Buffer.from(bytes);
|
---|
| 266 | return (0, _leb.decodeInt64)(buffer);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | function readU64() {
|
---|
| 270 | var bytes = readBytes(_leb.MAX_NUMBER_OF_BYTE_U64);
|
---|
| 271 | var buffer = Buffer.from(bytes);
|
---|
| 272 | return (0, _leb.decodeUInt64)(buffer);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | function readByte() {
|
---|
| 276 | return readBytes(1)[0];
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | function parseModuleHeader() {
|
---|
| 280 | if (isEOF() === true || offset + 4 > buf.length) {
|
---|
| 281 | throw new Error("unexpected end");
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | var header = readBytes(4);
|
---|
| 285 |
|
---|
| 286 | if (byteArrayEq(_helperWasmBytecode.default.magicModuleHeader, header) === false) {
|
---|
| 287 | throw new _helperApiError.CompileError("magic header not detected");
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | dump(header, "wasm magic header");
|
---|
| 291 | eatBytes(4);
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | function parseVersion() {
|
---|
| 295 | if (isEOF() === true || offset + 4 > buf.length) {
|
---|
| 296 | throw new Error("unexpected end");
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | var version = readBytes(4);
|
---|
| 300 |
|
---|
| 301 | if (byteArrayEq(_helperWasmBytecode.default.moduleVersion, version) === false) {
|
---|
| 302 | throw new _helperApiError.CompileError("unknown binary version");
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | dump(version, "wasm version");
|
---|
| 306 | eatBytes(4);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | function parseVec(cast) {
|
---|
| 310 | var u32 = readU32();
|
---|
| 311 | var length = u32.value;
|
---|
| 312 | eatBytes(u32.nextIndex);
|
---|
| 313 | dump([length], "number");
|
---|
| 314 |
|
---|
| 315 | if (length === 0) {
|
---|
| 316 | return [];
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | var elements = [];
|
---|
| 320 |
|
---|
| 321 | for (var i = 0; i < length; i++) {
|
---|
| 322 | var byte = readByte();
|
---|
| 323 | eatBytes(1);
|
---|
| 324 | var value = cast(byte);
|
---|
| 325 | dump([byte], value);
|
---|
| 326 |
|
---|
| 327 | if (typeof value === "undefined") {
|
---|
| 328 | throw new _helperApiError.CompileError("Internal failure: parseVec could not cast the value");
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | elements.push(value);
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | return elements;
|
---|
| 335 | } // Type section
|
---|
| 336 | // https://webassembly.github.io/spec/binary/modules.html#binary-typesec
|
---|
| 337 |
|
---|
| 338 |
|
---|
| 339 | function parseTypeSection(numberOfTypes) {
|
---|
| 340 | var typeInstructionNodes = [];
|
---|
| 341 | dump([numberOfTypes], "num types");
|
---|
| 342 |
|
---|
| 343 | for (var i = 0; i < numberOfTypes; i++) {
|
---|
| 344 | var _startLoc = getPosition();
|
---|
| 345 |
|
---|
| 346 | dumpSep("type " + i);
|
---|
| 347 | var type = readByte();
|
---|
| 348 | eatBytes(1);
|
---|
| 349 |
|
---|
| 350 | if (type == _helperWasmBytecode.default.types.func) {
|
---|
| 351 | dump([type], "func");
|
---|
| 352 | var paramValtypes = parseVec(function (b) {
|
---|
| 353 | return _helperWasmBytecode.default.valtypes[b];
|
---|
| 354 | });
|
---|
| 355 | var params = paramValtypes.map(function (v) {
|
---|
| 356 | return t.funcParam(
|
---|
| 357 | /*valtype*/
|
---|
| 358 | v);
|
---|
| 359 | });
|
---|
| 360 | var result = parseVec(function (b) {
|
---|
| 361 | return _helperWasmBytecode.default.valtypes[b];
|
---|
| 362 | });
|
---|
| 363 | typeInstructionNodes.push(function () {
|
---|
| 364 | var endLoc = getPosition();
|
---|
| 365 | return t.withLoc(t.typeInstruction(undefined, t.signature(params, result)), endLoc, _startLoc);
|
---|
| 366 | }());
|
---|
| 367 | state.typesInModule.push({
|
---|
| 368 | params: params,
|
---|
| 369 | result: result
|
---|
| 370 | });
|
---|
| 371 | } else {
|
---|
| 372 | throw new Error("Unsupported type: " + toHex(type));
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | return typeInstructionNodes;
|
---|
| 377 | } // Import section
|
---|
| 378 | // https://webassembly.github.io/spec/binary/modules.html#binary-importsec
|
---|
| 379 |
|
---|
| 380 |
|
---|
| 381 | function parseImportSection(numberOfImports) {
|
---|
| 382 | var imports = [];
|
---|
| 383 |
|
---|
| 384 | for (var i = 0; i < numberOfImports; i++) {
|
---|
| 385 | dumpSep("import header " + i);
|
---|
| 386 |
|
---|
| 387 | var _startLoc2 = getPosition();
|
---|
| 388 | /**
|
---|
| 389 | * Module name
|
---|
| 390 | */
|
---|
| 391 |
|
---|
| 392 |
|
---|
| 393 | var moduleName = readUTF8String();
|
---|
| 394 | eatBytes(moduleName.nextIndex);
|
---|
| 395 | dump([], "module name (".concat(moduleName.value, ")"));
|
---|
| 396 | /**
|
---|
| 397 | * Name
|
---|
| 398 | */
|
---|
| 399 |
|
---|
| 400 | var name = readUTF8String();
|
---|
| 401 | eatBytes(name.nextIndex);
|
---|
| 402 | dump([], "name (".concat(name.value, ")"));
|
---|
| 403 | /**
|
---|
| 404 | * Import descr
|
---|
| 405 | */
|
---|
| 406 |
|
---|
| 407 | var descrTypeByte = readByte();
|
---|
| 408 | eatBytes(1);
|
---|
| 409 | var descrType = _helperWasmBytecode.default.importTypes[descrTypeByte];
|
---|
| 410 | dump([descrTypeByte], "import kind");
|
---|
| 411 |
|
---|
| 412 | if (typeof descrType === "undefined") {
|
---|
| 413 | throw new _helperApiError.CompileError("Unknown import description type: " + toHex(descrTypeByte));
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | var importDescr = void 0;
|
---|
| 417 |
|
---|
| 418 | if (descrType === "func") {
|
---|
| 419 | var indexU32 = readU32();
|
---|
| 420 | var typeindex = indexU32.value;
|
---|
| 421 | eatBytes(indexU32.nextIndex);
|
---|
| 422 | dump([typeindex], "type index");
|
---|
| 423 | var signature = state.typesInModule[typeindex];
|
---|
| 424 |
|
---|
| 425 | if (typeof signature === "undefined") {
|
---|
| 426 | throw new _helperApiError.CompileError("function signature not found (".concat(typeindex, ")"));
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | var id = getUniqueName("func");
|
---|
| 430 | importDescr = t.funcImportDescr(id, t.signature(signature.params, signature.result));
|
---|
| 431 | state.functionsInModule.push({
|
---|
| 432 | id: t.identifier(name.value),
|
---|
| 433 | signature: signature,
|
---|
| 434 | isExternal: true
|
---|
| 435 | });
|
---|
| 436 | } else if (descrType === "global") {
|
---|
| 437 | importDescr = parseGlobalType();
|
---|
| 438 | var globalNode = t.global(importDescr, []);
|
---|
| 439 | state.globalsInModule.push(globalNode);
|
---|
| 440 | } else if (descrType === "table") {
|
---|
| 441 | importDescr = parseTableType(i);
|
---|
| 442 | } else if (descrType === "mem") {
|
---|
| 443 | var memoryNode = parseMemoryType(0);
|
---|
| 444 | state.memoriesInModule.push(memoryNode);
|
---|
| 445 | importDescr = memoryNode;
|
---|
| 446 | } else {
|
---|
| 447 | throw new _helperApiError.CompileError("Unsupported import of type: " + descrType);
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | imports.push(function () {
|
---|
| 451 | var endLoc = getPosition();
|
---|
| 452 | return t.withLoc(t.moduleImport(moduleName.value, name.value, importDescr), endLoc, _startLoc2);
|
---|
| 453 | }());
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | return imports;
|
---|
| 457 | } // Function section
|
---|
| 458 | // https://webassembly.github.io/spec/binary/modules.html#function-section
|
---|
| 459 |
|
---|
| 460 |
|
---|
| 461 | function parseFuncSection(numberOfFunctions) {
|
---|
| 462 | dump([numberOfFunctions], "num funcs");
|
---|
| 463 |
|
---|
| 464 | for (var i = 0; i < numberOfFunctions; i++) {
|
---|
| 465 | var indexU32 = readU32();
|
---|
| 466 | var typeindex = indexU32.value;
|
---|
| 467 | eatBytes(indexU32.nextIndex);
|
---|
| 468 | dump([typeindex], "type index");
|
---|
| 469 | var signature = state.typesInModule[typeindex];
|
---|
| 470 |
|
---|
| 471 | if (typeof signature === "undefined") {
|
---|
| 472 | throw new _helperApiError.CompileError("function signature not found (".concat(typeindex, ")"));
|
---|
| 473 | } // preserve anonymous, a name might be resolved later
|
---|
| 474 |
|
---|
| 475 |
|
---|
| 476 | var id = t.withRaw(t.identifier(getUniqueName("func")), "");
|
---|
| 477 | state.functionsInModule.push({
|
---|
| 478 | id: id,
|
---|
| 479 | signature: signature,
|
---|
| 480 | isExternal: false
|
---|
| 481 | });
|
---|
| 482 | }
|
---|
| 483 | } // Export section
|
---|
| 484 | // https://webassembly.github.io/spec/binary/modules.html#export-section
|
---|
| 485 |
|
---|
| 486 |
|
---|
| 487 | function parseExportSection(numberOfExport) {
|
---|
| 488 | dump([numberOfExport], "num exports"); // Parse vector of exports
|
---|
| 489 |
|
---|
| 490 | for (var i = 0; i < numberOfExport; i++) {
|
---|
| 491 | var _startLoc3 = getPosition();
|
---|
| 492 | /**
|
---|
| 493 | * Name
|
---|
| 494 | */
|
---|
| 495 |
|
---|
| 496 |
|
---|
| 497 | var name = readUTF8String();
|
---|
| 498 | eatBytes(name.nextIndex);
|
---|
| 499 | dump([], "export name (".concat(name.value, ")"));
|
---|
| 500 | /**
|
---|
| 501 | * exportdescr
|
---|
| 502 | */
|
---|
| 503 |
|
---|
| 504 | var typeIndex = readByte();
|
---|
| 505 | eatBytes(1);
|
---|
| 506 | dump([typeIndex], "export kind");
|
---|
| 507 | var indexu32 = readU32();
|
---|
| 508 | var index = indexu32.value;
|
---|
| 509 | eatBytes(indexu32.nextIndex);
|
---|
| 510 | dump([index], "export index");
|
---|
| 511 | var id = void 0,
|
---|
| 512 | signature = void 0;
|
---|
| 513 |
|
---|
| 514 | if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Func") {
|
---|
| 515 | var func = state.functionsInModule[index];
|
---|
| 516 |
|
---|
| 517 | if (typeof func === "undefined") {
|
---|
| 518 | throw new _helperApiError.CompileError("unknown function (".concat(index, ")"));
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | id = t.numberLiteralFromRaw(index, String(index));
|
---|
| 522 | signature = func.signature;
|
---|
| 523 | } else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Table") {
|
---|
| 524 | var table = state.tablesInModule[index];
|
---|
| 525 |
|
---|
| 526 | if (typeof table === "undefined") {
|
---|
| 527 | throw new _helperApiError.CompileError("unknown table ".concat(index));
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | id = t.numberLiteralFromRaw(index, String(index));
|
---|
| 531 | signature = null;
|
---|
| 532 | } else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Mem") {
|
---|
| 533 | var memNode = state.memoriesInModule[index];
|
---|
| 534 |
|
---|
| 535 | if (typeof memNode === "undefined") {
|
---|
| 536 | throw new _helperApiError.CompileError("unknown memory ".concat(index));
|
---|
| 537 | }
|
---|
| 538 |
|
---|
| 539 | id = t.numberLiteralFromRaw(index, String(index));
|
---|
| 540 | signature = null;
|
---|
| 541 | } else if (_helperWasmBytecode.default.exportTypes[typeIndex] === "Global") {
|
---|
| 542 | var global = state.globalsInModule[index];
|
---|
| 543 |
|
---|
| 544 | if (typeof global === "undefined") {
|
---|
| 545 | throw new _helperApiError.CompileError("unknown global ".concat(index));
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | id = t.numberLiteralFromRaw(index, String(index));
|
---|
| 549 | signature = null;
|
---|
| 550 | } else {
|
---|
| 551 | console.warn("Unsupported export type: " + toHex(typeIndex));
|
---|
| 552 | return;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | var endLoc = getPosition();
|
---|
| 556 | state.elementsInExportSection.push({
|
---|
| 557 | name: name.value,
|
---|
| 558 | type: _helperWasmBytecode.default.exportTypes[typeIndex],
|
---|
| 559 | signature: signature,
|
---|
| 560 | id: id,
|
---|
| 561 | index: index,
|
---|
| 562 | endLoc: endLoc,
|
---|
| 563 | startLoc: _startLoc3
|
---|
| 564 | });
|
---|
| 565 | }
|
---|
| 566 | } // Code section
|
---|
| 567 | // https://webassembly.github.io/spec/binary/modules.html#code-section
|
---|
| 568 |
|
---|
| 569 |
|
---|
| 570 | function parseCodeSection(numberOfFuncs) {
|
---|
| 571 | dump([numberOfFuncs], "number functions"); // Parse vector of function
|
---|
| 572 |
|
---|
| 573 | for (var i = 0; i < numberOfFuncs; i++) {
|
---|
| 574 | var _startLoc4 = getPosition();
|
---|
| 575 |
|
---|
| 576 | dumpSep("function body " + i); // the u32 size of the function code in bytes
|
---|
| 577 | // Ignore it for now
|
---|
| 578 |
|
---|
| 579 | var bodySizeU32 = readU32();
|
---|
| 580 | eatBytes(bodySizeU32.nextIndex);
|
---|
| 581 | dump([bodySizeU32.value], "function body size");
|
---|
| 582 | var code = [];
|
---|
| 583 | /**
|
---|
| 584 | * Parse locals
|
---|
| 585 | */
|
---|
| 586 |
|
---|
| 587 | var funcLocalNumU32 = readU32();
|
---|
| 588 | var funcLocalNum = funcLocalNumU32.value;
|
---|
| 589 | eatBytes(funcLocalNumU32.nextIndex);
|
---|
| 590 | dump([funcLocalNum], "num locals");
|
---|
| 591 | var locals = [];
|
---|
| 592 |
|
---|
| 593 | for (var _i = 0; _i < funcLocalNum; _i++) {
|
---|
| 594 | var _startLoc5 = getPosition();
|
---|
| 595 |
|
---|
| 596 | var localCountU32 = readU32();
|
---|
| 597 | var localCount = localCountU32.value;
|
---|
| 598 | eatBytes(localCountU32.nextIndex);
|
---|
| 599 | dump([localCount], "num local");
|
---|
| 600 | var valtypeByte = readByte();
|
---|
| 601 | eatBytes(1);
|
---|
| 602 | var type = _helperWasmBytecode.default.valtypes[valtypeByte];
|
---|
| 603 | var args = [];
|
---|
| 604 |
|
---|
| 605 | for (var _i2 = 0; _i2 < localCount; _i2++) {
|
---|
| 606 | args.push(t.valtypeLiteral(type));
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | var localNode = function () {
|
---|
| 610 | var endLoc = getPosition();
|
---|
| 611 | return t.withLoc(t.instruction("local", args), endLoc, _startLoc5);
|
---|
| 612 | }();
|
---|
| 613 |
|
---|
| 614 | locals.push(localNode);
|
---|
| 615 | dump([valtypeByte], type);
|
---|
| 616 |
|
---|
| 617 | if (typeof type === "undefined") {
|
---|
| 618 | throw new _helperApiError.CompileError("Unexpected valtype: " + toHex(valtypeByte));
|
---|
| 619 | }
|
---|
| 620 | }
|
---|
| 621 |
|
---|
| 622 | code.push.apply(code, locals); // Decode instructions until the end
|
---|
| 623 |
|
---|
| 624 | parseInstructionBlock(code);
|
---|
| 625 | var endLoc = getPosition();
|
---|
| 626 | state.elementsInCodeSection.push({
|
---|
| 627 | code: code,
|
---|
| 628 | locals: locals,
|
---|
| 629 | endLoc: endLoc,
|
---|
| 630 | startLoc: _startLoc4,
|
---|
| 631 | bodySize: bodySizeU32.value
|
---|
| 632 | });
|
---|
| 633 | }
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | function parseInstructionBlock(code) {
|
---|
| 637 | while (true) {
|
---|
| 638 | var _startLoc6 = getPosition();
|
---|
| 639 |
|
---|
| 640 | var instructionAlreadyCreated = false;
|
---|
| 641 | var instructionByte = readByte();
|
---|
| 642 | eatBytes(1);
|
---|
| 643 |
|
---|
| 644 | if (instructionByte === 0xfe) {
|
---|
| 645 | instructionByte = 0xfe00 + readByte();
|
---|
| 646 | eatBytes(1);
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | var instruction = _helperWasmBytecode.default.symbolsByByte[instructionByte];
|
---|
| 650 |
|
---|
| 651 | if (typeof instruction === "undefined") {
|
---|
| 652 | throw new _helperApiError.CompileError("Unexpected instruction: " + toHex(instructionByte));
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | if (typeof instruction.object === "string") {
|
---|
| 656 | dump([instructionByte], "".concat(instruction.object, ".").concat(instruction.name));
|
---|
| 657 | } else {
|
---|
| 658 | dump([instructionByte], instruction.name);
|
---|
| 659 | }
|
---|
| 660 | /**
|
---|
| 661 | * End of the function
|
---|
| 662 | */
|
---|
| 663 |
|
---|
| 664 |
|
---|
| 665 | if (instruction.name === "end") {
|
---|
| 666 | var node = function () {
|
---|
| 667 | var endLoc = getPosition();
|
---|
| 668 | return t.withLoc(t.instruction(instruction.name), endLoc, _startLoc6);
|
---|
| 669 | }();
|
---|
| 670 |
|
---|
| 671 | code.push(node);
|
---|
| 672 | break;
|
---|
| 673 | }
|
---|
| 674 |
|
---|
| 675 | var args = [];
|
---|
| 676 |
|
---|
| 677 | if (instruction.name === "loop") {
|
---|
| 678 | var _startLoc7 = getPosition();
|
---|
| 679 |
|
---|
| 680 | var blocktypeByte = readByte();
|
---|
| 681 | eatBytes(1);
|
---|
| 682 | var blocktype = _helperWasmBytecode.default.blockTypes[blocktypeByte];
|
---|
| 683 | dump([blocktypeByte], "blocktype");
|
---|
| 684 |
|
---|
| 685 | if (typeof blocktype === "undefined") {
|
---|
| 686 | throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(blocktypeByte));
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | var instr = [];
|
---|
| 690 | parseInstructionBlock(instr); // preserve anonymous
|
---|
| 691 |
|
---|
| 692 | var label = t.withRaw(t.identifier(getUniqueName("loop")), "");
|
---|
| 693 |
|
---|
| 694 | var loopNode = function () {
|
---|
| 695 | var endLoc = getPosition();
|
---|
| 696 | return t.withLoc(t.loopInstruction(label, blocktype, instr), endLoc, _startLoc7);
|
---|
| 697 | }();
|
---|
| 698 |
|
---|
| 699 | code.push(loopNode);
|
---|
| 700 | instructionAlreadyCreated = true;
|
---|
| 701 | } else if (instruction.name === "if") {
|
---|
| 702 | var _startLoc8 = getPosition();
|
---|
| 703 |
|
---|
| 704 | var _blocktypeByte = readByte();
|
---|
| 705 |
|
---|
| 706 | eatBytes(1);
|
---|
| 707 | var _blocktype = _helperWasmBytecode.default.blockTypes[_blocktypeByte];
|
---|
| 708 | dump([_blocktypeByte], "blocktype");
|
---|
| 709 |
|
---|
| 710 | if (typeof _blocktype === "undefined") {
|
---|
| 711 | throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(_blocktypeByte));
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | var testIndex = t.withRaw(t.identifier(getUniqueName("if")), "");
|
---|
| 715 | var ifBody = [];
|
---|
| 716 | parseInstructionBlock(ifBody); // Defaults to no alternate
|
---|
| 717 |
|
---|
| 718 | var elseIndex = 0;
|
---|
| 719 |
|
---|
| 720 | for (elseIndex = 0; elseIndex < ifBody.length; ++elseIndex) {
|
---|
| 721 | var _instr = ifBody[elseIndex];
|
---|
| 722 |
|
---|
| 723 | if (_instr.type === "Instr" && _instr.id === "else") {
|
---|
| 724 | break;
|
---|
| 725 | }
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | var consequentInstr = ifBody.slice(0, elseIndex);
|
---|
| 729 | var alternate = ifBody.slice(elseIndex + 1); // wast sugar
|
---|
| 730 |
|
---|
| 731 | var testInstrs = [];
|
---|
| 732 |
|
---|
| 733 | var ifNode = function () {
|
---|
| 734 | var endLoc = getPosition();
|
---|
| 735 | return t.withLoc(t.ifInstruction(testIndex, testInstrs, _blocktype, consequentInstr, alternate), endLoc, _startLoc8);
|
---|
| 736 | }();
|
---|
| 737 |
|
---|
| 738 | code.push(ifNode);
|
---|
| 739 | instructionAlreadyCreated = true;
|
---|
| 740 | } else if (instruction.name === "block") {
|
---|
| 741 | var _startLoc9 = getPosition();
|
---|
| 742 |
|
---|
| 743 | var _blocktypeByte2 = readByte();
|
---|
| 744 |
|
---|
| 745 | eatBytes(1);
|
---|
| 746 | var _blocktype2 = _helperWasmBytecode.default.blockTypes[_blocktypeByte2];
|
---|
| 747 | dump([_blocktypeByte2], "blocktype");
|
---|
| 748 |
|
---|
| 749 | if (typeof _blocktype2 === "undefined") {
|
---|
| 750 | throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(_blocktypeByte2));
|
---|
| 751 | }
|
---|
| 752 |
|
---|
| 753 | var _instr2 = [];
|
---|
| 754 | parseInstructionBlock(_instr2); // preserve anonymous
|
---|
| 755 |
|
---|
| 756 | var _label = t.withRaw(t.identifier(getUniqueName("block")), "");
|
---|
| 757 |
|
---|
| 758 | var blockNode = function () {
|
---|
| 759 | var endLoc = getPosition();
|
---|
| 760 | return t.withLoc(t.blockInstruction(_label, _instr2, _blocktype2), endLoc, _startLoc9);
|
---|
| 761 | }();
|
---|
| 762 |
|
---|
| 763 | code.push(blockNode);
|
---|
| 764 | instructionAlreadyCreated = true;
|
---|
| 765 | } else if (instruction.name === "call") {
|
---|
| 766 | var indexu32 = readU32();
|
---|
| 767 | var index = indexu32.value;
|
---|
| 768 | eatBytes(indexu32.nextIndex);
|
---|
| 769 | dump([index], "index");
|
---|
| 770 |
|
---|
| 771 | var callNode = function () {
|
---|
| 772 | var endLoc = getPosition();
|
---|
| 773 | return t.withLoc(t.callInstruction(t.indexLiteral(index)), endLoc, _startLoc6);
|
---|
| 774 | }();
|
---|
| 775 |
|
---|
| 776 | code.push(callNode);
|
---|
| 777 | instructionAlreadyCreated = true;
|
---|
| 778 | } else if (instruction.name === "call_indirect") {
|
---|
| 779 | var _startLoc10 = getPosition();
|
---|
| 780 |
|
---|
| 781 | var indexU32 = readU32();
|
---|
| 782 | var typeindex = indexU32.value;
|
---|
| 783 | eatBytes(indexU32.nextIndex);
|
---|
| 784 | dump([typeindex], "type index");
|
---|
| 785 | var signature = state.typesInModule[typeindex];
|
---|
| 786 |
|
---|
| 787 | if (typeof signature === "undefined") {
|
---|
| 788 | throw new _helperApiError.CompileError("call_indirect signature not found (".concat(typeindex, ")"));
|
---|
| 789 | }
|
---|
| 790 |
|
---|
| 791 | var _callNode = t.callIndirectInstruction(t.signature(signature.params, signature.result), []);
|
---|
| 792 |
|
---|
| 793 | var flagU32 = readU32();
|
---|
| 794 | var flag = flagU32.value; // 0x00 - reserved byte
|
---|
| 795 |
|
---|
| 796 | eatBytes(flagU32.nextIndex);
|
---|
| 797 |
|
---|
| 798 | if (flag !== 0) {
|
---|
| 799 | throw new _helperApiError.CompileError("zero flag expected");
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | code.push(function () {
|
---|
| 803 | var endLoc = getPosition();
|
---|
| 804 | return t.withLoc(_callNode, endLoc, _startLoc10);
|
---|
| 805 | }());
|
---|
| 806 | instructionAlreadyCreated = true;
|
---|
| 807 | } else if (instruction.name === "br_table") {
|
---|
| 808 | var indicesu32 = readU32();
|
---|
| 809 | var indices = indicesu32.value;
|
---|
| 810 | eatBytes(indicesu32.nextIndex);
|
---|
| 811 | dump([indices], "num indices");
|
---|
| 812 |
|
---|
| 813 | for (var i = 0; i <= indices; i++) {
|
---|
| 814 | var _indexu = readU32();
|
---|
| 815 |
|
---|
| 816 | var _index = _indexu.value;
|
---|
| 817 | eatBytes(_indexu.nextIndex);
|
---|
| 818 | dump([_index], "index");
|
---|
| 819 | args.push(t.numberLiteralFromRaw(_indexu.value.toString(), "u32"));
|
---|
| 820 | }
|
---|
| 821 | } else if (instructionByte >= 0x28 && instructionByte <= 0x40) {
|
---|
| 822 | /**
|
---|
| 823 | * Memory instructions
|
---|
| 824 | */
|
---|
| 825 | if (instruction.name === "grow_memory" || instruction.name === "current_memory") {
|
---|
| 826 | var _indexU = readU32();
|
---|
| 827 |
|
---|
| 828 | var _index2 = _indexU.value;
|
---|
| 829 | eatBytes(_indexU.nextIndex);
|
---|
| 830 |
|
---|
| 831 | if (_index2 !== 0) {
|
---|
| 832 | throw new Error("zero flag expected");
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | dump([_index2], "index");
|
---|
| 836 | } else {
|
---|
| 837 | var aligun32 = readU32();
|
---|
| 838 | var align = aligun32.value;
|
---|
| 839 | eatBytes(aligun32.nextIndex);
|
---|
| 840 | dump([align], "align");
|
---|
| 841 | var offsetu32 = readU32();
|
---|
| 842 | var _offset2 = offsetu32.value;
|
---|
| 843 | eatBytes(offsetu32.nextIndex);
|
---|
| 844 | dump([_offset2], "offset");
|
---|
| 845 | }
|
---|
| 846 | } else if (instructionByte >= 0x41 && instructionByte <= 0x44) {
|
---|
| 847 | /**
|
---|
| 848 | * Numeric instructions
|
---|
| 849 | */
|
---|
| 850 | if (instruction.object === "i32") {
|
---|
| 851 | var value32 = read32();
|
---|
| 852 | var value = value32.value;
|
---|
| 853 | eatBytes(value32.nextIndex);
|
---|
| 854 | dump([value], "i32 value");
|
---|
| 855 | args.push(t.numberLiteralFromRaw(value));
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 | if (instruction.object === "u32") {
|
---|
| 859 | var valueu32 = readU32();
|
---|
| 860 | var _value = valueu32.value;
|
---|
| 861 | eatBytes(valueu32.nextIndex);
|
---|
| 862 | dump([_value], "u32 value");
|
---|
| 863 | args.push(t.numberLiteralFromRaw(_value));
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | if (instruction.object === "i64") {
|
---|
| 867 | var value64 = read64();
|
---|
| 868 | var _value2 = value64.value;
|
---|
| 869 | eatBytes(value64.nextIndex);
|
---|
| 870 | dump([Number(_value2.toString())], "i64 value");
|
---|
| 871 | var high = _value2.high,
|
---|
| 872 | low = _value2.low;
|
---|
| 873 | var _node = {
|
---|
| 874 | type: "LongNumberLiteral",
|
---|
| 875 | value: {
|
---|
| 876 | high: high,
|
---|
| 877 | low: low
|
---|
| 878 | }
|
---|
| 879 | };
|
---|
| 880 | args.push(_node);
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 | if (instruction.object === "u64") {
|
---|
| 884 | var valueu64 = readU64();
|
---|
| 885 | var _value3 = valueu64.value;
|
---|
| 886 | eatBytes(valueu64.nextIndex);
|
---|
| 887 | dump([Number(_value3.toString())], "u64 value");
|
---|
| 888 | var _high = _value3.high,
|
---|
| 889 | _low = _value3.low;
|
---|
| 890 | var _node2 = {
|
---|
| 891 | type: "LongNumberLiteral",
|
---|
| 892 | value: {
|
---|
| 893 | high: _high,
|
---|
| 894 | low: _low
|
---|
| 895 | }
|
---|
| 896 | };
|
---|
| 897 | args.push(_node2);
|
---|
| 898 | }
|
---|
| 899 |
|
---|
| 900 | if (instruction.object === "f32") {
|
---|
| 901 | var valuef32 = readF32();
|
---|
| 902 | var _value4 = valuef32.value;
|
---|
| 903 | eatBytes(valuef32.nextIndex);
|
---|
| 904 | dump([_value4], "f32 value");
|
---|
| 905 | args.push( // $FlowIgnore
|
---|
| 906 | t.floatLiteral(_value4, valuef32.nan, valuef32.inf, String(_value4)));
|
---|
| 907 | }
|
---|
| 908 |
|
---|
| 909 | if (instruction.object === "f64") {
|
---|
| 910 | var valuef64 = readF64();
|
---|
| 911 | var _value5 = valuef64.value;
|
---|
| 912 | eatBytes(valuef64.nextIndex);
|
---|
| 913 | dump([_value5], "f64 value");
|
---|
| 914 | args.push( // $FlowIgnore
|
---|
| 915 | t.floatLiteral(_value5, valuef64.nan, valuef64.inf, String(_value5)));
|
---|
| 916 | }
|
---|
| 917 | } else if (instructionByte >= 0xfe00 && instructionByte <= 0xfeff) {
|
---|
| 918 | /**
|
---|
| 919 | * Atomic memory instructions
|
---|
| 920 | */
|
---|
| 921 | var align32 = readU32();
|
---|
| 922 | var _align = align32.value;
|
---|
| 923 | eatBytes(align32.nextIndex);
|
---|
| 924 | dump([_align], "align");
|
---|
| 925 |
|
---|
| 926 | var _offsetu = readU32();
|
---|
| 927 |
|
---|
| 928 | var _offset3 = _offsetu.value;
|
---|
| 929 | eatBytes(_offsetu.nextIndex);
|
---|
| 930 | dump([_offset3], "offset");
|
---|
| 931 | } else {
|
---|
| 932 | for (var _i3 = 0; _i3 < instruction.numberOfArgs; _i3++) {
|
---|
| 933 | var u32 = readU32();
|
---|
| 934 | eatBytes(u32.nextIndex);
|
---|
| 935 | dump([u32.value], "argument " + _i3);
|
---|
| 936 | args.push(t.numberLiteralFromRaw(u32.value));
|
---|
| 937 | }
|
---|
| 938 | }
|
---|
| 939 |
|
---|
| 940 | if (instructionAlreadyCreated === false) {
|
---|
| 941 | if (typeof instruction.object === "string") {
|
---|
| 942 | var _node3 = function () {
|
---|
| 943 | var endLoc = getPosition();
|
---|
| 944 | return t.withLoc(t.objectInstruction(instruction.name, instruction.object, args), endLoc, _startLoc6);
|
---|
| 945 | }();
|
---|
| 946 |
|
---|
| 947 | code.push(_node3);
|
---|
| 948 | } else {
|
---|
| 949 | var _node4 = function () {
|
---|
| 950 | var endLoc = getPosition();
|
---|
| 951 | return t.withLoc(t.instruction(instruction.name, args), endLoc, _startLoc6);
|
---|
| 952 | }();
|
---|
| 953 |
|
---|
| 954 | code.push(_node4);
|
---|
| 955 | }
|
---|
| 956 | }
|
---|
| 957 | }
|
---|
| 958 | } // https://webassembly.github.io/spec/core/binary/types.html#limits
|
---|
| 959 |
|
---|
| 960 |
|
---|
| 961 | function parseLimits() {
|
---|
| 962 | var limitType = readByte();
|
---|
| 963 | eatBytes(1);
|
---|
| 964 | var shared = limitType === 0x03;
|
---|
| 965 | dump([limitType], "limit type" + (shared ? " (shared)" : ""));
|
---|
| 966 | var min, max;
|
---|
| 967 |
|
---|
| 968 | if (limitType === 0x01 || limitType === 0x03 // shared limits
|
---|
| 969 | ) {
|
---|
| 970 | var u32min = readU32();
|
---|
| 971 | min = parseInt(u32min.value);
|
---|
| 972 | eatBytes(u32min.nextIndex);
|
---|
| 973 | dump([min], "min");
|
---|
| 974 | var u32max = readU32();
|
---|
| 975 | max = parseInt(u32max.value);
|
---|
| 976 | eatBytes(u32max.nextIndex);
|
---|
| 977 | dump([max], "max");
|
---|
| 978 | }
|
---|
| 979 |
|
---|
| 980 | if (limitType === 0x00) {
|
---|
| 981 | var _u32min = readU32();
|
---|
| 982 |
|
---|
| 983 | min = parseInt(_u32min.value);
|
---|
| 984 | eatBytes(_u32min.nextIndex);
|
---|
| 985 | dump([min], "min");
|
---|
| 986 | }
|
---|
| 987 |
|
---|
| 988 | return t.limit(min, max, shared);
|
---|
| 989 | } // https://webassembly.github.io/spec/core/binary/types.html#binary-tabletype
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 | function parseTableType(index) {
|
---|
| 993 | var name = t.withRaw(t.identifier(getUniqueName("table")), String(index));
|
---|
| 994 | var elementTypeByte = readByte();
|
---|
| 995 | eatBytes(1);
|
---|
| 996 | dump([elementTypeByte], "element type");
|
---|
| 997 | var elementType = _helperWasmBytecode.default.tableTypes[elementTypeByte];
|
---|
| 998 |
|
---|
| 999 | if (typeof elementType === "undefined") {
|
---|
| 1000 | throw new _helperApiError.CompileError("Unknown element type in table: " + toHex(elementType));
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | var limits = parseLimits();
|
---|
| 1004 | return t.table(elementType, limits, name);
|
---|
| 1005 | } // https://webassembly.github.io/spec/binary/types.html#global-types
|
---|
| 1006 |
|
---|
| 1007 |
|
---|
| 1008 | function parseGlobalType() {
|
---|
| 1009 | var valtypeByte = readByte();
|
---|
| 1010 | eatBytes(1);
|
---|
| 1011 | var type = _helperWasmBytecode.default.valtypes[valtypeByte];
|
---|
| 1012 | dump([valtypeByte], type);
|
---|
| 1013 |
|
---|
| 1014 | if (typeof type === "undefined") {
|
---|
| 1015 | throw new _helperApiError.CompileError("Unknown valtype: " + toHex(valtypeByte));
|
---|
| 1016 | }
|
---|
| 1017 |
|
---|
| 1018 | var globalTypeByte = readByte();
|
---|
| 1019 | eatBytes(1);
|
---|
| 1020 | var globalType = _helperWasmBytecode.default.globalTypes[globalTypeByte];
|
---|
| 1021 | dump([globalTypeByte], "global type (".concat(globalType, ")"));
|
---|
| 1022 |
|
---|
| 1023 | if (typeof globalType === "undefined") {
|
---|
| 1024 | throw new _helperApiError.CompileError("Invalid mutability: " + toHex(globalTypeByte));
|
---|
| 1025 | }
|
---|
| 1026 |
|
---|
| 1027 | return t.globalType(type, globalType);
|
---|
| 1028 | } // function parseNameModule() {
|
---|
| 1029 | // const lenu32 = readVaruint32();
|
---|
| 1030 | // eatBytes(lenu32.nextIndex);
|
---|
| 1031 | // console.log("len", lenu32);
|
---|
| 1032 | // const strlen = lenu32.value;
|
---|
| 1033 | // dump([strlen], "string length");
|
---|
| 1034 | // const bytes = readBytes(strlen);
|
---|
| 1035 | // eatBytes(strlen);
|
---|
| 1036 | // const value = utf8.decode(bytes);
|
---|
| 1037 | // return [t.moduleNameMetadata(value)];
|
---|
| 1038 | // }
|
---|
| 1039 | // this section contains an array of function names and indices
|
---|
| 1040 |
|
---|
| 1041 |
|
---|
| 1042 | function parseNameSectionFunctions() {
|
---|
| 1043 | var functionNames = [];
|
---|
| 1044 | var numberOfFunctionsu32 = readU32();
|
---|
| 1045 | var numbeOfFunctions = numberOfFunctionsu32.value;
|
---|
| 1046 | eatBytes(numberOfFunctionsu32.nextIndex);
|
---|
| 1047 |
|
---|
| 1048 | for (var i = 0; i < numbeOfFunctions; i++) {
|
---|
| 1049 | var indexu32 = readU32();
|
---|
| 1050 | var index = indexu32.value;
|
---|
| 1051 | eatBytes(indexu32.nextIndex);
|
---|
| 1052 | var name = readUTF8String();
|
---|
| 1053 | eatBytes(name.nextIndex);
|
---|
| 1054 | functionNames.push(t.functionNameMetadata(name.value, index));
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | return functionNames;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
| 1060 | function parseNameSectionLocals() {
|
---|
| 1061 | var localNames = [];
|
---|
| 1062 | var numbeOfFunctionsu32 = readU32();
|
---|
| 1063 | var numbeOfFunctions = numbeOfFunctionsu32.value;
|
---|
| 1064 | eatBytes(numbeOfFunctionsu32.nextIndex);
|
---|
| 1065 |
|
---|
| 1066 | for (var i = 0; i < numbeOfFunctions; i++) {
|
---|
| 1067 | var functionIndexu32 = readU32();
|
---|
| 1068 | var functionIndex = functionIndexu32.value;
|
---|
| 1069 | eatBytes(functionIndexu32.nextIndex);
|
---|
| 1070 | var numLocalsu32 = readU32();
|
---|
| 1071 | var numLocals = numLocalsu32.value;
|
---|
| 1072 | eatBytes(numLocalsu32.nextIndex);
|
---|
| 1073 |
|
---|
| 1074 | for (var _i4 = 0; _i4 < numLocals; _i4++) {
|
---|
| 1075 | var localIndexu32 = readU32();
|
---|
| 1076 | var localIndex = localIndexu32.value;
|
---|
| 1077 | eatBytes(localIndexu32.nextIndex);
|
---|
| 1078 | var name = readUTF8String();
|
---|
| 1079 | eatBytes(name.nextIndex);
|
---|
| 1080 | localNames.push(t.localNameMetadata(name.value, localIndex, functionIndex));
|
---|
| 1081 | }
|
---|
| 1082 | }
|
---|
| 1083 |
|
---|
| 1084 | return localNames;
|
---|
| 1085 | } // this is a custom section used for name resolution
|
---|
| 1086 | // https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#name-section
|
---|
| 1087 |
|
---|
| 1088 |
|
---|
| 1089 | function parseNameSection(remainingBytes) {
|
---|
| 1090 | var nameMetadata = [];
|
---|
| 1091 | var initialOffset = offset;
|
---|
| 1092 |
|
---|
| 1093 | while (offset - initialOffset < remainingBytes) {
|
---|
| 1094 | // name_type
|
---|
| 1095 | var sectionTypeByte = readVaruint7();
|
---|
| 1096 | eatBytes(sectionTypeByte.nextIndex); // name_payload_len
|
---|
| 1097 |
|
---|
| 1098 | var subSectionSizeInBytesu32 = readVaruint32();
|
---|
| 1099 | eatBytes(subSectionSizeInBytesu32.nextIndex);
|
---|
| 1100 |
|
---|
| 1101 | switch (sectionTypeByte.value) {
|
---|
| 1102 | // case 0: {
|
---|
| 1103 | // TODO(sven): re-enable that
|
---|
| 1104 | // Current status: it seems that when we decode the module's name
|
---|
| 1105 | // no name_payload_len is used.
|
---|
| 1106 | //
|
---|
| 1107 | // See https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#name-section
|
---|
| 1108 | //
|
---|
| 1109 | // nameMetadata.push(...parseNameModule());
|
---|
| 1110 | // break;
|
---|
| 1111 | // }
|
---|
| 1112 | case 1:
|
---|
| 1113 | {
|
---|
| 1114 | nameMetadata.push.apply(nameMetadata, _toConsumableArray(parseNameSectionFunctions()));
|
---|
| 1115 | break;
|
---|
| 1116 | }
|
---|
| 1117 |
|
---|
| 1118 | case 2:
|
---|
| 1119 | {
|
---|
| 1120 | nameMetadata.push.apply(nameMetadata, _toConsumableArray(parseNameSectionLocals()));
|
---|
| 1121 | break;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | default:
|
---|
| 1125 | {
|
---|
| 1126 | // skip unknown subsection
|
---|
| 1127 | eatBytes(subSectionSizeInBytesu32.value);
|
---|
| 1128 | }
|
---|
| 1129 | }
|
---|
| 1130 | }
|
---|
| 1131 |
|
---|
| 1132 | return nameMetadata;
|
---|
| 1133 | } // this is a custom section used for information about the producers
|
---|
| 1134 | // https://github.com/WebAssembly/tool-conventions/blob/master/ProducersSection.md
|
---|
| 1135 |
|
---|
| 1136 |
|
---|
| 1137 | function parseProducersSection() {
|
---|
| 1138 | var metadata = t.producersSectionMetadata([]); // field_count
|
---|
| 1139 |
|
---|
| 1140 | var sectionTypeByte = readVaruint32();
|
---|
| 1141 | eatBytes(sectionTypeByte.nextIndex);
|
---|
| 1142 | dump([sectionTypeByte.value], "num of producers");
|
---|
| 1143 | var fields = {
|
---|
| 1144 | language: [],
|
---|
| 1145 | "processed-by": [],
|
---|
| 1146 | sdk: []
|
---|
| 1147 | }; // fields
|
---|
| 1148 |
|
---|
| 1149 | for (var fieldI = 0; fieldI < sectionTypeByte.value; fieldI++) {
|
---|
| 1150 | // field_name
|
---|
| 1151 | var fieldName = readUTF8String();
|
---|
| 1152 | eatBytes(fieldName.nextIndex); // field_value_count
|
---|
| 1153 |
|
---|
| 1154 | var valueCount = readVaruint32();
|
---|
| 1155 | eatBytes(valueCount.nextIndex); // field_values
|
---|
| 1156 |
|
---|
| 1157 | for (var producerI = 0; producerI < valueCount.value; producerI++) {
|
---|
| 1158 | var producerName = readUTF8String();
|
---|
| 1159 | eatBytes(producerName.nextIndex);
|
---|
| 1160 | var producerVersion = readUTF8String();
|
---|
| 1161 | eatBytes(producerVersion.nextIndex);
|
---|
| 1162 | fields[fieldName.value].push(t.producerMetadataVersionedName(producerName.value, producerVersion.value));
|
---|
| 1163 | }
|
---|
| 1164 |
|
---|
| 1165 | metadata.producers.push(fields[fieldName.value]);
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | return metadata;
|
---|
| 1169 | }
|
---|
| 1170 |
|
---|
| 1171 | function parseGlobalSection(numberOfGlobals) {
|
---|
| 1172 | var globals = [];
|
---|
| 1173 | dump([numberOfGlobals], "num globals");
|
---|
| 1174 |
|
---|
| 1175 | for (var i = 0; i < numberOfGlobals; i++) {
|
---|
| 1176 | var _startLoc11 = getPosition();
|
---|
| 1177 |
|
---|
| 1178 | var globalType = parseGlobalType();
|
---|
| 1179 | /**
|
---|
| 1180 | * Global expressions
|
---|
| 1181 | */
|
---|
| 1182 |
|
---|
| 1183 | var init = [];
|
---|
| 1184 | parseInstructionBlock(init);
|
---|
| 1185 |
|
---|
| 1186 | var node = function () {
|
---|
| 1187 | var endLoc = getPosition();
|
---|
| 1188 | return t.withLoc(t.global(globalType, init), endLoc, _startLoc11);
|
---|
| 1189 | }();
|
---|
| 1190 |
|
---|
| 1191 | globals.push(node);
|
---|
| 1192 | state.globalsInModule.push(node);
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
| 1195 | return globals;
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | function parseElemSection(numberOfElements) {
|
---|
| 1199 | var elems = [];
|
---|
| 1200 | dump([numberOfElements], "num elements");
|
---|
| 1201 |
|
---|
| 1202 | for (var i = 0; i < numberOfElements; i++) {
|
---|
| 1203 | var _startLoc12 = getPosition();
|
---|
| 1204 |
|
---|
| 1205 | var tableindexu32 = readU32();
|
---|
| 1206 | var tableindex = tableindexu32.value;
|
---|
| 1207 | eatBytes(tableindexu32.nextIndex);
|
---|
| 1208 | dump([tableindex], "table index");
|
---|
| 1209 | /**
|
---|
| 1210 | * Parse instructions
|
---|
| 1211 | */
|
---|
| 1212 |
|
---|
| 1213 | var instr = [];
|
---|
| 1214 | parseInstructionBlock(instr);
|
---|
| 1215 | /**
|
---|
| 1216 | * Parse ( vector function index ) *
|
---|
| 1217 | */
|
---|
| 1218 |
|
---|
| 1219 | var indicesu32 = readU32();
|
---|
| 1220 | var indices = indicesu32.value;
|
---|
| 1221 | eatBytes(indicesu32.nextIndex);
|
---|
| 1222 | dump([indices], "num indices");
|
---|
| 1223 | var indexValues = [];
|
---|
| 1224 |
|
---|
| 1225 | for (var _i5 = 0; _i5 < indices; _i5++) {
|
---|
| 1226 | var indexu32 = readU32();
|
---|
| 1227 | var index = indexu32.value;
|
---|
| 1228 | eatBytes(indexu32.nextIndex);
|
---|
| 1229 | dump([index], "index");
|
---|
| 1230 | indexValues.push(t.indexLiteral(index));
|
---|
| 1231 | }
|
---|
| 1232 |
|
---|
| 1233 | var elemNode = function () {
|
---|
| 1234 | var endLoc = getPosition();
|
---|
| 1235 | return t.withLoc(t.elem(t.indexLiteral(tableindex), instr, indexValues), endLoc, _startLoc12);
|
---|
| 1236 | }();
|
---|
| 1237 |
|
---|
| 1238 | elems.push(elemNode);
|
---|
| 1239 | }
|
---|
| 1240 |
|
---|
| 1241 | return elems;
|
---|
| 1242 | } // https://webassembly.github.io/spec/core/binary/types.html#memory-types
|
---|
| 1243 |
|
---|
| 1244 |
|
---|
| 1245 | function parseMemoryType(i) {
|
---|
| 1246 | var limits = parseLimits();
|
---|
| 1247 | return t.memory(limits, t.indexLiteral(i));
|
---|
| 1248 | } // https://webassembly.github.io/spec/binary/modules.html#table-section
|
---|
| 1249 |
|
---|
| 1250 |
|
---|
| 1251 | function parseTableSection(numberOfElements) {
|
---|
| 1252 | var tables = [];
|
---|
| 1253 | dump([numberOfElements], "num elements");
|
---|
| 1254 |
|
---|
| 1255 | for (var i = 0; i < numberOfElements; i++) {
|
---|
| 1256 | var tablesNode = parseTableType(i);
|
---|
| 1257 | state.tablesInModule.push(tablesNode);
|
---|
| 1258 | tables.push(tablesNode);
|
---|
| 1259 | }
|
---|
| 1260 |
|
---|
| 1261 | return tables;
|
---|
| 1262 | } // https://webassembly.github.io/spec/binary/modules.html#memory-section
|
---|
| 1263 |
|
---|
| 1264 |
|
---|
| 1265 | function parseMemorySection(numberOfElements) {
|
---|
| 1266 | var memories = [];
|
---|
| 1267 | dump([numberOfElements], "num elements");
|
---|
| 1268 |
|
---|
| 1269 | for (var i = 0; i < numberOfElements; i++) {
|
---|
| 1270 | var memoryNode = parseMemoryType(i);
|
---|
| 1271 | state.memoriesInModule.push(memoryNode);
|
---|
| 1272 | memories.push(memoryNode);
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
| 1275 | return memories;
|
---|
| 1276 | } // https://webassembly.github.io/spec/binary/modules.html#binary-startsec
|
---|
| 1277 |
|
---|
| 1278 |
|
---|
| 1279 | function parseStartSection() {
|
---|
| 1280 | var startLoc = getPosition();
|
---|
| 1281 | var u32 = readU32();
|
---|
| 1282 | var startFuncIndex = u32.value;
|
---|
| 1283 | eatBytes(u32.nextIndex);
|
---|
| 1284 | dump([startFuncIndex], "index");
|
---|
| 1285 | return function () {
|
---|
| 1286 | var endLoc = getPosition();
|
---|
| 1287 | return t.withLoc(t.start(t.indexLiteral(startFuncIndex)), endLoc, startLoc);
|
---|
| 1288 | }();
|
---|
| 1289 | } // https://webassembly.github.io/spec/binary/modules.html#data-section
|
---|
| 1290 |
|
---|
| 1291 |
|
---|
| 1292 | function parseDataSection(numberOfElements) {
|
---|
| 1293 | var dataEntries = [];
|
---|
| 1294 | dump([numberOfElements], "num elements");
|
---|
| 1295 |
|
---|
| 1296 | for (var i = 0; i < numberOfElements; i++) {
|
---|
| 1297 | var memoryIndexu32 = readU32();
|
---|
| 1298 | var memoryIndex = memoryIndexu32.value;
|
---|
| 1299 | eatBytes(memoryIndexu32.nextIndex);
|
---|
| 1300 | dump([memoryIndex], "memory index");
|
---|
| 1301 | var instrs = [];
|
---|
| 1302 | parseInstructionBlock(instrs);
|
---|
| 1303 | var hasExtraInstrs = instrs.filter(function (i) {
|
---|
| 1304 | return i.id !== "end";
|
---|
| 1305 | }).length !== 1;
|
---|
| 1306 |
|
---|
| 1307 | if (hasExtraInstrs) {
|
---|
| 1308 | throw new _helperApiError.CompileError("data section offset must be a single instruction");
|
---|
| 1309 | }
|
---|
| 1310 |
|
---|
| 1311 | var bytes = parseVec(function (b) {
|
---|
| 1312 | return b;
|
---|
| 1313 | });
|
---|
| 1314 | dump([], "init");
|
---|
| 1315 | dataEntries.push(t.data(t.memIndexLiteral(memoryIndex), instrs[0], t.byteArray(bytes)));
|
---|
| 1316 | }
|
---|
| 1317 |
|
---|
| 1318 | return dataEntries;
|
---|
| 1319 | } // https://webassembly.github.io/spec/binary/modules.html#binary-section
|
---|
| 1320 |
|
---|
| 1321 |
|
---|
| 1322 | function parseSection(sectionIndex) {
|
---|
| 1323 | var sectionId = readByte();
|
---|
| 1324 | eatBytes(1);
|
---|
| 1325 |
|
---|
| 1326 | if (sectionId >= sectionIndex || sectionIndex === _helperWasmBytecode.default.sections.custom) {
|
---|
| 1327 | sectionIndex = sectionId + 1;
|
---|
| 1328 | } else {
|
---|
| 1329 | if (sectionId !== _helperWasmBytecode.default.sections.custom) throw new _helperApiError.CompileError("Unexpected section: " + toHex(sectionId));
|
---|
| 1330 | }
|
---|
| 1331 |
|
---|
| 1332 | var nextSectionIndex = sectionIndex;
|
---|
| 1333 | var startOffset = offset;
|
---|
| 1334 | var startLoc = getPosition();
|
---|
| 1335 | var u32 = readU32();
|
---|
| 1336 | var sectionSizeInBytes = u32.value;
|
---|
| 1337 | eatBytes(u32.nextIndex);
|
---|
| 1338 |
|
---|
| 1339 | var sectionSizeInBytesNode = function () {
|
---|
| 1340 | var endLoc = getPosition();
|
---|
| 1341 | return t.withLoc(t.numberLiteralFromRaw(sectionSizeInBytes), endLoc, startLoc);
|
---|
| 1342 | }();
|
---|
| 1343 |
|
---|
| 1344 | switch (sectionId) {
|
---|
| 1345 | case _helperWasmBytecode.default.sections.type:
|
---|
| 1346 | {
|
---|
| 1347 | dumpSep("section Type");
|
---|
| 1348 | dump([sectionId], "section code");
|
---|
| 1349 | dump([sectionSizeInBytes], "section size");
|
---|
| 1350 |
|
---|
| 1351 | var _startLoc13 = getPosition();
|
---|
| 1352 |
|
---|
| 1353 | var _u = readU32();
|
---|
| 1354 |
|
---|
| 1355 | var numberOfTypes = _u.value;
|
---|
| 1356 | eatBytes(_u.nextIndex);
|
---|
| 1357 |
|
---|
| 1358 | var _metadata = t.sectionMetadata("type", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1359 | var endLoc = getPosition();
|
---|
| 1360 | return t.withLoc(t.numberLiteralFromRaw(numberOfTypes), endLoc, _startLoc13);
|
---|
| 1361 | }());
|
---|
| 1362 |
|
---|
| 1363 | var _nodes = parseTypeSection(numberOfTypes);
|
---|
| 1364 |
|
---|
| 1365 | return {
|
---|
| 1366 | nodes: _nodes,
|
---|
| 1367 | metadata: _metadata,
|
---|
| 1368 | nextSectionIndex: nextSectionIndex
|
---|
| 1369 | };
|
---|
| 1370 | }
|
---|
| 1371 |
|
---|
| 1372 | case _helperWasmBytecode.default.sections.table:
|
---|
| 1373 | {
|
---|
| 1374 | dumpSep("section Table");
|
---|
| 1375 | dump([sectionId], "section code");
|
---|
| 1376 | dump([sectionSizeInBytes], "section size");
|
---|
| 1377 |
|
---|
| 1378 | var _startLoc14 = getPosition();
|
---|
| 1379 |
|
---|
| 1380 | var _u2 = readU32();
|
---|
| 1381 |
|
---|
| 1382 | var numberOfTable = _u2.value;
|
---|
| 1383 | eatBytes(_u2.nextIndex);
|
---|
| 1384 | dump([numberOfTable], "num tables");
|
---|
| 1385 |
|
---|
| 1386 | var _metadata2 = t.sectionMetadata("table", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1387 | var endLoc = getPosition();
|
---|
| 1388 | return t.withLoc(t.numberLiteralFromRaw(numberOfTable), endLoc, _startLoc14);
|
---|
| 1389 | }());
|
---|
| 1390 |
|
---|
| 1391 | var _nodes2 = parseTableSection(numberOfTable);
|
---|
| 1392 |
|
---|
| 1393 | return {
|
---|
| 1394 | nodes: _nodes2,
|
---|
| 1395 | metadata: _metadata2,
|
---|
| 1396 | nextSectionIndex: nextSectionIndex
|
---|
| 1397 | };
|
---|
| 1398 | }
|
---|
| 1399 |
|
---|
| 1400 | case _helperWasmBytecode.default.sections.import:
|
---|
| 1401 | {
|
---|
| 1402 | dumpSep("section Import");
|
---|
| 1403 | dump([sectionId], "section code");
|
---|
| 1404 | dump([sectionSizeInBytes], "section size");
|
---|
| 1405 |
|
---|
| 1406 | var _startLoc15 = getPosition();
|
---|
| 1407 |
|
---|
| 1408 | var numberOfImportsu32 = readU32();
|
---|
| 1409 | var numberOfImports = numberOfImportsu32.value;
|
---|
| 1410 | eatBytes(numberOfImportsu32.nextIndex);
|
---|
| 1411 | dump([numberOfImports], "number of imports");
|
---|
| 1412 |
|
---|
| 1413 | var _metadata3 = t.sectionMetadata("import", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1414 | var endLoc = getPosition();
|
---|
| 1415 | return t.withLoc(t.numberLiteralFromRaw(numberOfImports), endLoc, _startLoc15);
|
---|
| 1416 | }());
|
---|
| 1417 |
|
---|
| 1418 | var _nodes3 = parseImportSection(numberOfImports);
|
---|
| 1419 |
|
---|
| 1420 | return {
|
---|
| 1421 | nodes: _nodes3,
|
---|
| 1422 | metadata: _metadata3,
|
---|
| 1423 | nextSectionIndex: nextSectionIndex
|
---|
| 1424 | };
|
---|
| 1425 | }
|
---|
| 1426 |
|
---|
| 1427 | case _helperWasmBytecode.default.sections.func:
|
---|
| 1428 | {
|
---|
| 1429 | dumpSep("section Function");
|
---|
| 1430 | dump([sectionId], "section code");
|
---|
| 1431 | dump([sectionSizeInBytes], "section size");
|
---|
| 1432 |
|
---|
| 1433 | var _startLoc16 = getPosition();
|
---|
| 1434 |
|
---|
| 1435 | var numberOfFunctionsu32 = readU32();
|
---|
| 1436 | var numberOfFunctions = numberOfFunctionsu32.value;
|
---|
| 1437 | eatBytes(numberOfFunctionsu32.nextIndex);
|
---|
| 1438 |
|
---|
| 1439 | var _metadata4 = t.sectionMetadata("func", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1440 | var endLoc = getPosition();
|
---|
| 1441 | return t.withLoc(t.numberLiteralFromRaw(numberOfFunctions), endLoc, _startLoc16);
|
---|
| 1442 | }());
|
---|
| 1443 |
|
---|
| 1444 | parseFuncSection(numberOfFunctions);
|
---|
| 1445 | var _nodes4 = [];
|
---|
| 1446 | return {
|
---|
| 1447 | nodes: _nodes4,
|
---|
| 1448 | metadata: _metadata4,
|
---|
| 1449 | nextSectionIndex: nextSectionIndex
|
---|
| 1450 | };
|
---|
| 1451 | }
|
---|
| 1452 |
|
---|
| 1453 | case _helperWasmBytecode.default.sections.export:
|
---|
| 1454 | {
|
---|
| 1455 | dumpSep("section Export");
|
---|
| 1456 | dump([sectionId], "section code");
|
---|
| 1457 | dump([sectionSizeInBytes], "section size");
|
---|
| 1458 |
|
---|
| 1459 | var _startLoc17 = getPosition();
|
---|
| 1460 |
|
---|
| 1461 | var _u3 = readU32();
|
---|
| 1462 |
|
---|
| 1463 | var numberOfExport = _u3.value;
|
---|
| 1464 | eatBytes(_u3.nextIndex);
|
---|
| 1465 |
|
---|
| 1466 | var _metadata5 = t.sectionMetadata("export", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1467 | var endLoc = getPosition();
|
---|
| 1468 | return t.withLoc(t.numberLiteralFromRaw(numberOfExport), endLoc, _startLoc17);
|
---|
| 1469 | }());
|
---|
| 1470 |
|
---|
| 1471 | parseExportSection(numberOfExport);
|
---|
| 1472 | var _nodes5 = [];
|
---|
| 1473 | return {
|
---|
| 1474 | nodes: _nodes5,
|
---|
| 1475 | metadata: _metadata5,
|
---|
| 1476 | nextSectionIndex: nextSectionIndex
|
---|
| 1477 | };
|
---|
| 1478 | }
|
---|
| 1479 |
|
---|
| 1480 | case _helperWasmBytecode.default.sections.code:
|
---|
| 1481 | {
|
---|
| 1482 | dumpSep("section Code");
|
---|
| 1483 | dump([sectionId], "section code");
|
---|
| 1484 | dump([sectionSizeInBytes], "section size");
|
---|
| 1485 |
|
---|
| 1486 | var _startLoc18 = getPosition();
|
---|
| 1487 |
|
---|
| 1488 | var _u4 = readU32();
|
---|
| 1489 |
|
---|
| 1490 | var numberOfFuncs = _u4.value;
|
---|
| 1491 | eatBytes(_u4.nextIndex);
|
---|
| 1492 |
|
---|
| 1493 | var _metadata6 = t.sectionMetadata("code", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1494 | var endLoc = getPosition();
|
---|
| 1495 | return t.withLoc(t.numberLiteralFromRaw(numberOfFuncs), endLoc, _startLoc18);
|
---|
| 1496 | }());
|
---|
| 1497 |
|
---|
| 1498 | if (opts.ignoreCodeSection === true) {
|
---|
| 1499 | var remainingBytes = sectionSizeInBytes - _u4.nextIndex;
|
---|
| 1500 | eatBytes(remainingBytes); // eat the entire section
|
---|
| 1501 | } else {
|
---|
| 1502 | parseCodeSection(numberOfFuncs);
|
---|
| 1503 | }
|
---|
| 1504 |
|
---|
| 1505 | var _nodes6 = [];
|
---|
| 1506 | return {
|
---|
| 1507 | nodes: _nodes6,
|
---|
| 1508 | metadata: _metadata6,
|
---|
| 1509 | nextSectionIndex: nextSectionIndex
|
---|
| 1510 | };
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
| 1513 | case _helperWasmBytecode.default.sections.start:
|
---|
| 1514 | {
|
---|
| 1515 | dumpSep("section Start");
|
---|
| 1516 | dump([sectionId], "section code");
|
---|
| 1517 | dump([sectionSizeInBytes], "section size");
|
---|
| 1518 |
|
---|
| 1519 | var _metadata7 = t.sectionMetadata("start", startOffset, sectionSizeInBytesNode);
|
---|
| 1520 |
|
---|
| 1521 | var _nodes7 = [parseStartSection()];
|
---|
| 1522 | return {
|
---|
| 1523 | nodes: _nodes7,
|
---|
| 1524 | metadata: _metadata7,
|
---|
| 1525 | nextSectionIndex: nextSectionIndex
|
---|
| 1526 | };
|
---|
| 1527 | }
|
---|
| 1528 |
|
---|
| 1529 | case _helperWasmBytecode.default.sections.element:
|
---|
| 1530 | {
|
---|
| 1531 | dumpSep("section Element");
|
---|
| 1532 | dump([sectionId], "section code");
|
---|
| 1533 | dump([sectionSizeInBytes], "section size");
|
---|
| 1534 |
|
---|
| 1535 | var _startLoc19 = getPosition();
|
---|
| 1536 |
|
---|
| 1537 | var numberOfElementsu32 = readU32();
|
---|
| 1538 | var numberOfElements = numberOfElementsu32.value;
|
---|
| 1539 | eatBytes(numberOfElementsu32.nextIndex);
|
---|
| 1540 |
|
---|
| 1541 | var _metadata8 = t.sectionMetadata("element", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1542 | var endLoc = getPosition();
|
---|
| 1543 | return t.withLoc(t.numberLiteralFromRaw(numberOfElements), endLoc, _startLoc19);
|
---|
| 1544 | }());
|
---|
| 1545 |
|
---|
| 1546 | var _nodes8 = parseElemSection(numberOfElements);
|
---|
| 1547 |
|
---|
| 1548 | return {
|
---|
| 1549 | nodes: _nodes8,
|
---|
| 1550 | metadata: _metadata8,
|
---|
| 1551 | nextSectionIndex: nextSectionIndex
|
---|
| 1552 | };
|
---|
| 1553 | }
|
---|
| 1554 |
|
---|
| 1555 | case _helperWasmBytecode.default.sections.global:
|
---|
| 1556 | {
|
---|
| 1557 | dumpSep("section Global");
|
---|
| 1558 | dump([sectionId], "section code");
|
---|
| 1559 | dump([sectionSizeInBytes], "section size");
|
---|
| 1560 |
|
---|
| 1561 | var _startLoc20 = getPosition();
|
---|
| 1562 |
|
---|
| 1563 | var numberOfGlobalsu32 = readU32();
|
---|
| 1564 | var numberOfGlobals = numberOfGlobalsu32.value;
|
---|
| 1565 | eatBytes(numberOfGlobalsu32.nextIndex);
|
---|
| 1566 |
|
---|
| 1567 | var _metadata9 = t.sectionMetadata("global", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1568 | var endLoc = getPosition();
|
---|
| 1569 | return t.withLoc(t.numberLiteralFromRaw(numberOfGlobals), endLoc, _startLoc20);
|
---|
| 1570 | }());
|
---|
| 1571 |
|
---|
| 1572 | var _nodes9 = parseGlobalSection(numberOfGlobals);
|
---|
| 1573 |
|
---|
| 1574 | return {
|
---|
| 1575 | nodes: _nodes9,
|
---|
| 1576 | metadata: _metadata9,
|
---|
| 1577 | nextSectionIndex: nextSectionIndex
|
---|
| 1578 | };
|
---|
| 1579 | }
|
---|
| 1580 |
|
---|
| 1581 | case _helperWasmBytecode.default.sections.memory:
|
---|
| 1582 | {
|
---|
| 1583 | dumpSep("section Memory");
|
---|
| 1584 | dump([sectionId], "section code");
|
---|
| 1585 | dump([sectionSizeInBytes], "section size");
|
---|
| 1586 |
|
---|
| 1587 | var _startLoc21 = getPosition();
|
---|
| 1588 |
|
---|
| 1589 | var _numberOfElementsu = readU32();
|
---|
| 1590 |
|
---|
| 1591 | var _numberOfElements = _numberOfElementsu.value;
|
---|
| 1592 | eatBytes(_numberOfElementsu.nextIndex);
|
---|
| 1593 |
|
---|
| 1594 | var _metadata10 = t.sectionMetadata("memory", startOffset, sectionSizeInBytesNode, function () {
|
---|
| 1595 | var endLoc = getPosition();
|
---|
| 1596 | return t.withLoc(t.numberLiteralFromRaw(_numberOfElements), endLoc, _startLoc21);
|
---|
| 1597 | }());
|
---|
| 1598 |
|
---|
| 1599 | var _nodes10 = parseMemorySection(_numberOfElements);
|
---|
| 1600 |
|
---|
| 1601 | return {
|
---|
| 1602 | nodes: _nodes10,
|
---|
| 1603 | metadata: _metadata10,
|
---|
| 1604 | nextSectionIndex: nextSectionIndex
|
---|
| 1605 | };
|
---|
| 1606 | }
|
---|
| 1607 |
|
---|
| 1608 | case _helperWasmBytecode.default.sections.data:
|
---|
| 1609 | {
|
---|
| 1610 | dumpSep("section Data");
|
---|
| 1611 | dump([sectionId], "section code");
|
---|
| 1612 | dump([sectionSizeInBytes], "section size");
|
---|
| 1613 |
|
---|
| 1614 | var _metadata11 = t.sectionMetadata("data", startOffset, sectionSizeInBytesNode);
|
---|
| 1615 |
|
---|
| 1616 | var _startLoc22 = getPosition();
|
---|
| 1617 |
|
---|
| 1618 | var _numberOfElementsu2 = readU32();
|
---|
| 1619 |
|
---|
| 1620 | var _numberOfElements2 = _numberOfElementsu2.value;
|
---|
| 1621 | eatBytes(_numberOfElementsu2.nextIndex);
|
---|
| 1622 |
|
---|
| 1623 | _metadata11.vectorOfSize = function () {
|
---|
| 1624 | var endLoc = getPosition();
|
---|
| 1625 | return t.withLoc(t.numberLiteralFromRaw(_numberOfElements2), endLoc, _startLoc22);
|
---|
| 1626 | }();
|
---|
| 1627 |
|
---|
| 1628 | if (opts.ignoreDataSection === true) {
|
---|
| 1629 | var _remainingBytes = sectionSizeInBytes - _numberOfElementsu2.nextIndex;
|
---|
| 1630 |
|
---|
| 1631 | eatBytes(_remainingBytes); // eat the entire section
|
---|
| 1632 |
|
---|
| 1633 | dumpSep("ignore data (" + sectionSizeInBytes + " bytes)");
|
---|
| 1634 | return {
|
---|
| 1635 | nodes: [],
|
---|
| 1636 | metadata: _metadata11,
|
---|
| 1637 | nextSectionIndex: nextSectionIndex
|
---|
| 1638 | };
|
---|
| 1639 | } else {
|
---|
| 1640 | var _nodes11 = parseDataSection(_numberOfElements2);
|
---|
| 1641 |
|
---|
| 1642 | return {
|
---|
| 1643 | nodes: _nodes11,
|
---|
| 1644 | metadata: _metadata11,
|
---|
| 1645 | nextSectionIndex: nextSectionIndex
|
---|
| 1646 | };
|
---|
| 1647 | }
|
---|
| 1648 | }
|
---|
| 1649 |
|
---|
| 1650 | case _helperWasmBytecode.default.sections.custom:
|
---|
| 1651 | {
|
---|
| 1652 | dumpSep("section Custom");
|
---|
| 1653 | dump([sectionId], "section code");
|
---|
| 1654 | dump([sectionSizeInBytes], "section size");
|
---|
| 1655 | var _metadata12 = [t.sectionMetadata("custom", startOffset, sectionSizeInBytesNode)];
|
---|
| 1656 | var sectionName = readUTF8String();
|
---|
| 1657 | eatBytes(sectionName.nextIndex);
|
---|
| 1658 | dump([], "section name (".concat(sectionName.value, ")"));
|
---|
| 1659 |
|
---|
| 1660 | var _remainingBytes2 = sectionSizeInBytes - sectionName.nextIndex;
|
---|
| 1661 |
|
---|
| 1662 | if (sectionName.value === "name") {
|
---|
| 1663 | var initialOffset = offset;
|
---|
| 1664 |
|
---|
| 1665 | try {
|
---|
| 1666 | _metadata12.push.apply(_metadata12, _toConsumableArray(parseNameSection(_remainingBytes2)));
|
---|
| 1667 | } catch (e) {
|
---|
| 1668 | console.warn("Failed to decode custom \"name\" section @".concat(offset, "; ignoring (").concat(e.message, ")."));
|
---|
| 1669 | eatBytes(offset - (initialOffset + _remainingBytes2));
|
---|
| 1670 | }
|
---|
| 1671 | } else if (sectionName.value === "producers") {
|
---|
| 1672 | var _initialOffset = offset;
|
---|
| 1673 |
|
---|
| 1674 | try {
|
---|
| 1675 | _metadata12.push(parseProducersSection());
|
---|
| 1676 | } catch (e) {
|
---|
| 1677 | console.warn("Failed to decode custom \"producers\" section @".concat(offset, "; ignoring (").concat(e.message, ")."));
|
---|
| 1678 | eatBytes(offset - (_initialOffset + _remainingBytes2));
|
---|
| 1679 | }
|
---|
| 1680 | } else {
|
---|
| 1681 | // We don't parse the custom section
|
---|
| 1682 | eatBytes(_remainingBytes2);
|
---|
| 1683 | dumpSep("ignore custom " + JSON.stringify(sectionName.value) + " section (" + _remainingBytes2 + " bytes)");
|
---|
| 1684 | }
|
---|
| 1685 |
|
---|
| 1686 | return {
|
---|
| 1687 | nodes: [],
|
---|
| 1688 | metadata: _metadata12,
|
---|
| 1689 | nextSectionIndex: nextSectionIndex
|
---|
| 1690 | };
|
---|
| 1691 | }
|
---|
| 1692 | }
|
---|
| 1693 |
|
---|
| 1694 | throw new _helperApiError.CompileError("Unexpected section: " + toHex(sectionId));
|
---|
| 1695 | }
|
---|
| 1696 |
|
---|
| 1697 | parseModuleHeader();
|
---|
| 1698 | parseVersion();
|
---|
| 1699 | var moduleFields = [];
|
---|
| 1700 | var sectionIndex = 0;
|
---|
| 1701 | var moduleMetadata = {
|
---|
| 1702 | sections: [],
|
---|
| 1703 | functionNames: [],
|
---|
| 1704 | localNames: [],
|
---|
| 1705 | producers: []
|
---|
| 1706 | };
|
---|
| 1707 | /**
|
---|
| 1708 | * All the generate declaration are going to be stored in our state
|
---|
| 1709 | */
|
---|
| 1710 |
|
---|
| 1711 | while (offset < buf.length) {
|
---|
| 1712 | var _parseSection = parseSection(sectionIndex),
|
---|
| 1713 | _nodes12 = _parseSection.nodes,
|
---|
| 1714 | _metadata13 = _parseSection.metadata,
|
---|
| 1715 | nextSectionIndex = _parseSection.nextSectionIndex;
|
---|
| 1716 |
|
---|
| 1717 | moduleFields.push.apply(moduleFields, _toConsumableArray(_nodes12));
|
---|
| 1718 | var metadataArray = Array.isArray(_metadata13) ? _metadata13 : [_metadata13];
|
---|
| 1719 | metadataArray.forEach(function (metadataItem) {
|
---|
| 1720 | if (metadataItem.type === "FunctionNameMetadata") {
|
---|
| 1721 | moduleMetadata.functionNames.push(metadataItem);
|
---|
| 1722 | } else if (metadataItem.type === "LocalNameMetadata") {
|
---|
| 1723 | moduleMetadata.localNames.push(metadataItem);
|
---|
| 1724 | } else if (metadataItem.type === "ProducersSectionMetadata") {
|
---|
| 1725 | moduleMetadata.producers.push(metadataItem);
|
---|
| 1726 | } else {
|
---|
| 1727 | moduleMetadata.sections.push(metadataItem);
|
---|
| 1728 | }
|
---|
| 1729 | }); // Ignore custom section
|
---|
| 1730 |
|
---|
| 1731 | if (nextSectionIndex) {
|
---|
| 1732 | sectionIndex = nextSectionIndex;
|
---|
| 1733 | }
|
---|
| 1734 | }
|
---|
| 1735 | /**
|
---|
| 1736 | * Transform the state into AST nodes
|
---|
| 1737 | */
|
---|
| 1738 |
|
---|
| 1739 |
|
---|
| 1740 | var funcIndex = 0;
|
---|
| 1741 | state.functionsInModule.forEach(function (func) {
|
---|
| 1742 | var params = func.signature.params;
|
---|
| 1743 | var result = func.signature.result;
|
---|
| 1744 | var body = []; // External functions doesn't provide any code, can skip it here
|
---|
| 1745 |
|
---|
| 1746 | if (func.isExternal === true) {
|
---|
| 1747 | return;
|
---|
| 1748 | }
|
---|
| 1749 |
|
---|
| 1750 | var decodedElementInCodeSection = state.elementsInCodeSection[funcIndex];
|
---|
| 1751 |
|
---|
| 1752 | if (opts.ignoreCodeSection === false) {
|
---|
| 1753 | if (typeof decodedElementInCodeSection === "undefined") {
|
---|
| 1754 | throw new _helperApiError.CompileError("func " + toHex(funcIndex) + " code not found");
|
---|
| 1755 | }
|
---|
| 1756 |
|
---|
| 1757 | body = decodedElementInCodeSection.code;
|
---|
| 1758 | }
|
---|
| 1759 |
|
---|
| 1760 | funcIndex++;
|
---|
| 1761 | var funcNode = t.func(func.id, t.signature(params, result), body);
|
---|
| 1762 |
|
---|
| 1763 | if (func.isExternal === true) {
|
---|
| 1764 | funcNode.isExternal = func.isExternal;
|
---|
| 1765 | } // Add function position in the binary if possible
|
---|
| 1766 |
|
---|
| 1767 |
|
---|
| 1768 | if (opts.ignoreCodeSection === false) {
|
---|
| 1769 | var _startLoc23 = decodedElementInCodeSection.startLoc,
|
---|
| 1770 | endLoc = decodedElementInCodeSection.endLoc,
|
---|
| 1771 | bodySize = decodedElementInCodeSection.bodySize;
|
---|
| 1772 | funcNode = t.withLoc(funcNode, endLoc, _startLoc23);
|
---|
| 1773 | funcNode.metadata = {
|
---|
| 1774 | bodySize: bodySize
|
---|
| 1775 | };
|
---|
| 1776 | }
|
---|
| 1777 |
|
---|
| 1778 | moduleFields.push(funcNode);
|
---|
| 1779 | });
|
---|
| 1780 | state.elementsInExportSection.forEach(function (moduleExport) {
|
---|
| 1781 | /**
|
---|
| 1782 | * If the export has no id, we won't be able to call it from the outside
|
---|
| 1783 | * so we can omit it
|
---|
| 1784 | */
|
---|
| 1785 | if (moduleExport.id != null) {
|
---|
| 1786 | moduleFields.push(t.withLoc(t.moduleExport(moduleExport.name, t.moduleExportDescr(moduleExport.type, moduleExport.id)), moduleExport.endLoc, moduleExport.startLoc));
|
---|
| 1787 | }
|
---|
| 1788 | });
|
---|
| 1789 | dumpSep("end of program");
|
---|
| 1790 | var module = t.module(null, moduleFields, t.moduleMetadata(moduleMetadata.sections, moduleMetadata.functionNames, moduleMetadata.localNames, moduleMetadata.producers));
|
---|
| 1791 | return t.program([module]);
|
---|
| 1792 | } |
---|