Skip to content

Commit c4001da

Browse files
wip
1 parent 07bf6f7 commit c4001da

File tree

5 files changed

+65
-65
lines changed

5 files changed

+65
-65
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
#ifndef wasm_ir_import_name_h
18-
#define wasm_ir_import_name_h
17+
#ifndef wasm_ir_import_names_h
18+
#define wasm_ir_import_names_h
1919

2020
#include <ostream>
2121

@@ -47,4 +47,4 @@ template<> struct hash<wasm::ImportNames> {
4747

4848
} // namespace std
4949

50-
#endif // wasm_ir_import_name_h
50+
#endif // wasm_ir_import_names_h

src/ir/import-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#ifndef wasm_ir_import_h
1818
#define wasm_ir_import_h
1919

20-
#include "ir/import-name.h"
20+
#include "ir/import-names.h"
2121
#include "ir/runtime-table.h"
2222
#include "literal.h"
2323
#include "wasm.h"

src/tools/wasm-ctor-eval.cpp

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -68,40 +68,6 @@ bool isNullableAndMutable(Expression* ref, Index fieldIndex) {
6868
// the output.
6969
#define RECOMMENDATION "\n recommendation: "
7070

71-
class EvallingImportResolver : public ImportResolver {
72-
public:
73-
EvallingImportResolver() : stubLiteral({Literal(0)}) {};
74-
75-
// Return an unused stub value. We throw FailToEvalException on reading any
76-
// imported globals. We ignore the type and return an i32 literal since some
77-
// types can't be created anyway (e.g. ref none).
78-
Literals* getGlobalOrNull(ImportNames name, Type type) const override {
79-
return &stubLiteral;
80-
}
81-
82-
RuntimeTable* getTableOrNull(ImportNames name,
83-
const Table& type) const override {
84-
throw FailToEvalException{"Imported table access."};
85-
}
86-
87-
// We assume that each tag import is distinct. This is wrong if the same tag
88-
// instantiation is imported twice with different import names.
89-
Tag* getTagOrNull(ImportNames name,
90-
const Signature& signature) const override {
91-
auto [it, inserted] = importedTags.try_emplace(name, Tag{});
92-
if (inserted) {
93-
auto& tag = it->second;
94-
tag.type = HeapType(signature);
95-
}
96-
97-
return &it->second;
98-
}
99-
100-
private:
101-
mutable Literals stubLiteral;
102-
mutable std::unordered_map<ImportNames, Tag> importedTags;
103-
};
104-
10571
class EvallingRuntimeTable : public RuntimeTable {
10672
public:
10773
// TODO: putting EvallingModuleRunner into its own header would allow us to
@@ -180,6 +146,53 @@ class EvallingRuntimeTable : public RuntimeTable {
180146
const std::function<Literal(Name, Type)> makeFuncData;
181147
};
182148

149+
class EvallingImportResolver : public ImportResolver {
150+
public:
151+
EvallingImportResolver(const bool& instanceInitialized,
152+
const Module& wasm,
153+
std::function<Literal(Name, Type)> makeFuncData)
154+
: stubLiteral({Literal(0)}), instanceInitialized(instanceInitialized),
155+
wasm(wasm), makeFuncData(makeFuncData) {};
156+
157+
// Return an unused stub value. We throw FailToEvalException on reading any
158+
// imported globals. We ignore the type and return an i32 literal since some
159+
// types can't be created anyway (e.g. ref none).
160+
Literals* getGlobalOrNull(ImportNames name, Type type) const override {
161+
return &stubLiteral;
162+
}
163+
164+
RuntimeTable* getTableOrNull(ImportNames name,
165+
const Table& type) const override {
166+
auto [it, inserted] =
167+
tables.emplace(name,
168+
std::make_unique<EvallingRuntimeTable>(
169+
type, instanceInitialized, wasm, makeFuncData));
170+
return it->second.get();
171+
}
172+
173+
// We assume that each tag import is distinct. This is wrong if the same tag
174+
// instantiation is imported twice with different import names.
175+
Tag* getTagOrNull(ImportNames name,
176+
const Signature& signature) const override {
177+
auto [it, inserted] = importedTags.try_emplace(name, Tag{});
178+
if (inserted) {
179+
auto& tag = it->second;
180+
tag.type = HeapType(signature);
181+
}
182+
183+
return &it->second;
184+
}
185+
186+
private:
187+
mutable Literals stubLiteral;
188+
mutable std::unordered_map<ImportNames, std::unique_ptr<EvallingRuntimeTable>>
189+
tables;
190+
const bool& instanceInitialized;
191+
const Module& wasm;
192+
const std::function<Literal(Name, Type)> makeFuncData;
193+
mutable std::unordered_map<ImportNames, Tag> importedTags;
194+
};
195+
183196
class EvallingModuleRunner : public ModuleRunnerBase<EvallingModuleRunner> {
184197
public:
185198
EvallingModuleRunner(
@@ -190,17 +203,11 @@ class EvallingModuleRunner : public ModuleRunnerBase<EvallingModuleRunner> {
190203
: ModuleRunnerBase(
191204
wasm,
192205
externalInterface,
193-
std::make_shared<EvallingImportResolver>(),
194-
linkedInstances_,
195-
// TODO: Only use EvallingRuntimeTable for table imports. We can use
196-
// RealRuntimeTable for non-imported tables.
197-
[this, &instanceInitialized](Literal initial, Table table) {
198-
return std::make_unique<EvallingRuntimeTable>(
199-
table,
200-
instanceInitialized,
201-
this->wasm,
202-
[this](Name name, Type type) { return makeFuncData(name, type); });
203-
}) {}
206+
std::make_shared<EvallingImportResolver>(
207+
instanceInitialized,
208+
wasm,
209+
[this](Name name, Type type) { return makeFuncData(name, type); }),
210+
linkedInstances_) {}
204211

205212
Flow visitGlobalGet(GlobalGet* curr) {
206213
// Error on reads of imported globals.
@@ -1166,6 +1173,10 @@ EvalCtorOutcome evalCtor(EvallingModuleRunner& instance,
11661173
std::cout << " ...stopping due to non-constant func\n";
11671174
}
11681175
break;
1176+
} catch (TrapException& trap) {
1177+
if (!quiet) {
1178+
std::cout << " ...stopping due to trap\n";
1179+
}
11691180
}
11701181

11711182
if (flow.breakTo == NONCONSTANT_FLOW) {

src/wasm-interpreter.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,25 +3167,15 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
31673167

31683168
std::unordered_map<Name, Tag*> allTags;
31693169

3170-
using CreateTableFunc = std::unique_ptr<RuntimeTable>(Literal, Table);
3171-
31723170
ModuleRunnerBase(
31733171
Module& wasm,
31743172
ExternalInterface* externalInterface,
31753173
std::shared_ptr<ImportResolver> importResolver,
3176-
std::map<Name, std::shared_ptr<SubType>> linkedInstances_ = {},
3177-
std::function<CreateTableFunc> createTable = {})
3174+
std::map<Name, std::shared_ptr<SubType>> linkedInstances_ = {})
31783175
: ExpressionRunner<SubType>(&wasm), wasm(wasm),
31793176
externalInterface(externalInterface),
31803177
linkedInstances(std::move(linkedInstances_)),
3181-
importResolver(std::move(importResolver)),
3182-
createTable(
3183-
createTable != nullptr
3184-
? std::move(createTable)
3185-
: static_cast<std::function<CreateTableFunc>>(
3186-
[](Literal initial, Table t) -> std::unique_ptr<RuntimeTable> {
3187-
return std::make_unique<RealRuntimeTable>(initial, t);
3188-
})) {
3178+
importResolver(std::move(importResolver)) {
31893179
// Set up a single shared CurrContinuations for all these linked instances,
31903180
// reusing one if it exists.
31913181
std::shared_ptr<ContinuationStore> shared;
@@ -3523,8 +3513,8 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
35233513
"We only support nullable tables today");
35243514

35253515
auto null = Literal::makeNull(table->type.getHeapType());
3526-
auto& runtimeTable =
3527-
definedTables.emplace_back(createTable(null, *table));
3516+
auto& runtimeTable = definedTables.emplace_back(
3517+
std::make_unique<RealRuntimeTable>(null, *table));
35283518
[[maybe_unused]] auto [_, inserted] =
35293519
allTables.try_emplace(table->name, runtimeTable.get());
35303520
assert(inserted && "Unexpected repeated table name");
@@ -5235,7 +5225,6 @@ class ModuleRunnerBase : public ExpressionRunner<SubType> {
52355225
ExternalInterface* externalInterface;
52365226
std::map<Name, std::shared_ptr<SubType>> linkedInstances;
52375227
std::shared_ptr<ImportResolver> importResolver;
5238-
std::function<CreateTableFunc> createTable;
52395228
};
52405229

52415230
class ModuleRunner : public ModuleRunnerBase<ModuleRunner> {

src/wasm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <unordered_map>
3434
#include <vector>
3535

36-
#include "ir/import-name.h"
36+
#include "ir/import-names.h"
3737
#include "literal.h"
3838
#include "support/index.h"
3939
#include "support/mixed_arena.h"

0 commit comments

Comments
 (0)