Search in sources :

Example 1 with ImportDescriptor

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

the class WebAssembly method moduleImports.

public static Sequence<ModuleImportDescriptor> moduleImports(WasmModule module) {
    CompilerAsserts.neverPartOfCompilation();
    final EconomicMap<ImportDescriptor, Integer> importedGlobalDescriptors = module.importedGlobalDescriptors();
    final ArrayList<ModuleImportDescriptor> list = new ArrayList<>();
    for (ImportDescriptor descriptor : module.importedSymbols()) {
        switch(descriptor.identifier) {
            case ImportIdentifier.FUNCTION:
                final WasmFunction f = module.importedFunction(descriptor);
                list.add(new ModuleImportDescriptor(f.importedModuleName(), f.importedFunctionName(), ImportExportKind.function.name(), WebAssembly.functionTypeToString(f)));
                break;
            case ImportIdentifier.TABLE:
                if (Objects.equals(module.importedTable(), descriptor)) {
                    list.add(new ModuleImportDescriptor(descriptor.moduleName, descriptor.memberName, ImportExportKind.table.name(), null));
                } else {
                    throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Table import inconsistent.");
                }
                break;
            case ImportIdentifier.MEMORY:
                if (Objects.equals(module.importedMemory(), descriptor)) {
                    list.add(new ModuleImportDescriptor(descriptor.moduleName, descriptor.memberName, ImportExportKind.memory.name(), null));
                } else {
                    throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Memory import inconsistent.");
                }
                break;
            case ImportIdentifier.GLOBAL:
                final Integer index = importedGlobalDescriptors.get(descriptor);
                String valueType = ValueType.fromByteValue(module.globalValueType(index)).toString();
                list.add(new ModuleImportDescriptor(descriptor.moduleName, descriptor.memberName, ImportExportKind.global.name(), valueType));
                break;
            default:
                throw WasmException.create(Failure.UNSPECIFIED_INTERNAL, "Unknown import descriptor type: " + descriptor.identifier);
        }
    }
    return new Sequence<>(list);
}
Also used : ArrayList(java.util.ArrayList) ImportDescriptor(org.graalvm.wasm.ImportDescriptor) WasmFunction(org.graalvm.wasm.WasmFunction)

Aggregations

ArrayList (java.util.ArrayList)1 ImportDescriptor (org.graalvm.wasm.ImportDescriptor)1 WasmFunction (org.graalvm.wasm.WasmFunction)1