diff options
-rw-r--r-- | src/binary-reader-interpreter.cc | 33 | ||||
-rw-r--r-- | src/interpreter.cc | 70 | ||||
-rw-r--r-- | src/interpreter.h | 45 | ||||
-rw-r--r-- | src/tools/wasm-interp.cc | 329 |
4 files changed, 199 insertions, 278 deletions
diff --git a/src/binary-reader-interpreter.cc b/src/binary-reader-interpreter.cc index def93165..74724439 100644 --- a/src/binary-reader-interpreter.cc +++ b/src/binary-reader-interpreter.cc @@ -558,14 +558,12 @@ wabt::Result BinaryReaderInterpreter::OnImport(Index index, string_view module_name, string_view field_name) { Import* import = &module->imports[index]; - import->module_name = - dup_string_slice(string_view_to_string_slice(module_name)); - import->field_name = - dup_string_slice(string_view_to_string_slice(field_name)); + import->module_name = module_name.to_string(); + import->field_name = field_name.to_string(); Module* module = env->FindRegisteredModule(import->module_name); if (!module) { - PrintError("unknown import module \"" PRIstringslice "\"", - WABT_PRINTF_STRING_SLICE_ARG(import->module_name)); + PrintError("unknown import module \"" PRIstringview "\"", + WABT_PRINTF_STRING_VIEW_ARG(import->module_name)); return wabt::Result::Error; } if (module->is_host) { @@ -577,8 +575,8 @@ wabt::Result BinaryReaderInterpreter::OnImport(Index index, } else { Export* export_ = module->GetExport(import->field_name); if (!export_) { - PrintError("unknown module field \"" PRIstringslice "\"", - WABT_PRINTF_STRING_SLICE_ARG(import->field_name)); + PrintError("unknown module field \"" PRIstringview "\"", + WABT_PRINTF_STRING_VIEW_ARG(import->field_name)); return wabt::Result::Error; } @@ -613,10 +611,10 @@ wabt::Result BinaryReaderInterpreter::CheckImportKind( Import* import, ExternalKind expected_kind) { if (import->kind != expected_kind) { - PrintError("expected import \"" PRIstringslice "." PRIstringslice + PrintError("expected import \"" PRIstringview "." PRIstringview "\" to have kind %s, not %s", - WABT_PRINTF_STRING_SLICE_ARG(import->module_name), - WABT_PRINTF_STRING_SLICE_ARG(import->field_name), + WABT_PRINTF_STRING_VIEW_ARG(import->module_name), + WABT_PRINTF_STRING_VIEW_ARG(import->field_name), get_kind_name(expected_kind), get_kind_name(import->kind)); return wabt::Result::Error; } @@ -657,11 +655,10 @@ wabt::Result BinaryReaderInterpreter::AppendExport(Module* module, return wabt::Result::Error; } - module->exports.emplace_back( - dup_string_slice(string_view_to_string_slice(name)), kind, item_index); + module->exports.emplace_back(name, kind, item_index); Export* export_ = &module->exports.back(); - module->export_bindings.emplace(string_slice_to_string(export_->name), + module->export_bindings.emplace(export_->name, Binding(module->exports.size() - 1)); return wabt::Result::Ok; } @@ -692,7 +689,7 @@ wabt::Result BinaryReaderInterpreter::OnImportFunc(Index import_index, func_env_index = env->GetFuncCount() - 1; AppendExport(host_import_module, ExternalKind::Func, func_env_index, - string_slice_to_string(import->field_name)); + import->field_name); } else { CHECK_RESULT(CheckImportKind(import, ExternalKind::Func)); Func* func = env->GetFunc(import_env_index); @@ -731,7 +728,7 @@ wabt::Result BinaryReaderInterpreter::OnImportTable(Index import_index, module->table_index = env->GetTableCount() - 1; AppendExport(host_import_module, ExternalKind::Table, module->table_index, - string_slice_to_string(import->field_name)); + import->field_name); } else { CHECK_RESULT(CheckImportKind(import, ExternalKind::Table)); Table* table = env->GetTable(import_env_index); @@ -766,7 +763,7 @@ wabt::Result BinaryReaderInterpreter::OnImportMemory( module->memory_index = env->GetMemoryCount() - 1; AppendExport(host_import_module, ExternalKind::Memory, module->memory_index, - string_slice_to_string(import->field_name)); + import->field_name); } else { CHECK_RESULT(CheckImportKind(import, ExternalKind::Memory)); Memory* memory = env->GetMemory(import_env_index); @@ -795,7 +792,7 @@ wabt::Result BinaryReaderInterpreter::OnImportGlobal(Index import_index, global_env_index = env->GetGlobalCount() - 1; AppendExport(host_import_module, ExternalKind::Global, global_env_index, - string_slice_to_string(import->field_name)); + import->field_name); } else { CHECK_RESULT(CheckImportKind(import, ExternalKind::Global)); // TODO: check type and mutability diff --git a/src/interpreter.cc b/src/interpreter.cc index f2f20323..fdfc6ed5 100644 --- a/src/interpreter.cc +++ b/src/interpreter.cc @@ -63,20 +63,20 @@ static const char* GetOpcodeName(Opcode opcode) { Environment::Environment() : istream_(new OutputBuffer()) {} -Index Environment::FindModuleIndex(StringSlice name) const { - auto iter = module_bindings_.find(string_slice_to_string(name)); +Index Environment::FindModuleIndex(string_view name) const { + auto iter = module_bindings_.find(name.to_string()); if (iter == module_bindings_.end()) return kInvalidIndex; return iter->second.index; } -Module* Environment::FindModule(StringSlice name) { +Module* Environment::FindModule(string_view name) { Index index = FindModuleIndex(name); return index == kInvalidIndex ? nullptr : modules_[index].get(); } -Module* Environment::FindRegisteredModule(StringSlice name) { - auto iter = registered_module_bindings_.find(string_slice_to_string(name)); +Module* Environment::FindRegisteredModule(string_view name) { + auto iter = registered_module_bindings_.find(name.to_string()); if (iter == registered_module_bindings_.end()) return nullptr; return modules_[iter->second.index].get(); @@ -107,9 +107,7 @@ FuncSignature::FuncSignature(Index param_count, result_types(result_types, result_types + result_count) {} Import::Import() : kind(ExternalKind::Func) { - ZeroMemory(module_name); - ZeroMemory(field_name); - ZeroMemory(func.sig_index); + func.sig_index = kInvalidIndex; } Import::Import(Import&& other) { @@ -118,10 +116,8 @@ Import::Import(Import&& other) { Import& Import::operator=(Import&& other) { kind = other.kind; - module_name = other.module_name; - ZeroMemory(other.module_name); - field_name = other.field_name; - ZeroMemory(other.field_name); + 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; @@ -144,46 +140,18 @@ Import& Import::operator=(Import&& other) { return *this; } -Import::~Import() { - destroy_string_slice(&module_name); - destroy_string_slice(&field_name); -} - -Export::Export(Export&& other) - : name(other.name), kind(other.kind), index(other.index) { - ZeroMemory(other.name); -} - -Export& Export::operator=(Export&& other) { - name = other.name; - kind = other.kind; - index = other.index; - ZeroMemory(other.name); - return *this; -} - -Export::~Export() { - destroy_string_slice(&name); -} - Module::Module(bool is_host) : memory_index(kInvalidIndex), table_index(kInvalidIndex), - is_host(is_host) { - ZeroMemory(name); -} + is_host(is_host) {} -Module::Module(const StringSlice& name, bool is_host) - : name(name), +Module::Module(string_view name, bool is_host) + : name(name.to_string()), memory_index(kInvalidIndex), table_index(kInvalidIndex), is_host(is_host) {} -Module::~Module() { - destroy_string_slice(&name); -} - -Export* Module::GetExport(StringSlice name) { +Export* Module::GetExport(string_view name) { int field_index = export_bindings.FindIndex(name); if (field_index < 0) return nullptr; @@ -196,7 +164,7 @@ DefinedModule::DefinedModule() istream_start(kInvalidIstreamOffset), istream_end(kInvalidIstreamOffset) {} -HostModule::HostModule(const StringSlice& name) : Module(name, true) {} +HostModule::HostModule(string_view name) : Module(name, true) {} Environment::MarkPoint Environment::Mark() { MarkPoint mark; @@ -213,9 +181,9 @@ Environment::MarkPoint Environment::Mark() { void Environment::ResetToMarkPoint(const MarkPoint& mark) { // Destroy entries in the binding hash. for (size_t i = mark.modules_size; i < modules_.size(); ++i) { - const StringSlice* name = &modules_[i]->name; - if (!string_slice_is_empty(name)) - module_bindings_.erase(string_slice_to_string(*name)); + std::string name = modules_[i]->name; + if (!name.empty()) + module_bindings_.erase(name); } // registered_module_bindings_ maps from an arbitrary name to a module index, @@ -237,10 +205,10 @@ void Environment::ResetToMarkPoint(const MarkPoint& mark) { istream_->data.resize(mark.istream_size); } -HostModule* Environment::AppendHostModule(StringSlice name) { - HostModule* module = new HostModule(dup_string_slice(name)); +HostModule* Environment::AppendHostModule(string_view name) { + HostModule* module = new HostModule(name); modules_.emplace_back(module); - registered_module_bindings_.emplace(string_slice_to_string(name), + registered_module_bindings_.emplace(name.to_string(), Binding(modules_.size() - 1)); return module; } diff --git a/src/interpreter.h b/src/interpreter.h index d1f588ab..5815a334 100644 --- a/src/interpreter.h +++ b/src/interpreter.h @@ -174,10 +174,10 @@ struct Import { Import(); Import(Import&&); Import& operator=(Import&&); - ~Import(); + ~Import() = default; - StringSlice module_name; - StringSlice field_name; + std::string module_name; + std::string field_name; ExternalKind kind; union { struct { @@ -230,15 +230,13 @@ struct DefinedFunc : Func { }; struct HostFunc : Func { - HostFunc(const StringSlice& module_name, - const StringSlice& field_name, - Index sig_index) + HostFunc(string_view module_name, string_view field_name, Index sig_index) : Func(sig_index, true), - module_name(module_name), - field_name(field_name) {} + module_name(module_name.to_string()), + field_name(field_name.to_string()) {} - StringSlice module_name; - StringSlice field_name; + std::string module_name; + std::string field_name; HostFuncCallback callback; void* user_data; }; @@ -254,13 +252,10 @@ HostFunc* Func::as_host() { } struct Export { - Export(const StringSlice& name, ExternalKind kind, Index index) - : name(name), kind(kind), index(index) {} - Export(Export&&); - Export& operator=(Export&&); - ~Export(); + Export(string_view name, ExternalKind kind, Index index) + : name(name.to_string()), kind(kind), index(index) {} - StringSlice name; + std::string name; ExternalKind kind; Index index; }; @@ -282,15 +277,15 @@ class HostImportDelegate { struct Module { WABT_DISALLOW_COPY_AND_ASSIGN(Module); explicit Module(bool is_host); - Module(const StringSlice& name, bool is_host); - virtual ~Module(); + Module(string_view name, bool is_host); + virtual ~Module() = default; inline struct DefinedModule* as_defined(); inline struct HostModule* as_host(); - Export* GetExport(StringSlice name); + Export* GetExport(string_view name); - StringSlice name; + std::string name; std::vector<Export> exports; BindingHash export_bindings; Index memory_index; /* kInvalidIndex if not defined */ @@ -308,7 +303,7 @@ struct DefinedModule : Module { }; struct HostModule : Module { - explicit HostModule(const StringSlice& name); + explicit HostModule(string_view name); std::unique_ptr<HostImportDelegate> import_delegate; }; @@ -354,7 +349,7 @@ class Environment { Index GetLastModuleIndex() const { return modules_.empty() ? kInvalidIndex : modules_.size() - 1; } - Index FindModuleIndex(StringSlice name) const; + Index FindModuleIndex(string_view name) const; FuncSignature* GetFuncSignature(Index index) { return &sigs_[index]; } Func* GetFunc(Index index) { @@ -381,8 +376,8 @@ class Environment { Module* GetLastModule() { return modules_.empty() ? nullptr : modules_.back().get(); } - Module* FindModule(StringSlice name); - Module* FindRegisteredModule(StringSlice name); + Module* FindModule(string_view name); + Module* FindRegisteredModule(string_view name); template <typename... Args> FuncSignature* EmplaceBackFuncSignature(Args&&... args) { @@ -430,7 +425,7 @@ class Environment { registered_module_bindings_.emplace(args...); } - HostModule* AppendHostModule(StringSlice name); + HostModule* AppendHostModule(string_view name); bool FuncSignaturesAreEqual(Index sig_index_0, Index sig_index_1) const; diff --git a/src/tools/wasm-interp.cc b/src/tools/wasm-interp.cc index 1754ada1..8fe8cdfe 100644 --- a/src/tools/wasm-interp.cc +++ b/src/tools/wasm-interp.cc @@ -120,25 +120,21 @@ enum class ModuleType { Binary, }; -static StringSlice get_dirname(const char* s) { - /* strip everything after and including the last slash (or backslash), e.g.: - * - * s = "foo/bar/baz", => "foo/bar" - * s = "/usr/local/include/stdio.h", => "/usr/local/include" - * s = "foo.bar", => "" - * s = "some\windows\directory", => "some\windows" - */ - const char* last_slash = strrchr(s, '/'); - const char* last_backslash = strrchr(s, '\\'); - if (!last_slash) - last_slash = s; - if (!last_backslash) - last_backslash = s; - - StringSlice result; - result.start = s; - result.length = std::max(last_slash, last_backslash) - s; - return result; +static string_view get_dirname(string_view path) { + // Strip everything after and including the last slash (or backslash), e.g.: + // + // s = "foo/bar/baz", => "foo/bar" + // s = "/usr/local/include/stdio.h", => "/usr/local/include" + // s = "foo.bar", => "" + // s = "some\windows\directory", => "some\windows" + size_t last_slash = path.find_last_of('/'); + size_t last_backslash = path.find_last_of('\\'); + if (last_slash == string_view::npos) + last_slash = 0; + if (last_backslash == string_view::npos) + last_backslash = 0; + + return path.substr(0, std::max(last_slash, last_backslash)); } /* Not sure, but 100 chars is probably safe */ @@ -195,14 +191,14 @@ static void print_interpreter_result(const char* desc, printf("%s: %s\n", desc, s_trap_strings[static_cast<size_t>(iresult)]); } -static void print_call(StringSlice module_name, - StringSlice func_name, +static void print_call(string_view module_name, + string_view func_name, const std::vector<TypedValue>& args, const std::vector<TypedValue>& results, interpreter::Result iresult) { - if (module_name.length) - printf(PRIstringslice ".", WABT_PRINTF_STRING_SLICE_ARG(module_name)); - printf(PRIstringslice "(", WABT_PRINTF_STRING_SLICE_ARG(func_name)); + if (!module_name.empty()) + printf(PRIstringview ".", WABT_PRINTF_STRING_VIEW_ARG(module_name)); + printf(PRIstringview "(", WABT_PRINTF_STRING_VIEW_ARG(func_name)); print_typed_value_vector(args); printf(") =>"); if (iresult == interpreter::Result::Ok) { @@ -245,8 +241,8 @@ static interpreter::Result run_export(Thread* thread, const std::vector<TypedValue>& args, std::vector<TypedValue>* out_results) { if (s_trace) { - printf(">>> running export \"" PRIstringslice "\":\n", - WABT_PRINTF_STRING_SLICE_ARG(export_->name)); + printf(">>> running export \"" PRIstringview "\":\n", + WABT_PRINTF_STRING_VIEW_ARG(export_->name)); } assert(export_->kind == ExternalKind::Func); @@ -256,11 +252,11 @@ static interpreter::Result run_export(Thread* thread, static interpreter::Result run_export_by_name( Thread* thread, Module* module, - const StringSlice* name, + string_view name, const std::vector<TypedValue>& args, std::vector<TypedValue>* out_results, RunVerbosity verbose) { - Export* export_ = module->GetExport(*name); + Export* export_ = module->GetExport(name); if (!export_) return interpreter::Result::UnknownExport; if (export_->kind != ExternalKind::Func) @@ -271,9 +267,9 @@ static interpreter::Result run_export_by_name( static interpreter::Result get_global_export_by_name( Thread* thread, Module* module, - const StringSlice* name, + string_view name, std::vector<TypedValue>* out_results) { - Export* export_ = module->GetExport(*name); + Export* export_ = module->GetExport(name); if (!export_) return interpreter::Result::UnknownExport; if (export_->kind != ExternalKind::Global) @@ -293,7 +289,7 @@ static void run_all_exports(Module* module, for (const Export& export_ : module->exports) { interpreter::Result iresult = run_export(thread, &export_, args, &results); if (verbose == RunVerbosity::Verbose) { - print_call(empty_string_slice(), export_.name, args, results, iresult); + print_call(string_view(), export_.name, args, results, iresult); } } } @@ -342,10 +338,10 @@ static interpreter::Result default_host_callback(const HostFunc* func, return interpreter::Result::Ok; } -#define PRIimport "\"" PRIstringslice "." PRIstringslice "\"" +#define PRIimport "\"" PRIstringview "." PRIstringview "\"" #define PRINTF_IMPORT_ARG(x) \ - WABT_PRINTF_STRING_SLICE_ARG((x).module_name) \ - , WABT_PRINTF_STRING_SLICE_ARG((x).field_name) + WABT_PRINTF_STRING_VIEW_ARG((x).module_name) \ + , WABT_PRINTF_STRING_VIEW_ARG((x).field_name) class SpectestHostImportDelegate : public HostImportDelegate { public: @@ -353,7 +349,7 @@ class SpectestHostImportDelegate : public HostImportDelegate { Func* func, FuncSignature* func_sig, const ErrorCallback& callback) override { - if (string_slice_eq_cstr(&import->field_name, "print")) { + if (import->field_name == "print") { func->as_host()->callback = default_host_callback; return wabt::Result::Ok; } else { @@ -366,7 +362,7 @@ class SpectestHostImportDelegate : public HostImportDelegate { wabt::Result ImportTable(Import* import, Table* table, const ErrorCallback& callback) override { - if (string_slice_eq_cstr(&import->field_name, "table")) { + if (import->field_name == "table") { table->limits.has_max = true; table->limits.initial = 10; table->limits.max = 20; @@ -381,7 +377,7 @@ class SpectestHostImportDelegate : public HostImportDelegate { wabt::Result ImportMemory(Import* import, Memory* memory, const ErrorCallback& callback) override { - if (string_slice_eq_cstr(&import->field_name, "memory")) { + if (import->field_name == "memory") { memory->page_limits.has_max = true; memory->page_limits.initial = 1; memory->page_limits.max = 2; @@ -397,7 +393,7 @@ class SpectestHostImportDelegate : public HostImportDelegate { wabt::Result ImportGlobal(Import* import, Global* global, const ErrorCallback& callback) override { - if (string_slice_eq_cstr(&import->field_name, "global")) { + if (import->field_name == "global") { switch (global->typed_value.type) { case Type::I32: global->typed_value.value.i32 = 666; @@ -441,8 +437,7 @@ class SpectestHostImportDelegate : public HostImportDelegate { }; static void init_environment(Environment* env) { - HostModule* host_module = - env->AppendHostModule(string_slice_from_cstr("spectest")); + HostModule* host_module = env->AppendHostModule("spectest"); host_module->import_delegate.reset(new SpectestHostImportDelegate()); } @@ -479,9 +474,7 @@ struct Context { has_prev_loc(0), command_line_number(0), passed(0), - total(0) { - ZeroMemory(source_filename); - } + total(0) {} Environment env; Thread thread; @@ -490,7 +483,7 @@ struct Context { /* Parsing info */ char* json_data; size_t json_data_size; - StringSlice source_filename; + std::string source_filename; size_t json_offset; Location loc; Location prev_loc; @@ -508,14 +501,9 @@ enum class ActionType { }; struct Action { - Action() { - ZeroMemory(module_name); - ZeroMemory(field_name); - } - ActionType type = ActionType::Invoke; - StringSlice module_name; - StringSlice field_name; + std::string module_name; + std::string field_name; std::vector<TypedValue> args; }; @@ -540,8 +528,8 @@ static void WABT_PRINTF_FORMAT(2, 3) static void WABT_PRINTF_FORMAT(2, 3) print_command_error(Context* ctx, const char* format, ...) { WABT_SNPRINTF_ALLOCA(buffer, length, format); - printf(PRIstringslice ":%u: %s\n", - WABT_PRINTF_STRING_SLICE_ARG(ctx->source_filename), + printf(PRIstringview ":%u: %s\n", + WABT_PRINTF_STRING_VIEW_ARG(ctx->source_filename), ctx->command_line_number, buffer); } @@ -642,21 +630,15 @@ static wabt::Result parse_uint32(Context* ctx, uint32_t* out_int) { return wabt::Result::Ok; } -static wabt::Result parse_string(Context* ctx, StringSlice* out_string) { - ZeroMemory(*out_string); +static wabt::Result parse_string(Context* ctx, std::string* out_string) { + out_string->clear(); skip_whitespace(ctx); if (read_char(ctx) != '"') { print_parse_error(ctx, "expected string"); return wabt::Result::Error; } - /* Modify json_data in-place so we can use the StringSlice directly - * without having to allocate additional memory; this is only necessary when - * the string contains an escape, but we do it always because the code is - * simpler. */ - char* start = &ctx->json_data[ctx->json_offset]; - char* p = start; - out_string->start = start; + while (1) { int c = read_char(ctx); if (c == '"') { @@ -686,30 +668,29 @@ static wabt::Result parse_string(Context* ctx, StringSlice* out_string) { } if (code < 256) { - *p++ = code; + *out_string += code; } else { print_parse_error(ctx, "only escape codes < 256 allowed, got %u\n", code); } } else { - *p++ = c; + *out_string += c; } } - out_string->length = p - start; return wabt::Result::Ok; } static wabt::Result parse_key_string_value(Context* ctx, const char* key, - StringSlice* out_string) { - ZeroMemory(*out_string); + std::string* out_string) { + out_string->clear(); EXPECT_KEY(key); return parse_string(ctx, out_string); } static wabt::Result parse_opt_name_string_value(Context* ctx, - StringSlice* out_string) { - ZeroMemory(*out_string); + std::string* out_string) { + out_string->clear(); if (match(ctx, "\"name\"")) { EXPECT(":"); CHECK_RESULT(parse_string(ctx, out_string)); @@ -725,26 +706,26 @@ static wabt::Result parse_line(Context* ctx) { } static wabt::Result parse_type_object(Context* ctx, Type* out_type) { - StringSlice type_str; + std::string type_str; EXPECT("{"); PARSE_KEY_STRING_VALUE("type", &type_str); EXPECT("}"); - if (string_slice_eq_cstr(&type_str, "i32")) { + if (type_str == "i32") { *out_type = Type::I32; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&type_str, "f32")) { + } else if (type_str == "f32") { *out_type = Type::F32; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&type_str, "i64")) { + } else if (type_str == "i64") { *out_type = Type::I64; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&type_str, "f64")) { + } else if (type_str == "f64") { *out_type = Type::F64; return wabt::Result::Ok; } else { - print_parse_error(ctx, "unknown type: \"" PRIstringslice "\"", - WABT_PRINTF_STRING_SLICE_ARG(type_str)); + print_parse_error(ctx, "unknown type: \"" PRIstringview "\"", + WABT_PRINTF_STRING_VIEW_ARG(type_str)); return wabt::Result::Error; } } @@ -765,39 +746,39 @@ static wabt::Result parse_type_vector(Context* ctx, TypeVector* out_types) { } static wabt::Result parse_const(Context* ctx, TypedValue* out_value) { - StringSlice type_str; - StringSlice value_str; + std::string type_str; + std::string value_str; EXPECT("{"); PARSE_KEY_STRING_VALUE("type", &type_str); EXPECT(","); PARSE_KEY_STRING_VALUE("value", &value_str); EXPECT("}"); - const char* value_start = value_str.start; - const char* value_end = value_str.start + value_str.length; + const char* value_start = value_str.data(); + const char* value_end = value_str.data() + value_str.size(); - if (string_slice_eq_cstr(&type_str, "i32")) { + if (type_str == "i32") { uint32_t value; CHECK_RESULT(parse_int32(value_start, value_end, &value, ParseIntType::UnsignedOnly)); out_value->type = Type::I32; out_value->value.i32 = value; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&type_str, "f32")) { + } else if (type_str == "f32") { uint32_t value_bits; CHECK_RESULT(parse_int32(value_start, value_end, &value_bits, ParseIntType::UnsignedOnly)); out_value->type = Type::F32; out_value->value.f32_bits = value_bits; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&type_str, "i64")) { + } else if (type_str == "i64") { uint64_t value; CHECK_RESULT(parse_int64(value_start, value_end, &value, ParseIntType::UnsignedOnly)); out_value->type = Type::I64; out_value->value.i64 = value; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&type_str, "f64")) { + } else if (type_str == "f64") { uint64_t value_bits; CHECK_RESULT(parse_int64(value_start, value_end, &value_bits, ParseIntType::UnsignedOnly)); @@ -805,8 +786,8 @@ static wabt::Result parse_const(Context* ctx, TypedValue* out_value) { out_value->value.f64_bits = value_bits; return wabt::Result::Ok; } else { - print_parse_error(ctx, "unknown type: \"" PRIstringslice "\"", - WABT_PRINTF_STRING_SLICE_ARG(type_str)); + print_parse_error(ctx, "unknown type: \"" PRIstringview "\"", + WABT_PRINTF_STRING_VIEW_ARG(type_str)); return wabt::Result::Error; } } @@ -854,60 +835,54 @@ static wabt::Result parse_action(Context* ctx, Action* out_action) { } static wabt::Result parse_module_type(Context* ctx, ModuleType* out_type) { - StringSlice module_type_str; - ZeroMemory(module_type_str); + std::string module_type_str; PARSE_KEY_STRING_VALUE("module_type", &module_type_str); - if (string_slice_eq_cstr(&module_type_str, "text")) { + if (module_type_str == "text") { *out_type = ModuleType::Text; return wabt::Result::Ok; - } else if (string_slice_eq_cstr(&module_type_str, "binary")) { + } else if (module_type_str == "binary") { *out_type = ModuleType::Binary; return wabt::Result::Ok; } else { - print_parse_error(ctx, "unknown module type: \"" PRIstringslice "\"", - WABT_PRINTF_STRING_SLICE_ARG(module_type_str)); + print_parse_error(ctx, "unknown module type: \"" PRIstringview "\"", + WABT_PRINTF_STRING_VIEW_ARG(module_type_str)); return wabt::Result::Error; } } -static char* create_module_path(Context* ctx, StringSlice filename) { +static std::string CreateModulePath(Context* ctx, string_view filename) { const char* spec_json_filename = ctx->loc.filename; - StringSlice dirname = get_dirname(spec_json_filename); - size_t path_len = dirname.length + 1 + filename.length + 1; - char* path = new char[path_len]; + string_view dirname = get_dirname(spec_json_filename); + std::string path; - if (dirname.length == 0) { - snprintf(path, path_len, PRIstringslice, - WABT_PRINTF_STRING_SLICE_ARG(filename)); + if (dirname.size() == 0) { + path = filename.to_string(); } else { - snprintf(path, path_len, PRIstringslice "/" PRIstringslice, - WABT_PRINTF_STRING_SLICE_ARG(dirname), - WABT_PRINTF_STRING_SLICE_ARG(filename)); + path = dirname.to_string(); + path += '/'; + path += filename.to_string(); } - ConvertBackslashToSlash(path, path_len); + ConvertBackslashToSlash(&path); return path; } static wabt::Result on_module_command(Context* ctx, - StringSlice filename, - StringSlice name) { - char* path = create_module_path(ctx, filename); + string_view filename, + string_view name) { + std::string path = CreateModulePath(ctx, filename); Environment::MarkPoint mark = ctx->env.Mark(); ErrorHandlerFile error_handler(Location::Type::Binary); wabt::Result result = - read_module(path, &ctx->env, &error_handler, &ctx->last_module); + read_module(path.c_str(), &ctx->env, &error_handler, &ctx->last_module); if (Failed(result)) { ctx->env.ResetToMarkPoint(mark); - print_command_error(ctx, "error reading module: \"%s\"", path); - delete[] path; + print_command_error(ctx, "error reading module: \"%s\"", path.c_str()); return wabt::Result::Error; } - delete[] path; - interpreter::Result iresult = run_start_function(&ctx->thread, ctx->last_module); if (iresult != interpreter::Result::Ok) { @@ -916,9 +891,9 @@ static wabt::Result on_module_command(Context* ctx, return wabt::Result::Error; } - if (!string_slice_is_empty(&name)) { - ctx->last_module->name = dup_string_slice(name); - ctx->env.EmplaceModuleBinding(string_slice_to_string(name), + if (!name.empty()) { + ctx->last_module->name = name.to_string(); + ctx->env.EmplaceModuleBinding(name.to_string(), Binding(ctx->env.GetModuleCount() - 1)); } return wabt::Result::Ok; @@ -932,7 +907,7 @@ static wabt::Result run_action(Context* ctx, out_results->clear(); Module* module; - if (!string_slice_is_empty(&action->module_name)) { + if (!action->module_name.empty()) { module = ctx->env.FindModule(action->module_name); } else { module = ctx->env.GetLastModule(); @@ -942,17 +917,17 @@ static wabt::Result run_action(Context* ctx, switch (action->type) { case ActionType::Invoke: *out_iresult = - run_export_by_name(&ctx->thread, module, &action->field_name, + run_export_by_name(&ctx->thread, module, action->field_name, action->args, out_results, verbose); if (verbose == RunVerbosity::Verbose) { - print_call(empty_string_slice(), action->field_name, action->args, + print_call(string_view(), action->field_name, action->args, *out_results, *out_iresult); } return wabt::Result::Ok; case ActionType::Get: { - *out_iresult = get_global_export_by_name( - &ctx->thread, module, &action->field_name, out_results); + *out_iresult = get_global_export_by_name(&ctx->thread, module, + action->field_name, out_results); return wabt::Result::Ok; } @@ -998,8 +973,8 @@ static wabt::Result read_invalid_module(Context* ctx, ModuleType module_type, const char* desc) { std::string header = - string_printf(PRIstringslice ":%d: %s passed", - WABT_PRINTF_STRING_SLICE_ARG(ctx->source_filename), + string_printf(PRIstringview ":%d: %s passed", + WABT_PRINTF_STRING_VIEW_ARG(ctx->source_filename), ctx->command_line_number, desc); switch (module_type) { @@ -1022,33 +997,33 @@ static wabt::Result read_invalid_module(Context* ctx, } static wabt::Result on_assert_malformed_command(Context* ctx, - StringSlice filename, - StringSlice text, + string_view filename, + string_view text, ModuleType module_type) { Environment env; init_environment(&env); ctx->total++; - char* path = create_module_path(ctx, filename); - wabt::Result result = - read_invalid_module(ctx, path, &env, module_type, "assert_malformed"); + std::string path = CreateModulePath(ctx, filename); + wabt::Result result = read_invalid_module(ctx, path.c_str(), &env, + module_type, "assert_malformed"); if (Failed(result)) { ctx->passed++; result = wabt::Result::Ok; } else { - print_command_error(ctx, "expected module to be malformed: \"%s\"", path); + print_command_error(ctx, "expected module to be malformed: \"%s\"", + path.c_str()); result = wabt::Result::Error; } - delete[] path; return result; } static wabt::Result on_register_command(Context* ctx, - StringSlice name, - StringSlice as) { + string_view name, + string_view as) { Index module_index; - if (!string_slice_is_empty(&name)) { + if (!name.empty()) { module_index = ctx->env.FindModuleIndex(name); } else { module_index = ctx->env.GetLastModuleIndex(); @@ -1059,85 +1034,85 @@ static wabt::Result on_register_command(Context* ctx, return wabt::Result::Error; } - ctx->env.EmplaceRegisteredModuleBinding(string_slice_to_string(as), + ctx->env.EmplaceRegisteredModuleBinding(as.to_string(), Binding(module_index)); return wabt::Result::Ok; } static wabt::Result on_assert_unlinkable_command(Context* ctx, - StringSlice filename, - StringSlice text, + string_view filename, + string_view text, ModuleType module_type) { ctx->total++; - char* path = create_module_path(ctx, filename); + std::string path = CreateModulePath(ctx, filename); Environment::MarkPoint mark = ctx->env.Mark(); - wabt::Result result = read_invalid_module(ctx, path, &ctx->env, module_type, - "assert_unlinkable"); + wabt::Result result = read_invalid_module(ctx, path.c_str(), &ctx->env, + module_type, "assert_unlinkable"); ctx->env.ResetToMarkPoint(mark); if (Failed(result)) { ctx->passed++; result = wabt::Result::Ok; } else { - print_command_error(ctx, "expected module to be unlinkable: \"%s\"", path); + print_command_error(ctx, "expected module to be unlinkable: \"%s\"", + path.c_str()); result = wabt::Result::Error; } - delete[] path; return result; } static wabt::Result on_assert_invalid_command(Context* ctx, - StringSlice filename, - StringSlice text, + string_view filename, + string_view text, ModuleType module_type) { Environment env; init_environment(&env); ctx->total++; - char* path = create_module_path(ctx, filename); - wabt::Result result = - read_invalid_module(ctx, path, &env, module_type, "assert_invalid"); + std::string path = CreateModulePath(ctx, filename); + wabt::Result result = read_invalid_module(ctx, path.c_str(), &env, + module_type, "assert_invalid"); if (Failed(result)) { ctx->passed++; result = wabt::Result::Ok; } else { - print_command_error(ctx, "expected module to be invalid: \"%s\"", path); + print_command_error(ctx, "expected module to be invalid: \"%s\"", + path.c_str()); result = wabt::Result::Error; } - delete[] path; return result; } static wabt::Result on_assert_uninstantiable_command(Context* ctx, - StringSlice filename, - StringSlice text, + string_view filename, + string_view text, ModuleType module_type) { ErrorHandlerFile error_handler(Location::Type::Binary); ctx->total++; - char* path = create_module_path(ctx, filename); + std::string path = CreateModulePath(ctx, filename); DefinedModule* module; Environment::MarkPoint mark = ctx->env.Mark(); - wabt::Result result = read_module(path, &ctx->env, &error_handler, &module); + wabt::Result result = + read_module(path.c_str(), &ctx->env, &error_handler, &module); if (Succeeded(result)) { interpreter::Result iresult = run_start_function(&ctx->thread, module); if (iresult == interpreter::Result::Ok) { print_command_error(ctx, "expected error running start function: \"%s\"", - path); + path.c_str()); result = wabt::Result::Error; } else { ctx->passed++; result = wabt::Result::Ok; } } else { - print_command_error(ctx, "error reading module: \"%s\"", path); + print_command_error(ctx, "error reading module: \"%s\"", path.c_str()); result = wabt::Result::Error; } ctx->env.ResetToMarkPoint(mark); - delete[] path; return result; } @@ -1278,7 +1253,7 @@ static wabt::Result on_assert_return_nan_command(Context* ctx, static wabt::Result on_assert_trap_command(Context* ctx, Action* action, - StringSlice text) { + string_view text) { std::vector<TypedValue> results; interpreter::Result iresult; @@ -1289,8 +1264,8 @@ static wabt::Result on_assert_trap_command(Context* ctx, if (iresult != interpreter::Result::Ok) { ctx->passed++; } else { - print_command_error(ctx, "expected trap: \"" PRIstringslice "\"", - WABT_PRINTF_STRING_SLICE_ARG(text)); + print_command_error(ctx, "expected trap: \"" PRIstringview "\"", + WABT_PRINTF_STRING_VIEW_ARG(text)); result = wabt::Result::Error; } } @@ -1322,10 +1297,8 @@ static wabt::Result parse_command(Context* ctx) { EXPECT("{"); EXPECT_KEY("type"); if (match(ctx, "\"module\"")) { - StringSlice name; - StringSlice filename; - ZeroMemory(name); - ZeroMemory(filename); + std::string name; + std::string filename; EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1342,10 +1315,8 @@ static wabt::Result parse_command(Context* ctx) { CHECK_RESULT(parse_action(ctx, &action)); on_action_command(ctx, &action); } else if (match(ctx, "\"register\"")) { - StringSlice as; - StringSlice name; - ZeroMemory(as); - ZeroMemory(name); + std::string as; + std::string name; EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1354,11 +1325,9 @@ static wabt::Result parse_command(Context* ctx) { PARSE_KEY_STRING_VALUE("as", &as); on_register_command(ctx, name, as); } else if (match(ctx, "\"assert_malformed\"")) { - StringSlice filename; - StringSlice text; + std::string filename; + std::string text; ModuleType module_type; - ZeroMemory(filename); - ZeroMemory(text); EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1370,11 +1339,9 @@ static wabt::Result parse_command(Context* ctx) { CHECK_RESULT(parse_module_type(ctx, &module_type)); on_assert_malformed_command(ctx, filename, text, module_type); } else if (match(ctx, "\"assert_invalid\"")) { - StringSlice filename; - StringSlice text; + std::string filename; + std::string text; ModuleType module_type; - ZeroMemory(filename); - ZeroMemory(text); EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1386,11 +1353,9 @@ static wabt::Result parse_command(Context* ctx) { CHECK_RESULT(parse_module_type(ctx, &module_type)); on_assert_invalid_command(ctx, filename, text, module_type); } else if (match(ctx, "\"assert_unlinkable\"")) { - StringSlice filename; - StringSlice text; + std::string filename; + std::string text; ModuleType module_type; - ZeroMemory(filename); - ZeroMemory(text); EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1402,11 +1367,9 @@ static wabt::Result parse_command(Context* ctx) { CHECK_RESULT(parse_module_type(ctx, &module_type)); on_assert_unlinkable_command(ctx, filename, text, module_type); } else if (match(ctx, "\"assert_uninstantiable\"")) { - StringSlice filename; - StringSlice text; + std::string filename; + std::string text; ModuleType module_type; - ZeroMemory(filename); - ZeroMemory(text); EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1457,8 +1420,7 @@ static wabt::Result parse_command(Context* ctx) { on_assert_return_nan_command(ctx, &action, false); } else if (match(ctx, "\"assert_trap\"")) { Action action; - StringSlice text; - ZeroMemory(text); + std::string text; EXPECT(","); CHECK_RESULT(parse_line(ctx)); @@ -1469,8 +1431,7 @@ static wabt::Result parse_command(Context* ctx) { on_assert_trap_command(ctx, &action, text); } else if (match(ctx, "\"assert_exhaustion\"")) { Action action; - StringSlice text; - ZeroMemory(text); + std::string text; EXPECT(","); CHECK_RESULT(parse_line(ctx)); |