Skip to content

Commit c48fb7e

Browse files
committed
[WIP] Initial support for compact imports proposal. NFC.
https://github.com/WebAssembly/compact-import-section
1 parent 5fe2f80 commit c48fb7e

File tree

7 files changed

+529
-110
lines changed

7 files changed

+529
-110
lines changed

src/parser/parsers.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ template<typename Ctx> MaybeResult<> typedef_(Ctx&);
384384
template<typename Ctx> MaybeResult<> rectype(Ctx&);
385385
template<typename Ctx> MaybeResult<typename Ctx::LocalsT> locals(Ctx&);
386386
template<typename Ctx> MaybeResult<> import_(Ctx&);
387+
template<typename Ctx> MaybeResult<> importItem(Ctx&, Name module);
387388
template<typename Ctx> MaybeResult<> func(Ctx&);
388389
template<typename Ctx> MaybeResult<> table(Ctx&);
389390
template<typename Ctx> MaybeResult<> memory(Ctx&);
@@ -3308,7 +3309,12 @@ template<typename Ctx> MaybeResult<typename Ctx::LocalsT> locals(Ctx& ctx) {
33083309
return {};
33093310
}
33103311

3312+
template<typename Ctx> MaybeResult<> importItem(Ctx& ctx, Name name) {
3313+
return Ok{};
3314+
}
3315+
33113316
// import ::= '(' 'import' mod:name nm:name importdesc ')'
3317+
// '(' 'import' mod:name compactimportdesc ')'
33123318
// importdesc ::= '(' 'func' id? exacttypeuse ')'
33133319
// | '(' 'table' id? tabletype ')'
33143320
// | '(' 'memory' id? memtype ')'
@@ -3326,6 +3332,16 @@ template<typename Ctx> MaybeResult<> import_(Ctx& ctx) {
33263332
return ctx.in.err("expected import module name");
33273333
}
33283334

3335+
if (ctx.in.peekSExprStart("item"sv)) {
3336+
while (ctx.in.peekSExprStart("item"sv)) {
3337+
CHECK_ERR(importItem(ctx, *mod));
3338+
}
3339+
if (!ctx.in.takeRParen()) {
3340+
return ctx.in.err("expected end of import");
3341+
}
3342+
return Ok{};
3343+
}
3344+
33293345
auto nm = ctx.in.takeName();
33303346
if (!nm) {
33313347
return ctx.in.err("expected import name");

src/wasm-binary.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ extern const char* BulkMemoryOptFeature;
461461
extern const char* CallIndirectOverlongFeature;
462462
extern const char* CustomDescriptorsFeature;
463463
extern const char* RelaxedAtomicsFeature;
464+
extern const char* CompactImportsFeature;
464465

465466
enum Subsection {
466467
NameModule = 0,
@@ -1637,6 +1638,11 @@ class WasmBinaryReader {
16371638
Address defaultIfNoMax);
16381639
void readImports();
16391640

1641+
void addImport(uint32_t kind, std::unique_ptr<Importable> importable);
1642+
std::unique_ptr<Importable>
1643+
readImportDetails(Name module, Name field, uint32_t kind);
1644+
std::unique_ptr<Importable> copyImportable(uint32_t kind, Importable& details);
1645+
16401646
// The signatures of each function, including imported functions, given in the
16411647
// import and function sections. Store HeapTypes instead of Signatures because
16421648
// reconstructing the HeapTypes from the Signatures is expensive.

src/wasm-features.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ struct FeatureSet {
5656
CallIndirectOverlong = 1 << 20,
5757
CustomDescriptors = 1 << 21,
5858
RelaxedAtomics = 1 << 22,
59+
CompactImports = 1 << 23,
5960
MVP = None,
6061
// Keep in sync with llvm default features:
6162
// https://github.com/llvm/llvm-project/blob/c7576cb89d6c95f03968076e902d3adfd1996577/clang/lib/Basic/Targets/WebAssembly.cpp#L150-L153
6263
Default = SignExt | MutableGlobals,
63-
All = (1 << 23) - 1,
64+
All = (1 << 24) - 1,
6465
};
6566

6667
static std::string toString(Feature f) {
@@ -111,6 +112,8 @@ struct FeatureSet {
111112
return "custom-descriptors";
112113
case RelaxedAtomics:
113114
return "relaxed-atomics";
115+
case CompactImports:
116+
return "compact-imports";
114117
case MVP:
115118
case Default:
116119
case All:
@@ -172,6 +175,7 @@ struct FeatureSet {
172175
return (features & CustomDescriptors) != 0;
173176
}
174177
bool hasRelaxedAtomics() const { return (features & RelaxedAtomics) != 0; }
178+
bool hasCompactImports() const { return (features & CompactImports) != 0; }
175179
bool hasAll() const { return (features & All) != 0; }
176180

177181
void set(FeatureSet f, bool v = true) {

0 commit comments

Comments
 (0)