Skip to content

Commit

Permalink
Implementation of func_type() from WebAssembly embedding interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamstolis committed Jul 17, 2021
1 parent 32e53c5 commit e40daab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
26 changes: 3 additions & 23 deletions wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/api/Module.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -55,7 +55,6 @@
import org.graalvm.wasm.WasmCustomSection;
import org.graalvm.wasm.WasmFunction;
import org.graalvm.wasm.WasmModule;
import org.graalvm.wasm.WasmType;
import org.graalvm.wasm.constants.ImportIdentifier;
import org.graalvm.wasm.exception.Failure;
import org.graalvm.wasm.exception.WasmException;
Expand Down Expand Up @@ -85,7 +84,7 @@ public Sequence<ModuleExportDescriptor> exports() {
} else if (module.exportedTableNames().contains(name)) {
list.add(new ModuleExportDescriptor(name, table.name(), null));
} else if (f != null) {
list.add(new ModuleExportDescriptor(name, function.name(), functionTypeToString(f)));
list.add(new ModuleExportDescriptor(name, function.name(), WebAssembly.functionTypeToString(f)));
} else if (globalIndex != null) {
String valueType = ValueType.fromByteValue(module.globalValueType(globalIndex)).toString();
list.add(new ModuleExportDescriptor(name, global.name(), valueType));
Expand All @@ -104,7 +103,7 @@ public Sequence<ModuleImportDescriptor> imports() {
switch (descriptor.identifier) {
case ImportIdentifier.FUNCTION:
final WasmFunction f = module.importedFunction(descriptor);
list.add(new ModuleImportDescriptor(f.importedModuleName(), f.importedFunctionName(), function.name(), functionTypeToString(f)));
list.add(new ModuleImportDescriptor(f.importedModuleName(), f.importedFunctionName(), function.name(), WebAssembly.functionTypeToString(f)));
break;
case ImportIdentifier.TABLE:
if (Objects.equals(module.importedTable(), descriptor)) {
Expand Down Expand Up @@ -135,25 +134,6 @@ public Sequence<ModuleImportDescriptor> imports() {
return new Sequence<>(list);
}

private static String functionTypeToString(WasmFunction f) {
StringBuilder typeInfo = new StringBuilder();

typeInfo.append(f.index());

typeInfo.append('(');
int argumentCount = f.numArguments();
for (int i = 0; i < argumentCount; i++) {
typeInfo.append(ValueType.fromByteValue(f.argumentTypeAt(i)));
}
typeInfo.append(')');

byte returnType = f.returnType();
if (returnType != WasmType.VOID_TYPE) {
typeInfo.append(ValueType.fromByteValue(f.returnType()));
}
return typeInfo.toString();
}

public Sequence<ByteArrayBuffer> customSections(Object sectionName) {
List<ByteArrayBuffer> sections = new ArrayList<>();
for (WasmCustomSection section : module.customSections()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import org.graalvm.wasm.WasmContext;
import org.graalvm.wasm.WasmFunction;
import org.graalvm.wasm.WasmFunctionInstance;
import org.graalvm.wasm.WasmType;
import org.graalvm.wasm.exception.WasmException;
import org.graalvm.wasm.exception.WasmJsApiException;

Expand All @@ -65,6 +68,8 @@ public WebAssembly(WasmContext currentContext) {
module.addMember("imports", new Executable(args -> moduleImports(args)));
module.addMember("customSections", new Executable(args -> moduleCustomSections(args)));
addMember("Module", module);

addMember("func_type", new Executable(args -> funcType(args)));
}

private Object instantiate(Object[] args) {
Expand Down Expand Up @@ -251,4 +256,33 @@ private static Object moduleCustomSections(Object[] args) {
return toModule(args).customSections(args[1]);
}

private static Object funcType(Object[] args) {
checkArgumentCount(args, 1);
if (args[0] instanceof WasmFunctionInstance) {
WasmFunction fn = ((WasmFunctionInstance) args[0]).function();
return functionTypeToString(fn);
} else {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, "First argument must be wasm function");
}
}

public static String functionTypeToString(WasmFunction f) {
StringBuilder typeInfo = new StringBuilder();

typeInfo.append(f.index());

typeInfo.append('(');
int argumentCount = f.numArguments();
for (int i = 0; i < argumentCount; i++) {
typeInfo.append(ValueType.fromByteValue(f.argumentTypeAt(i)));
}
typeInfo.append(')');

byte returnType = f.returnType();
if (returnType != WasmType.VOID_TYPE) {
typeInfo.append(ValueType.fromByteValue(f.returnType()));
}
return typeInfo.toString();
}

}

0 comments on commit e40daab

Please sign in to comment.