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.nodes.*;
|
|
16 import com.oracle.truffle.ruby.runtime.*;
|
|
17 import com.oracle.truffle.ruby.runtime.control.*;
|
|
18 import com.oracle.truffle.ruby.runtime.core.*;
|
|
19 import com.oracle.truffle.ruby.runtime.core.array.*;
|
|
20 import com.oracle.truffle.ruby.runtime.methods.*;
|
|
21 import com.oracle.truffle.ruby.runtime.objects.*;
|
|
22
|
|
23 /**
|
|
24 * Represents a super call - that is a call with self as the receiver, but the superclass of self
|
|
25 * used for lookup. Currently implemented without any caching, and needs to be replaced with the
|
|
26 * same caching mechanism as for normal calls without complicating the existing calls too much.
|
|
27 */
|
|
28 @NodeInfo(shortName = "general-super-call")
|
|
29 public class GeneralSuperCallNode extends RubyNode {
|
|
30
|
|
31 private final String name;
|
|
32 private final boolean isSplatted;
|
|
33 @Child protected RubyNode block;
|
|
34 @Children protected final RubyNode[] arguments;
|
|
35
|
|
36 public GeneralSuperCallNode(RubyContext context, SourceSection sourceSection, String name, RubyNode block, RubyNode[] arguments, boolean isSplatted) {
|
|
37 super(context, sourceSection);
|
|
38
|
|
39 assert name != null;
|
|
40 assert arguments != null;
|
|
41 assert !isSplatted || arguments.length == 1;
|
|
42
|
|
43 this.name = name;
|
|
44 this.block = adoptChild(block);
|
|
45 this.arguments = adoptChildren(arguments);
|
|
46 this.isSplatted = isSplatted;
|
|
47 }
|
|
48
|
|
49 @ExplodeLoop
|
|
50 @Override
|
|
51 public final Object execute(VirtualFrame frame) {
|
|
52 // This method is only a simple implementation - it needs proper caching
|
|
53
|
|
54 CompilerAsserts.neverPartOfCompilation();
|
|
55
|
|
56 final RubyBasicObject self = (RubyBasicObject) frame.getArguments(RubyArguments.class).getSelf();
|
|
57
|
|
58 // Execute the arguments
|
|
59
|
|
60 final Object[] argumentsObjects = new Object[arguments.length];
|
|
61
|
|
62 CompilerAsserts.compilationConstant(arguments.length);
|
|
63 for (int i = 0; i < arguments.length; i++) {
|
|
64 argumentsObjects[i] = arguments[i].execute(frame);
|
|
65 }
|
|
66
|
|
67 // Execute the block
|
|
68
|
|
69 RubyProc blockObject;
|
|
70
|
|
71 if (block != null) {
|
|
72 final Object blockTempObject = block.execute(frame);
|
|
73
|
|
74 if (blockTempObject instanceof NilPlaceholder) {
|
|
75 blockObject = null;
|
|
76 } else {
|
|
77 blockObject = (RubyProc) blockTempObject;
|
|
78 }
|
|
79 } else {
|
|
80 blockObject = null;
|
|
81 }
|
|
82
|
|
83 // Lookup method
|
|
84
|
|
85 final RubyClass selfClass = self.getRubyClass();
|
|
86 final RubyMethod method = selfClass.getSuperclass().lookupMethod(name);
|
|
87
|
|
88 if (method == null || method.isUndefined()) {
|
|
89 CompilerDirectives.transferToInterpreter();
|
|
90 throw new RaiseException(getContext().getCoreLibrary().nameErrorNoMethod(name, self.toString()));
|
|
91 }
|
|
92
|
|
93 if (!method.isVisibleTo(self)) {
|
|
94 CompilerDirectives.transferToInterpreter();
|
|
95 throw new RaiseException(getContext().getCoreLibrary().noMethodError("(unknown)"));
|
|
96 }
|
|
97
|
|
98 // Call the method
|
|
99
|
|
100 if (isSplatted) {
|
|
101 final RubyArray argumentsArray = (RubyArray) argumentsObjects[0];
|
|
102 return method.call(frame.pack(), self, blockObject, argumentsArray.asList().toArray());
|
|
103 } else {
|
|
104 return method.call(frame.pack(), self, blockObject, argumentsObjects);
|
|
105 }
|
|
106 }
|
|
107
|
|
108 @Override
|
|
109 public Object isDefined(VirtualFrame frame) {
|
|
110 final RubyContext context = getContext();
|
|
111
|
|
112 try {
|
|
113 final RubyBasicObject self = context.getCoreLibrary().box(frame.getArguments(RubyArguments.class).getSelf());
|
|
114 final RubyBasicObject receiverRubyObject = context.getCoreLibrary().box(self);
|
|
115
|
|
116 final RubyMethod method = receiverRubyObject.getRubyClass().getSuperclass().lookupMethod(name);
|
|
117
|
|
118 if (method == null || method.isUndefined() || !method.isVisibleTo(self)) {
|
|
119 return NilPlaceholder.INSTANCE;
|
|
120 } else {
|
|
121 return context.makeString("super");
|
|
122 }
|
|
123 } catch (Exception e) {
|
|
124 return NilPlaceholder.INSTANCE;
|
|
125 }
|
|
126 }
|
|
127
|
|
128 }
|