Search in sources :

Example 16 with WasmInstance

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

the class WasmJsApiSuite method testExportMemoryTwice.

@Test
public void testExportMemoryTwice() throws IOException, InterruptedException {
    final byte[] exportMemoryTwice = compileWat("exportMemoryTwice", "(memory 1) (export \"a\" (memory 0)) (export \"b\" (memory 0))");
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
        try {
            final InteropLibrary lib = InteropLibrary.getUncached();
            final Object memoryABuffer = WebAssembly.instanceExport(instance, "a");
            final Object memoryBBuffer = WebAssembly.instanceExport(instance, "b");
            lib.writeArrayElement(memoryABuffer, 0, (byte) 42);
            final byte readValue = lib.asByte(lib.readArrayElement(memoryBBuffer, 0));
            Assert.assertEquals("Written value should correspond to read value", (byte) 42, readValue);
        } catch (UnsupportedMessageException | UnsupportedTypeException | InvalidArrayIndexException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) 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) Test(org.junit.Test)

Example 17 with WasmInstance

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

the class WasmJsApiSuite method testTableInstanceGrowLimit.

@Test
public void testTableInstanceGrowLimit() 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 grow the table to 10,000,000.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object grow = wasm.readMember("table_grow");
            lib.execute(grow, table, 9999999);
        } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException | ArityException e) {
            throw new RuntimeException(e);
        }
        // But growing to 10,000,001 should fail.
        try {
            final Object table = WebAssembly.instanceExport(instance, "t");
            final Object grow = wasm.readMember("table_grow");
            lib.execute(grow, 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 18 with WasmInstance

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

the class WasmJsApiSuite method testFuncTypeExport.

@Test
public void testFuncTypeExport() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithMemoryExport, null);
        try {
            final Object funcType = wasm.readMember("func_type");
            final Object fn = WebAssembly.instanceExport(instance, "readZero");
            Assert.assertEquals("func_type", "0()i32", InteropLibrary.getUncached(funcType).execute(funcType, fn));
        } 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 19 with WasmInstance

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

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

the class WasiModule method createInstance.

@Override
protected WasmInstance createInstance(WasmLanguage language, WasmContext context, String name) {
    WasmInstance instance = new WasmInstance(context, WasmModule.createBuiltin(name), NUMBER_OF_FUNCTIONS);
    importMemory(instance, "main", "memory", 0, MAX_MEMORY_DECLARATION_SIZE);
    defineFunction(instance, "args_sizes_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiArgsSizesGetNode(language, instance));
    defineFunction(instance, "args_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiArgsGetNode(language, instance));
    defineFunction(instance, "environ_sizes_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiEnvironSizesGetNode(language, instance));
    defineFunction(instance, "environ_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiEnvironGetNode(language, instance));
    defineFunction(instance, "clock_time_get", types(I32_TYPE, I64_TYPE, I32_TYPE), types(I32_TYPE), new WasiClockTimeGetNode(language, instance));
    defineFunction(instance, "proc_exit", types(I32_TYPE), types(), new WasiProcExitNode(language, instance));
    defineFunction(instance, "fd_write", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdWriteNode(language, instance));
    defineFunction(instance, "fd_read", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdReadNode(language, instance));
    defineFunction(instance, "fd_close", types(I32_TYPE), types(I32_TYPE), new WasiFdCloseNode(language, instance));
    defineFunction(instance, "fd_seek", types(I32_TYPE, I64_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdSeekNode(language, instance));
    defineFunction(instance, "fd_fdstat_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdFdstatGetNode(language, instance));
    defineFunction(instance, "fd_fdstat_set_flags", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdFdstatSetFlagsNode(language, instance));
    defineFunction(instance, "fd_prestat_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdPrestatGetNode(language, instance));
    defineFunction(instance, "fd_prestat_dir_name", types(I32_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdPrestatDirNameNode(language, instance));
    defineFunction(instance, "fd_filestat_get", types(I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiFdFilestatGetNode(language, instance));
    defineFunction(instance, "path_open", types(I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE, I32_TYPE, I64_TYPE, I64_TYPE, I32_TYPE, I32_TYPE), types(I32_TYPE), new WasiPathOpenNode(language, instance));
    return instance;
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance)

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