comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/core/FileNodes.java @ 13529:856c2c294f84

Merge.
author Christian Humer <christian.humer@gmail.com>
date Tue, 07 Jan 2014 18:53:04 +0100
parents 0fbee3eb71f0
children
comparison
equal deleted inserted replaced
13528:5a0c694ef735 13529:856c2c294f84
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 java.io.*;
13
14 import com.oracle.truffle.api.*;
15 import com.oracle.truffle.api.dsl.*;
16 import com.oracle.truffle.api.frame.*;
17 import com.oracle.truffle.ruby.runtime.*;
18 import com.oracle.truffle.ruby.runtime.core.*;
19 import com.oracle.truffle.ruby.runtime.core.array.*;
20
21 @CoreClass(name = "File")
22 public abstract class FileNodes {
23
24 @CoreMethod(names = "absolute_path", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
25 public abstract static class AbsolutePathNode extends CoreMethodNode {
26
27 public AbsolutePathNode(RubyContext context, SourceSection sourceSection) {
28 super(context, sourceSection);
29 }
30
31 public AbsolutePathNode(AbsolutePathNode prev) {
32 super(prev);
33 }
34
35 @Specialization
36 public RubyString absolutePath(RubyString path) {
37 return getContext().makeString(new File(path.toString()).getAbsolutePath());
38 }
39
40 }
41
42 @CoreMethod(names = "close", maxArgs = 0)
43 public abstract static class CloseNode extends CoreMethodNode {
44
45 public CloseNode(RubyContext context, SourceSection sourceSection) {
46 super(context, sourceSection);
47 }
48
49 public CloseNode(CloseNode prev) {
50 super(prev);
51 }
52
53 @Specialization
54 public NilPlaceholder close(RubyFile file) {
55 file.close();
56 return NilPlaceholder.INSTANCE;
57 }
58
59 }
60
61 @CoreMethod(names = "directory?", isModuleMethod = true, needsSelf = false, maxArgs = 1)
62 public abstract static class DirectoryNode extends CoreMethodNode {
63
64 public DirectoryNode(RubyContext context, SourceSection sourceSection) {
65 super(context, sourceSection);
66 }
67
68 public DirectoryNode(DirectoryNode prev) {
69 super(prev);
70 }
71
72 @Specialization
73 public boolean directory(RubyString path) {
74 return new File(path.toString()).isDirectory();
75 }
76
77 }
78
79 @CoreMethod(names = "dirname", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
80 public abstract static class DirnameNode extends CoreMethodNode {
81
82 public DirnameNode(RubyContext context, SourceSection sourceSection) {
83 super(context, sourceSection);
84 }
85
86 public DirnameNode(DirnameNode prev) {
87 super(prev);
88 }
89
90 @Specialization
91 public RubyString dirname(RubyString path) {
92 final String parent = new File(path.toString()).getParent();
93
94 if (parent == null) {
95 return getContext().makeString(".");
96 } else {
97 return getContext().makeString(parent);
98 }
99 }
100
101 }
102
103 @CoreMethod(names = "each_line", needsBlock = true, maxArgs = 0)
104 public abstract static class EachLineNode extends YieldingCoreMethodNode {
105
106 public EachLineNode(RubyContext context, SourceSection sourceSection) {
107 super(context, sourceSection);
108 }
109
110 public EachLineNode(EachLineNode prev) {
111 super(prev);
112 }
113
114 @Specialization
115 public NilPlaceholder eachLine(VirtualFrame frame, RubyFile file, RubyProc block) {
116 final RubyContext context = getContext();
117
118 // TODO(cs): this buffered reader may consume too much
119
120 final BufferedReader lineReader = new BufferedReader(file.getReader());
121
122 while (true) {
123 String line;
124
125 try {
126 line = lineReader.readLine();
127 } catch (IOException e) {
128 throw new RuntimeException(e);
129 }
130
131 if (line == null) {
132 break;
133 }
134
135 yield(frame, block, context.makeString(line));
136 }
137
138 return NilPlaceholder.INSTANCE;
139 }
140
141 }
142
143 @CoreMethod(names = {"exist?", "exists?"}, isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
144 public abstract static class ExistsNode extends CoreMethodNode {
145
146 public ExistsNode(RubyContext context, SourceSection sourceSection) {
147 super(context, sourceSection);
148 }
149
150 public ExistsNode(ExistsNode prev) {
151 super(prev);
152 }
153
154 @Specialization
155 public boolean exists(RubyString path) {
156 return new File(path.toString()).isFile();
157 }
158
159 }
160
161 @CoreMethod(names = "executable?", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
162 public abstract static class ExecutableNode extends CoreMethodNode {
163
164 public ExecutableNode(RubyContext context, SourceSection sourceSection) {
165 super(context, sourceSection);
166 }
167
168 public ExecutableNode(ExecutableNode prev) {
169 super(prev);
170 }
171
172 @Specialization
173 public boolean executable(RubyString path) {
174 return new File(path.toString()).canExecute();
175 }
176
177 }
178
179 @CoreMethod(names = "expand_path", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 2)
180 public abstract static class ExpandPathNode extends CoreMethodNode {
181
182 public ExpandPathNode(RubyContext context, SourceSection sourceSection) {
183 super(context, sourceSection);
184 }
185
186 public ExpandPathNode(ExpandPathNode prev) {
187 super(prev);
188 }
189
190 @Specialization
191 public RubyString expandPath(RubyString path, @SuppressWarnings("unused") UndefinedPlaceholder dir) {
192 return getContext().makeString(RubyFile.expandPath(path.toString()));
193 }
194
195 @Specialization
196 public RubyString expandPath(RubyString path, RubyString dir) {
197 return getContext().makeString(RubyFile.expandPath(path.toString(), dir.toString()));
198 }
199
200 }
201
202 @CoreMethod(names = "file?", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
203 public abstract static class FileNode extends CoreMethodNode {
204
205 public FileNode(RubyContext context, SourceSection sourceSection) {
206 super(context, sourceSection);
207 }
208
209 public FileNode(FileNode prev) {
210 super(prev);
211 }
212
213 @Specialization
214 public boolean file(RubyString path) {
215 return new File(path.toString()).isFile();
216 }
217
218 }
219
220 @CoreMethod(names = "join", isModuleMethod = true, needsSelf = false, isSplatted = true)
221 public abstract static class JoinNode extends CoreMethodNode {
222
223 public JoinNode(RubyContext context, SourceSection sourceSection) {
224 super(context, sourceSection);
225 }
226
227 public JoinNode(JoinNode prev) {
228 super(prev);
229 }
230
231 @Specialization
232 public RubyString join(Object[] parts) {
233 return getContext().makeString(RubyArray.join(parts, File.separator));
234 }
235 }
236
237 @CoreMethod(names = "open", isModuleMethod = true, needsSelf = false, needsBlock = true, minArgs = 2, maxArgs = 2)
238 public abstract static class OpenNode extends YieldingCoreMethodNode {
239
240 public OpenNode(RubyContext context, SourceSection sourceSection) {
241 super(context, sourceSection);
242 }
243
244 public OpenNode(OpenNode prev) {
245 super(prev);
246 }
247
248 @Specialization
249 public Object open(VirtualFrame frame, RubyString fileName, RubyString mode, RubyProc block) {
250 final RubyFile file = RubyFile.open(getContext(), fileName.toString(), mode.toString());
251
252 if (block != null) {
253 try {
254 yield(frame, block, file);
255 } finally {
256 file.close();
257 }
258 }
259
260 return file;
261 }
262
263 }
264
265 @CoreMethod(names = "write", maxArgs = 0)
266 public abstract static class WriteNode extends CoreMethodNode {
267
268 public WriteNode(RubyContext context, SourceSection sourceSection) {
269 super(context, sourceSection);
270 }
271
272 public WriteNode(WriteNode prev) {
273 super(prev);
274 }
275
276 @Specialization
277 public NilPlaceholder write(RubyFile file, RubyString string) {
278 try {
279 final Writer writer = file.getWriter();
280 writer.write(string.toString());
281 writer.flush();
282 } catch (IOException e) {
283 throw new RuntimeException(e);
284 }
285
286 return NilPlaceholder.INSTANCE;
287 }
288
289 }
290
291 }