Search in sources :

Example 31 with WasmInstance

use of org.graalvm.wasm.WasmInstance 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 32 with WasmInstance

use of org.graalvm.wasm.WasmInstance 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 33 with WasmInstance

use of org.graalvm.wasm.WasmInstance 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)

Example 34 with WasmInstance

use of org.graalvm.wasm.WasmInstance in project graal by oracle.

the class WebAssembly method instanceExport.

private static Object instanceExport(Object[] args) {
    checkArgumentCount(args, 2);
    if (!(args[0] instanceof WasmInstance)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm instance");
    }
    if (!(args[1] instanceof String)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Second argument must be string");
    }
    WasmInstance instance = (WasmInstance) args[0];
    String name = (String) args[1];
    return instanceExport(instance, name);
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException)

Example 35 with WasmInstance

use of org.graalvm.wasm.WasmInstance in project graal by oracle.

the class WebAssembly method moduleInstantiate.

public WasmInstance moduleInstantiate(WasmModule module, Object importObject) {
    final TruffleContext innerTruffleContext = currentContext.environment().newContextBuilder().build();
    final Object prev = innerTruffleContext.enter(null);
    try {
        final WasmContext instanceContext = WasmContext.get(null);
        WasmInstance instance = instantiateModule(module, importObject, instanceContext, innerTruffleContext);
        instanceContext.linker().tryLink(instance);
        return instance;
    } finally {
        innerTruffleContext.leave(null, prev);
    }
}
Also used : WasmContext(org.graalvm.wasm.WasmContext) WasmInstance(org.graalvm.wasm.WasmInstance) TruffleContext(com.oracle.truffle.api.TruffleContext)

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