diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-01-22 16:02:52 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-01-22 16:02:52 -0800 |
commit | 91ed39b09313549924be949e706d1bbe9c970234 (patch) | |
tree | 95812f50e635fed53bf12a93351876c0955430bc | |
parent | 8bf766c858c996c88017c589d921da3326210801 (diff) | |
download | binaryen-91ed39b09313549924be949e706d1bbe9c970234.tar.gz binaryen-91ed39b09313549924be949e706d1bbe9c970234.tar.bz2 binaryen-91ed39b09313549924be949e706d1bbe9c970234.zip |
more flexibility in section reading in binary format code
-rw-r--r-- | src/wasm-binary.h | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index a6c388c52..821b11270 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -388,6 +388,7 @@ public: } void writeMemory() { + if (wasm->memory.max == 0) return; if (debug) std::cerr << "== writeMemory" << std::endl; o << int8_t(BinaryConsts::Memory) << int8_t(log2(wasm->memory.initial)) << int8_t(log2(wasm->memory.max)) @@ -395,6 +396,7 @@ public: } void writeSignatures() { + if (wasm->functionTypes.size() == 0) return; if (debug) std::cerr << "== writeSignatures" << std::endl; o << int8_t(BinaryConsts::Signatures) << LEB128(wasm->functionTypes.size()); for (auto* type : wasm->functionTypes) { @@ -456,6 +458,7 @@ public: } void writeFunctions() { + if (wasm->functions.size() + wasm->imports.size() == 0) return; if (debug) std::cerr << "== writeFunctions" << std::endl; size_t total = wasm->imports.size() + wasm->functions.size(); o << int8_t(BinaryConsts::Functions) << LEB128(total); @@ -505,6 +508,7 @@ public: } void writeDataSegments() { + if (wasm->memory.segments.size() == 0) return; o << int8_t(BinaryConsts::DataSegments) << LEB128(wasm->memory.segments.size()); for (auto& segment : wasm->memory.segments) { o << int32_t(segment.offset); @@ -526,6 +530,7 @@ public: } void writeFunctionTable() { + if (wasm->table.names.size() == 0) return; if (debug) std::cerr << "== writeFunctionTable" << std::endl; o << int8_t(BinaryConsts::FunctionTable) << LEB128(wasm->table.names.size()); for (auto name : wasm->table.names) { @@ -924,12 +929,24 @@ public: WasmBinaryBuilder(AllocatingModule& wasm, std::vector<char>& input, bool debug) : wasm(wasm), allocator(wasm.allocator), input(input), debug(debug), pos(0) {} void read() { - readMemory(); - readSignatures(); - readFunctions(); - readDataSegments(); - readFunctionTable(); - readEnd(); + // read sections until the end + while (1) { + int8_t section = getInt8(); + + if (section == BinaryConsts::End) { + if (debug) std::cerr << "== readEnd" << std::endl; + break; + } + + switch (section) { + case BinaryConsts::Memory: readMemory(); break; + case BinaryConsts::Signatures: readSignatures(); break; + case BinaryConsts::Functions: readFunctions(); break; + case BinaryConsts::DataSegments: readDataSegments(); break; + case BinaryConsts::FunctionTable: readFunctionTable(); break; + default: abort(); + } + } processFunctions(); } @@ -1026,7 +1043,6 @@ public: void readMemory() { if (debug) std::cerr << "== readMemory" << std::endl; - verifyInt8(BinaryConsts::Memory); wasm.memory.initial = std::pow<size_t>(2, getInt8()); wasm.memory.max = std::pow<size_t>(2, getInt8()); verifyInt8(1); // export memory @@ -1034,7 +1050,6 @@ public: void readSignatures() { if (debug) std::cerr << "== readSignatures" << std::endl; - verifyInt8(BinaryConsts::Signatures); size_t numTypes = getLEB128(); if (debug) std::cerr << "num: " << numTypes << std::endl; for (size_t i = 0; i < numTypes; i++) { @@ -1060,7 +1075,6 @@ public: void readFunctions() { if (debug) std::cerr << "== readFunctions" << std::endl; - verifyInt8(BinaryConsts::Functions); size_t total = getLEB128(); // imports and functions for (size_t i = 0; i < total; i++) { if (debug) std::cerr << "read one at " << pos << std::endl; @@ -1165,7 +1179,6 @@ public: void readDataSegments() { if (debug) std::cerr << "== readDataSegments" << std::endl; - verifyInt8(BinaryConsts::DataSegments); auto num = getLEB128(); for (size_t i = 0; i < num; i++) { auto curr = allocator.alloc<Memory::Segment>(); @@ -1182,18 +1195,12 @@ public: void readFunctionTable() { if (debug) std::cerr << "== readFunctionTable" << std::endl; - verifyInt8(BinaryConsts::FunctionTable); auto num = getLEB128(); for (size_t i = 0; i < num; i++) { wasm.table.names.push_back(mappedFunctions[getInt16()]); } } - void readEnd() { - if (debug) std::cerr << "== readEnd" << std::endl; - verifyInt8(BinaryConsts::End); - } - // AST reading int depth; // only for debugging |