Search in sources :

Example 6 with WasmGlobal

use of org.graalvm.wasm.globals.WasmGlobal in project graal by oracle.

the class WasmJsApiSuite method checkInstantiateWithImportGlobal.

private static void checkInstantiateWithImportGlobal(byte[] binaryWithGlobalImport, String globalType, Object globalValue) throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmGlobal global = WebAssembly.globalAlloc(ValueType.valueOf(globalType), false, globalValue);
        Dictionary importObject = Dictionary.create(new Object[] { "host", Dictionary.create(new Object[] { "defaultGlobal", global }) });
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithGlobalImport, importObject);
        try {
            InteropLibrary interop = InteropLibrary.getUncached();
            final Object readGlobal1 = WebAssembly.instanceExport(instance, "readGlobal1");
            final Object readGlobal2 = WebAssembly.instanceExport(instance, "readGlobal2");
            final Object result1 = interop.execute(readGlobal1);
            final Object result2 = interop.execute(readGlobal2);
            Assert.assertEquals("Must be " + globalValue + " initially.", globalValue, result1);
            Assert.assertEquals("Must be " + globalValue + " initially.", globalValue, result2);
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Dictionary(org.graalvm.wasm.api.Dictionary) WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException)

Example 7 with WasmGlobal

use of org.graalvm.wasm.globals.WasmGlobal in project graal by oracle.

the class WasmJsApiSuite method testGlobalEmbedderData.

@Test
public void testGlobalEmbedderData() throws IOException {
    runTest(context -> {
        WasmGlobal global = WebAssembly.globalAlloc(ValueType.i32, false, 0);
        checkEmbedderData(global);
    });
}
Also used : WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) Test(org.junit.Test)

Example 8 with WasmGlobal

use of org.graalvm.wasm.globals.WasmGlobal 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

WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)8 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)3 WasmInstance (org.graalvm.wasm.WasmInstance)3 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)3 DefaultWasmGlobal (org.graalvm.wasm.globals.DefaultWasmGlobal)3 ExportedWasmGlobal (org.graalvm.wasm.globals.ExportedWasmGlobal)3 InteropException (com.oracle.truffle.api.interop.InteropException)2 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 HashMap (java.util.HashMap)2 WasmFunction (org.graalvm.wasm.WasmFunction)2 WasmTable (org.graalvm.wasm.WasmTable)2 WebAssembly (org.graalvm.wasm.api.WebAssembly)2 WasmMemory (org.graalvm.wasm.memory.WasmMemory)2 Test (org.junit.Test)2 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)1 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 Map (java.util.Map)1 Pair (org.graalvm.collections.Pair)1 SymbolTable (org.graalvm.wasm.SymbolTable)1