@JRubyMethod(name = "get") public IRubyObject getValue(ThreadContext context, IRubyObject msgRb) { RubyMessage message = (RubyMessage) msgRb; if (message.getDescriptor() != fieldDef.getContainingType()) { throw context.runtime.newTypeError("set method called on wrong message type"); } return message.getField(context, fieldDef); }
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: if (!(value instanceof RubyBoolean)) throw runtime.newTypeError("Invalid argument for boolean field."); break; case BYTES: break; case MESSAGE: if (value.getMetaClass() != typeClass) { throw runtime.newTypeError(value, typeClass); throw runtime.newRangeError("Enum value " + value + " is not found."); } else if(!isRubyNum(value)) { throw runtime.newTypeError("Expected number or symbol type for enum field.");
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"); fields.remove(oneofCase); if (value.isNil()) { oneofCases.remove(oneofDescriptor); fields.remove(fieldDescriptor); if (fieldType == Descriptors.FieldDescriptor.Type.MESSAGE) { typeClass = ((RubyDescriptor) getDescriptorForField(context, fieldDescriptor)).msgclass(context); if (value.isNil()){ addValue = false;
@JRubyMethod(name = "new", meta = true) public static final IRubyObject newStructByReference(ThreadContext context, IRubyObject klass, IRubyObject structClass) { if (!(structClass instanceof RubyClass)) { throw context.runtime.newTypeError("wrong argument type " + structClass.getMetaClass().getName() + " (expected Class)"); } if (!((RubyClass) structClass).isKindOfModule(context.runtime.getFFI().structClass)) { throw context.runtime.newTypeError("wrong argument type " + structClass.getMetaClass().getName() + " (expected subclass of FFI::Struct)"); } return new StructByReference(context.runtime, (RubyClass) klass, (RubyClass) structClass, null); }
@JRubyMethod(module = true, visibility = PRIVATE) public static IRubyObject set_trace_func(ThreadContext context, IRubyObject recv, IRubyObject trace_func, Block block) { if (trace_func.isNil()) { context.runtime.setTraceFunction(null); } else if (!(trace_func instanceof RubyProc)) { throw context.runtime.newTypeError("trace_func needs to be Proc."); } else { context.runtime.setTraceFunction((RubyProc) trace_func); } return trace_func; }
/** rb_flo_induced_from * */ @Deprecated public static IRubyObject induced_from(ThreadContext context, IRubyObject recv, IRubyObject number) { if (number instanceof RubyFixnum || number instanceof RubyBignum || number instanceof RubyRational) { return number.callMethod(context, "to_f"); } else if (number instanceof RubyFloat) { return number; } throw recv.getRuntime().newTypeError("failed to convert " + number.getMetaClass() + " into Float"); }
private static void checkArrayType(Ruby runtime, IRubyObject obj) { if (!(obj instanceof RubyArray)) { throw runtime.newTypeError("wrong argument type " + obj.getMetaClass().getName() + " (expected Array)"); } }
private static RubyString getStringForPattern(Ruby runtime, IRubyObject obj) { if (obj instanceof RubyString) return (RubyString) obj; IRubyObject val = obj.checkStringType(); if (val.isNil()) throw runtime.newTypeError("wrong argument type " + obj.getMetaClass() + " (expected Regexp)"); return (RubyString) val; }
@JRubyMethod(required = 1, meta = true) public static IRubyObject kill(IRubyObject receiver, IRubyObject rubyThread, Block block) { if (!(rubyThread instanceof RubyThread)) throw receiver.getRuntime().newTypeError(rubyThread, receiver.getRuntime().getThread()); return ((RubyThread)rubyThread).kill(); }
@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; }
private static long other2long(IRubyObject arg) throws RaiseException { if (arg instanceof RubyFloat) return float2long((RubyFloat) arg); if (arg instanceof RubyBignum) return RubyBignum.big2long((RubyBignum) arg); if (arg.isNil()) { throw arg.getRuntime().newTypeError("no implicit conversion from nil to integer"); } return arg.convertToInteger().getLongValue(); }
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())); } }
/** num_init_copy * */ @Override @JRubyMethod(name = "initialize_copy", visibility = Visibility.PRIVATE) public IRubyObject initialize_copy(IRubyObject arg) { throw getRuntime().newTypeError("can't copy " + getType().getName()); }
/** nucomp_coerce * */ @JRubyMethod(name = "coerce") public IRubyObject coerce(ThreadContext context, IRubyObject other) { if (other instanceof RubyNumeric && f_real_p(context, other)) { return context.runtime.newArray(newComplexBang(context, getMetaClass(), (RubyNumeric) other), this); } if (other instanceof RubyComplex) return context.runtime.newArray(other, this); Ruby runtime = context.runtime; throw runtime.newTypeError(str(runtime, types(runtime, other.getMetaClass()), " can't be coerced into ", types(runtime, getMetaClass()))); }
public void writeUserClass(IRubyObject obj, RubyClass type) throws IOException { write(TYPE_UCLASS); // w_unique if (type.getName().charAt(0) == '#') { Ruby runtime = obj.getRuntime(); throw runtime.newTypeError(str(runtime, "can't dump anonymous class ", types(runtime, type))); } // w_symbol writeAndRegisterSymbol(RubySymbol.newSymbol(runtime, type.getName()).getBytes()); }
private static RubyModule module(IRubyObject obj) { if (!(obj instanceof RubyModule)) { throw obj.getRuntime().newTypeError("not a module"); } return (RubyModule) obj; }
public static IRubyObject[] toAry(ThreadContext context, IRubyObject[] args) { IRubyObject ary; if (args.length == 1 && (ary = Helpers.aryOrToAry(context, args[0])) != context.nil) { if (ary instanceof RubyArray) { args = ((RubyArray) ary).toJavaArray(); } else { throw context.runtime.newTypeError(args[0].getType().getName() + "#to_ary should return Array"); } } return args; }
@JRubyMethod(name = "new", meta = true) public static final IRubyObject newStructByReference(ThreadContext context, IRubyObject klass, IRubyObject structClass) { if (!(structClass instanceof RubyClass)) { throw context.runtime.newTypeError("wrong argument type " + structClass.getMetaClass().getName() + " (expected Class)"); } if (!((RubyClass) structClass).isKindOfModule(context.runtime.getFFI().structClass)) { throw context.runtime.newTypeError("wrong argument type " + structClass.getMetaClass().getName() + " (expected subclass of FFI::Struct)"); } return new StructByReference(context.runtime, (RubyClass) klass, (RubyClass) structClass, null); }
@JRubyMethod public synchronized IRubyObject feed(ThreadContext context, IRubyObject val) { final Nexter nexter = ensureNexter(context.runtime); if (!feedValue.isNil()) { throw context.runtime.newTypeError("feed value already set"); } feedValue = val; nexter.setFeedValue(val); return context.nil; }
/** rb_flo_induced_from * */ @Deprecated public static IRubyObject induced_from(ThreadContext context, IRubyObject recv, IRubyObject number) { if (number instanceof RubyFixnum || number instanceof RubyBignum || number instanceof RubyRational) { return number.callMethod(context, "to_f"); } else if (number instanceof RubyFloat) { return number; } throw recv.getRuntime().newTypeError("failed to convert " + number.getMetaClass() + " into Float"); }