use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class RelativeSourceInspectDebugTest method testSourcePath.
@Test
public void testSourcePath() throws Exception {
// Using 3 source path elements and 3 sources to verify that
// the correct final source path is built.
final int numSources = 3;
URI[] sourcePathURI = new URI[numSources];
String[] sourceContent = new String[numSources];
String[] relativePath = new String[numSources];
URI[] resolvedURI = new URI[numSources];
String[] hashes = new String[] { "fdfc3c86f176a91df464039fffffffffffffffff", "fdfc3c86f176a91df1786babffffffffffffffff", "fdfc3c86f176a91dee8cd3b7ffffffffffffffff" };
// Create a file source-path
sourceContent[0] = "relative source1\nVarA";
relativePath[0] = "relative/test1.file";
Path testSourcePath1 = Files.createTempDirectory("testPath").toRealPath();
sourcePathURI[0] = testSourcePath1.toUri();
Files.createDirectory(testSourcePath1.resolve("relative"));
Path filePath1 = testSourcePath1.resolve(relativePath[0]);
Files.write(filePath1, sourceContent[0].getBytes());
resolvedURI[0] = filePath1.toUri();
// Create a zip source-path with root matching the zip root
sourceContent[1] = "relative source2\nVarB";
relativePath[1] = "relative/test2.file";
File zip2 = File.createTempFile("TestZip", ".zip");
zip2.deleteOnExit();
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip2))) {
ZipEntry e = new ZipEntry(relativePath[1]);
out.putNextEntry(e);
byte[] data = sourceContent[1].getBytes();
out.write(data, 0, data.length);
out.closeEntry();
}
try (FileSystem fs = FileSystems.newFileSystem(zip2.toPath(), (ClassLoader) null)) {
Path spInZip = fs.getPath("/");
sourcePathURI[1] = spInZip.toUri();
resolvedURI[1] = fs.getPath(relativePath[1]).toUri();
}
// Create a zip source-path with root under the zip root
sourceContent[2] = "relative source3\nVarC";
relativePath[2] = "relative/test3.file";
String folderInZip3 = "src/main";
File zip3 = File.createTempFile("TestZip", ".zip");
zip3.deleteOnExit();
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip3))) {
ZipEntry e = new ZipEntry(folderInZip3 + "/" + relativePath[2]);
out.putNextEntry(e);
byte[] data = sourceContent[2].getBytes();
out.write(data, 0, data.length);
out.closeEntry();
}
try (FileSystem fs = FileSystems.newFileSystem(zip3.toPath(), (ClassLoader) null)) {
Path spInZip = fs.getPath(folderInZip3);
sourcePathURI[2] = spInZip.toUri();
resolvedURI[2] = fs.getPath(folderInZip3, relativePath[2]).toUri();
}
InspectorTester tester = InspectorTester.start(true, false, false, Arrays.asList(sourcePathURI));
tester.sendMessage("{\"id\":1,\"method\":\"Runtime.enable\"}");
assertEquals("{\"result\":{},\"id\":1}", tester.getMessages(true).trim());
tester.sendMessage("{\"id\":2,\"method\":\"Debugger.enable\"}");
assertEquals("{\"result\":{},\"id\":2}", tester.getMessages(true).trim());
tester.sendMessage("{\"id\":3,\"method\":\"Runtime.runIfWaitingForDebugger\"}");
// @formatter:off The default formatting makes unnecessarily big indents and illogical line breaks
// CheckStyle: stop line length check
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":3}\n" + "{\"method\":\"Runtime.executionContextCreated\",\"params\":{\"context\":{\"origin\":\"\",\"name\":\"test\",\"id\":1}}}\n"));
long cid = tester.getContextId();
int cmdId = 1;
int objId = 1;
for (int i = 0; i < numSources; i++) {
TestDebugNoContentLanguage language = new TestDebugNoContentLanguage(relativePath[i], true, true);
ProxyLanguage.setDelegate(language);
Source source = Source.create(ProxyLanguage.ID, sourceContent[i]);
String funcName = source.getCharacters(1).toString();
funcName = funcName.substring(0, funcName.indexOf(' '));
tester.eval(source);
// Suspend at the beginning of the script:
assertTrue(tester.compareReceivedMessages("{\"method\":\"Debugger.scriptParsed\",\"params\":{\"endLine\":1,\"scriptId\":\"" + i + "\",\"endColumn\":4,\"startColumn\":0,\"startLine\":0,\"length\":21,\"executionContextId\":" + cid + ",\"url\":\"" + resolvedURI[i] + "\",\"hash\":\"" + hashes[i] + "\"}}\n" + "{\"method\":\"Debugger.paused\",\"params\":{\"reason\":\"other\",\"hitBreakpoints\":[]," + "\"callFrames\":[{\"callFrameId\":\"0\",\"functionName\":\"" + funcName + "\"," + "\"scopeChain\":[{\"name\":\"" + funcName + "\",\"type\":\"local\",\"object\":{\"description\":\"" + funcName + "\",\"type\":\"object\",\"objectId\":\"" + objId + "\"}}]," + "\"this\":null," + "\"functionLocation\":{\"scriptId\":\"" + i + "\",\"columnNumber\":0,\"lineNumber\":0}," + "\"location\":{\"scriptId\":\"" + i + "\",\"columnNumber\":0,\"lineNumber\":0}," + "\"url\":\"" + resolvedURI[i] + "\"}]}}\n"));
objId++;
tester.sendMessage("{\"id\":" + cmdId + ",\"method\":\"Debugger.getScriptSource\",\"params\":{\"scriptId\":\"" + i + "\"}}");
assertTrue(tester.compareReceivedMessages("{\"result\":{\"scriptSource\":\"" + sourceContent[i].replace("\n", "\\n") + "\"},\"id\":" + cmdId + "}\n"));
cmdId++;
tester.sendMessage("{\"id\":" + cmdId + ",\"method\":\"Debugger.resume\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":" + cmdId + "}\n" + "{\"method\":\"Debugger.resumed\"}\n"));
cmdId++;
tester.sendMessage("{\"id\":" + cmdId + ",\"method\":\"Debugger.pause\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":" + cmdId + "}\n"));
cmdId++;
}
// @formatter:on
// CheckStyle: resume line length check
// Reset the delegate so that we can GC the tested Engine
ProxyLanguage.setDelegate(new ProxyLanguage());
tester.finish();
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class BuggyLanguageInspectDebugTest method testBuggyCalls.
// @formatter:off The default formatting makes unnecessarily big indents and illogical line breaks
// CheckStyle: stop line length check
private void testBuggyCalls(AtomicReference<ProxyLanguage> language, String prefix, boolean haveScope, BugVerifier bugVerifier) throws Exception {
tester = InspectorTester.start(true);
tester.setErr(errorStream);
tester.sendMessage("{\"id\":1,\"method\":\"Runtime.enable\"}");
tester.sendMessage("{\"id\":2,\"method\":\"Debugger.enable\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":1}\n" + "{\"result\":{},\"id\":2}\n"));
tester.sendMessage("{\"id\":3,\"method\":\"Runtime.runIfWaitingForDebugger\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":3}\n" + "{\"method\":\"Runtime.executionContextCreated\",\"params\":{\"context\":{\"origin\":\"\",\"name\":\"test\",\"id\":1}}}\n"));
// Do not keep language reference
ProxyLanguage.setDelegate(language.getAndSet(null));
Source source = Source.newBuilder(ProxyLanguage.ID, prefix + "1", "BuggyCall1.bug").build();
String sourceURI = InspectorTester.getStringURI(source.getURI());
String hash = new Script(0, null, DebuggerTester.getSourceImpl(source)).getHash();
tester.eval(source);
long id = tester.getContextId();
int endLine = source.getLineCount() - 1;
int endColumn = source.getLineLength(1);
assertTrue(tester.compareReceivedMessages("{\"method\":\"Debugger.scriptParsed\",\"params\":{\"endLine\":" + endLine + ",\"scriptId\":\"0\",\"endColumn\":" + endColumn + ",\"startColumn\":0,\"startLine\":0,\"length\":" + source.getLength() + ",\"executionContextId\":" + id + ",\"url\":\"" + sourceURI + "\",\"hash\":\"" + hash + "\"}}\n"));
skipConsoleMessages(tester);
assertPaused(prefix + "1", haveScope, 1, sourceURI, 0);
bugVerifier.verifyMessages(tester, 1);
tester.sendMessage("{\"id\":100,\"method\":\"Debugger.resume\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":100}\n" + "{\"method\":\"Debugger.resumed\"}\n"));
tester.sendMessage("{\"id\":8,\"method\":\"Debugger.pause\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":8}\n"));
source = Source.newBuilder(ProxyLanguage.ID, prefix + "2", "BuggyCall2.bug").build();
sourceURI = InspectorTester.getStringURI(source.getURI());
hash = new Script(0, null, DebuggerTester.getSourceImpl(source)).getHash();
tester.eval(source);
assertTrue(tester.compareReceivedMessages("{\"method\":\"Debugger.scriptParsed\",\"params\":{\"endLine\":" + endLine + ",\"scriptId\":\"1\",\"endColumn\":" + endColumn + ",\"startColumn\":0,\"startLine\":0,\"length\":" + source.getLength() + ",\"executionContextId\":" + id + ",\"url\":\"" + sourceURI + "\",\"hash\":\"" + hash + "\"}}\n"));
skipConsoleMessages(tester);
assertPaused(prefix + "2", haveScope, haveScope ? 3 : 2, sourceURI, 1);
bugVerifier.verifyMessages(tester, 2);
tester.sendMessage("{\"id\":100,\"method\":\"Debugger.resume\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":100}\n" + "{\"method\":\"Debugger.resumed\"}\n"));
tester.sendMessage("{\"id\":11,\"method\":\"Debugger.pause\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":11}\n"));
source = Source.newBuilder(ProxyLanguage.ID, prefix + "3", "BuggyCall3.bug").build();
sourceURI = InspectorTester.getStringURI(source.getURI());
hash = new Script(0, null, DebuggerTester.getSourceImpl(source)).getHash();
tester.eval(source);
assertTrue(tester.compareReceivedMessages("{\"method\":\"Debugger.scriptParsed\",\"params\":{\"endLine\":" + endLine + ",\"scriptId\":\"2\",\"endColumn\":" + endColumn + ",\"startColumn\":0,\"startLine\":0,\"length\":" + source.getLength() + ",\"executionContextId\":" + id + ",\"url\":\"" + sourceURI + "\",\"hash\":\"" + hash + "\"}}\n"));
skipConsoleMessages(tester);
assertPaused(prefix + "3", haveScope, haveScope ? 5 : 3, sourceURI, 2);
bugVerifier.verifyMessages(tester, 3);
tester.sendMessage("{\"id\":100,\"method\":\"Debugger.resume\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":100}\n" + "{\"method\":\"Debugger.resumed\"}\n"));
// Reset the delegate so that we can GC the tested Engine
ProxyLanguage.setDelegate(new ProxyLanguage());
tester.finish();
assertTrue(errorStream.size() > 0);
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class InspectorReceiverTest method checkMultipleReceivers.
private static void checkMultipleReceivers(String receiver, String sourceHash) throws Exception {
ProxyLanguage.setDelegate(new TestReceiverLanguage());
Source source = Source.create(ProxyLanguage.ID, receiver + "\n" + "a 1 b 2 " + receiver + " 3\n" + "c 4 d 5\n" + "a 6 b 7 " + receiver + " 8");
String sourceURI = InspectorTester.getStringURI(source.getURI());
InspectorTester tester = InspectorTester.start(true);
tester.sendMessage("{\"id\":1,\"method\":\"Runtime.enable\"}");
assertEquals("{\"result\":{},\"id\":1}", tester.getMessages(true).trim());
tester.sendMessage("{\"id\":2,\"method\":\"Debugger.enable\"}");
assertEquals("{\"result\":{},\"id\":2}", tester.getMessages(true).trim());
tester.sendMessage("{\"id\":3,\"method\":\"Runtime.runIfWaitingForDebugger\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":3}\n" + "{\"method\":\"Runtime.executionContextCreated\",\"params\":{\"context\":{\"origin\":\"\",\"name\":\"test\",\"id\":1}}}\n"));
tester.eval(source);
int endColumn = source.getLength() - source.getCharacters().toString().lastIndexOf('\n') - 1;
long id = tester.getContextId();
assertTrue(tester.compareReceivedMessages("{\"method\":\"Debugger.scriptParsed\",\"params\":{\"endLine\":3,\"scriptId\":\"0\",\"endColumn\":" + endColumn + ",\"startColumn\":0,\"startLine\":0,\"length\":" + source.getLength() + "," + "\"executionContextId\":" + id + ",\"url\":\"" + sourceURI + "\",\"hash\":\"" + sourceHash + "\"}}\n" + "{\"method\":\"Debugger.paused\",\"params\":{\"reason\":\"other\",\"hitBreakpoints\":[]," + "\"callFrames\":[{\"callFrameId\":\"0\",\"functionName\":\"\"," + "\"scopeChain\":[{\"name\":\"0\",\"type\":\"block\",\"object\":{\"description\":\"0\",\"type\":\"object\",\"objectId\":\"1\"}}," + "{\"name\":\"1\",\"type\":\"block\",\"object\":{\"description\":\"1\",\"type\":\"object\",\"objectId\":\"2\"}}," + "{\"name\":\"2\",\"type\":\"block\",\"object\":{\"description\":\"2\",\"type\":\"object\",\"objectId\":\"3\"}}]," + "\"this\":null," + "\"location\":{\"scriptId\":\"0\",\"columnNumber\":0,\"lineNumber\":0}," + "\"url\":\"" + sourceURI + "\"}]}}\n"));
// Scope's variables contain "this":
tester.sendMessage("{\"id\":4,\"method\":\"Runtime.getProperties\",\"params\":{\"objectId\":\"1\"}}");
assertTrue(tester.compareReceivedMessages("{\"result\":{\"result\":[{\"isOwn\":true,\"enumerable\":true,\"name\":\"" + receiver + "\",\"value\":{\"description\":\"3\",\"type\":\"object\",\"value\":\"3\"},\"configurable\":true,\"writable\":false}," + "{\"isOwn\":true,\"enumerable\":true,\"name\":\"a\",\"value\":{\"description\":\"1\",\"type\":\"object\",\"value\":\"1\"},\"configurable\":true,\"writable\":false}," + "{\"isOwn\":true,\"enumerable\":true,\"name\":\"b\",\"value\":{\"description\":\"2\",\"type\":\"object\",\"value\":\"2\"},\"configurable\":true,\"writable\":false}],\"internalProperties\":[]},\"id\":4}\n"));
tester.sendMessage("{\"id\":5,\"method\":\"Runtime.getProperties\",\"params\":{\"objectId\":\"2\"}}");
assertTrue(tester.compareReceivedMessages("{\"result\":{\"result\":[{\"isOwn\":true,\"enumerable\":true,\"name\":\"c\",\"value\":{\"description\":\"4\",\"type\":\"object\",\"value\":\"4\"},\"configurable\":true,\"writable\":false}," + "{\"isOwn\":true,\"enumerable\":true,\"name\":\"d\",\"value\":{\"description\":\"5\",\"type\":\"object\",\"value\":\"5\"},\"configurable\":true,\"writable\":false}],\"internalProperties\":[]},\"id\":5}\n"));
tester.sendMessage("{\"id\":6,\"method\":\"Runtime.getProperties\",\"params\":{\"objectId\":\"3\"}}");
assertTrue(tester.compareReceivedMessages("{\"result\":{\"result\":[{\"isOwn\":true,\"enumerable\":true,\"name\":\"" + receiver + "\",\"value\":{\"description\":\"8\",\"type\":\"object\",\"value\":\"8\"},\"configurable\":true,\"writable\":false}," + "{\"isOwn\":true,\"enumerable\":true,\"name\":\"a\",\"value\":{\"description\":\"6\",\"type\":\"object\",\"value\":\"6\"},\"configurable\":true,\"writable\":false}," + "{\"isOwn\":true,\"enumerable\":true,\"name\":\"b\",\"value\":{\"description\":\"7\",\"type\":\"object\",\"value\":\"7\"},\"configurable\":true,\"writable\":false}],\"internalProperties\":[]},\"id\":6}\n"));
tester.sendMessage("{\"id\":10,\"method\":\"Debugger.resume\"}");
assertTrue(tester.compareReceivedMessages("{\"result\":{},\"id\":10}\n" + "{\"method\":\"Debugger.resumed\"}\n"));
// Reset the delegate so that we can GC the tested Engine
ProxyLanguage.setDelegate(new ProxyLanguage());
tester.finish();
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class LegacyTruffleExceptionTest method testLegacyException.
@Test
public void testLegacyException() {
setupEnv(createContext(verifyingHandler), new ProxyLanguage() {
@Override
protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
return createAST(LegacyTruffleExceptionTest.class, languageInstance, (n) -> {
LegacyCatchableException exception = new LegacyCatchableException("Test exception", n);
LangObject exceptionObject = new LangObject(exception);
exception.setExceptionObject(exceptionObject);
return exceptionObject;
}, false);
}
});
verifyingHandler.expect(BlockNode.Kind.TRY, BlockNode.Kind.CATCH, BlockNode.Kind.FINALLY);
context.eval(ProxyLanguage.ID, "Test");
}
use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.
the class IteratorTest method testImpl.
private void testImpl(Function<Object[], Object> factory) {
String[] items = { "one", "two", "three", "four" };
setupEnv(createContext(verifyingHandler), new ProxyLanguage() {
@Override
protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
return createAST(languageInstance, factory.apply(items));
}
});
verifyingHandler.expect(items);
context.eval(ProxyLanguage.ID, "Test");
}
Aggregations