Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wasmparser

Package Overview
Dependencies
Maintainers
2
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wasmparser - npm Package Compare versions

Comparing version 0.3.10 to 0.4.0

dist/index.d.ts

13

dist/WasmDis.d.ts
import { BinaryReader } from './WasmParser';
export interface IDisassemblerResult {
lines: Array<string>;
offsets?: Array<number>;
done: boolean;
}
export declare class WasmDisassembler {
private _lines;
private _offsets;
private _buffer;

@@ -14,4 +21,8 @@ private _types;

private _nextLineToAddOffset;
private _done;
private _currentPosition;
constructor();
addOffsets: boolean;
private appendBuffer(s);
private newLine();
private printType(typeIndex);

@@ -22,2 +33,4 @@ private printFuncType(type, printVars);

disassemble(reader: BinaryReader): string;
getResult(): IDisassemblerResult;
disassembleChunk(reader: BinaryReader, offsetInModule?: number): boolean;
}

903

dist/WasmDis.js

@@ -1,449 +0,494 @@

(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./WasmParser"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* Copyright 2016 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var WasmParser_1 = require("./WasmParser");
function binToString(b) {
var buffer = [];
for (var i = 0; i < b.length; i++) {
var byte = b[i];
if (byte < 0x20 || byte >= 0x7F ||
byte == 0x22 || byte == 0x5c) {
buffer.push('\\' + (byte >> 4).toString(16) + (byte & 15).toString(16));
}
else {
buffer.push(String.fromCharCode(byte));
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* Copyright 2016 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var WasmParser_1 = require("./WasmParser");
function binToString(b) {
var buffer = [];
for (var i = 0; i < b.length; i++) {
var byte = b[i];
if (byte < 0x20 || byte >= 0x7F ||
byte == 0x22 || byte == 0x5c) {
buffer.push('\\' + (byte >> 4).toString(16) + (byte & 15).toString(16));
}
return buffer.join('');
}
function typeToString(type) {
switch (type) {
case -1 /* i32 */: return 'i32';
case -2 /* i64 */: return 'i64';
case -3 /* f32 */: return 'f32';
case -4 /* f64 */: return 'f64';
case -16 /* anyfunc */: return 'anyfunc';
default: throw new Error('Unexpected type');
else {
buffer.push(String.fromCharCode(byte));
}
}
function formatFloat32(n) {
if (n === 0)
return (1 / n) < 0 ? '-0.0' : '0.0';
if (isFinite(n))
return n.toString();
if (!isNaN(n))
return n < 0 ? '-infinity' : 'infinity';
var view = new DataView(new ArrayBuffer(8));
view.setFloat32(0, n, true);
var data = view.getInt32(0, true);
var payload = data & 0x7FFFFF;
var canonicalBits = 4194304; // 0x800..0
if (data > 0 && payload === canonicalBits)
return 'nan'; // canonical NaN;
else if (payload === canonicalBits)
return '-nan';
return (data < 0 ? '-' : '+') + 'nan:0x' + payload.toString(16);
return buffer.join('');
}
function typeToString(type) {
switch (type) {
case -1 /* i32 */: return 'i32';
case -2 /* i64 */: return 'i64';
case -3 /* f32 */: return 'f32';
case -4 /* f64 */: return 'f64';
case -16 /* anyfunc */: return 'anyfunc';
default: throw new Error('Unexpected type');
}
function formatFloat64(n) {
if (n === 0)
return (1 / n) < 0 ? '-0.0' : '0.0';
if (isFinite(n))
return n.toString();
if (!isNaN(n))
return n < 0 ? '-infinity' : 'infinity';
var view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, n, true);
var data1 = view.getUint32(0, true);
var data2 = view.getInt32(4, true);
var payload = data1 + (data2 & 0xFFFFF) * 4294967296;
var canonicalBits = 524288 * 4294967296; // 0x800..0
if (data2 > 0 && payload === canonicalBits)
return 'nan'; // canonical NaN;
else if (payload === canonicalBits)
return '-nan';
return (data2 < 0 ? '-' : '+') + 'nan:0x' + payload.toString(16);
}
function formatFloat32(n) {
if (n === 0)
return (1 / n) < 0 ? '-0.0' : '0.0';
if (isFinite(n))
return n.toString();
if (!isNaN(n))
return n < 0 ? '-infinity' : 'infinity';
var view = new DataView(new ArrayBuffer(8));
view.setFloat32(0, n, true);
var data = view.getInt32(0, true);
var payload = data & 0x7FFFFF;
var canonicalBits = 4194304; // 0x800..0
if (data > 0 && payload === canonicalBits)
return 'nan'; // canonical NaN;
else if (payload === canonicalBits)
return '-nan';
return (data < 0 ? '-' : '+') + 'nan:0x' + payload.toString(16);
}
function formatFloat64(n) {
if (n === 0)
return (1 / n) < 0 ? '-0.0' : '0.0';
if (isFinite(n))
return n.toString();
if (!isNaN(n))
return n < 0 ? '-infinity' : 'infinity';
var view = new DataView(new ArrayBuffer(8));
view.setFloat64(0, n, true);
var data1 = view.getUint32(0, true);
var data2 = view.getInt32(4, true);
var payload = data1 + (data2 & 0xFFFFF) * 4294967296;
var canonicalBits = 524288 * 4294967296; // 0x800..0
if (data2 > 0 && payload === canonicalBits)
return 'nan'; // canonical NaN;
else if (payload === canonicalBits)
return '-nan';
return (data2 < 0 ? '-' : '+') + 'nan:0x' + payload.toString(16);
}
function memoryAddressToString(address, code) {
var defaultAlignFlags;
switch (code) {
case 41 /* i64_load */:
case 55 /* i64_store */:
defaultAlignFlags = 3;
break;
case 40 /* i32_load */:
case 52 /* i64_load32_s */:
case 53 /* i64_load32_u */:
case 54 /* i32_store */:
case 62 /* i64_store32 */:
defaultAlignFlags = 2;
break;
case 46 /* i32_load16_s */:
case 47 /* i32_load16_u */:
case 50 /* i64_load16_s */:
case 51 /* i64_load16_u */:
case 59 /* i32_store16 */:
case 61 /* i64_store16 */:
defaultAlignFlags = 1;
break;
case 44 /* i32_load8_s */:
case 45 /* i32_load8_u */:
case 48 /* i64_load8_s */:
case 49 /* i64_load8_u */:
case 58 /* i32_store8 */:
case 60 /* i64_store8 */:
defaultAlignFlags = 0;
break;
}
function memoryAddressToString(address, code) {
var defaultAlignFlags;
switch (code) {
case 41 /* i64_load */:
case 55 /* i64_store */:
defaultAlignFlags = 3;
break;
case 40 /* i32_load */:
case 52 /* i64_load32_s */:
case 53 /* i64_load32_u */:
case 54 /* i32_store */:
case 62 /* i64_store32 */:
defaultAlignFlags = 2;
break;
case 46 /* i32_load16_s */:
case 47 /* i32_load16_u */:
case 50 /* i64_load16_s */:
case 51 /* i64_load16_u */:
case 59 /* i32_store16 */:
case 61 /* i64_store16 */:
defaultAlignFlags = 1;
break;
case 44 /* i32_load8_s */:
case 45 /* i32_load8_u */:
case 48 /* i64_load8_s */:
case 49 /* i64_load8_u */:
case 58 /* i32_store8 */:
case 60 /* i64_store8 */:
defaultAlignFlags = 0;
break;
}
if (address.flags == defaultAlignFlags)
return "offset=" + address.offset;
if (!address.offset)
return "align=" + (1 << address.flags);
return "offset=" + address.offset + " align=" + (1 << address.flags);
}
function globalTypeToString(type) {
if (!type.mutability)
return typeToString(type.contentType);
return "(mut " + typeToString(type.contentType) + ")";
}
function limitsToString(limits) {
return limits.initial + (limits.maximum !== undefined ? ' ' + limits.maximum : '');
}
function formatHex(n, width) {
var s = n.toString(16).toUpperCase();
if (width === undefined)
return s;
while (s.length < width)
s = '0' + s;
if (address.flags == defaultAlignFlags)
return "offset=" + address.offset;
if (!address.offset)
return "align=" + (1 << address.flags);
return "offset=" + address.offset + " align=" + (1 << address.flags);
}
function globalTypeToString(type) {
if (!type.mutability)
return typeToString(type.contentType);
return "(mut " + typeToString(type.contentType) + ")";
}
function limitsToString(limits) {
return limits.initial + (limits.maximum !== undefined ? ' ' + limits.maximum : '');
}
function formatHex(n, width) {
var s = n.toString(16).toUpperCase();
if (width === undefined)
return s;
while (s.length < width)
s = '0' + s;
return s;
}
var IndentIncrement = ' ';
var WasmDisassembler = (function () {
function WasmDisassembler() {
this._lines = [];
this._offsets = [];
this._buffer = [];
this._types = [];
this._funcIndex = 0;
this._funcTypes = [];
this._importCount = 0;
this._globalCount = 0;
this._tableCount = 0;
this._indent = null;
this._indentLevel = 0;
this._addOffsets = false;
this._done = false;
this._currentPosition = 0;
}
var IndentIncrement = ' ';
var WasmDisassembler = (function () {
function WasmDisassembler() {
this._buffer = [];
this._types = [];
this._funcIndex = 0;
this._funcTypes = [];
this._importCount = 0;
this._globalCount = 0;
this._tableCount = 0;
this._indent = null;
this._indentLevel = 0;
this._addOffsets = false;
this._nextLineToAddOffset = 0;
Object.defineProperty(WasmDisassembler.prototype, "addOffsets", {
get: function () {
return this._addOffsets;
},
set: function (value) {
if (this._lines.length > 0 && this._addOffsets != value)
throw new Error('Cannot switch addOffsets in the middle of the chunk.');
this._addOffsets = value;
},
enumerable: true,
configurable: true
});
WasmDisassembler.prototype.appendBuffer = function (s) {
this._buffer.push(s);
};
WasmDisassembler.prototype.newLine = function () {
if (this.addOffsets)
this._offsets.push(this._currentPosition);
var line = this._buffer.join('');
this._buffer.length = 0;
this._lines.push(line);
};
WasmDisassembler.prototype.printType = function (typeIndex) {
var type = this._types[typeIndex];
if (type.form !== -32 /* func */)
throw new Error('NYI other function form');
return "(func" + this.printFuncType(type, false) + ")";
};
WasmDisassembler.prototype.printFuncType = function (type, printVars) {
var result = [];
if (printVars) {
for (var i = 0; i < type.params.length; i++)
result.push(" (param $var" + i + " " + typeToString(type.params[i]) + ")");
}
Object.defineProperty(WasmDisassembler.prototype, "addOffsets", {
get: function () {
return this._addOffsets;
},
set: function (value) {
this._addOffsets = value;
},
enumerable: true,
configurable: true
});
WasmDisassembler.prototype.printType = function (typeIndex) {
var type = this._types[typeIndex];
if (type.form !== -32 /* func */)
throw new Error('NYI other function form');
return "(func" + this.printFuncType(type, false) + ")";
else if (type.params.length > 0) {
result.push(' (param');
for (var i = 0; i < type.params.length; i++)
result.push(' ', typeToString(type.params[i]));
result.push(')');
}
for (var i = 0; i < type.returns.length; i++) {
result.push(" (result " + typeToString(type.returns[i]) + ")");
}
return result.join('');
};
WasmDisassembler.prototype.increaseIndent = function () {
this._indent += IndentIncrement;
this._indentLevel++;
};
WasmDisassembler.prototype.decreaseIndent = function () {
this._indent = this._indent.slice(0, -IndentIncrement.length);
this._indentLevel--;
};
WasmDisassembler.prototype.disassemble = function (reader) {
var _this = this;
var done = this.disassembleChunk(reader);
if (!done)
return null;
var lines = this._lines;
if (this._addOffsets) {
lines = lines.map(function (line, index) {
var position = formatHex(_this._offsets[index], 4);
return line + ' ;; @' + position;
});
}
lines.push(''); // we need '\n' after last line
var result = lines.join('\n');
this._lines.length = 0;
this._offsets.length = 0;
return result;
};
WasmDisassembler.prototype.getResult = function () {
var result = {
lines: this._lines,
offsets: this._addOffsets ? this._offsets : undefined,
done: this._done,
};
WasmDisassembler.prototype.printFuncType = function (type, printVars) {
var result = [];
if (printVars) {
for (var i = 0; i < type.params.length; i++)
result.push(" (param $var" + i + " " + typeToString(type.params[i]) + ")");
}
else if (type.params.length > 0) {
result.push(' (param');
for (var i = 0; i < type.params.length; i++)
result.push(' ', typeToString(type.params[i]));
result.push(')');
}
for (var i = 0; i < type.returns.length; i++) {
result.push(" (result " + typeToString(type.returns[i]) + ")");
}
return result.join('');
};
WasmDisassembler.prototype.increaseIndent = function () {
this._indent += IndentIncrement;
this._indentLevel++;
};
WasmDisassembler.prototype.decreaseIndent = function () {
this._indent = this._indent.slice(0, -IndentIncrement.length);
this._indentLevel--;
};
WasmDisassembler.prototype.disassemble = function (reader) {
var _this = this;
while (true) {
var position = reader.position;
if (!reader.read())
return null;
switch (reader.state) {
case 2 /* END_WASM */:
this._buffer.push(')\n');
if (!reader.hasMoreBytes()) {
var result = this._buffer.join('');
this._buffer.length = 0;
return result;
this._lines = [];
if (this._addOffsets)
this._offsets = [];
return result;
};
WasmDisassembler.prototype.disassembleChunk = function (reader, offsetInModule) {
var _this = this;
if (offsetInModule === void 0) { offsetInModule = 0; }
if (this._done)
throw new Error('Invalid state: disassembly process was already finished.');
while (true) {
this._currentPosition = reader.position + offsetInModule;
if (!reader.read())
return false;
switch (reader.state) {
case 2 /* END_WASM */:
this.appendBuffer(')');
this.newLine();
if (!reader.hasMoreBytes()) {
this._done = true;
return true;
}
break;
case -1 /* ERROR */:
throw reader.error;
case 1 /* BEGIN_WASM */:
this.appendBuffer('(module');
this.newLine();
break;
case 4 /* END_SECTION */:
break;
case 3 /* BEGIN_SECTION */:
var sectionInfo = reader.result;
switch (sectionInfo.id) {
case 1 /* Type */:
case 2 /* Import */:
case 7 /* Export */:
case 6 /* Global */:
case 3 /* Function */:
case 10 /* Code */:
case 5 /* Memory */:
case 11 /* Data */:
case 4 /* Table */:
case 9 /* Element */:
break; // reading known section;
default:
reader.skipSection();
break;
}
break;
case 15 /* MEMORY_SECTION_ENTRY */:
var memoryInfo = reader.result;
this.appendBuffer(" (memory " + memoryInfo.limits.initial);
if (memoryInfo.limits.maximum !== undefined) {
this.appendBuffer(" " + memoryInfo.limits.maximum);
}
this.appendBuffer(')');
this.newLine();
break;
case 14 /* TABLE_SECTION_ENTRY */:
var tableInfo = reader.result;
this.appendBuffer(" (table $table" + this._tableCount++ + " " + limitsToString(tableInfo.limits) + " " + typeToString(tableInfo.elementType) + ")");
this.newLine();
break;
case 17 /* EXPORT_SECTION_ENTRY */:
var exportInfo = reader.result;
this.appendBuffer(" (export \"" + binToString(exportInfo.field) + "\" ");
switch (exportInfo.kind) {
case 0 /* Function */:
this.appendBuffer("$func" + exportInfo.index);
break;
case 1 /* Table */:
this.appendBuffer("(table $table" + exportInfo.index + ")");
break;
case 2 /* Memory */:
this.appendBuffer("memory");
break;
case 3 /* Global */:
this.appendBuffer("(global $global" + exportInfo.index + ")");
break;
default:
throw new Error("Unsupported export " + exportInfo.kind);
}
this.appendBuffer(')');
this.newLine();
break;
case 12 /* IMPORT_SECTION_ENTRY */:
var importInfo = reader.result;
var importSource = "\"" + binToString(importInfo.module) + "\" \"" + binToString(importInfo.field) + "\"";
switch (importInfo.kind) {
case 0 /* Function */:
this.appendBuffer(" (import $func" + this._importCount++ + " " + importSource + " " + this.printType(importInfo.funcTypeIndex) + ")");
break;
case 1 /* Table */:
var tableImportInfo = importInfo.type;
this.appendBuffer(" (import " + importSource + " (table $table" + this._tableCount++ + " " + limitsToString(tableImportInfo.limits) + " " + typeToString(tableImportInfo.elementType) + "))");
break;
case 2 /* Memory */:
var memoryImportInfo = importInfo.type;
this.appendBuffer(" (import " + importSource + " (memory " + limitsToString(memoryImportInfo.limits) + "))");
break;
case 3 /* Global */:
var globalImportInfo = importInfo.type;
this.appendBuffer(" (import " + importSource + " (global $global" + this._globalCount++ + " " + globalTypeToString(globalImportInfo) + "))");
break;
default:
throw new Error("NYI other import types: " + importInfo.kind);
}
this.newLine();
break;
case 33 /* BEGIN_ELEMENT_SECTION_ENTRY */:
var elementSegmentInfo = reader.result;
this.appendBuffer(" (elem");
this.newLine();
break;
case 35 /* END_ELEMENT_SECTION_ENTRY */:
this.appendBuffer(' )');
this.newLine();
break;
case 34 /* ELEMENT_SECTION_ENTRY_BODY */:
var elementSegmentBody = reader.result;
this.appendBuffer(' ');
elementSegmentBody.elements.forEach(function (funcIndex) {
_this.appendBuffer(" $func" + funcIndex);
});
this.newLine();
break;
case 39 /* BEGIN_GLOBAL_SECTION_ENTRY */:
var globalInfo = reader.result;
this.appendBuffer(" (global $global" + this._globalCount++ + " " + globalTypeToString(globalInfo.type));
this.newLine();
break;
case 40 /* END_GLOBAL_SECTION_ENTRY */:
this.appendBuffer(' )');
this.newLine();
break;
case 11 /* TYPE_SECTION_ENTRY */:
var funcType = reader.result;
var typeIndex = this._types.length;
this._types.push(funcType);
this.appendBuffer(" (type $type" + typeIndex + " " + this.printType(typeIndex) + ")");
this.newLine();
break;
case 36 /* BEGIN_DATA_SECTION_ENTRY */:
this.appendBuffer(" (data");
this.newLine();
break;
case 37 /* DATA_SECTION_ENTRY_BODY */:
var body = reader.result;
this.appendBuffer(" \"" + binToString(body.data) + "\"");
this.newLine();
break;
case 38 /* END_DATA_SECTION_ENTRY */:
this.appendBuffer(" )");
this.newLine();
break;
case 25 /* BEGIN_INIT_EXPRESSION_BODY */:
this._indent = ' ';
this._indentLevel = 0;
this.appendBuffer(' (');
this.newLine();
break;
case 27 /* END_INIT_EXPRESSION_BODY */:
this.appendBuffer(' )');
this.newLine();
break;
case 13 /* FUNCTION_SECTION_ENTRY */:
this._funcTypes.push(reader.result.typeIndex);
break;
case 28 /* BEGIN_FUNCTION_BODY */:
var func = reader.result;
var type = this._types[this._funcTypes[this._funcIndex]];
var printIndex = this._funcIndex + this._importCount;
this.appendBuffer(" (func $func" + printIndex + this.printFuncType(type, true));
this.newLine();
var localIndex = type.params.length;
for (var _i = 0, _a = func.locals; _i < _a.length; _i++) {
var l = _a[_i];
for (var i = 0; i < l.count; i++) {
this.appendBuffer(" (local $var" + localIndex++ + " " + typeToString(l.type) + ")");
this.newLine();
}
}
this._funcIndex++;
this._indent = ' ';
this._indentLevel = 0;
break;
case 26 /* INIT_EXPRESSION_OPERATOR */:
case 30 /* CODE_OPERATOR */:
var operator = reader.result;
if (operator.code == 11 /* end */ && this._indentLevel == 0) {
// reached of the function, skipping the operator
break;
case -1 /* ERROR */:
throw reader.error;
case 1 /* BEGIN_WASM */:
this._buffer.push('(module\n');
break;
case 4 /* END_SECTION */:
break;
case 3 /* BEGIN_SECTION */:
var sectionInfo = reader.result;
switch (sectionInfo.id) {
case 1 /* Type */:
case 2 /* Import */:
case 7 /* Export */:
case 6 /* Global */:
case 3 /* Function */:
case 10 /* Code */:
case 5 /* Memory */:
case 11 /* Data */:
case 4 /* Table */:
case 9 /* Element */:
break; // reading known section;
default:
reader.skipSection();
}
switch (operator.code) {
case 11 /* end */:
case 5 /* else */:
this.decreaseIndent();
break;
}
var str = WasmParser_1.OperatorCodeNames[operator.code].replace(/^([if](32|64))_/, "$1.").replace(/_([if](32|64))$/, "\/$1");
if (operator.blockType !== undefined &&
operator.blockType !== -64 /* empty_block_type */) {
str += ' ' + typeToString(operator.blockType);
}
this.appendBuffer(this._indent);
this.appendBuffer(str);
if (operator.localIndex !== undefined) {
this.appendBuffer(" $var" + operator.localIndex);
}
if (operator.funcIndex !== undefined) {
this.appendBuffer(" $func" + operator.funcIndex);
}
if (operator.typeIndex !== undefined) {
this.appendBuffer(" $type" + operator.typeIndex);
}
if (operator.literal !== undefined) {
switch (operator.code) {
case 65 /* i32_const */:
this.appendBuffer(" " + operator.literal.toString());
break;
}
break;
case 15 /* MEMORY_SECTION_ENTRY */:
var memoryInfo = reader.result;
this._buffer.push(" (memory " + memoryInfo.limits.initial);
if (memoryInfo.limits.maximum !== undefined) {
this._buffer.push(" " + memoryInfo.limits.maximum);
}
this._buffer.push(')\n');
break;
case 14 /* TABLE_SECTION_ENTRY */:
var tableInfo = reader.result;
this._buffer.push(" (table $table" + this._tableCount++ + " " + limitsToString(tableInfo.limits) + " " + typeToString(tableInfo.elementType) + ")\n");
break;
case 17 /* EXPORT_SECTION_ENTRY */:
var exportInfo = reader.result;
this._buffer.push(" (export \"" + binToString(exportInfo.field) + "\" ");
switch (exportInfo.kind) {
case 0 /* Function */:
this._buffer.push("$func" + exportInfo.index);
case 67 /* f32_const */:
this.appendBuffer(" " + formatFloat32(operator.literal));
break;
case 1 /* Table */:
this._buffer.push("(table $table" + exportInfo.index + ")");
case 68 /* f64_const */:
this.appendBuffer(" " + formatFloat64(operator.literal));
break;
case 2 /* Memory */:
this._buffer.push("memory");
case 66 /* i64_const */:
this.appendBuffer(" " + operator.literal.toDouble());
break;
case 3 /* Global */:
this._buffer.push("(global $global" + exportInfo.index + ")");
break;
default:
throw new Error("Unsupported export " + exportInfo.kind);
}
this._buffer.push(')\n');
break;
case 12 /* IMPORT_SECTION_ENTRY */:
var importInfo = reader.result;
var importSource = "\"" + binToString(importInfo.module) + "\" \"" + binToString(importInfo.field) + "\"";
switch (importInfo.kind) {
case 0 /* Function */:
this._buffer.push(" (import $func" + this._importCount++ + " " + importSource + " " + this.printType(importInfo.funcTypeIndex) + ")\n");
break;
case 1 /* Table */:
var tableImportInfo = importInfo.type;
this._buffer.push(" (import " + importSource + " (table $table" + this._tableCount++ + " " + limitsToString(tableImportInfo.limits) + " " + typeToString(tableImportInfo.elementType) + "))\n");
break;
case 2 /* Memory */:
var memoryImportInfo = importInfo.type;
this._buffer.push(" (import " + importSource + " (memory " + limitsToString(memoryImportInfo.limits) + "))\n");
break;
case 3 /* Global */:
var globalImportInfo = importInfo.type;
this._buffer.push(" (import " + importSource + " (global $global" + this._globalCount++ + " " + globalTypeToString(globalImportInfo) + "))\n");
break;
default:
throw new Error("NYI other import types: " + importInfo.kind);
}
break;
case 33 /* BEGIN_ELEMENT_SECTION_ENTRY */:
var elementSegmentInfo = reader.result;
this._buffer.push(" (elem\n");
break;
case 35 /* END_ELEMENT_SECTION_ENTRY */:
this._buffer.push(' )\n');
break;
case 34 /* ELEMENT_SECTION_ENTRY_BODY */:
var elementSegmentBody = reader.result;
this._buffer.push(' ');
elementSegmentBody.elements.forEach(function (funcIndex) {
_this._buffer.push(" $func" + funcIndex);
});
this._buffer.push('\n');
break;
case 39 /* BEGIN_GLOBAL_SECTION_ENTRY */:
var globalInfo = reader.result;
this._buffer.push(" (global $global" + this._globalCount++ + " " + globalTypeToString(globalInfo.type) + "\n");
break;
case 40 /* END_GLOBAL_SECTION_ENTRY */:
this._buffer.push(' )\n');
break;
case 11 /* TYPE_SECTION_ENTRY */:
var funcType = reader.result;
var typeIndex = this._types.length;
this._types.push(funcType);
this._buffer.push(" (type $type" + typeIndex + " " + this.printType(typeIndex) + ")\n");
break;
case 36 /* BEGIN_DATA_SECTION_ENTRY */:
this._buffer.push(" (data\n");
break;
case 37 /* DATA_SECTION_ENTRY_BODY */:
var body = reader.result;
this._buffer.push(" \"" + binToString(body.data) + "\"\n");
break;
case 38 /* END_DATA_SECTION_ENTRY */:
this._buffer.push(" )\n");
break;
case 25 /* BEGIN_INIT_EXPRESSION_BODY */:
this._indent = ' ';
this._indentLevel = 0;
this._buffer.push(' (\n');
break;
case 27 /* END_INIT_EXPRESSION_BODY */:
this._buffer.push(' )\n');
break;
case 13 /* FUNCTION_SECTION_ENTRY */:
this._funcTypes.push(reader.result.typeIndex);
break;
case 28 /* BEGIN_FUNCTION_BODY */:
var func = reader.result;
var type = this._types[this._funcTypes[this._funcIndex]];
var printIndex = this._funcIndex + this._importCount;
this._buffer.push(" (func $func" + printIndex + this.printFuncType(type, true) + "\n");
var localIndex = type.params.length;
for (var _i = 0, _a = func.locals; _i < _a.length; _i++) {
var l = _a[_i];
for (var i = 0; i < l.count; i++) {
this._buffer.push(" (local $var" + localIndex++ + " " + typeToString(l.type) + ")\n");
}
}
this._funcIndex++;
this._indent = ' ';
this._indentLevel = 0;
break;
case 26 /* INIT_EXPRESSION_OPERATOR */:
case 30 /* CODE_OPERATOR */:
var operator = reader.result;
if (operator.code == 11 /* end */ && this._indentLevel == 0) {
// reached of the function, skipping the operator
}
if (operator.memoryAddress !== undefined) {
this.appendBuffer(" " + memoryAddressToString(operator.memoryAddress, operator.code));
}
if (operator.brDepth !== undefined) {
this.appendBuffer(" " + operator.brDepth);
}
if (operator.brTable !== undefined) {
for (var i = 0; i < operator.brTable.length; i++)
this.appendBuffer(" " + operator.brTable[i]);
}
if (operator.globalIndex !== undefined) {
this.appendBuffer(" $global" + operator.globalIndex);
}
this.newLine();
switch (operator.code) {
case 4 /* if */:
case 2 /* block */:
case 3 /* loop */:
case 5 /* else */:
this.increaseIndent();
break;
}
switch (operator.code) {
case 11 /* end */:
case 5 /* else */:
this.decreaseIndent();
break;
}
var str = WasmParser_1.OperatorCodeNames[operator.code].replace(/^([if](32|64))_/, "$1.").replace(/_([if](32|64))$/, "\/$1");
if (operator.blockType !== undefined &&
operator.blockType !== -64 /* empty_block_type */) {
str += ' ' + typeToString(operator.blockType);
}
this._buffer.push(this._indent, str);
if (operator.localIndex !== undefined) {
this._buffer.push(" $var" + operator.localIndex);
}
if (operator.funcIndex !== undefined) {
this._buffer.push(" $func" + operator.funcIndex);
}
if (operator.typeIndex !== undefined) {
this._buffer.push(" $type" + operator.typeIndex);
}
if (operator.literal !== undefined) {
switch (operator.code) {
case 65 /* i32_const */:
this._buffer.push(" " + operator.literal.toString());
break;
case 67 /* f32_const */:
this._buffer.push(" " + formatFloat32(operator.literal));
break;
case 68 /* f64_const */:
this._buffer.push(" " + formatFloat64(operator.literal));
break;
case 66 /* i64_const */:
this._buffer.push(" " + operator.literal.toDouble());
break;
}
}
if (operator.memoryAddress !== undefined) {
this._buffer.push(" " + memoryAddressToString(operator.memoryAddress, operator.code));
}
if (operator.brDepth !== undefined) {
this._buffer.push(" " + operator.brDepth);
}
if (operator.brTable !== undefined) {
for (var i = 0; i < operator.brTable.length; i++)
this._buffer.push(" " + operator.brTable[i]);
}
if (operator.globalIndex !== undefined) {
this._buffer.push(" $global" + operator.globalIndex);
}
this._buffer.push('\n');
switch (operator.code) {
case 4 /* if */:
case 2 /* block */:
case 3 /* loop */:
case 5 /* else */:
this.increaseIndent();
break;
}
break;
case 31 /* END_FUNCTION_BODY */:
this._buffer.push(" )\n");
break;
default:
throw new Error("Expectected state: " + reader.state);
}
if (this._addOffsets && this._nextLineToAddOffset < this._buffer.length) {
var i = this._nextLineToAddOffset;
var line = this._buffer[i];
while (line.indexOf('\n') < 0) {
if (++i >= this._buffer.length)
break;
line = this._buffer[i];
}
this._buffer[i] = line.replace(/\n/, " ;; @" + formatHex(position, 4) + "\n");
this._nextLineToAddOffset = this._buffer.length;
}
break;
case 31 /* END_FUNCTION_BODY */:
this.appendBuffer(" )");
this.newLine();
break;
default:
throw new Error("Expectected state: " + reader.state);
}
};
return WasmDisassembler;
}());
exports.WasmDisassembler = WasmDisassembler;
});
}
};
return WasmDisassembler;
}());
exports.WasmDisassembler = WasmDisassembler;
//# sourceMappingURL=WasmDis.js.map

@@ -1,748 +0,738 @@

(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* Copyright 2017 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var WasmParser_1 = require("./WasmParser");
var EmitterState;
(function (EmitterState) {
EmitterState[EmitterState["Initial"] = 0] = "Initial";
EmitterState[EmitterState["Error"] = 1] = "Error";
EmitterState[EmitterState["Wasm"] = 2] = "Wasm";
EmitterState[EmitterState["CustomSecton"] = 3] = "CustomSecton";
EmitterState[EmitterState["TypeSection"] = 4] = "TypeSection";
EmitterState[EmitterState["ImportSection"] = 5] = "ImportSection";
EmitterState[EmitterState["FunctionSection"] = 6] = "FunctionSection";
EmitterState[EmitterState["TableSection"] = 7] = "TableSection";
EmitterState[EmitterState["MemorySection"] = 8] = "MemorySection";
EmitterState[EmitterState["GlobalSection"] = 9] = "GlobalSection";
EmitterState[EmitterState["ExportSection"] = 10] = "ExportSection";
EmitterState[EmitterState["StartSection"] = 11] = "StartSection";
EmitterState[EmitterState["ElementSection"] = 12] = "ElementSection";
EmitterState[EmitterState["CodeSection"] = 13] = "CodeSection";
EmitterState[EmitterState["DataSection"] = 14] = "DataSection";
EmitterState[EmitterState["FunctionBody"] = 15] = "FunctionBody";
EmitterState[EmitterState["DataSectionEntry"] = 16] = "DataSectionEntry";
EmitterState[EmitterState["DataSectionEntryBody"] = 17] = "DataSectionEntryBody";
EmitterState[EmitterState["DataSectionEntryEnd"] = 18] = "DataSectionEntryEnd";
EmitterState[EmitterState["InitExpression"] = 19] = "InitExpression";
EmitterState[EmitterState["ElementSectionEntry"] = 20] = "ElementSectionEntry";
EmitterState[EmitterState["ElementSectionEntryBody"] = 21] = "ElementSectionEntryBody";
EmitterState[EmitterState["ElementSectionEntryEnd"] = 22] = "ElementSectionEntryEnd";
EmitterState[EmitterState["GlobalSectionEntry"] = 23] = "GlobalSectionEntry";
EmitterState[EmitterState["GlobalSectionEntryEnd"] = 24] = "GlobalSectionEntryEnd";
EmitterState[EmitterState["RawDataSection"] = 25] = "RawDataSection";
EmitterState[EmitterState["NameEntry"] = 26] = "NameEntry";
EmitterState[EmitterState["RelocHeader"] = 27] = "RelocHeader";
EmitterState[EmitterState["RelocEntry"] = 28] = "RelocEntry";
EmitterState[EmitterState["LinkingEntry"] = 29] = "LinkingEntry";
EmitterState[EmitterState["SourceMappingURL"] = 30] = "SourceMappingURL";
EmitterState[EmitterState["SourceMappingURLEnd"] = 31] = "SourceMappingURLEnd";
})(EmitterState || (EmitterState = {}));
var Emitter = (function () {
function Emitter() {
this._buffer = [];
this._state = EmitterState.Initial;
this._sectionStart = 0;
this._sectionSizeBytes = 0;
this._sectionEntiesCount = 0;
this._sectionEntiesCountBytes = 0;
this._bodyStart = 0;
this._bodySizeBytes = 0;
this._data = null;
this._endWritten = false;
this._initExpressionAfterState = EmitterState.Initial;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./WasmParser"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* Copyright 2017 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var WasmParser_1 = require("./WasmParser");
var EmitterState;
(function (EmitterState) {
EmitterState[EmitterState["Initial"] = 0] = "Initial";
EmitterState[EmitterState["Error"] = 1] = "Error";
EmitterState[EmitterState["Wasm"] = 2] = "Wasm";
EmitterState[EmitterState["CustomSecton"] = 3] = "CustomSecton";
EmitterState[EmitterState["TypeSection"] = 4] = "TypeSection";
EmitterState[EmitterState["ImportSection"] = 5] = "ImportSection";
EmitterState[EmitterState["FunctionSection"] = 6] = "FunctionSection";
EmitterState[EmitterState["TableSection"] = 7] = "TableSection";
EmitterState[EmitterState["MemorySection"] = 8] = "MemorySection";
EmitterState[EmitterState["GlobalSection"] = 9] = "GlobalSection";
EmitterState[EmitterState["ExportSection"] = 10] = "ExportSection";
EmitterState[EmitterState["StartSection"] = 11] = "StartSection";
EmitterState[EmitterState["ElementSection"] = 12] = "ElementSection";
EmitterState[EmitterState["CodeSection"] = 13] = "CodeSection";
EmitterState[EmitterState["DataSection"] = 14] = "DataSection";
EmitterState[EmitterState["FunctionBody"] = 15] = "FunctionBody";
EmitterState[EmitterState["DataSectionEntry"] = 16] = "DataSectionEntry";
EmitterState[EmitterState["DataSectionEntryBody"] = 17] = "DataSectionEntryBody";
EmitterState[EmitterState["DataSectionEntryEnd"] = 18] = "DataSectionEntryEnd";
EmitterState[EmitterState["InitExpression"] = 19] = "InitExpression";
EmitterState[EmitterState["ElementSectionEntry"] = 20] = "ElementSectionEntry";
EmitterState[EmitterState["ElementSectionEntryBody"] = 21] = "ElementSectionEntryBody";
EmitterState[EmitterState["ElementSectionEntryEnd"] = 22] = "ElementSectionEntryEnd";
EmitterState[EmitterState["GlobalSectionEntry"] = 23] = "GlobalSectionEntry";
EmitterState[EmitterState["GlobalSectionEntryEnd"] = 24] = "GlobalSectionEntryEnd";
EmitterState[EmitterState["RawDataSection"] = 25] = "RawDataSection";
EmitterState[EmitterState["NameEntry"] = 26] = "NameEntry";
EmitterState[EmitterState["RelocHeader"] = 27] = "RelocHeader";
EmitterState[EmitterState["RelocEntry"] = 28] = "RelocEntry";
EmitterState[EmitterState["LinkingEntry"] = 29] = "LinkingEntry";
EmitterState[EmitterState["SourceMappingURL"] = 30] = "SourceMappingURL";
EmitterState[EmitterState["SourceMappingURLEnd"] = 31] = "SourceMappingURLEnd";
})(EmitterState || (EmitterState = {}));
var Emitter = (function () {
function Emitter() {
this._buffer = [];
this._state = EmitterState.Initial;
this._sectionStart = 0;
this._sectionSizeBytes = 0;
this._sectionEntiesCount = 0;
this._sectionEntiesCountBytes = 0;
this._bodyStart = 0;
this._bodySizeBytes = 0;
this._data = null;
this._endWritten = false;
this._initExpressionAfterState = EmitterState.Initial;
Object.defineProperty(Emitter.prototype, "data", {
get: function () {
return this._data;
},
enumerable: true,
configurable: true
});
Emitter.prototype.write = function (reader) {
this.writeStateAndResult(reader.state, reader.result);
};
Emitter.prototype.writeData = function (data) {
this.writeStateAndResult(data.state, data.result || null);
};
Emitter.prototype.writeStateAndResult = function (state, result) {
switch (state) {
case 1 /* BEGIN_WASM */:
this.writeBeginWasm(result);
break;
case 2 /* END_WASM */:
this.writeEndWasm();
break;
case 3 /* BEGIN_SECTION */:
this.writeBeginSection(result);
break;
case 4 /* END_SECTION */:
this.writeEndSection();
break;
case 11 /* TYPE_SECTION_ENTRY */:
this.writeTypeSectionEntry(result);
break;
case 12 /* IMPORT_SECTION_ENTRY */:
this.writeImportSectionEntry(result);
break;
case 13 /* FUNCTION_SECTION_ENTRY */:
this.writeFunctionSectionEntry(result);
break;
case 17 /* EXPORT_SECTION_ENTRY */:
this.writeExportSectionEntry(result);
break;
case 28 /* BEGIN_FUNCTION_BODY */:
this.writeBeginFunctionBody(result);
break;
case 31 /* END_FUNCTION_BODY */:
this.writeEndFunctionBody();
break;
case 15 /* MEMORY_SECTION_ENTRY */:
this.writeMemorySectionEntry(result);
break;
case 26 /* INIT_EXPRESSION_OPERATOR */:
case 30 /* CODE_OPERATOR */:
this.writeOperator(result);
break;
case 36 /* BEGIN_DATA_SECTION_ENTRY */:
this.writeBeginDataSectionEntry(result);
break;
case 37 /* DATA_SECTION_ENTRY_BODY */:
this.writeDataSectionBody(result);
break;
case 38 /* END_DATA_SECTION_ENTRY */:
this.writeEndDataSectionEntry();
break;
case 25 /* BEGIN_INIT_EXPRESSION_BODY */:
this.writeBeginInitExpression();
break;
case 27 /* END_INIT_EXPRESSION_BODY */:
this.writeEndInitExpression();
break;
case 14 /* TABLE_SECTION_ENTRY */:
this.writeTableSectionEntry(result);
break;
case 33 /* BEGIN_ELEMENT_SECTION_ENTRY */:
this.writeBeginElementSectionEntry(result);
break;
case 35 /* END_ELEMENT_SECTION_ENTRY */:
this.writeEndElementSectionEntry();
break;
case 34 /* ELEMENT_SECTION_ENTRY_BODY */:
this.writeElementSectionBody(result);
break;
case 39 /* BEGIN_GLOBAL_SECTION_ENTRY */:
this.writeBeginGlobalSectionEntry(result);
break;
case 40 /* END_GLOBAL_SECTION_ENTRY */:
this.writeEndGlobalSectionEntry();
break;
case 7 /* SECTION_RAW_DATA */:
this.writeSectionRawData(result);
break;
case 19 /* NAME_SECTION_ENTRY */:
this.writeNameEntry(result);
break;
case 41 /* RELOC_SECTION_HEADER */:
this.writeRelocHeader(result);
break;
case 42 /* RELOC_SECTION_ENTRY */:
this.writeRelocEntry(result);
break;
case 21 /* LINKING_SECTION_ENTRY */:
this.writeLinkingSection(result);
break;
case 43 /* SOURCE_MAPPING_URL */:
this.writeSourceMappingURL(result);
break;
default:
throw new Error("Invalid state: " + state);
}
Object.defineProperty(Emitter.prototype, "data", {
get: function () {
return this._data;
},
enumerable: true,
configurable: true
});
Emitter.prototype.write = function (reader) {
this.writeStateAndResult(reader.state, reader.result);
};
Emitter.prototype.writeData = function (data) {
this.writeStateAndResult(data.state, data.result || null);
};
Emitter.prototype.writeStateAndResult = function (state, result) {
switch (state) {
case 1 /* BEGIN_WASM */:
this.writeBeginWasm(result);
break;
case 2 /* END_WASM */:
this.writeEndWasm();
break;
case 3 /* BEGIN_SECTION */:
this.writeBeginSection(result);
break;
case 4 /* END_SECTION */:
this.writeEndSection();
break;
case 11 /* TYPE_SECTION_ENTRY */:
this.writeTypeSectionEntry(result);
break;
case 12 /* IMPORT_SECTION_ENTRY */:
this.writeImportSectionEntry(result);
break;
case 13 /* FUNCTION_SECTION_ENTRY */:
this.writeFunctionSectionEntry(result);
break;
case 17 /* EXPORT_SECTION_ENTRY */:
this.writeExportSectionEntry(result);
break;
case 28 /* BEGIN_FUNCTION_BODY */:
this.writeBeginFunctionBody(result);
break;
case 31 /* END_FUNCTION_BODY */:
this.writeEndFunctionBody();
break;
case 15 /* MEMORY_SECTION_ENTRY */:
this.writeMemorySectionEntry(result);
break;
case 26 /* INIT_EXPRESSION_OPERATOR */:
case 30 /* CODE_OPERATOR */:
this.writeOperator(result);
break;
case 36 /* BEGIN_DATA_SECTION_ENTRY */:
this.writeBeginDataSectionEntry(result);
break;
case 37 /* DATA_SECTION_ENTRY_BODY */:
this.writeDataSectionBody(result);
break;
case 38 /* END_DATA_SECTION_ENTRY */:
this.writeEndDataSectionEntry();
break;
case 25 /* BEGIN_INIT_EXPRESSION_BODY */:
this.writeBeginInitExpression();
break;
case 27 /* END_INIT_EXPRESSION_BODY */:
this.writeEndInitExpression();
break;
case 14 /* TABLE_SECTION_ENTRY */:
this.writeTableSectionEntry(result);
break;
case 33 /* BEGIN_ELEMENT_SECTION_ENTRY */:
this.writeBeginElementSectionEntry(result);
break;
case 35 /* END_ELEMENT_SECTION_ENTRY */:
this.writeEndElementSectionEntry();
break;
case 34 /* ELEMENT_SECTION_ENTRY_BODY */:
this.writeElementSectionBody(result);
break;
case 39 /* BEGIN_GLOBAL_SECTION_ENTRY */:
this.writeBeginGlobalSectionEntry(result);
break;
case 40 /* END_GLOBAL_SECTION_ENTRY */:
this.writeEndGlobalSectionEntry();
break;
case 7 /* SECTION_RAW_DATA */:
this.writeSectionRawData(result);
break;
case 19 /* NAME_SECTION_ENTRY */:
this.writeNameEntry(result);
break;
case 41 /* RELOC_SECTION_HEADER */:
this.writeRelocHeader(result);
break;
case 42 /* RELOC_SECTION_ENTRY */:
this.writeRelocEntry(result);
break;
case 21 /* LINKING_SECTION_ENTRY */:
this.writeLinkingSection(result);
break;
case 43 /* SOURCE_MAPPING_URL */:
this.writeSourceMappingURL(result);
break;
default:
throw new Error("Invalid state: " + state);
}
};
Emitter.prototype.writeByte = function (byte) {
this._buffer.push(byte);
};
Emitter.prototype.writeMutiple = function () {
var bytes = [];
for (var _i = 0; _i < arguments.length; _i++) {
bytes[_i] = arguments[_i];
}
(_a = this._buffer).push.apply(_a, bytes);
var _a;
};
Object.defineProperty(Emitter.prototype, "_position", {
get: function () {
return this._buffer.length;
},
enumerable: true,
configurable: true
});
Emitter.prototype.patchByte = function (pos, byte) {
this._buffer[pos] = byte;
};
Emitter.prototype.writeVarUint = function (n) {
while ((n & ~0x7F)) {
this.writeByte(0x80 | (n & 0x7f));
n >>>= 7;
}
this.writeByte(n);
};
Emitter.prototype.writeVarInt = function (n) {
n |= 0;
var test = n >> 31;
while ((n >> 6) != test) {
this.writeByte(0x80 | (n & 0x7f));
n >>= 7;
}
this.writeByte(n & 0x7f);
};
Emitter.prototype.writePatchableVarUint32 = function () {
var pos = this._position;
this.writeMutiple(0x80, 0x80, 0x80, 0x80, 0x00);
return pos;
};
Emitter.prototype.writePatchableSectionEntriesCount = function () {
this._sectionEntiesCountBytes = this.writePatchableVarUint32();
this._sectionEntiesCount = 0;
};
Emitter.prototype.writeBytes = function (bytes, start, end) {
for (var i = start; i < end; i++)
this.writeByte(bytes[i]);
};
Emitter.prototype.writeString = function (str) {
this.writeVarUint(str.length);
this.writeBytes(str, 0, str.length);
};
Emitter.prototype.patchVarUint32 = function (pos, n) {
this.patchByte(pos, 0x80 | (n & 0x7F));
this.patchByte(pos + 1, 0x80 | ((n >>> 7) & 0x7F));
this.patchByte(pos + 2, 0x80 | ((n >>> 14) & 0x7F));
this.patchByte(pos + 3, 0x80 | ((n >>> 21) & 0x7F));
this.patchByte(pos + 4, ((n >>> 28) & 0x7F));
};
Emitter.prototype.ensureState = function (state) {
if (this._state !== state)
throw new Error("Unexpected state: " + this._state + " (expected " + state + ").");
};
Emitter.prototype.ensureEitherState = function (states) {
if (states.indexOf(this._state) < 0)
throw new Error("Unexpected state: " + this._state + " (expected one of " + states + ").");
};
Emitter.prototype.ensureEndOperatorWritten = function () {
if (!this._endWritten)
throw new Error('End as a last written operator is expected.');
};
Emitter.prototype.writeBeginWasm = function (header) {
this.ensureState(EmitterState.Initial);
this.writeMutiple(0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00);
this._state = EmitterState.Wasm;
};
Emitter.prototype.writeEndWasm = function () {
this.ensureState(EmitterState.Wasm);
this._state = EmitterState.Initial;
this._data = new Uint8Array(this._buffer);
this._buffer.length = 0;
};
Emitter.prototype.writeBeginSection = function (section) {
this.ensureState(EmitterState.Wasm);
this.writeVarUint(section.id);
this._sectionSizeBytes = this.writePatchableVarUint32();
this._sectionStart = this._position;
switch (section.id) {
case 0 /* Custom */:
this.writeString(section.name);
var sectionName = WasmParser_1.bytesToString(section.name);
if (sectionName === 'name') {
this._state = EmitterState.NameEntry;
break;
}
if (sectionName.indexOf('reloc.') === 0) {
this._state = EmitterState.RelocHeader;
break;
}
if (sectionName === 'linking') {
this._state = EmitterState.LinkingEntry;
break;
}
if (sectionName === 'sourceMappingURL') {
this._state = EmitterState.SourceMappingURL;
break;
}
this._state = EmitterState.RawDataSection;
break;
default:
this._state = EmitterState.Error;
throw new Error("Unexpected section " + section.id);
case 1 /* Type */:
this._state = EmitterState.TypeSection;
this.writePatchableSectionEntriesCount();
break;
case 2 /* Import */:
this._state = EmitterState.ImportSection;
this.writePatchableSectionEntriesCount();
break;
case 3 /* Function */:
this._state = EmitterState.FunctionSection;
this.writePatchableSectionEntriesCount();
break;
case 7 /* Export */:
this._state = EmitterState.ExportSection;
this.writePatchableSectionEntriesCount();
break;
case 10 /* Code */:
this._state = EmitterState.CodeSection;
this.writePatchableSectionEntriesCount();
break;
case 5 /* Memory */:
this._state = EmitterState.MemorySection;
this.writePatchableSectionEntriesCount();
break;
case 6 /* Global */:
this._state = EmitterState.GlobalSection;
this.writePatchableSectionEntriesCount();
break;
case 11 /* Data */:
this._state = EmitterState.DataSection;
this.writePatchableSectionEntriesCount();
break;
case 4 /* Table */:
this._state = EmitterState.TableSection;
this.writePatchableSectionEntriesCount();
break;
case 9 /* Element */:
this._state = EmitterState.ElementSection;
this.writePatchableSectionEntriesCount();
break;
case 6 /* Global */:
this._state = EmitterState.GlobalSection;
this.writePatchableSectionEntriesCount();
break;
}
};
Emitter.prototype.writeBeginSectionRawData = function (section) {
this.ensureState(EmitterState.Wasm);
this.writeVarUint(section.id);
if (section.id == 0 /* Custom */) {
};
Emitter.prototype.writeByte = function (byte) {
this._buffer.push(byte);
};
Emitter.prototype.writeMutiple = function () {
var bytes = [];
for (var _i = 0; _i < arguments.length; _i++) {
bytes[_i] = arguments[_i];
}
(_a = this._buffer).push.apply(_a, bytes);
var _a;
};
Object.defineProperty(Emitter.prototype, "_position", {
get: function () {
return this._buffer.length;
},
enumerable: true,
configurable: true
});
Emitter.prototype.patchByte = function (pos, byte) {
this._buffer[pos] = byte;
};
Emitter.prototype.writeVarUint = function (n) {
while ((n & ~0x7F)) {
this.writeByte(0x80 | (n & 0x7f));
n >>>= 7;
}
this.writeByte(n);
};
Emitter.prototype.writeVarInt = function (n) {
n |= 0;
var test = n >> 31;
while ((n >> 6) != test) {
this.writeByte(0x80 | (n & 0x7f));
n >>= 7;
}
this.writeByte(n & 0x7f);
};
Emitter.prototype.writePatchableVarUint32 = function () {
var pos = this._position;
this.writeMutiple(0x80, 0x80, 0x80, 0x80, 0x00);
return pos;
};
Emitter.prototype.writePatchableSectionEntriesCount = function () {
this._sectionEntiesCountBytes = this.writePatchableVarUint32();
this._sectionEntiesCount = 0;
};
Emitter.prototype.writeBytes = function (bytes, start, end) {
for (var i = start; i < end; i++)
this.writeByte(bytes[i]);
};
Emitter.prototype.writeString = function (str) {
this.writeVarUint(str.length);
this.writeBytes(str, 0, str.length);
};
Emitter.prototype.patchVarUint32 = function (pos, n) {
this.patchByte(pos, 0x80 | (n & 0x7F));
this.patchByte(pos + 1, 0x80 | ((n >>> 7) & 0x7F));
this.patchByte(pos + 2, 0x80 | ((n >>> 14) & 0x7F));
this.patchByte(pos + 3, 0x80 | ((n >>> 21) & 0x7F));
this.patchByte(pos + 4, ((n >>> 28) & 0x7F));
};
Emitter.prototype.ensureState = function (state) {
if (this._state !== state)
throw new Error("Unexpected state: " + this._state + " (expected " + state + ").");
};
Emitter.prototype.ensureEitherState = function (states) {
if (states.indexOf(this._state) < 0)
throw new Error("Unexpected state: " + this._state + " (expected one of " + states + ").");
};
Emitter.prototype.ensureEndOperatorWritten = function () {
if (!this._endWritten)
throw new Error('End as a last written operator is expected.');
};
Emitter.prototype.writeBeginWasm = function (header) {
this.ensureState(EmitterState.Initial);
this.writeMutiple(0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00);
this._state = EmitterState.Wasm;
};
Emitter.prototype.writeEndWasm = function () {
this.ensureState(EmitterState.Wasm);
this._state = EmitterState.Initial;
this._data = new Uint8Array(this._buffer);
this._buffer.length = 0;
};
Emitter.prototype.writeBeginSection = function (section) {
this.ensureState(EmitterState.Wasm);
this.writeVarUint(section.id);
this._sectionSizeBytes = this.writePatchableVarUint32();
this._sectionStart = this._position;
switch (section.id) {
case 0 /* Custom */:
this.writeString(section.name);
}
this._state = EmitterState.RawDataSection;
};
Emitter.prototype.writeSectionRawData = function (bytes) {
this.ensureState(EmitterState.RawDataSection);
this.writeBytes(bytes, 0, bytes.length);
};
Emitter.prototype.writeFuncType = function (type) {
this.writeVarInt(type.form);
this.writeVarUint(type.params.length);
for (var i = 0; i < type.params.length; i++)
this.writeVarInt(type.params[i]);
this.writeVarUint(type.returns.length);
for (var i = 0; i < type.returns.length; i++)
this.writeVarInt(type.returns[i]);
};
Emitter.prototype.writeTypeSectionEntry = function (type) {
this.ensureState(EmitterState.TypeSection);
this._sectionEntiesCount++;
this.writeFuncType(type);
};
Emitter.prototype.writeResizableLimits = function (limits) {
var flags = limits.maximum == undefined ? 0 : 1;
this.writeVarUint(flags);
this.writeVarUint(limits.initial);
if (flags)
this.writeVarUint(limits.maximum);
};
Emitter.prototype.writeTableType = function (type) {
this.writeVarInt(type.elementType);
this.writeResizableLimits(type.limits);
};
Emitter.prototype.writeMemoryType = function (type) {
this.writeResizableLimits(type.limits);
};
Emitter.prototype.writeGlobalType = function (type) {
this.writeVarInt(type.contentType);
this.writeVarUint(type.mutability);
};
Emitter.prototype.writeImportSectionEntry = function (entry) {
this.ensureState(EmitterState.ImportSection);
this._sectionEntiesCount++;
this.writeString(entry.module);
this.writeString(entry.field);
this.writeByte(entry.kind);
switch (entry.kind) {
case 0 /* Function */:
this.writeVarUint(entry.funcTypeIndex);
var sectionName = WasmParser_1.bytesToString(section.name);
if (sectionName === 'name') {
this._state = EmitterState.NameEntry;
break;
case 1 /* Table */:
this.writeTableType(entry.type);
}
if (sectionName.indexOf('reloc.') === 0) {
this._state = EmitterState.RelocHeader;
break;
case 2 /* Memory */:
this.writeMemoryType(entry.type);
}
if (sectionName === 'linking') {
this._state = EmitterState.LinkingEntry;
break;
case 3 /* Global */:
this.writeGlobalType(entry.type);
}
if (sectionName === 'sourceMappingURL') {
this._state = EmitterState.SourceMappingURL;
break;
default:
throw new Error("Invalid import kind: " + entry.kind);
}
this._state = EmitterState.RawDataSection;
break;
default:
this._state = EmitterState.Error;
throw new Error("Unexpected section " + section.id);
case 1 /* Type */:
this._state = EmitterState.TypeSection;
this.writePatchableSectionEntriesCount();
break;
case 2 /* Import */:
this._state = EmitterState.ImportSection;
this.writePatchableSectionEntriesCount();
break;
case 3 /* Function */:
this._state = EmitterState.FunctionSection;
this.writePatchableSectionEntriesCount();
break;
case 7 /* Export */:
this._state = EmitterState.ExportSection;
this.writePatchableSectionEntriesCount();
break;
case 10 /* Code */:
this._state = EmitterState.CodeSection;
this.writePatchableSectionEntriesCount();
break;
case 5 /* Memory */:
this._state = EmitterState.MemorySection;
this.writePatchableSectionEntriesCount();
break;
case 6 /* Global */:
this._state = EmitterState.GlobalSection;
this.writePatchableSectionEntriesCount();
break;
case 11 /* Data */:
this._state = EmitterState.DataSection;
this.writePatchableSectionEntriesCount();
break;
case 4 /* Table */:
this._state = EmitterState.TableSection;
this.writePatchableSectionEntriesCount();
break;
case 9 /* Element */:
this._state = EmitterState.ElementSection;
this.writePatchableSectionEntriesCount();
break;
case 6 /* Global */:
this._state = EmitterState.GlobalSection;
this.writePatchableSectionEntriesCount();
break;
}
};
Emitter.prototype.writeBeginSectionRawData = function (section) {
this.ensureState(EmitterState.Wasm);
this.writeVarUint(section.id);
if (section.id == 0 /* Custom */) {
this.writeString(section.name);
}
this._state = EmitterState.RawDataSection;
};
Emitter.prototype.writeSectionRawData = function (bytes) {
this.ensureState(EmitterState.RawDataSection);
this.writeBytes(bytes, 0, bytes.length);
};
Emitter.prototype.writeFuncType = function (type) {
this.writeVarInt(type.form);
this.writeVarUint(type.params.length);
for (var i = 0; i < type.params.length; i++)
this.writeVarInt(type.params[i]);
this.writeVarUint(type.returns.length);
for (var i = 0; i < type.returns.length; i++)
this.writeVarInt(type.returns[i]);
};
Emitter.prototype.writeTypeSectionEntry = function (type) {
this.ensureState(EmitterState.TypeSection);
this._sectionEntiesCount++;
this.writeFuncType(type);
};
Emitter.prototype.writeResizableLimits = function (limits) {
var flags = limits.maximum == undefined ? 0 : 1;
this.writeVarUint(flags);
this.writeVarUint(limits.initial);
if (flags)
this.writeVarUint(limits.maximum);
};
Emitter.prototype.writeTableType = function (type) {
this.writeVarInt(type.elementType);
this.writeResizableLimits(type.limits);
};
Emitter.prototype.writeMemoryType = function (type) {
this.writeResizableLimits(type.limits);
};
Emitter.prototype.writeGlobalType = function (type) {
this.writeVarInt(type.contentType);
this.writeVarUint(type.mutability);
};
Emitter.prototype.writeImportSectionEntry = function (entry) {
this.ensureState(EmitterState.ImportSection);
this._sectionEntiesCount++;
this.writeString(entry.module);
this.writeString(entry.field);
this.writeByte(entry.kind);
switch (entry.kind) {
case 0 /* Function */:
this.writeVarUint(entry.funcTypeIndex);
break;
case 1 /* Table */:
this.writeTableType(entry.type);
break;
case 2 /* Memory */:
this.writeMemoryType(entry.type);
break;
case 3 /* Global */:
this.writeGlobalType(entry.type);
break;
default:
throw new Error("Invalid import kind: " + entry.kind);
}
};
Emitter.prototype.writeFunctionSectionEntry = function (entry) {
this.ensureState(EmitterState.FunctionSection);
this._sectionEntiesCount++;
this.writeVarUint(entry.typeIndex);
};
Emitter.prototype.writeExportSectionEntry = function (entry) {
this.ensureState(EmitterState.ExportSection);
this._sectionEntiesCount++;
this.writeString(entry.field);
this.writeByte(entry.kind);
this.writeVarUint(entry.index);
};
Emitter.prototype.writeBeginFunctionBody = function (functionInfo) {
this.ensureState(EmitterState.CodeSection);
this._sectionEntiesCount++;
this._bodySizeBytes = this.writePatchableVarUint32();
this._bodyStart = this._position;
this._endWritten = false;
this._state = EmitterState.FunctionBody;
this.writeVarUint(functionInfo.locals.length);
for (var i = 0; i < functionInfo.locals.length; i++) {
this.writeVarUint(functionInfo.locals[i].count);
this.writeVarInt(functionInfo.locals[i].type);
}
};
Emitter.prototype.writeEndFunctionBody = function () {
this.ensureState(EmitterState.FunctionBody);
this.ensureEndOperatorWritten();
var bodySize = this._position - this._bodyStart;
this.patchVarUint32(this._bodySizeBytes, bodySize);
this._state = EmitterState.CodeSection;
};
Emitter.prototype.writeBeginDataSectionEntry = function (entry) {
this.ensureState(EmitterState.DataSection);
this._sectionEntiesCount++;
this.writeVarUint(entry.index);
this._state = EmitterState.DataSectionEntry;
};
Emitter.prototype.writeDataSectionBody = function (body) {
this.ensureState(EmitterState.DataSectionEntryBody);
this.writeString(body.data);
this._state = EmitterState.DataSectionEntryEnd;
};
Emitter.prototype.writeEndDataSectionEntry = function () {
this.ensureState(EmitterState.DataSectionEntryEnd);
this._state = EmitterState.DataSection;
};
Emitter.prototype.writeTableSectionEntry = function (entry) {
this.ensureState(EmitterState.TableSection);
this._sectionEntiesCount++;
this.writeVarInt(entry.elementType);
this.writeResizableLimits(entry.limits);
};
Emitter.prototype.writeBeginElementSectionEntry = function (entry) {
this.ensureState(EmitterState.ElementSection);
this._sectionEntiesCount++;
this.writeVarUint(entry.index);
this._state = EmitterState.ElementSectionEntry;
};
Emitter.prototype.writeElementSectionBody = function (body) {
this.ensureState(EmitterState.ElementSectionEntryBody);
this.writeVarUint(body.elements.length);
for (var i = 0; i < body.elements.length; i++)
this.writeVarUint(body.elements[i]);
this._state = EmitterState.ElementSectionEntryEnd;
};
Emitter.prototype.writeEndElementSectionEntry = function () {
this.ensureState(EmitterState.ElementSectionEntryEnd);
this._state = EmitterState.ElementSection;
};
Emitter.prototype.writeBeginGlobalSectionEntry = function (entry) {
this.ensureState(EmitterState.GlobalSection);
this._sectionEntiesCount++;
this.writeGlobalType(entry.type);
this._state = EmitterState.GlobalSectionEntry;
};
Emitter.prototype.writeEndGlobalSectionEntry = function () {
this.ensureState(EmitterState.GlobalSectionEntryEnd);
this._state = EmitterState.GlobalSection;
};
Emitter.prototype.writeBeginInitExpression = function () {
switch (this._state) {
case EmitterState.DataSectionEntry:
this._initExpressionAfterState = EmitterState.DataSectionEntryBody;
break;
case EmitterState.ElementSectionEntry:
this._initExpressionAfterState = EmitterState.ElementSectionEntryBody;
break;
case EmitterState.GlobalSectionEntry:
this._initExpressionAfterState = EmitterState.GlobalSectionEntryEnd;
break;
default:
throw new Error("Unexpected state " + this._state + " at writeEndInitExpression");
}
this._endWritten = false;
this._state = EmitterState.InitExpression;
};
Emitter.prototype.writeEndInitExpression = function () {
this.ensureState(EmitterState.InitExpression);
this.ensureEndOperatorWritten();
this._state = this._initExpressionAfterState;
};
Emitter.prototype.writeMemoryImmediate = function (address) {
this.writeVarUint(address.flags);
this.writeVarUint(address.offset);
};
Emitter.prototype.writeVarInt64 = function (n) {
var pos = 0, end = 7;
var highBit = n.data[end] & 0x80;
var optionalBits = highBit ? 0xFF : 0;
while (end > 0 && n.data[end] === optionalBits) {
end--;
}
var buffer = n.data[pos], buffered = 8;
do {
this.writeByte(0x80 | (buffer & 0x7F));
buffer >>= 7;
buffered -= 7;
if (buffered > 7)
continue;
if (pos < end) {
++pos;
buffer |= n.data[pos] << buffered;
buffered += 8;
}
};
Emitter.prototype.writeFunctionSectionEntry = function (entry) {
this.ensureState(EmitterState.FunctionSection);
this._sectionEntiesCount++;
this.writeVarUint(entry.typeIndex);
};
Emitter.prototype.writeExportSectionEntry = function (entry) {
this.ensureState(EmitterState.ExportSection);
this._sectionEntiesCount++;
this.writeString(entry.field);
this.writeByte(entry.kind);
this.writeVarUint(entry.index);
};
Emitter.prototype.writeBeginFunctionBody = function (functionInfo) {
this.ensureState(EmitterState.CodeSection);
this._sectionEntiesCount++;
this._bodySizeBytes = this.writePatchableVarUint32();
this._bodyStart = this._position;
this._endWritten = false;
this._state = EmitterState.FunctionBody;
this.writeVarUint(functionInfo.locals.length);
for (var i = 0; i < functionInfo.locals.length; i++) {
this.writeVarUint(functionInfo.locals[i].count);
this.writeVarInt(functionInfo.locals[i].type);
else if (pos == end && buffer === 7 &&
(n.data[pos] & 0x80) !== highBit) {
++pos;
buffer |= optionalBits << buffered;
buffered += 8;
}
};
Emitter.prototype.writeEndFunctionBody = function () {
this.ensureState(EmitterState.FunctionBody);
this.ensureEndOperatorWritten();
var bodySize = this._position - this._bodyStart;
this.patchVarUint32(this._bodySizeBytes, bodySize);
this._state = EmitterState.CodeSection;
};
Emitter.prototype.writeBeginDataSectionEntry = function (entry) {
this.ensureState(EmitterState.DataSection);
this._sectionEntiesCount++;
this.writeVarUint(entry.index);
this._state = EmitterState.DataSectionEntry;
};
Emitter.prototype.writeDataSectionBody = function (body) {
this.ensureState(EmitterState.DataSectionEntryBody);
this.writeString(body.data);
this._state = EmitterState.DataSectionEntryEnd;
};
Emitter.prototype.writeEndDataSectionEntry = function () {
this.ensureState(EmitterState.DataSectionEntryEnd);
this._state = EmitterState.DataSection;
};
Emitter.prototype.writeTableSectionEntry = function (entry) {
this.ensureState(EmitterState.TableSection);
this._sectionEntiesCount++;
this.writeVarInt(entry.elementType);
this.writeResizableLimits(entry.limits);
};
Emitter.prototype.writeBeginElementSectionEntry = function (entry) {
this.ensureState(EmitterState.ElementSection);
this._sectionEntiesCount++;
this.writeVarUint(entry.index);
this._state = EmitterState.ElementSectionEntry;
};
Emitter.prototype.writeElementSectionBody = function (body) {
this.ensureState(EmitterState.ElementSectionEntryBody);
this.writeVarUint(body.elements.length);
for (var i = 0; i < body.elements.length; i++)
this.writeVarUint(body.elements[i]);
this._state = EmitterState.ElementSectionEntryEnd;
};
Emitter.prototype.writeEndElementSectionEntry = function () {
this.ensureState(EmitterState.ElementSectionEntryEnd);
this._state = EmitterState.ElementSection;
};
Emitter.prototype.writeBeginGlobalSectionEntry = function (entry) {
this.ensureState(EmitterState.GlobalSection);
this._sectionEntiesCount++;
this.writeGlobalType(entry.type);
this._state = EmitterState.GlobalSectionEntry;
};
Emitter.prototype.writeEndGlobalSectionEntry = function () {
this.ensureState(EmitterState.GlobalSectionEntryEnd);
this._state = EmitterState.GlobalSection;
};
Emitter.prototype.writeBeginInitExpression = function () {
switch (this._state) {
case EmitterState.DataSectionEntry:
this._initExpressionAfterState = EmitterState.DataSectionEntryBody;
break;
case EmitterState.ElementSectionEntry:
this._initExpressionAfterState = EmitterState.ElementSectionEntryBody;
break;
case EmitterState.GlobalSectionEntry:
this._initExpressionAfterState = EmitterState.GlobalSectionEntryEnd;
break;
default:
throw new Error("Unexpected state " + this._state + " at writeEndInitExpression");
}
this._endWritten = false;
this._state = EmitterState.InitExpression;
};
Emitter.prototype.writeEndInitExpression = function () {
this.ensureState(EmitterState.InitExpression);
this.ensureEndOperatorWritten();
this._state = this._initExpressionAfterState;
};
Emitter.prototype.writeMemoryImmediate = function (address) {
this.writeVarUint(address.flags);
this.writeVarUint(address.offset);
};
Emitter.prototype.writeVarInt64 = function (n) {
var pos = 0, end = 7;
var highBit = n.data[end] & 0x80;
var optionalBits = highBit ? 0xFF : 0;
while (end > 0 && n.data[end] === optionalBits) {
end--;
}
var buffer = n.data[pos], buffered = 8;
do {
this.writeByte(0x80 | (buffer & 0x7F));
buffer >>= 7;
buffered -= 7;
if (buffered > 7)
continue;
if (pos < end) {
++pos;
buffer |= n.data[pos] << buffered;
buffered += 8;
} while (buffered > 7);
buffer |= optionalBits << buffered;
this.writeByte(buffer & 0x7f);
};
Emitter.prototype.writeFloat32 = function (n) {
var data = new Uint8Array(4);
new DataView(data.buffer, 0).setFloat32(0, n, true);
this.writeBytes(data, 0, data.length);
};
Emitter.prototype.writeFloat64 = function (n) {
var data = new Uint8Array(8);
new DataView(data.buffer, 0).setFloat64(0, n, true);
this.writeBytes(data, 0, data.length);
};
Emitter.prototype.writeOperator = function (opInfo) {
this.ensureEitherState([EmitterState.FunctionBody, EmitterState.InitExpression]);
this.writeByte(opInfo.code);
this._endWritten = opInfo.code == 11 /* end */;
switch (opInfo.code) {
case 2 /* block */:
case 3 /* loop */:
case 4 /* if */:
this.writeVarInt(opInfo.blockType);
break;
case 12 /* br */:
case 13 /* br_if */:
this.writeVarUint(opInfo.brDepth);
break;
case 14 /* br_table */:
var tableCount = opInfo.brTable.length - 1;
this.writeVarUint(tableCount);
for (var i = 0; i <= tableCount; i++) {
this.writeVarUint(opInfo.brTable[i]);
}
else if (pos == end && buffer === 7 &&
(n.data[pos] & 0x80) !== highBit) {
++pos;
buffer |= optionalBits << buffered;
buffered += 8;
}
} while (buffered > 7);
buffer |= optionalBits << buffered;
this.writeByte(buffer & 0x7f);
};
Emitter.prototype.writeFloat32 = function (n) {
var data = new Uint8Array(4);
new DataView(data.buffer, 0).setFloat32(0, n, true);
this.writeBytes(data, 0, data.length);
};
Emitter.prototype.writeFloat64 = function (n) {
var data = new Uint8Array(8);
new DataView(data.buffer, 0).setFloat64(0, n, true);
this.writeBytes(data, 0, data.length);
};
Emitter.prototype.writeOperator = function (opInfo) {
this.ensureEitherState([EmitterState.FunctionBody, EmitterState.InitExpression]);
this.writeByte(opInfo.code);
this._endWritten = opInfo.code == 11 /* end */;
switch (opInfo.code) {
case 2 /* block */:
case 3 /* loop */:
case 4 /* if */:
this.writeVarInt(opInfo.blockType);
break;
case 12 /* br */:
case 13 /* br_if */:
this.writeVarUint(opInfo.brDepth);
break;
case 14 /* br_table */:
var tableCount = opInfo.brTable.length - 1;
this.writeVarUint(tableCount);
for (var i = 0; i <= tableCount; i++) {
this.writeVarUint(opInfo.brTable[i]);
}
break;
case 16 /* call */:
this.writeVarUint(opInfo.funcIndex);
break;
case 17 /* call_indirect */:
this.writeVarUint(opInfo.typeIndex);
this.writeVarUint(0);
break;
case 32 /* get_local */:
case 33 /* set_local */:
case 34 /* tee_local */:
this.writeVarUint(opInfo.localIndex);
break;
case 35 /* get_global */:
case 36 /* set_global */:
this.writeVarUint(opInfo.globalIndex);
break;
case 40 /* i32_load */:
case 41 /* i64_load */:
case 42 /* f32_load */:
case 43 /* f64_load */:
case 44 /* i32_load8_s */:
case 45 /* i32_load8_u */:
case 46 /* i32_load16_s */:
case 47 /* i32_load16_u */:
case 48 /* i64_load8_s */:
case 49 /* i64_load8_u */:
case 50 /* i64_load16_s */:
case 51 /* i64_load16_u */:
case 52 /* i64_load32_s */:
case 53 /* i64_load32_u */:
case 54 /* i32_store */:
case 55 /* i64_store */:
case 56 /* f32_store */:
case 57 /* f64_store */:
case 58 /* i32_store8 */:
case 59 /* i32_store16 */:
case 60 /* i64_store8 */:
case 61 /* i64_store16 */:
case 62 /* i64_store32 */:
this.writeMemoryImmediate(opInfo.memoryAddress);
break;
case 63 /* current_memory */:
case 64 /* grow_memory */:
this.writeVarUint(0);
break;
case 65 /* i32_const */:
this.writeVarInt(opInfo.literal | 0);
break;
case 66 /* i64_const */:
this.writeVarInt64(opInfo.literal);
break;
case 67 /* f32_const */:
this.writeFloat32(opInfo.literal);
break;
case 68 /* f64_const */:
this.writeFloat64(opInfo.literal);
break;
}
};
Emitter.prototype.writeMemorySectionEntry = function (entry) {
this.ensureState(EmitterState.MemorySection);
this._sectionEntiesCount++;
this.writeMemoryType(entry);
};
Emitter.prototype.writeNameMap = function (map) {
var _this = this;
this.writeVarUint(map.length);
map.forEach(function (naming) {
_this.writeVarUint(naming.index);
_this.writeString(naming.name);
});
};
Emitter.prototype.writeNameEntry = function (entry) {
var _this = this;
this.ensureState(EmitterState.NameEntry);
this.writeVarUint(entry.type);
var payloadLengthPatchable = this.writePatchableVarUint32();
var start = this._position;
switch (entry.type) {
case 1 /* Function */:
this.writeNameMap(entry.names);
break;
case 2 /* Local */:
var funcs = entry.funcs;
this.writeVarUint(funcs.length);
funcs.forEach(function (func) {
_this.writeVarUint(func.index);
_this.writeNameMap(func.locals);
});
break;
default:
throw new Error("Unexpected name entry type " + entry.type);
}
this.patchVarUint32(payloadLengthPatchable, this._position - start);
};
Emitter.prototype.writeRelocHeader = function (header) {
this.ensureState(EmitterState.RelocHeader);
this.writeVarInt(header.id);
if (header.id == 0 /* Custom */) {
this.writeString(header.name);
}
this.writePatchableSectionEntriesCount();
this._state = EmitterState.RelocEntry;
};
Emitter.prototype.writeRelocEntry = function (entry) {
this.ensureState(EmitterState.RelocEntry);
this._sectionEntiesCount++;
this.writeVarUint(entry.type);
this.writeVarUint(entry.offset);
this.writeVarUint(entry.index);
switch (entry.type) {
case 0 /* FunctionIndex_LEB */:
case 1 /* TableIndex_SLEB */:
case 2 /* TableIndex_I32 */:
break;
case 3 /* GlobalAddr_LEB */:
case 4 /* GlobalAddr_SLEB */:
case 5 /* GlobalAddr_I32 */:
this.writeVarUint(entry.addend);
break;
default:
throw new Error("Unexpected reloc entry type " + entry.type);
}
};
Emitter.prototype.writeLinkingSection = function (entry) {
this.ensureState(EmitterState.LinkingEntry);
this._sectionEntiesCount++;
this.writeVarUint(entry.type);
switch (entry.type) {
case 1 /* StackPointer */:
this.writeVarUint(entry.index);
break;
default:
throw new Error("Unexpected linking entry type " + entry.type);
}
};
Emitter.prototype.writeSourceMappingURL = function (url) {
this.ensureState(EmitterState.SourceMappingURL);
this.writeString(url.url);
this._state = EmitterState.SourceMappingURLEnd;
};
Emitter.prototype.writeEndSection = function () {
switch (this._state) {
case EmitterState.TypeSection:
case EmitterState.ImportSection:
case EmitterState.FunctionSection:
case EmitterState.ExportSection:
case EmitterState.CodeSection:
case EmitterState.MemorySection:
case EmitterState.GlobalSection:
case EmitterState.DataSection:
case EmitterState.TableSection:
case EmitterState.ElementSection:
case EmitterState.RelocEntry:
case EmitterState.LinkingEntry:
this.patchVarUint32(this._sectionEntiesCountBytes, this._sectionEntiesCount);
break;
case EmitterState.NameEntry:
case EmitterState.SourceMappingURLEnd:
case EmitterState.RawDataSection:
break;
default:
throw new Error("Unexpected state: " + this._state + " (expected section state)");
}
var sectionLength = this._position - this._sectionStart;
this.patchVarUint32(this._sectionSizeBytes, sectionLength);
this._state = EmitterState.Wasm;
};
return Emitter;
}());
exports.Emitter = Emitter;
});
break;
case 16 /* call */:
this.writeVarUint(opInfo.funcIndex);
break;
case 17 /* call_indirect */:
this.writeVarUint(opInfo.typeIndex);
this.writeVarUint(0);
break;
case 32 /* get_local */:
case 33 /* set_local */:
case 34 /* tee_local */:
this.writeVarUint(opInfo.localIndex);
break;
case 35 /* get_global */:
case 36 /* set_global */:
this.writeVarUint(opInfo.globalIndex);
break;
case 40 /* i32_load */:
case 41 /* i64_load */:
case 42 /* f32_load */:
case 43 /* f64_load */:
case 44 /* i32_load8_s */:
case 45 /* i32_load8_u */:
case 46 /* i32_load16_s */:
case 47 /* i32_load16_u */:
case 48 /* i64_load8_s */:
case 49 /* i64_load8_u */:
case 50 /* i64_load16_s */:
case 51 /* i64_load16_u */:
case 52 /* i64_load32_s */:
case 53 /* i64_load32_u */:
case 54 /* i32_store */:
case 55 /* i64_store */:
case 56 /* f32_store */:
case 57 /* f64_store */:
case 58 /* i32_store8 */:
case 59 /* i32_store16 */:
case 60 /* i64_store8 */:
case 61 /* i64_store16 */:
case 62 /* i64_store32 */:
this.writeMemoryImmediate(opInfo.memoryAddress);
break;
case 63 /* current_memory */:
case 64 /* grow_memory */:
this.writeVarUint(0);
break;
case 65 /* i32_const */:
this.writeVarInt(opInfo.literal | 0);
break;
case 66 /* i64_const */:
this.writeVarInt64(opInfo.literal);
break;
case 67 /* f32_const */:
this.writeFloat32(opInfo.literal);
break;
case 68 /* f64_const */:
this.writeFloat64(opInfo.literal);
break;
}
};
Emitter.prototype.writeMemorySectionEntry = function (entry) {
this.ensureState(EmitterState.MemorySection);
this._sectionEntiesCount++;
this.writeMemoryType(entry);
};
Emitter.prototype.writeNameMap = function (map) {
var _this = this;
this.writeVarUint(map.length);
map.forEach(function (naming) {
_this.writeVarUint(naming.index);
_this.writeString(naming.name);
});
};
Emitter.prototype.writeNameEntry = function (entry) {
var _this = this;
this.ensureState(EmitterState.NameEntry);
this.writeVarUint(entry.type);
var payloadLengthPatchable = this.writePatchableVarUint32();
var start = this._position;
switch (entry.type) {
case 1 /* Function */:
this.writeNameMap(entry.names);
break;
case 2 /* Local */:
var funcs = entry.funcs;
this.writeVarUint(funcs.length);
funcs.forEach(function (func) {
_this.writeVarUint(func.index);
_this.writeNameMap(func.locals);
});
break;
default:
throw new Error("Unexpected name entry type " + entry.type);
}
this.patchVarUint32(payloadLengthPatchable, this._position - start);
};
Emitter.prototype.writeRelocHeader = function (header) {
this.ensureState(EmitterState.RelocHeader);
this.writeVarInt(header.id);
if (header.id == 0 /* Custom */) {
this.writeString(header.name);
}
this.writePatchableSectionEntriesCount();
this._state = EmitterState.RelocEntry;
};
Emitter.prototype.writeRelocEntry = function (entry) {
this.ensureState(EmitterState.RelocEntry);
this._sectionEntiesCount++;
this.writeVarUint(entry.type);
this.writeVarUint(entry.offset);
this.writeVarUint(entry.index);
switch (entry.type) {
case 0 /* FunctionIndex_LEB */:
case 1 /* TableIndex_SLEB */:
case 2 /* TableIndex_I32 */:
break;
case 3 /* GlobalAddr_LEB */:
case 4 /* GlobalAddr_SLEB */:
case 5 /* GlobalAddr_I32 */:
this.writeVarUint(entry.addend);
break;
default:
throw new Error("Unexpected reloc entry type " + entry.type);
}
};
Emitter.prototype.writeLinkingSection = function (entry) {
this.ensureState(EmitterState.LinkingEntry);
this._sectionEntiesCount++;
this.writeVarUint(entry.type);
switch (entry.type) {
case 1 /* StackPointer */:
this.writeVarUint(entry.index);
break;
default:
throw new Error("Unexpected linking entry type " + entry.type);
}
};
Emitter.prototype.writeSourceMappingURL = function (url) {
this.ensureState(EmitterState.SourceMappingURL);
this.writeString(url.url);
this._state = EmitterState.SourceMappingURLEnd;
};
Emitter.prototype.writeEndSection = function () {
switch (this._state) {
case EmitterState.TypeSection:
case EmitterState.ImportSection:
case EmitterState.FunctionSection:
case EmitterState.ExportSection:
case EmitterState.CodeSection:
case EmitterState.MemorySection:
case EmitterState.GlobalSection:
case EmitterState.DataSection:
case EmitterState.TableSection:
case EmitterState.ElementSection:
case EmitterState.RelocEntry:
case EmitterState.LinkingEntry:
this.patchVarUint32(this._sectionEntiesCountBytes, this._sectionEntiesCount);
break;
case EmitterState.NameEntry:
case EmitterState.SourceMappingURLEnd:
case EmitterState.RawDataSection:
break;
default:
throw new Error("Unexpected state: " + this._state + " (expected section state)");
}
var sectionLength = this._position - this._sectionStart;
this.patchVarUint32(this._sectionSizeBytes, sectionLength);
this._state = EmitterState.Wasm;
};
return Emitter;
}());
exports.Emitter = Emitter;
//# sourceMappingURL=WasmEmitter.js.map

@@ -15,2 +15,3 @@ /* Copyright 2016 Mozilla Foundation

*/
"use strict";
var __extends = (this && this.__extends) || (function () {

@@ -26,74 +27,63 @@ var extendStatics = Object.setPrototypeOf ||

})();
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
Object.defineProperty(exports, "__esModule", { value: true });
var stream_1 = require("stream");
var WasmParser_1 = require("./WasmParser");
var WasmParser_2 = require("./WasmParser");
exports.BinaryReaderState = WasmParser_2.BinaryReaderState;
exports.SectionCode = WasmParser_2.SectionCode;
var BinaryReaderTransform = (function (_super) {
__extends(BinaryReaderTransform, _super);
function BinaryReaderTransform() {
var _this = _super.call(this, {
readableObjectMode: true
}) || this;
_this._buffer = new ArrayBuffer(1024);
_this._bufferSize = 0;
_this._parser = new WasmParser_1.BinaryReader();
return _this;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "stream", "./WasmParser", "./WasmParser"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var stream_1 = require("stream");
var WasmParser_1 = require("./WasmParser");
var WasmParser_2 = require("./WasmParser");
exports.BinaryReaderState = WasmParser_2.BinaryReaderState;
exports.SectionCode = WasmParser_2.SectionCode;
var BinaryReaderTransform = (function (_super) {
__extends(BinaryReaderTransform, _super);
function BinaryReaderTransform() {
var _this = _super.call(this, {
readableObjectMode: true
}) || this;
_this._buffer = new ArrayBuffer(1024);
_this._bufferSize = 0;
_this._parser = new WasmParser_1.BinaryReader();
return _this;
BinaryReaderTransform.prototype._transform = function (chunk, encoding, callback) {
var buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
var bufferNeeded = this._bufferSize + buf.length;
if (bufferNeeded > this._buffer.byteLength) {
var oldData = new Uint8Array(this._buffer, 0, this._bufferSize);
var newBuffer = new ArrayBuffer(bufferNeeded);
new Uint8Array(newBuffer).set(oldData);
this._buffer = newBuffer;
}
BinaryReaderTransform.prototype._transform = function (chunk, encoding, callback) {
var buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
var bufferNeeded = this._bufferSize + buf.length;
if (bufferNeeded > this._buffer.byteLength) {
var oldData = new Uint8Array(this._buffer, 0, this._bufferSize);
var newBuffer = new ArrayBuffer(bufferNeeded);
new Uint8Array(newBuffer).set(oldData);
this._buffer = newBuffer;
var arr = new Uint8Array(this._buffer, 0, bufferNeeded);
arr.set(new Uint8Array(buf.buffer, buf.byteOffset, buf.length), this._bufferSize);
this._bufferSize = bufferNeeded;
var parser = this._parser;
parser.setData(this._buffer, 0, bufferNeeded, false);
while (parser.read()) {
this.push({
state: parser.state,
result: parser.result
});
}
if (parser.position > 0) {
var left = parser.length - parser.position;
if (left > 0) {
arr.set(arr.subarray(parser.position, parser.length));
}
var arr = new Uint8Array(this._buffer, 0, bufferNeeded);
arr.set(new Uint8Array(buf.buffer, buf.byteOffset, buf.length), this._bufferSize);
this._bufferSize = bufferNeeded;
var parser = this._parser;
parser.setData(this._buffer, 0, bufferNeeded, false);
while (parser.read()) {
this.push({
state: parser.state,
result: parser.result
});
}
if (parser.position > 0) {
var left = parser.length - parser.position;
if (left > 0) {
arr.set(arr.subarray(parser.position, parser.length));
}
this._bufferSize = left;
}
callback();
};
BinaryReaderTransform.prototype._flush = function (callback) {
var parser = this._parser;
parser.setData(this._buffer, 0, this._bufferSize, true);
while (parser.read()) {
this.push({
state: parser.state,
result: parser.result
});
}
this._bufferSize = 0;
callback();
};
return BinaryReaderTransform;
}(stream_1.Transform));
exports.BinaryReaderTransform = BinaryReaderTransform;
});
this._bufferSize = left;
}
callback();
};
BinaryReaderTransform.prototype._flush = function (callback) {
var parser = this._parser;
parser.setData(this._buffer, 0, this._bufferSize, true);
while (parser.read()) {
this.push({
state: parser.state,
result: parser.result
});
}
this._bufferSize = 0;
callback();
};
return BinaryReaderTransform;
}(stream_1.Transform));
exports.BinaryReaderTransform = BinaryReaderTransform;
//# sourceMappingURL=WasmParserTransform.js.map
{
"name": "wasmparser",
"version": "0.3.10",
"version": "0.4.0",
"description": "Binary WebAssembly file parser.",
"main": "./dist/WasmParser.js",
"types": "./dist/WasmParser.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {

@@ -8,0 +8,0 @@ "disassemble-wasm": "./disassemble-wasm.js"

@@ -138,3 +138,10 @@ /* Copyright 2016 Mozilla Foundation

const IndentIncrement: string = ' ';
export interface IDisassemblerResult {
lines: Array<string>;
offsets?: Array<number>
done: boolean;
}
export class WasmDisassembler {
private _lines: Array<string>;
private _offsets: Array<number>;
private _buffer: Array<string>;

@@ -151,3 +158,7 @@ private _types: Array<IFunctionType>;

private _nextLineToAddOffset: number;
private _done: boolean;
private _currentPosition: number;
constructor() {
this._lines = [];
this._offsets = [];
this._buffer = [];

@@ -163,3 +174,4 @@ this._types = [];

this._addOffsets = false;
this._nextLineToAddOffset = 0;
this._done = false;
this._currentPosition = 0;
}

@@ -170,4 +182,16 @@ public get addOffsets() {

public set addOffsets(value: boolean) {
if (this._lines.length > 0 && this._addOffsets != value)
throw new Error('Cannot switch addOffsets in the middle of the chunk.');
this._addOffsets = value;
}
private appendBuffer(s: string) {
this._buffer.push(s);
}
private newLine() {
if (this.addOffsets)
this._offsets.push(this._currentPosition);
let line = this._buffer.join('');
this._buffer.length = 0;
this._lines.push(line);
}
private printType(typeIndex: number): string {

@@ -204,13 +228,43 @@ var type = this._types[typeIndex];

public disassemble(reader: BinaryReader): string {
let done = this.disassembleChunk(reader);
if (!done)
return null;
let lines = this._lines;
if (this._addOffsets) {
lines = lines.map((line, index) => {
var position = formatHex(this._offsets[index], 4);
return line + ' ;; @' + position;
});
}
lines.push(''); // we need '\n' after last line
let result = lines.join('\n');
this._lines.length = 0;
this._offsets.length = 0;
return result;
}
public getResult(): IDisassemblerResult {
let result = {
lines: this._lines,
offsets: this._addOffsets ? this._offsets : undefined,
done: this._done,
};
this._lines = [];
if (this._addOffsets)
this._offsets = [];
return result;
}
public disassembleChunk(reader: BinaryReader, offsetInModule: number = 0): boolean {
if (this._done)
throw new Error('Invalid state: disassembly process was already finished.')
while (true) {
var position = reader.position;
this._currentPosition = reader.position + offsetInModule;
if (!reader.read())
return null;
return false;
switch (reader.state) {
case BinaryReaderState.END_WASM:
this._buffer.push(')\n');
this.appendBuffer(')');
this.newLine();
if (!reader.hasMoreBytes()) {
let result = this._buffer.join('');
this._buffer.length = 0;
return result;
this._done = true;
return true;
}

@@ -221,3 +275,4 @@ break;

case BinaryReaderState.BEGIN_WASM:
this._buffer.push('(module\n');
this.appendBuffer('(module');
this.newLine();
break;

@@ -247,27 +302,29 @@ case BinaryReaderState.END_SECTION:

var memoryInfo = <IMemoryType>reader.result;
this._buffer.push(` (memory ${memoryInfo.limits.initial}`);
this.appendBuffer(` (memory ${memoryInfo.limits.initial}`);
if (memoryInfo.limits.maximum !== undefined) {
this._buffer.push(` ${memoryInfo.limits.maximum}`);
this.appendBuffer(` ${memoryInfo.limits.maximum}`);
}
this._buffer.push(')\n');
this.appendBuffer(')');
this.newLine();
break;
case BinaryReaderState.TABLE_SECTION_ENTRY:
var tableInfo = <ITableType>reader.result;
this._buffer.push(` (table $table${this._tableCount++} ${limitsToString(tableInfo.limits)} ${typeToString(tableInfo.elementType)})\n`);
this.appendBuffer(` (table $table${this._tableCount++} ${limitsToString(tableInfo.limits)} ${typeToString(tableInfo.elementType)})`);
this.newLine();
break;
case BinaryReaderState.EXPORT_SECTION_ENTRY:
var exportInfo = <IExportEntry>reader.result;
this._buffer.push(` (export "${binToString(exportInfo.field)}" `);
this.appendBuffer(` (export "${binToString(exportInfo.field)}" `);
switch (exportInfo.kind) {
case ExternalKind.Function:
this._buffer.push(`$func${exportInfo.index}`);
this.appendBuffer(`$func${exportInfo.index}`);
break;
case ExternalKind.Table:
this._buffer.push(`(table $table${exportInfo.index})`);
this.appendBuffer(`(table $table${exportInfo.index})`);
break;
case ExternalKind.Memory:
this._buffer.push(`memory`);
this.appendBuffer(`memory`);
break;
case ExternalKind.Global:
this._buffer.push(`(global $global${exportInfo.index})`);
this.appendBuffer(`(global $global${exportInfo.index})`);
break;

@@ -277,3 +334,4 @@ default:

}
this._buffer.push(')\n');
this.appendBuffer(')');
this.newLine();
break;

@@ -285,15 +343,15 @@ case BinaryReaderState.IMPORT_SECTION_ENTRY:

case ExternalKind.Function:
this._buffer.push(` (import $func${this._importCount++} ${importSource} ${this.printType(importInfo.funcTypeIndex)})\n`);
this.appendBuffer(` (import $func${this._importCount++} ${importSource} ${this.printType(importInfo.funcTypeIndex)})`);
break;
case ExternalKind.Table:
var tableImportInfo = <ITableType>importInfo.type;
this._buffer.push(` (import ${importSource} (table $table${this._tableCount++} ${limitsToString(tableImportInfo.limits)} ${typeToString(tableImportInfo.elementType)}))\n`);
this.appendBuffer(` (import ${importSource} (table $table${this._tableCount++} ${limitsToString(tableImportInfo.limits)} ${typeToString(tableImportInfo.elementType)}))`);
break;
case ExternalKind.Memory:
var memoryImportInfo = <IMemoryType>importInfo.type;
this._buffer.push(` (import ${importSource} (memory ${limitsToString(memoryImportInfo.limits)}))\n`);
this.appendBuffer(` (import ${importSource} (memory ${limitsToString(memoryImportInfo.limits)}))`);
break;
case ExternalKind.Global:
var globalImportInfo = <IGlobalType>importInfo.type;
this._buffer.push(` (import ${importSource} (global $global${this._globalCount++} ${globalTypeToString(globalImportInfo)}))\n`);
this.appendBuffer(` (import ${importSource} (global $global${this._globalCount++} ${globalTypeToString(globalImportInfo)}))`);
break;

@@ -303,24 +361,29 @@ default:

}
this.newLine();
break;
case BinaryReaderState.BEGIN_ELEMENT_SECTION_ENTRY:
var elementSegmentInfo = <IElementSegment>reader.result;
this._buffer.push(` (elem\n`);
this.appendBuffer(` (elem`);
this.newLine();
break;
case BinaryReaderState.END_ELEMENT_SECTION_ENTRY:
this._buffer.push(' )\n');
this.appendBuffer(' )');
this.newLine();
break;
case BinaryReaderState.ELEMENT_SECTION_ENTRY_BODY:
var elementSegmentBody = <IElementSegmentBody>reader.result;
this._buffer.push(' ');
this.appendBuffer(' ');
elementSegmentBody.elements.forEach(funcIndex => {
this._buffer.push(` $func${funcIndex}`);
this.appendBuffer(` $func${funcIndex}`);
});
this._buffer.push('\n');
this.newLine();
break;
case BinaryReaderState.BEGIN_GLOBAL_SECTION_ENTRY:
var globalInfo = <IGlobalVariable>reader.result;
this._buffer.push(` (global $global${this._globalCount++} ${globalTypeToString(globalInfo.type)}\n`);
this.appendBuffer(` (global $global${this._globalCount++} ${globalTypeToString(globalInfo.type)}`);
this.newLine();
break;
case BinaryReaderState.END_GLOBAL_SECTION_ENTRY:
this._buffer.push(' )\n');
this.appendBuffer(' )');
this.newLine();
break;

@@ -331,13 +394,17 @@ case BinaryReaderState.TYPE_SECTION_ENTRY:

this._types.push(funcType);
this._buffer.push(` (type $type${typeIndex} ${this.printType(typeIndex)})\n`);
this.appendBuffer(` (type $type${typeIndex} ${this.printType(typeIndex)})`);
this.newLine();
break;
case BinaryReaderState.BEGIN_DATA_SECTION_ENTRY:
this._buffer.push(` (data\n`);
this.appendBuffer(` (data`);
this.newLine();
break;
case BinaryReaderState.DATA_SECTION_ENTRY_BODY:
var body = <IDataSegmentBody>reader.result;
this._buffer.push(` "${binToString(body.data)}"\n`);
this.appendBuffer(` "${binToString(body.data)}"`);
this.newLine();
break;
case BinaryReaderState.END_DATA_SECTION_ENTRY:
this._buffer.push(` )\n`);
this.appendBuffer(` )`);
this.newLine();
break;

@@ -347,6 +414,8 @@ case BinaryReaderState.BEGIN_INIT_EXPRESSION_BODY:

this._indentLevel = 0;
this._buffer.push(' (\n');
this.appendBuffer(' (');
this.newLine();
break;
case BinaryReaderState.END_INIT_EXPRESSION_BODY:
this._buffer.push(' )\n');
this.appendBuffer(' )');
this.newLine();
break;

@@ -360,7 +429,9 @@ case BinaryReaderState.FUNCTION_SECTION_ENTRY:

var printIndex = this._funcIndex + this._importCount;
this._buffer.push(` (func $func${printIndex}${this.printFuncType(type, true)}\n`);
this.appendBuffer(` (func $func${printIndex}${this.printFuncType(type, true)}`);
this.newLine();
var localIndex = type.params.length;
for (var l of func.locals) {
for (var i = 0; i < l.count; i++) {
this._buffer.push(` (local $var${localIndex++} ${typeToString(l.type)})\n`);
this.appendBuffer(` (local $var${localIndex++} ${typeToString(l.type)})`);
this.newLine();
}

@@ -390,11 +461,12 @@ }

}
this._buffer.push(this._indent, str);
this.appendBuffer(this._indent);
this.appendBuffer(str);
if (operator.localIndex !== undefined) {
this._buffer.push(` $var${operator.localIndex}`);
this.appendBuffer(` $var${operator.localIndex}`);
}
if (operator.funcIndex !== undefined) {
this._buffer.push(` $func${operator.funcIndex}`);
this.appendBuffer(` $func${operator.funcIndex}`);
}
if (operator.typeIndex !== undefined) {
this._buffer.push(` $type${operator.typeIndex}`);
this.appendBuffer(` $type${operator.typeIndex}`);
}

@@ -404,12 +476,12 @@ if (operator.literal !== undefined) {

case OperatorCode.i32_const:
this._buffer.push(` ${(<number>operator.literal).toString()}`);
this.appendBuffer(` ${(<number>operator.literal).toString()}`);
break;
case OperatorCode.f32_const:
this._buffer.push(` ${formatFloat32(<number>operator.literal)}`);
this.appendBuffer(` ${formatFloat32(<number>operator.literal)}`);
break;
case OperatorCode.f64_const:
this._buffer.push(` ${formatFloat64(<number>operator.literal)}`);
this.appendBuffer(` ${formatFloat64(<number>operator.literal)}`);
break;
case OperatorCode.i64_const:
this._buffer.push(` ${(<Int64>operator.literal).toDouble()}`);
this.appendBuffer(` ${(<Int64>operator.literal).toDouble()}`);
break;

@@ -419,15 +491,15 @@ }

if (operator.memoryAddress !== undefined) {
this._buffer.push(` ${memoryAddressToString(operator.memoryAddress, operator.code)}`);
this.appendBuffer(` ${memoryAddressToString(operator.memoryAddress, operator.code)}`);
}
if (operator.brDepth !== undefined) {
this._buffer.push(` ${operator.brDepth}`);
this.appendBuffer(` ${operator.brDepth}`);
}
if (operator.brTable !== undefined) {
for (var i = 0; i < operator.brTable.length; i++)
this._buffer.push(` ${operator.brTable[i]}`);
this.appendBuffer(` ${operator.brTable[i]}`);
}
if (operator.globalIndex !== undefined) {
this._buffer.push(` $global${operator.globalIndex}`);
this.appendBuffer(` $global${operator.globalIndex}`);
}
this._buffer.push('\n');
this.newLine();
switch (operator.code) {

@@ -443,3 +515,4 @@ case OperatorCode.if:

case BinaryReaderState.END_FUNCTION_BODY:
this._buffer.push(` )\n`);
this.appendBuffer(` )`);
this.newLine();
break;

@@ -449,14 +522,4 @@ default:

}
if (this._addOffsets && this._nextLineToAddOffset < this._buffer.length) {
var i = this._nextLineToAddOffset;
var line = this._buffer[i];
while (line.indexOf('\n') < 0) {
if (++i >= this._buffer.length) break;
line = this._buffer[i];
}
this._buffer[i] = line.replace(/\n/, ` ;; @${formatHex(position, 4)}\n`);
this._nextLineToAddOffset = this._buffer.length;
}
}
}
}

@@ -8,4 +8,3 @@ {

"target": "es5",
"module": "umd",
"moduleResolution": "classic"
"module": "commonjs"
},

@@ -16,2 +15,3 @@ "exclude": [

"files": [
"src/index.ts",
"src/WasmParser.ts",

@@ -18,0 +18,0 @@ "src/WasmParserTransform.ts",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc