Skip to content

Commit 1e783f3

Browse files
committed
allow a maximum 0 size for a table
1 parent a3dc8bf commit 1e783f3

5 files changed

Lines changed: 33 additions & 19 deletions

File tree

src/passes/Print.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ struct PrintSExpression : public Visitor<PrintSExpression> {
615615
void printTableHeader(Table* curr) {
616616
printOpening(o, "table") << ' ';
617617
o << curr->initial;
618-
if (curr->max && curr->max != Table::kMaxSize) o << ' ' << curr->max;
618+
if (curr->max != Table::kMaxSize) o << ' ' << curr->max;
619619
o << " anyfunc)";
620620
}
621621
void visitTable(Table *curr) {

src/wasm-binary.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,11 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
558558
return ret;
559559
}
560560

561-
void writeResizableLimits(Address initial, Address maximum) {
562-
uint32_t flags = maximum ? 1 : 0;
561+
void writeResizableLimits(Address initial, Address maximum, bool hasMaximum) {
562+
uint32_t flags = hasMaximum ? 1 : 0;
563563
o << U32LEB(flags);
564564
o << U32LEB(initial);
565-
if (flags) {
565+
if (hasMaximum) {
566566
o << U32LEB(maximum);
567567
}
568568
}
@@ -590,8 +590,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
590590
if (debug) std::cerr << "== writeMemory" << std::endl;
591591
auto start = startSection(BinaryConsts::Section::Memory);
592592
o << U32LEB(1); // Define 1 memory
593-
Address max = wasm->memory.max == Memory::kMaxSize ? Address(0) : wasm->memory.max;
594-
writeResizableLimits(wasm->memory.initial, max);
593+
writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.max != Memory::kMaxSize);
595594
finishSection(start);
596595
}
597596

@@ -639,13 +638,12 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
639638
case ExternalKind::Function: o << U32LEB(getFunctionTypeIndex(import->functionType->name)); break;
640639
case ExternalKind::Table: {
641640
o << U32LEB(BinaryConsts::ElementType::AnyFunc);
642-
auto max = wasm->table.max == Table::kMaxSize ? Address(0) : wasm->table.max;
643-
writeResizableLimits(wasm->table.initial, max);
641+
writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize);
644642
break;
645643
}
646644
case ExternalKind::Memory: {
647-
auto max = wasm->memory.max == Memory::kMaxSize ? Address(0) : wasm->memory.max;
648-
writeResizableLimits(wasm->memory.initial, max); break;
645+
writeResizableLimits(wasm->memory.initial, wasm->memory.max, wasm->memory.max != Memory::kMaxSize);
646+
break;
649647
}
650648
case ExternalKind::Global:
651649
o << binaryWasmType(import->globalType);
@@ -850,8 +848,7 @@ class WasmBinaryWriter : public Visitor<WasmBinaryWriter, void> {
850848
auto start = startSection(BinaryConsts::Section::Table);
851849
o << U32LEB(1); // Declare 1 table.
852850
o << U32LEB(BinaryConsts::ElementType::AnyFunc);
853-
Address max = wasm->table.max == Table::kMaxSize ? Address(0) : wasm->table.max;
854-
writeResizableLimits(wasm->table.initial, max);
851+
writeResizableLimits(wasm->table.initial, wasm->table.max, wasm->table.max != Table::kMaxSize);
855852
finishSection(start);
856853
}
857854

@@ -1562,7 +1559,7 @@ class WasmBinaryBuilder {
15621559
auto numMemories = getU32LEB();
15631560
if (!numMemories) return;
15641561
assert(numMemories == 1);
1565-
getResizableLimits(wasm.memory.initial, &wasm.memory.max);
1562+
getResizableLimits(wasm.memory.initial, wasm.memory.max, Memory::kMaxSize);
15661563
}
15671564

15681565
void readSignatures() {
@@ -1606,12 +1603,12 @@ class WasmBinaryBuilder {
16061603
}
16071604
}
16081605

1609-
void getResizableLimits(Address& initial, Address* max) {
1606+
void getResizableLimits(Address& initial, Address& max, Address defaultIfNoMax) {
16101607
auto flags = getU32LEB();
16111608
initial = getU32LEB();
16121609
bool hasMax = flags & 0x1;
1613-
assert(max || !hasMax);
1614-
if (hasMax) *max = getU32LEB();
1610+
if (hasMax) max = getU32LEB();
1611+
else max = defaultIfNoMax;
16151612
}
16161613

16171614
void readImports() {
@@ -1640,10 +1637,13 @@ class WasmBinaryBuilder {
16401637
if (elementType != BinaryConsts::ElementType::AnyFunc) throw ParseException("Imported table type is not AnyFunc");
16411638
wasm.table.exists = true;
16421639
wasm.table.imported = true;
1643-
getResizableLimits(wasm.table.initial, &wasm.table.max);
1640+
getResizableLimits(wasm.table.initial, wasm.table.max, Table::kMaxSize);
1641+
break;
1642+
}
1643+
case ExternalKind::Memory: {
1644+
getResizableLimits(wasm.memory.initial, wasm.memory.max, Memory::kMaxSize);
16441645
break;
16451646
}
1646-
case ExternalKind::Memory: getResizableLimits(wasm.memory.initial, &wasm.memory.max); break;
16471647
case ExternalKind::Global: {
16481648
curr->globalType = getWasmType();
16491649
auto globalMutable = getU32LEB();
@@ -1897,7 +1897,7 @@ class WasmBinaryBuilder {
18971897
wasm.table.exists = true;
18981898
auto elemType = getU32LEB();
18991899
if (elemType != BinaryConsts::ElementType::AnyFunc) throw ParseException("ElementType must be AnyFunc in MVP");
1900-
getResizableLimits(wasm.table.initial, &wasm.table.max);
1900+
getResizableLimits(wasm.table.initial, wasm.table.max, Table::kMaxSize);
19011901
}
19021902

19031903
void readTableElements() {

test/empty_table.wast

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module
2+
(table 0 0 anyfunc)
3+
(memory $0 0)
4+
)

test/empty_table.wast.fromBinary

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(table 0 0 anyfunc)
3+
(memory $0 0)
4+
)
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(table 0 0 anyfunc)
3+
(memory $0 0)
4+
)
5+

0 commit comments

Comments
 (0)