From 9fb040740ec3ca1c298ecd95f21c071ab1ab170e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 4 Apr 2016 14:34:02 -0700 Subject: add apis for accessing module elements --- src/wasm.h | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/wasm.h') diff --git a/src/wasm.h b/src/wasm.h index af767b688..a07ab3079 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1114,23 +1114,41 @@ public: class Module { public: - // internal wasm contents (don't access these from outside; use add*() and the *Map objects) + // wasm contents (generally you shouldn't access these from outside, except maybe for iterating; use add*() and the get() functions) std::vector functionTypes; std::vector imports; std::vector exports; std::vector functions; - // publicly-accessible content + Table table; + Memory memory; + Name start; + +private: + // TODO: add a build option where Names are just indices, and then these methods are not needed std::map functionTypesMap; std::map importsMap; std::map exportsMap; std::map functionsMap; - Table table; - Memory memory; - Name start; +public: Module() : functionTypeIndex(0), importIndex(0), exportIndex(0), functionIndex(0) {} + FunctionType* getFunctionType(size_t i) { assert(i < functionTypes.size());return functionTypes[i]; } + Import* getImport(size_t i) { assert(i < imports.size()); return imports[i]; } + Export* getExport(size_t i) { assert(i < exports.size()); return exports[i]; } + Function* getFunction(size_t i) { assert(i < functions.size()); return functions[i]; } + + FunctionType* getFunctionType(Name name) { assert(functionTypesMap[name]); return functionTypesMap[name]; } + Import* getImport(Name name) { assert(importsMap[name]); return importsMap[name]; } + Export* getExport(Name name) { assert(exportsMap[name]); return exportsMap[name]; } + Function* getFunction(Name name) { assert(functionsMap[name]); return functionsMap[name]; } + + FunctionType* checkFunctionType(Name name) { if (functionTypesMap.find(name) == functionTypesMap.end()) return nullptr; return functionTypesMap[name]; } + Import* checkImport(Name name) { if (importsMap.find(name) == importsMap.end()) return nullptr; return importsMap[name]; } + Export* checkExport(Name name) { if (exportsMap.find(name) == exportsMap.end()) return nullptr; return exportsMap[name]; } + Function* checkFunction(Name name) { if (functionsMap.find(name) == functionsMap.end()) return nullptr; return functionsMap[name]; } + void addFunctionType(FunctionType* curr) { Name numericName = Name::fromInt(functionTypeIndex); // TODO: remove all these, assert on names already existing, do numeric stuff in wasm-s-parser etc. if (curr->name.isNull()) { -- cgit v1.2.3