use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SourceSectionFilterTest method testIndexIn.
@Test
public void testIndexIn() {
Source sampleSource = Source.newBuilder("line1\nline2\nline3\nline4").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
Node root = createNode(sampleSource.createSection(0, 23));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(0, 0).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(0, 1).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(0, 4), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(0, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(4, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(4, 6), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(10, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(9, 1), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createSection(9, 5), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(5, 5).build(), root, createNode(sampleSource.createUnavailableSection())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(IndexRange.between(0, 5)).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().indexIn(IndexRange.between(0, 5), IndexRange.between(5, 6)).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().indexIn(IndexRange.between(0, 5), IndexRange.between(11, 12)).build(), root, createNode(sampleSource.createSection(5, 5), tags())));
Assert.assertNotNull(SourceSectionFilter.newBuilder().indexIn(5, 5).build().toString());
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class SourceSectionFilterTest method testSourceSectionEquals.
@Test
public void testSourceSectionEquals() {
Source sampleSource1 = Source.newBuilder("line1\nline2\nline3\nline4").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
Source sampleSource2 = Source.newBuilder("line1\nline2\nline3\nline4").name("unknown").mimeType(InstrumentationTestLanguage.MIME_TYPE).build();
Node root1 = createNode(sampleSource1.createSection(0, 23));
Node root2 = createNode(sampleSource2.createSection(0, 23));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 6)).build(), root1, createNode(sampleSource1.createSection(1, 6), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 6)).build(), root2, createNode(sampleSource2.createSection(1, 6), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 7)).build(), root1, createNode(sampleSource1.createSection(1, 6), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6)).build(), root1, createNode(sampleSource1.createSection(1, 6), tags())));
Assert.assertTrue(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6), sampleSource1.createSection(2, 7)).build(), root1, createNode(sampleSource1.createSection(2, 7), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6), sampleSource1.createSection(2, 7)).build(), root1, createNode(sampleSource1.createSection(2, 8), tags())));
Assert.assertFalse(isInstrumented(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(2, 6), sampleSource1.createSection(2, 7)).build(), null, createNode(sampleSource1.createUnavailableSection())));
Assert.assertNotNull(SourceSectionFilter.newBuilder().sourceSectionEquals(sampleSource1.createSection(1, 6)).build().toString());
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class InputFilterTest method assertCleanedUp.
private void assertCleanedUp(String code) {
// first we capture all root nodes used by the code.
Set<RootNode> rootNodes = new HashSet<>();
EventBinding<?> binding = instrumenter.attachExecutionEventListener(SourceSectionFilter.ANY, new ExecutionEventListener() {
public void onEnter(EventContext c, VirtualFrame frame) {
addRoot(c);
}
@TruffleBoundary
private void addRoot(EventContext c) {
rootNodes.add(c.getInstrumentedNode().getRootNode());
}
public void onReturnValue(EventContext c, VirtualFrame frame, Object result) {
}
public void onReturnExceptional(EventContext c, VirtualFrame frame, Throwable exception) {
}
});
execute(code);
binding.dispose();
// we execute again to let the instrumentation wrappers be cleaned up
execute(code);
for (RootNode root : rootNodes) {
// all frame slots got removed
assertEquals(new HashSet<>(), root.getFrameDescriptor().getIdentifiers());
// no wrappers left
root.accept(new NodeVisitor() {
public boolean visit(Node node) {
if (node instanceof WrapperNode) {
throw new AssertionError();
}
return true;
}
});
}
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class InstrumentableNodeTest method assertProperties.
private static void assertProperties(Object receiver, Object... properties) {
try {
assertTrue(receiver instanceof TruffleObject);
TruffleObject obj = (TruffleObject) receiver;
Node hasKeysNode = Message.HAS_KEYS.createNode();
Node keysNode = Message.KEYS.createNode();
assertTrue(ForeignAccess.sendHasKeys(hasKeysNode, obj));
TruffleObject keys = ForeignAccess.sendKeys(keysNode, obj);
for (int i = 0; i < properties.length; i = i + 2) {
String expectedKey = (String) properties[i];
Object expectedValue = properties[i + 1];
Node readNode = Message.READ.createNode();
Object key = ForeignAccess.sendRead(readNode, keys, i / 2);
assertEquals(expectedKey, key);
assertEquals(expectedValue, ForeignAccess.sendRead(readNode, obj, key));
}
} catch (InteropException e) {
throw e.raise();
}
}
use of com.oracle.truffle.api.nodes.Node in project graal by oracle.
the class DefaultNearestNodeSearch method findNearestNodeAt.
/**
* We have a source offset (the character index) and a context node given. The offset is in
* node's source. The context node is an instrumentable node, but may not have any particular
* tags. We're searching for an instrumentable node tagged with at least one of the given tags,
* which is nearest to the given offset according to the guest language execution flow. We find
* the nearest tagged {@link Node node} in following steps:
* <ul>
* <li>When the offset is inside the context node, we search for the first tagged parent node
* and call {@link #findChildTaggedNode(Node, int, Set, boolean)} to find a tagged child. If we
* find it, we return it, if not, we return the tagged parent.</li>
* <li>When the offset is behind the context node, we return the last tagged child of the
* context node.</li>
* <li>When the offset is before the context node, we return the first tagged child of the
* context node.</li>
* </ul>
*/
static Node findNearestNodeAt(int offset, Node contextNode, Set<Class<? extends Tag>> tags) {
Node node = (Node) ((InstrumentableNode) contextNode).materializeInstrumentableNodes(tags);
SourceSection section = node.getSourceSection();
int startIndex = section.getCharIndex();
int endIndex = getCharEndIndex(section);
if (startIndex <= offset && offset <= endIndex) {
Node parent = findParentTaggedNode(node, tags);
Node ch = findChildTaggedNode(node, offset, tags, parent != null, false);
if (ch == null) {
return parent;
}
return ch;
} else if (endIndex < offset) {
return findLastNode(node, tags);
} else {
// offset < o1
return findFirstNode(node, tags);
}
}
Aggregations