Search in sources :

Example 1 with EvaluationResultException

use of org.graalvm.tools.lsp.exceptions.EvaluationResultException in project graal by oracle.

the class SourceCodeEvaluator method runToSectionAndEval.

public EvaluationResult runToSectionAndEval(final TextDocumentSurrogate surrogate, final SourceSection sourceSection, SourceSectionFilter eventFilter, SourceSectionFilter inputFilter) throws DiagnosticsNotification {
    Set<URI> coverageUris = surrogate.getCoverageUris(sourceSection);
    URI runScriptUriFallback = coverageUris == null ? null : coverageUris.stream().findFirst().orElseGet(() -> null);
    TextDocumentSurrogate surrogateOfTestFile = createSurrogateForTestFile(surrogate, runScriptUriFallback);
    final CallTarget callTarget = parse(surrogateOfTestFile);
    final boolean isInputFilterDefined = inputFilter != null;
    EventBinding<ExecutionEventNodeFactory> binding = env.getInstrumenter().attachExecutionEventFactory(eventFilter, inputFilter, new ExecutionEventNodeFactory() {

        StringBuilder indent = new StringBuilder("");

        @Override
        public ExecutionEventNode create(EventContext context) {
            return new ExecutionEventNode() {

                private String sourceSectionFormat(SourceSection section) {
                    return "SourceSection(" + section.getCharacters().toString().replaceAll("\n", Matcher.quoteReplacement("\\n")) + ")";
                }

                @Override
                public void onReturnValue(VirtualFrame frame, Object result) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnReturnValue(result);
                    }
                    if (!isInputFilterDefined) {
                        CompilerDirectives.transferToInterpreter();
                        throw new EvaluationResultException(result);
                    }
                }

                @TruffleBoundary
                private void logOnReturnValue(Object result) {
                    if (indent.length() > 1) {
                        indent.setLength(indent.length() - 2);
                    }
                    logger.log(Level.FINEST, "{0}onReturnValue {1} {2} {3} {4}", new Object[] { indent, context.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()), result });
                }

                @Override
                public void onReturnExceptional(VirtualFrame frame, Throwable exception) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnReturnExceptional();
                    }
                }

                @TruffleBoundary
                private void logOnReturnExceptional() {
                    indent.setLength(indent.length() - 2);
                    logger.log(Level.FINEST, "{0}onReturnExceptional {1}", new Object[] { indent, sourceSectionFormat(context.getInstrumentedSourceSection()) });
                }

                @Override
                public void onEnter(VirtualFrame frame) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnEnter();
                    }
                }

                @TruffleBoundary
                private void logOnEnter() {
                    logger.log(Level.FINEST, "{0}onEnter {1} {2}", new Object[] { indent, context.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()) });
                    indent.append("  ");
                }

                @Override
                public void onInputValue(VirtualFrame frame, EventContext inputContext, int inputIndex, Object inputValue) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnInputValue(inputContext, inputIndex, inputValue);
                    }
                    CompilerDirectives.transferToInterpreter();
                    throw new EvaluationResultException(inputValue);
                }

                @TruffleBoundary
                private void logOnInputValue(EventContext inputContext, int inputIndex, Object inputValue) {
                    indent.setLength(indent.length() - 2);
                    Object view = env.getLanguageView(inputContext.getInstrumentedNode().getRootNode().getLanguageInfo(), inputValue);
                    String metaObject;
                    try {
                        metaObject = INTEROP.hasMetaObject(view) ? INTEROP.asString(INTEROP.toDisplayString(INTEROP.getMetaObject(view))) : null;
                    } catch (UnsupportedMessageException e) {
                        CompilerDirectives.transferToInterpreter();
                        throw new AssertionError(e);
                    }
                    logger.log(Level.FINEST, "{0}onInputValue idx:{1} {2} {3} {4} {5} {6}", new Object[] { indent, inputIndex, inputContext.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()), sourceSectionFormat(inputContext.getInstrumentedSourceSection()), inputValue, metaObject });
                    indent.append("  ");
                }
            };
        }
    });
    try {
        callTarget.call();
    } catch (EvaluationResultException e) {
        return e.isError() ? EvaluationResult.createError(e.getResult()) : EvaluationResult.createResult(e.getResult());
    } catch (RuntimeException e) {
        if (INTEROP.isException(e)) {
            try {
                if (INTEROP.getExceptionType(e) == ExceptionType.EXIT) {
                    return EvaluationResult.createEvaluationSectionNotReached();
                } else {
                    return EvaluationResult.createError(e);
                }
            } catch (UnsupportedMessageException ume) {
                throw CompilerDirectives.shouldNotReachHere(ume);
            }
        }
    } finally {
        binding.dispose();
    }
    return EvaluationResult.createEvaluationSectionNotReached();
}
Also used : ExecutionEventNodeFactory(com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory) CallTarget(com.oracle.truffle.api.CallTarget) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) URI(java.net.URI) EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) EvaluationResultException(org.graalvm.tools.lsp.exceptions.EvaluationResultException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) SourceSection(com.oracle.truffle.api.source.SourceSection) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode)

Aggregations

CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1 EventContext (com.oracle.truffle.api.instrumentation.EventContext)1 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)1 ExecutionEventNodeFactory (com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 URI (java.net.URI)1 EvaluationResultException (org.graalvm.tools.lsp.exceptions.EvaluationResultException)1 TextDocumentSurrogate (org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)1