Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/objects/DefineOrGetModuleNode.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 module, or get the existing one of the same name. | |
21 */ | |
22 public class DefineOrGetModuleNode extends RubyNode { | |
23 | |
24 private final String name; | |
25 @Child protected RubyNode moduleDefinedIn; | |
26 | |
27 public DefineOrGetModuleNode(RubyContext context, SourceSection sourceSection, String name, RubyNode moduleDefinedIn) { | |
28 super(context, sourceSection); | |
29 this.name = name; | |
30 this.moduleDefinedIn = adoptChild(moduleDefinedIn); | |
31 } | |
32 | |
33 @Override | |
34 public Object execute(VirtualFrame frame) { | |
35 CompilerAsserts.neverPartOfCompilation(); | |
36 | |
37 final RubyContext context = getContext(); | |
38 | |
39 final RubyModule moduleDefinedInObject = (RubyModule) moduleDefinedIn.execute(frame); | |
40 | |
41 // Look for a current definition of the module, or create a new one | |
42 | |
43 final Object constantValue = moduleDefinedInObject.lookupConstant(name); | |
44 | |
45 RubyModule definingModule; | |
46 | |
47 if (constantValue == null) { | |
48 final Object self = frame.getArguments(RubyArguments.class).getSelf(); | |
49 | |
50 RubyModule parentModule; | |
51 | |
52 if (self instanceof RubyModule) { | |
53 parentModule = (RubyModule) self; | |
54 } else { | |
55 // Because it's top level, and so self is the magic main object | |
56 parentModule = null; | |
57 } | |
58 | |
59 definingModule = new RubyModule(context.getCoreLibrary().getModuleClass(), parentModule, name); | |
60 moduleDefinedInObject.setConstant(name, definingModule); | |
61 moduleDefinedInObject.getSingletonClass().setConstant(name, definingModule); | |
62 } else { | |
63 if (constantValue instanceof RubyModule) { | |
64 definingModule = (RubyModule) constantValue; | |
65 } else { | |
66 throw new RaiseException(context.getCoreLibrary().typeErrorIsNotA(constantValue.toString(), "module")); | |
67 } | |
68 } | |
69 | |
70 return definingModule; | |
71 } | |
72 | |
73 } |