diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm-binary.h | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index 908121543..09291157e 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -871,6 +871,8 @@ public: readDataSegments(); readFunctionTable(); readEnd(); + + processFunctions(); } uint8_t getInt8() { @@ -1046,15 +1048,12 @@ public: addLocals(f32); addLocals(f64); } - size_t pre = pos; size_t size = getInt16(); - { - nextLabel = 0; - assert(breakStack.empty()); - readExpression(func->body); - assert(breakStack.empty()); - } - assert(pos = pre + size); + // we can't read the function yet - it might call other functions that are defined later, + // and we do depend on the function type, as well as the mappedFunctions table. + functions.emplace_back(func, pos, size); + pos += size; + func->body = nullptr; // will be filled later. but we do have the name and the type already. wasm.addFunction(func); } if (export_) { @@ -1066,6 +1065,26 @@ public: } } + struct FunctionData { + Function* func; + size_t pos, size; + FunctionData(Function* func, size_t pos, size_t size) : func(func), pos(pos), size(size) {} + }; + + std::vector<FunctionData> functions; + + void processFunctions() { + for (auto& func : functions) { + Function* curr = func.func; + pos = func.pos; + nextLabel = 0; + assert(breakStack.empty()); + readExpression(curr->body); + assert(breakStack.empty()); + assert(pos == func.pos + func.size); + } + } + void readDataSegments() { if (debug) std::cerr << "== readDataSegments" << std::endl; verifyInt8(BinaryConsts::DataSegments); |