Search in sources :

Example 1 with WasmFunctionInstance

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

the class WasmBlockNode method executeDirectCall.

@BytecodeInterpreterSwitchBoundary
private Object executeDirectCall(int childrenOffset, WasmFunction function, Object[] args) {
    final boolean imported = function.isImported();
    CompilerAsserts.partialEvaluationConstant(imported);
    DirectCallNode callNode = (DirectCallNode) children[childrenOffset];
    assert assertDirectCall(function, callNode);
    if (imported) {
        WasmFunctionInstance functionInstance = instance().functionInstance(function.index());
        TruffleContext truffleContext = functionInstance.getTruffleContext();
        Object prev = truffleContext.enter(this);
        try {
            return callNode.call(args);
        } finally {
            truffleContext.leave(this, prev);
        }
    } else {
        return callNode.call(args);
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) BytecodeInterpreterSwitchBoundary(com.oracle.truffle.api.HostCompilerDirectives.BytecodeInterpreterSwitchBoundary)

Example 2 with WasmFunctionInstance

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

the class WasmJsApiSuite method testTableInstanceOutOfBoundsSet.

@Test
public void testTableInstanceOutOfBoundsSet() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMixedExports, null);
        final InteropLibrary lib = InteropLibrary.getUncached();
        WasmContext wasmContext = WasmContext.get(null);
        final WasmFunctionInstance functionInstance = new WasmFunctionInstance(wasmContext, null, new RootNode(wasmContext.language()) {

            @Override
            public Object execute(VirtualFrame frame) {
                return 42;
            }
        }.getCallTarget());
        // We should be able to set element 1.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object writeTable = wasm.readMember("table_write");
            lib.execute(writeTable, table, 0, functionInstance);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But not element 2.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object writeTable = wasm.readMember("table_write");
            lib.execute(writeTable, table, 1, functionInstance);
            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) RootNode(com.oracle.truffle.api.nodes.RootNode) WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WebAssembly(org.graalvm.wasm.api.WebAssembly) ArityException(com.oracle.truffle.api.interop.ArityException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) WasmContext(org.graalvm.wasm.WasmContext) 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)

Example 3 with WasmFunctionInstance

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

Example 4 with WasmFunctionInstance

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

the class WasmJsApiSuite method testFuncTypeMultipleParameters.

@Test
public void testFuncTypeMultipleParameters() throws IOException, InterruptedException {
    final byte[] source = compileWat("data", "(module (func (export \"func\") (param i32 i64) (result f32) f32.const 0))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, source, null);
        final WasmFunctionInstance fn = (WasmFunctionInstance) WebAssembly.instanceExport(instance, "func");
        final String fnType = WebAssembly.functionTypeToString(fn.function());
        Assert.assertEquals("func_type", "0(i32 i64)f32", fnType);
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) Test(org.junit.Test)

Example 5 with WasmFunctionInstance

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

the class WasmJsApiSuite method testInstantiateWithImportTable.

@Test
public void testInstantiateWithImportTable() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmTable table = WebAssembly.tableAlloc(4, 8);
        Dictionary importObject = Dictionary.create(new Object[] { "host", Dictionary.create(new Object[] { "defaultTable", table }) });
        WebAssembly.tableWrite(table, 0, new WasmFunctionInstance(context, null, new RootNode(context.language()) {

            @Override
            public Object execute(VirtualFrame frame) {
                return 210;
            }
        }.getCallTarget()));
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableImport, importObject);
        try {
            final Object callFirst = WebAssembly.instanceExport(instance, "callFirst");
            Object result = InteropLibrary.getUncached(callFirst).execute(callFirst);
            Assert.assertEquals("Must return 210.", 210, InteropLibrary.getUncached(result).asInt(result));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Dictionary(org.graalvm.wasm.api.Dictionary) RootNode(com.oracle.truffle.api.nodes.RootNode) WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) WasmFunctionInstance(org.graalvm.wasm.WasmFunctionInstance) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Aggregations

WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)7 WasmInstance (org.graalvm.wasm.WasmInstance)5 WebAssembly (org.graalvm.wasm.api.WebAssembly)4 Test (org.junit.Test)4 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 RootNode (com.oracle.truffle.api.nodes.RootNode)3 WasmTable (org.graalvm.wasm.WasmTable)3 BytecodeInterpreterSwitchBoundary (com.oracle.truffle.api.HostCompilerDirectives.BytecodeInterpreterSwitchBoundary)2 TruffleContext (com.oracle.truffle.api.TruffleContext)2 ArityException (com.oracle.truffle.api.interop.ArityException)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)2 SymbolTable (org.graalvm.wasm.SymbolTable)2 WasmContext (org.graalvm.wasm.WasmContext)2 WasmFunction (org.graalvm.wasm.WasmFunction)2 WasmMemory (org.graalvm.wasm.memory.WasmMemory)2 CallTarget (com.oracle.truffle.api.CallTarget)1