Search in sources :

Example 26 with WasmInstance

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

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

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

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

Example 30 with WasmInstance

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

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