summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/binary-reader-interpreter.cc37
-rw-r--r--src/interpreter.h41
-rw-r--r--src/tools/wasm-interp.cc162
3 files changed, 106 insertions, 134 deletions
diff --git a/src/binary-reader-interpreter.cc b/src/binary-reader-interpreter.cc
index 0acd40fe..6ddc9f52 100644
--- a/src/binary-reader-interpreter.cc
+++ b/src/binary-reader-interpreter.cc
@@ -259,8 +259,7 @@ class BinaryReaderInterpreter : public BinaryReaderNop {
Index item_index,
StringSlice name);
- PrintErrorCallback MakePrintErrorCallback();
- static void OnHostImportPrintError(const char* msg, void* user_data);
+ HostImportDelegate::ErrorCallback MakePrintErrorCallback();
BinaryErrorHandler* error_handler = nullptr;
Environment* env = nullptr;
@@ -664,17 +663,9 @@ wabt::Result BinaryReaderInterpreter::AppendExport(Module* module,
return wabt::Result::Ok;
}
-// static
-void BinaryReaderInterpreter::OnHostImportPrintError(const char* msg,
- void* user_data) {
- static_cast<BinaryReaderInterpreter*>(user_data)->PrintError("%s", msg);
-}
-
-PrintErrorCallback BinaryReaderInterpreter::MakePrintErrorCallback() {
- PrintErrorCallback result;
- result.print_error = OnHostImportPrintError;
- result.user_data = this;
- return result;
+HostImportDelegate::ErrorCallback
+BinaryReaderInterpreter::MakePrintErrorCallback() {
+ return [this](const char* msg) { PrintError("%s", msg); };
}
wabt::Result BinaryReaderInterpreter::OnImportFunc(Index import_index,
@@ -691,10 +682,9 @@ wabt::Result BinaryReaderInterpreter::OnImportFunc(Index import_index,
import->func.sig_index);
env->EmplaceBackFunc(func);
- HostImportDelegate* host_delegate = &host_import_module->import_delegate;
FuncSignature* sig = env->GetFuncSignature(func->sig_index);
- CHECK_RESULT(host_delegate->import_func(
- import, func, sig, MakePrintErrorCallback(), host_delegate->user_data));
+ CHECK_RESULT(host_import_module->import_delegate->ImportFunc(
+ import, func, sig, MakePrintErrorCallback()));
assert(func->callback);
func_env_index = env->GetFuncCount() - 1;
@@ -731,9 +721,8 @@ wabt::Result BinaryReaderInterpreter::OnImportTable(Index import_index,
if (is_host_import) {
Table* table = env->EmplaceBackTable(*elem_limits);
- HostImportDelegate* host_delegate = &host_import_module->import_delegate;
- CHECK_RESULT(host_delegate->import_table(
- import, table, MakePrintErrorCallback(), host_delegate->user_data));
+ CHECK_RESULT(host_import_module->import_delegate->ImportTable(
+ import, table, MakePrintErrorCallback()));
CHECK_RESULT(CheckImportLimits(elem_limits, &table->limits));
@@ -767,9 +756,8 @@ wabt::Result BinaryReaderInterpreter::OnImportMemory(
if (is_host_import) {
Memory* memory = env->EmplaceBackMemory();
- HostImportDelegate* host_delegate = &host_import_module->import_delegate;
- CHECK_RESULT(host_delegate->import_memory(
- import, memory, MakePrintErrorCallback(), host_delegate->user_data));
+ CHECK_RESULT(host_import_module->import_delegate->ImportMemory(
+ import, memory, MakePrintErrorCallback()));
CHECK_RESULT(CheckImportLimits(page_limits, &memory->page_limits));
@@ -799,9 +787,8 @@ wabt::Result BinaryReaderInterpreter::OnImportGlobal(Index import_index,
if (is_host_import) {
Global* global = env->EmplaceBackGlobal(TypedValue(type), mutable_);
- HostImportDelegate* host_delegate = &host_import_module->import_delegate;
- CHECK_RESULT(host_delegate->import_global(
- import, global, MakePrintErrorCallback(), host_delegate->user_data));
+ CHECK_RESULT(host_import_module->import_delegate->ImportGlobal(
+ import, global, MakePrintErrorCallback()));
global_env_index = env->GetGlobalCount() - 1;
AppendExport(host_import_module, ExternalKind::Global, global_env_index,
diff --git a/src/interpreter.h b/src/interpreter.h
index 3f60bdbb..cd5c6b5b 100644
--- a/src/interpreter.h
+++ b/src/interpreter.h
@@ -19,6 +19,7 @@
#include <stdint.h>
+#include <functional>
#include <memory>
#include <vector>
@@ -253,30 +254,18 @@ struct Export {
Index index;
};
-struct PrintErrorCallback {
- void* user_data;
- void (*print_error)(const char* msg, void* user_data);
-};
-
-struct HostImportDelegate {
- void* user_data;
- ::wabt::Result (*import_func)(Import*,
- Func*,
- FuncSignature*,
- PrintErrorCallback,
- void* user_data);
- ::wabt::Result (*import_table)(Import*,
- Table*,
- PrintErrorCallback,
- void* user_data);
- ::wabt::Result (*import_memory)(Import*,
- Memory*,
- PrintErrorCallback,
- void* user_data);
- ::wabt::Result (*import_global)(Import*,
- Global*,
- PrintErrorCallback,
- void* user_data);
+class HostImportDelegate {
+ public:
+ typedef std::function<void(const char* msg)> ErrorCallback;
+
+ virtual ~HostImportDelegate() {}
+ virtual wabt::Result ImportFunc(Import*,
+ Func*,
+ FuncSignature*,
+ const ErrorCallback&) = 0;
+ virtual wabt::Result ImportTable(Import*, Table*, const ErrorCallback&) = 0;
+ virtual wabt::Result ImportMemory(Import*, Memory*, const ErrorCallback&) = 0;
+ virtual wabt::Result ImportGlobal(Import*, Global*, const ErrorCallback&) = 0;
};
struct Module {
@@ -308,9 +297,9 @@ struct DefinedModule : Module {
};
struct HostModule : Module {
- HostModule(const StringSlice& name);
+ explicit HostModule(const StringSlice& name);
- HostImportDelegate import_delegate;
+ std::unique_ptr<HostImportDelegate> import_delegate;
};
DefinedModule* Module::as_defined() {
diff --git a/src/tools/wasm-interp.cc b/src/tools/wasm-interp.cc
index f24f4030..a6679ec2 100644
--- a/src/tools/wasm-interp.cc
+++ b/src/tools/wasm-interp.cc
@@ -401,107 +401,103 @@ static interpreter::Result default_host_callback(const HostFunc* func,
WABT_PRINTF_STRING_SLICE_ARG((x).module_name) \
, WABT_PRINTF_STRING_SLICE_ARG((x).field_name)
-static void WABT_PRINTF_FORMAT(2, 3)
- print_error(PrintErrorCallback callback, const char* format, ...) {
- WABT_SNPRINTF_ALLOCA(buffer, length, format);
- callback.print_error(buffer, callback.user_data);
-}
-
-static wabt::Result spectest_import_func(Import* import,
- Func* func,
- FuncSignature* sig,
- PrintErrorCallback callback,
- void* user_data) {
- if (string_slice_eq_cstr(&import->field_name, "print")) {
- func->as_host()->callback = default_host_callback;
- return wabt::Result::Ok;
- } else {
- print_error(callback, "unknown host function import " PRIimport,
- PRINTF_IMPORT_ARG(*import));
- return wabt::Result::Error;
+class SpectestHostImportDelegate : public HostImportDelegate {
+ public:
+ wabt::Result ImportFunc(Import* import,
+ Func* func,
+ FuncSignature* func_sig,
+ const ErrorCallback& callback) override {
+ if (string_slice_eq_cstr(&import->field_name, "print")) {
+ func->as_host()->callback = default_host_callback;
+ return wabt::Result::Ok;
+ } else {
+ PrintError(callback, "unknown host function import " PRIimport,
+ PRINTF_IMPORT_ARG(*import));
+ return wabt::Result::Error;
+ }
}
-}
-static wabt::Result spectest_import_table(Import* import,
- Table* table,
- PrintErrorCallback callback,
- void* user_data) {
- if (string_slice_eq_cstr(&import->field_name, "table")) {
- table->limits.has_max = true;
- table->limits.initial = 10;
- table->limits.max = 20;
- return wabt::Result::Ok;
- } else {
- print_error(callback, "unknown host table import " PRIimport,
- PRINTF_IMPORT_ARG(*import));
- return wabt::Result::Error;
+ wabt::Result ImportTable(Import* import,
+ Table* table,
+ const ErrorCallback& callback) override {
+ if (string_slice_eq_cstr(&import->field_name, "table")) {
+ table->limits.has_max = true;
+ table->limits.initial = 10;
+ table->limits.max = 20;
+ return wabt::Result::Ok;
+ } else {
+ PrintError(callback, "unknown host table import " PRIimport,
+ PRINTF_IMPORT_ARG(*import));
+ return wabt::Result::Error;
+ }
}
-}
-static wabt::Result spectest_import_memory(Import* import,
- Memory* memory,
- PrintErrorCallback callback,
- void* user_data) {
- if (string_slice_eq_cstr(&import->field_name, "memory")) {
- memory->page_limits.has_max = true;
- memory->page_limits.initial = 1;
- memory->page_limits.max = 2;
- memory->data.resize(memory->page_limits.initial * WABT_MAX_PAGES);
- return wabt::Result::Ok;
- } else {
- print_error(callback, "unknown host memory import " PRIimport,
- PRINTF_IMPORT_ARG(*import));
- return wabt::Result::Error;
+ wabt::Result ImportMemory(Import* import,
+ Memory* memory,
+ const ErrorCallback& callback) override {
+ if (string_slice_eq_cstr(&import->field_name, "memory")) {
+ memory->page_limits.has_max = true;
+ memory->page_limits.initial = 1;
+ memory->page_limits.max = 2;
+ memory->data.resize(memory->page_limits.initial * WABT_MAX_PAGES);
+ return wabt::Result::Ok;
+ } else {
+ PrintError(callback, "unknown host memory import " PRIimport,
+ PRINTF_IMPORT_ARG(*import));
+ return wabt::Result::Error;
+ }
}
-}
-static wabt::Result spectest_import_global(Import* import,
- Global* global,
- PrintErrorCallback callback,
- void* user_data) {
- if (string_slice_eq_cstr(&import->field_name, "global")) {
- switch (global->typed_value.type) {
- case Type::I32:
- global->typed_value.value.i32 = 666;
- break;
+ wabt::Result ImportGlobal(Import* import,
+ Global* global,
+ const ErrorCallback& callback) override {
+ if (string_slice_eq_cstr(&import->field_name, "global")) {
+ switch (global->typed_value.type) {
+ case Type::I32:
+ global->typed_value.value.i32 = 666;
+ break;
- case Type::F32: {
- float value = 666.6f;
- memcpy(&global->typed_value.value.f32_bits, &value, sizeof(value));
- break;
- }
+ case Type::F32: {
+ float value = 666.6f;
+ memcpy(&global->typed_value.value.f32_bits, &value, sizeof(value));
+ break;
+ }
- case Type::I64:
- global->typed_value.value.i64 = 666;
- break;
+ case Type::I64:
+ global->typed_value.value.i64 = 666;
+ break;
- case Type::F64: {
- double value = 666.6;
- memcpy(&global->typed_value.value.f64_bits, &value, sizeof(value));
- break;
+ case Type::F64: {
+ double value = 666.6;
+ memcpy(&global->typed_value.value.f64_bits, &value, sizeof(value));
+ break;
+ }
+
+ default:
+ PrintError(callback, "bad type for host global import " PRIimport,
+ PRINTF_IMPORT_ARG(*import));
+ return wabt::Result::Error;
}
- default:
- print_error(callback, "bad type for host global import " PRIimport,
- PRINTF_IMPORT_ARG(*import));
- return wabt::Result::Error;
+ return wabt::Result::Ok;
+ } else {
+ PrintError(callback, "unknown host global import " PRIimport,
+ PRINTF_IMPORT_ARG(*import));
+ return wabt::Result::Error;
}
+ }
- return wabt::Result::Ok;
- } else {
- print_error(callback, "unknown host global import " PRIimport,
- PRINTF_IMPORT_ARG(*import));
- return wabt::Result::Error;
+ private:
+ void PrintError(const ErrorCallback& callback, const char* format, ...) {
+ WABT_SNPRINTF_ALLOCA(buffer, length, format);
+ callback(buffer);
}
-}
+};
static void init_environment(Environment* env) {
HostModule* host_module =
env->AppendHostModule(string_slice_from_cstr("spectest"));
- host_module->import_delegate.import_func = spectest_import_func;
- host_module->import_delegate.import_table = spectest_import_table;
- host_module->import_delegate.import_memory = spectest_import_memory;
- host_module->import_delegate.import_global = spectest_import_global;
+ host_module->import_delegate.reset(new SpectestHostImportDelegate());
}
static wabt::Result read_and_run_module(const char* module_filename) {