Search in sources :

Example 26 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class WasmJsApiSuite method testExportSameFunctionInDifferentModuleTable.

@Test
public void testExportSameFunctionInDifferentModuleTable() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (func $f (result i32) i32.const 42) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"m\" \"f\" (func $f (result i32))) (table 1 funcref) (elem (i32.const 0) $f) (export \"t\" (table 0)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Function = WebAssembly.instanceExport(m1Instance, "f");
            final Dictionary d = new Dictionary();
            d.addMember("m", Dictionary.create(new Object[] { "f", m1Function }));
            final WasmInstance m2Instance = moduleInstantiate(wasm, m2, d);
            final Object m2Table = WebAssembly.instanceExport(m2Instance, "t");
            final Object m2Function = WebAssembly.tableRead((WasmTable) m2Table, 0);
            Assert.assertTrue("Returned function instances must be reference equal", m1Function == m2Function);
            final Object m1Value = lib.execute(m1Function);
            final Object m2Value = lib.execute(m2Function);
            Assert.assertEquals("Return value of functions is equal", m1Value, m2Value);
        } catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) Dictionary(org.graalvm.wasm.api.Dictionary) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) 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 27 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class WasmJsApiSuite method testExportSameFunctionInDifferentModules.

@Test
public void testExportSameFunctionInDifferentModules() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (func $f (result i32) i32.const 42) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"m\" \"f\" (func $f (result i32))) (export \"f\" (func $f)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Function = WebAssembly.instanceExport(m1Instance, "f");
            final Dictionary d = new Dictionary();
            d.addMember("m", Dictionary.create(new Object[] { "f", m1Function }));
            final WasmInstance m2Instance = moduleInstantiate(wasm, m2, d);
            final Object m2Function = WebAssembly.instanceExport(m2Instance, "f");
            Assert.assertTrue("Returned function instances must be reference equal", m1Function == m2Function);
            final Object m1Value = lib.execute(m1Function);
            final Object m2Value = lib.execute(m2Function);
            Assert.assertEquals("Return value of functions is equal", m1Value, m2Value);
        } catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) Dictionary(org.graalvm.wasm.api.Dictionary) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) 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 28 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class WasmJsApiSuite method testExportImportedFunctionInDifferentModuleTable.

@Test
public void testExportImportedFunctionInDifferentModuleTable() throws IOException, InterruptedException {
    final byte[] m1 = compileWat("export", "(module (import \"a\" \"f\" (func $f (result i32))) (table $f 1 funcref) (elem (i32.const 0) $f) (export \"t\" (table 0)) (export \"f\" (func $f)))");
    final byte[] m2 = compileWat("import", "(module (import \"b\" \"t\" (table $t 1 funcref)) (export \"t\" (table 0)))");
    runTest(context -> {
        final Dictionary importObject = new Dictionary();
        importObject.addMember("a", Dictionary.create(new Object[] { "f", new Executable(args -> 2) }));
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance m1Instance = moduleInstantiate(wasm, m1, importObject);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object m1Function = WebAssembly.instanceExport(m1Instance, "f");
            final Object m1Table = WebAssembly.instanceExport(m1Instance, "t");
            final Object m1TableFunction = WebAssembly.tableRead((WasmTable) m1Table, 0);
            Assert.assertTrue("Returned function instances must be reference equal", m1Function == m1TableFunction);
            Object m1Value = lib.execute(m1Function);
            final Object m1TableValue = lib.execute(m1TableFunction);
            Assert.assertEquals("Return value of functions is equal", m1Value, m1TableValue);
            final Dictionary d = new Dictionary();
            d.addMember("b", Dictionary.create(new Object[] { "f", m1Function, "t", m1Table }));
            final WasmInstance m2Instance = moduleInstantiate(wasm, m2, d);
            final Object m2Table = WebAssembly.instanceExport(m2Instance, "t");
            final Object m2Function = WebAssembly.tableRead((WasmTable) m2Table, 0);
            Assert.assertTrue("Returned function instances must be reference equal", m1Function == m2Function);
            m1Value = lib.execute(m1Function);
            final Object m2Value = lib.execute(m2Function);
            Assert.assertEquals("Return value of functions is equal", m1Value, m2Value);
        } catch (UnsupportedMessageException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : Dictionary(org.graalvm.wasm.api.Dictionary) WasmInstance(org.graalvm.wasm.WasmInstance) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) 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) Executable(org.graalvm.wasm.api.Executable) ArityException(com.oracle.truffle.api.interop.ArityException) Test(org.junit.Test)

Example 29 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class WasmJsApiSuite method testInstantiateWithExportGlobal.

@Test
public void testInstantiateWithExportGlobal() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithGlobalExport, null);
        try {
            final WasmGlobal global = (WasmGlobal) WebAssembly.instanceExport(instance, "exportedGlobal");
            Assert.assertEquals("Exported global must be 1096.", 1096, global.loadAsInt());
            final Object setGlobal = WebAssembly.instanceExport(instance, "setGlobal");
            final Object getGlobal = WebAssembly.instanceExport(instance, "getGlobal");
            InteropLibrary interop = InteropLibrary.getUncached();
            final Object result1 = interop.execute(getGlobal);
            Assert.assertEquals("Must be 2345 initially.", 2345, interop.asInt(result1));
            interop.execute(setGlobal, 25);
            final Object result2 = interop.execute(getGlobal);
            Assert.assertEquals("Must be 25 later.", 25, interop.asInt(result2));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : 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) Test(org.junit.Test)

Example 30 with InteropLibrary

use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.

the class WasmJsApiSuite method testExportTableTwice.

@Test
public void testExportTableTwice() throws IOException, InterruptedException {
    final byte[] exportMemoryTwice = compileWat("exportTableTwice", "(module (table 1 funcref) (export \"a\" (table 0)) (export \"b\" (table 0)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        try {
            final Object f = new WasmFunctionInstance(context, null, new RootNode(context.language()) {

                @Override
                public Object execute(VirtualFrame frame) {
                    return 42;
                }
            }.getCallTarget());
            final Object writeTable = wasm.readMember("table_write");
            final Object readTable = wasm.readMember("table_read");
            final Object a = WebAssembly.instanceExport(instance, "a");
            final Object b = WebAssembly.instanceExport(instance, "b");
            lib.execute(writeTable, a, 0, f);
            final Object readValue = lib.execute(readTable, b, 0);
            Assert.assertEquals("Written function should correspond ro read function", 42, lib.asInt(lib.execute(readValue)));
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) RootNode(com.oracle.truffle.api.nodes.RootNode) WebAssembly(org.graalvm.wasm.api.WebAssembly) ArityException(com.oracle.truffle.api.interop.ArityException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) Test(org.junit.Test)

Aggregations

InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)158 Test (org.junit.Test)76 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)60 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)51 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)18 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)18 ArityException (com.oracle.truffle.api.interop.ArityException)16 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)15 WasmInstance (org.graalvm.wasm.WasmInstance)15 WebAssembly (org.graalvm.wasm.api.WebAssembly)15 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)12 RootNode (com.oracle.truffle.api.nodes.RootNode)12 InteropException (com.oracle.truffle.api.interop.InteropException)11 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)11 Dictionary (org.graalvm.wasm.api.Dictionary)8 SourceSection (com.oracle.truffle.api.source.SourceSection)6 CallTarget (com.oracle.truffle.api.CallTarget)5 Node (com.oracle.truffle.api.nodes.Node)5 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)5 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)5