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();
}
Aggregations