use of org.graalvm.wasm.WasmFunction in project graal by oracle.
the class WasmBlockNode method resolveCallNode.
@TruffleBoundary
public void resolveCallNode(int childOffset) {
final WasmFunction function = ((WasmCallStubNode) children[childOffset]).function();
final CallTarget target = instance().target(function.index());
children[childOffset] = DirectCallNode.create(target);
}
use of org.graalvm.wasm.WasmFunction 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;
}
use of org.graalvm.wasm.WasmFunction in project graal by oracle.
the class WebAssembly method instanceExport.
public static Object instanceExport(WasmInstance instance, String name) {
CompilerAsserts.neverPartOfCompilation();
WasmFunction function = instance.module().exportedFunctions().get(name);
Integer globalIndex = instance.module().exportedGlobals().get(name);
if (function != null) {
return instance.functionInstance(function);
} else if (globalIndex != null) {
final int index = globalIndex;
final int address = instance.globalAddress(index);
if (address < 0) {
return instance.context().globals().externalGlobal(address);
} else {
final ValueType valueType = ValueType.fromByteValue(instance.symbolTable().globalValueType(index));
final boolean mutable = instance.symbolTable().isGlobalMutable(index);
return new ExportedWasmGlobal(valueType, mutable, instance.context().globals(), address);
}
} else if (instance.module().exportedMemoryNames().contains(name)) {
return instance.memory();
} else if (instance.module().exportedTableNames().contains(name)) {
return instance.table();
} else {
throw new WasmJsApiException(WasmJsApiException.Kind.TypeError, name + " is not a exported name of the given instance");
}
}
use of org.graalvm.wasm.WasmFunction in project graal by oracle.
the class BuiltinModule method defineExportedFunction.
protected void defineExportedFunction(WasmInstance instance, String name, byte[] paramType, byte[] retTypes, WasmFunctionInstance functionInstance) {
final int typeIdx = instance.symbolTable().allocateFunctionType(paramType, retTypes);
final WasmFunction function = instance.symbolTable().declareExportedFunction(typeIdx, name);
instance.setFunctionInstance(function.index(), functionInstance);
instance.setTarget(function.index(), functionInstance.target());
}
use of org.graalvm.wasm.WasmFunction in project graal by oracle.
the class BuiltinModule method defineFunction.
protected void defineFunction(WasmInstance instance, String name, byte[] paramTypes, byte[] retTypes, RootNode rootNode) {
// We could check if the same function type had already been allocated,
// but this is just an optimization, and probably not very important,
// since predefined modules have a relatively small size.
final int typeIdx = instance.symbolTable().allocateFunctionType(paramTypes, retTypes);
final WasmFunction function = instance.symbolTable().declareExportedFunction(typeIdx, name);
instance.setTarget(function.index(), rootNode.getCallTarget());
}
Aggregations