use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class UnwindInstrumentationReenterSnippets method isValidVarsNodeObject.
private static void isValidVarsNodeObject(Object obj, String varNameProperty) {
InteropLibrary interop = InteropLibrary.getFactory().getUncached(obj);
if (!interop.isMemberReadable(obj, varNameProperty)) {
throw new AssertionError("Invalid node object " + obj + ", does not have " + varNameProperty + " member.");
}
Object varName;
try {
varName = interop.readMember(obj, varNameProperty);
} catch (UnsupportedMessageException | UnknownIdentifierException ex) {
throw new AssertionError("Invalid node object " + obj + ", can not read " + varNameProperty + " member.", ex);
}
if (varName instanceof String) {
return;
}
interop = InteropLibrary.getFactory().getUncached(varName);
if (interop.hasArrayElements(varName)) {
long size;
try {
size = interop.getArraySize(varName);
} catch (UnsupportedMessageException e) {
throw new AssertionError("Invalid node object: the returned variable name object must have a size when it's an array.");
}
for (long i = 0; i < size; i++) {
Object var;
try {
var = interop.readArrayElement(varName, i);
} catch (InvalidArrayIndexException | UnsupportedMessageException e) {
throw new AssertionError("Invalid node object: the returned variable name object must be readable at number index " + i);
}
isValidVarObject(var);
}
} else {
isValidVarObject(varName);
}
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class UnwindInstrumentationReenterSnippets method isValidNodeObject.
private boolean isValidNodeObject(Object obj) {
CompilerAsserts.neverPartOfCompilation();
InteropLibrary interop = InteropLibrary.getFactory().getUncached(obj);
if (!interop.hasMembers(obj)) {
throw new AssertionError("Invalid node object: must return true for the hasMembers message.");
}
Object members;
try {
members = interop.getMembers(obj);
} catch (UnsupportedMessageException e) {
throw new AssertionError("Invalid node object: must support the getMembers message.", e);
}
InteropLibrary membersInterop = InteropLibrary.getFactory().getUncached(members);
if (!membersInterop.hasArrayElements(members)) {
throw new AssertionError("Invalid node object: the returned members object must support hasArrayElements.");
}
long size;
try {
size = membersInterop.getArraySize(members);
} catch (UnsupportedMessageException e) {
throw new AssertionError("Invalid node object: the returned members object must have a size.");
}
for (long i = 0; i < size; i++) {
Object key;
try {
key = membersInterop.readArrayElement(members, i);
} catch (InvalidArrayIndexException | UnsupportedMessageException e) {
throw new AssertionError("Invalid node object: the returned members object must be readable at number index " + i);
}
InteropLibrary keyInterop = InteropLibrary.getFactory().getUncached(key);
if (!keyInterop.isString(key)) {
throw new AssertionError("Invalid node object: the returned member must return a string at index " + i + ". But was " + key.getClass().getName() + ".");
}
String member;
try {
member = keyInterop.asString(key);
} catch (UnsupportedMessageException e1) {
throw new AssertionError("Invalid node object: the returned member must return a string ");
}
try {
interop.readMember(obj, member);
} catch (UnknownIdentifierException | UnsupportedMessageException e) {
throw new AssertionError("Invalid node object: the returned member must be readable with identifier " + member);
}
if (interop.isMemberWritable(obj, member)) {
throw new AssertionError("Invalid node object: The member " + member + " is marked as writable but node objects must not be writable.");
}
}
if (interop.hasArrayElements(obj)) {
throw new AssertionError("Invalid node object: the node object must not return true for hasArrayElements.");
}
return isValidTaggedNodeObject(obj);
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class WasmJsApiSuite method testImportOrder.
@Test
public void testImportOrder() throws IOException {
runTest(context -> {
final WebAssembly wasm = new WebAssembly(context);
final WasmModule module = wasm.moduleDecode(binaryWithMixedImports);
final Sequence<ModuleImportDescriptor> moduleImports = WebAssembly.moduleImports(module);
String[] expected = new String[] { "f1", "g1", "t", "m", "g2", "f2" };
try {
Assert.assertEquals("Must import all members.", 6L, moduleImports.getArraySize());
for (int i = 0; i < moduleImports.getArraySize(); i++) {
Assert.assertEquals("Module member " + i + " should correspond to the expected import.", expected[i], ((ModuleImportDescriptor) moduleImports.readArrayElement(i)).name());
}
} catch (InvalidArrayIndexException e) {
throw new RuntimeException(e);
}
});
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class HeapGenerator method readMember.
private static <T> T readMember(InteropLibrary iop, Object obj, String member, Convert<T> convert) {
String errMsg;
try {
Object value = iop.readMember(obj, member);
if (!iop.isNull(value)) {
return convert.convert(value);
}
errMsg = "'" + member + "' should be defined";
} catch (UnsupportedTypeException | UnsupportedMessageException | UnknownIdentifierException ex) {
errMsg = "Cannot find '" + member + "'";
}
StringBuilder sb = new StringBuilder(errMsg);
try {
Object members = iop.getMembers(obj);
long count = iop.getArraySize(members);
sb.append(" among [");
String sep = "";
for (long i = 0; i < count; i++) {
sb.append(sep);
try {
sb.append(iop.readArrayElement(members, i));
} catch (InvalidArrayIndexException ex) {
// ignore
}
sep = ", ";
}
sb.append("]");
} catch (UnsupportedMessageException cannotDumpMembers) {
sb.append(" in ").append(iop.toDisplayString(obj));
}
throw new HeapException(sb.toString());
}
use of com.oracle.truffle.api.interop.InvalidArrayIndexException in project graal by oracle.
the class InsightFilter method create.
static InsightFilter.Data create(AgentType at, Object[] arr) throws IllegalArgumentException, UnsupportedMessageException {
List<Class<? extends Tag>> allTags = new ArrayList<>();
String rootNameRegExp = null;
Object rootNameFn = null;
Object sourceFilterFn = null;
if (arr != null && arr.length > 2) {
Object config = arr[2];
final InteropLibrary iop = InteropLibrary.getFactory().getUncached();
Map<String, Object> map = new LinkedHashMap<>();
if (iop.hasHashEntries(config)) {
Object it = iop.getHashEntriesIterator(config);
while (iop.hasIteratorNextElement(it)) {
try {
Object keyAndValue = iop.getIteratorNextElement(it);
Object key;
Object value;
try {
key = iop.readArrayElement(keyAndValue, 0);
value = iop.readArrayElement(keyAndValue, 1);
} catch (InvalidArrayIndexException ex) {
throw CompilerDirectives.shouldNotReachHere(ex);
}
String type = iop.asString(key);
map.put(type, value);
} catch (StopIterationException ex) {
break;
}
}
} else {
Object allMembers = iop.getMembers(config, false);
long allMembersSize = iop.getArraySize(allMembers);
for (int i = 0; i < allMembersSize; i++) {
Object atI;
try {
atI = iop.readArrayElement(allMembers, i);
} catch (InvalidArrayIndexException ex) {
continue;
}
String type = iop.asString(atI);
Object value;
try {
value = iop.readMember(config, type);
} catch (UnknownIdentifierException ex) {
continue;
}
map.put(type, value);
}
}
for (Map.Entry<String, Object> entry : map.entrySet()) {
String type = entry.getKey();
switch(type) {
case "expressions":
if (isSet(iop, map, "expressions")) {
allTags.add(StandardTags.ExpressionTag.class);
}
break;
case "statements":
if (isSet(iop, map, "statements")) {
allTags.add(StandardTags.StatementTag.class);
}
break;
case "roots":
if (isSet(iop, map, "roots")) {
allTags.add(StandardTags.RootBodyTag.class);
}
break;
case "rootNameFilter":
{
Object fn = map.get("rootNameFilter");
if (fn != null && !iop.isNull(fn)) {
if (iop.isString(fn)) {
rootNameRegExp = iop.asString(fn);
} else {
if (!iop.isExecutable(fn)) {
throw new IllegalArgumentException("rootNameFilter should be a string, a regular expression!");
}
rootNameFn = fn;
}
}
break;
}
case "sourceFilter":
{
Object fn = map.get("sourceFilter");
if (fn != null && !iop.isNull(fn)) {
if (!iop.isExecutable(fn)) {
throw new IllegalArgumentException("sourceFilter has to be a function!");
}
sourceFilterFn = fn;
}
break;
}
default:
throw InsightException.unknownAttribute(type);
}
}
}
if (allTags.isEmpty()) {
throw new IllegalArgumentException("No elements specified to listen to for execution listener. Need to specify at least one element kind: expressions, statements or roots.");
}
allTags.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
InsightFilter filter = new InsightFilter(allTags, rootNameRegExp, rootNameFn != null, sourceFilterFn != null);
return new Data(at, filter, arr[1], rootNameFn, sourceFilterFn);
}
Aggregations