comparison graal/com.oracle.truffle.ruby.runtime/src/com/oracle/truffle/ruby/runtime/subsystems/FeatureManager.java @ 13514:0fbee3eb71f0

Ruby: import project.
author Chris Seaton <chris.seaton@oracle.com>
date Mon, 06 Jan 2014 17:12:09 +0000
parents
children 497fada09efb
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.runtime.subsystems;
11
12 import java.io.*;
13 import java.net.*;
14 import java.util.*;
15
16 import com.oracle.truffle.ruby.runtime.*;
17 import com.oracle.truffle.ruby.runtime.control.*;
18 import com.oracle.truffle.ruby.runtime.core.array.*;
19
20 /**
21 * Manages the features loaded into Ruby. This basically means which library files are loaded, but
22 * Ruby often talks about requiring features, not files.
23 *
24 */
25 public class FeatureManager {
26
27 private RubyContext context;
28
29 private final Set<String> requiredFiles = new HashSet<>();
30
31 public FeatureManager(RubyContext context) {
32 this.context = context;
33 }
34
35 public boolean require(String feature) throws IOException {
36 // Some features are handled specially
37
38 if (context.getConfiguration().getRubyVersion().is19OrLater() && feature.equals("continuation")) {
39 // We always load continuations
40 return true;
41 }
42
43 if (feature.equals("stringio")) {
44 context.implementationMessage("stringio not yet implemented");
45 return true;
46 }
47
48 if (feature.equals("rbconfig")) {
49 // Kernel#rbconfig is always there
50 return true;
51 }
52
53 if (feature.equals("pp")) {
54 // Kernel#pretty_inspect is always there
55 return true;
56 }
57
58 // Get the load path
59
60 final Object loadPathObject = context.getCoreLibrary().getGlobalVariablesObject().getInstanceVariable("$:");
61
62 if (!(loadPathObject instanceof RubyArray)) {
63 throw new RuntimeException("$: is not an array");
64 }
65
66 final List<Object> loadPath = ((RubyArray) loadPathObject).asList();
67
68 // Try as a full path
69
70 if (requireInPath("", feature)) {
71 return true;
72 }
73
74 // Try each load path in turn
75
76 for (Object pathObject : loadPath) {
77 final String path = pathObject.toString();
78
79 if (requireInPath(path, feature)) {
80 return true;
81 }
82 }
83
84 // Didn't find the feature
85
86 throw new RaiseException(context.getCoreLibrary().loadErrorCannotLoad(feature));
87 }
88
89 public boolean requireInPath(String path, String feature) throws IOException {
90 if (requireFile(feature)) {
91 return true;
92 }
93
94 if (requireFile(feature + ".rb")) {
95 return true;
96 }
97
98 if (requireFile(path + File.separator + feature)) {
99 return true;
100 }
101
102 if (requireFile(path + File.separator + feature + ".rb")) {
103 return true;
104 }
105
106 return false;
107 }
108
109 private boolean requireFile(String fileName) throws IOException {
110 if (requiredFiles.contains(fileName)) {
111 return true;
112 }
113
114 /*
115 * There is unfortunately no way to check if a string is a file path, or a URL. file:foo.txt
116 * is a valid file name, as well as a valid URL. We try as a file path first.
117 */
118
119 if (new File(fileName).isFile()) {
120 context.loadFile(fileName);
121 requiredFiles.add(fileName);
122 return true;
123 } else {
124 URL url;
125
126 try {
127 url = new URL(fileName);
128 } catch (MalformedURLException e) {
129 return false;
130 }
131
132 InputStream inputStream;
133
134 try {
135 inputStream = url.openConnection().getInputStream();
136 } catch (IOException e) {
137 return false;
138 }
139
140 context.load(context.getSourceManager().get(url.toString(), inputStream));
141 requiredFiles.add(fileName);
142 return true;
143 }
144 }
145
146 }