Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/Interop.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/Interop.java@a79a3e467245 |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
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.dsl.test.examples; | |
24 | |
25 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.*; | |
26 import static org.junit.Assert.*; | |
27 | |
28 import org.junit.*; | |
29 | |
30 import com.oracle.truffle.api.*; | |
31 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | |
32 import com.oracle.truffle.api.dsl.*; | |
33 import com.oracle.truffle.api.dsl.test.examples.InteropFactory.UseInteropNodeGen; | |
34 import com.oracle.truffle.api.frame.*; | |
35 import com.oracle.truffle.api.nodes.*; | |
36 | |
37 /** | |
38 * This example aims to illustrate how the {@link Cached} annotation can be used to implement a | |
39 * cache for a simplified language interoperability pattern. | |
40 */ | |
41 public class Interop { | |
42 | |
43 @Test | |
44 public void testInterop() { | |
45 UseInterop node = UseInteropNodeGen.create(createArguments(2)); | |
46 CallTarget target = createTarget(node); | |
47 TruffleObject o1 = new TruffleObject(); | |
48 TruffleObject o2 = new TruffleObject(); | |
49 TruffleObject o3 = new TruffleObject(); | |
50 TruffleObject o4 = new TruffleObject(); | |
51 assertEquals(42, target.call(o1, 42)); | |
52 assertEquals(43, target.call(o2, 43)); | |
53 assertEquals(44, target.call(o3, 44)); | |
54 assertEquals(3, node.cached); | |
55 assertEquals(0, node.generic); | |
56 assertEquals(45, target.call(o4, 45)); // operation gets generic | |
57 assertEquals(42, target.call(o1, 42)); | |
58 assertEquals(43, target.call(o2, 43)); | |
59 assertEquals(44, target.call(o3, 44)); | |
60 assertEquals(3, node.cached); | |
61 assertEquals(4, node.generic); | |
62 } | |
63 | |
64 public static class UseInterop extends ExampleNode { | |
65 | |
66 int cached = 0; | |
67 int generic = 0; | |
68 | |
69 @Specialization(guards = "operation.accept(target)") | |
70 protected Object interopCached(VirtualFrame frame, TruffleObject target, Object value, // | |
71 @Cached("target.createOperation()") TruffleObjectOperation operation) { | |
72 cached++; | |
73 return operation.execute(frame, target, value); | |
74 } | |
75 | |
76 @Specialization(contains = "interopCached") | |
77 protected Object interopGeneric(VirtualFrame frame, TruffleObject target, Object value) { | |
78 generic++; | |
79 return target.createOperation().execute(frame, target, value); | |
80 } | |
81 } | |
82 | |
83 public abstract static class TruffleObjectOperation extends Node { | |
84 | |
85 public abstract boolean accept(TruffleObject object); | |
86 | |
87 public abstract Object execute(VirtualFrame frame, Object target, Object value); | |
88 | |
89 } | |
90 | |
91 public static class TruffleObject { | |
92 | |
93 @TruffleBoundary | |
94 public TruffleObjectOperation createOperation() { | |
95 return new TruffleObjectOperation() { | |
96 @Override | |
97 public Object execute(VirtualFrame frame, Object target, Object value) { | |
98 return value; | |
99 } | |
100 | |
101 @Override | |
102 public boolean accept(TruffleObject object) { | |
103 return TruffleObject.this == object; | |
104 } | |
105 }; | |
106 } | |
107 } | |
108 | |
109 } |