Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/examples/RubyCall.java @ 22157:dc83cc1f94f2
Using fully qualified imports
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 16 Sep 2015 11:33:22 +0200 |
parents | 9c8c0937da41 |
children | 14e6dfb1ef05 |
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 |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
25 import com.oracle.truffle.api.Assumption; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
26 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
|
27 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
28 import com.oracle.truffle.api.dsl.Cached; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
29 import com.oracle.truffle.api.dsl.ImportStatic; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
30 import com.oracle.truffle.api.dsl.Specialization; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
31 import com.oracle.truffle.api.dsl.internal.SpecializedNode; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
32 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.createArguments; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
33 import static com.oracle.truffle.api.dsl.test.examples.ExampleNode.createTarget; |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 import com.oracle.truffle.api.dsl.test.examples.RubyCallFactory.RubyDispatchNodeGen; |
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.RubyCallFactory.RubyHeadNodeGen; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
36 import com.oracle.truffle.api.dsl.test.examples.RubyCallFactory.RubyLookupNodeGen; |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
37 import com.oracle.truffle.api.frame.VirtualFrame; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
38 import com.oracle.truffle.api.nodes.DirectCallNode; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
39 import com.oracle.truffle.api.nodes.IndirectCallNode; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
40 import com.oracle.truffle.api.nodes.Node; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
41 import com.oracle.truffle.api.nodes.Node.Child; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
42 import com.oracle.truffle.api.utilities.CyclicAssumption; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
43 import java.util.HashMap; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
44 import java.util.Map; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
45 import static org.junit.Assert.assertEquals; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
46 import org.junit.Test; |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 /** |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 * This example illustrates a simplified version of a Ruby function call semantics (RubyHeadNode). |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 * The example usage shows how methods can be redefined in this implementation. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 */ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 @SuppressWarnings("unused") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 public class RubyCall { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 @Test |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 public void testCall() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 RubyHeadNode node = RubyHeadNodeGen.create(createArguments(4)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 CallTarget nodeTarget = createTarget(node); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 final Object firstArgument = "someArgument"; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 // dummyMethod is just going to return the some argument of the function |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 final Object testMethodName = "getSomeArgument"; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 // implementation returns first argument |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 InternalMethod aClassTestMethod = new InternalMethod(ExampleNode.createDummyTarget(3)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 // implementation returns second argument |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 InternalMethod bClassTestMethod = new InternalMethod(ExampleNode.createDummyTarget(4)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 // implementation returns third argument |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 InternalMethod cClassTestMethod = new InternalMethod(ExampleNode.createDummyTarget(5)); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 // defines hierarchy C extends B extends A |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 RubyClass aClass = new RubyClass("A", null); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 RubyClass bClass = new RubyClass("B", aClass); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 RubyClass cClass = new RubyClass("C", bClass); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 RubyObject aInstance = new RubyObject(aClass); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 RubyObject bInstance = new RubyObject(bClass); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 RubyObject cInstance = new RubyObject(cClass); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 // undefined method call |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 assertEquals(RubyObject.NIL, nodeTarget.call(cInstance, testMethodName, null, new Object[]{firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 // method defined in a |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 aClass.addMethod(testMethodName, aClassTestMethod); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 assertEquals(firstArgument, nodeTarget.call(aInstance, testMethodName, null, new Object[]{firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 assertEquals(firstArgument, nodeTarget.call(bInstance, testMethodName, null, new Object[]{firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 assertEquals(firstArgument, nodeTarget.call(cInstance, testMethodName, null, new Object[]{firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 // method redefined in b |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 bClass.addMethod(testMethodName, bClassTestMethod); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 assertEquals(firstArgument, nodeTarget.call(aInstance, testMethodName, null, new Object[]{firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 assertEquals(firstArgument, nodeTarget.call(bInstance, testMethodName, null, new Object[]{null, firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 assertEquals(firstArgument, nodeTarget.call(cInstance, testMethodName, null, new Object[]{null, firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 // method redefined in c |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 cClass.addMethod(testMethodName, cClassTestMethod); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 assertEquals(firstArgument, nodeTarget.call(aInstance, testMethodName, null, new Object[]{firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 assertEquals(firstArgument, nodeTarget.call(bInstance, testMethodName, null, new Object[]{null, firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 assertEquals(firstArgument, nodeTarget.call(cInstance, testMethodName, null, new Object[]{null, null, firstArgument})); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
99 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 } |
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 public static class RubyHeadNode extends ExampleNode { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 |
20940
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
104 @Child private RubyLookupNode lookup = RubyLookupNodeGen.create(); |
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
105 @Child private RubyDispatchNode dispatch = RubyDispatchNodeGen.create(); |
19301
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 @Specialization |
20940
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
108 public Object doCall(VirtualFrame frame, RubyObject receiverObject, Object methodName, Object blockObject, Object... argumentsObjects) { |
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
109 InternalMethod method = lookup.executeLookup(receiverObject, methodName); |
19301
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 Object[] packedArguments = new Object[argumentsObjects.length + 3]; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
112 packedArguments[0] = method; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
113 packedArguments[1] = receiverObject; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
114 packedArguments[2] = blockObject; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
115 System.arraycopy(argumentsObjects, 0, packedArguments, 3, argumentsObjects.length); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
116 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
117 return dispatch.executeDispatch(frame, method, packedArguments); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
118 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
119 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
120 |
20940
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
121 public abstract static class RubyLookupNode extends Node { |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
122 |
20940
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
123 public abstract InternalMethod executeLookup(RubyObject receiver, Object method); |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
124 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
125 @Specialization(guards = "receiver.getRubyClass() == cachedClass", assumptions = "cachedClass.getDependentAssumptions()") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
126 protected static InternalMethod cachedLookup(RubyObject receiver, Object name, // |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
127 @Cached("receiver.getRubyClass()") RubyClass cachedClass, // |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
128 @Cached("genericLookup(receiver, name)") InternalMethod cachedLookup) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
129 return cachedLookup; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
130 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
131 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
132 @Specialization(contains = "cachedLookup") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
133 protected static InternalMethod genericLookup(RubyObject receiver, Object name) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
134 return receiver.getRubyClass().lookup(name); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
135 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
136 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
137 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
138 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
139 @ImportStatic(InternalMethod.class) |
20940
476374f3fe9a
Truffle-DSL: generate better polymorphic execute signatures
Christian Humer <christian.humer@gmail.com>
parents:
19301
diff
changeset
|
140 public abstract static class RubyDispatchNode extends Node { |
19301
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
141 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
142 public abstract Object executeDispatch(VirtualFrame frame, InternalMethod function, Object[] packedArguments); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
143 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
144 /* |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
145 * Please note that cachedMethod != METHOD_MISSING is invoked once at specialization |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
146 * instantiation. It is never executed on the fast path. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
147 */ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
148 @Specialization(guards = {"method == cachedMethod", "cachedMethod != METHOD_MISSING"}) |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
149 protected static Object directCall(VirtualFrame frame, InternalMethod method, Object[] arguments, // |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
150 @Cached("method") InternalMethod cachedMethod, // |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
151 @Cached("create(cachedMethod.getTarget())") DirectCallNode callNode) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
152 return callNode.call(frame, arguments); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
153 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
154 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
155 /* |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
156 * The method == METHOD_MISSING can fold if the RubyLookup results just in a single entry |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
157 * returning the constant METHOD_MISSING. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
158 */ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
159 @Specialization(guards = "method == METHOD_MISSING") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
160 protected static Object methodMissing(VirtualFrame frame, InternalMethod method, Object[] arguments) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
161 // a real implementation would do a call to a method named method_missing here |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
162 return RubyObject.NIL; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
163 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
164 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
165 @Specialization(contains = "directCall", guards = "method != METHOD_MISSING") |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
166 protected static Object indirectCall(VirtualFrame frame, InternalMethod method, Object[] arguments, // |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
167 @Cached("create()") IndirectCallNode callNode) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
168 return callNode.call(frame, method.getTarget(), arguments); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
169 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
170 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
171 @Override |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
172 public String toString() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
173 return ((SpecializedNode) this).getSpecializationNode().toString(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
174 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
175 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
176 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
177 public static final class RubyObject { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
178 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
179 public static final RubyObject NIL = new RubyObject(null); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
180 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
181 private final RubyClass rubyClass; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
182 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
183 public RubyObject(RubyClass rubyClass) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
184 this.rubyClass = rubyClass; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
185 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
186 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
187 public RubyClass getRubyClass() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
188 return rubyClass; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
189 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
190 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
191 @Override |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
192 public String toString() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
193 return "RubyObject[class=" + rubyClass + "]"; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
194 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
195 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
196 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
197 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
198 public static final class RubyClass /* this would extend RubyModule */{ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
199 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
200 private final String name; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
201 private final RubyClass parent; // this would be a RubyModule |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
202 private final CyclicAssumption unmodified; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
203 private final Map<Object, InternalMethod> methods = new HashMap<>(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
204 private Assumption[] cachedDependentAssumptions; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
205 private final int depth; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
206 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
207 public RubyClass(String name, RubyClass parent) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
208 this.name = name; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
209 this.parent = parent; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
210 this.unmodified = new CyclicAssumption("unmodified class " + name); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
211 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
212 // lookup depth for array allocation |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
213 RubyClass clazz = parent; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
214 int currentDepth = 1; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
215 while (clazz != null) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
216 currentDepth++; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
217 clazz = clazz.parent; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
218 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
219 this.depth = currentDepth; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
220 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
221 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
222 @TruffleBoundary |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
223 public InternalMethod lookup(Object methodName) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
224 InternalMethod method = methods.get(methodName); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
225 if (method == null) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
226 if (parent != null) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
227 return parent.lookup(methodName); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
228 } else { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
229 return InternalMethod.METHOD_MISSING; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
230 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
231 } else { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
232 return method; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
233 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
234 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
235 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
236 @TruffleBoundary |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
237 public void addMethod(Object methodName, InternalMethod method) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
238 // check for existing method omitted for simplicity |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
239 this.methods.put(methodName, method); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
240 this.unmodified.invalidate(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
241 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
242 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
243 /* |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
244 * Method collects all unmodified assumptions in the class hierarchy. The result is cached |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
245 * per class to void recreation per call site. |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
246 */ |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
247 @TruffleBoundary |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
248 public Assumption[] getDependentAssumptions() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
249 Assumption[] dependentAssumptions = cachedDependentAssumptions; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
250 if (dependentAssumptions != null) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
251 // we can use the cached dependent assumptions only if they are still valid |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
252 for (Assumption assumption : cachedDependentAssumptions) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
253 if (!assumption.isValid()) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
254 dependentAssumptions = null; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
255 break; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
256 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
257 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
258 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
259 if (dependentAssumptions == null) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
260 cachedDependentAssumptions = dependentAssumptions = createDependentAssumptions(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
261 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
262 return dependentAssumptions; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
263 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
264 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
265 @Override |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
266 public String toString() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
267 return "RubyClass[name=" + name + "]"; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
268 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
269 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
270 private Assumption[] createDependentAssumptions() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
271 Assumption[] dependentAssumptions; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
272 RubyClass clazz = this; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
273 dependentAssumptions = new Assumption[depth]; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
274 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
275 // populate array |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
276 int index = 0; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
277 do { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
278 dependentAssumptions[index] = clazz.unmodified.getAssumption(); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
279 index++; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
280 clazz = clazz.parent; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
281 } while (clazz != null); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
282 return dependentAssumptions; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
283 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
284 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
285 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
286 public static final class InternalMethod { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
287 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
288 public static final InternalMethod METHOD_MISSING = new InternalMethod(null); |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
289 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
290 private final CallTarget target; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
291 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
292 public InternalMethod(CallTarget target) { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
293 this.target = target; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
294 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
295 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
296 public CallTarget getTarget() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
297 return target; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
298 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
299 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
300 @Override |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
301 public String toString() { |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
302 return "InternalMethod[target=" + getTarget() + "]"; |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
303 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
304 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
305 } |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
306 |
a79a3e467245
Truffle-DSL: move examples package into test package for the project canonicalizer.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
307 } |