Skip to content

Commit 9e4ba45

Browse files
committedMar 19, 2018
Add Pathname#glob based on CRuby.
This does not yet pass MRI Pathname#glob test.
1 parent 4cf9456 commit 9e4ba45

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/ext/pathname/RubyPathname.java

+41-12
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,12 @@
3030

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

33-
import org.jruby.Ruby;
34-
import org.jruby.RubyArray;
35-
import org.jruby.RubyClass;
36-
import org.jruby.RubyFixnum;
37-
import org.jruby.RubyModule;
38-
import org.jruby.RubyObject;
39-
import org.jruby.RubyString;
33+
import org.jruby.*;
4034
import org.jruby.anno.JRubyClass;
4135
import org.jruby.anno.JRubyMethod;
4236
import org.jruby.exceptions.RaiseException;
4337
import org.jruby.internal.runtime.methods.JavaMethod;
44-
import org.jruby.runtime.Block;
45-
import org.jruby.runtime.Helpers;
46-
import org.jruby.runtime.ObjectAllocator;
47-
import org.jruby.runtime.ThreadContext;
48-
import org.jruby.runtime.Visibility;
38+
import org.jruby.runtime.*;
4939
import org.jruby.runtime.builtin.IRubyObject;
5040

5141
@JRubyClass(name = "Pathname")
@@ -371,6 +361,41 @@ public static IRubyObject glob(ThreadContext context, IRubyObject recv, IRubyObj
371361
}
372362
}
373363

364+
@JRubyMethod(required = 1, optional = 1)
365+
public IRubyObject glob(ThreadContext context, IRubyObject[] _args, Block block) {
366+
Ruby runtime = context.runtime;
367+
368+
IRubyObject[] args = new IRubyObject[3];
369+
370+
args[0] = _args[0];
371+
if (_args.length == 1) {
372+
args[1] = RubyFixnum.zero(runtime);
373+
} else {
374+
args[1] = _args[1];
375+
}
376+
377+
args[2] = RubyHash.newSmallHash(runtime);
378+
((RubyHash) args[2]).fastASetSmall(runtime.newSymbol("base"), getPath());
379+
380+
JavaSites.PathnameSites sites = sites(context);
381+
CallSite glob = sites.glob;
382+
383+
if (block.isGiven()) {
384+
return glob.call(context, this, runtime.getDir(), args, block);
385+
} else {
386+
RubyArray ary;
387+
long i;
388+
ary = glob.call(context, this, runtime.getDir(), args).convertToArray();
389+
CallSite op_plus = sites.op_plus;
390+
for (i = 0; i < ary.size(); i++) {
391+
IRubyObject elt = ary.eltOk(i);
392+
elt = op_plus.call(context, this, this, elt);
393+
ary.eltSetOk(i, elt);
394+
}
395+
return ary;
396+
}
397+
}
398+
374399
@JRubyMethod
375400
public IRubyObject opendir(ThreadContext context, Block block) {
376401
return context.runtime.getDir().callMethod(context, "open", new IRubyObject[] { getPath()},
@@ -445,4 +470,8 @@ private static RubyArray mapToPathnames(ThreadContext context, RubyClass clazz,
445470
}
446471
return paths;
447472
}
473+
474+
private static JavaSites.PathnameSites sites(ThreadContext context) {
475+
return context.sites.Pathname;
476+
}
448477
}

Diff for: ‎core/src/main/java/org/jruby/runtime/JavaSites.java

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class JavaSites {
4242
public final TimeoutSites Timeout = new TimeoutSites();
4343
public final ArgfSites Argf = new ArgfSites();
4444
public final TracePointSites TracePoint = new TracePointSites();
45+
public final PathnameSites Pathname = new PathnameSites();
4546

4647
public static class BasicObjectSites {
4748
public final CallSite respond_to = new FunctionalCachingCallSite("respond_to?");
@@ -427,6 +428,11 @@ public static class TracePointSites {
427428
public final CheckedSites to_sym = new CheckedSites("to_sym");
428429
}
429430

431+
public static class PathnameSites {
432+
public final CallSite glob = new FunctionalCachingCallSite("glob");
433+
public final CallSite op_plus = new FunctionalCachingCallSite("+");
434+
}
435+
430436
public static class CheckedSites {
431437
public final RespondToCallSite respond_to_X;
432438
public final CachingCallSite respond_to_missing = new FunctionalCachingCallSite("respond_to_missing?");

0 commit comments

Comments
 (0)
Please sign in to comment.