Skip to content

Commit 2a4c832

Browse files
Fix for duplicate imports
1 parent d2469f2 commit 2a4c832

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

src/tools/wasm-ctor-eval.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,20 @@ std::vector<std::unique_ptr<Module>> buildStubModules(Module& wasm) {
140140
Module* module = it->second.get();
141141

142142
struct Visitor {
143-
Module* module;
143+
Module* module = nullptr;
144+
144145
void operator()(Memory* memory) {
145146
auto* copied = ModuleUtils::copyMemory(memory, *module);
146147
copied->module = Name();
147148
copied->base = Name();
148-
module->addExport(Builder(*module).makeExport(
149+
module->maybeAddExport(Builder::makeExport(
149150
memory->base, copied->name, ExternalKind::Memory));
150151
}
151152
void operator()(Table* table) {
152-
// create tables with similar initial and max values
153153
auto* copied = ModuleUtils::copyTable(table, *module);
154154
copied->module = Name();
155155
copied->base = Name();
156-
module->addExport(Builder(*module).makeExport(
156+
module->maybeAddExport(Builder::makeExport(
157157
table->base, copied->name, ExternalKind::Table));
158158
}
159159
void operator()(Global* global) {
@@ -163,17 +163,14 @@ std::vector<std::unique_ptr<Module>> buildStubModules(Module& wasm) {
163163

164164
Builder builder(*module);
165165
copied->init = builder.makeConst(Literal::makeZero(global->type));
166-
module->addExport(builder.makeExport(
166+
module->maybeAddExport(Builder::makeExport(
167167
global->base, copied->name, ExternalKind::Global));
168168
}
169169
void operator()(Function* func) {
170170
Builder builder(*module);
171-
auto* copied = ModuleUtils::copyFunction(func, *module);
172-
copied->module = Name();
173-
copied->base = Name();
174-
copied->body = builder.makeUnreachable();
175-
module->addExport(builder.makeExport(
176-
func->base, copied->name, ExternalKind::Function));
171+
auto stubbedFunc = builder.makeUnreachableFunction(func->name);
172+
module->maybeAddExport(Builder::makeExport(
173+
func->base, stubbedFunc->name, ExternalKind::Function));
177174
}
178175
void operator()(Tag* tag) {
179176
// no-op

src/wasm-builder.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@ class Builder {
4343

4444
// make* functions create an expression instance.
4545

46+
std::unique_ptr<Function> makeUnreachableFunction(Name name) {
47+
auto func = std::make_unique<Function>();
48+
func->name = name;
49+
func->type = Type::unreachable;
50+
func->body = makeUnreachable();
51+
return func;
52+
}
53+
4654
static std::unique_ptr<Function> makeFunction(Name name,
4755
Type type,
4856
std::vector<Type>&& vars,
4957
Expression* body = nullptr) {
50-
assert(type.isSignature() && type.isNonNullable());
58+
// assert(type.isSignature() && type.isNonNullable());
5159
auto func = std::make_unique<Function>();
5260
func->name = name;
5361
func->type = type;

src/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,7 @@ class Module {
25962596
Tag* addTag(Tag* curr);
25972597

25982598
Export* addExport(std::unique_ptr<Export>&& curr);
2599+
Export* maybeAddExport(std::unique_ptr<Export>&& curr);
25992600
Function* addFunction(std::unique_ptr<Function>&& curr);
26002601
Table* addTable(std::unique_ptr<Table>&& curr);
26012602
ElementSegment* addElementSegment(std::unique_ptr<ElementSegment>&& curr);

src/wasm/wasm.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,22 @@ Elem* addModuleElement(Vector& v,
18291829
return ret;
18301830
}
18311831

1832+
template<typename Vector, typename Map, typename Elem>
1833+
Elem* maybeAddModuleElement(Vector& v,
1834+
Map& m,
1835+
std::unique_ptr<Elem> curr,
1836+
std::string funcName) {
1837+
if (!curr->name.is()) {
1838+
Fatal() << "Module::" << funcName << ": empty name";
1839+
}
1840+
if (getModuleElementOrNull(m, curr->name)) {
1841+
return nullptr;
1842+
}
1843+
auto* ret = m[curr->name] = curr.get();
1844+
v.push_back(std::move(curr));
1845+
return ret;
1846+
}
1847+
18321848
Export* Module::addExport(Export* curr) {
18331849
return addModuleElement(exports, exportsMap, curr, "addExport");
18341850
}
@@ -1849,6 +1865,11 @@ Export* Module::addExport(std::unique_ptr<Export>&& curr) {
18491865
return addModuleElement(exports, exportsMap, std::move(curr), "addExport");
18501866
}
18511867

1868+
Export* Module::maybeAddExport(std::unique_ptr<Export>&& curr) {
1869+
return maybeAddModuleElement(
1870+
exports, exportsMap, std::move(curr), "addExport");
1871+
}
1872+
18521873
Function* Module::addFunction(std::unique_ptr<Function>&& curr) {
18531874
return addModuleElement(
18541875
functions, functionsMap, std::move(curr), "addFunction");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(module
2+
(import "import" "log" (func $logi32 (param i32)))
3+
(import "import" "log" (func $logi64 (param i64)))
4+
(export "foo" (func $0))
5+
(func $0 (param $0 i32)
6+
(local.get $0)
7+
(call $logi32)
8+
)
9+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
(type $0 (func (param i32)))
3+
(import "import" "log" (func $logi32 (type $0) (param i32)))
4+
(export "foo" (func $0))
5+
(func $0 (type $0) (param $0 i32)
6+
(call $logi32
7+
(local.get $0)
8+
)
9+
)
10+
)

0 commit comments

Comments
 (0)