diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-03-15 18:21:42 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-03-15 18:21:42 -0700 |
commit | 59c18e0a395cd73d1bd9069b2c447600907a6357 (patch) | |
tree | 553b773024209d2232d6fc13c4eb541888949391 /src/wasm-binary.h | |
parent | 32fff6e2a757589e234fb2d224791f6cf58a0ad5 (diff) | |
download | binaryen-59c18e0a395cd73d1bd9069b2c447600907a6357.tar.gz binaryen-59c18e0a395cd73d1bd9069b2c447600907a6357.tar.bz2 binaryen-59c18e0a395cd73d1bd9069b2c447600907a6357.zip |
add exports section
Diffstat (limited to 'src/wasm-binary.h')
-rw-r--r-- | src/wasm-binary.h | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/src/wasm-binary.h b/src/wasm-binary.h index ff038315f..64ffdeabb 100644 --- a/src/wasm-binary.h +++ b/src/wasm-binary.h @@ -176,7 +176,8 @@ namespace Section { auto Signatures = "signatures"; auto ImportTable = "import_table"; auto FunctionSignatures = "function_signatures"; - auto Functions = "functions"; // FIXME + auto Functions = "functions"; + auto ExportTable = "export_table"; auto DataSegments = "data_segments"; auto FunctionTable = "function_table"; auto End = "end"; @@ -418,6 +419,7 @@ public: writeImports(); writeFunctionSignatures(); writeFunctions(); + writeExports(); writeDataSegments(); writeFunctionTable(); writeEnd(); @@ -558,10 +560,6 @@ public: auto start = startSection(BinaryConsts::Section::Functions); size_t total = wasm->functions.size(); o << LEB128(total); - std::map<Name, Name> exportedFunctions; - for (auto* e : wasm->exports) { - exportedFunctions[e->value] = e->name; - } for (size_t i = 0; i < total; i++) { if (debug) std::cerr << "write one at" << o.size() << std::endl; Function* function = wasm->functions[i]; @@ -571,12 +569,9 @@ public: mappedLocals.clear(); numLocalsByType.clear(); if (debug) std::cerr << "writing" << name << std::endl; - bool export_ = exportedFunctions.count(name) > 0; o << int8_t(BinaryConsts::Named | - (BinaryConsts::Locals * (function && function->locals.size() > 0)) | - (BinaryConsts::Export * export_)); + (BinaryConsts::Locals * (function && function->locals.size() > 0))); emitString(name.str); - if (export_) emitString(exportedFunctions[name].str); // XXX addition to v8 binary format mapLocals(function); if (function->locals.size() > 0) { o << uint16_t(numLocalsByType[i32]) @@ -599,6 +594,19 @@ public: finishSection(start); } + void writeExports() { + if (wasm->exports.size() == 0) return; + if (debug) std::cerr << "== writeexports" << std::endl; + auto start = startSection(BinaryConsts::Section::ExportTable); + o << LEB128(wasm->exports.size()); + for (auto* curr : wasm->exports) { + if (debug) std::cerr << "write one" << std::endl; + o << LEB128(getFunctionIndex(curr->value)); + writeInlineString(curr->name.str); + } + finishSection(start); + } + void writeDataSegments() { if (wasm->memory.segments.size() == 0) return; uint32_t num = 0; @@ -1073,6 +1081,7 @@ public: else if (match(BinaryConsts::Section::ImportTable)) readImports(); else if (match(BinaryConsts::Section::FunctionSignatures)) readFunctionSignatures(); else if (match(BinaryConsts::Section::Functions)) readFunctions(); + else if (match(BinaryConsts::Section::ExportTable)) readExports(); else if (match(BinaryConsts::Section::DataSegments)) readDataSegments(); else if (match(BinaryConsts::Section::FunctionTable)) readFunctionTable(); else if (match(BinaryConsts::Section::End)) { @@ -1280,15 +1289,7 @@ public: bool named = data & BinaryConsts::Named; assert(named); bool locals = data & BinaryConsts::Locals; - bool export_ = data & BinaryConsts::Export; Name name = getString(); - if (export_) { // XXX addition to v8 binary format - Name exportName = getString(); - auto e = allocator.alloc<Export>(); - e->name = exportName; - e->value = name; - wasm.addExport(e); - } if (debug) std::cerr << "reading" << name << std::endl; auto func = allocator.alloc<Function>(); func->name = name; @@ -1325,6 +1326,22 @@ public: } } + void readExports() { + if (debug) std::cerr << "== readExports" << std::endl; + size_t num = getLEB128(); + if (debug) std::cerr << "num: " << num << std::endl; + for (size_t i = 0; i < num; i++) { + if (debug) std::cerr << "read one" << std::endl; + auto curr = allocator.alloc<Export>(); + auto index = getLEB128(); + assert(index < wasm.functions.size()); + curr->value = wasm.functions[index]->name; + assert(curr->value.is()); + curr->name = getInlineString(); + wasm.addExport(curr); + } + } + struct FunctionData { Function* func; size_t pos, size; |