diff --git a/src/ir/import-name.h b/src/ir/import-names.h similarity index 61% rename from src/ir/import-name.h rename to src/ir/import-names.h index 81fd0a3e768..3be646cc2c3 100644 --- a/src/ir/import-name.h +++ b/src/ir/import-names.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef wasm_ir_import_name_h -#define wasm_ir_import_name_h +#ifndef wasm_ir_import_names_h +#define wasm_ir_import_names_h #include @@ -26,8 +26,24 @@ namespace wasm { struct ImportNames { Name module; Name name; + + bool operator==(const ImportNames& other) const { + return module == other.module && name == other.name; + } }; } // namespace wasm -#endif // wasm_ir_import_name_h +namespace std { + +// TODO? +template<> struct hash { + size_t operator()(const wasm::ImportNames& importNames) const { + return std::hash{}(importNames.module) ^ + std::hash{}(importNames.name); + } +}; + +} // namespace std + +#endif // wasm_ir_import_names_h diff --git a/src/ir/import-utils.h b/src/ir/import-utils.h index 481d62af7f1..ad14eebca39 100644 --- a/src/ir/import-utils.h +++ b/src/ir/import-utils.h @@ -17,7 +17,7 @@ #ifndef wasm_ir_import_h #define wasm_ir_import_h -#include "ir/import-name.h" +#include "ir/import-names.h" #include "ir/runtime-table.h" #include "literal.h" #include "wasm.h" diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index fb57865999d..5f0dd512353 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -68,26 +68,6 @@ bool isNullableAndMutable(Expression* ref, Index fieldIndex) { // the output. #define RECOMMENDATION "\n recommendation: " -class EvallingImportResolver : public ImportResolver { -public: - EvallingImportResolver() : stubLiteral({Literal(0)}) {}; - - // Return an unused stub value. We throw FailToEvalException on reading any - // imported globals. We ignore the type and return an i32 literal since some - // types can't be created anyway (e.g. ref none). - Literals* getGlobalOrNull(ImportNames name, Type type) const override { - return &stubLiteral; - } - - RuntimeTable* getTableOrNull(ImportNames name, - const Table& type) const override { - throw FailToEvalException{"Imported table access."}; - } - -private: - mutable Literals stubLiteral; -}; - class EvallingRuntimeTable : public RuntimeTable { public: // TODO: putting EvallingModuleRunner into its own header would allow us to @@ -166,6 +146,39 @@ class EvallingRuntimeTable : public RuntimeTable { const std::function makeFuncData; }; +class EvallingImportResolver : public ImportResolver { +public: + EvallingImportResolver(const bool& instanceInitialized, + const Module& wasm, + std::function makeFuncData) + : stubLiteral({Literal(0)}), instanceInitialized(instanceInitialized), + wasm(wasm), makeFuncData(makeFuncData) {}; + + // Return an unused stub value. We throw FailToEvalException on reading any + // imported globals. We ignore the type and return an i32 literal since some + // types can't be created anyway (e.g. ref none). + Literals* getGlobalOrNull(ImportNames name, Type type) const override { + return &stubLiteral; + } + + RuntimeTable* getTableOrNull(ImportNames name, + const Table& type) const override { + auto [it, inserted] = + tables.emplace(name, + std::make_unique( + type, instanceInitialized, wasm, makeFuncData)); + return it->second.get(); + } + +private: + mutable Literals stubLiteral; + mutable std::unordered_map> + tables; + const bool& instanceInitialized; + const Module& wasm; + const std::function makeFuncData; +}; + class EvallingModuleRunner : public ModuleRunnerBase { public: EvallingModuleRunner( @@ -176,17 +189,11 @@ class EvallingModuleRunner : public ModuleRunnerBase { : ModuleRunnerBase( wasm, externalInterface, - std::make_shared(), - linkedInstances_, - // TODO: Only use EvallingRuntimeTable for table imports. We can use - // RealRuntimeTable for non-imported tables. - [this, &instanceInitialized](Literal initial, Table table) { - return std::make_unique( - table, - instanceInitialized, - this->wasm, - [this](Name name, Type type) { return makeFuncData(name, type); }); - }) {} + std::make_shared( + instanceInitialized, + wasm, + [this](Name name, Type type) { return makeFuncData(name, type); }), + linkedInstances_) {} Flow visitGlobalGet(GlobalGet* curr) { // Error on reads of imported globals. @@ -1156,6 +1163,10 @@ EvalCtorOutcome evalCtor(EvallingModuleRunner& instance, std::cout << " ...stopping due to non-constant func\n"; } break; + } catch (TrapException& trap) { + if (!quiet) { + std::cout << " ...stopping due to trap\n"; + } } if (flow.breakTo == NONCONSTANT_FLOW) { diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index cf51b3d8f72..a9fb9eed83a 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -3173,25 +3173,15 @@ class ModuleRunnerBase : public ExpressionRunner { // Like `allGlobals`. Keyed by internal name. All tables including imports. std::unordered_map allTables; - using CreateTableFunc = std::unique_ptr(Literal, Table); - ModuleRunnerBase( Module& wasm, ExternalInterface* externalInterface, std::shared_ptr importResolver, - std::map> linkedInstances_ = {}, - std::function createTable = {}) + std::map> linkedInstances_ = {}) : ExpressionRunner(&wasm), wasm(wasm), externalInterface(externalInterface), linkedInstances(std::move(linkedInstances_)), - importResolver(std::move(importResolver)), - createTable( - createTable != nullptr - ? std::move(createTable) - : static_cast>( - [](Literal initial, Table t) -> std::unique_ptr { - return std::make_unique(initial, t); - })) { + importResolver(std::move(importResolver)) { // Set up a single shared CurrContinuations for all these linked instances, // reusing one if it exists. std::shared_ptr shared; @@ -3485,8 +3475,8 @@ class ModuleRunnerBase : public ExpressionRunner { "We only support nullable tables today"); auto null = Literal::makeNull(table->type.getHeapType()); - auto& runtimeTable = - definedTables.emplace_back(createTable(null, *table)); + auto& runtimeTable = definedTables.emplace_back( + std::make_unique(null, *table)); auto [_, inserted] = allTables.try_emplace(table->name, runtimeTable.get()); (void)inserted; // for noassert builds @@ -5194,7 +5184,6 @@ class ModuleRunnerBase : public ExpressionRunner { ExternalInterface* externalInterface; std::map> linkedInstances; std::shared_ptr importResolver; - std::function createTable; }; class ModuleRunner : public ModuleRunnerBase { diff --git a/src/wasm.h b/src/wasm.h index 2904bcefd5f..012bf29a4ea 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -33,7 +33,7 @@ #include #include -#include "ir/import-name.h" +#include "ir/import-names.h" #include "literal.h" #include "support/index.h" #include "support/mixed_arena.h"