Search in sources :

Example 1 with StopIterationException

use of com.oracle.truffle.api.interop.StopIterationException 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);
}
Also used : StopIterationException(com.oracle.truffle.api.interop.StopIterationException) ArrayList(java.util.ArrayList) StandardTags(com.oracle.truffle.api.instrumentation.StandardTags) LinkedHashMap(java.util.LinkedHashMap) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) Tag(com.oracle.truffle.api.instrumentation.Tag) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 2 with StopIterationException

use of com.oracle.truffle.api.interop.StopIterationException in project graal by oracle.

the class DebugValue method getIteratorNextElement.

/**
 * Returns the next element in the iteration. When the underlying data structure is modified the
 * {@link #getIteratorNextElement()} may throw the {@link NoSuchElementException} despite the
 * {@link #hasIteratorNextElement()} returned {@code true}.
 *
 * @throws DebugException if {@link #isIterator()} returns {@code false} or when the underlying
 *             iterator element exists but is not readable, or guest language code throws an
 *             exception.
 * @throws NoSuchElementException if the iteration has no more elements.
 *
 * @see #isIterator()
 * @see #hasIteratorNextElement()
 * @see InteropLibrary#getIteratorNextElement(Object)
 * @since 21.2
 */
public DebugValue getIteratorNextElement() throws NoSuchElementException, DebugException {
    Object value = get();
    Object next;
    try {
        next = INTEROP.getIteratorNextElement(value);
    } catch (ThreadDeath td) {
        throw td;
    } catch (StopIterationException ex) {
        throw new NoSuchElementException(ex.getLocalizedMessage());
    } catch (Throwable ex) {
        throw DebugException.create(getSession(), ex, resolveLanguage(), null, true, null);
    }
    return new DebugValue.HeapValue(getSession(), preferredLanguage, null, next);
}
Also used : StopIterationException(com.oracle.truffle.api.interop.StopIterationException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

StopIterationException (com.oracle.truffle.api.interop.StopIterationException)2 StandardTags (com.oracle.truffle.api.instrumentation.StandardTags)1 Tag (com.oracle.truffle.api.instrumentation.Tag)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 InvalidArrayIndexException (com.oracle.truffle.api.interop.InvalidArrayIndexException)1 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1