use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class WebAssembly method instantiateModule.
private static WasmInstance instantiateModule(WasmModule module, Object importObject, WasmContext context, TruffleContext truffleContext) {
final HashMap<String, ImportModule> importModules;
// To read the content of the import object, we need to enter the parent context that this
// import object originates from.
Object prev = truffleContext.getParent().enter(null);
try {
importModules = readModuleImports(module, importObject);
} finally {
truffleContext.getParent().leave(null, prev);
}
for (Map.Entry<String, ImportModule> entry : importModules.entrySet()) {
final String name = entry.getKey();
final ImportModule importModule = entry.getValue();
final WasmInstance importedInstance = importModule.createInstance(context.language(), context, name);
context.register(importedInstance);
}
return context.readInstance(module);
}
use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class TestutilModule method createInstance.
@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
final Path temporaryDirectory = createTemporaryDirectory();
WasmInstance instance = new WasmInstance(context, WasmModule.createBuiltin(name), NUMBER_OF_FUNCTIONS);
// Note: in the following methods, the types are not important here, since these methods
// are not accessed by Wasm code.
defineFunction(instance, Names.RUN_CUSTOM_INITIALIZATION, types(), types(), new RunCustomInitializationNode(language));
// The following methods are exposed to the Wasm test programs.
defineFunction(instance, Names.SAVE_BINARY_FILE, types(I32_TYPE, I32_TYPE, I32_TYPE), types(), new SaveBinaryFileNode(language, instance, temporaryDirectory));
return instance;
}
use of org.graalvm.wasm.WasmInstance in project graal by oracle.
the class ImportModule method createInstance.
@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
WasmInstance instance = new WasmInstance(context, WasmModule.createBuiltin(name), functions.size());
for (Map.Entry<String, Pair<WasmFunction, Object>> entry : functions.entrySet()) {
final String functionName = entry.getKey();
final Pair<WasmFunction, Object> info = entry.getValue();
final WasmFunction function = info.getLeft();
final SymbolTable.FunctionType type = function.type();
if (info.getRight() instanceof WasmFunctionInstance) {
defineExportedFunction(instance, functionName, type.paramTypes(), type.returnTypes(), (WasmFunctionInstance) info.getRight());
} else {
defineFunction(instance, functionName, type.paramTypes(), type.returnTypes(), new ExecuteInParentContextNode(context.language(), instance, info.getRight()));
}
}
for (Map.Entry<String, WasmMemory> entry : memories.entrySet()) {
final String memoryName = entry.getKey();
final WasmMemory memory = entry.getValue();
defineExternalMemory(instance, memoryName, memory);
}
for (Map.Entry<String, WasmTable> entry : tables.entrySet()) {
final String tableName = entry.getKey();
final WasmTable table = entry.getValue();
defineExternalTable(instance, tableName, table);
}
for (Map.Entry<String, WasmGlobal> entry : globals.entrySet()) {
final String globalName = entry.getKey();
final WasmGlobal global = entry.getValue();
defineExternalGlobal(instance, globalName, global);
}
return instance;
}
Aggregations