Skip to content

Commit 1d0fa77

Browse files
authored
allow exporting an import (#1326)
1 parent 9c51f2b commit 1d0fa77

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

src/wasm/wasm-validator.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -926,16 +926,13 @@ static void validateExports(Module& module, ValidationInfo& info) {
926926
for (auto& exp : module.exports) {
927927
Name name = exp->value;
928928
if (exp->kind == ExternalKind::Function) {
929-
bool found = false;
930-
for (auto& func : module.functions) {
931-
if (func->name == name) {
932-
found = true;
933-
break;
934-
}
935-
}
936-
info.shouldBeTrue(found, name, "module function exports must be found");
929+
Import* imp;
930+
info.shouldBeTrue(module.getFunctionOrNull(name) ||
931+
((imp = module.getImportOrNull(name)) && imp->kind == ExternalKind::Function), name, "module function exports must be found");
937932
} else if (exp->kind == ExternalKind::Global) {
938-
info.shouldBeTrue(module.getGlobalOrNull(name), name, "module global exports must be found");
933+
Import* imp;
934+
info.shouldBeTrue(module.getGlobalOrNull(name) ||
935+
((imp = module.getImportOrNull(name)) && imp->kind == ExternalKind::Global), name, "module global exports must be found");
939936
} else if (exp->kind == ExternalKind::Table) {
940937
info.shouldBeTrue(name == Name("0") || name == module.table.name, name, "module table exports must be found");
941938
} else if (exp->kind == ExternalKind::Memory) {

test/export-import.wast

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(type $v (func))
3+
(import "env" "test1" (func $test1))
4+
(import "env" "test2" (global $test2 i32))
5+
(export "test1" (func $test1))
6+
(export "test2" (global $test2))
7+
)
8+

test/export-import.wast.from-wast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(module
2+
(type $v (func))
3+
(type $FUNCSIG$v (func))
4+
(import "env" "test1" (func $test1))
5+
(import "env" "test2" (global $test2 i32))
6+
(memory $0 0)
7+
(export "test1" (func $test1))
8+
(export "test2" (global $test2))
9+
)

test/export-import.wast.fromBinary

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(type $0 (func))
3+
(type $1 (func))
4+
(import "env" "test2" (global $import$1 i32))
5+
(import "env" "test1" (func $test1))
6+
(memory $0 0)
7+
(export "test1" (func $test1))
8+
(export "test2" (global $import$1))
9+
)
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(type $0 (func))
3+
(type $1 (func))
4+
(import "env" "test2" (global $import$1 i32))
5+
(import "env" "test1" (func $import$0))
6+
(memory $0 0)
7+
(export "test1" (func $import$0))
8+
(export "test2" (global $import$1))
9+
)
10+

0 commit comments

Comments
 (0)