use of com.oracle.truffle.api.interop.InvalidArrayIndexException 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 com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class HoverRequestHandler method tryFrameScope.
private Hover tryFrameScope(MaterializedFrame frame, CoverageEventNode node, String textAtHoverPosition, LanguageInfo langInfo, SourceSection hoverSection) {
Node instrumentedNode = node.getInstrumentedNode();
NodeLibrary nodeLibrary = NodeLibrary.getUncached(instrumentedNode);
if (nodeLibrary.hasScope(instrumentedNode, frame)) {
try {
Object scope = nodeLibrary.getScope(instrumentedNode, frame, true);
Object keys = INTEROP.getMembers(scope);
long size = INTEROP.getArraySize(keys);
for (long i = 0; i < size; i++) {
String key = INTEROP.asString(INTEROP.readArrayElement(keys, i));
if (key.equals(textAtHoverPosition)) {
Object var = INTEROP.readMember(scope, key);
return Hover.create(createDefaultHoverInfos(textAtHoverPosition, var, langInfo)).setRange(SourceUtils.sourceSectionToRange(hoverSection));
}
}
} catch (UnsupportedMessageException | UnknownIdentifierException | InvalidArrayIndexException e) {
}
}
return null;
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class HostProxy method getMembers.
@ExportMessage
@TruffleBoundary
Object getMembers(@SuppressWarnings("unused") boolean includeInternal, @CachedLibrary("this") InteropLibrary library, @Shared("cache") @Cached(value = "this.context.getGuestToHostCache()", allowUncached = true) GuestToHostCodeCache cache) throws UnsupportedMessageException {
if (proxy instanceof ProxyObject) {
Object result = guestToHostCall(library, cache.memberKeys, context, proxy);
if (result == null) {
result = EMPTY;
}
Object guestValue = context.toGuestValue(library, result);
InteropLibrary interop = InteropLibrary.getFactory().getUncached();
if (!interop.hasArrayElements(guestValue)) {
if (guestValue instanceof HostObject) {
HostObject hostObject = (HostObject) guestValue;
if (hostObject.obj.getClass().isArray() && !hostObject.getHostClassCache().isArrayAccess()) {
throw illegalProxy(context, "getMemberKeys() returned a Java array %s, but allowArrayAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
} else if (hostObject.obj instanceof List && !hostObject.getHostClassCache().isListAccess()) {
throw illegalProxy(context, "getMemberKeys() returned a Java List %s, but allowListAccess in HostAccess is false.", context.asValue(library, guestValue).toString());
}
}
throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
}
// Todo: Use interop to determine an array element type when the GR-5737 is resolved.
for (int i = 0; i < interop.getArraySize(guestValue); i++) {
try {
Object element = interop.readArrayElement(guestValue, i);
if (!interop.isString(element)) {
throw illegalProxy(context, "getMemberKeys() returned invalid value %s but must return an array of member key Strings.", context.asValue(library, guestValue).toString());
}
} catch (UnsupportedOperationException e) {
CompilerDirectives.shouldNotReachHere(e);
} catch (InvalidArrayIndexException e) {
continue;
}
}
return guestValue;
} else {
throw UnsupportedMessageException.create();
}
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class WasmJsApiSuite method testCompile.
@Test
public void testCompile() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmModule module = wasm.moduleDecode(binaryWithExports);
try {
HashMap<String, ModuleExportDescriptor> exports = new HashMap<>();
int i = 0;
while (i < WebAssembly.moduleExports(module).getArraySize()) {
final ModuleExportDescriptor d = (ModuleExportDescriptor) WebAssembly.moduleExports(module).readArrayElement(i);
exports.put(d.name(), d);
i++;
}
Assert.assertEquals("Should export main.", ImportExportKind.function, exports.get("main").kind());
Assert.assertEquals("Should export memory.", ImportExportKind.memory, exports.get("memory").kind());
Assert.assertEquals("Should export global __heap_base.", ImportExportKind.global, exports.get("__heap_base").kind());
Assert.assertEquals("Should export global __data_end.", ImportExportKind.global, exports.get("__data_end").kind());
Assert.assertEquals("Should have empty imports.", 0L, WebAssembly.moduleImports(module).getArraySize());
} catch (InvalidArrayIndexException e) {
throw new RuntimeException(e);
}
});
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException 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);
}
});
}
Aggregations