use of org.graalvm.polyglot.Source in project graal by oracle.
the class DebuggerThreadsTest method testSingleThread.
@Test
public void testSingleThread() throws Throwable {
final Source source = testSource("STATEMENT()");
TestThreadsListener threadsListener = new TestThreadsListener();
List<ThreadEvent> events = threadsListener.events;
try (DebuggerSession session = startSession()) {
session.suspendNextExecution();
session.setThreadsListener(threadsListener, false);
startEval(source);
expectSuspended((SuspendedEvent event) -> {
assertEquals(1, events.size());
assertTrue(events.get(0).isNew);
assertEquals(Thread.currentThread(), events.get(0).thread);
assertNotNull(events.get(0).context);
});
expectDone();
closeEngine();
// We're closing the engine not on the execution thread - we get two more thread events.
assertEquals(4, events.size());
assertFalse(events.get(3).isNew);
assertEquals(events.get(0).context, events.get(3).context);
}
events.clear();
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class DoubleHaltTest method testCallLoopStepInto.
@Test
public void testCallLoopStepInto() throws Throwable {
Source testSource = testSource("ROOT(\n" + " DEFINE(foo,\n" + " LOOP(3,\n" + " STATEMENT)\n" + " ),\n" + " CALL(foo)\n" + ")\n");
try (DebuggerSession session = startSession()) {
Breakpoint breakpoint4 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(4).build());
session.suspendNextExecution();
startEval(testSource);
for (int i = 0; i < 3; i++) {
final int modI = i % 3;
expectSuspended((SuspendedEvent event) -> {
SuspendedEvent e = checkState(event, 4, true, "STATEMENT");
assertEquals(1, e.getBreakpoints().size());
assertSame(breakpoint4, e.getBreakpoints().iterator().next());
switch(modI) {
case 0:
/*
* Note Chumer: breakpoints should always hit independent if we are
* currently stepping out or not. thats why step out does not step out
* here.
*/
e.prepareStepOut(1);
break;
case 1:
e.prepareStepInto(1);
break;
case 2:
e.prepareStepOver(1);
break;
}
});
}
expectSuspended((SuspendedEvent event) -> {
checkState(event, 6, false, "CALL(foo)");
});
expectDone();
}
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class DoubleHaltTest method testBreakpointStepping.
@Test
public void testBreakpointStepping() throws Throwable {
Source testSource = testSource("ROOT(\n" + " STATEMENT,\n" + " STATEMENT,\n" + " STATEMENT,\n" + " STATEMENT,\n" + " STATEMENT,\n" + " STATEMENT,\n" + " STATEMENT,\n" + " STATEMENT\n" + ")\n");
try (DebuggerSession session = startSession()) {
Breakpoint breakpoint2 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(2).build());
Breakpoint breakpoint3 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(3).build());
Breakpoint breakpoint5 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(5).build());
Breakpoint breakpoint6 = session.install(Breakpoint.newBuilder(getSourceImpl(testSource)).lineIs(6).build());
session.suspendNextExecution();
startEval(testSource);
expectSuspended((SuspendedEvent event) -> {
checkState(event, 2, true, "STATEMENT");
assertEquals(1, event.getBreakpoints().size());
assertSame(breakpoint2, event.getBreakpoints().iterator().next());
event.prepareStepInto(1);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 3, true, "STATEMENT");
assertEquals(1, event.getBreakpoints().size());
assertSame(breakpoint3, event.getBreakpoints().iterator().next());
event.prepareStepOver(2);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 5, true, "STATEMENT");
assertEquals(1, event.getBreakpoints().size());
assertSame(breakpoint5, event.getBreakpoints().iterator().next());
event.prepareStepInto(2);
});
expectSuspended((SuspendedEvent event) -> {
checkState(event, 6, true, "STATEMENT");
assertEquals(1, event.getBreakpoints().size());
assertSame(breakpoint6, event.getBreakpoints().iterator().next());
event.prepareContinue();
});
expectDone();
assertEquals(1, breakpoint2.getHitCount());
assertEquals(1, breakpoint3.getHitCount());
assertEquals(1, breakpoint5.getHitCount());
assertEquals(1, breakpoint6.getHitCount());
}
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class SourceAPITest method testCharSequenceNotMaterialized.
@Test
public void testCharSequenceNotMaterialized() throws IOException {
AtomicBoolean materialized = new AtomicBoolean(false);
final CharSequence testString = "testString";
Source source = Source.newBuilder(SourceAPITestLanguage.ID, new CharSequence() {
public CharSequence subSequence(int start, int end) {
return testString.subSequence(start, end);
}
public int length() {
return testString.length();
}
public char charAt(int index) {
return testString.charAt(index);
}
@Override
public String toString() {
materialized.set(true);
throw new AssertionError("Should not materialize CharSequence.");
}
}, "testsource").buildLiteral();
Context context = Context.create(SourceAPITestLanguage.ID);
context.eval(source);
assertEquals(1, source.getLineCount());
assertTrue(equalsCharSequence(testString, source.getCharacters()));
assertTrue(equalsCharSequence(testString, source.getCharacters(1)));
assertEquals(0, source.getLineStartOffset(1));
assertNull(source.getURL());
assertNotNull(source.getName());
assertNull(source.getPath());
assertEquals(6, source.getColumnNumber(5));
assertEquals(SourceAPITestLanguage.ID, source.getLanguage());
assertEquals(testString.length(), source.getLength());
assertFalse(source.isInteractive());
assertFalse(source.isInternal());
// consume reader CharSequence should not be materialized
CharBuffer charBuffer = CharBuffer.allocate(source.getLength());
Reader reader = source.getReader();
reader.read(charBuffer);
charBuffer.position(0);
assertEquals(testString, charBuffer.toString());
assertFalse(materialized.get());
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class MultiLanguageShell method readEvalPrint.
public int readEvalPrint() throws IOException {
ConsoleReader console = new ConsoleReader(in, out);
console.setHandleUserInterrupt(true);
console.setExpandEvents(false);
console.setCopyPasteDetection(true);
console.println("GraalVM MultiLanguage Shell " + context.getEngine().getVersion());
console.println("Copyright (c) 2013-2018, Oracle and/or its affiliates");
List<Language> languages = new ArrayList<>();
Set<Language> uniqueValues = new HashSet<>();
for (Language language : context.getEngine().getLanguages().values()) {
if (language.isInteractive()) {
if (uniqueValues.add(language)) {
languages.add(language);
}
}
}
languages.sort(Comparator.comparing(Language::getName));
Map<String, Language> prompts = new HashMap<>();
StringBuilder promptsString = new StringBuilder();
for (Language language : languages) {
String prompt = createPrompt(language).trim();
promptsString.append(prompt).append(" ");
prompts.put(prompt, language);
console.println(" " + language.getName() + " version " + language.getVersion());
}
if (languages.isEmpty()) {
throw new Launcher.AbortException("Error: No Graal languages installed. Exiting shell.", 1);
}
printUsage(console, promptsString, false);
int maxNameLength = 0;
for (Language language : languages) {
maxNameLength = Math.max(maxNameLength, language.getName().length());
}
String startLanguage = defaultStartLanguage;
if (startLanguage == null) {
startLanguage = languages.get(0).getId();
}
Language currentLanguage = context.getEngine().getLanguages().get(startLanguage);
if (currentLanguage == null) {
throw new Launcher.AbortException("Error: could not find language '" + startLanguage + "'", 1);
}
assert languages.indexOf(currentLanguage) >= 0;
Source bufferSource = null;
String id = currentLanguage.getId();
// console.println("initialize time: " + (System.currentTimeMillis() - start));
String prompt = createPrompt(currentLanguage);
console.getKeys().bind(String.valueOf((char) 12), new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new ChangeLanguageException(null);
}
});
console.getKeys().bind(String.valueOf((char) 10), new ActionListener() {
public void actionPerformed(ActionEvent e) {
throw new RuntimeIncompleteSourceException();
}
});
// initializes the language
context.initialize(currentLanguage.getId());
boolean verboseErrors = false;
for (; ; ) {
String input = null;
Source source = null;
try {
input = console.readLine(bufferSource == null ? prompt : createBufferPrompt(prompt));
if (input == null) {
break;
} else if (input.trim().equals("")) {
continue;
}
Language switchedLanguage = null;
String trimmedInput = input.trim();
if (trimmedInput.equals("-usage")) {
printUsage(console, promptsString, true);
input = "";
} else if (trimmedInput.equals("-verboseErrors")) {
verboseErrors = !verboseErrors;
if (verboseErrors) {
console.println("Verbose errors is now on.");
} else {
console.println("Verbose errors is now off.");
}
input = "";
} else if (prompts.containsKey(trimmedInput)) {
switchedLanguage = prompts.get(input);
input = "";
}
NonBlockingInputStream nonBlockIn = ((NonBlockingInputStream) console.getInput());
while (nonBlockIn.isNonBlockingEnabled() && nonBlockIn.peek(10) != -2 && switchedLanguage == null) {
String line = console.readLine(createBufferPrompt(prompt));
String trimmedLine = line.trim();
if (prompts.containsKey(trimmedLine)) {
switchedLanguage = prompts.get(trimmedLine);
break;
} else {
input += "\n" + line;
}
}
if (!input.trim().equals("")) {
source = Source.newBuilder(currentLanguage.getId(), input, "<shell>").interactive(true).build();
context.eval(source);
bufferSource = null;
console.getHistory().replace(source.getCharacters());
}
if (switchedLanguage != null) {
throw new ChangeLanguageException(switchedLanguage);
}
} catch (UserInterruptException | EOFException e) {
// interrupted by ctrl-c
break;
} catch (ChangeLanguageException e) {
bufferSource = null;
histories.put(currentLanguage, console.getHistory());
currentLanguage = e.getLanguage() == null ? languages.get((languages.indexOf(currentLanguage) + 1) % languages.size()) : e.getLanguage();
History history = histories.computeIfAbsent(currentLanguage, k -> new MemoryHistory());
console.setHistory(history);
id = currentLanguage.getId();
prompt = createPrompt(currentLanguage);
console.resetPromptLine("", "", 0);
context.initialize(id);
} catch (ThreadDeath e) {
console.println("Execution killed!");
continue;
} catch (RuntimeIncompleteSourceException e) {
console.println();
input += "\n";
bufferSource = source;
} catch (PolyglotException e) {
input += "\n";
bufferSource = source;
if (e.isExit()) {
return e.getExitStatus();
} else if (e.isIncompleteSource()) {
input += "\n";
bufferSource = source;
} else if (!e.isInternalError()) {
if (e.getMessage() != null && e.getMessage().isEmpty()) {
console.println(e.toString());
} else {
if (verboseErrors) {
e.printStackTrace(new PrintWriter(console.getOutput()));
}
}
} else {
e.printStackTrace(new PrintWriter(console.getOutput()));
}
} catch (Throwable e) {
e.printStackTrace(new PrintWriter(console.getOutput()));
}
}
return 0;
}
Aggregations