| 11 | using namespace Decode; |
| 12 | |
| 13 | WasmModule WasmVM::module_decode(std::istream& istream){ |
| 14 | // Check magic & version |
| 15 | { |
| 16 | u32_t data; |
| 17 | istream.read((char*) &data, sizeof(u32_t)); |
| 18 | if(data != Decode::magic){ |
| 19 | throw Exception::incorrect_magic(); |
| 20 | } |
| 21 | istream.read((char*) &data, sizeof(u32_t)); |
| 22 | if(data != Decode::version){ |
| 23 | throw Exception::incorrect_magic(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | WasmModule module; |
| 28 | Decode::Stream stream(istream, module); |
| 29 | // Types |
| 30 | stream >> Decode::Type(module.types); |
| 31 | // Imports |
| 32 | stream >> Decode::Import(module.imports); |
| 33 | // Funcs |
| 34 | stream >> Decode::Func(module.funcs); |
| 35 | // Tables |
| 36 | stream >> Decode::Table(module.tables); |
| 37 | // Mems |
| 38 | stream >> Decode::Memory(module.mems); |
| 39 | // Tags |
| 40 | stream >> Decode::Tag(module.tags); |
| 41 | // Globals |
| 42 | stream >> Decode::Global(module.globals); |
| 43 | // Exports |
| 44 | stream >> Decode::Export(module.exports); |
| 45 | // Start |
| 46 | stream >> Decode::Start(module.start); |
| 47 | // Elems |
| 48 | stream >> Decode::Elem(module.elems); |
| 49 | // Data count |
| 50 | stream >> Decode::DataCount(module.datas); |
| 51 | // Code |
| 52 | stream >> Decode::Code(module.funcs); |
| 53 | // Datas |
| 54 | stream >> Decode::Data(module.datas); |
| 55 | return module; |
| 56 | } |
| 57 | |
| 58 | byte_t Decode::Stream::peek(){ |
| 59 | return (byte_t)istream.peek(); |