Skip to content

Commit

Permalink
Showing 28 changed files with 450 additions and 682 deletions.
55 changes: 30 additions & 25 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -249,7 +249,18 @@ private Ruby(RubyInstanceConfig config) {

constant = OptoFactory.newConstantWrapper(Ruby.class, this);

getJRubyClassLoader(); // force JRubyClassLoader to init if possible
// force JRubyClassLoader to init if possible
if (!Ruby.isSecurityRestricted()) {
if (config.isClassloaderDelegate()){
jrubyClassLoader = new JRubyClassLoader(config.getLoader());
}
else {
jrubyClassLoader = new SelfFirstJRubyClassLoader(config.getLoader());
}
}
else {
jrubyClassLoader = null; // a NullClassLoader object would be better ...
}

this.staticScopeFactory = new StaticScopeFactory(this);
this.beanManager = BeanManagerFactory.create(this, config.isManagementEnabled());
@@ -2515,16 +2526,24 @@ public RubyClass getInvalidByteSequenceError() {
return invalidByteSequenceError;
}

private RubyRandom.RandomType defaultRand;
RubyRandom.RandomType defaultRand;

/**
* @deprecated internal API, to be hidden
*/
public RubyRandom.RandomType getDefaultRand() {
return defaultRand;
}

/**
* @deprecated internal API, to be hidden
*/
public void setDefaultRand(RubyRandom.RandomType defaultRand) {
this.defaultRand = defaultRand;
}

private RubyHash charsetMap;
@Deprecated // no longer used (internal API)
public RubyHash getCharsetMap() {
if (charsetMap == null) charsetMap = new RubyHash(this);
return charsetMap;
@@ -2586,24 +2605,7 @@ public static ClassLoader getClassLoader() {
return loader;
}

/**
* TODO the property {@link #jrubyClassLoader} will only be set in constructor. in the first call of
* {@link #getJRubyClassLoader() getJRubyClassLoader}. So the field {@link #jrubyClassLoader} can be final
* set in the constructor directly and we avoid the synchronized here.
*
* @return
*/
public synchronized JRubyClassLoader getJRubyClassLoader() {
// FIXME: Get rid of laziness and handle restricted access elsewhere
if (!Ruby.isSecurityRestricted() && jrubyClassLoader == null) {
if (config.isClassloaderDelegate()){
jrubyClassLoader = new JRubyClassLoader(config.getLoader());
}
else {
jrubyClassLoader = new SelfFirstJRubyClassLoader(config.getLoader());
}
}

public JRubyClassLoader getJRubyClassLoader() {
return jrubyClassLoader;
}

@@ -3336,9 +3338,9 @@ public void tearDown(boolean systemExit) {
* release the runtime loader but not otherwise - you should do that manually.
*/
public void releaseClassLoader() {
if ( jrubyClassLoader != null ) {
getJRubyClassLoader().close();
jrubyClassLoader = null;
if (jrubyClassLoader != null) {
jrubyClassLoader.close();
//jrubyClassLoader = null;
}
}

@@ -4575,6 +4577,9 @@ public CoverageData getCoverageData() {
return coverageData;
}

/**
* @deprecated internal API, to be removed
*/
public Random getRandom() {
return random;
}
@@ -4954,7 +4959,7 @@ private MRIRecursionGuard oldRecursionGuard() {

// Java support
private JavaSupport javaSupport;
private JRubyClassLoader jrubyClassLoader;
private final JRubyClassLoader jrubyClassLoader;

// Management/monitoring
private BeanManager beanManager;
@@ -5090,7 +5095,7 @@ private MRIRecursionGuard oldRecursionGuard() {
private static ThreadLocal<Ruby> threadLocalRuntime = new ThreadLocal<Ruby>();

/** The runtime-local random number generator. Uses SecureRandom if permissions allow. */
private final Random random;
final Random random;

/** The runtime-local seed for hash randomization */
private final long hashSeedK0;
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
@@ -260,6 +260,7 @@ protected RubyBasicObject(Ruby runtime, RubyClass metaClass, boolean useObjectSp
runtime.addToObjectSpace(useObjectSpace, this);
}

@Deprecated
protected void taint(Ruby runtime) {
if (!isTaint()) {
testFrozen();
47 changes: 23 additions & 24 deletions core/src/main/java/org/jruby/RubyComplex.java
Original file line number Diff line number Diff line change
@@ -1021,15 +1021,18 @@ public IRubyObject infinite_p(ThreadContext context) {
return sites(context).infinite.call(context, magnitude, magnitude);
}

private static final ByteList SEP = RubyFile.SLASH;
private static final ByteList _eE = new ByteList(new byte[] { '.', 'e', 'E' }, false);

static RubyArray str_to_c_internal(ThreadContext context, IRubyObject recv) {
RubyString s = recv.convertToString();
ByteList bytes = s.getByteList();

Ruby runtime = context.runtime;
if (bytes.getRealSize() == 0) return runtime.newArray(runtime.getNil(), recv);
if (bytes.getRealSize() == 0) return runtime.newArray(context.nil, recv);

IRubyObject sr, si, re;
sr = si = re = runtime.getNil();
sr = si = re = context.nil;
boolean po = false;
IRubyObject m = RubyRegexp.newDummyRegexp(runtime, Numeric.ComplexPatterns.comp_pat0).match_m(context, s, false);

@@ -1059,15 +1062,15 @@ static RubyArray str_to_c_internal(ThreadContext context, IRubyObject recv) {

if (m.isNil()) {
m = RubyRegexp.newDummyRegexp(runtime, Numeric.ComplexPatterns.comp_pat2).match_m(context, s, false);
if (m.isNil()) return runtime.newArray(runtime.getNil(), recv);
if (m.isNil()) return runtime.newArray(context.nil, recv);
RubyMatchData match = (RubyMatchData)m;
sr = match.at(1);
if (match.at(2).isNil()) {
si = runtime.getNil();
si = context.nil;
} else {
si = match.at(3);
IRubyObject t = match.at(4);
if (t.isNil()) t = runtime.newString(RubyFixnum.SINGLE_CHAR_BYTELISTS19['1']);
if (t.isNil()) t = runtime.newString(RubyInteger.singleCharByteList((byte) '1'));
si.convertToString().cat(t.convertToString().getByteList());
}
re = match.post_match(context);
@@ -1077,28 +1080,24 @@ static RubyArray str_to_c_internal(ThreadContext context, IRubyObject recv) {
IRubyObject r = RubyFixnum.zero(runtime);
IRubyObject i = r;

if (!sr.isNil()) {
if (sr.callMethod(context, "include?", runtime.newString(new ByteList(new byte[]{'/'}))).isTrue()) {
r = f_to_r(context, sr);
} else if (f_gt_p(context, sr.callMethod(context, "count", runtime.newString(".eE")), RubyFixnum.zero(runtime)).isTrue()) {
r = f_to_f(context, sr);
} else {
r = f_to_i(context, sr);
}
}
r = convertString(context, sr, r);
i = convertString(context, si, i);

if (!si.isNil()) {
if (si.callMethod(context, "include?", runtime.newString(new ByteList(new byte[]{'/'}))).isTrue()) {
i = f_to_r(context, si);
} else if (f_gt_p(context, si.callMethod(context, "count", runtime.newString(".eE")), RubyFixnum.zero(runtime)).isTrue()) {
i = f_to_f(context, si);
} else {
i = f_to_i(context, si);
}
}
return runtime.newArray(po ? newComplexPolar(context, r, i) : newComplexCanonicalize(context, r, i), re);
}


private static IRubyObject convertString(ThreadContext context, final IRubyObject s, IRubyObject def) {
if (s == context.nil) return def;
final Ruby runtime = context.runtime;
if (s.callMethod(context, "include?", RubyString.newStringShared(runtime, SEP)).isTrue()) {
return f_to_r(context, s);
}
if (f_gt_p(context, s.callMethod(context, "count", RubyString.newStringShared(runtime, _eE)), RubyFixnum.zero(runtime)).isTrue()) {
return f_to_f(context, s);
}
return f_to_i(context, s);
}

private static IRubyObject str_to_c_strict(ThreadContext context, IRubyObject recv) {
RubyArray a = str_to_c_internal(context, recv);
if (a.eltInternal(0).isNil() || a.eltInternal(1).convertToString().getByteList().length() > 0) {
10 changes: 7 additions & 3 deletions core/src/main/java/org/jruby/RubyFile.java
Original file line number Diff line number Diff line change
@@ -92,6 +92,10 @@
**/
@JRubyClass(name="File", parent="IO", include="FileTest")
public class RubyFile extends RubyIO implements EncodingCapable {

static final ByteList SLASH = new ByteList(new byte[] { '/' }, false);
static final ByteList BACKSLASH = new ByteList(new byte[] { '\\' }, false);

public static RubyClass createFileClass(Ruby runtime) {
ThreadContext context = runtime.getCurrentContext();

@@ -107,13 +111,13 @@ public static RubyClass createFileClass(Ruby runtime) {
fileClass.kindOf = new RubyModule.JavaClassKindOf(RubyFile.class);

// file separator constants
RubyString separator = RubyString.newString(runtime, new ByteList(new byte[] { '/' }, false));
RubyString separator = RubyString.newString(runtime, SLASH);
separator.freeze(context);
fileClass.defineConstant("SEPARATOR", separator);
fileClass.defineConstant("Separator", separator);

if (File.separatorChar == '\\') {
RubyString altSeparator = RubyString.newString(runtime, new ByteList(new byte[] { '\\' }, false));
RubyString altSeparator = RubyString.newString(runtime, BACKSLASH);
altSeparator.freeze(context);
fileClass.defineConstant("ALT_SEPARATOR", altSeparator);
} else {
@@ -1153,7 +1157,7 @@ public static IRubyObject utime(ThreadContext context, IRubyObject recv, IRubyOb
long[] atimeval = null;
long[] mtimeval = null;

if (args[0] != runtime.getNil() || args[1] != runtime.getNil()) {
if (args[0] != context.nil || args[1] != context.nil) {
atimeval = extractTimeval(context, args[0]);
mtimeval = extractTimeval(context, args[1]);
}
5 changes: 2 additions & 3 deletions core/src/main/java/org/jruby/RubyFloat.java
Original file line number Diff line number Diff line change
@@ -183,9 +183,8 @@ public RubyFloat convertToFloat() {
return this;
}

protected int compareValue(RubyNumeric other) {
double otherVal = other.getDoubleValue();
return getValue() > otherVal ? 1 : getValue() < otherVal ? -1 : 0;
public int signum() {
return (int) Math.signum(value); // NOTE: (int) NaN ?
}

public static RubyFloat newFloat(Ruby runtime, double value) {
16 changes: 5 additions & 11 deletions core/src/main/java/org/jruby/RubyGenerator.java
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ public RubyGenerator(Ruby runtime, RubyClass klass) {
public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;

IRubyObject proc;
final IRubyObject proc;

if (args.length == 0) {
proc = RubyProc.newProc(runtime, block, Block.Type.PROC);
@@ -76,7 +76,9 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block b
}
}

return init(runtime, proc);
// generator_init
this.proc = proc;
return this;
}

// generator_init_copy
@@ -96,15 +98,7 @@ public IRubyObject initialize_copy(ThreadContext context, IRubyObject other) {
// generator_each
@JRubyMethod(rest = true)
public IRubyObject each(ThreadContext context, IRubyObject[] args, Block block) {
return ((RubyProc) proc).call19(context, ArraySupport.newCopy(RubyYielder.newYielder(context, block), args), Block.NULL_BLOCK);
}


// generator_init
private IRubyObject init(Ruby runtime, IRubyObject proc) {
this.proc = proc;

return this;
return ((RubyProc) proc).call(context, ArraySupport.newCopy(RubyYielder.newYielder(context, block), args), Block.NULL_BLOCK);
}

private IRubyObject proc;
14 changes: 7 additions & 7 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
@@ -1073,7 +1073,7 @@ public IRubyObject internal_encoding(ThreadContext context) {
public IRubyObject set_encoding(ThreadContext context, IRubyObject encodingObj) {
setEncoding(context, encodingObj, context.nil, context.nil);

return context.nil;
return this;
}

@JRubyMethod
@@ -1085,14 +1085,14 @@ public IRubyObject set_encoding(ThreadContext context, IRubyObject encodingStrin
setEncoding(context, encodingString, internalEncoding, context.nil);
}

return context.nil;
return this;
}

@JRubyMethod
public IRubyObject set_encoding(ThreadContext context, IRubyObject encodingString, IRubyObject internalEncoding, IRubyObject options) {
setEncoding(context, encodingString, internalEncoding, options);

return context.nil;
return this;
}

// mri: io_encoding_set
@@ -1642,9 +1642,9 @@ public IRubyObject printf(ThreadContext context, IRubyObject[] args) {
public IRubyObject putc(ThreadContext context, IRubyObject ch) {
IRubyObject str;
if (ch instanceof RubyString) {
str = ((RubyString)ch).substr(context.runtime, 0, 1);
str = ((RubyString) ch).substr(context.runtime, 0, 1);
} else {
str = RubyString.newStringShared(context.runtime, RubyFixnum.SINGLE_CHAR_BYTELISTS19[RubyNumeric.num2chr(ch) & 0xFF]);
str = RubyString.newStringShared(context.runtime, RubyInteger.singleCharByteList(RubyNumeric.num2chr(ch)));
}

sites(context).write.call(context, this, this, str);
@@ -1654,10 +1654,10 @@ public IRubyObject putc(ThreadContext context, IRubyObject ch) {

public static IRubyObject putc(ThreadContext context, IRubyObject maybeIO, IRubyObject object) {
if (maybeIO instanceof RubyIO) {
((RubyIO)maybeIO).putc(context, object);
((RubyIO) maybeIO).putc(context, object);
} else {
byte c = RubyNumeric.num2chr(object);
IRubyObject str = RubyString.newStringShared(context.runtime, RubyFixnum.SINGLE_CHAR_BYTELISTS19[c & 0xFF]);
IRubyObject str = RubyString.newStringShared(context.runtime, RubyInteger.singleCharByteList(c));
sites(context).write.call(context, maybeIO, maybeIO, str);
}

29 changes: 14 additions & 15 deletions core/src/main/java/org/jruby/RubyInteger.java
Original file line number Diff line number Diff line change
@@ -298,20 +298,20 @@ public IRubyObject succ(ThreadContext context) {
}

static final ByteList[] SINGLE_CHAR_BYTELISTS;
@Deprecated
public static final ByteList[] SINGLE_CHAR_BYTELISTS19;
static {
SINGLE_CHAR_BYTELISTS = new ByteList[256];
SINGLE_CHAR_BYTELISTS19 = new ByteList[256];
for (int i = 0; i < 256; i++) {
ByteList usascii = new ByteList(new byte[]{(byte)i}, false);
SINGLE_CHAR_BYTELISTS[i] = usascii;
SINGLE_CHAR_BYTELISTS19[i] = i < 0x80 ?
new ByteList(new byte[]{(byte)i}, USASCIIEncoding.INSTANCE)
:
new ByteList(
new byte[]{(byte)i},
ASCIIEncoding.INSTANCE);
ByteList bytes = new ByteList(new byte[] { (byte) i }, false);
SINGLE_CHAR_BYTELISTS[i] = bytes;
bytes.setEncoding(i < 0x80 ? USASCIIEncoding.INSTANCE : ASCIIEncoding.INSTANCE);
}
SINGLE_CHAR_BYTELISTS19 = SINGLE_CHAR_BYTELISTS;
}

static ByteList singleCharByteList(final byte index) {
return SINGLE_CHAR_BYTELISTS[index & 0xFF];
}

/** int_chr
@@ -320,18 +320,17 @@ public IRubyObject succ(ThreadContext context) {
@JRubyMethod(name = "chr")
public RubyString chr(ThreadContext context) {
Ruby runtime = context.runtime;
int value = (int)getLongValue();
int value = (int) getLongValue();
if (value >= 0 && value <= 0xFF) {
ByteList bytes = SINGLE_CHAR_BYTELISTS19[value];
ByteList bytes = SINGLE_CHAR_BYTELISTS[value];
return RubyString.newStringShared(runtime, bytes, bytes.getEncoding());
} else {
Encoding enc = runtime.getDefaultInternalEncoding();
if (value > 0xFF && (enc == null || enc == ASCIIEncoding.INSTANCE)) {
throw runtime.newRangeError(this.toString() + " out of char range");
} else {
if (enc == null) enc = USASCIIEncoding.INSTANCE;
return RubyString.newStringNoCopy(runtime, fromEncodedBytes(runtime, enc, value), enc, 0);
throw runtime.newRangeError(toString() + " out of char range");
}
if (enc == null) enc = USASCIIEncoding.INSTANCE;
return RubyString.newStringNoCopy(runtime, fromEncodedBytes(runtime, enc, value), enc, 0);
}
}

Loading

0 comments on commit 3860d6f

Please sign in to comment.