use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.
the class Linker method resolveMemoryImport.
void resolveMemoryImport(WasmContext context, WasmInstance instance, ImportDescriptor importDescriptor, int declaredMinSize, int declaredMaxSize) {
final String importedModuleName = importDescriptor.moduleName;
final String importedMemoryName = importDescriptor.memberName;
final Runnable resolveAction = () -> {
final WasmInstance importedInstance = context.moduleInstances().get(importedModuleName);
if (importedInstance == null) {
throw WasmException.create(Failure.UNKNOWN_IMPORT, String.format("The module '%s', referenced in the import of memory '%s' in module '%s', does not exist", importedModuleName, importedMemoryName, instance.name()));
}
final List<String> exportedMemory = importedInstance.symbolTable().exportedMemoryNames();
if (exportedMemory.size() == 0) {
throw WasmException.create(Failure.UNKNOWN_IMPORT, String.format("The imported module '%s' does not export any memories, so cannot resolve memory '%s' imported in module '%s'.", importedModuleName, importedMemoryName, instance.name()));
}
if (!exportedMemory.contains(importedMemoryName)) {
throw WasmException.create(Failure.UNKNOWN_IMPORT, String.format("The imported module '%s' exports a memory '%s', but module '%s' imports a memory '%s'.", importedModuleName, exportedMemory, instance.name(), importedModuleName));
}
final WasmMemory memory = importedInstance.memory();
// Rules for limits matching:
// https://webassembly.github.io/spec/core/exec/modules.html#limits
// If no max size is declared, then declaredMaxSize value will be
// MAX_TABLE_DECLARATION_SIZE, so this condition will pass.
assertUnsignedIntLessOrEqual(declaredMinSize, memory.declaredMinSize(), Failure.INCOMPATIBLE_IMPORT_TYPE);
assertUnsignedIntGreaterOrEqual(declaredMaxSize, memory.declaredMaxSize(), Failure.INCOMPATIBLE_IMPORT_TYPE);
instance.setMemory(memory);
};
resolutionDag.resolveLater(new ImportMemorySym(instance.name(), importDescriptor), new Sym[] { new ExportMemorySym(importedModuleName, importedMemoryName) }, resolveAction);
}
use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.
the class WasmJsApiSuite method testInstantiateWithExportMemory.
@Test
public void testInstantiateWithExportMemory() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
try {
final WasmMemory memory = (WasmMemory) WebAssembly.instanceExport(instance, "memory");
final Object readZero = WebAssembly.instanceExport(instance, "readZero");
memory.store_i32(null, 0, 174);
final Object result = InteropLibrary.getUncached(readZero).execute(readZero);
Assert.assertEquals("Must be 174.", 174, InteropLibrary.getUncached(result).asInt(result));
} catch (InteropException e) {
throw new RuntimeException(e);
}
});
}
use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.
the class SymbolTable method allocateExternalMemory.
public void allocateExternalMemory(WasmMemory externalMemory) {
checkNotParsed();
validateSingleMemory();
memory = new MemoryInfo(externalMemory.declaredMinSize(), externalMemory.declaredMaxSize());
module().addLinkAction((context, instance) -> {
final int memoryIndex = context.memories().registerExternal(externalMemory);
final WasmMemory allocatedMemory = context.memories().memory(memoryIndex);
instance.setMemory(allocatedMemory);
});
}
use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.
the class SymbolTable method allocateMemory.
public void allocateMemory(int declaredMinSize, int declaredMaxSize) {
checkNotParsed();
validateSingleMemory();
memory = new MemoryInfo(declaredMinSize, declaredMaxSize);
module().addLinkAction((context, instance) -> {
final int maxAllowedSize = minUnsigned(declaredMaxSize, module().limits().memoryInstanceSizeLimit());
module().limits().checkMemoryInstanceSize(declaredMinSize);
final WasmMemory wasmMemory;
if (context.environment().getOptions().get(WasmOptions.UseUnsafeMemory)) {
wasmMemory = new UnsafeWasmMemory(declaredMinSize, declaredMaxSize, maxAllowedSize);
} else {
wasmMemory = new ByteArrayWasmMemory(declaredMinSize, declaredMaxSize, maxAllowedSize);
}
final int memoryIndex = context.memories().register(wasmMemory);
final WasmMemory allocatedMemory = context.memories().memory(memoryIndex);
instance.setMemory(allocatedMemory);
});
}
use of org.graalvm.wasm.memory.WasmMemory in project graal by oracle.
the class WasmJsApiSuite method testMemoryEmbedderData.
@Test
public void testMemoryEmbedderData() throws IOException {
runTest(context -> {
WasmMemory memory = WebAssembly.memAlloc(1, 1);
checkEmbedderData(memory);
});
}
Aggregations