protected static void validateTypeClass(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { Ruby runtime = context.runtime; if (!(value instanceof RubyModule)) { throw runtime.newArgumentError("TypeClass has incorrect type"); } RubyModule klass = (RubyModule) value; IRubyObject descriptor = klass.getInstanceVariable(DESCRIPTOR_INSTANCE_VAR); if (descriptor.isNil()) { throw runtime.newArgumentError("Type class has no descriptor. Please pass a " + "class or enum as returned by the DescriptorPool."); } if (type == Descriptors.FieldDescriptor.Type.MESSAGE) { if (! (descriptor instanceof RubyDescriptor)) { throw runtime.newArgumentError("Descriptor has an incorrect type"); } } else if (type == Descriptors.FieldDescriptor.Type.ENUM) { if (! (descriptor instanceof RubyEnumDescriptor)) { throw runtime.newArgumentError("Descriptor has an incorrect type"); } } }
@JRubyMethod public IRubyObject concat(ThreadContext context, IRubyObject list) { if (list instanceof RubyArray) { checkArrayElementType(context, (RubyArray) list); this.storage.addAll((RubyArray) list); } else { RubyRepeatedField repeatedField = (RubyRepeatedField) list; if (! fieldType.equals(repeatedField.fieldType) || (typeClass != null && ! typeClass.equals(repeatedField.typeClass))) throw context.runtime.newArgumentError("Attempt to append RepeatedField with different element type."); this.storage.addAll((RubyArray) repeatedField.toArray(context)); } return this.storage; }
@JRubyMethod(required = 4, optional = 1) public IRubyObject map(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.runtime; 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."); valueField.setNumber(context, runtime.newFixnum(2)); valueField.setType(context, valueType); if (! typeClass.isNil()) valueField.setSubmsgName(context, typeClass); mapentryDesc.addField(context, valueField);
@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 Encoding toEncoding(ThreadContext context, IRubyObject enc) { RubyString encStr = enc.convertToString(); if (!encStr.getEncoding().isAsciiCompatible()) { throw context.runtime.newArgumentError("invalid name encoding (non ASCII)"); } Encoding idx = context.runtime.getEncodingService().getEncodingFromObject(encStr); // check for missing encoding is in getEncodingFromObject return idx; }
private static IRubyObject str_to_r_strict(ThreadContext context, IRubyObject recv) { RubyArray a = str_to_r_internal(context, recv); if (a.eltInternal(0).isNil() || a.eltInternal(1).convertToString().getByteList().length() > 0) { IRubyObject s = recv.callMethod(context, "inspect"); throw context.runtime.newArgumentError("invalid value for convert(): " + s.convertToString()); } return a.eltInternal(0); } }
@JRubyMethod(required = 1, optional = 2) public IRubyObject initialize(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.runtime; IRubyObject ary = null; if (!(args[0] instanceof RubySymbol)) { throw runtime.newArgumentError("Expected Symbol for type name"); || fieldType == Descriptors.FieldDescriptor.Type.ENUM) { if (args.length < 2) throw runtime.newArgumentError("Expected at least 2 arguments for message/enum"); typeClass = args[1]; if (args.length > 2) } else { if (args.length > 2) throw runtime.newArgumentError("Too many arguments: expected 1 or 2"); if (args.length > 1) ary = args[1];
@JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject clock_gettime(ThreadContext context, IRubyObject self, IRubyObject _clock_id, IRubyObject _unit) { Ruby runtime = context.runtime; if (!(_unit instanceof RubySymbol) && !_unit.isNil()) { throw runtime.newArgumentError("unexpected unit: " + _unit); } return makeClockResult(runtime, getTimeForClock(_clock_id, runtime), _unit.toString()); }
public static RubyFieldDescriptor msgdefCreateField(ThreadContext context, String label, IRubyObject name, IRubyObject type, IRubyObject number, IRubyObject typeClass, RubyClass cFieldDescriptor) { Ruby runtime = context.runtime; RubyFieldDescriptor fieldDef = (RubyFieldDescriptor) cFieldDescriptor.newInstance(context, Block.NULL_BLOCK); fieldDef.setLabel(context, runtime.newString(label)); fieldDef.setName(context, name); fieldDef.setType(context, type); fieldDef.setNumber(context, number); if (!typeClass.isNil()) { if (!(typeClass instanceof RubyString)) { throw runtime.newArgumentError("expected string for type class"); } fieldDef.setSubmsgName(context, typeClass); } return fieldDef; }
@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); }
private static RubyNumeric str_to_r_strict(ThreadContext context, RubyString str) { IRubyObject[] ary = str_to_r_internal(context, str); if (ary[0] == context.nil || ary[1].convertToString().getByteList().length() > 0) { throw context.runtime.newArgumentError("invalid value for convert(): " + str.inspect(context.runtime)); } return (RubyNumeric) ary[0]; // (RubyRational) }
private static IRubyObject str_to_r_strict(ThreadContext context, IRubyObject recv) { RubyArray a = str_to_r_internal(context, recv); if (a.eltInternal(0).isNil() || a.eltInternal(1).convertToString().getByteList().length() > 0) { IRubyObject s = recv.callMethod(context, "inspect"); throw context.runtime.newArgumentError("invalid value for convert(): " + s.convertToString()); } return a.eltInternal(0); } }
@JRubyMethod(name = {"+"}) public IRubyObject plus(ThreadContext context, IRubyObject list) { RubyRepeatedField dup = (RubyRepeatedField) dup(context); if (list instanceof RubyArray) { checkArrayElementType(context, (RubyArray) list); dup.storage.addAll((RubyArray) list); } else { RubyRepeatedField repeatedField = (RubyRepeatedField) list; if (! fieldType.equals(repeatedField.fieldType) || (typeClass != null && ! typeClass.equals(repeatedField.typeClass))) throw context.runtime.newArgumentError("Attempt to append RepeatedField with different element type."); dup.storage.addAll((RubyArray) repeatedField.toArray(context)); } return dup; }
@JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject clock_getres(ThreadContext context, IRubyObject self, IRubyObject _clock_id, IRubyObject _unit) { Ruby runtime = context.runtime; if (!(_unit instanceof RubySymbol) && !_unit.isNil()) { throw runtime.newArgumentError("unexpected unit: " + _unit); } return makeClockResolutionResult(runtime, getResolutionForClock(_clock_id, runtime), _unit.toString()); }
private void init(ThreadContext context, IRubyObject begin, IRubyObject end, boolean isExclusive) { if (!(begin instanceof RubyFixnum && end instanceof RubyFixnum)) { IRubyObject result = invokedynamic(context, begin, MethodNames.OP_CMP, end); if (result.isNil()) { throw context.runtime.newArgumentError("bad value for range"); } } this.begin = begin; this.end = end; this.isExclusive = isExclusive; this.isInited = true; }
@JRubyMethod(name = "digest", required = 1, rest = true, meta = true) public static IRubyObject s_digest(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) { final Ruby runtime = context.runtime; if (args.length < 1) { throw runtime.newArgumentError("no data given"); } RubyString str = args[0].convertToString(); args = ArraySupport.newCopy(args, 1, args.length - 1); // skip first arg IRubyObject obj = ((RubyClass) recv).newInstance(context, args, Block.NULL_BLOCK); return obj.callMethod(context, "digest", str); }
public static Encoding toEncoding(ThreadContext context, IRubyObject enc) { RubyString encStr = enc.convertToString(); if (!encStr.getEncoding().isAsciiCompatible()) { throw context.runtime.newArgumentError("invalid name encoding (non ASCII)"); } Encoding idx = context.runtime.getEncodingService().getEncodingFromObject(enc); // check for missing encoding is in getEncodingFromObject return idx; }
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) { IRubyObject s = recv.callMethod(context, "inspect"); throw context.runtime.newArgumentError("invalid value for convert(): " + s.convertToString()); } return a.eltInternal(0); } }
@JRubyMethod(optional = 1) public IRubyObject initialize(final ThreadContext context, IRubyObject[] args) { final Ruby runtime = context.runtime; if (args.length == 1) { if (!(args[0] instanceof RubyHash)) { throw runtime.newArgumentError("expected Hash arguments.");
@JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject clock_getres(ThreadContext context, IRubyObject self, IRubyObject _clock_id, IRubyObject _unit) { Ruby runtime = context.runtime; if (!(_unit instanceof RubySymbol) && !_unit.isNil()) { throw runtime.newArgumentError("unexpected unit: " + _unit); } return makeClockResolutionResult(runtime, getResolutionForClock(_clock_id, runtime), _unit.toString()); }