Search in sources :

Example 31 with WebAssembly

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

the class WasmJsApiSuite method testExportSameFunctionInAndOutsideTable.

@Test
public void testExportSameFunctionInAndOutsideTable() throws IOException, InterruptedException {
    final byte[] sameFunctionInExportAndTable = compileWat("sameFunctionInExportAndTable", "(module (func $f (result i32) i32.const 1) (table 1 funcref) (elem (i32.const 0) $f) (export \"f\" (func $f)) (export \"t\" (table 0)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, sameFunctionInExportAndTable, null);
        final Object f = WebAssembly.instanceExport(instance, "f");
        final WasmTable t = (WasmTable) WebAssembly.instanceExport(instance, "t");
        final Object fInTable = WebAssembly.tableRead(t, 0);
        Assert.assertTrue("Returned function instances must be reference equal", f == fInTable);
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 32 with WebAssembly

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

the class WasmJsApiSuite method testInstantiate.

@Test
public void testInstantiate() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithExports, null);
        try {
            final Object main = WebAssembly.instanceExport(instance, "main");
            final Object result = InteropLibrary.getUncached(main).execute(main);
            Assert.assertEquals("Should return 42 from main.", 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 33 with WebAssembly

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

the class WasmJsApiSuite method testMemoryBufferMessages.

@Test
public void testMemoryBufferMessages() throws IOException {
    runTest(context -> {
        WebAssembly wasm = new WebAssembly(context);
        WasmModule module = wasm.moduleDecode(binaryWithMemoryExport);
        WasmInstance instance = wasm.moduleInstantiate(module, new Dictionary());
        try {
            Object buffer = WebAssembly.instanceExport(instance, "memory");
            long bufferSize = 4 * Sizes.MEMORY_PAGE_SIZE;
            InteropLibrary interop = InteropLibrary.getUncached(buffer);
            Assert.assertTrue("Should have buffer elements", interop.hasBufferElements(buffer));
            Assert.assertEquals("Should have correct buffer size", bufferSize, interop.getBufferSize(buffer));
            Assert.assertTrue("Should have writable buffer", interop.isBufferWritable(buffer));
            Assert.assertEquals("Read first byte", (byte) 0, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read last byte", (byte) 0, interop.readBufferByte(buffer, bufferSize - 1));
            Assert.assertEquals("Read first short LE", (short) 0, interop.readBufferShort(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read first short BE", (short) 0, interop.readBufferShort(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read last short LE", (short) 0, interop.readBufferShort(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 2));
            Assert.assertEquals("Read last short BE", (short) 0, interop.readBufferShort(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 2));
            Assert.assertEquals("Read first int LE", 0, interop.readBufferInt(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read first int BE", 0, interop.readBufferInt(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read last int LE", 0, interop.readBufferInt(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 4));
            Assert.assertEquals("Read last int BE", 0, interop.readBufferInt(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 4));
            Assert.assertEquals("Read first long LE", 0L, interop.readBufferLong(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read first long BE", 0L, interop.readBufferLong(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read last long LE", 0L, interop.readBufferLong(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 8));
            Assert.assertEquals("Read last long BE", 0L, interop.readBufferLong(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 8));
            Assert.assertEquals("Read first float LE", (float) 0, interop.readBufferFloat(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read first float BE", (float) 0, interop.readBufferFloat(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read last float LE", (float) 0, interop.readBufferFloat(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 4));
            Assert.assertEquals("Read last float BE", (float) 0, interop.readBufferFloat(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 4));
            Assert.assertEquals("Read first double LE", 0d, interop.readBufferDouble(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read first double BE", 0d, interop.readBufferDouble(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read last double LE", 0d, interop.readBufferDouble(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 8));
            Assert.assertEquals("Read last double BE", 0d, interop.readBufferDouble(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 8));
            interop.writeBufferByte(buffer, 0, (byte) 1);
            Assert.assertEquals("Read written byte", (byte) 1, interop.readBufferByte(buffer, 0));
            interop.writeBufferShort(buffer, ByteOrder.LITTLE_ENDIAN, 0, (short) 0x0102);
            Assert.assertEquals("Read written short LE", (short) 0x0102, interop.readBufferShort(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of short LE", (byte) 0x02, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of short LE", (byte) 0x01, interop.readBufferByte(buffer, 1));
            interop.writeBufferShort(buffer, ByteOrder.BIG_ENDIAN, 0, (short) 0x0102);
            Assert.assertEquals("Read written short BE", (short) 0x0102, interop.readBufferShort(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of short BE", (byte) 0x01, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of short BE", (byte) 0x02, interop.readBufferByte(buffer, 1));
            interop.writeBufferInt(buffer, ByteOrder.LITTLE_ENDIAN, 0, 0x01020304);
            Assert.assertEquals("Read written int LE", 0x01020304, interop.readBufferInt(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of int LE", (byte) 0x04, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of int LE", (byte) 0x03, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of int LE", (byte) 0x02, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of int LE", (byte) 0x01, interop.readBufferByte(buffer, 3));
            interop.writeBufferInt(buffer, ByteOrder.BIG_ENDIAN, 0, 0x01020304);
            Assert.assertEquals("Read written int BE", 0x01020304, interop.readBufferInt(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of int BE", (byte) 0x01, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of int BE", (byte) 0x02, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of int BE", (byte) 0x03, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of int BE", (byte) 0x04, interop.readBufferByte(buffer, 3));
            interop.writeBufferLong(buffer, ByteOrder.LITTLE_ENDIAN, 0, 0x0102030405060708L);
            Assert.assertEquals("Read written long LE", 0x0102030405060708L, interop.readBufferLong(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of long LE", (byte) 0x08, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of long LE", (byte) 0x07, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of long LE", (byte) 0x06, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of long LE", (byte) 0x05, interop.readBufferByte(buffer, 3));
            Assert.assertEquals("Read byte 4 of long LE", (byte) 0x04, interop.readBufferByte(buffer, 4));
            Assert.assertEquals("Read byte 5 of long LE", (byte) 0x03, interop.readBufferByte(buffer, 5));
            Assert.assertEquals("Read byte 6 of long LE", (byte) 0x02, interop.readBufferByte(buffer, 6));
            Assert.assertEquals("Read byte 7 of long LE", (byte) 0x01, interop.readBufferByte(buffer, 7));
            interop.writeBufferLong(buffer, ByteOrder.BIG_ENDIAN, 0, 0x0102030405060708L);
            Assert.assertEquals("Read written long BE", 0x0102030405060708L, interop.readBufferLong(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of long BE", (byte) 0x01, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of long BE", (byte) 0x02, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of long BE", (byte) 0x03, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of long BE", (byte) 0x04, interop.readBufferByte(buffer, 3));
            Assert.assertEquals("Read byte 4 of long BE", (byte) 0x05, interop.readBufferByte(buffer, 4));
            Assert.assertEquals("Read byte 5 of long BE", (byte) 0x06, interop.readBufferByte(buffer, 5));
            Assert.assertEquals("Read byte 6 of long BE", (byte) 0x07, interop.readBufferByte(buffer, 6));
            Assert.assertEquals("Read byte 7 of long BE", (byte) 0x08, interop.readBufferByte(buffer, 7));
            float f = Float.intBitsToFloat(0x01020304);
            interop.writeBufferFloat(buffer, ByteOrder.LITTLE_ENDIAN, 0, f);
            Assert.assertEquals("Read written float LE", f, interop.readBufferFloat(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of float LE", (byte) 0x04, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of float LE", (byte) 0x03, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of float LE", (byte) 0x02, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of float LE", (byte) 0x01, interop.readBufferByte(buffer, 3));
            interop.writeBufferFloat(buffer, ByteOrder.BIG_ENDIAN, 0, f);
            Assert.assertEquals("Read written float BE", f, interop.readBufferFloat(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of float BE", (byte) 0x01, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of float BE", (byte) 0x02, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of float BE", (byte) 0x03, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of float BE", (byte) 0x04, interop.readBufferByte(buffer, 3));
            double d = Double.longBitsToDouble(0x0102030405060708L);
            interop.writeBufferDouble(buffer, ByteOrder.LITTLE_ENDIAN, 0, d);
            Assert.assertEquals("Read written double LE", d, interop.readBufferDouble(buffer, ByteOrder.LITTLE_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of double LE", (byte) 0x08, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of double LE", (byte) 0x07, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of double LE", (byte) 0x06, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of double LE", (byte) 0x05, interop.readBufferByte(buffer, 3));
            Assert.assertEquals("Read byte 4 of double LE", (byte) 0x04, interop.readBufferByte(buffer, 4));
            Assert.assertEquals("Read byte 5 of double LE", (byte) 0x03, interop.readBufferByte(buffer, 5));
            Assert.assertEquals("Read byte 6 of double LE", (byte) 0x02, interop.readBufferByte(buffer, 6));
            Assert.assertEquals("Read byte 7 of double LE", (byte) 0x01, interop.readBufferByte(buffer, 7));
            interop.writeBufferDouble(buffer, ByteOrder.BIG_ENDIAN, 0, d);
            Assert.assertEquals("Read written double BE", d, interop.readBufferDouble(buffer, ByteOrder.BIG_ENDIAN, 0));
            Assert.assertEquals("Read byte 0 of double BE", (byte) 0x01, interop.readBufferByte(buffer, 0));
            Assert.assertEquals("Read byte 1 of double BE", (byte) 0x02, interop.readBufferByte(buffer, 1));
            Assert.assertEquals("Read byte 2 of double BE", (byte) 0x03, interop.readBufferByte(buffer, 2));
            Assert.assertEquals("Read byte 3 of double BE", (byte) 0x04, interop.readBufferByte(buffer, 3));
            Assert.assertEquals("Read byte 4 of double BE", (byte) 0x05, interop.readBufferByte(buffer, 4));
            Assert.assertEquals("Read byte 5 of double BE", (byte) 0x06, interop.readBufferByte(buffer, 5));
            Assert.assertEquals("Read byte 6 of double BE", (byte) 0x07, interop.readBufferByte(buffer, 6));
            Assert.assertEquals("Read byte 7 of double BE", (byte) 0x08, interop.readBufferByte(buffer, 7));
            // Offset too small
            assertThrowsIBOE(() -> interop.readBufferByte(buffer, -1));
            assertThrowsIBOE(() -> interop.readBufferShort(buffer, ByteOrder.LITTLE_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferShort(buffer, ByteOrder.BIG_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferInt(buffer, ByteOrder.LITTLE_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferInt(buffer, ByteOrder.BIG_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferLong(buffer, ByteOrder.LITTLE_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferLong(buffer, ByteOrder.BIG_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferFloat(buffer, ByteOrder.LITTLE_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferFloat(buffer, ByteOrder.BIG_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferDouble(buffer, ByteOrder.LITTLE_ENDIAN, -1));
            assertThrowsIBOE(() -> interop.readBufferDouble(buffer, ByteOrder.BIG_ENDIAN, -1));
            // Offset too large
            assertThrowsIBOE(() -> interop.readBufferByte(buffer, bufferSize));
            assertThrowsIBOE(() -> interop.readBufferShort(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 1));
            assertThrowsIBOE(() -> interop.readBufferShort(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 1));
            assertThrowsIBOE(() -> interop.readBufferInt(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 3));
            assertThrowsIBOE(() -> interop.readBufferInt(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 3));
            assertThrowsIBOE(() -> interop.readBufferLong(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 7));
            assertThrowsIBOE(() -> interop.readBufferLong(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 7));
            assertThrowsIBOE(() -> interop.readBufferFloat(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 3));
            assertThrowsIBOE(() -> interop.readBufferFloat(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 3));
            assertThrowsIBOE(() -> interop.readBufferDouble(buffer, ByteOrder.LITTLE_ENDIAN, bufferSize - 7));
            assertThrowsIBOE(() -> interop.readBufferDouble(buffer, ByteOrder.BIG_ENDIAN, bufferSize - 7));
        } catch (InteropException ex) {
            Assert.fail(ex.getMessage());
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) Dictionary(org.graalvm.wasm.api.Dictionary) WasmModule(org.graalvm.wasm.WasmModule) WebAssembly(org.graalvm.wasm.api.WebAssembly) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 34 with WebAssembly

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

the class WasmJsApiSuite method testImportOrder.

@Test
public void testImportOrder() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmModule module = wasm.moduleDecode(binaryWithMixedImports);
        final Sequence<ModuleImportDescriptor> moduleImports = WebAssembly.moduleImports(module);
        String[] expected = new String[] { "f1", "g1", "t", "m", "g2", "f2" };
        try {
            Assert.assertEquals("Must import all members.", 6L, moduleImports.getArraySize());
            for (int i = 0; i < moduleImports.getArraySize(); i++) {
                Assert.assertEquals("Module member " + i + " should correspond to the expected import.", expected[i], ((ModuleImportDescriptor) moduleImports.readArrayElement(i)).name());
            }
        } catch (InvalidArrayIndexException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : ModuleImportDescriptor(org.graalvm.wasm.api.ModuleImportDescriptor) WasmModule(org.graalvm.wasm.WasmModule) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) WebAssembly(org.graalvm.wasm.api.WebAssembly) Test(org.junit.Test)

Example 35 with WebAssembly

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

the class WasmJsApiSuite method testExportSameFunctionAtDifferentTableIndices.

@Test
public void testExportSameFunctionAtDifferentTableIndices() throws IOException, InterruptedException {
    final byte[] sameFunctionInExportAndTable = compileWat("sameFunctionInExportAndTable", "(module (func $f (result i32) i32.const 1) (table 2 funcref) (elem (i32.const 0) $f) (elem (i32.const 1) $f) (export \"t\" (table 0)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, sameFunctionInExportAndTable, null);
        final WasmTable t = (WasmTable) WebAssembly.instanceExport(instance, "t");
        final Object f1 = WebAssembly.tableRead(t, 0);
        final Object f2 = WebAssembly.tableRead(t, 1);
        Assert.assertTrue("Returned function instances must be reference equal", f1 == f2);
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) 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