[ewasm] Support compiling data subobjects

This commit is contained in:
Alex Beregszaszi 2020-10-30 18:03:27 +00:00
parent e41700e5f7
commit 6e11754c8b
4 changed files with 19 additions and 1 deletions

View File

@ -315,6 +315,14 @@ bytes BinaryTransform::run(Module const& _module)
size_t const offset = ret.size() - length;
subModulePosAndSize[name] = {offset, length};
}
for (auto const& [name, data]: _module.customSections)
{
size_t const length = data.size();
ret += customSection(name, data);
// Skip all the previous sections and the size field of this current custom section.
size_t const offset = ret.size() - length;
subModulePosAndSize[name] = {offset, length};
}
BinaryTransform bt(
move(globalIDs),

View File

@ -44,6 +44,11 @@ string TextTransform::run(wasm::Module const& _module)
" ;; sub-module \"" +
sub.first +
"\" will be encoded as custom section in binary here, but is skipped in text mode.\n";
for (auto const& data: _module.customSections)
ret +=
" ;; custom-section \"" +
data.first +
"\" will be encoded as custom section in binary here, but is skipped in text mode.\n";
for (wasm::FunctionImport const& imp: _module.imports)
{
ret += " (import \"" + imp.module + "\" \"" + imp.externalName + "\" (func $" + imp.internalName;

View File

@ -21,6 +21,8 @@
#pragma once
#include <libsolutil/Common.h>
#include <variant>
#include <string>
#include <vector>
@ -108,6 +110,7 @@ struct Module
std::vector<FunctionImport> imports;
std::vector<FunctionDefinition> functions;
std::map<std::string, Module> subModules;
std::map<std::string, bytes> customSections;
};
}

View File

@ -51,8 +51,10 @@ wasm::Module WasmObjectCompiler::run(Object& _object)
for (auto& subNode: _object.subObjects)
if (Object* subObject = dynamic_cast<Object*>(subNode.get()))
module.subModules[subObject->name.str()] = run(*subObject);
else if (Data* subObject = dynamic_cast<Data*>(subNode.get()))
module.customSections[subObject->name.str()] = subObject->data;
else
yulAssert(false, "Data is not yet supported for Wasm.");
yulAssert(false, "");
return module;
}