13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.nodes.call;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.frame.*;
|
|
14 import com.oracle.truffle.api.nodes.*;
|
|
15 import com.oracle.truffle.ruby.runtime.*;
|
|
16 import com.oracle.truffle.ruby.runtime.core.*;
|
|
17 import com.oracle.truffle.ruby.runtime.methods.*;
|
|
18
|
|
19 /**
|
|
20 * A node in the dispatch chain that comes before the boxing point and caches a method on a Java
|
|
21 * object, matching it by looking at the class and assuming it has not been modified.
|
|
22 */
|
|
23 public class CachedUnboxedDispatchNode extends UnboxedDispatchNode {
|
|
24
|
|
25 private final Class expectedClass;
|
|
26 private final Assumption unmodifiedAssumption;
|
|
27 private final RubyMethod method;
|
|
28
|
|
29 @Child protected UnboxedDispatchNode next;
|
|
30
|
|
31 public CachedUnboxedDispatchNode(RubyContext context, SourceSection sourceSection, Class expectedClass, Assumption unmodifiedAssumption, RubyMethod method, UnboxedDispatchNode next) {
|
|
32 super(context, sourceSection);
|
|
33
|
|
34 assert expectedClass != null;
|
|
35 assert unmodifiedAssumption != null;
|
|
36 assert method != null;
|
|
37
|
|
38 this.expectedClass = expectedClass;
|
|
39 this.unmodifiedAssumption = unmodifiedAssumption;
|
|
40 this.method = method;
|
|
41 this.next = adoptChild(next);
|
|
42 }
|
|
43
|
|
44 @Override
|
|
45 public Object dispatch(VirtualFrame frame, Object receiverObject, RubyProc blockObject, Object[] argumentsObjects) {
|
|
46 // Check the class is what we expect
|
|
47
|
|
48 if (receiverObject.getClass() != expectedClass) {
|
|
49 return next.dispatch(frame, receiverObject, blockObject, argumentsObjects);
|
|
50 }
|
|
51
|
|
52 // Check the class has not been modified
|
|
53
|
|
54 try {
|
|
55 unmodifiedAssumption.check();
|
|
56 } catch (InvalidAssumptionException e) {
|
|
57 return respecialize("class modified", frame, receiverObject, blockObject, argumentsObjects);
|
|
58 }
|
|
59
|
|
60 // Call the method
|
|
61
|
|
62 return method.call(frame.pack(), receiverObject, blockObject, argumentsObjects);
|
|
63 }
|
|
64
|
|
65 @Override
|
|
66 public void setNext(UnboxedDispatchNode next) {
|
|
67 this.next = adoptChild(next);
|
|
68 }
|
|
69
|
|
70 }
|