Skip to content

Commit

Permalink
Change buildString to str, add types() in additiona to ids() for prop…
Browse files Browse the repository at this point in the history
…er printing

or types and non-types for error strings.
enebo committed Mar 9, 2018
1 parent 6e76d1f commit b8bd2f1
Showing 17 changed files with 147 additions and 130 deletions.
56 changes: 27 additions & 29 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -208,8 +208,9 @@
import static java.lang.invoke.MethodHandles.insertArguments;
import static java.lang.invoke.MethodType.methodType;
import static org.jruby.internal.runtime.GlobalVariable.Scope.GLOBAL;
import static org.jruby.util.RubyStringBuilder.buildString;
import static org.jruby.util.RubyStringBuilder.str;
import static org.jruby.util.RubyStringBuilder.ids;
import static org.jruby.util.RubyStringBuilder.types;

/**
* The Ruby object represents the top-level of a JRuby "instance" in a given VM.
@@ -1033,22 +1034,22 @@ public RubyClass defineClassUnder(String name, RubyClass superClass, ObjectAlloc
* A variation of defineClassUnder that allows passing in an array of
* supplementary call sites to improve dynamic invocation.
*
* @param name The name for the new class
* @param id The name for the new class as an ISO-8859_1 String (id-value)
* @param superClass The super class for the new class
* @param allocator An ObjectAllocator instance that can construct
* instances of the new class.
* @param parent The namespace under which to define the new class
* @param callSites The array of call sites to add
* @return The new class
*/
public RubyClass defineClassUnder(String name, RubyClass superClass, ObjectAllocator allocator, RubyModule parent, CallSite[] callSites) {
IRubyObject classObj = parent.getConstantAt(name);
public RubyClass defineClassUnder(String id, RubyClass superClass, ObjectAllocator allocator, RubyModule parent, CallSite[] callSites) {
IRubyObject classObj = parent.getConstantAt(id);

if (classObj != null) {
if (!(classObj instanceof RubyClass)) throw newTypeError(buildString(this, newSymbol(name), " is not a class"));
if (!(classObj instanceof RubyClass)) throw newTypeError(str(this, ids(this, id), " is not a class"));
RubyClass klazz = (RubyClass)classObj;
if (klazz.getSuperClass().getRealClass() != superClass) {
throw newNameError(buildString(this, newSymbol(name), " is already defined"), name);
throw newNameError(str(this, ids(this, id), " is already defined"), id);
}
// If we define a class in Ruby, but later want to allow it to be defined in Java,
// the allocator needs to be updated
@@ -1061,15 +1062,14 @@ public RubyClass defineClassUnder(String name, RubyClass superClass, ObjectAlloc
boolean parentIsObject = parent == objectClass;

if (superClass == null) {
IRubyObject className = parentIsObject ?
newSymbol(name) :
parent.toRubyString(getCurrentContext()).append(newString("::")).append(ids(this, name));
warnings.warn(ID.NO_SUPER_CLASS, buildString(this, "no super class for `", className, "', Object assumed"));
IRubyObject className = parentIsObject ? ids(this, id) :
parent.toRubyString(getCurrentContext()).append(newString("::")).append(ids(this, id));
warnings.warn(ID.NO_SUPER_CLASS, str(this, "no super class for `", className, "', Object assumed"));

superClass = objectClass;
}

return RubyClass.newClass(this, superClass, name, allocator, parent, !parentIsObject, callSites);
return RubyClass.newClass(this, superClass, id, allocator, parent, !parentIsObject, callSites);
}

/**
@@ -1102,12 +1102,10 @@ public RubyModule defineModuleUnder(String name, RubyModule parent) {
if (moduleObj != null ) {
if (moduleObj.isModule()) return (RubyModule)moduleObj;

ThreadContext context = getCurrentContext();
if (parentIsObject) {
throw newTypeError(buildString(this, moduleObj.getMetaClass().toRubyString(context), " is not a module"));
} else {
throw newTypeError(buildString(this, parent.toRubyString(context), "::", moduleObj.getMetaClass().toRubyString(context), " is not a module"));
}
RubyString typeName = parentIsObject ?
types(this, moduleObj.getMetaClass()) : types(this, parent, moduleObj.getMetaClass());

throw newTypeError(str(this, typeName, " is not a module"));
}

return RubyModule.newModule(this, name, parent, !parentIsObject);
@@ -1117,15 +1115,15 @@ public RubyModule defineModuleUnder(String name, RubyModule parent) {
* From Object, retrieve the named module. If it doesn't exist a
* new module is created.
*
* @param name The name of the module
* @param id The name of the module
* @returns The existing or new module
*/
public RubyModule getOrCreateModule(String name) {
IRubyObject module = objectClass.getConstantAt(name);
public RubyModule getOrCreateModule(String id) {
IRubyObject module = objectClass.getConstantAt(id);
if (module == null) {
module = defineModule(name);
module = defineModule(id);
} else if (!module.isModule()) {
throw newTypeError(buildString(this, newSymbol(name), " is not a Module"));
throw newTypeError(str(this, ids(this, id), " is not a Module"));
}

return (RubyModule) module;
@@ -2863,7 +2861,7 @@ public PrintStream getOutputStream() {

public RubyModule getClassFromPath(final String path) {
if (path.length() == 0 || path.charAt(0) == '#') {
throw newTypeError(buildString(this, "can't retrieve anonymous class ", newSymbol(path)));
throw newTypeError(str(this, "can't retrieve anonymous class ", ids(this, path)));
}

RubyModule c = getObject();
@@ -2875,14 +2873,14 @@ public RubyModule getClassFromPath(final String path) {

if ( p < l && path.charAt(p) == ':' ) {
if ( ++p < l && path.charAt(p) != ':' ) {
throw newTypeError(buildString(this, "undefined class/module ", newSymbol(str)));
throw newTypeError(str(this, "undefined class/module ", ids(this, str)));
}
pbeg = ++p;
}

IRubyObject cc = c.getConstant(str);
if ( ! ( cc instanceof RubyModule ) ) {
throw newTypeError(buildString(this, newSymbol(path), " does not refer to class/module"));
throw newTypeError(str(this, ids(this, path), " does not refer to class/module"));
}
c = (RubyModule) cc;
}
@@ -3591,7 +3589,7 @@ public RaiseException newArgumentError(int got, int expected) {
}

public RaiseException newArgumentError(String name, int got, int expected) {
return newRaiseException(getArgumentError(), buildString(this, "wrong number of arguments calling `", newSymbol(name), ("` (" + got + " for " + expected + ")")));
return newRaiseException(getArgumentError(), str(this, "wrong number of arguments calling `", ids(this, name), ("` (" + got + " for " + expected + ")")));
}

public RaiseException newErrnoEBADFError() {
@@ -4094,12 +4092,12 @@ public RaiseException newFrozenError(String objectType) {
}

public RaiseException newFrozenError(RubyString type) {
return newRaiseException(getRuntimeError(), buildString(this, "can't modify frozen ", ids(this, type)));
return newRaiseException(getRuntimeError(), str(this, "can't modify frozen ", ids(this, type)));
}

public RaiseException newFrozenError(String objectType, boolean runtimeError) {
// TODO: Should frozen error have its own distinct class? If not should more share?
return newRaiseException(getRuntimeError(), buildString(this, "can't modify frozen ", ids(this, objectType)));
return newRaiseException(getRuntimeError(), str(this, "can't modify frozen ", ids(this, objectType)));
}

public RaiseException newSystemStackError(String message) {
@@ -4148,7 +4146,7 @@ public RaiseException newTypeError(IRubyObject receivedObject, RubyModule expect

public RaiseException newTypeError(IRubyObject receivedObject, String expectedType) {
return newRaiseException(getTypeError(),
buildString(this, "wrong argument type ",
str(this, "wrong argument type ",
receivedObject.getMetaClass().getRealClass().toRubyString(getCurrentContext()),
" (expected ", ids(this, expectedType), ")"));
}
7 changes: 4 additions & 3 deletions core/src/main/java/org/jruby/RubyArray.java
Original file line number Diff line number Diff line change
@@ -88,7 +88,8 @@
import static org.jruby.runtime.Helpers.hashEnd;
import static org.jruby.runtime.Helpers.murmurCombine;
import static org.jruby.runtime.Visibility.PRIVATE;
import static org.jruby.util.RubyStringBuilder.buildString;
import static org.jruby.util.RubyStringBuilder.str;
import static org.jruby.util.RubyStringBuilder.types;

/**
* The implementation of the built-in class Array in Ruby.
@@ -2361,7 +2362,7 @@ private int bsearch_index_internal(ThreadContext context, Block block) {
case -1: smaller = false;
}
} else {
throw runtime.newTypeError(buildString(runtime, "wrong argument type ", v.getType().toRubyString(context), " (must be numeric, true, false or nil"));
throw runtime.newTypeError(str(runtime, "wrong argument type ", types(runtime, v.getType()), " (must be numeric, true, false or nil"));
}
if (smaller) {
high = mid;
@@ -4744,7 +4745,7 @@ public RubyString pack(ThreadContext context, IRubyObject obj, IRubyObject maybe
if (!opts.isNil()) {
buffer = ArgsUtil.extractKeywordArg(context, "buffer", opts);
if (!buffer.isNil() && !(buffer instanceof RubyString)) {
throw runtime.newTypeError(buildString(runtime, "buffer must be String, not ", buffer.getType().toRubyString(context)));
throw runtime.newTypeError(str(runtime, "buffer must be String, not ", types(runtime, buffer.getType())));
}
}

28 changes: 14 additions & 14 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
@@ -81,7 +81,9 @@
import static org.jruby.runtime.invokedynamic.MethodNames.OP_CMP;
import static org.jruby.runtime.invokedynamic.MethodNames.EQL;
import static org.jruby.runtime.invokedynamic.MethodNames.INSPECT;
import static org.jruby.util.RubyStringBuilder.buildString;
import static org.jruby.util.RubyStringBuilder.ids;
import static org.jruby.util.RubyStringBuilder.str;
import static org.jruby.util.RubyStringBuilder.types;
import static org.jruby.util.io.EncodingUtils.encStrBufCat;

import org.jruby.runtime.ivars.VariableTableManager;
@@ -618,7 +620,7 @@ public final boolean respondsTo(String name) {
return respondTo.call(context, this, metaClass, respondName, mname).isTrue();
}
if ( arity.required() != 2 ) {
throw runtime.newArgumentError(buildString(runtime, runtime.newSymbol(respondName), " must accept 1 or 2 arguments (requires " + arity.getValue() + ")"));
throw runtime.newArgumentError(str(runtime, ids(runtime, respondName), " must accept 1 or 2 arguments (requires " + arity.getValue() + ")"));
}
}

@@ -682,7 +684,7 @@ public Class getJavaClass() {
public String asJavaString() {
IRubyObject str = checkStringType();
if (!str.isNil()) return ((RubyString) str).asJavaString();
throw getRuntime().newTypeError(buildString(getRuntime(), inspect(), " is not a string"));
throw getRuntime().newTypeError(str(getRuntime(), inspect(), " is not a string"));
}

/** rb_obj_as_string
@@ -753,7 +755,7 @@ public RubyInteger convertToInteger() {
IRubyObject result = TypeConverter.convertToType(context, this, runtime.getInteger(), sites.to_int_checked, true);

if (!(result instanceof RubyInteger)) {
throw getRuntime().newTypeError(buildString(runtime, getMetaClass().rubyName(), "#to_int should return Integer"));
throw getRuntime().newTypeError(str(runtime, types(runtime, getMetaClass()), "#to_int should return Integer"));
}

return (RubyInteger) result;
@@ -769,18 +771,17 @@ public RubyInteger convertToInteger(String convertMethod) {
if (convertMethod.equals("to_int")) return convertToInteger();

IRubyObject result;
Ruby runtime = getRuntime();
if (convertMethod.equals("to_i")) {
Ruby runtime = getRuntime();
ThreadContext context = runtime.getCurrentContext();
BasicObjectSites sites = sites(context);
result = TypeConverter.convertToType(context, this, runtime.getInteger(), sites.to_i_checked, true);
} else {
result = TypeConverter.convertToType(this, getRuntime().getInteger(), convertMethod, true);
result = TypeConverter.convertToType(this, runtime.getInteger(), convertMethod, true);
}

if (!(result instanceof RubyInteger)) {
Ruby runtime = getRuntime();
throw runtime.newTypeError(buildString(runtime, getMetaClass().rubyName(), "#", runtime.newSymbol(convertMethod), " should return Integer"));
throw runtime.newTypeError(str(runtime, types(runtime, getMetaClass()), "#", ids(runtime, convertMethod), " should return Integer"));
}

return (RubyInteger) result;
@@ -987,7 +988,7 @@ public IRubyObject rbClone(ThreadContext context, IRubyObject maybeOpts) {
if (!opts.isNil()) {
IRubyObject freeze = ((RubyHash) opts).fastARef(runtime.newSymbol("freeze"));
if (freeze != null && freeze != runtime.getTrue() && freeze != runtime.getFalse()) {
throw runtime.newArgumentError(buildString(runtime, "unexpected value for freeze: ", freeze.getType().rubyName()));
throw runtime.newArgumentError(str(runtime, "unexpected value for freeze: ", types(runtime, freeze.getType())));
}
if (freeze != null) {
kwfreeze = freeze.isTrue();
@@ -1001,9 +1002,8 @@ private IRubyObject rbCloneInternal(ThreadContext context, boolean freeze) {
Ruby runtime = context.runtime;

if (isSpecialObject()) {
if (!freeze) {
throw runtime.newArgumentError(buildString(runtime, "can't unfreeze ", getType().rubyName()));
}
if (!freeze) throw runtime.newArgumentError(str(runtime, "can't unfreeze ", types(runtime, getType())));

return this;
}

@@ -2488,7 +2488,7 @@ public RubyArray singleton_methods(ThreadContext context, IRubyObject[] args) {

public IRubyObject singleton_method(IRubyObject name) {
RubySymbol symbol = TypeConverter.checkID(name);
final String methodName = symbol.asJavaString();
final String methodName = symbol.getRawString();
final RubyClass klass = metaClass;
if (klass.isSingleton()) {
DynamicMethod method = klass.searchMethod(methodName);
@@ -2498,7 +2498,7 @@ public IRubyObject singleton_method(IRubyObject name) {
return newMethod;
}
}
throw getRuntime().newNameError(buildString(getRuntime(), "undefined method `", symbol, "' for `", inspect(), "'"), methodName);
throw getRuntime().newNameError(str(getRuntime(), "undefined method `", symbol, "' for `", inspect(), "'"), methodName);
}

/** rb_obj_method
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/RubyComplex.java
Original file line number Diff line number Diff line change
@@ -74,7 +74,8 @@
import static org.jruby.runtime.Helpers.invokedynamic;
import static org.jruby.runtime.invokedynamic.MethodNames.HASH;
import static org.jruby.util.Numeric.safe_mul;
import static org.jruby.util.RubyStringBuilder.buildString;
import static org.jruby.util.RubyStringBuilder.str;
import static org.jruby.util.RubyStringBuilder.types;

/**
* complex.c as of revision: 20011
@@ -697,14 +698,14 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
*/
@JRubyMethod(name = "coerce")
public IRubyObject coerce(ThreadContext context, IRubyObject other) {
if (other instanceof RubyNumeric && f_real_p(context, (RubyNumeric) other)) {
if (other instanceof RubyNumeric && f_real_p(context, other)) {
return context.runtime.newArray(newComplexBang(context, getMetaClass(), (RubyNumeric) other), this);
}

Ruby runtime = context.runtime;
if (other instanceof RubyComplex) return runtime.newArray(other, this);
if (other instanceof RubyComplex) return context.runtime.newArray(other, this);

throw runtime.newTypeError(buildString(runtime, other.getMetaClass().toRubyString(context), " can't be coerced into ", getMetaClass().toRubyString(context)));
Ruby runtime = context.runtime;
throw runtime.newTypeError(str(runtime, types(runtime, other.getMetaClass()), " can't be coerced into ", types(runtime, getMetaClass())));
}

/** nucomp_abs
@@ -1119,7 +1120,7 @@ private static RubyNumeric convertString(ThreadContext context, final IRubyObjec
private static RubyNumeric str_to_c_strict(ThreadContext context, RubyString str) {
IRubyObject[] ary = str_to_c_internal(context, str);
if (ary[0] == context.nil || ary[1].convertToString().getByteList().length() > 0) {
throw context.runtime.newArgumentError(buildString(context.runtime, "invalid value for convert(): ", str.callMethod(context, "inspect")));
throw context.runtime.newArgumentError(str(context.runtime, "invalid value for convert(): ", str.callMethod(context, "inspect")));

}
return (RubyNumeric) ary[0]; // (RubyComplex)
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
@@ -44,13 +44,13 @@
import org.jruby.api.API;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.JavaSites.IOSites;
import org.jruby.runtime.callsite.RespondToCallSite;
import org.jruby.util.IOChannel;
import org.jruby.util.StringSupport;
import org.jruby.util.io.ChannelFD;
import org.jruby.util.io.EncodingUtils;

import static org.jruby.util.RubyStringBuilder.buildString;
import static org.jruby.util.RubyStringBuilder.str;
import static org.jruby.util.RubyStringBuilder.types;
import static org.jruby.util.io.EncodingUtils.vmodeVperm;
import static org.jruby.util.io.EncodingUtils.vperm;

@@ -885,10 +885,10 @@ public static IRubyObject newInstance(ThreadContext context, IRubyObject recv, I
RubyClass klass = (RubyClass)recv;

if (block.isGiven()) {
IRubyObject className = klass.rubyName();
context.runtime.getWarnings().warn(
ID.BLOCK_NOT_ACCEPTED,
buildString(context.runtime, className, "::new() does not take block; use ", className, "::open() instead"));
IRubyObject className = types(context.runtime, klass);

context.runtime.getWarnings().warn(ID.BLOCK_NOT_ACCEPTED,
str(context.runtime, className, "::new() does not take block; use ", className, "::open() instead"));
}

return klass.newInstance(context, args, block);
Loading

0 comments on commit b8bd2f1

Please sign in to comment.