Skip to content

Commit 8c831e1

Browse files
committed
Drop TableRegistry
1 parent 1d34199 commit 8c831e1

File tree

10 files changed

+39
-143
lines changed

10 files changed

+39
-143
lines changed

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmFileSuite.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ private void runInContext(WasmCase testCase, Context context, List<Source> sourc
278278
for (int j = 0; j < instance.store().memories().count(); ++j) {
279279
WasmMemoryLibrary.getUncached().reset(instance.store().memories().memory(j));
280280
}
281-
for (int j = 0; j < instance.store().tables().tableCount(); ++j) {
282-
instance.store().tables().table(j).reset();
281+
for (int j = 0; j < instance.module().tableCount(); ++j) {
282+
instance.table(j).reset();
283283
}
284284
}
285285
}

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Linker.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,7 @@ void resolvePassiveDataSegment(WasmStore store, WasmInstance instance, int dataS
815815
}
816816

817817
public static void initializeTable(WasmInstance instance, int tableIndex, Object initValue) {
818-
int tableAddress = instance.tableAddress(tableIndex);
819-
WasmTable table = instance.store().tables().table(tableAddress);
818+
WasmTable table = instance.table(tableIndex);
820819
table.fill(0, table.size(), initValue);
821820
}
822821

@@ -840,11 +839,9 @@ void resolveTableInitialization(WasmInstance instance, int tableIndex, byte[] in
840839
void resolveTableImport(WasmStore store, WasmInstance instance, ImportDescriptor importDescriptor, int tableIndex, int declaredMinSize, int declaredMaxSize, int elemType,
841840
ImportValueSupplier imports) {
842841
final Runnable resolveAction = () -> {
843-
WasmTable externalTable = lookupImportObject(instance, importDescriptor, imports, WasmTable.class);
844-
final int tableAddress;
845-
if (externalTable != null) {
842+
WasmTable importedTable = lookupImportObject(instance, importDescriptor, imports, WasmTable.class);
843+
if (importedTable != null) {
846844
assert tableIndex == importDescriptor.targetIndex();
847-
tableAddress = store.tables().register(externalTable);
848845
} else {
849846
final WasmInstance importedInstance = store.lookupModuleInstance(importDescriptor.moduleName());
850847
final String importedModuleName = importDescriptor.moduleName();
@@ -863,10 +860,9 @@ void resolveTableImport(WasmStore store, WasmInstance instance, ImportDescriptor
863860
throw WasmException.create(Failure.UNKNOWN_IMPORT,
864861
"Table '" + importedTableName + "', imported into module '" + instance.name() + "', was not exported in the module '" + importedModuleName + "'.");
865862
}
866-
tableAddress = importedInstance.tableAddress(exportedTableIndex);
863+
importedTable = importedInstance.table(exportedTableIndex);
867864
}
868865
}
869-
final WasmTable importedTable = store.tables().table(tableAddress);
870866
// Rules for limits matching:
871867
// https://webassembly.github.io/spec/core/exec/modules.html#limits
872868
// If no max size is declared, then declaredMaxSize value will be
@@ -876,7 +872,7 @@ void resolveTableImport(WasmStore store, WasmInstance instance, ImportDescriptor
876872
// when matching element types of imported tables, we need to check for type equivalence
877873
// instead of subtyping, as tables have read/write access
878874
assertTrue(instance.symbolTable().closedTypeOf(elemType).equals(importedTable.closedElemType()), Failure.INCOMPATIBLE_IMPORT_TYPE);
879-
instance.setTableAddress(tableIndex, tableAddress);
875+
instance.setTable(tableIndex, importedTable);
880876
};
881877
final ImportTableSym importTableSym = new ImportTableSym(instance.name(), importDescriptor);
882878
Sym[] dependencies = new Sym[]{new ExportTableSym(importDescriptor.moduleName(), importDescriptor.memberName())};
@@ -1001,8 +997,7 @@ public void immediatelyResolveElemSegment(WasmStore store, WasmInstance instance
1001997
return;
1002998
}
1003999
assertTrue(instance.symbolTable().checkTableIndex(tableIndex), String.format("No table declared or imported in the module '%s'", instance.name()), Failure.UNSPECIFIED_MALFORMED);
1004-
final int tableAddress = instance.tableAddress(tableIndex);
1005-
final WasmTable table = store.tables().table(tableAddress);
1000+
final WasmTable table = instance.table(tableIndex);
10061001
Assert.assertNotNull(table, String.format("No table declared or imported in the module '%s'", instance.name()), Failure.UNKNOWN_TABLE);
10071002
final int baseAddress;
10081003
if (offsetBytecode != null) {

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/RuntimeState.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,7 @@ public abstract class RuntimeState {
7373

7474
private final GlobalRegistry globals;
7575

76-
/**
77-
* This array is monotonically populated from the left. An index i denotes the i-th table in
78-
* this module. The value at index i denotes the address of the table in the memory space for
79-
* all the tables from all the module (see {@link TableRegistry}).
80-
* <p>
81-
* The separation of table instances is done because the index spaces of the tables are
82-
* module-specific, and the tables can be imported across modules. Thus, the address-space of
83-
* the tables is not the same as the module-specific index-space.
84-
*/
85-
@CompilationFinal(dimensions = 1) private int[] tableAddresses;
76+
@CompilationFinal(dimensions = 1) private WasmTable[] tables;
8677

8778
@CompilationFinal(dimensions = 1) private WasmMemory[] memories;
8879

@@ -121,10 +112,10 @@ public abstract class RuntimeState {
121112
}
122113

123114
private void ensureTablesCapacity(int index) {
124-
if (index >= tableAddresses.length) {
125-
final int[] nTableAddresses = new int[Math.max(Integer.highestOneBit(index) << 1, 2 * tableAddresses.length)];
126-
System.arraycopy(tableAddresses, 0, nTableAddresses, 0, tableAddresses.length);
127-
tableAddresses = nTableAddresses;
115+
if (index >= tables.length) {
116+
final WasmTable[] nTables = new WasmTable[Math.max(Integer.highestOneBit(index) << 1, 2 * tables.length)];
117+
System.arraycopy(tables, 0, nTables, 0, tables.length);
118+
tables = nTables;
128119
}
129120
}
130121

@@ -148,7 +139,7 @@ public RuntimeState(WasmStore store, WasmModule module, int numberOfFunctions, i
148139
this.store = store;
149140
this.module = module;
150141
this.globals = new GlobalRegistry(module.numInternalGlobals(), module.numExternalGlobals());
151-
this.tableAddresses = new int[INITIAL_TABLES_SIZE];
142+
this.tables = new WasmTable[INITIAL_TABLES_SIZE];
152143
this.memories = new WasmMemory[INITIAL_MEMORIES_SIZE];
153144
this.tags = new WasmTag[INITIAL_TAG_SIZE];
154145
this.targets = new CallTarget[numberOfFunctions];
@@ -265,16 +256,16 @@ public void setExternalGlobal(int globalIndex, WasmGlobal global) {
265256
globals.setExternalGlobal(symbolTable().globalAddress(globalIndex), global);
266257
}
267258

268-
public int tableAddress(int index) {
269-
final int result = tableAddresses[index];
270-
assert result != SymbolTable.UNINITIALIZED_ADDRESS : "Uninitialized table at index: " + index;
259+
public WasmTable table(int index) {
260+
final WasmTable result = tables[index];
261+
assert result != null : "Uninitialized table at index: " + index;
271262
return result;
272263
}
273264

274-
public void setTableAddress(int tableIndex, int address) {
265+
public void setTable(int tableIndex, WasmTable table) {
275266
ensureTablesCapacity(tableIndex);
276267
checkNotLinked();
277-
tableAddresses[tableIndex] = address;
268+
tables[tableIndex] = table;
278269
}
279270

280271
public WasmMemory memory(int index) {

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/SymbolTable.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,7 @@ public void declareTable(int index, int declaredMinSize, int declaredMaxSize, in
14171417
} else {
14181418
wasmTable = new WasmTable(declaredMinSize, declaredMaxSize, maxAllowedSize, elemType, this);
14191419
}
1420-
final int address = store.tables().register(wasmTable);
1421-
instance.setTableAddress(index, address);
1420+
instance.setTable(index, wasmTable);
14221421

14231422
store.linker().resolveTableInitialization(instance, index, initBytecode, initValue);
14241423
});
@@ -1431,7 +1430,7 @@ void importTable(String moduleName, String tableName, int index, int initSize, i
14311430
importedTables.put(index, importedTable);
14321431
importSymbol(importedTable);
14331432
module().addLinkAction((context, store, instance, imports) -> {
1434-
instance.setTableAddress(index, UNINITIALIZED_ADDRESS);
1433+
instance.setTable(index, null);
14351434
store.linker().resolveTableImport(store, instance, importedTable, index, initSize, maxSize, elemType, imports);
14361435
});
14371436
}

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/TableRegistry.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmInstanceExports.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Object readMember(String member) throws UnknownIdentifierException {
8080
}
8181
final Integer tableIndex = symbolTable.exportedTables().get(member);
8282
if (tableIndex != null) {
83-
return instance.store().tables().table(instance.tableAddress(tableIndex));
83+
return instance.table(tableIndex);
8484
}
8585
final Integer memoryIndex = symbolTable.exportedMemories().get(member);
8686
if (memoryIndex != null) {

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmInstantiator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static List<LinkAction> recreateLinkActions(WasmModule module) {
143143
final ImportDescriptor tableDescriptor = module.importedTable(tableIndex);
144144
if (tableDescriptor != null) {
145145
linkActions.add((context, store, instance, imports) -> {
146-
instance.setTableAddress(tableIndex, SymbolTable.UNINITIALIZED_ADDRESS);
146+
instance.setTable(tableIndex, null);
147147
store.linker().resolveTableImport(store, instance, tableDescriptor, tableIndex, tableMinSize, tableMaxSize, tableElemType, imports);
148148
});
149149
} else {
@@ -152,8 +152,7 @@ static List<LinkAction> recreateLinkActions(WasmModule module) {
152152
final int maxAllowedSize = WasmMath.minUnsigned(tableMaxSize, limits.tableInstanceSizeLimit());
153153
limits.checkTableInstanceSize(tableMinSize);
154154
final WasmTable wasmTable = new WasmTable(tableMinSize, tableMaxSize, maxAllowedSize, tableElemType, module);
155-
final int address = store.tables().register(wasmTable);
156-
instance.setTableAddress(tableIndex, address);
155+
instance.setTable(tableIndex, wasmTable);
157156

158157
final byte[] initBytecode = module.tableInitializerBytecode(tableIndex);
159158
final Object initValue = module.tableInitialValue(tableIndex);

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/WasmStore.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public final class WasmStore implements TruffleObject {
6969
private final WasmContext context;
7070
private final WasmLanguage language;
7171
private final MemoryRegistry memoryRegistry;
72-
private final TableRegistry tableRegistry;
7372
private final Linker linker;
7473
private final Map<String, WasmInstance> moduleInstances;
7574
private final FdManager filesManager;
@@ -79,7 +78,6 @@ public WasmStore(WasmContext context, WasmLanguage language) {
7978
this.context = context;
8079
this.language = language;
8180
this.contextOptions = context.getContextOptions();
82-
this.tableRegistry = new TableRegistry();
8381
this.memoryRegistry = new MemoryRegistry();
8482
this.moduleInstances = new LinkedHashMap<>();
8583
this.linker = new Linker();
@@ -102,10 +100,6 @@ public MemoryRegistry memories() {
102100
return memoryRegistry;
103101
}
104102

105-
public TableRegistry tables() {
106-
return tableRegistry;
107-
}
108-
109103
public Linker linker() {
110104
return linker;
111105
}

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/api/WebAssembly.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,8 +950,7 @@ public static Object instanceExport(WasmInstance instance, String name) {
950950
} else if (memoryIndex != null) {
951951
return instance.memory(memoryIndex);
952952
} else if (tableIndex != null) {
953-
final int address = instance.tableAddress(tableIndex);
954-
return instance.store().tables().table(address);
953+
return instance.table(tableIndex);
955954
} else if (tagIndex != null) {
956955
return instance.tag(tagIndex);
957956
} else {

0 commit comments

Comments
 (0)