Skip to content

Commit 9de830f

Browse files
committed
[Truffle] Add Dir#{each,foreach,home}.
1 parent 1457605 commit 9de830f

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
fails:Dir#each yields each directory entry in succession
21
fails:Dir#each returns the directory which remains open
3-
fails:Dir#each returns an Enumerator if no block given
42
fails:Dir#each raises an IOError when called on a closed Dir instance

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

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 0 additions & 3 deletions
This file was deleted.

truffle/src/main/java/org/jruby/truffle/nodes/core/ModuleNodes.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.oracle.truffle.api.source.Source;
2222
import com.oracle.truffle.api.source.SourceSection;
2323
import com.oracle.truffle.api.utilities.ConditionProfile;
24+
import jnr.posix.Passwd;
2425
import org.jcodings.Encoding;
2526
import org.jruby.runtime.Visibility;
2627
import org.jruby.truffle.nodes.RubyNode;
@@ -1800,6 +1801,27 @@ private RubyModule undefMethod(RubyModule module, String name) {
18001801

18011802
}
18021803

1804+
@CoreMethod(names = "get_user_home", needsSelf = false, required = 1)
1805+
public abstract static class GetUserHomeNode extends CoreMethodArrayArgumentsNode {
1806+
1807+
public GetUserHomeNode(RubyContext context, SourceSection sourceSection) {
1808+
super(context, sourceSection);
1809+
}
1810+
1811+
@Specialization
1812+
public RubyString userHome(RubyString uname) {
1813+
notDesignedForCompilation();
1814+
// TODO BJF 30-APR-2015 Review the more robust getHomeDirectoryPath implementation
1815+
final Passwd passwd = getContext().getPosix().getpwnam(uname.toString());
1816+
if (passwd == null) {
1817+
CompilerDirectives.transferToInterpreter();
1818+
throw new RaiseException(getContext().getCoreLibrary().argumentError("user " + uname.toString() + " does not exist", this));
1819+
}
1820+
return getContext().makeString(passwd.getHome());
1821+
}
1822+
1823+
}
1824+
18031825
@NodeChildren({ @NodeChild(value = "module"), @NodeChild(value = "names") })
18041826
public abstract static class SetVisibilityNode extends CoreMethodNode {
18051827

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,22 @@ def self.mkdir(path, mode = 0777)
121121
error
122122
end
123123

124+
def self.foreach(path)
125+
return to_enum(:foreach, path) unless block_given?
126+
127+
open(path) do |dir|
128+
while s = dir.read
129+
yield s
130+
end
131+
end
132+
133+
nil
134+
end
135+
136+
def self.join_path(p1, p2, dirsep)
137+
"#{p1}#{dirsep ? '/' : ''}#{p2}"
138+
end
139+
124140
def self.chdir(path = ENV['HOME'])
125141
path = Rubinius::Type.coerce_to_path path
126142

@@ -157,6 +173,16 @@ def self.getwd
157173
Rubinius::Type.external_string wd
158174
end
159175

176+
def each
177+
return to_enum unless block_given?
178+
179+
while s = read
180+
yield s
181+
end
182+
183+
self
184+
end
185+
160186
class << self
161187
alias_method :pwd, :getwd
162188
alias_method :delete, :rmdir
@@ -171,4 +197,8 @@ class << self
171197
alias_method :exists?, :exist?
172198
end
173199

200+
def self.home(user=nil)
201+
PrivateFile.expand_path("~#{user}")
202+
end
203+
174204
end

0 commit comments

Comments
 (0)