Search in sources :

Example 36 with WasmInstance

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);
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) HashMap(java.util.HashMap) EconomicMap(org.graalvm.collections.EconomicMap) Map(java.util.Map)

Example 37 with WasmInstance

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;
}
Also used : Path(java.nio.file.Path) WasmInstance(org.graalvm.wasm.WasmInstance)

Example 38 with WasmInstance

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;
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WasmTable(org.graalvm.wasm.WasmTable) SymbolTable(org.graalvm.wasm.SymbolTable) WasmFunction(org.graalvm.wasm.WasmFunction) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) WasmMemory(org.graalvm.wasm.memory.WasmMemory) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.graalvm.collections.Pair)

Aggregations

WasmInstance (org.graalvm.wasm.WasmInstance)38 WebAssembly (org.graalvm.wasm.api.WebAssembly)29 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)28 Test (org.junit.Test)28 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)15 InteropException (com.oracle.truffle.api.interop.InteropException)13 Dictionary (org.graalvm.wasm.api.Dictionary)12 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)11 ArityException (com.oracle.truffle.api.interop.ArityException)10 WasmTable (org.graalvm.wasm.WasmTable)6 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)5 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)4 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)4 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)3 RootNode (com.oracle.truffle.api.nodes.RootNode)3 WasmContext (org.graalvm.wasm.WasmContext)3 Executable (org.graalvm.wasm.api.Executable)3 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)3 WasmMemory (org.graalvm.wasm.memory.WasmMemory)3