Search in sources :

Example 1 with WasmTable

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

the class WebAssembly method tableWrite.

private static Object tableWrite(Object[] args) {
    checkArgumentCount(args, 3);
    if (!(args[0] instanceof WasmTable)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm table");
    }
    if (!(args[1] instanceof Integer)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Second argument must be integer");
    }
    WasmTable table = (WasmTable) args[0];
    int index = (Integer) args[1];
    return tableWrite(table, index, args[2]);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmTable(org.graalvm.wasm.WasmTable)

Example 2 with WasmTable

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

the class WebAssembly method readModuleImports.

private static HashMap<String, ImportModule> readModuleImports(WasmModule module, Object importObject) {
    CompilerAsserts.neverPartOfCompilation();
    final Sequence<ModuleImportDescriptor> imports = moduleImports(module);
    if (imports.getArraySize() != 0 && importObject == null) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "Module requires imports, but import object is undefined.");
    }
    HashMap<String, ImportModule> importModules = new HashMap<>();
    final InteropLibrary lib = InteropLibrary.getUncached();
    try {
        int i = 0;
        while (i < imports.getArraySize()) {
            final ModuleImportDescriptor d = (ModuleImportDescriptor) imports.readArrayElement(i);
            final Object importedModule = getMember(importObject, d.module());
            final Object member = getMember(importedModule, d.name());
            switch(d.kind()) {
                case function:
                    if (!lib.isExecutable(member)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not callable.");
                    }
                    WasmFunction f = module.importedFunction(d.name());
                    ensureImportModule(importModules, d.module()).addFunction(d.name(), Pair.create(f, member));
                    break;
                case memory:
                    if (!(member instanceof WasmMemory)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid memory.");
                    }
                    ensureImportModule(importModules, d.module()).addMemory(d.name(), (WasmMemory) member);
                    break;
                case table:
                    if (!(member instanceof WasmTable)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid table.");
                    }
                    ensureImportModule(importModules, d.module()).addTable(d.name(), (WasmTable) member);
                    break;
                case global:
                    if (!(member instanceof WasmGlobal)) {
                        throw new WasmJsApiException(WasmJsApiException.Kind.LinkError, "Member " + member + " is not a valid global.");
                    }
                    ensureImportModule(importModules, d.module()).addGlobal(d.name(), (WasmGlobal) member);
                    break;
                default:
                    throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unimplemented case: " + d.kind());
            }
            i += 1;
        }
    } catch (InvalidArrayIndexException | UnknownIdentifierException | ClassCastException | UnsupportedMessageException e) {
        throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unexpected state.");
    }
    return importModules;
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) HashMap(java.util.HashMap) WasmTable(org.graalvm.wasm.WasmTable) WasmFunction(org.graalvm.wasm.WasmFunction) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) ExportedWasmGlobal(org.graalvm.wasm.globals.ExportedWasmGlobal) DefaultWasmGlobal(org.graalvm.wasm.globals.DefaultWasmGlobal) WasmGlobal(org.graalvm.wasm.globals.WasmGlobal) UnsafeWasmMemory(org.graalvm.wasm.memory.UnsafeWasmMemory) WasmMemory(org.graalvm.wasm.memory.WasmMemory) ByteArrayWasmMemory(org.graalvm.wasm.memory.ByteArrayWasmMemory)

Example 3 with WasmTable

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

the class WebAssembly method tableSize.

private static Object tableSize(Object[] args) {
    checkArgumentCount(args, 1);
    if (!(args[0] instanceof WasmTable)) {
        throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm table");
    }
    WasmTable table = (WasmTable) args[0];
    return tableSize(table);
}
Also used : WasmJsApiException(org.graalvm.wasm.exception.WasmJsApiException) WasmTable(org.graalvm.wasm.WasmTable)

Example 4 with WasmTable

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

the class WasmJsApiSuite method testFuncTypeTable.

@Test
public void testFuncTypeTable() throws IOException {
    runTest(context -> {
        final WebAssembly wasm = new WebAssembly(context);
        final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableExport, null);
        try {
            final Object funcType = wasm.readMember("func_type");
            final WasmTable table = (WasmTable) WebAssembly.instanceExport(instance, "defaultTable");
            final Object fn = WebAssembly.tableRead(table, 0);
            InteropLibrary interop = InteropLibrary.getUncached(funcType);
            Assert.assertEquals("func_type", "0(i32)i32", interop.execute(funcType, fn));
            // set + get should not break func_type()
            WebAssembly.tableWrite(table, 0, fn);
            final Object fnAgain = WebAssembly.tableRead(table, 0);
            Assert.assertEquals("func_type", "0(i32)i32", interop.execute(funcType, fnAgain));
        } catch (InteropException e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : WasmInstance(org.graalvm.wasm.WasmInstance) WebAssembly(org.graalvm.wasm.api.WebAssembly) WasmTable(org.graalvm.wasm.WasmTable) 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 5 with WasmTable

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

WasmTable (org.graalvm.wasm.WasmTable)14 Test (org.junit.Test)7 WasmInstance (org.graalvm.wasm.WasmInstance)6 WasmJsApiException (org.graalvm.wasm.exception.WasmJsApiException)6 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)5 WebAssembly (org.graalvm.wasm.api.WebAssembly)5 WasmFunction (org.graalvm.wasm.WasmFunction)3 WasmFunctionInstance (org.graalvm.wasm.WasmFunctionInstance)3 WasmMemory (org.graalvm.wasm.memory.WasmMemory)3 InteropException (com.oracle.truffle.api.interop.InteropException)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)2 HashMap (java.util.HashMap)2 SymbolTable (org.graalvm.wasm.SymbolTable)2 WasmGlobal (org.graalvm.wasm.globals.WasmGlobal)2 CallTarget (com.oracle.truffle.api.CallTarget)1 BytecodeInterpreterSwitch (com.oracle.truffle.api.HostCompilerDirectives.BytecodeInterpreterSwitch)1 BytecodeInterpreterSwitchBoundary (com.oracle.truffle.api.HostCompilerDirectives.BytecodeInterpreterSwitchBoundary)1 TruffleContext (com.oracle.truffle.api.TruffleContext)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1