Refine search
@JRubyMethod(name = "==") public IRubyObject eq(ThreadContext context, IRubyObject other) { Ruby runtime = context.runtime; if (!(other instanceof RubyMessage)) return runtime.getFalse(); RubyMessage message = (RubyMessage) other; if (descriptor != message.descriptor) { return runtime.getFalse(); } for (Descriptors.FieldDescriptor fdef : descriptor.getFields()) { IRubyObject thisVal = getField(context, fdef); IRubyObject thatVal = message.getField(context, fdef); IRubyObject ret = thisVal.callMethod(context, "==", thatVal); if (!ret.isTrue()) { return runtime.getFalse(); } } return runtime.getTrue(); }
public static int num2uint(IRubyObject value) { long longVal = RubyNumeric.num2long(value); if (longVal > UINT_MAX) throw value.getRuntime().newRangeError("Integer " + longVal + " too big to convert to 'unsigned int'"); long num = longVal; if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) // encode to UINT32 num = (-longVal ^ (-1l >>> 32) ) + 1; RubyNumeric.checkInt(value, num); return (int) num; }
@JRubyMethod(optional = 1) public IRubyObject initialize(final ThreadContext context, IRubyObject[] args) { final Ruby runtime = context.runtime; this.cRepeatedField = (RubyClass) runtime.getClassFromPath("Google::Protobuf::RepeatedField"); this.cMap = (RubyClass) runtime.getClassFromPath("Google::Protobuf::Map"); this.builder = DynamicMessage.newBuilder(this.descriptor); this.repeatedFields = new HashMap<Descriptors.FieldDescriptor, RubyRepeatedField>(); if (args.length == 1) { if (!(args[0] instanceof RubyHash)) { throw runtime.newArgumentError("expected Hash arguments."); RubyHash hash = args[0].convertToHash(); hash.visitAll(new RubyHash.Visitor() { @Override
@JRubyMethod(name = "method_missing", rest = true) public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) { if (args.length == 1) { RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]); if (oneofDescriptor.isNil()) { if (!hasField(args[0])) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return index(context, args[0]); } RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor; Descriptors.FieldDescriptor fieldDescriptor = oneofCases.get(rubyOneofDescriptor.getOneofDescriptor()); if (fieldDescriptor == null) return context.runtime.getNil(); return context.runtime.newSymbol(fieldDescriptor.getName()); } else { // fieldName is RubySymbol RubyString field = args[0].asString(); RubyString equalSign = context.runtime.newString(Utils.EQUAL_SIGN); if (field.end_with_p(context, equalSign).isTrue()) { field.chomp_bang(context, equalSign); } if (!hasField(field)) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return indexSet(context, field, args[1]); } }
case UINT64: if (!isRubyNum(value)) { throw runtime.newTypeError("Expected number type for integral field."); case FLOAT: if (!isRubyNum(value)) throw runtime.newTypeError("Expected number type for float field."); break; case DOUBLE: if (!isRubyNum(value)) throw runtime.newTypeError("Expected number type for double field."); break; case BOOL: break; case MESSAGE: if (value.getMetaClass() != typeClass) { throw runtime.newTypeError(value, typeClass); if (value instanceof RubySymbol) { Descriptors.EnumDescriptor enumDescriptor = ((RubyEnumDescriptor) typeClass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR)).getDescriptor(); val = enumDescriptor.findValueByName(value.asJavaString()); if (val == null) throw runtime.newRangeError("Enum value " + value + " is not found.");
@JRubyMethod(name = "digest", required = 1, rest = true, meta = true) public static IRubyObject s_digest(ThreadContext ctx, IRubyObject recv, IRubyObject[] args, Block unusedBlock) { Ruby runtime = recv.getRuntime(); if (args.length < 1) { throw runtime.newArgumentError("no data given"); } RubyString str = args[0].convertToString(); IRubyObject[] newArgs = new IRubyObject[args.length - 1]; System.arraycopy(args, 1, newArgs, 0, args.length - 1); IRubyObject obj = ((RubyClass)recv).newInstance(ctx, newArgs, Block.NULL_BLOCK); return obj.callMethod(ctx, "digest", str); }
public static RubyString typeAsString(IRubyObject obj) { if (obj.isNil()) return obj.getRuntime().newString("nil"); if (obj instanceof RubyBoolean) return obj.getRuntime().newString(obj.isTrue() ? "true" : "false"); return obj.getMetaClass().getRealClass().rubyName(); }
IRubyObject valueType = args[2]; IRubyObject number = args[3]; IRubyObject typeClass = args.length > 4 ? args[4] : context.runtime.getNil(); keyType.equals(RubySymbol.newSymbol(runtime, "enum")) || keyType.equals(RubySymbol.newSymbol(runtime, "message"))) throw runtime.newArgumentError("Cannot add a map field with a float, double, enum, or message type."); RubyDescriptor mapentryDesc = (RubyDescriptor) cDescriptor.newInstance(context, Block.NULL_BLOCK); IRubyObject mapentryDescName = RubySymbol.newSymbol(runtime, name).id2name(context); mapentryDesc.setName(context, mapentryDescName); RubyFieldDescriptor keyField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); keyField.setName(context, runtime.newString("key")); keyField.setLabel(context, RubySymbol.newSymbol(runtime, "optional")); keyField.setNumber(context, runtime.newFixnum(1)); RubyFieldDescriptor valueField = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); valueField.setName(context, runtime.newString("value")); valueField.setLabel(context, RubySymbol.newSymbol(runtime, "optional")); valueField.setNumber(context, runtime.newFixnum(2)); valueField.setType(context, valueType); if (! typeClass.isNil()) valueField.setSubmsgName(context, typeClass); mapentryDesc.addField(context, valueField);
public int read() throws IOException { if (in != null) { return in.read(); } final Ruby runtime = io.getRuntime(); IRubyObject readValue = readAdapter.call(runtime.getCurrentContext(), io, io, numOne); if (readValue.isNil()) return -1; return readValue.convertToString().getByteList().get(0) & 0xff; }
@Deprecated public static IRubyObject convertToTypeWithCheck(IRubyObject obj, RubyClass target, int convertMethodIndex, String convertMethod) { if (target.isInstance(obj)) return obj; IRubyObject val = TypeConverter.convertToType(obj, target, convertMethod, false); if (val.isNil()) return val; if (!target.isInstance(val)) { Ruby runtime = obj.getRuntime(); throw runtime.newTypeError(str(runtime, types(runtime, obj.getMetaClass()), "#" + convertMethod + " should return ", types(runtime, target))); } return val; }
protected IRubyObject setField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor, IRubyObject value) { if (Utils.isMapEntry(fieldDescriptor)) { if (!(value instanceof RubyMap)) { throw context.runtime.newTypeError("Expected Map instance"); addRepeatedField(fieldDescriptor, (RubyRepeatedField) value); } else { RubyArray ary = value.convertToArray(); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, ary); addRepeatedField(fieldDescriptor, repeatedField); fields.remove(oneofCase); if (value.isNil()) { oneofCases.remove(oneofDescriptor); fields.remove(fieldDescriptor); IRubyObject typeClass = context.runtime.getObject(); boolean addValue = true; if (fieldType == Descriptors.FieldDescriptor.Type.MESSAGE) { typeClass = ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context); if (value.isNil()){ addValue = false; Descriptors.EnumValueDescriptor val = enumDescriptor.findValueByNumberCreatingIfUnknown(RubyNumeric.num2int(value)); if (val.getIndex() != -1) value = context.runtime.newSymbol(val.getName());
private RubyModule buildModuleFromDescriptor(ThreadContext context) { Ruby runtime = context.runtime; Utils.checkNameAvailability(context, name.asJavaString()); RubyModule enumModule = RubyModule.newModule(runtime); for (Descriptors.EnumValueDescriptor value : descriptor.getValues()) { enumModule.defineConstant(value.getName(), runtime.newFixnum(value.getNumber())); } enumModule.instance_variable_set(runtime.newString(Utils.DESCRIPTOR_INSTANCE_VAR), this); enumModule.defineAnnotatedMethods(RubyEnum.class); return enumModule; }
@Override @JRubyMethod(name = "==") public RubyBoolean op_equal(ThreadContext context, IRubyObject other) { if (this == other) return context.tru; boolean equal = context.runtime.getException().isInstance(other) && getMetaClass().getRealClass() == other.getMetaClass().getRealClass() && callMethod(context, "message").equals(other.callMethod(context, "message")) && callMethod(context, "backtrace").equals(other.callMethod(context, "backtrace")); return context.runtime.newBoolean(equal); }
@JRubyMethod(name = "new", rest = true, meta = true) public static IRubyObject newInstance(IRubyObject recv, IRubyObject[] args, Block block) { IRubyObject obj = ((RubyClass)recv).allocate(); obj.callMethod(recv.getRuntime().getCurrentContext(), "initialize", args, block); return obj; }
/** rb_cmperr * */ public static IRubyObject cmperr(IRubyObject recv, IRubyObject other) { IRubyObject target; if (other.isImmediate() || !(other.isNil() || other.isTrue() || other == recv.getRuntime().getFalse())) { target = other.inspect(); } else { target = other.getType(); } throw recv.getRuntime().newArgumentError("comparison of " + recv.getType() + " with " + target + " failed"); }
final RubyBoolean respond_to_p(ThreadContext context, IRubyObject methodName, final boolean includePrivate) { final Ruby runtime = context.runtime; final String name = methodName.asJavaString(); if (getMetaClass().respondsToMethod(name, !includePrivate)) return runtime.getTrue(); // MRI (1.9) always passes down a symbol when calling respond_to_missing? if ( ! (methodName instanceof RubySymbol) ) methodName = runtime.newSymbol(name); IRubyObject respond = sites(context).respond_to_missing.call(context, this, this, methodName, runtime.newBoolean(includePrivate)); return runtime.newBoolean( respond.isTrue() ); }
private String formatMessage(final IRubyObject msg) { if (getRuntime().getString().equals(msg.getType())) { return msg.asJavaString(); } else if (getRuntime().getHash().equals(msg.getType())) { final RubyHash hash = (RubyHash) msg; return Objects.toString(hash.get(getRuntime().newSymbol(LOG_PROPERTY_TEXT))); } throw new IllegalArgumentException(Objects.toString(msg)); }
/** nurat_int_check * */ private static RubyInteger intCheck(ThreadContext context, IRubyObject num) { if (num instanceof RubyInteger) return (RubyInteger) num; if (!(num instanceof RubyNumeric) || !integer_p(context).call(context, num, num).isTrue()) { // num.integer? throw context.runtime.newTypeError("can't convert " + num.getMetaClass().getName() + " into Rational"); } return num.convertToInteger(); }
RandomType(IRubyObject seed) { this.seed = seed.convertToInteger(); if (this.seed instanceof RubyFixnum) { this.impl = randomFromFixnum((RubyFixnum) this.seed); } else if (this.seed instanceof RubyBignum) { this.impl = randomFromBignum((RubyBignum) this.seed); } else { throw seed.getRuntime().newTypeError( String.format("failed to convert %s into Integer", seed.getMetaClass().getName())); } }
@JRubyMethod(name = "==", compat = CompatVersion.RUBY1_9) @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { if (this == other) return context.runtime.getTrue(); boolean equal = context.runtime.getException().isInstance(other) && callMethod(context, "message").equals(other.callMethod(context, "message")) && callMethod(context, "backtrace").equals(other.callMethod(context, "backtrace")); if (context.runtime.is2_0()) { equal = equal && getMetaClass().getRealClass() == other.getMetaClass().getRealClass(); } return context.runtime.newBoolean(equal); }