Skip to content

Commit

Permalink
fix: make chunked parsing work for element entries
Browse files Browse the repository at this point in the history
This fixes chunked disassembly (via the `WasmParser`) for the
bulk memory proposal, in paricular the parsing of the element
section.

Refs: #22
  • Loading branch information
bmeurer committed Jul 13, 2020
1 parent ff8ae89 commit 34d35d0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/WasmParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1793,33 +1793,44 @@ export class BinaryReader {
}
private readElementEntryBody(): boolean {
let funcType = Type.unspecified;
const pos = this._pos;
if (
this._segmentFlags &
(SegmentFlags.IsPassive | SegmentFlags.HasTableIndex)
) {
if (!this.hasMoreBytes()) return false;
funcType = this.readVarInt7();
}
if (!this.hasVarIntBytes()) return false;
const pos = this._pos;
const numElemements = this.readVarUint32();
if (!this.hasBytes(numElemements)) {
// Shall have at least the numElemements amount of bytes.
if (!this.hasVarIntBytes()) {
this._pos = pos;
return false;
}
const numElemements = this.readVarUint32();
const elements = new Uint32Array(numElemements);
for (let i = 0; i < numElemements; i++) {
if (this._segmentFlags & SegmentFlags.FunctionsAsElements) {
if (!this.hasMoreBytes()) {
this._pos = pos;
return false;
}
// Read initializer expression, which must either be null ref or func ref
let operator = this.readUint8();
if (operator == OperatorCode.ref_null) {
elements[i] = NULL_FUNCTION_INDEX;
} else if (operator == OperatorCode.ref_func) {
if (!this.hasVarIntBytes()) {
this._pos = pos;
return false;
}
elements[i] = this.readVarInt32();
} else {
this.error = new Error("Invalid initializer expression for element");
return true;
}
if (!this.hasMoreBytes()) {
this._pos = pos;
return false;
}
operator = this.readUint8();
if (operator != OperatorCode.end) {
this.error = new Error(
Expand Down

0 comments on commit 34d35d0

Please sign in to comment.