Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: cfce27aef814
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 6baf780b27e3
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 7, 2018

  1. Copy the full SHA
    8cefd62 View commit details
  2. Copy the full SHA
    175f11e View commit details
  3. [refactor] avoid converting dir entries back to Java on Dir.children

    ... just to remove '.' and '..'
    kares committed Jun 7, 2018
    Copy the full SHA
    6baf780 View commit details
144 changes: 74 additions & 70 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
@@ -58,12 +59,7 @@
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.Dir;
import org.jruby.util.FileResource;
import org.jruby.util.JRubyFile;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;
import org.jruby.util.TypeConverter;
import org.jruby.util.*;
import org.jruby.ast.util.ArgsUtil;

/**
@@ -131,12 +127,8 @@ private final void checkDirIgnoreClosed() {
* <code>Dir</code> object returned, so a new <code>Dir</code> instance
* must be created to reflect changes to the underlying file system.
*/
public IRubyObject initialize(ThreadContext context, IRubyObject arg) {
return initialize19(context, arg);
}

@JRubyMethod(name = "initialize")
public IRubyObject initialize19(ThreadContext context, IRubyObject arg) {
public IRubyObject initialize(ThreadContext context, IRubyObject arg) {
Ruby runtime = context.runtime;
RubyString newPath = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg));
path = newPath;
@@ -151,10 +143,15 @@ public IRubyObject initialize19(ThreadContext context, IRubyObject arg) {
return this;
}

@Deprecated
public IRubyObject initialize19(ThreadContext context, IRubyObject arg) {
return initialize(context, arg);
}

// ----- Ruby Class Methods ----------------------------------------------------

private static ArrayList<ByteList> dirGlobs(ThreadContext context, String cwd, IRubyObject[] args, int flags) {
ArrayList<ByteList> dirs = new ArrayList<ByteList>();
ArrayList<ByteList> dirs = new ArrayList<>();

for ( int i = 0; i < args.length; i++ ) {
dirs.addAll(Dir.push_glob(context.runtime, cwd, globArgumentAsByteList(context, args[i]), flags));
@@ -192,14 +189,14 @@ private static String globOptions(ThreadContext context, IRubyObject[] args, int

if (args.length > 1) {
IRubyObject tmp = TypeConverter.checkHashType(runtime, args[args.length - 1]);
if (tmp.isNil()) {
if (tmp == context.nil) {
if (flags != null) {
flags[0] = RubyNumeric.num2int(args[1]);
}
} else {
String[] keys = flags != null ? new String[] {"base", "flags"} : new String[] {"base"};
IRubyObject[] rets = ArgsUtil.extractKeywordArgs(context, (RubyHash) tmp, keys);
String base = rets[0] == UNDEF || rets[0].isNil() ? "" : RubyFile.get_path(context, rets[0]).asJavaString();
String base = rets[0] == UNDEF || rets[0] == context.nil ? "" : RubyFile.get_path(context, rets[0]).asJavaString();

// Deep down in glob things are unhappy if base is not absolute.
if (!base.isEmpty()) {
@@ -232,8 +229,7 @@ public static IRubyObject aref(ThreadContext context, IRubyObject recv, IRubyObj
} else {
IRubyObject[] arefArgs;
if (base != null) { // kwargs are present
arefArgs = new IRubyObject[args.length - 1];
System.arraycopy(args, 0, arefArgs, 0, args.length - 1);
arefArgs = ArraySupport.newCopy(args, args.length - 1);
} else {
arefArgs = args;
base = "";
@@ -312,7 +308,7 @@ public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyOb

RubyString path = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, arg));

return entriesCommon(context, path.asJavaString(), runtime.getDefaultEncoding());
return entriesCommon(context, path.asJavaString(), runtime.getDefaultEncoding(), false);
}

@JRubyMethod(name = "entries", meta = true)
@@ -328,23 +324,28 @@ public static RubyArray entries(ThreadContext context, IRubyObject recv, IRubyOb
encoding = runtime.getEncodingService().getEncodingFromObject(encodingArg);
}
}
return entriesCommon(context, path.asJavaString(), encoding);
return entriesCommon(context, path.asJavaString(), encoding, false);
}

private static RubyArray entriesCommon(ThreadContext context, String path, Encoding encoding) {
private static RubyArray entriesCommon(ThreadContext context, String path, Encoding encoding, final boolean childrenOnly) {
Ruby runtime = context.runtime;
String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, path, null);
checkDirIsTwoSlashesOnWindows(runtime, adjustedPath);

FileResource directory = JRubyFile.createResource(context, path);
String[] files = getEntries(context, directory, adjustedPath);

return RubyArray.newArrayMayCopy(runtime, JavaUtil.convertStringArrayToRuby(runtime, files, new JavaUtil.StringConverter() {
public IRubyObject convert(Ruby runtime, Object object) {
if (object == null) return runtime.getNil();
return RubyString.newString(runtime, (String)object, encoding);
RubyArray result = RubyArray.newArray(runtime, files.length);
for (String file : files) {
if (childrenOnly) { // removeIf(f -> f.equals(".") || f.equals(".."));
final int len = file.length();
if (len == 1 && file.charAt(0) == '.') continue;
if (len == 2 && file.charAt(0) == '.' && file.charAt(1) == '.') continue;
}
}));

result.append( RubyString.newString(runtime, file, encoding) );
}
return result;
}

private static final String[] NO_FILES = StringSupport.EMPTY_STRING_ARRAY;
@@ -426,9 +427,16 @@ public static RubyArray children(ThreadContext context, IRubyObject recv, IRubyO
}

private static RubyArray childrenCommon(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject opts) {
RubyArray entries = entries(context, recv, arg, opts);
entries.removeIf(f -> f.equals(".") || f.equals(".."));
return entries;
Encoding encoding = null;
if (opts != context.nil) {
IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, "encoding", opts);
if (encodingArg != context.nil) {
encoding = context.runtime.getEncodingService().getEncodingFromObject(encodingArg);
}
}
if (encoding == null) encoding = context.runtime.getDefaultEncoding();

return entriesCommon(context, RubyFile.get_path(context, arg).asJavaString(), encoding, true);
}

/**
@@ -446,7 +454,7 @@ public static IRubyObject rmdir19(ThreadContext context, IRubyObject recv, IRuby
return rmdirCommon(runtime, cleanPath.asJavaString());
}

private static IRubyObject rmdirCommon(Ruby runtime, String path) {
private static RubyFixnum rmdirCommon(Ruby runtime, String path) {
JRubyFile directory = getDirForRmdir(runtime, path);

// at this point, only thing preventing delete should be non-emptiness
@@ -464,9 +472,7 @@ private static IRubyObject rmdirCommon(Ruby runtime, String path) {
*/
@JRubyMethod(name = "each_child", meta = true)
public static IRubyObject each_child(ThreadContext context, IRubyObject recv, IRubyObject arg, Block block) {
RubyString pathString = RubyFile.get_path(context, arg);

return eachChildCommon(context, recv, context.runtime, pathString, null, block);
return eachChildCommon(context, recv, RubyFile.get_path(context, arg), null, block);
}

/**
@@ -476,7 +482,6 @@ public static IRubyObject each_child(ThreadContext context, IRubyObject recv, IR
*/
@JRubyMethod(name = "each_child", meta = true)
public static IRubyObject each_child(ThreadContext context, IRubyObject recv, IRubyObject arg, IRubyObject enc, Block block) {
RubyString pathString = RubyFile.get_path(context, arg);
RubyEncoding encoding;

if (enc instanceof RubyEncoding) {
@@ -485,27 +490,20 @@ public static IRubyObject each_child(ThreadContext context, IRubyObject recv, IR
throw context.runtime.newTypeError(enc, context.runtime.getEncoding());
}

return eachChildCommon(context, recv, context.runtime, pathString, encoding, block);
return eachChildCommon(context, recv, RubyFile.get_path(context, arg), encoding, block);
}

/**
* Executes the block once for each file in the directory specified by
* <code>path</code>.
*/
public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRubyObject _path, Block block) {
return foreach19(context, recv, _path, block);
}

@JRubyMethod(name = "foreach", meta = true)
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject arg, Block block) {
RubyString pathString = RubyFile.get_path(context, arg);

return foreachCommon(context, recv, context.runtime, pathString, null, block);
public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
return foreachCommon(context, recv, RubyFile.get_path(context, path), null, block);
}

@JRubyMethod(name = "foreach", meta = true)
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject enc, Block block) {
RubyString pathString = RubyFile.get_path(context, path);
public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject enc, Block block) {
RubyEncoding encoding;

if (enc instanceof RubyEncoding) {
@@ -514,47 +512,53 @@ public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRu
throw context.runtime.newTypeError(enc, context.runtime.getEncoding());
}

return foreachCommon(context, recv, context.runtime, pathString, encoding, block);
return foreachCommon(context, recv, RubyFile.get_path(context, path), encoding, block);
}

@Deprecated
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
return foreach(context, recv, path, block);
}

private static IRubyObject eachChildCommon(ThreadContext context, IRubyObject recv, Ruby runtime, RubyString _path, RubyEncoding encoding, Block block) {
@Deprecated
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject enc, Block block) {
return foreach(context, recv, path, enc, block);
}

private static IRubyObject eachChildCommon(ThreadContext context, IRubyObject recv, RubyString path, RubyEncoding encoding, Block block) {
if (block.isGiven()) {
RubyClass dirClass = runtime.getDir();
RubyDir dir = (RubyDir) dirClass.newInstance(context, new IRubyObject[]{_path}, block);
RubyDir dir = (RubyDir) context.runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);

dir.each_child(context, block);
return runtime.getNil();
return context.nil;
}

if (encoding == null) {
return enumeratorize(runtime, recv, "each_child", _path);
} else {
return enumeratorize(runtime, recv, "each_child", new IRubyObject[]{_path, encoding});
return enumeratorize(context.runtime, recv, "each_child", path);
}
return enumeratorize(context.runtime, recv, "each_child", path, encoding);
}

private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv, Ruby runtime, RubyString _path, RubyEncoding encoding, Block block) {
private static IRubyObject foreachCommon(ThreadContext context, IRubyObject recv, RubyString path, RubyEncoding encoding, Block block) {
if (block.isGiven()) {
RubyClass dirClass = runtime.getDir();
RubyDir dir = (RubyDir) dirClass.newInstance(context, new IRubyObject[]{_path}, block);
RubyDir dir = (RubyDir) context.runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);

dir.each(context, block);
return runtime.getNil();
return context.nil;
}

if (encoding == null) {
return enumeratorize(runtime, recv, "foreach", _path);
} else {
return enumeratorize(runtime, recv, "foreach", new IRubyObject[]{_path, encoding});
return enumeratorize(context.runtime, recv, "foreach", path);
}
return enumeratorize(context.runtime, recv, "foreach", path, encoding);
}

/** Returns the current directory. */
@JRubyMethod(name = {"getwd", "pwd"}, meta = true)
public static RubyString getwd(IRubyObject recv) {
Ruby ruby = recv.getRuntime();
Ruby runtime = recv.getRuntime();

RubyString pwd = RubyString.newUnicodeString(ruby, getCWD(ruby));
RubyString pwd = RubyString.newUnicodeString(runtime, getCWD(runtime));
pwd.setTaint(true);
return pwd;
}
@@ -622,14 +626,9 @@ private static IRubyObject mkdirCommon(Ruby runtime, String path, IRubyObject[]
* provided, a new directory object is passed to the block, which closes the
* directory object before terminating.
*/
public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
return open19(context, recv, path, block);
}

@JRubyMethod(name = "open", meta = true)
public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
RubyDir directory = (RubyDir) context.runtime.getDir().newInstance(context,
new IRubyObject[] { RubyFile.get_path(context, path) }, Block.NULL_BLOCK);
public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
RubyDir directory = (RubyDir) context.runtime.getDir().newInstance(context, RubyFile.get_path(context, path), Block.NULL_BLOCK);

if (!block.isGiven()) return directory;

@@ -640,7 +639,13 @@ public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyO
}
}

@Deprecated
public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
return open(context, recv, path, block);
}

// ----- Ruby Instance Methods -------------------------------------------------

/**
* Closes the directory stream.
*/
@@ -907,8 +912,7 @@ protected static List<String> getContents(FileResource directory) {
// If an IO exception occurs (something odd, but possible)
// A directory may return null.
if (contents != null) {
result = new ArrayList<String>(contents.length);
Collections.addAll(result, contents);
result = Arrays.asList(contents);
}
else {
result = Collections.emptyList();
@@ -925,7 +929,7 @@ protected static List<RubyString> getContents(FileResource directory, Ruby runti

final List<RubyString> result;
if (contents != null) {
result = new ArrayList<RubyString>(contents.length);
result = new ArrayList<>(contents.length);
for (int i = 0; i < contents.length; i++) {
result.add( runtime.newString(contents[i]) );
}
Original file line number Diff line number Diff line change
@@ -51,9 +51,9 @@
import org.jruby.runtime.Block;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ASM;
import org.jruby.util.ClassDefiningClassLoader;
import org.jruby.util.ClassDefiningJRubyClassLoader;
import org.jruby.util.ClassLoaderAwareWriter;
import org.jruby.util.Loader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
@@ -175,7 +175,7 @@ public static Class defineOldStyleImplClass(final Ruby ruby, final String name,
newClass = loader.loadClass(name);
}
catch (ClassNotFoundException ex) {
ClassWriter cw = new ClassLoaderAwareWriter(loader.asClassLoader(), ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ClassWriter cw = ASM.newClassWriter(loader.asClassLoader());
String pathName = name.replace('.', '/');

// construct the class, implementing all supertypes
Original file line number Diff line number Diff line change
@@ -43,9 +43,9 @@

import org.jruby.Ruby;
import org.jruby.runtime.Helpers;
import org.jruby.util.ASM;
import org.jruby.util.ArraySupport;
import org.jruby.util.ClassDefiningClassLoader;
import org.jruby.util.ClassLoaderAwareWriter;
import org.jruby.util.OneShotClassLoader;
import org.jruby.util.cli.Options;
import org.jruby.util.log.Logger;
@@ -208,7 +208,7 @@ private static String targetClassName(final Class<?> clazz) {
private static ClassWriter beginProxyClass(final String className,
final Class superClass, final Class[] interfaces, final ClassDefiningClassLoader loader) {

ClassWriter cw = new ClassLoaderAwareWriter(loader.asClassLoader(), ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
ClassWriter cw = ASM.newClassWriter(loader.asClassLoader());

// start class
cw.visit(JAVA_VERSION, Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER,
111 changes: 111 additions & 0 deletions core/src/main/java/org/jruby/util/ASM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.util;

import org.objectweb.asm.ClassWriter;

/**
* ASM helpers for JRuby.
*
* @author kares
*/
public abstract class ASM {

private ASM() { /* no instances */ }

public static ClassWriter newClassWriter() {
return new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
}

public static ClassWriter newClassWriter(ClassLoader loader) {
return newClassWriter(loader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
}

public static ClassWriter newClassWriter(ClassLoader loader, final int flags) {
return new ClassLoaderAwareWriter(loader, flags);
}

/**
* NOTE: required to account for ASM calculating stack-map frames
* in which case it might need to know of JRuby's dynamically loaded classes
*
* @author kares
*/
private static class ClassLoaderAwareWriter extends ClassWriter {

private final ClassLoader loader;

ClassLoaderAwareWriter(final ClassLoader loader, final int flags) {
super(flags);
this.loader = loader != null ? loader : ASM.class.getClassLoader();
}

@Override
protected String getCommonSuperClass(String type1, String type2) {
try {
return getCommonSuperClassImpl(type1, type2, loader);
}
catch (TypeNotPresentException ex) {
throw ex;
}
}

private static String getCommonSuperClassImpl(String type1, String type2, final ClassLoader loader)
throws TypeNotPresentException {
Class<?> class1;
try {
class1 = Class.forName(type1.replace('/', '.'), false, loader);
}
catch (ClassNotFoundException|Error e) {
throw new TypeNotPresentException(type1, e);
}
Class<?> class2;
try {
class2 = Class.forName(type2.replace('/', '.'), false, loader);
}
catch (ClassNotFoundException|Error e) {
throw new TypeNotPresentException(type2, e);
}
if (class1.isAssignableFrom(class2)) {
return type1;
}
if (class2.isAssignableFrom(class1)) {
return type2;
}
if (class1.isInterface() || class2.isInterface()) {
return "java/lang/Object";
}
do {
class1 = class1.getSuperclass();
}
while (!class1.isAssignableFrom(class2));

return class1.getName().replace('.', '/');
}

}

}
93 changes: 0 additions & 93 deletions core/src/main/java/org/jruby/util/ClassLoaderAwareWriter.java

This file was deleted.