comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/objects/DefineOrGetClassNode.java @ 13514:0fbee3eb71f0

Ruby: import project.
author Chris Seaton <chris.seaton@oracle.com>
date Mon, 06 Jan 2014 17:12:09 +0000
parents
children
comparison
equal deleted inserted replaced
13513:64a23ce736a0 13514:0fbee3eb71f0
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;
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.control.*;
17 import com.oracle.truffle.ruby.runtime.core.*;
18
19 /**
20 * Define a new class, or get the existing one of the same name.
21 */
22 public class DefineOrGetClassNode extends RubyNode {
23
24 private final String name;
25 @Child protected RubyNode moduleDefinedIn;
26 @Child protected RubyNode superClass;
27
28 public DefineOrGetClassNode(RubyContext context, SourceSection sourceSection, String name, RubyNode moduleDefinedIn, RubyNode superClass) {
29 super(context, sourceSection);
30 this.name = name;
31 this.moduleDefinedIn = adoptChild(moduleDefinedIn);
32 this.superClass = adoptChild(superClass);
33 }
34
35 @Override
36 public Object execute(VirtualFrame frame) {
37 CompilerAsserts.neverPartOfCompilation();
38
39 final RubyContext context = getContext();
40
41 final RubyModule moduleDefinedInObject = (RubyModule) moduleDefinedIn.execute(frame);
42
43 // Look for a current definition of the class, or create a new one
44
45 final Object constantValue = moduleDefinedInObject.lookupConstant(name);
46
47 RubyClass definingClass;
48
49 if (constantValue == null) {
50 final Object self = frame.getArguments(RubyArguments.class).getSelf();
51
52 RubyModule parentModule;
53
54 if (self instanceof RubyModule) {
55 parentModule = (RubyModule) self;
56 } else {
57 // Because it's top level, and so self is the magic main object
58 parentModule = null;
59 }
60
61 final RubyClass superClassObject = (RubyClass) superClass.execute(frame);
62
63 if (superClassObject instanceof RubyException.RubyExceptionClass) {
64 definingClass = new RubyException.RubyExceptionClass(superClassObject, name);
65 } else {
66 definingClass = new RubyClass(parentModule, superClassObject, name);
67 }
68
69 moduleDefinedInObject.setConstant(name, definingClass);
70 moduleDefinedInObject.getSingletonClass().setConstant(name, definingClass);
71
72 definingClass.getRubyClass().include(moduleDefinedInObject);
73 } else {
74 if (constantValue instanceof RubyClass) {
75 definingClass = (RubyClass) constantValue;
76 } else {
77 throw new RaiseException(context.getCoreLibrary().typeErrorIsNotA(constantValue.toString(), "class"));
78 }
79 }
80
81 return definingClass;
82 }
83 }