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(name = "[]=") public IRubyObject indexSet(ThreadContext context, IRubyObject key, IRubyObject value) { key = Utils.checkType(context, keyType, key, (RubyModule) valueTypeClass); value = Utils.checkType(context, valueType, value, (RubyModule) valueTypeClass); IRubyObject symbol; if (valueType == Descriptors.FieldDescriptor.Type.ENUM && Utils.isRubyNum(value) && ! (symbol = RubyEnum.lookup(context, valueTypeClass, value)).isNil()) { value = symbol; } this.table.put(key, value); return value; }
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 = "inflate", required = 1) public IRubyObject inflate(ThreadContext context, IRubyObject string, Block block) { ByteList data = null; if (!string.isNil()) { data = string.convertToString().getByteList(); } return inflate(context, data, block); }
@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; }
@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);
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; }
private void setupStructValuesFromHash(ThreadContext context, RubyHash kwArgs) { RubyArray __members__ = __member__(); Set<Map.Entry<IRubyObject, IRubyObject>> entries = kwArgs.directEntrySet(); entries.stream().forEach( entry -> { IRubyObject key = entry.getKey(); if (!(key instanceof RubySymbol)) key = context.runtime.newSymbol(key.convertToString().getByteList()); IRubyObject index = __members__.index(context, key); if (index.isNil()) throw context.runtime.newArgumentError("unknown keywords: " + key); values[index.convertToInteger().getIntValue()] = entry.getValue(); }); }
@JRubyMethod(name = "readpartial", required = 1, optional = 1) public IRubyObject readpartial(ThreadContext context, IRubyObject[] args) { // ruby bug 11885 if (args.length == 2) { args[1] = args[1].convertToString(); } IRubyObject value = getPartial(context, args, false, false); if (value.isNil()) { throw context.runtime.newEOFError(); } return value; }
@JRubyMethod public static IRubyObject detect_implicit(IRubyObject self, IRubyObject val) { IRubyObject tmp = TypeConverter.convertToTypeWithCheck(val, self.getRuntime().getString(), "to_str"); if(!tmp.isNil()) { ByteList bl = ((RubyString)tmp).getByteList(); String type_id = ImplicitScanner2.matchImplicit(Pointer.create(bl.bytes, bl.begin), bl.realSize); return self.getRuntime().newString(type_id); } return RubyString.newEmptyString(self.getRuntime()); } }
public static PointerParameterStrategy stringParameterStrategy(IRubyObject parameter) { if (parameter instanceof RubyString) { return DIRECT_STRING_PARAMETER_STRATEGY; } else if (parameter.isNil()) { return NIL_POINTER_STRATEGY; } else { return stringParameterStrategy(parameter.convertToString()); } }
private static RubySymbol stringOrSymbol(IRubyObject obj) { if (obj instanceof RubySymbol) return (RubySymbol) obj; if (obj instanceof RubyString) return RubySymbol.newSymbol(obj.getRuntime(), obj.asJavaString().intern()); if (obj instanceof RubyFixnum) { obj.getRuntime().getWarnings().warn(ID.FIXNUMS_NOT_SYMBOLS, "do not use Fixnums as Symbols"); throw obj.getRuntime().newArgumentError("" + obj + " is not a symbol"); } IRubyObject strValue = obj.checkStringType(); if (!strValue.isNil()) return RubySymbol.newSymbol(obj.getRuntime(), obj.asJavaString().intern()); throw obj.getRuntime().newTypeError("" + obj + " is not a symbol"); }
protected int read(CallSite read, ByteBuffer dst) throws IOException { IRubyObject readValue = read.call(runtime.getCurrentContext(), io, io, runtime.newFixnum(dst.remaining())); int returnValue = -1; if (!readValue.isNil()) { ByteList str = ((RubyString)readValue).getByteList(); dst.put(str.getUnsafeBytes(), str.getBegin(), str.getRealSize()); returnValue = str.getRealSize(); } return returnValue; }
@JRubyMethod(name = "inflate", required = 1) public IRubyObject inflate(ThreadContext context, IRubyObject string, Block block) { ByteList data = null; if (!string.isNil()) { data = string.convertToString().getByteList(); } return inflate(context, data, block); }
@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; }
@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()); }
@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]); } }
public int read() throws IOException { if (in != null) { return in.read(); } IRubyObject readValue = readAdapter.call(io.getRuntime().getCurrentContext(), io, io, numOne); int returnValue = -1; if (!readValue.isNil()) { returnValue = readValue.convertToString().getByteList().get(0) & 0xff; } return returnValue; }
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); } }
private static RubyArray coerceResult(final Ruby runtime, final IRubyObject result, final boolean err) { if (result instanceof RubyArray && ((RubyArray) result).getLength() == 2) return (RubyArray) result; if (err || !result.isNil()) throw runtime.newTypeError("coerce must return [x, y]"); return null; }