Search in sources :

Example 21 with WasmInstance

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

the class WasmFileSuite method runInContext.

private static void runInContext(WasmCase testCase, Context context, List<Source> sources, int iterations, String phaseIcon, String phaseLabel) throws IOException {
    try {
        // Whereas the test needs memory to be reset between iterations.
        final boolean requiresZeroMemory = Boolean.parseBoolean(testCase.options().getProperty("zero-memory", "false"));
        resetStatus(System.out, PHASE_PARSE_ICON, "parsing");
        // This is needed so that we can call WasmContext.getCurrent().
        context.enter();
        try {
            sources.forEach(context::eval);
        } catch (PolyglotException e) {
            validateThrown(testCase.data(), WasmCaseData.ErrorType.Validation, e);
            return;
        }
        final WasmContext wasmContext = WasmContext.get(null);
        final Value mainFunction = findMain(wasmContext);
        resetStatus(System.out, phaseIcon, phaseLabel);
        final String argString = testCase.options().getProperty("argument");
        final Integer arg = argString == null ? null : Integer.parseInt(argString);
        ContextState firstIterationContextState = null;
        for (int i = 0; i != iterations; ++i) {
            try {
                TEST_OUT.reset();
                final Value result = arg == null ? mainFunction.execute() : mainFunction.execute(arg);
                WasmCase.validateResult(testCase.data().resultValidator(), result, TEST_OUT);
            } catch (PolyglotException e) {
                // then we check stdout.
                if (e.isExit() && testCase.data().expectedErrorMessage() == null) {
                    Assert.assertEquals("Program exited with non-zero return code.", e.getExitStatus(), 0);
                    WasmCase.validateResult(testCase.data().resultValidator(), null, TEST_OUT);
                } else if (testCase.data().expectedErrorTime() == WasmCaseData.ErrorType.Validation) {
                    validateThrown(testCase.data(), WasmCaseData.ErrorType.Validation, e);
                    return;
                } else {
                    validateThrown(testCase.data(), WasmCaseData.ErrorType.Runtime, e);
                }
            } catch (Throwable t) {
                final RuntimeException e = new RuntimeException("Error during test phase '" + phaseLabel + "'", t);
                e.setStackTrace(new StackTraceElement[0]);
                throw e;
            } finally {
                // Save context state, and check that it's consistent with the previous one.
                if (iterationNeedsStateCheck(i)) {
                    final ContextState contextState = saveContext(wasmContext);
                    if (firstIterationContextState == null) {
                        firstIterationContextState = contextState;
                    } else {
                        assertContextEqual(firstIterationContextState, contextState);
                    }
                }
                // Reset context state.
                final boolean reinitMemory = requiresZeroMemory || iterationNeedsStateCheck(i + 1);
                if (reinitMemory) {
                    for (int j = 0; j < wasmContext.memories().count(); ++j) {
                        wasmContext.memories().memory(j).reset();
                    }
                    for (int j = 0; j < wasmContext.tables().tableCount(); ++j) {
                        wasmContext.tables().table(j).reset();
                    }
                }
                for (final WasmInstance instance : wasmContext.moduleInstances().values()) {
                    if (!instance.isBuiltin()) {
                        wasmContext.reinitInstance(instance, reinitMemory);
                    }
                }
                // Reset stdin
                if (wasmContext.environment().in() instanceof ByteArrayInputStream) {
                    wasmContext.environment().in().reset();
                }
            }
        }
    } finally {
        System.setOut(System.out);
        context.close(true);
    }
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) PolyglotException(org.graalvm.polyglot.PolyglotException) WasmContext(org.graalvm.wasm.WasmContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Value(org.graalvm.polyglot.Value)

Example 22 with WasmInstance

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

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

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

Example 25 with WasmInstance

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

the class WasmJsApiSuite method testInstantiateWithExportTable.

@Test
public void testInstantiateWithExportTable() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableExport, null);
        try {
            final WasmTable table = (WasmTable) WebAssembly.instanceExport(instance, "defaultTable");
            final Object result = InteropLibrary.getUncached().execute(WebAssembly.tableRead(table, 0), 9);
            Assert.assertEquals("Must be 81.", 81, result);
        } catch (UnsupportedTypeException | UnsupportedMessageException | ArityException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) 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)

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