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.methods;
|
|
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.core.*;
|
|
18 import com.oracle.truffle.ruby.runtime.methods.*;
|
|
19
|
|
20 @NodeInfo(shortName = "add-method")
|
|
21 public class AddMethodNode extends RubyNode {
|
|
22
|
|
23 @Child protected RubyNode receiver;
|
|
24 @Child protected MethodDefinitionNode method;
|
|
25
|
|
26 public AddMethodNode(RubyContext context, SourceSection section, RubyNode receiver, MethodDefinitionNode method) {
|
|
27 super(context, section);
|
|
28 this.receiver = adoptChild(receiver);
|
|
29 this.method = adoptChild(method);
|
|
30 }
|
|
31
|
|
32 @Override
|
|
33 public Object execute(VirtualFrame frame) {
|
|
34 final Object receiverObject = receiver.execute(frame);
|
|
35
|
|
36 final RubyMethod methodObject = (RubyMethod) method.execute(frame);
|
|
37
|
|
38 final FrameSlot moduleFunctionFlagSlot = frame.getFrameDescriptor().findFrameSlot(RubyModule.MODULE_FUNCTION_FLAG_FRAME_SLOT_ID);
|
|
39
|
|
40 boolean moduleFunctionFlag;
|
|
41
|
|
42 if (moduleFunctionFlagSlot == null) {
|
|
43 moduleFunctionFlag = false;
|
|
44 } else {
|
|
45 Object moduleFunctionObject;
|
|
46
|
|
47 try {
|
|
48 moduleFunctionObject = frame.getObject(moduleFunctionFlagSlot);
|
|
49 } catch (FrameSlotTypeException e) {
|
|
50 throw new RuntimeException(e);
|
|
51 }
|
|
52
|
|
53 if (moduleFunctionObject instanceof Boolean) {
|
|
54 moduleFunctionFlag = (boolean) moduleFunctionObject;
|
|
55 } else {
|
|
56 moduleFunctionFlag = false;
|
|
57 }
|
|
58 }
|
|
59
|
|
60 final RubyModule module = (RubyModule) receiverObject;
|
|
61
|
|
62 final RubyMethod methodWithDeclaringModule = methodObject.withDeclaringModule(module);
|
|
63
|
|
64 module.addMethod(methodWithDeclaringModule);
|
|
65
|
|
66 if (moduleFunctionFlag) {
|
|
67 module.getSingletonClass().addMethod(methodWithDeclaringModule);
|
|
68 }
|
|
69
|
|
70 return NilPlaceholder.INSTANCE;
|
|
71 }
|
|
72 }
|