Skip to content

Commit

Permalink
Add Pathname#glob based on CRuby.
Browse files Browse the repository at this point in the history
This does not yet pass MRI Pathname#glob test.
  • Loading branch information
headius committed Mar 19, 2018
1 parent 4cf9456 commit 9e4ba45
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
53 changes: 41 additions & 12 deletions core/src/main/java/org/jruby/ext/pathname/RubyPathname.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,12 @@

import static org.jruby.anno.FrameField.BACKREF;

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.internal.runtime.methods.JavaMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.*;
import org.jruby.runtime.builtin.IRubyObject;

@JRubyClass(name = "Pathname")
Expand Down Expand Up @@ -371,6 +361,41 @@ public static IRubyObject glob(ThreadContext context, IRubyObject recv, IRubyObj
}
}

@JRubyMethod(required = 1, optional = 1)
public IRubyObject glob(ThreadContext context, IRubyObject[] _args, Block block) {
Ruby runtime = context.runtime;

IRubyObject[] args = new IRubyObject[3];

args[0] = _args[0];
if (_args.length == 1) {
args[1] = RubyFixnum.zero(runtime);
} else {
args[1] = _args[1];
}

args[2] = RubyHash.newSmallHash(runtime);
((RubyHash) args[2]).fastASetSmall(runtime.newSymbol("base"), getPath());

JavaSites.PathnameSites sites = sites(context);
CallSite glob = sites.glob;

if (block.isGiven()) {
return glob.call(context, this, runtime.getDir(), args, block);
} else {
RubyArray ary;
long i;
ary = glob.call(context, this, runtime.getDir(), args).convertToArray();
CallSite op_plus = sites.op_plus;
for (i = 0; i < ary.size(); i++) {
IRubyObject elt = ary.eltOk(i);
elt = op_plus.call(context, this, this, elt);
ary.eltSetOk(i, elt);
}
return ary;
}
}

@JRubyMethod
public IRubyObject opendir(ThreadContext context, Block block) {
return context.runtime.getDir().callMethod(context, "open", new IRubyObject[] { getPath()},
Expand Down Expand Up @@ -445,4 +470,8 @@ private static RubyArray mapToPathnames(ThreadContext context, RubyClass clazz,
}
return paths;
}

private static JavaSites.PathnameSites sites(ThreadContext context) {
return context.sites.Pathname;
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class JavaSites {
public final TimeoutSites Timeout = new TimeoutSites();
public final ArgfSites Argf = new ArgfSites();
public final TracePointSites TracePoint = new TracePointSites();
public final PathnameSites Pathname = new PathnameSites();

public static class BasicObjectSites {
public final CallSite respond_to = new FunctionalCachingCallSite("respond_to?");
Expand Down Expand Up @@ -427,6 +428,11 @@ public static class TracePointSites {
public final CheckedSites to_sym = new CheckedSites("to_sym");
}

public static class PathnameSites {
public final CallSite glob = new FunctionalCachingCallSite("glob");
public final CallSite op_plus = new FunctionalCachingCallSite("+");
}

public static class CheckedSites {
public final RespondToCallSite respond_to_X;
public final CachingCallSite respond_to_missing = new FunctionalCachingCallSite("respond_to_missing?");
Expand Down

0 comments on commit 9e4ba45

Please sign in to comment.