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.core;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.dsl.*;
|
|
14 import com.oracle.truffle.api.frame.*;
|
|
15 import com.oracle.truffle.ruby.nodes.call.*;
|
|
16 import com.oracle.truffle.ruby.runtime.*;
|
|
17 import com.oracle.truffle.ruby.runtime.core.*;
|
|
18 import com.oracle.truffle.ruby.runtime.objects.*;
|
|
19
|
|
20 @CoreClass(name = "Class")
|
|
21 public abstract class ClassNodes {
|
|
22
|
|
23 @CoreMethod(names = "===", minArgs = 1, maxArgs = 1)
|
|
24 public abstract static class ContainsInstanceNode extends CoreMethodNode {
|
|
25
|
|
26 public ContainsInstanceNode(RubyContext context, SourceSection sourceSection) {
|
|
27 super(context, sourceSection);
|
|
28 }
|
|
29
|
|
30 public ContainsInstanceNode(ContainsInstanceNode prev) {
|
|
31 super(prev);
|
|
32 }
|
|
33
|
|
34 @Specialization
|
|
35 public boolean containsInstance(RubyClass rubyClass, RubyBasicObject instance) {
|
|
36 return instance.getRubyClass().assignableTo(rubyClass);
|
|
37 }
|
|
38 }
|
|
39
|
|
40 @CoreMethod(names = "new", needsBlock = true, isSplatted = true)
|
|
41 public abstract static class NewNode extends CoreMethodNode {
|
|
42
|
|
43 @Child protected DispatchHeadNode initialize;
|
|
44
|
|
45 public NewNode(RubyContext context, SourceSection sourceSection) {
|
|
46 super(context, sourceSection);
|
|
47 initialize = adoptChild(new DispatchHeadNode(context, getSourceSection(), "initialize", false));
|
|
48 }
|
|
49
|
|
50 public NewNode(NewNode prev) {
|
|
51 super(prev);
|
|
52 initialize = adoptChild(prev.initialize);
|
|
53 }
|
|
54
|
|
55 @Specialization
|
|
56 public RubyBasicObject newInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, @SuppressWarnings("unused") UndefinedPlaceholder block) {
|
|
57 return doNewInstance(frame, rubyClass, args, null);
|
|
58 }
|
|
59
|
|
60 @Specialization
|
|
61 public RubyBasicObject newInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, RubyProc block) {
|
|
62 return doNewInstance(frame, rubyClass, args, block);
|
|
63 }
|
|
64
|
|
65 private RubyBasicObject doNewInstance(VirtualFrame frame, RubyClass rubyClass, Object[] args, RubyProc block) {
|
|
66 final RubyBasicObject instance = rubyClass.newInstance();
|
|
67 initialize.dispatch(frame, instance, block, args);
|
|
68 return instance;
|
|
69 }
|
|
70
|
|
71 }
|
|
72
|
|
73 @CoreMethod(names = "to_s", maxArgs = 0)
|
|
74 public abstract static class ToSNode extends CoreMethodNode {
|
|
75
|
|
76 public ToSNode(RubyContext context, SourceSection sourceSection) {
|
|
77 super(context, sourceSection);
|
|
78 }
|
|
79
|
|
80 public ToSNode(ToSNode prev) {
|
|
81 super(prev);
|
|
82 }
|
|
83
|
|
84 @Specialization
|
|
85 public RubyString toS(RubyClass rubyClass) {
|
|
86 return getContext().makeString(rubyClass.getName());
|
|
87 }
|
|
88 }
|
|
89
|
|
90 }
|