Skip to content

Commit

Permalink
Merge branch 'master' into truffle-head
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Apr 2, 2015
2 parents 7be2875 + 9fe48f1 commit fc954bc
Show file tree
Hide file tree
Showing 93 changed files with 1,348 additions and 1,411 deletions.
7 changes: 3 additions & 4 deletions core/src/main/java/org/jruby/RubyDir.java
Expand Up @@ -154,9 +154,8 @@ public IRubyObject initialize19(ThreadContext context, IRubyObject arg) {
private static List<ByteList> dirGlobs(ThreadContext context, String cwd, IRubyObject[] args, int flags) {
List<ByteList> dirs = new ArrayList<ByteList>();

POSIX posix = context.runtime.getPosix();
for (int i = 0; i < args.length; i++) {
dirs.addAll(Dir.push_glob(posix, cwd, globArgumentAsByteList(context, args[i]), flags));
dirs.addAll(Dir.push_glob(context.runtime, cwd, globArgumentAsByteList(context, args[i]), flags));
}

return dirs;
Expand Down Expand Up @@ -195,7 +194,7 @@ public static IRubyObject aref(ThreadContext context, IRubyObject recv, IRubyObj
Ruby runtime = context.runtime;
List<ByteList> dirs;
if (args.length == 1) {
dirs = Dir.push_glob(runtime.getPosix(), getCWD(runtime), globArgumentAsByteList(context, args[0]), 0);
dirs = Dir.push_glob(runtime, getCWD(runtime), globArgumentAsByteList(context, args[0]), 0);
} else {
dirs = dirGlobs(context, getCWD(runtime), args, 0);
}
Expand All @@ -221,7 +220,7 @@ public static IRubyObject glob(ThreadContext context, IRubyObject recv, IRubyObj
List<ByteList> dirs;
IRubyObject tmp = args[0].checkArrayType();
if (tmp.isNil()) {
dirs = Dir.push_glob(runtime.getPosix(), runtime.getCurrentDirectory(), globArgumentAsByteList(context, args[0]), flags);
dirs = Dir.push_glob(runtime, runtime.getCurrentDirectory(), globArgumentAsByteList(context, args[0]), flags);
} else {
dirs = dirGlobs(context, getCWD(runtime), ((RubyArray) tmp).toJavaArray(), flags);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFileStat.java
Expand Up @@ -134,7 +134,7 @@ private void setup(String filename, boolean lstat) {
filename += "/";
}

file = JRubyFile.createResource(runtime.getPosix(), runtime.getCurrentDirectory(), filename);
file = JRubyFile.createResource(runtime, filename);
stat = lstat ? file.lstat() : file.stat();

if (stat == null) throw runtime.newErrnoFromInt(file.errno(), filename);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyFileTest.java
Expand Up @@ -466,7 +466,7 @@ private static RubyFileStat getRubyFileStat(ThreadContext context, IRubyObject f
RubyFileStat stat = null;
if (!(filename instanceof RubyFile)) {
RubyString path = get_path(context, filename);
FileResource file = JRubyFile.createResource(runtime.getPosix(), runtime.getCurrentDirectory(), path.getUnicodeValue());
FileResource file = JRubyFile.createResource(runtime, path.getUnicodeValue());
if (file.exists()) {
stat = runtime.newFileStat(file.absolutePath(), false);
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/RubyIO.java
Expand Up @@ -514,7 +514,7 @@ protected RubyIO reopenIO(ThreadContext context, RubyIO nfile) {
}

private void checkReopenCloexecDup2(Ruby runtime, OpenFile orig, ChannelFD oldfd, ChannelFD newfd) {
OpenFile.cloexecDup2(new PosixShim(runtime.getPosix()), oldfd, newfd);
OpenFile.cloexecDup2(new PosixShim(runtime), oldfd, newfd);
}

// rb_io_binmode
Expand Down Expand Up @@ -1239,7 +1239,7 @@ private static ChannelFD cloexecOpen(Ruby runtime, Sysopen data)
// #elif defined O_NOINHERIT
// flags |= O_NOINHERIT;
// #endif
PosixShim shim = new PosixShim(runtime.getPosix());
PosixShim shim = new PosixShim(runtime);
ret = shim.open(runtime.getCurrentDirectory(), data.fname, ModeFlags.createModeFlags(data.oflags), data.perm);
if (ret == null) {
data.errno = shim.errno;
Expand Down Expand Up @@ -3967,7 +3967,7 @@ public static IRubyObject pipe19(ThreadContext context, IRubyObject klass, IRuby
}
}

PosixShim posix = new PosixShim(runtime.getPosix());
PosixShim posix = new PosixShim(runtime);
Channel[] fds = posix.pipe();
if (fds == null)
throw runtime.newErrnoFromErrno(posix.errno, "opening pipe");
Expand Down
225 changes: 5 additions & 220 deletions core/src/main/java/org/jruby/RubyString.java
Expand Up @@ -50,7 +50,6 @@
import org.jcodings.specific.UTF32LEEncoding;
import org.jcodings.specific.UTF8Encoding;
import org.jcodings.unicode.UnicodeEncoding;
import org.jcodings.util.IntHash;
import org.joni.Matcher;
import org.joni.Option;
import org.joni.Regex;
Expand Down Expand Up @@ -91,7 +90,6 @@
import static org.jruby.util.StringSupport.MBCLEN_CHARFOUND_P;
import static org.jruby.util.StringSupport.MBCLEN_INVALID_P;
import static org.jruby.util.StringSupport.MBCLEN_NEEDMORE_P;
import static org.jruby.util.StringSupport.TRANS_SIZE;
import static org.jruby.util.StringSupport.codeLength;
import static org.jruby.util.StringSupport.codePoint;
import static org.jruby.util.StringSupport.codeRangeScan;
Expand Down Expand Up @@ -914,6 +912,7 @@ public final void modify19() {
clearCodeRange();
}

@Override
public void modifyAndKeepCodeRange() {
modify();
keepCodeRange();
Expand Down Expand Up @@ -4850,231 +4849,17 @@ private IRubyObject trTrans19(ThreadContext context, IRubyObject src, IRubyObjec
RubyString replStr = repl.convertToString();
ByteList replList = replStr.value;
RubyString srcStr = src.convertToString();
ByteList srcList = srcStr.value;

if (value.getRealSize() == 0) return runtime.getNil();
if (replList.getRealSize() == 0) return delete_bang19(context, src);

int cr = getCodeRange();
Encoding e1 = checkEncoding(srcStr);
Encoding e2 = checkEncoding(replStr);
Encoding enc = e1 == e2 ? e1 : srcStr.checkEncoding(replStr);

final StringSupport.TR trSrc = new StringSupport.TR(srcList);
boolean cflag = false;
int[] l = {0};

if (value.getRealSize() > 1 &&
EncodingUtils.encAscget(trSrc.buf, trSrc.p, trSrc.pend, l, enc) == '^' &&
trSrc.p + 1 < trSrc.pend){
cflag = true;
trSrc.p++;
}

int c, c0, last = 0;
final int[]trans = new int[StringSupport.TRANS_SIZE];
final StringSupport.TR trRepl = new StringSupport.TR(replList);
boolean modify = false;
IntHash<Integer> hash = null;
boolean singlebyte = singleByteOptimizable();

if (cflag) {
for (int i=0; i< StringSupport.TRANS_SIZE; i++) {
trans[i] = 1;
}

while ((c = StringSupport.trNext(trSrc, runtime, enc)) != -1) {
if (c < StringSupport.TRANS_SIZE) {
trans[c] = -1;
} else {
if (hash == null) hash = new IntHash<Integer>();
hash.put(c, 1); // QTRUE
}
}
while ((c = StringSupport.trNext(trRepl, runtime, enc)) != -1) {} /* retrieve last replacer */
last = trRepl.now;
for (int i=0; i< StringSupport.TRANS_SIZE; i++) {
if (trans[i] != -1) {
trans[i] = last;
}
}
} else {
for (int i=0; i< StringSupport.TRANS_SIZE; i++) {
trans[i] = -1;
}

while ((c = StringSupport.trNext(trSrc, runtime, enc)) != -1) {
int r = StringSupport.trNext(trRepl, runtime, enc);
if (r == -1) r = trRepl.now;
if (c < StringSupport.TRANS_SIZE) {
trans[c] = r;
if (codeLength(enc, r) != 1) singlebyte = false;
} else {
if (hash == null) hash = new IntHash<Integer>();
hash.put(c, r);
}
}
}

if (cr == CR_VALID) {
cr = CR_7BIT;
}
modifyAndKeepCodeRange();
int s = value.getBegin();
int send = s + value.getRealSize();
byte sbytes[] = value.getUnsafeBytes();

if (sflag) {
int clen, tlen;
int max = value.getRealSize();
int save = -1;
byte[] buf = new byte[max];
int t = 0;
while (s < send) {
boolean mayModify = false;
c0 = c = codePoint(runtime, e1, sbytes, s, send);
clen = codeLength(e1, c);
tlen = enc == e1 ? clen : codeLength(enc, c);
s += clen;

if (c < TRANS_SIZE) {
c = trCode(c, trans, hash, cflag, last, false);
} else if (hash != null) {
Integer tmp = hash.get(c);
if (tmp == null) {
if (cflag) {
c = last;
} else {
c = -1;
}
} else if (cflag) {
c = -1;
} else {
c = tmp;
}
} else {
c = -1;
}
CodeRangeable ret = StringSupport.trTransHelper(runtime, this, srcStr, replStr, sflag);

if (c != -1) {
if (save == c) {
if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
continue;
}
save = c;
tlen = codeLength(enc, c);
modify = true;
} else {
save = -1;
c = c0;
if (enc != e1) mayModify = true;
}

while (t + tlen >= max) {
max *= 2;
buf = Arrays.copyOf(buf, max);
}
enc.codeToMbc(c, buf, t);
// MRI does not check s < send again because their null terminator can still be compared
if (mayModify && (s >= send || ByteList.memcmp(sbytes, s, buf, t, tlen) != 0)) modify = true;
if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
t += tlen;
}
value.setUnsafeBytes(buf);
value.setRealSize(t);
} else if (enc.isSingleByte() || (singlebyte && hash == null)) {
while (s < send) {
c = sbytes[s] & 0xff;
if (trans[c] != -1) {
if (!cflag) {
c = trans[c];
sbytes[s] = (byte)c;
} else {
sbytes[s] = (byte)last;
}
modify = true;
}
if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
s++;
}
} else {
int clen, tlen, max = (int)(value.realSize() * 1.2);
byte[] buf = new byte[max];
int t = 0;

while (s < send) {
boolean mayModify = false;
c0 = c = codePoint(runtime, e1, sbytes, s, send);
clen = codeLength(e1, c);
tlen = enc == e1 ? clen : codeLength(enc, c);

if (c < TRANS_SIZE) {
c = trans[c];
} else if (hash != null) {
Integer tmp = hash.get(c);
if (tmp == null) {
if (cflag) {
c = last;
} else {
c = -1;
}
} else if (cflag) {
c = -1;
} else {
c = tmp;
}
}
else {
c = cflag ? last : -1;
}
if (c != -1) {
tlen = codeLength(enc, c);
modify = true;
} else {
c = c0;
if (enc != e1) mayModify = true;
}
while (t + tlen >= max) {
max <<= 1;
buf = Arrays.copyOf(buf, max);
}
// headius: I don't see how s and t could ever be the same, since they refer to different buffers
// if (s != t) {
enc.codeToMbc(c, buf, t);
if (mayModify && ByteList.memcmp(sbytes, s, buf, t, tlen) != 0) {
modify = true;
}
// }

if (cr == CR_7BIT && !Encoding.isAscii(c)) cr = CR_VALID;
s += clen;
t += tlen;
}
value.setUnsafeBytes(buf);
value.setRealSize(t);
}

if (modify) {
if (cr != CR_BROKEN) setCodeRange(cr);
associateEncoding(enc);
return this;
if (ret == null) {
return runtime.getNil();
}
return runtime.getNil();
}

private int trCode(int c, int[]trans, IntHash<Integer> hash, boolean cflag, int last, boolean set) {
if (c < StringSupport.TRANS_SIZE) {
return trans[c];
} else if (hash != null) {
Integer tmp = hash.get(c);
if (tmp == null) {
return cflag ? last : -1;
} else {
return cflag ? -1 : tmp;
}
} else {
return cflag && set ? last : -1;
}
return (IRubyObject) ret;
}

/** rb_str_tr_s / rb_str_tr_s_bang
Expand Down
23 changes: 15 additions & 8 deletions core/src/main/java/org/jruby/runtime/load/LibrarySearcher.java
Expand Up @@ -15,7 +15,6 @@
import org.jruby.ast.executable.Script;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.load.LoadService.SuffixType;
import org.jruby.util.ClasspathResource;
import org.jruby.util.FileResource;
import org.jruby.util.JRubyFile;
import org.jruby.util.URLResource;
Expand Down Expand Up @@ -133,6 +132,7 @@ private FoundLibrary findResourceLibrary(String baseName, String suffix) {
return findFileResource(baseName, suffix);
}

// search the $LOAD_PATH
try {
for (IRubyObject loadPathEntry : loadService.loadPath.toJavaArray()) {
FoundLibrary library = findFileResourceWithLoadPath(baseName, suffix, getPath(loadPathEntry));
Expand All @@ -142,7 +142,19 @@ private FoundLibrary findResourceLibrary(String baseName, String suffix) {
t.printStackTrace();
}

return null;
// inside a classloader the path "." is the place where to find the jruby kernel
if (!runtime.getCurrentDirectory().startsWith(URLResource.URI_CLASSLOADER)) {

// ruby does not load a relative path unless the current working directory is in $LOAD_PATH
FoundLibrary library = findFileResourceWithLoadPath(baseName, suffix, ".");

// we did not find the file on the $LOAD_PATH but in current directory so we need to treat it
// as not found (the classloader search below will find it otherwise)
if (library != null) return null;
}

// load the jruby kernel and all resource added to $CLASSPATH
return findFileResourceWithLoadPath(baseName, suffix, URLResource.URI_CLASSLOADER);
}

// FIXME: to_path should not be called n times it should only be once and that means a cache which would
Expand Down Expand Up @@ -263,12 +275,7 @@ private void loadClass(Ruby runtime, InputStream is, boolean wrap) {
private void loadJar(Ruby runtime, boolean wrap) {
try {
URL url;
if (location.startsWith(ClasspathResource.CLASSPATH)){
// get URL directly from the classloader with its StreamHandler set
// by the classloader itself
url = ClasspathResource.getResourceURL(location);
}
else if (location.startsWith(URLResource.URI)){
if (location.startsWith(URLResource.URI)){
url = null;
runtime.getJRubyClassLoader().addURLNoIndex(URLResource.getResourceURL(runtime, location));
}
Expand Down

0 comments on commit fc954bc

Please sign in to comment.