summaryrefslogtreecommitdiff
path: root/src/binary-reader-ast.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/binary-reader-ast.cc')
-rw-r--r--src/binary-reader-ast.cc945
1 files changed, 432 insertions, 513 deletions
diff --git a/src/binary-reader-ast.cc b/src/binary-reader-ast.cc
index 39c9e8fa..de0a2a93 100644
--- a/src/binary-reader-ast.cc
+++ b/src/binary-reader-ast.cc
@@ -25,7 +25,7 @@
#include <vector>
#include "ast.h"
-#include "binary-reader.h"
+#include "binary-reader-nop.h"
#include "common.h"
#define CHECK_RESULT(expr) \
@@ -49,7 +49,155 @@ struct LabelNode {
LabelNode::LabelNode(LabelType label_type, Expr** first)
: label_type(label_type), first(first), last(nullptr) {}
-struct Context {
+class BinaryReaderAST : public BinaryReaderNop {
+ public:
+ BinaryReaderAST(Module* out_module, BinaryErrorHandler* error_handler);
+
+ virtual bool OnError(const char* message);
+
+ virtual Result OnTypeCount(uint32_t count);
+ virtual Result OnType(uint32_t index,
+ uint32_t param_count,
+ Type* param_types,
+ uint32_t result_count,
+ Type* result_types);
+
+ virtual Result OnImportCount(uint32_t count);
+ virtual Result OnImport(uint32_t index,
+ StringSlice module_name,
+ StringSlice field_name);
+ virtual Result OnImportFunc(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t func_index,
+ uint32_t sig_index);
+ virtual Result OnImportTable(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t table_index,
+ Type elem_type,
+ const Limits* elem_limits);
+ virtual Result OnImportMemory(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t memory_index,
+ const Limits* page_limits);
+ virtual Result OnImportGlobal(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t global_index,
+ Type type,
+ bool mutable_);
+
+ virtual Result OnFunctionCount(uint32_t count);
+ virtual Result OnFunction(uint32_t index, uint32_t sig_index);
+
+ virtual Result OnTableCount(uint32_t count);
+ virtual Result OnTable(uint32_t index,
+ Type elem_type,
+ const Limits* elem_limits);
+
+ virtual Result OnMemoryCount(uint32_t count);
+ virtual Result OnMemory(uint32_t index, const Limits* limits);
+
+ virtual Result OnGlobalCount(uint32_t count);
+ virtual Result BeginGlobal(uint32_t index, Type type, bool mutable_);
+ virtual Result BeginGlobalInitExpr(uint32_t index);
+ virtual Result EndGlobalInitExpr(uint32_t index);
+
+ virtual Result OnExportCount(uint32_t count);
+ virtual Result OnExport(uint32_t index,
+ ExternalKind kind,
+ uint32_t item_index,
+ StringSlice name);
+
+ virtual Result OnStartFunction(uint32_t func_index);
+
+ virtual Result OnFunctionBodyCount(uint32_t count);
+ virtual Result BeginFunctionBody(uint32_t index);
+ virtual Result OnLocalDecl(uint32_t decl_index, uint32_t count, Type type);
+
+ virtual Result OnBinaryExpr(Opcode opcode);
+ virtual Result OnBlockExpr(uint32_t num_types, Type* sig_types);
+ virtual Result OnBrExpr(uint32_t depth);
+ virtual Result OnBrIfExpr(uint32_t depth);
+ virtual Result OnBrTableExpr(uint32_t num_targets,
+ uint32_t* target_depths,
+ uint32_t default_target_depth);
+ virtual Result OnCallExpr(uint32_t func_index);
+ virtual Result OnCallIndirectExpr(uint32_t sig_index);
+ virtual Result OnCompareExpr(Opcode opcode);
+ virtual Result OnConvertExpr(Opcode opcode);
+ virtual Result OnDropExpr();
+ virtual Result OnElseExpr();
+ virtual Result OnEndExpr();
+ virtual Result OnF32ConstExpr(uint32_t value_bits);
+ virtual Result OnF64ConstExpr(uint64_t value_bits);
+ virtual Result OnGetGlobalExpr(uint32_t global_index);
+ virtual Result OnGetLocalExpr(uint32_t local_index);
+ virtual Result OnGrowMemoryExpr();
+ virtual Result OnI32ConstExpr(uint32_t value);
+ virtual Result OnI64ConstExpr(uint64_t value);
+ virtual Result OnIfExpr(uint32_t num_types, Type* sig_types);
+ virtual Result OnLoadExpr(Opcode opcode,
+ uint32_t alignment_log2,
+ uint32_t offset);
+ virtual Result OnLoopExpr(uint32_t num_types, Type* sig_types);
+ virtual Result OnCurrentMemoryExpr();
+ virtual Result OnNopExpr();
+ virtual Result OnReturnExpr();
+ virtual Result OnSelectExpr();
+ virtual Result OnSetGlobalExpr(uint32_t global_index);
+ virtual Result OnSetLocalExpr(uint32_t local_index);
+ virtual Result OnStoreExpr(Opcode opcode,
+ uint32_t alignment_log2,
+ uint32_t offset);
+ virtual Result OnTeeLocalExpr(uint32_t local_index);
+ virtual Result OnUnaryExpr(Opcode opcode);
+ virtual Result OnUnreachableExpr();
+ virtual Result EndFunctionBody(uint32_t index);
+
+ virtual Result OnElemSegmentCount(uint32_t count);
+ virtual Result BeginElemSegment(uint32_t index, uint32_t table_index);
+ virtual Result BeginElemSegmentInitExpr(uint32_t index);
+ virtual Result EndElemSegmentInitExpr(uint32_t index);
+ virtual Result OnElemSegmentFunctionIndexCount(uint32_t index,
+ uint32_t count);
+ virtual Result OnElemSegmentFunctionIndex(uint32_t index,
+ uint32_t func_index);
+
+ virtual Result OnDataSegmentCount(uint32_t count);
+ virtual Result BeginDataSegment(uint32_t index, uint32_t memory_index);
+ virtual Result BeginDataSegmentInitExpr(uint32_t index);
+ virtual Result EndDataSegmentInitExpr(uint32_t index);
+ virtual Result OnDataSegmentData(uint32_t index,
+ const void* data,
+ uint32_t size);
+
+ virtual Result OnFunctionNamesCount(uint32_t num_functions);
+ virtual Result OnFunctionName(uint32_t function_index,
+ StringSlice function_name);
+ virtual Result OnLocalNameLocalCount(uint32_t function_index,
+ uint32_t num_locals);
+ virtual Result OnLocalName(uint32_t function_index,
+ uint32_t local_index,
+ StringSlice local_name);
+
+ virtual Result OnInitExprF32ConstExpr(uint32_t index, uint32_t value);
+ virtual Result OnInitExprF64ConstExpr(uint32_t index, uint64_t value);
+ virtual Result OnInitExprGetGlobalExpr(uint32_t index, uint32_t global_index);
+ virtual Result OnInitExprI32ConstExpr(uint32_t index, uint32_t value);
+ virtual Result OnInitExprI64ConstExpr(uint32_t index, uint64_t value);
+
+ private:
+ bool HandleError(uint32_t offset, const char* message);
+ void PrintError(const char* format, ...);
+ void PushLabel(LabelType label_type, Expr** first);
+ Result PopLabel();
+ Result GetLabelAt(LabelNode** label, uint32_t depth);
+ Result TopLabel(LabelNode** label);
+ Result AppendExpr(Expr* expr);
+
BinaryErrorHandler* error_handler = nullptr;
Module* module = nullptr;
@@ -59,50 +207,50 @@ struct Context {
Expr** current_init_expr = nullptr;
};
-} // namespace
+BinaryReaderAST::BinaryReaderAST(Module* out_module,
+ BinaryErrorHandler* error_handler)
+ : error_handler(error_handler), module(out_module) {}
-static bool handle_error(Context* ctx, uint32_t offset, const char* message);
-
-static void WABT_PRINTF_FORMAT(2, 3)
- print_error(Context* ctx, const char* format, ...) {
+void WABT_PRINTF_FORMAT(2, 3) BinaryReaderAST::PrintError(const char* format,
+ ...) {
WABT_SNPRINTF_ALLOCA(buffer, length, format);
- handle_error(ctx, WABT_UNKNOWN_OFFSET, buffer);
+ HandleError(WABT_UNKNOWN_OFFSET, buffer);
}
-static void push_label(Context* ctx, LabelType label_type, Expr** first) {
- ctx->max_depth++;
- ctx->label_stack.emplace_back(label_type, first);
+void BinaryReaderAST::PushLabel(LabelType label_type, Expr** first) {
+ max_depth++;
+ label_stack.emplace_back(label_type, first);
}
-static Result pop_label(Context* ctx) {
- if (ctx->label_stack.size() == 0) {
- print_error(ctx, "popping empty label stack");
+Result BinaryReaderAST::PopLabel() {
+ if (label_stack.size() == 0) {
+ PrintError("popping empty label stack");
return Result::Error;
}
- ctx->max_depth--;
- ctx->label_stack.pop_back();
+ max_depth--;
+ label_stack.pop_back();
return Result::Ok;
}
-static Result get_label_at(Context* ctx, LabelNode** label, uint32_t depth) {
- if (depth >= ctx->label_stack.size()) {
- print_error(ctx, "accessing stack depth: %u >= max: %" PRIzd, depth,
- ctx->label_stack.size());
+Result BinaryReaderAST::GetLabelAt(LabelNode** label, uint32_t depth) {
+ if (depth >= label_stack.size()) {
+ PrintError("accessing stack depth: %u >= max: %" PRIzd, depth,
+ label_stack.size());
return Result::Error;
}
- *label = &ctx->label_stack[ctx->label_stack.size() - depth - 1];
+ *label = &label_stack[label_stack.size() - depth - 1];
return Result::Ok;
}
-static Result top_label(Context* ctx, LabelNode** label) {
- return get_label_at(ctx, label, 0);
+Result BinaryReaderAST::TopLabel(LabelNode** label) {
+ return GetLabelAt(label, 0);
}
-static Result append_expr(Context* ctx, Expr* expr) {
+Result BinaryReaderAST::AppendExpr(Expr* expr) {
LabelNode* label;
- if (WABT_FAILED(top_label(ctx, &label))) {
+ if (WABT_FAILED(TopLabel(&label))) {
delete expr;
return Result::Error;
}
@@ -115,154 +263,132 @@ static Result append_expr(Context* ctx, Expr* expr) {
return Result::Ok;
}
-static bool handle_error(Context* ctx, uint32_t offset, const char* message) {
- if (ctx->error_handler->on_error) {
- return ctx->error_handler->on_error(offset, message,
- ctx->error_handler->user_data);
+bool BinaryReaderAST::HandleError(uint32_t offset, const char* message) {
+ if (error_handler->on_error) {
+ return error_handler->on_error(offset, message, error_handler->user_data);
}
return false;
}
-static bool on_error(BinaryReaderContext* reader_context, const char* message) {
- Context* ctx = static_cast<Context*>(reader_context->user_data);
- return handle_error(ctx, reader_context->offset, message);
+bool BinaryReaderAST::OnError(const char* message) {
+ return HandleError(state->offset, message);
}
-static Result on_signature_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->func_types.reserve(count);
+Result BinaryReaderAST::OnTypeCount(uint32_t count) {
+ module->func_types.reserve(count);
return Result::Ok;
}
-static Result on_signature(uint32_t index,
- uint32_t param_count,
- Type* param_types,
- uint32_t result_count,
- Type* result_types,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnType(uint32_t index,
+ uint32_t param_count,
+ Type* param_types,
+ uint32_t result_count,
+ Type* result_types) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::FuncType;
field->func_type = new FuncType();
FuncType* func_type = field->func_type;
func_type->sig.param_types.assign(param_types, param_types + param_count);
func_type->sig.result_types.assign(result_types, result_types + result_count);
- ctx->module->func_types.push_back(func_type);
+ module->func_types.push_back(func_type);
return Result::Ok;
}
-static Result on_import_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->imports.reserve(count);
+Result BinaryReaderAST::OnImportCount(uint32_t count) {
+ module->imports.reserve(count);
return Result::Ok;
}
-static Result on_import(uint32_t index,
- StringSlice module_name,
- StringSlice field_name,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
-
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnImport(uint32_t index,
+ StringSlice module_name,
+ StringSlice field_name) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Import;
field->import = new Import();
Import* import = field->import;
import->module_name = dup_string_slice(module_name);
import->field_name = dup_string_slice(field_name);
- ctx->module->imports.push_back(import);
+ module->imports.push_back(import);
return Result::Ok;
}
-static Result on_import_func(uint32_t import_index,
- StringSlice module_name,
- StringSlice field_name,
- uint32_t func_index,
- uint32_t sig_index,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(import_index == ctx->module->imports.size() - 1);
- Import* import = ctx->module->imports[import_index];
+Result BinaryReaderAST::OnImportFunc(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t func_index,
+ uint32_t sig_index) {
+ assert(import_index == module->imports.size() - 1);
+ Import* import = module->imports[import_index];
import->kind = ExternalKind::Func;
import->func = new Func();
import->func->decl.has_func_type = true;
import->func->decl.type_var.type = VarType::Index;
import->func->decl.type_var.index = sig_index;
- import->func->decl.sig = ctx->module->func_types[sig_index]->sig;
+ import->func->decl.sig = module->func_types[sig_index]->sig;
- ctx->module->funcs.push_back(import->func);
- ctx->module->num_func_imports++;
+ module->funcs.push_back(import->func);
+ module->num_func_imports++;
return Result::Ok;
}
-static Result on_import_table(uint32_t import_index,
- StringSlice module_name,
- StringSlice field_name,
- uint32_t table_index,
- Type elem_type,
- const Limits* elem_limits,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(import_index == ctx->module->imports.size() - 1);
- Import* import = ctx->module->imports[import_index];
+Result BinaryReaderAST::OnImportTable(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t table_index,
+ Type elem_type,
+ const Limits* elem_limits) {
+ assert(import_index == module->imports.size() - 1);
+ Import* import = module->imports[import_index];
import->kind = ExternalKind::Table;
import->table = new Table();
import->table->elem_limits = *elem_limits;
- ctx->module->tables.push_back(import->table);
- ctx->module->num_table_imports++;
+ module->tables.push_back(import->table);
+ module->num_table_imports++;
return Result::Ok;
}
-static Result on_import_memory(uint32_t import_index,
- StringSlice module_name,
- StringSlice field_name,
- uint32_t memory_index,
- const Limits* page_limits,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(import_index == ctx->module->imports.size() - 1);
- Import* import = ctx->module->imports[import_index];
+Result BinaryReaderAST::OnImportMemory(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t memory_index,
+ const Limits* page_limits) {
+ assert(import_index == module->imports.size() - 1);
+ Import* import = module->imports[import_index];
import->kind = ExternalKind::Memory;
import->memory = new Memory();
import->memory->page_limits = *page_limits;
- ctx->module->memories.push_back(import->memory);
- ctx->module->num_memory_imports++;
+ module->memories.push_back(import->memory);
+ module->num_memory_imports++;
return Result::Ok;
}
-static Result on_import_global(uint32_t import_index,
- StringSlice module_name,
- StringSlice field_name,
- uint32_t global_index,
- Type type,
- bool mutable_,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(import_index == ctx->module->imports.size() - 1);
- Import* import = ctx->module->imports[import_index];
+Result BinaryReaderAST::OnImportGlobal(uint32_t import_index,
+ StringSlice module_name,
+ StringSlice field_name,
+ uint32_t global_index,
+ Type type,
+ bool mutable_) {
+ assert(import_index == module->imports.size() - 1);
+ Import* import = module->imports[import_index];
import->kind = ExternalKind::Global;
import->global = new Global();
import->global->type = type;
import->global->mutable_ = mutable_;
- ctx->module->globals.push_back(import->global);
- ctx->module->num_global_imports++;
+ module->globals.push_back(import->global);
+ module->num_global_imports++;
return Result::Ok;
}
-static Result on_function_signatures_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->funcs.reserve(ctx->module->num_func_imports + count);
+Result BinaryReaderAST::OnFunctionCount(uint32_t count) {
+ module->funcs.reserve(module->num_func_imports + count);
return Result::Ok;
}
-static Result on_function_signature(uint32_t index,
- uint32_t sig_index,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
-
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnFunction(uint32_t index, uint32_t sig_index) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Func;
field->func = new Func();
@@ -270,99 +396,79 @@ static Result on_function_signature(uint32_t index,
func->decl.has_func_type = true;
func->decl.type_var.type = VarType::Index;
func->decl.type_var.index = sig_index;
- func->decl.sig = ctx->module->func_types[sig_index]->sig;
+ func->decl.sig = module->func_types[sig_index]->sig;
- ctx->module->funcs.push_back(func);
+ module->funcs.push_back(func);
return Result::Ok;
}
-static Result on_table_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->tables.reserve(ctx->module->num_table_imports + count);
+Result BinaryReaderAST::OnTableCount(uint32_t count) {
+ module->tables.reserve(module->num_table_imports + count);
return Result::Ok;
}
-static Result on_table(uint32_t index,
- Type elem_type,
- const Limits* elem_limits,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
-
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnTable(uint32_t index,
+ Type elem_type,
+ const Limits* elem_limits) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Table;
field->table = new Table();
field->table->elem_limits = *elem_limits;
- ctx->module->tables.push_back(field->table);
+ module->tables.push_back(field->table);
return Result::Ok;
}
-static Result on_memory_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->memories.reserve(ctx->module->num_memory_imports + count);
+Result BinaryReaderAST::OnMemoryCount(uint32_t count) {
+ module->memories.reserve(module->num_memory_imports + count);
return Result::Ok;
}
-static Result on_memory(uint32_t index,
- const Limits* page_limits,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
-
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnMemory(uint32_t index, const Limits* page_limits) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Memory;
field->memory = new Memory();
field->memory->page_limits = *page_limits;
- ctx->module->memories.push_back(field->memory);
+ module->memories.push_back(field->memory);
return Result::Ok;
}
-static Result on_global_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->globals.reserve(ctx->module->num_global_imports + count);
+Result BinaryReaderAST::OnGlobalCount(uint32_t count) {
+ module->globals.reserve(module->num_global_imports + count);
return Result::Ok;
}
-static Result begin_global(uint32_t index,
- Type type,
- bool mutable_,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
-
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::BeginGlobal(uint32_t index, Type type, bool mutable_) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Global;
field->global = new Global();
field->global->type = type;
field->global->mutable_ = mutable_;
- ctx->module->globals.push_back(field->global);
+ module->globals.push_back(field->global);
return Result::Ok;
}
-static Result begin_global_init_expr(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(index == ctx->module->globals.size() - 1);
- Global* global = ctx->module->globals[index];
- ctx->current_init_expr = &global->init_expr;
+Result BinaryReaderAST::BeginGlobalInitExpr(uint32_t index) {
+ assert(index == module->globals.size() - 1);
+ Global* global = module->globals[index];
+ current_init_expr = &global->init_expr;
return Result::Ok;
}
-static Result end_global_init_expr(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->current_init_expr = nullptr;
+Result BinaryReaderAST::EndGlobalInitExpr(uint32_t index) {
+ current_init_expr = nullptr;
return Result::Ok;
}
-static Result on_export_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->exports.reserve(count);
+Result BinaryReaderAST::OnExportCount(uint32_t count) {
+ module->exports.reserve(count);
return Result::Ok;
}
-static Result on_export(uint32_t index,
- ExternalKind kind,
- uint32_t item_index,
- StringSlice name,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnExport(uint32_t index,
+ ExternalKind kind,
+ uint32_t item_index,
+ StringSlice name) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Export;
field->export_ = new Export();
@@ -370,157 +476,135 @@ static Result on_export(uint32_t index,
export_->name = dup_string_slice(name);
switch (kind) {
case ExternalKind::Func:
- assert(item_index < ctx->module->funcs.size());
+ assert(item_index < module->funcs.size());
break;
case ExternalKind::Table:
- assert(item_index < ctx->module->tables.size());
+ assert(item_index < module->tables.size());
break;
case ExternalKind::Memory:
- assert(item_index < ctx->module->memories.size());
+ assert(item_index < module->memories.size());
break;
case ExternalKind::Global:
- assert(item_index < ctx->module->globals.size());
+ assert(item_index < module->globals.size());
break;
}
export_->var.type = VarType::Index;
export_->var.index = item_index;
export_->kind = kind;
- ctx->module->exports.push_back(export_);
+ module->exports.push_back(export_);
return Result::Ok;
}
-static Result on_start_function(uint32_t func_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::OnStartFunction(uint32_t func_index) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::Start;
field->start.type = VarType::Index;
- assert(func_index < ctx->module->funcs.size());
+ assert(func_index < module->funcs.size());
field->start.index = func_index;
- ctx->module->start = &field->start;
+ module->start = &field->start;
return Result::Ok;
}
-static Result on_function_bodies_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(ctx->module->num_func_imports + count == ctx->module->funcs.size());
- WABT_USE(ctx);
+Result BinaryReaderAST::OnFunctionBodyCount(uint32_t count) {
+ assert(module->num_func_imports + count == module->funcs.size());
return Result::Ok;
}
-static Result begin_function_body(BinaryReaderContext* context,
- uint32_t index) {
- Context* ctx = static_cast<Context*>(context->user_data);
- ctx->current_func = ctx->module->funcs[index];
- push_label(ctx, LabelType::Func, &ctx->current_func->first_expr);
+Result BinaryReaderAST::BeginFunctionBody(uint32_t index) {
+ current_func = module->funcs[index];
+ PushLabel(LabelType::Func, &current_func->first_expr);
return Result::Ok;
}
-static Result on_local_decl(uint32_t decl_index,
- uint32_t count,
- Type type,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- TypeVector& types = ctx->current_func->local_types;
+Result BinaryReaderAST::OnLocalDecl(uint32_t decl_index,
+ uint32_t count,
+ Type type) {
+ TypeVector& types = current_func->local_types;
types.reserve(types.size() + count);
for (size_t i = 0; i < count; ++i)
types.push_back(type);
return Result::Ok;
}
-static Result on_binary_expr(Opcode opcode, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnBinaryExpr(Opcode opcode) {
Expr* expr = Expr::CreateBinary(opcode);
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_block_expr(uint32_t num_types,
- Type* sig_types,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnBlockExpr(uint32_t num_types, Type* sig_types) {
Expr* expr = Expr::CreateBlock(new Block());
expr->block->sig.assign(sig_types, sig_types + num_types);
- append_expr(ctx, expr);
- push_label(ctx, LabelType::Block, &expr->block->first);
+ AppendExpr(expr);
+ PushLabel(LabelType::Block, &expr->block->first);
return Result::Ok;
}
-static Result on_br_expr(uint32_t depth, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnBrExpr(uint32_t depth) {
Expr* expr = Expr::CreateBr(Var(depth));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_br_if_expr(uint32_t depth, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnBrIfExpr(uint32_t depth) {
Expr* expr = Expr::CreateBrIf(Var(depth));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_br_table_expr(BinaryReaderContext* context,
- uint32_t num_targets,
- uint32_t* target_depths,
- uint32_t default_target_depth) {
- Context* ctx = static_cast<Context*>(context->user_data);
+Result BinaryReaderAST::OnBrTableExpr(uint32_t num_targets,
+ uint32_t* target_depths,
+ uint32_t default_target_depth) {
VarVector* targets = new VarVector();
targets->resize(num_targets);
for (uint32_t i = 0; i < num_targets; ++i) {
(*targets)[i] = Var(target_depths[i]);
}
Expr* expr = Expr::CreateBrTable(targets, Var(default_target_depth));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_call_expr(uint32_t func_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(func_index < ctx->module->funcs.size());
+Result BinaryReaderAST::OnCallExpr(uint32_t func_index) {
+ assert(func_index < module->funcs.size());
Expr* expr = Expr::CreateCall(Var(func_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_call_indirect_expr(uint32_t sig_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(sig_index < ctx->module->func_types.size());
+Result BinaryReaderAST::OnCallIndirectExpr(uint32_t sig_index) {
+ assert(sig_index < module->func_types.size());
Expr* expr = Expr::CreateCallIndirect(Var(sig_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_compare_expr(Opcode opcode, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnCompareExpr(Opcode opcode) {
Expr* expr = Expr::CreateCompare(opcode);
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_convert_expr(Opcode opcode, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnConvertExpr(Opcode opcode) {
Expr* expr = Expr::CreateConvert(opcode);
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_current_memory_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnCurrentMemoryExpr() {
Expr* expr = Expr::CreateCurrentMemory();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_drop_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnDropExpr() {
Expr* expr = Expr::CreateDrop();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_else_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnElseExpr() {
LabelNode* label;
- CHECK_RESULT(top_label(ctx, &label));
+ CHECK_RESULT(TopLabel(&label));
if (label->label_type != LabelType::If) {
- print_error(ctx, "else expression without matching if");
+ PrintError("else expression without matching if");
return Result::Error;
}
LabelNode* parent_label;
- CHECK_RESULT(get_label_at(ctx, &parent_label, 1));
+ CHECK_RESULT(GetLabelAt(&parent_label, 1));
assert(parent_label->last->type == ExprType::If);
label->label_type = LabelType::Else;
@@ -529,196 +613,161 @@ static Result on_else_expr(void* user_data) {
return Result::Ok;
}
-static Result on_end_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- return pop_label(ctx);
+Result BinaryReaderAST::OnEndExpr() {
+ return PopLabel();
}
-static Result on_f32_const_expr(uint32_t value_bits, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnF32ConstExpr(uint32_t value_bits) {
Expr* expr = Expr::CreateConst(Const(Const::F32(), value_bits));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_f64_const_expr(uint64_t value_bits, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnF64ConstExpr(uint64_t value_bits) {
Expr* expr = Expr::CreateConst(Const(Const::F64(), value_bits));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_get_global_expr(uint32_t global_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnGetGlobalExpr(uint32_t global_index) {
Expr* expr = Expr::CreateGetGlobal(Var(global_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_get_local_expr(uint32_t local_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnGetLocalExpr(uint32_t local_index) {
Expr* expr = Expr::CreateGetLocal(Var(local_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_grow_memory_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnGrowMemoryExpr() {
Expr* expr = Expr::CreateGrowMemory();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_i32_const_expr(uint32_t value, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnI32ConstExpr(uint32_t value) {
Expr* expr = Expr::CreateConst(Const(Const::I32(), value));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_i64_const_expr(uint64_t value, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnI64ConstExpr(uint64_t value) {
Expr* expr = Expr::CreateConst(Const(Const::I64(), value));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_if_expr(uint32_t num_types, Type* sig_types, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnIfExpr(uint32_t num_types, Type* sig_types) {
Expr* expr = Expr::CreateIf(new Block());
expr->if_.true_->sig.assign(sig_types, sig_types + num_types);
expr->if_.false_ = nullptr;
- append_expr(ctx, expr);
- push_label(ctx, LabelType::If, &expr->if_.true_->first);
+ AppendExpr(expr);
+ PushLabel(LabelType::If, &expr->if_.true_->first);
return Result::Ok;
}
-static Result on_load_expr(Opcode opcode,
- uint32_t alignment_log2,
- uint32_t offset,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnLoadExpr(Opcode opcode,
+ uint32_t alignment_log2,
+ uint32_t offset) {
Expr* expr = Expr::CreateLoad(opcode, 1 << alignment_log2, offset);
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_loop_expr(uint32_t num_types,
- Type* sig_types,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnLoopExpr(uint32_t num_types, Type* sig_types) {
Expr* expr = Expr::CreateLoop(new Block());
expr->loop->sig.assign(sig_types, sig_types + num_types);
- append_expr(ctx, expr);
- push_label(ctx, LabelType::Loop, &expr->loop->first);
+ AppendExpr(expr);
+ PushLabel(LabelType::Loop, &expr->loop->first);
return Result::Ok;
}
-static Result on_nop_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnNopExpr() {
Expr* expr = Expr::CreateNop();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_return_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnReturnExpr() {
Expr* expr = Expr::CreateReturn();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_select_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnSelectExpr() {
Expr* expr = Expr::CreateSelect();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_set_global_expr(uint32_t global_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnSetGlobalExpr(uint32_t global_index) {
Expr* expr = Expr::CreateSetGlobal(Var(global_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_set_local_expr(uint32_t local_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnSetLocalExpr(uint32_t local_index) {
Expr* expr = Expr::CreateSetLocal(Var(local_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_store_expr(Opcode opcode,
- uint32_t alignment_log2,
- uint32_t offset,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnStoreExpr(Opcode opcode,
+ uint32_t alignment_log2,
+ uint32_t offset) {
Expr* expr = Expr::CreateStore(opcode, 1 << alignment_log2, offset);
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_tee_local_expr(uint32_t local_index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnTeeLocalExpr(uint32_t local_index) {
Expr* expr = Expr::CreateTeeLocal(Var(local_index));
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_unary_expr(Opcode opcode, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnUnaryExpr(Opcode opcode) {
Expr* expr = Expr::CreateUnary(opcode);
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result on_unreachable_expr(void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
+Result BinaryReaderAST::OnUnreachableExpr() {
Expr* expr = Expr::CreateUnreachable();
- return append_expr(ctx, expr);
+ return AppendExpr(expr);
}
-static Result end_function_body(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- CHECK_RESULT(pop_label(ctx));
- ctx->current_func = nullptr;
+Result BinaryReaderAST::EndFunctionBody(uint32_t index) {
+ CHECK_RESULT(PopLabel());
+ current_func = nullptr;
return Result::Ok;
}
-static Result on_elem_segment_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->elem_segments.reserve(count);
+Result BinaryReaderAST::OnElemSegmentCount(uint32_t count) {
+ module->elem_segments.reserve(count);
return Result::Ok;
}
-static Result begin_elem_segment(uint32_t index,
- uint32_t table_index,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::BeginElemSegment(uint32_t index, uint32_t table_index) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::ElemSegment;
field->elem_segment = new ElemSegment();
field->elem_segment->table_var.type = VarType::Index;
field->elem_segment->table_var.index = table_index;
- ctx->module->elem_segments.push_back(field->elem_segment);
+ module->elem_segments.push_back(field->elem_segment);
return Result::Ok;
}
-static Result begin_elem_segment_init_expr(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(index == ctx->module->elem_segments.size() - 1);
- ElemSegment* segment = ctx->module->elem_segments[index];
- ctx->current_init_expr = &segment->offset;
+Result BinaryReaderAST::BeginElemSegmentInitExpr(uint32_t index) {
+ assert(index == module->elem_segments.size() - 1);
+ ElemSegment* segment = module->elem_segments[index];
+ current_init_expr = &segment->offset;
return Result::Ok;
}
-static Result end_elem_segment_init_expr(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->current_init_expr = nullptr;
+Result BinaryReaderAST::EndElemSegmentInitExpr(uint32_t index) {
+ current_init_expr = nullptr;
return Result::Ok;
}
-static Result on_elem_segment_function_index_count(BinaryReaderContext* context,
- uint32_t index,
- uint32_t count) {
- Context* ctx = static_cast<Context*>(context->user_data);
- assert(index == ctx->module->elem_segments.size() - 1);
- ElemSegment* segment = ctx->module->elem_segments[index];
+Result BinaryReaderAST::OnElemSegmentFunctionIndexCount(uint32_t index,
+ uint32_t count) {
+ assert(index == module->elem_segments.size() - 1);
+ ElemSegment* segment = module->elem_segments[index];
segment->vars.reserve(count);
return Result::Ok;
}
-static Result on_elem_segment_function_index(uint32_t index,
- uint32_t func_index,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(index == ctx->module->elem_segments.size() - 1);
- ElemSegment* segment = ctx->module->elem_segments[index];
+Result BinaryReaderAST::OnElemSegmentFunctionIndex(uint32_t index,
+ uint32_t func_index) {
+ assert(index == module->elem_segments.size() - 1);
+ ElemSegment* segment = module->elem_segments[index];
segment->vars.emplace_back();
Var* var = &segment->vars.back();
var->type = VarType::Index;
@@ -726,142 +775,109 @@ static Result on_elem_segment_function_index(uint32_t index,
return Result::Ok;
}
-static Result on_data_segment_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->data_segments.reserve(count);
+Result BinaryReaderAST::OnDataSegmentCount(uint32_t count) {
+ module->data_segments.reserve(count);
return Result::Ok;
}
-static Result begin_data_segment(uint32_t index,
- uint32_t memory_index,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ModuleField* field = append_module_field(ctx->module);
+Result BinaryReaderAST::BeginDataSegment(uint32_t index,
+ uint32_t memory_index) {
+ ModuleField* field = append_module_field(module);
field->type = ModuleFieldType::DataSegment;
field->data_segment = new DataSegment();
field->data_segment->memory_var.type = VarType::Index;
field->data_segment->memory_var.index = memory_index;
- ctx->module->data_segments.push_back(field->data_segment);
+ module->data_segments.push_back(field->data_segment);
return Result::Ok;
}
-static Result begin_data_segment_init_expr(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(index == ctx->module->data_segments.size() - 1);
- DataSegment* segment = ctx->module->data_segments[index];
- ctx->current_init_expr = &segment->offset;
+Result BinaryReaderAST::BeginDataSegmentInitExpr(uint32_t index) {
+ assert(index == module->data_segments.size() - 1);
+ DataSegment* segment = module->data_segments[index];
+ current_init_expr = &segment->offset;
return Result::Ok;
}
-static Result end_data_segment_init_expr(uint32_t index, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- ctx->current_init_expr = nullptr;
+Result BinaryReaderAST::EndDataSegmentInitExpr(uint32_t index) {
+ current_init_expr = nullptr;
return Result::Ok;
}
-static Result on_data_segment_data(uint32_t index,
- const void* data,
- uint32_t size,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- assert(index == ctx->module->data_segments.size() - 1);
- DataSegment* segment = ctx->module->data_segments[index];
+Result BinaryReaderAST::OnDataSegmentData(uint32_t index,
+ const void* data,
+ uint32_t size) {
+ assert(index == module->data_segments.size() - 1);
+ DataSegment* segment = module->data_segments[index];
segment->data = new char[size];
segment->size = size;
memcpy(segment->data, data, size);
return Result::Ok;
}
-static Result on_function_names_count(uint32_t count, void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- if (count > ctx->module->funcs.size()) {
- print_error(
- ctx, "expected function name count (%u) <= function count (%" PRIzd ")",
- count, ctx->module->funcs.size());
+Result BinaryReaderAST::OnFunctionNamesCount(uint32_t count) {
+ if (count > module->funcs.size()) {
+ PrintError("expected function name count (%u) <= function count (%" PRIzd
+ ")",
+ count, module->funcs.size());
return Result::Error;
}
return Result::Ok;
}
-static Result on_function_name(uint32_t index,
- StringSlice name,
- void* user_data) {
+Result BinaryReaderAST::OnFunctionName(uint32_t index, StringSlice name) {
if (string_slice_is_empty(&name))
return Result::Ok;
- Context* ctx = static_cast<Context*>(user_data);
- ctx->module->func_bindings.emplace(string_slice_to_string(name),
- Binding(index));
- Func* func = ctx->module->funcs[index];
+ module->func_bindings.emplace(string_slice_to_string(name), Binding(index));
+ Func* func = module->funcs[index];
func->name = dup_string_slice(name);
return Result::Ok;
}
-static Result on_local_name_local_count(uint32_t index,
- uint32_t count,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- Module* module = ctx->module;
+Result BinaryReaderAST::OnLocalNameLocalCount(uint32_t index, uint32_t count) {
assert(index < module->funcs.size());
Func* func = module->funcs[index];
uint32_t num_params_and_locals = get_num_params_and_locals(func);
if (count > num_params_and_locals) {
- print_error(ctx, "expected local name count (%d) <= local count (%d)",
- count, num_params_and_locals);
+ PrintError("expected local name count (%d) <= local count (%d)", count,
+ num_params_and_locals);
return Result::Error;
}
return Result::Ok;
}
-static Result on_init_expr_f32_const_expr(uint32_t index,
- uint32_t value,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- *ctx->current_init_expr = Expr::CreateConst(Const(Const::F32(), value));
+Result BinaryReaderAST::OnInitExprF32ConstExpr(uint32_t index, uint32_t value) {
+ *current_init_expr = Expr::CreateConst(Const(Const::F32(), value));
return Result::Ok;
}
-static Result on_init_expr_f64_const_expr(uint32_t index,
- uint64_t value,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- *ctx->current_init_expr = Expr::CreateConst(Const(Const::F64(), value));
+Result BinaryReaderAST::OnInitExprF64ConstExpr(uint32_t index, uint64_t value) {
+ *current_init_expr = Expr::CreateConst(Const(Const::F64(), value));
return Result::Ok;
}
-static Result on_init_expr_get_global_expr(uint32_t index,
- uint32_t global_index,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- *ctx->current_init_expr = Expr::CreateGetGlobal(Var(global_index));
+Result BinaryReaderAST::OnInitExprGetGlobalExpr(uint32_t index,
+ uint32_t global_index) {
+ *current_init_expr = Expr::CreateGetGlobal(Var(global_index));
return Result::Ok;
}
-static Result on_init_expr_i32_const_expr(uint32_t index,
- uint32_t value,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- *ctx->current_init_expr = Expr::CreateConst(Const(Const::I32(), value));
+Result BinaryReaderAST::OnInitExprI32ConstExpr(uint32_t index, uint32_t value) {
+ *current_init_expr = Expr::CreateConst(Const(Const::I32(), value));
return Result::Ok;
}
-static Result on_init_expr_i64_const_expr(uint32_t index,
- uint64_t value,
- void* user_data) {
- Context* ctx = static_cast<Context*>(user_data);
- *ctx->current_init_expr = Expr::CreateConst(Const(Const::I64(), value));
+Result BinaryReaderAST::OnInitExprI64ConstExpr(uint32_t index, uint64_t value) {
+ *current_init_expr = Expr::CreateConst(Const(Const::I64(), value));
return Result::Ok;
}
-static Result on_local_name(uint32_t func_index,
- uint32_t local_index,
- StringSlice name,
- void* user_data) {
+Result BinaryReaderAST::OnLocalName(uint32_t func_index,
+ uint32_t local_index,
+ StringSlice name) {
if (string_slice_is_empty(&name))
return Result::Ok;
- Context* ctx = static_cast<Context*>(user_data);
- Module* module = ctx->module;
Func* func = module->funcs[func_index];
uint32_t num_params = get_num_params(func);
BindingHash* bindings;
@@ -879,112 +895,15 @@ static Result on_local_name(uint32_t func_index,
return Result::Ok;
}
+} // namespace
+
Result read_binary_ast(const void* data,
size_t size,
const ReadBinaryOptions* options,
BinaryErrorHandler* error_handler,
struct Module* out_module) {
- Context ctx;
- ctx.error_handler = error_handler;
- ctx.module = out_module;
-
- BinaryReader reader;
- WABT_ZERO_MEMORY(reader);
- reader.user_data = &ctx;
- reader.on_error = on_error;
-
- reader.on_signature_count = on_signature_count;
- reader.on_signature = on_signature;
-
- reader.on_import_count = on_import_count;
- reader.on_import = on_import;
- reader.on_import_func = on_import_func;
- reader.on_import_table = on_import_table;
- reader.on_import_memory = on_import_memory;
- reader.on_import_global = on_import_global;
-
- reader.on_function_signatures_count = on_function_signatures_count;
- reader.on_function_signature = on_function_signature;
-
- reader.on_table_count = on_table_count;
- reader.on_table = on_table;
-
- reader.on_memory_count = on_memory_count;
- reader.on_memory = on_memory;
-
- reader.on_global_count = on_global_count;
- reader.begin_global = begin_global;
- reader.begin_global_init_expr = begin_global_init_expr;
- reader.end_global_init_expr = end_global_init_expr;
-
- reader.on_export_count = on_export_count;
- reader.on_export = on_export;
-
- reader.on_start_function = on_start_function;
-
- reader.on_function_bodies_count = on_function_bodies_count;
- reader.begin_function_body = begin_function_body;
- reader.on_local_decl = on_local_decl;
- reader.on_binary_expr = on_binary_expr;
- reader.on_block_expr = on_block_expr;
- reader.on_br_expr = on_br_expr;
- reader.on_br_if_expr = on_br_if_expr;
- reader.on_br_table_expr = on_br_table_expr;
- reader.on_call_expr = on_call_expr;
- reader.on_call_indirect_expr = on_call_indirect_expr;
- reader.on_compare_expr = on_compare_expr;
- reader.on_convert_expr = on_convert_expr;
- reader.on_current_memory_expr = on_current_memory_expr;
- reader.on_drop_expr = on_drop_expr;
- reader.on_else_expr = on_else_expr;
- reader.on_end_expr = on_end_expr;
- reader.on_f32_const_expr = on_f32_const_expr;
- reader.on_f64_const_expr = on_f64_const_expr;
- reader.on_get_global_expr = on_get_global_expr;
- reader.on_get_local_expr = on_get_local_expr;
- reader.on_grow_memory_expr = on_grow_memory_expr;
- reader.on_i32_const_expr = on_i32_const_expr;
- reader.on_i64_const_expr = on_i64_const_expr;
- reader.on_if_expr = on_if_expr;
- reader.on_load_expr = on_load_expr;
- reader.on_loop_expr = on_loop_expr;
- reader.on_nop_expr = on_nop_expr;
- reader.on_return_expr = on_return_expr;
- reader.on_select_expr = on_select_expr;
- reader.on_set_global_expr = on_set_global_expr;
- reader.on_set_local_expr = on_set_local_expr;
- reader.on_store_expr = on_store_expr;
- reader.on_tee_local_expr = on_tee_local_expr;
- reader.on_unary_expr = on_unary_expr;
- reader.on_unreachable_expr = on_unreachable_expr;
- reader.end_function_body = end_function_body;
-
- reader.on_elem_segment_count = on_elem_segment_count;
- reader.begin_elem_segment = begin_elem_segment;
- reader.begin_elem_segment_init_expr = begin_elem_segment_init_expr;
- reader.end_elem_segment_init_expr = end_elem_segment_init_expr;
- reader.on_elem_segment_function_index_count =
- on_elem_segment_function_index_count;
- reader.on_elem_segment_function_index = on_elem_segment_function_index;
-
- reader.on_data_segment_count = on_data_segment_count;
- reader.begin_data_segment = begin_data_segment;
- reader.begin_data_segment_init_expr = begin_data_segment_init_expr;
- reader.end_data_segment_init_expr = end_data_segment_init_expr;
- reader.on_data_segment_data = on_data_segment_data;
-
- reader.on_function_names_count = on_function_names_count;
- reader.on_function_name = on_function_name;
- reader.on_local_name_local_count = on_local_name_local_count;
- reader.on_local_name = on_local_name;
-
- reader.on_init_expr_f32_const_expr = on_init_expr_f32_const_expr;
- reader.on_init_expr_f64_const_expr = on_init_expr_f64_const_expr;
- reader.on_init_expr_get_global_expr = on_init_expr_get_global_expr;
- reader.on_init_expr_i32_const_expr = on_init_expr_i32_const_expr;
- reader.on_init_expr_i64_const_expr = on_init_expr_i64_const_expr;
-
- Result result = read_binary(data, size, &reader, 1, options);
+ BinaryReaderAST reader(out_module, error_handler);
+ Result result = read_binary(data, size, &reader, options);
return result;
}