comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/nodes/SafeReplaceTest.java @ 18985:867058575979

Truffle: Improved support for "probing" AST nodes: - Node.isSafelyReplacaeableBy(Node) checks in advance if Node.replace(Node) would be unsafe (crash the VM). - Hoist Probe() from language imlementations into Node; now completely language agnostic. - Language implementations support probing by implementing Node.isInstrumentable() and Node.createWrapperNode() - Node.Probe() throws ProbeException (without side effects) if the probe fails. -- ProbeException contains an instance of ProbeFailure that diagnoses the failure in detail - Additional measures to prevent instrumentation from being applied to internal InstrumentationNodes. - Promote ProbeListener to top level interface and add a default implementation
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 27 Jan 2015 20:24:54 -0800
parents
children 67d9e635102f
comparison
equal deleted inserted replaced
18984:0f462015296f 18985:867058575979
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.nodes;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.frame.*;
30 import com.oracle.truffle.api.nodes.*;
31
32 /**
33 * Tests optional method for ensuring that a node replacement is type safe. Ordinary node
34 * replacement is performed by unsafe assignment of a parent node's child field.
35 */
36 public class SafeReplaceTest {
37
38 @Test
39 public void testCorrectReplacement() {
40 TestRootNode root = new TestRootNode();
41 final TestNode oldChild = new TestNode();
42 root.child = oldChild;
43 assertFalse(oldChild.isReplaceable()); // No parent node
44 root.adoptChildren();
45 assertTrue(oldChild.isReplaceable()); // Now adopted by parent
46 final TestNode newChild = new TestNode();
47 assertTrue(oldChild.isSafelyReplaceableBy(newChild)); // Parent field type is assignable by
48 // new node
49 oldChild.replace(newChild);
50 root.execute(null);
51 assertEquals(root.executed, 1);
52 assertEquals(oldChild.executed, 0);
53 assertEquals(newChild.executed, 1);
54 }
55
56 @Test
57 public void testIncorrectReplacement() {
58 TestRootNode root = new TestRootNode();
59 final TestNode oldChild = new TestNode();
60 root.child = oldChild;
61 root.adoptChildren();
62 final WrongTestNode newChild = new WrongTestNode();
63 assertFalse(oldChild.isSafelyReplaceableBy(newChild));
64 // Can't test: oldChild.replace(newChild);
65 // Fails if assertions checked; else unsafe assignment will eventually crash the VM
66 }
67
68 private static class TestNode extends Node {
69
70 private int executed;
71
72 public Object execute() {
73 executed++;
74 return null;
75 }
76 }
77
78 private static class TestRootNode extends RootNode {
79
80 @Child TestNode child;
81
82 private int executed;
83
84 @Override
85 public Object execute(VirtualFrame frame) {
86 executed++;
87 child.execute();
88 return null;
89 }
90 }
91
92 private static class WrongTestNode extends Node {
93 }
94
95 }