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.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 (feature.equals("stringio")) {
|
|
39 context.implementationMessage("stringio not yet implemented");
|
|
40 return true;
|
|
41 }
|
|
42
|
|
43 if (feature.equals("rbconfig")) {
|
|
44 // Kernel#rbconfig is always there
|
|
45 return true;
|
|
46 }
|
|
47
|
|
48 if (feature.equals("pp")) {
|
|
49 // Kernel#pretty_inspect is always there
|
|
50 return true;
|
|
51 }
|
|
52
|
|
53 // Get the load path
|
|
54
|
|
55 final Object loadPathObject = context.getCoreLibrary().getGlobalVariablesObject().getInstanceVariable("$:");
|
|
56
|
|
57 if (!(loadPathObject instanceof RubyArray)) {
|
|
58 throw new RuntimeException("$: is not an array");
|
|
59 }
|
|
60
|
|
61 final List<Object> loadPath = ((RubyArray) loadPathObject).asList();
|
|
62
|
|
63 // Try as a full path
|
|
64
|
|
65 if (requireInPath("", feature)) {
|
|
66 return true;
|
|
67 }
|
|
68
|
|
69 // Try each load path in turn
|
|
70
|
|
71 for (Object pathObject : loadPath) {
|
|
72 final String path = pathObject.toString();
|
|
73
|
|
74 if (requireInPath(path, feature)) {
|
|
75 return true;
|
|
76 }
|
|
77 }
|
|
78
|
|
79 // Didn't find the feature
|
|
80
|
|
81 throw new RaiseException(context.getCoreLibrary().loadErrorCannotLoad(feature));
|
|
82 }
|
|
83
|
|
84 public boolean requireInPath(String path, String feature) throws IOException {
|
|
85 if (requireFile(feature)) {
|
|
86 return true;
|
|
87 }
|
|
88
|
|
89 if (requireFile(feature + ".rb")) {
|
|
90 return true;
|
|
91 }
|
|
92
|
|
93 if (requireFile(path + File.separator + feature)) {
|
|
94 return true;
|
|
95 }
|
|
96
|
|
97 if (requireFile(path + File.separator + feature + ".rb")) {
|
|
98 return true;
|
|
99 }
|
|
100
|
|
101 return false;
|
|
102 }
|
|
103
|
|
104 private boolean requireFile(String fileName) throws IOException {
|
|
105 if (requiredFiles.contains(fileName)) {
|
|
106 return true;
|
|
107 }
|
|
108
|
|
109 /*
|
|
110 * There is unfortunately no way to check if a string is a file path, or a URL. file:foo.txt
|
|
111 * is a valid file name, as well as a valid URL. We try as a file path first.
|
|
112 */
|
|
113
|
|
114 if (new File(fileName).isFile()) {
|
|
115 context.loadFile(fileName);
|
|
116 requiredFiles.add(fileName);
|
|
117 return true;
|
|
118 } else {
|
|
119 URL url;
|
|
120
|
|
121 try {
|
|
122 url = new URL(fileName);
|
|
123 } catch (MalformedURLException e) {
|
|
124 return false;
|
|
125 }
|
|
126
|
|
127 InputStream inputStream;
|
|
128
|
|
129 try {
|
|
130 inputStream = url.openConnection().getInputStream();
|
|
131 } catch (IOException e) {
|
|
132 return false;
|
|
133 }
|
|
134
|
|
135 context.load(context.getSourceManager().get(url.toString(), inputStream));
|
|
136 requiredFiles.add(fileName);
|
|
137 return true;
|
|
138 }
|
|
139 }
|
|
140
|
|
141 }
|