Search in sources :

Example 26 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method testInstantiateWithExportTable.

@Test
public void testInstantiateWithExportTable() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableExport, null);
        try {
            final WasmTable table = (WasmTable) WebAssembly.instanceExport(instance, "defaultTable");
            final Object result = InteropLibrary.getUncached().execute(WebAssembly.tableRead(table, 0), 9);
            Assert.assertEquals("Must be 81.", 81, result);
        } catch (UnsupportedTypeException | UnsupportedMessageException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Example 27 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly 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);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) WasmMemory(org.graalvm.wasm.memory.WasmMemory) Test(org.junit.Test)

Example 28 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly 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 29 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method testInstantiateWithUnicodeExport.

@Test
public void testInstantiateWithUnicodeExport() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithUnicodeExport, null);
        try {
            final Object euroSignFn = WebAssembly.instanceExport(instance, "\u20AC");
            final Object result = InteropLibrary.getUncached(euroSignFn).execute(euroSignFn);
            Assert.assertEquals("Result should be 42", 42, InteropLibrary.getUncached(result).asInt(result));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 30 with WebAssembly

use of org.graalvm.wasm.api.WebAssembly in project graal by oracle.

the class WasmJsApiSuite method testTableInstanceOutOfBoundsGet.

@Test
public void testTableInstanceOutOfBoundsGet() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        // We should be able to get element 1.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object readTable = wasm.readMember("table_read");
            lib.execute(readTable, table, 0);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But not element 2.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object readTable = wasm.readMember("table_read");
            lib.execute(readTable, table, 1);
            Assert.fail("Should have failed - export count exceeds the limit");
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        } catch (WasmJsApiException e) {
            Assert.assertEquals("Range error expected", WasmJsApiException.Kind.RangeError, e.kind());
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Aggregations

WebAssembly (org.graalvm.wasm.api.WebAssembly)36 Test (org.junit.Test)33 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)29 WasmInstance (org.graalvm.wasm.WasmInstance)29 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)15 InteropException (com.oracle.truffle.api.interop.InteropException)14 Dictionary (org.graalvm.wasm.api.Dictionary)13 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)11 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)11 ArityException (com.oracle.truffle.api.interop.ArityException)10 WasmModule (org.graalvm.wasm.WasmModule)5 WasmTable (org.graalvm.wasm.WasmTable)5 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)4 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)4 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)3 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)3 RootNode (com.oracle.truffle.api.nodes.RootNode)3 Executable (org.graalvm.wasm.api.Executable)3 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)3 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)2