summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBen Smith <binjimin@gmail.com>2017-12-19 16:27:49 -0800
committerGitHub <noreply@github.com>2017-12-19 16:27:49 -0800
commitf835db8cfb418bb19dd61a10dbe49e1131b664bc (patch)
tree5dfaf9545d45e0d1787aa890e2d568bda9ee208f /src
parent59341472102801d2ccb9d7d955af46729e6cd2ae (diff)
downloadwabt-f835db8cfb418bb19dd61a10dbe49e1131b664bc.tar.gz
wabt-f835db8cfb418bb19dd61a10dbe49e1131b664bc.tar.bz2
wabt-f835db8cfb418bb19dd61a10dbe49e1131b664bc.zip
Fix bug when writing inline exports for import (#700)
The `(import...` syntax doesn't allow for inline exports, but the wat writer assumed that all exports would be written inline, so the exports would be omitted. This CL fixes it by writing the exports normally: ``` (module (import "foo" "bar" (func)) (export "baz" (func 0)) ) ``` It is also possible to fix this by writing the imports inline, but this is not currently supported by the .wat writer: ``` (module (func (export "baz") (import "foo" "bar")) ) ```
Diffstat (limited to 'src')
-rw-r--r--src/ir.cc22
-rw-r--r--src/ir.h5
-rw-r--r--src/wat-writer.cc5
3 files changed, 31 insertions, 1 deletions
diff --git a/src/ir.cc b/src/ir.cc
index 2746a17b..8482f281 100644
--- a/src/ir.cc
+++ b/src/ir.cc
@@ -112,6 +112,28 @@ Index Module::GetExceptIndex(const Var& var) const {
return except_bindings.FindIndex(var);
}
+bool Module::IsImport(ExternalKind kind, const Var& var) const {
+ switch (kind) {
+ case ExternalKind::Func:
+ return GetFuncIndex(var) < num_func_imports;
+
+ case ExternalKind::Global:
+ return GetGlobalIndex(var) < num_global_imports;
+
+ case ExternalKind::Memory:
+ return GetMemoryIndex(var) < num_memory_imports;
+
+ case ExternalKind::Table:
+ return GetTableIndex(var) < num_table_imports;
+
+ case ExternalKind::Except:
+ return GetExceptIndex(var) < num_except_imports;
+
+ default:
+ return false;
+ }
+}
+
Index Func::GetLocalIndex(const Var& var) const {
if (var.is_index()) {
return var.index();
diff --git a/src/ir.h b/src/ir.h
index 39c065ea..0db30a6c 100644
--- a/src/ir.h
+++ b/src/ir.h
@@ -684,6 +684,11 @@ struct Module {
Exception* GetExcept(const Var&) const;
Index GetExceptIndex(const Var&) const;
+ bool IsImport(ExternalKind kind, const Var&) const;
+ bool IsImport(const Export& export_) const {
+ return IsImport(export_.kind, export_.var);
+ }
+
// TODO(binji): move this into a builder class?
void AppendField(std::unique_ptr<DataSegmentModuleField>);
void AppendField(std::unique_ptr<ElemSegmentModuleField>);
diff --git a/src/wat-writer.cc b/src/wat-writer.cc
index 7954989c..7d4e1b29 100644
--- a/src/wat-writer.cc
+++ b/src/wat-writer.cc
@@ -1180,7 +1180,10 @@ void WatWriter::WriteImport(const Import& import) {
void WatWriter::WriteExport(const Export& export_) {
if (options_->inline_export) {
- return;
+ // Exported imports can't be written with inline exports.
+ if (!module_->IsImport(export_)) {
+ return;
+ }
}
WriteOpenSpace("export");
WriteQuotedString(export_.name, NextChar::Space);