diff options
author | Ben Smith <binjimin@gmail.com> | 2017-08-17 20:34:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-17 20:34:08 -0700 |
commit | 477cd75c3924e84dd1a8338ddfc0318308ca9d52 (patch) | |
tree | b0e727e3992773ab3aaa5e70baa76ee3ed33f9d1 /src/interpreter.cc | |
parent | bc3deb3288252866d9b571cd28c4104b9ffffd56 (diff) | |
download | wabt-477cd75c3924e84dd1a8338ddfc0318308ca9d52.tar.gz wabt-477cd75c3924e84dd1a8338ddfc0318308ca9d52.tar.bz2 wabt-477cd75c3924e84dd1a8338ddfc0318308ca9d52.zip |
Refactor interpreter imports (#592)
* Create Import derived classes instead of using union
* Use cast/dyn_cast for interpreter Func and Module (Defined vs. Host)
* Remove OnImport callback in BinaryReaderInterpreter; do all the work
in the kind-specific callbacks (e.g. OnImportFunc)
Diffstat (limited to 'src/interpreter.cc')
-rw-r--r-- | src/interpreter.cc | 57 |
1 files changed, 13 insertions, 44 deletions
diff --git a/src/interpreter.cc b/src/interpreter.cc index 793d1aef..51d377a7 100644 --- a/src/interpreter.cc +++ b/src/interpreter.cc @@ -24,6 +24,7 @@ #include <type_traits> #include <vector> +#include "cast.h" #include "stream.h" namespace wabt { @@ -107,40 +108,6 @@ FuncSignature::FuncSignature(Index param_count, : param_types(param_types, param_types + param_count), result_types(result_types, result_types + result_count) {} -Import::Import() : kind(ExternalKind::Func) { - func.sig_index = kInvalidIndex; -} - -Import::Import(Import&& other) { - *this = std::move(other); -} - -Import& Import::operator=(Import&& other) { - kind = other.kind; - module_name = std::move(other.module_name); - field_name = std::move(other.field_name); - switch (kind) { - case ExternalKind::Func: - func.sig_index = other.func.sig_index; - break; - case ExternalKind::Table: - table.limits = other.table.limits; - break; - case ExternalKind::Memory: - memory.limits = other.memory.limits; - break; - case ExternalKind::Global: - global.type = other.global.type; - global.mutable_ = other.global.mutable_; - break; - case ExternalKind::Except: - // TODO(karlschimpf) Define - WABT_FATAL("Import::operator=() not implemented for exceptions"); - break; - } - return *this; -} - Module::Module(bool is_host) : memory_index(kInvalidIndex), table_index(kInvalidIndex), @@ -1089,8 +1056,9 @@ Result Thread::RunFunction(Index func_index, Result result = PushArgs(sig, args); if (result == Result::Ok) { - result = func->is_host ? CallHost(func->as_host()) - : RunDefinedFunction(func->as_defined()->offset); + result = func->is_host + ? CallHost(cast<HostFunc>(func)) + : RunDefinedFunction(cast<DefinedFunc>(func)->offset); if (result == Result::Ok) CopyResults(sig, out_results); } @@ -1110,9 +1078,9 @@ Result Thread::TraceFunction(Index func_index, Result result = PushArgs(sig, args); if (result == Result::Ok) { - result = func->is_host - ? CallHost(func->as_host()) - : TraceDefinedFunction(func->as_defined()->offset, stream); + result = func->is_host ? CallHost(cast<HostFunc>(func)) + : TraceDefinedFunction( + cast<DefinedFunc>(func)->offset, stream); if (result == Result::Ok) CopyResults(sig, out_results); } @@ -1302,17 +1270,17 @@ Result Thread::Run(int num_instructions, IstreamOffset* call_stack_return_top) { TRAP_UNLESS(env_->FuncSignaturesAreEqual(func->sig_index, sig_index), IndirectCallSignatureMismatch); if (func->is_host) { - CallHost(func->as_host()); + CallHost(cast<HostFunc>(func)); } else { CHECK_TRAP(PushCall(pc)); - GOTO(func->as_defined()->offset); + GOTO(cast<DefinedFunc>(func)->offset); } break; } case Opcode::CallHost: { Index func_index = read_u32(&pc); - CallHost(env_->funcs_[func_index]->as_host()); + CallHost(cast<HostFunc>(env_->funcs_[func_index].get())); break; } @@ -2713,8 +2681,9 @@ void Environment::Disassemble(Stream* stream, void Environment::DisassembleModule(Stream* stream, Module* module) { assert(!module->is_host); - Disassemble(stream, module->as_defined()->istream_start, - module->as_defined()->istream_end); + auto* defined_module = cast<DefinedModule>(module); + Disassemble(stream, defined_module->istream_start, + defined_module->istream_end); } } // namespace interpreter |