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.objects.instancevariables;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.frame.*;
|
|
14 import com.oracle.truffle.ruby.nodes.*;
|
|
15 import com.oracle.truffle.ruby.runtime.*;
|
|
16 import com.oracle.truffle.ruby.runtime.objects.*;
|
|
17
|
|
18 public abstract class ReadInstanceVariableNode extends RubyNode implements ReadNode {
|
|
19
|
|
20 protected final String name;
|
|
21 @Child protected RubyNode receiver;
|
|
22
|
|
23 public ReadInstanceVariableNode(RubyContext context, SourceSection sourceSection, String name, RubyNode receiver) {
|
|
24 super(context, sourceSection);
|
|
25 this.name = name;
|
|
26 this.receiver = adoptChild(receiver);
|
|
27 }
|
|
28
|
|
29 public ReadInstanceVariableNode respecialize(RubyBasicObject receiverObject) {
|
|
30 final StorageLocation storageLocation = receiverObject.getUpdatedObjectLayout().findStorageLocation(name);
|
|
31
|
|
32 if (storageLocation == null) {
|
|
33 return new ReadMissingInstanceVariableNode(getSourceSection(), name, receiver, receiverObject.getObjectLayout(), getContext());
|
|
34 }
|
|
35
|
|
36 if (storageLocation instanceof FixnumStorageLocation) {
|
|
37 return new ReadFixnumInstanceVariableNode(getContext(), getSourceSection(), name, receiver, storageLocation.getObjectLayout(), (FixnumStorageLocation) storageLocation);
|
|
38 } else if (storageLocation instanceof FloatStorageLocation) {
|
|
39 return new ReadFloatInstanceVariableNode(getContext(), getSourceSection(), name, receiver, storageLocation.getObjectLayout(), (FloatStorageLocation) storageLocation);
|
|
40 } else {
|
|
41 return new ReadObjectInstanceVariableNode(getContext(), getSourceSection(), name, receiver, storageLocation.getObjectLayout(), (ObjectStorageLocation) storageLocation);
|
|
42 }
|
|
43 }
|
|
44
|
|
45 public WriteInstanceVariableNode makeWriteNode(RubyNode rhs) {
|
|
46 return new UninitializedWriteInstanceVariableNode(getContext(), getEncapsulatingSourceSection(), name, (RubyNode) receiver.copy(), rhs);
|
|
47 }
|
|
48
|
|
49 public String getName() {
|
|
50 return name;
|
|
51 }
|
|
52
|
|
53 public RubyNode getReceiver() {
|
|
54 return receiver;
|
|
55 }
|
|
56
|
|
57 @Override
|
|
58 public Object isDefined(VirtualFrame frame) {
|
|
59 final RubyContext context = getContext();
|
|
60
|
|
61 try {
|
|
62 final Object receiverObject = receiver.execute(frame);
|
|
63 final RubyBasicObject receiverRubyObject = context.getCoreLibrary().box(receiverObject);
|
|
64
|
|
65 final ObjectLayout layout = receiverRubyObject.getObjectLayout();
|
|
66 final StorageLocation storageLocation = layout.findStorageLocation(name);
|
|
67
|
|
68 if (storageLocation.isSet(receiverRubyObject)) {
|
|
69 return context.makeString("instance-variable");
|
|
70 } else {
|
|
71 return NilPlaceholder.INSTANCE;
|
|
72 }
|
|
73 } catch (Exception e) {
|
|
74 return NilPlaceholder.INSTANCE;
|
|
75 }
|
|
76 }
|
|
77 }
|