Search in sources :

Example 21 with WebAssembly

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

the class WasmJsApiSuite method testNameSection.

@Test
public void testNameSection() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        // Should not throw an exception i.e. is a valid module
        // (despite the name section may not be formed correctly).
        wasm.moduleDecode(binaryWithEmptyNameSection);
        wasm.moduleDecode(binaryWithTruncatedNameSection);
        wasm.moduleDecode(binaryWithNameSectionWithInvalidIndex);
    });
}
Also used : WebAssembly(org.graalvm.wasm.api.WebAssembly) Test(org.junit.Test)

Example 22 with WebAssembly

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

the class WasmJsApiSuite method testFunctionEmbedderData.

@Test
public void testFunctionEmbedderData() throws IOException {
    runTest(context -> {
        WebAssembly wasm = new WebAssembly(context);
        WasmInstance instance = moduleInstantiate(wasm, binaryWithExports, null);
        Object fn = WebAssembly.instanceExport(instance, "main");
        checkEmbedderData(fn);
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 23 with WebAssembly

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

Example 24 with WebAssembly

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

the class WasmJsApiSuite method testImportManyGlobals.

@Test
public void testImportManyGlobals() throws IOException, InterruptedException {
    String importManyGlobalsWat = "(module\n" + "(global $global0 (import \"globals\" \"global0\") i32)\n" + "(global $global1 (import \"globals\" \"global1\") i32)\n" + "(global $global2 (import \"globals\" \"global2\") i32)\n" + "(global $global3 (import \"globals\" \"global3\") i32)\n" + "(global $global4 (import \"globals\" \"global4\") i32)\n" + "(global $global5 (import \"globals\" \"global5\") i32)\n" + "(global $global6 (import \"globals\" \"global6\") i32)\n" + "(global $global7 (import \"globals\" \"global7\") i32)\n" + "(global $global8 (import \"globals\" \"global8\") i32)\n" + "(func (export \"sum\") (result i32)\n" + "    get_global $global0\n" + "    get_global $global1\n" + "    i32.add\n" + "    get_global $global2\n" + "    i32.add\n" + "    get_global $global3\n" + "    i32.add\n" + "    get_global $global4\n" + "    i32.add\n" + "    get_global $global5\n" + "    i32.add\n" + "    get_global $global6\n" + "    i32.add\n" + "    get_global $global7\n" + "    i32.add\n" + "    get_global $global8\n" + "    i32.add\n" + ")\n" + ")";
    byte[] importManyGlobalsBytes = compileWat("importManyGlobals", importManyGlobalsWat);
    runTest(context -> {
        WebAssembly wasm = new WebAssembly(context);
        Dictionary importObject = Dictionary.create(new Object[] { "globals", Dictionary.create(new Object[] { "global0", WebAssembly.globalAlloc(ValueType.i32, false, 1), "global1", WebAssembly.globalAlloc(ValueType.i32, false, 2), "global2", WebAssembly.globalAlloc(ValueType.i32, false, 3), "global3", WebAssembly.globalAlloc(ValueType.i32, false, 4), "global4", WebAssembly.globalAlloc(ValueType.i32, false, 5), "global5", WebAssembly.globalAlloc(ValueType.i32, false, 6), "global6", WebAssembly.globalAlloc(ValueType.i32, false, 7), "global7", WebAssembly.globalAlloc(ValueType.i32, false, 8), "global8", WebAssembly.globalAlloc(ValueType.i32, false, 9) }) });
        WasmInstance instance = moduleInstantiate(wasm, importManyGlobalsBytes, importObject);
        try {
            InteropLibrary lib = InteropLibrary.getUncached();
            Object sum = lib.execute(WebAssembly.instanceExport(instance, "sum"));
            int intSum = lib.asInt(sum);
            Assert.assertEquals("Incorrect sum of imported globals", 45, intSum);
        } 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) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) InteropException(com.oracle.truffle.api.interop.InteropException) Test(org.junit.Test)

Example 25 with WebAssembly

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

the class WasmJsApiSuite method testExportSameFunctionWithDifferentNames.

@Test
public void testExportSameFunctionWithDifferentNames() throws IOException, InterruptedException {
    final byte[] sameFunctionWithDifferentNames = compileWat("sameFunctionWithDifferentNames", "(module (func $f (result i32) i32.const 1) (export \"f1\" (func $f)) (export \"f2\" (func $f)))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, sameFunctionWithDifferentNames, null);
        final Object f1 = WebAssembly.instanceExport(instance, "f1");
        final Object f2 = WebAssembly.instanceExport(instance, "f2");
        Assert.assertTrue("Returned function instances must be reference equal", f1 == f2);
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) 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