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: ba0ad9ee8c5e
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 83c3ae5e90ba
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on Nov 22, 2016

  1. Copy the full SHA
    7a33035 View commit details
  2. Copy the full SHA
    83c3ae5 View commit details
12 changes: 11 additions & 1 deletion core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -1597,6 +1597,8 @@ private void initCore() {
}

TracePoint.createTracePointClass(this);

RubyWarnings.createWarningModule(this);
}

public static final int NIL_PREFILLED_ARRAY_SIZE = RubyArray.ARRAY_DEFAULT_SIZE * 8;
@@ -2411,6 +2413,14 @@ public void setLocation(RubyClass location) {
this.locationClass = location;
}

public RubyModule getWarning() {
return warningModule;
}

public void setWarning(RubyModule warningModule) {
this.warningModule = warningModule;
}

public RubyModule getErrno() {
return errnoModule;
}
@@ -5126,7 +5136,7 @@ public IRubyObject call(ThreadContext context, RecursiveFunction func, IRubyObje
kernelModule, comparableModule, enumerableModule, mathModule,
marshalModule, etcModule, fileTestModule, gcModule,
objectSpaceModule, processModule, procUIDModule, procGIDModule,
procSysModule, precisionModule, errnoModule;
procSysModule, precisionModule, errnoModule, warningModule;

private DynamicMethod privateMethodMissing, protectedMethodMissing, variableMethodMissing,
superMethodMissing, normalMethodMissing, defaultMethodMissing, defaultModuleMethodMissing,
5 changes: 5 additions & 0 deletions core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
@@ -1545,6 +1545,11 @@ private IRubyObject initialize(ThreadContext context, IRubyObject arg0, RubyHash

if (opts != null) {
IRubyObject encoding = opts.fastARef(context.runtime.newSymbol("encoding"));
IRubyObject capacity = opts.fastARef(context.runtime.newSymbol("capacity"));

if (!(capacity == null || capacity.isNil())) {
modify(capacity.convertToInteger().getIntValue());
}

if (!(encoding == null || encoding.isNil())) {
modify();
82 changes: 68 additions & 14 deletions core/src/main/java/org/jruby/common/RubyWarnings.java
Original file line number Diff line number Diff line change
@@ -31,10 +31,15 @@
import java.util.Set;
import org.joni.WarnCallback;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.backtrace.RubyStackTraceElement;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.TypeConverter;

/**
*
@@ -47,6 +52,17 @@ public RubyWarnings(Ruby runtime) {
this.runtime = runtime;
}

public static RubyModule createWarningModule(Ruby runtime) {
RubyModule warning = runtime.defineModule("Warning");

warning.defineAnnotatedMethods(RubyWarnings.class);
warning.extend_object(warning);

runtime.setWarning(warning);

return warning;
}

@Override
public void warn(String message) {
warn(ID.MISCELLANEOUS, message);
@@ -83,23 +99,26 @@ public void warn(ID id, String fileName, int lineNumber, String message) {

buffer.append(fileName).append(':').append(lineNumber).append(": ");
buffer.append("warning: ").append(message).append('\n');
IRubyObject errorStream = runtime.getGlobalVariables().get("$stderr");
errorStream.callMethod(runtime.getCurrentContext(), "write", runtime.newString(buffer.toString()));
RubyString errorString = runtime.newString(buffer.toString());

writeWarningDyncall(runtime.getCurrentContext(), errorString);
}

/**
* Prints a warning, unless $VERBOSE is nil.
*/
@Override
public void warn(ID id, String fileName, String message) {
if (!runtime.warningsEnabled()) return;
// MRI: rb_write_warning_str
public static void writeWarningDyncall(ThreadContext context, RubyString errorString) {
RubyModule warning = context.runtime.getWarning();

StringBuilder buffer = new StringBuilder(100);
sites(context).warn.call(context, warning, warning, errorString);
}

// MR: rb_write_error_str
public static void writeWarningToError(ThreadContext context, RubyString errorString) {
Ruby runtime = context.runtime;

buffer.append(fileName).append(' ');
buffer.append("warning: ").append(message).append('\n');
IRubyObject errorStream = runtime.getGlobalVariables().get("$stderr");
errorStream.callMethod(runtime.getCurrentContext(), "write", runtime.newString(buffer.toString()));
RubyModule warning = runtime.getWarning();

sites(context).write.call(context, warning, errorStream, errorString);
}

@Override
@@ -144,6 +163,10 @@ public void warning(String message) {
public void warning(ID id, String message) {
if (!runtime.warningsEnabled() || !runtime.isVerbose()) return;

writeWarning(runtime, id, message);
}

private static void writeWarning(Ruby runtime, ID id, String message) {
RubyStackTraceElement[] stack = getRubyStackTrace(runtime);
String file;
int line;
@@ -156,7 +179,7 @@ public void warning(ID id, String message) {
line = stack[0].getLineNumber();
}

warning(id, file, line, message);
runtime.getWarnings().warning(id, file, line, message);
}

/**
@@ -177,10 +200,41 @@ public void warning(ID id, String fileName, int lineNumber, String message) {
warn(id, fileName, lineNumber, message);
}

@JRubyMethod
public static IRubyObject warn(ThreadContext context, IRubyObject recv, IRubyObject arg) {
Ruby runtime = context.runtime;
TypeConverter.checkType(context, arg, runtime.getString());
RubyString str = (RubyString) arg;
if (!str.getEncoding().isAsciiCompatible()) {
throw runtime.newEncodingCompatibilityError("ASCII incompatible encoding: " + str.getEncoding());
}
writeWarningToError(runtime.getCurrentContext(), str);
return context.nil;
}

private static RubyStackTraceElement[] getRubyStackTrace(Ruby runtime) {
ThreadContext context = runtime.getCurrentContext();
RubyStackTraceElement[] stack = context.createWarningBacktrace(runtime);

return stack;
}

private static JavaSites.WarningSites sites(ThreadContext context) {
return context.sites.Warning;
}

/**
* Prints a warning, unless $VERBOSE is nil.
*/
@Override
@Deprecated
public void warn(ID id, String fileName, String message) {
if (!runtime.warningsEnabled()) return;

StringBuilder buffer = new StringBuilder(100);

buffer.append(fileName).append(' ');
buffer.append("warning: ").append(message).append('\n');
IRubyObject errorStream = runtime.getGlobalVariables().get("$stderr");
errorStream.callMethod(runtime.getCurrentContext(), "write", runtime.newString(buffer.toString()));
}
}
7 changes: 7 additions & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ public class JavaSites {
public final ComplexSites Complex = new ComplexSites();
public final RationalSites Rational = new RationalSites();
public final RangeSites Range = new RangeSites();
public final WarningSites Warning = new WarningSites();
public final ZlibSites Zlib = new ZlibSites();

public static class BasicObjectSites {
@@ -378,6 +379,12 @@ public static class RangeSites {
public final CallSite exclude_end = new FunctionalCachingCallSite("exclude_end?");
}

public static class WarningSites {
public final CheckedSites to_int_checked = new CheckedSites("to_str");
public final CallSite warn = new FunctionalCachingCallSite("warn");
public final CallSite write = new FunctionalCachingCallSite("write");
}

public static class ZlibSites {
public final RespondToCallSite reader_respond_to = new RespondToCallSite();
public final RespondToCallSite writer_respond_to = new RespondToCallSite();