Skip to content

Commit ef1fc91

Browse files
committed
[Truffle] Add Dir#{pos,rewind,seek,tell}.
1 parent bc4f1f6 commit ef1fc91

File tree

7 files changed

+94
-10
lines changed

7 files changed

+94
-10
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
fails:Dir#each returns the directory which remains open
21
fails:Dir#each raises an IOError when called on a closed Dir instance
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
fails:Dir#pos returns an Integer representing the current position in the directory
2-
fails:Dir#pos returns a different Integer if moved from previous position
31
fails:Dir#pos raises an IOError when called on a closed Dir instance
4-
fails:Dir#pos= moves the read position to a previously obtained position
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
fails:Dir#rewind resets the next read to start from the first entry
2-
fails:Dir#rewind returns the Dir instance
31
fails:Dir#rewind raises an IOError when called on a closed Dir instance

spec/truffle/tags/core/dir/seek_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
fails:Dir#tell returns an Integer representing the current position in the directory
2-
fails:Dir#tell returns a different Integer if moved from previous position
31
fails:Dir#tell raises an IOError when called on a closed Dir instance

truffle/src/main/java/org/jruby/truffle/nodes/rubinius/DirPrimitiveNodes.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@
66
* Eclipse Public License version 1.0
77
* GNU General Public License version 2
88
* GNU Lesser General Public License version 2.1
9+
*
10+
* Some of the code in this class is transliterated from C++ code in Rubinius.
11+
*
12+
* Copyright (c) 2007-2014, Evan Phoenix and contributors
13+
* All rights reserved.
14+
*
15+
* Redistribution and use in source and binary forms, with or without
16+
* modification, are permitted provided that the following conditions are met:
17+
*
18+
* * Redistributions of source code must retain the above copyright notice, this
19+
* list of conditions and the following disclaimer.
20+
* * Redistributions in binary form must reproduce the above copyright notice
21+
* this list of conditions and the following disclaimer in the documentation
22+
* and/or other materials provided with the distribution.
23+
* * Neither the name of Rubinius nor the names of its contributors
24+
* may be used to endorse or promote products derived from this software
25+
* without specific prior written permission.
26+
*
27+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
31+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
937
*/
1038
package org.jruby.truffle.nodes.rubinius;
1139

@@ -136,6 +164,44 @@ public Object read(RubyBasicObject dir) {
136164

137165
}
138166

167+
168+
@RubiniusPrimitive(name = "dir_control")
169+
public static abstract class DirControlPrimitiveNode extends RubiniusPrimitiveNode {
170+
171+
@Child private ReadHeadObjectFieldNode readPositionNode;
172+
@Child private WriteHeadObjectFieldNode writePositionNode;
173+
174+
public DirControlPrimitiveNode(RubyContext context, SourceSection sourceSection) {
175+
super(context, sourceSection);
176+
readPositionNode = new ReadHeadObjectFieldNode(positionKey);
177+
writePositionNode = new WriteHeadObjectFieldNode(positionKey);
178+
}
179+
180+
@CompilerDirectives.TruffleBoundary
181+
@Specialization
182+
public Object control(RubyBasicObject dir, int kind, int position) {
183+
switch (kind) {
184+
case 0:
185+
writePositionNode.execute(dir, position);
186+
return true;
187+
case 1:
188+
writePositionNode.execute(dir, -2);
189+
return true;
190+
case 2:
191+
try {
192+
return readPositionNode.executeInteger(dir);
193+
} catch (UnexpectedResultException e) {
194+
throw new IllegalStateException();
195+
}
196+
197+
}
198+
return nil();
199+
}
200+
201+
}
202+
203+
204+
139205
@RubiniusPrimitive(name = "dir_close")
140206
public static abstract class DirClosePrimitiveNode extends RubiniusPrimitiveNode {
141207

truffle/src/main/ruby/core/rubinius/common/dir.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,34 @@ def each
183183
self
184184
end
185185

186+
SeekKind = 0
187+
RewindKind = 1
188+
TellKind = 2
189+
190+
def pos
191+
control TellKind, 0
192+
end
193+
194+
alias_method :tell, :pos
195+
196+
def pos=(position)
197+
seek(position)
198+
199+
position
200+
end
201+
202+
def seek(position)
203+
control SeekKind, position
204+
205+
self
206+
end
207+
208+
def rewind
209+
control RewindKind, 0
210+
211+
self
212+
end
213+
186214
class << self
187215
alias_method :pwd, :getwd
188216
alias_method :delete, :rmdir

0 commit comments

Comments
 (0)