Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/Interop.java @ 22548:6b76a24fffbd default tip
Use all variables: a, b, u,v and x, y
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 14 Jan 2016 14:20:57 +0100 |
parents | 5309cc9668e3 |
children |
rev | line source |
---|---|
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * accompanied this code). |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * questions. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 */ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 package com.oracle.truffle.api.dsl.test.examples; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 |
22241
14e6dfb1ef05
Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
22157
diff
changeset
|
25 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.createArguments; |
14e6dfb1ef05
Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
22157
diff
changeset
|
26 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.createTarget; |
14e6dfb1ef05
Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
22157
diff
changeset
|
27 import static org.junit.Assert.assertEquals; |
14e6dfb1ef05
Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
22157
diff
changeset
|
28 |
14e6dfb1ef05
Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
22157
diff
changeset
|
29 import org.junit.Test; |
14e6dfb1ef05
Truffle/Testing: massive rework of tests to accommodate recent instrumentation change with respect to "applyInstrumentation"
Michael Van De Vanter <michael.van.de.vanter@oracle.com>
parents:
22157
diff
changeset
|
30 |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
31 import com.oracle.truffle.api.CallTarget; |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
32 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
33 import com.oracle.truffle.api.dsl.Cached; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
34 import com.oracle.truffle.api.dsl.Specialization; |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 import com.oracle.truffle.api.dsl.test.examples.InteropFactory.UseInteropNodeGen; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
36 import com.oracle.truffle.api.frame.VirtualFrame; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
37 import com.oracle.truffle.api.nodes.Node; |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 /** |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 * This example aims to illustrate how the {@link Cached} annotation can be used to implement a |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 * cache for a simplified language interoperability pattern. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 */ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 public class Interop { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 @Test |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 public void testInterop() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 UseInterop node = UseInteropNodeGen.create(createArguments(2)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 CallTarget target = createTarget(node); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 TruffleObject o1 = new TruffleObject(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 TruffleObject o2 = new TruffleObject(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 TruffleObject o3 = new TruffleObject(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 TruffleObject o4 = new TruffleObject(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 assertEquals(42, target.call(o1, 42)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 assertEquals(43, target.call(o2, 43)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 assertEquals(44, target.call(o3, 44)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 assertEquals(3, node.cached); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 assertEquals(0, node.generic); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 assertEquals(45, target.call(o4, 45)); // operation gets generic |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 assertEquals(42, target.call(o1, 42)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 assertEquals(43, target.call(o2, 43)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 assertEquals(44, target.call(o3, 44)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 assertEquals(3, node.cached); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 assertEquals(4, node.generic); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 public static class UseInterop extends ExampleNode { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 int cached = 0; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 int generic = 0; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 @Specialization(guards = "operation.accept(target)") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 protected Object interopCached(VirtualFrame frame, TruffleObject target, Object value, // |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 @Cached("target.createOperation()") TruffleObjectOperation operation) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 cached++; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 return operation.execute(frame, target, value); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 @Specialization(contains = "interopCached") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 protected Object interopGeneric(VirtualFrame frame, TruffleObject target, Object value) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 generic++; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 return target.createOperation().execute(frame, target, value); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 public abstract static class TruffleObjectOperation extends Node { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 public abstract boolean accept(TruffleObject object); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 public abstract Object execute(VirtualFrame frame, Object target, Object value); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 public static class TruffleObject { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 @TruffleBoundary |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 public TruffleObjectOperation createOperation() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 return new TruffleObjectOperation() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 @Override |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
99 public Object execute(VirtualFrame frame, Object target, Object value) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 return value; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 @Override |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 public boolean accept(TruffleObject object) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 return TruffleObject.this == object; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 }; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
110 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
111 } |