feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -365,7 +365,13 @@ function decode(ab, opts) {
if (type == _helperWasmBytecode["default"].types.func) {
dump([type], "func");
var paramValtypes = parseVec(function (b) {
return _helperWasmBytecode["default"].valtypes[b];
var valtype = _helperWasmBytecode["default"].valtypes[b];
if (valtype === undefined) {
throw new Error("unexpected value type ".concat(b));
}
return valtype;
});
var params = paramValtypes.map(function (v) {
return t.funcParam(
@@ -594,10 +600,7 @@ function decode(ab, opts) {
var bodySizeU32 = readU32();
eatBytes(bodySizeU32.nextIndex);
dump([bodySizeU32.value], "function body size");
var code = [];
/**
* Parse locals
*/
var code = []; // Parse locals
var funcLocalNumU32 = readU32();
var funcLocalNum = funcLocalNumU32.value;
@@ -659,6 +662,13 @@ function decode(ab, opts) {
if (instructionByte === 0xfe) {
instructionByte = 0xfe00 + readByte();
eatBytes(1);
} // Table instructions
// https://webassembly.github.io/spec/core/binary/instructions.html#table-instructions
if (instructionByte === 0xfc) {
instructionByte = 0xfc00 + readByte();
eatBytes(1);
}
var instruction = _helperWasmBytecode["default"].symbolsByByte[instructionByte];
@@ -667,6 +677,10 @@ function decode(ab, opts) {
throw new _helperApiError.CompileError("Unexpected instruction: " + toHex(instructionByte));
}
if (instruction === "illegal") {
throw new Error("tried to decode an illegal bytecode: ".concat(toHex(instructionByte)));
}
if (typeof instruction.object === "string") {
dump([instructionByte], "".concat(instruction.object, ".").concat(instruction.name));
} else {
@@ -693,15 +707,7 @@ function decode(ab, opts) {
if (instruction.name === "loop") {
var _startLoc7 = getPosition();
var blocktypeByte = readByte();
eatBytes(1);
var blocktype = _helperWasmBytecode["default"].blockTypes[blocktypeByte];
dump([blocktypeByte], "blocktype");
if (typeof blocktype === "undefined") {
throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(blocktypeByte));
}
var blocktype = parseBlockType();
var instr = [];
parseInstructionBlock(instr); // preserve anonymous
@@ -717,15 +723,7 @@ function decode(ab, opts) {
} else if (instruction.name === "if") {
var _startLoc8 = getPosition();
var _blocktypeByte = readByte();
eatBytes(1);
var _blocktype = _helperWasmBytecode["default"].blockTypes[_blocktypeByte];
dump([_blocktypeByte], "blocktype");
if (typeof _blocktype === "undefined") {
throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(_blocktypeByte));
}
var _blocktype = parseBlockType();
var testIndex = t.withRaw(t.identifier(getUniqueName("if")), "");
var ifBody = [];
@@ -756,15 +754,7 @@ function decode(ab, opts) {
} else if (instruction.name === "block") {
var _startLoc9 = getPosition();
var _blocktypeByte2 = readByte();
eatBytes(1);
var _blocktype2 = _helperWasmBytecode["default"].blockTypes[_blocktypeByte2];
dump([_blocktypeByte2], "blocktype");
if (typeof _blocktype2 === "undefined") {
throw new _helperApiError.CompileError("Unexpected blocktype: " + toHex(_blocktypeByte2));
}
var _blocktype2 = parseBlockType();
var _instr2 = [];
parseInstructionBlock(_instr2); // preserve anonymous
@@ -1015,7 +1005,7 @@ function decode(ab, opts) {
var elementType = _helperWasmBytecode["default"].tableTypes[elementTypeByte];
if (typeof elementType === "undefined") {
throw new _helperApiError.CompileError("Unknown element type in table: " + toHex(elementType));
throw new _helperApiError.CompileError("Unknown element type in table: " + toHex(elementTypeByte));
}
var limits = parseLimits();
@@ -1221,39 +1211,176 @@ function decode(ab, opts) {
var _startLoc12 = getPosition();
var tableindexu32 = readU32();
var tableindex = tableindexu32.value;
var bitfield = tableindexu32.value;
eatBytes(tableindexu32.nextIndex);
dump([tableindex], "table index");
/**
* Parse instructions
*/
dump([bitfield], "bitfield");
var instr = [];
parseInstructionBlock(instr);
/**
* Parse ( vector function index ) *
*/
if (bitfield === 0) {
// Parse instructions
var instr = [];
parseInstructionBlock(instr); // Parse ( vector function index ) *
var indicesu32 = readU32();
var indices = indicesu32.value;
eatBytes(indicesu32.nextIndex);
dump([indices], "num indices");
var indexValues = [];
var indicesu32 = readU32();
var indices = indicesu32.value;
eatBytes(indicesu32.nextIndex);
dump([indices], "num indices");
var indexValues = [];
for (var _i5 = 0; _i5 < indices; _i5++) {
var indexu32 = readU32();
var index = indexu32.value;
eatBytes(indexu32.nextIndex);
dump([index], "index");
indexValues.push(t.indexLiteral(index));
for (var _i5 = 0; _i5 < indices; _i5++) {
var indexu32 = readU32();
var index = indexu32.value;
eatBytes(indexu32.nextIndex);
dump([index], "index");
indexValues.push(t.indexLiteral(index));
}
var elemNode = function () {
var endLoc = getPosition();
return t.withLoc(t.elem(t.indexLiteral(bitfield), instr, indexValues), endLoc, _startLoc12);
}();
elems.push(elemNode);
} else if (bitfield === 1) {
var elemKind = readByte();
eatBytes(1);
if (elemKind !== 0) {
throw new Error("unexpected Elem kind: ".concat(toHex(elemKind)));
} // Parse ( vector function index ) *
var _indicesu = readU32();
var _indices = _indicesu.value;
eatBytes(_indicesu.nextIndex);
dump([_indices], "num indices");
var _indexValues = [];
for (var _i6 = 0; _i6 < _indices; _i6++) {
var _indexu2 = readU32();
var _index3 = _indexu2.value;
eatBytes(_indexu2.nextIndex);
dump([_index3], "index");
_indexValues.push(t.indexLiteral(_index3));
} // TODO: emit a AST node, for now just make it parse.
} else if (bitfield === 2) {
var u32 = readU32();
var tableidx = u32.value;
eatBytes(u32.nextIndex);
dump([tableidx], "tableidx"); // Parse instructions
var _instr3 = [];
parseInstructionBlock(_instr3);
var _elemKind = readByte();
eatBytes(1);
if (_elemKind !== 0) {
throw new Error("unexpected Elem kind: ".concat(toHex(_elemKind)));
} // Parse ( vector function index ) *
var _indicesu2 = readU32();
var _indices2 = _indicesu2.value;
eatBytes(_indicesu2.nextIndex);
dump([_indices2], "num indices");
var _indexValues2 = [];
for (var _i7 = 0; _i7 < _indices2; _i7++) {
var _indexu3 = readU32();
var _index4 = _indexu3.value;
eatBytes(_indexu3.nextIndex);
dump([_index4], "index");
_indexValues2.push(t.indexLiteral(_index4));
}
var _elemNode = function () {
var endLoc = getPosition();
return t.withLoc(t.elem(t.indexLiteral(bitfield), _instr3, _indexValues2), endLoc, _startLoc12);
}();
elems.push(_elemNode);
} else if (bitfield === 3) {
var _elemKind2 = readByte();
eatBytes(1);
if (_elemKind2 !== 0) {
throw new Error("unexpected Elem kind: ".concat(toHex(_elemKind2)));
} // Parse ( vector function index ) *
var countU32 = readU32();
var count = countU32.value;
eatBytes(countU32.nextIndex);
dump([count], "count");
for (var _i8 = 0; _i8 < count; _i8++) {
var _indexu4 = readU32();
var _index5 = _indexu4.value;
eatBytes(_indexu4.nextIndex);
dump([_index5], "index");
} // TODO: emit a AST node, for now just make it parse.
} else if (bitfield === 4) {
var expr = [];
parseInstructionBlock(expr);
var _countU = readU32();
var _count = _countU.value;
eatBytes(_countU.nextIndex);
dump([_count], "count");
for (var _i9 = 0; _i9 < _count; _i9++) {
var code = [];
parseInstructionBlock(code);
} // TODO: emit a AST node, for now just make it parse.
} else if (bitfield === 5) {
var reftype = readByte();
eatBytes(1);
dump([reftype], "reftype");
var _countU2 = readU32();
var _count2 = _countU2.value;
eatBytes(_countU2.nextIndex);
dump([_count2], "count");
for (var _i10 = 0; _i10 < _count2; _i10++) {
var _code = [];
parseInstructionBlock(_code);
} // TODO: emit a AST node, for now just make it parse.
} else if (bitfield === 7) {
var _reftype = readByte();
eatBytes(1);
dump([_reftype], "reftype");
var _countU3 = readU32();
var _count3 = _countU3.value;
eatBytes(_countU3.nextIndex);
dump([_count3], "count");
for (var _i11 = 0; _i11 < _count3; _i11++) {
var _code2 = [];
parseInstructionBlock(_code2);
} // TODO: emit a AST node, for now just make it parse.
} else {
throw new Error("unexpected Elem with bitfield ".concat(toHex(bitfield)));
}
var elemNode = function () {
var endLoc = getPosition();
return t.withLoc(t.elem(t.indexLiteral(tableindex), instr, indexValues), endLoc, _startLoc12);
}();
elems.push(elemNode);
}
return elems;
@@ -1722,6 +1849,28 @@ function decode(ab, opts) {
}
}
function parseBlockType() {
var blocktypeByte = readByte();
var blocktype = _helperWasmBytecode["default"].blockTypes[blocktypeByte];
if (typeof blocktype !== "undefined") {
eatBytes(1);
dump([blocktypeByte], "blocktype"); // value type
return blocktype;
} else {
// type index
var u32 = readU32();
eatBytes(u32.nextIndex);
var signature = state.typesInModule[u32.value];
console.log({
signature: signature
});
dump([u32.value], "typeidx");
return u32.value;
}
}
parseModuleHeader();
parseVersion();
var moduleFields = [];