diff options
author | Ben Smith <binjimin@gmail.com> | 2018-05-01 10:55:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-01 10:55:45 -0700 |
commit | a0bdeb720a1cdcff2b2045aa0a0e0e33a5a5e8a9 (patch) | |
tree | 351c74bc6abebbe3b599509a7b84533f08bc8b99 /src | |
parent | 2ec0d8536daedca867b188d88c47e45cd3b38c64 (diff) | |
download | wabt-a0bdeb720a1cdcff2b2045aa0a0e0e33a5a5e8a9.tar.gz wabt-a0bdeb720a1cdcff2b2045aa0a0e0e33a5a5e8a9.tar.bz2 wabt-a0bdeb720a1cdcff2b2045aa0a0e0e33a5a5e8a9.zip |
Read and write module names in the names section (#831)
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-reader-ir.cc | 18 | ||||
-rw-r--r-- | src/binary-reader-logging.cc | 15 | ||||
-rw-r--r-- | src/binary-reader-logging.h | 4 | ||||
-rw-r--r-- | src/binary-reader-nop.h | 6 | ||||
-rw-r--r-- | src/binary-reader.cc | 8 | ||||
-rw-r--r-- | src/binary-reader.h | 4 | ||||
-rw-r--r-- | src/binary-writer.cc | 7 | ||||
-rw-r--r-- | src/binary.h | 1 | ||||
-rw-r--r-- | src/wat-writer.cc | 7 |
9 files changed, 67 insertions, 3 deletions
diff --git a/src/binary-reader-ir.cc b/src/binary-reader-ir.cc index 9c9c5203..33c2ff5d 100644 --- a/src/binary-reader-ir.cc +++ b/src/binary-reader-ir.cc @@ -200,6 +200,7 @@ class BinaryReaderIR : public BinaryReaderNop { const void* data, Address size) override; + Result OnModuleName(string_view module_name) override; Result OnFunctionNamesCount(Index num_functions) override; Result OnFunctionName(Index function_index, string_view function_name) override; @@ -935,13 +936,26 @@ Result BinaryReaderIR::OnFunctionNamesCount(Index count) { return Result::Ok; } +static std::string MakeDollarName(string_view name) { + return std::string("$") + name.to_string(); +} + +Result BinaryReaderIR::OnModuleName(string_view name) { + if (name.empty()) { + return Result::Ok; + } + + module_->name = MakeDollarName(name); + return Result::Ok; +} + Result BinaryReaderIR::OnFunctionName(Index index, string_view name) { if (name.empty()) { return Result::Ok; } Func* func = module_->funcs[index]; - std::string dollar_name = std::string("$") + name.to_string(); + std::string dollar_name = MakeDollarName(name); int counter = 1; std::string orig_name = dollar_name; while (module_->func_bindings.count(dollar_name) != 0) { @@ -1028,7 +1042,7 @@ Result BinaryReaderIR::OnLocalName(Index func_index, bindings = &func->local_bindings; index = local_index - num_params; } - bindings->emplace(std::string("$") + name.to_string(), Binding(index)); + bindings->emplace(MakeDollarName(name), Binding(index)); return Result::Ok; } diff --git a/src/binary-reader-logging.cc b/src/binary-reader-logging.cc index 03a17cd6..9aa14f79 100644 --- a/src/binary-reader-logging.cc +++ b/src/binary-reader-logging.cc @@ -372,6 +372,21 @@ Result BinaryReaderLogging::OnDataSegmentData(Index index, return reader_->OnDataSegmentData(index, data, size); } +Result BinaryReaderLogging::OnModuleNameSubsection(Index index, + uint32_t name_type, + Offset subsection_size) { + LOGF("OnModuleNameSubsection(index:%" PRIindex ", nametype:%u, size:%" PRIzd + ")\n", + index, name_type, subsection_size); + return reader_->OnModuleNameSubsection(index, name_type, subsection_size); +} + +Result BinaryReaderLogging::OnModuleName(string_view name) { + LOGF("OnModuleName(name: \"" PRIstringview "\")\n", + WABT_PRINTF_STRING_VIEW_ARG(name)); + return reader_->OnModuleName(name); +} + Result BinaryReaderLogging::OnFunctionNameSubsection(Index index, uint32_t name_type, Offset subsection_size) { diff --git a/src/binary-reader-logging.h b/src/binary-reader-logging.h index 2acf7ae5..37893f8b 100644 --- a/src/binary-reader-logging.h +++ b/src/binary-reader-logging.h @@ -227,6 +227,10 @@ class BinaryReaderLogging : public BinaryReaderDelegate { Result EndDataSection() override; Result BeginNamesSection(Offset size) override; + Result OnModuleNameSubsection(Index index, + uint32_t name_type, + Offset subsection_size) override; + Result OnModuleName(string_view name) override; Result OnFunctionNameSubsection(Index index, uint32_t name_type, Offset subsection_size) override; diff --git a/src/binary-reader-nop.h b/src/binary-reader-nop.h index f0a2e4e5..e2c80d50 100644 --- a/src/binary-reader-nop.h +++ b/src/binary-reader-nop.h @@ -310,6 +310,12 @@ class BinaryReaderNop : public BinaryReaderDelegate { /* Names section */ Result BeginNamesSection(Offset size) override { return Result::Ok; } + Result OnModuleNameSubsection(Index index, + uint32_t name_type, + Offset subsection_size) override { + return Result::Ok; + } + Result OnModuleName(string_view name) override { return Result::Ok; } Result OnFunctionNameSubsection(Index index, uint32_t name_type, Offset subsection_size) override { diff --git a/src/binary-reader.cc b/src/binary-reader.cc index e377ae08..620a0c71 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -1330,6 +1330,14 @@ Result BinaryReader::ReadNameSection(Offset section_size) { read_end_ = subsection_end; switch (static_cast<NameSectionSubsection>(name_type)) { + case NameSectionSubsection::Module: + CALLBACK(OnModuleNameSubsection, i, name_type, subsection_size); + if (subsection_size) { + string_view name; + CHECK_RESULT(ReadStr(&name, "module name")); + CALLBACK(OnModuleName, name); + } + break; case NameSectionSubsection::Function: CALLBACK(OnFunctionNameSubsection, i, name_type, subsection_size); if (subsection_size) { diff --git a/src/binary-reader.h b/src/binary-reader.h index 89169271..5dea6c77 100644 --- a/src/binary-reader.h +++ b/src/binary-reader.h @@ -280,6 +280,10 @@ class BinaryReaderDelegate { /* Names section */ virtual Result BeginNamesSection(Offset size) = 0; + virtual Result OnModuleNameSubsection(Index index, + uint32_t name_type, + Offset subsection_size) = 0; + virtual Result OnModuleName(string_view name) = 0; virtual Result OnFunctionNameSubsection(Index index, uint32_t name_type, Offset subsection_size) = 0; diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 7136c60c..a64522bc 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -977,6 +977,13 @@ Result BinaryWriter::WriteModule() { } } + if (!module_->name.empty()) { + WriteU32Leb128(stream_, 0, "module name type"); + BeginSubsection("module name subsection"); + WriteDebugName(stream_, module_->name, "module name"); + EndSubsection(); + } + if (named_functions > 0) { WriteU32Leb128(stream_, 1, "function name type"); BeginSubsection("function name subsection"); diff --git a/src/binary.h b/src/binary.h index 9f21c0ac..12e1481a 100644 --- a/src/binary.h +++ b/src/binary.h @@ -59,6 +59,7 @@ enum class BinarySection { static const int kBinarySectionCount = WABT_ENUM_COUNT(BinarySection); enum class NameSectionSubsection { + Module = 0, Function = 1, Local = 2, }; diff --git a/src/wat-writer.cc b/src/wat-writer.cc index 0f52e6a4..ba681139 100644 --- a/src/wat-writer.cc +++ b/src/wat-writer.cc @@ -1458,7 +1458,12 @@ Result WatWriter::WriteModule(const Module& module) { module_ = &module; BuildInlineExportMap(); BuildInlineImportMap(); - WriteOpenNewline("module"); + WriteOpenSpace("module"); + if (module.name.empty()) { + WriteNewline(NO_FORCE_NEWLINE); + } else { + WriteName(module.name, NextChar::Newline); + } for (const ModuleField& field : module.fields) { switch (field.type()) { case ModuleFieldType::Func: |