comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/instrument/ToolNodeInstrumentationTest.java @ 20106:2e3cc2a27711

Truffle/Instrumentation: a new flavor of Instrument that lazily provides an AST fragment to be attached/adopted directly into a running AST, and to which execution event notifications will be routed. Important use cases so far include conditional breakpoints (with optimizeable conditions) and Ruby set_trace_func.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 31 Mar 2015 19:01:07 -0700
parents
children f96165ecb6f1
comparison
equal deleted inserted replaced
20105:e73096245a4c 20106:2e3cc2a27711
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.test.instrument;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.api.frame.*;
31 import com.oracle.truffle.api.instrument.*;
32 import com.oracle.truffle.api.nodes.*;
33 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestAdditionNode;
34 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestRootNode;
35 import com.oracle.truffle.api.test.instrument.InstrumentationTestNodes.TestValueNode;
36
37 /**
38 * Tests instrumentation where a client can attach a node that gets attached into the AST.
39 */
40 public class ToolNodeInstrumentationTest {
41
42 @Test
43 public void testToolNodeListener() {
44 // Create a simple addition AST
45 final TruffleRuntime runtime = Truffle.getRuntime();
46 final TestValueNode leftValueNode = new TestValueNode(6);
47 final TestValueNode rightValueNode = new TestValueNode(7);
48 final TestAdditionNode addNode = new TestAdditionNode(leftValueNode, rightValueNode);
49 final TestRootNode rootNode = new TestRootNode(addNode);
50 final CallTarget callTarget1 = runtime.createCallTarget(rootNode);
51
52 // Ensure it executes correctly
53 assertEquals(13, callTarget1.call());
54
55 // Probe the addition node
56 final Probe probe = addNode.probe();
57
58 assertEquals(13, callTarget1.call());
59
60 // Attach a listener that never actually attaches a node.
61 final Instrument instrument = Instrument.create(new ToolNodeInstrumentListener() {
62
63 public ToolNode getToolNode(Probe p) {
64 return null;
65 }
66
67 }, null);
68 probe.attach(instrument);
69
70 assertEquals(13, callTarget1.call());
71
72 final int[] count = new int[1];
73
74 // Attach a listener that never actually attaches a node.
75 probe.attach(Instrument.create(new ToolNodeInstrumentListener() {
76
77 public ToolNode getToolNode(Probe p) {
78 return new ToolNode() {
79
80 @Override
81 public void enter(Node node, VirtualFrame vFrame) {
82 count[0] = count[0] + 1;
83 }
84 };
85 }
86
87 }, null));
88 assertEquals(0, count[0]);
89
90 assertEquals(13, callTarget1.call());
91
92 assertEquals(1, count[0]);
93
94 }
95
96 }