static void check_repeated_field_type(VALUE val, const upb_fielddef* field) { RepeatedField* self; assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED); if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) || RTYPEDDATA_TYPE(val) != &RepeatedField_type) { rb_raise(cTypeError, "Expected repeated field array"); } self = ruby_to_RepeatedField(val); if (self->field_type != upb_fielddef_type(field)) { rb_raise(cTypeError, "Repeated field array has wrong element type"); } if (self->field_type == UPB_TYPE_MESSAGE) { if (self->field_type_class != Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) { rb_raise(cTypeError, "Repeated field array has wrong message class"); } } if (self->field_type == UPB_TYPE_ENUM) { if (self->field_type_class != EnumDescriptor_enummodule(get_def_obj(upb_fielddef_subdef(field)))) { rb_raise(cTypeError, "Repeated field array has wrong enum class"); } } }
static void check_map_field_type(VALUE val, const upb_fielddef* field) { const upb_fielddef* key_field = map_field_key(field); const upb_fielddef* value_field = map_field_value(field); Map* self; if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) || RTYPEDDATA_TYPE(val) != &Map_type) { rb_raise(cTypeError, "Expected Map instance"); } self = ruby_to_Map(val); if (self->key_type != upb_fielddef_type(key_field)) { rb_raise(cTypeError, "Map key type does not match field's key type"); } if (self->value_type != upb_fielddef_type(value_field)) { rb_raise(cTypeError, "Map value type does not match field's value type"); } if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE || upb_fielddef_type(value_field) == UPB_TYPE_ENUM) { if (self->value_type_class != get_def_obj(upb_fielddef_subdef(value_field))) { rb_raise(cTypeError, "Map value type has wrong message/enum class"); } } }
// default behavior of async (doesn't accept Symbol) // call-seq: // async { } // async Proc.new static VALUE kern_async(int argc, VALUE *argv, VALUE self) { VALUE obj; rb_proc_t *proc; rb_iseq_t *niseq; rb_scan_args(argc, argv, "01", &obj); if (!NIL_P(obj)) { if (!rb_obj_is_proc(obj)) { VALUE thread = rb_thread_current(); rb_thread_t * th; TypedData_Get_Struct(thread, rb_thread_t, RTYPEDDATA_TYPE(thread), th); if (self == th->vm->top_self) { return mod_async(CLASS_OF(self), obj); } else { rb_raise(rb_eTypeError, "wrong argument type (expected Proc)"); } } } else if (rb_block_given_p()) { obj = rb_block_proc(); } else { rb_raise(rb_eArgError, "Proc or block is required"); } proc = (rb_proc_t *)DATA_PTR(obj); // うーーー niseq = transform(obj); proc->block.iseq = niseq; return obj; }
/* * call-seq: * RepeatedField.+(other) => repeated field * * Returns a new repeated field that contains the concatenated list of this * repeated field's elements and other's elements. The other (second) list may * be either another repeated field or a Ruby array. */ VALUE RepeatedField_plus(VALUE _self, VALUE list) { VALUE dupped = RepeatedField_dup(_self); if (TYPE(list) == T_ARRAY) { for (int i = 0; i < RARRAY_LEN(list); i++) { VALUE elem = rb_ary_entry(list, i); RepeatedField_push(dupped, elem); } } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) && RTYPEDDATA_TYPE(list) == &RepeatedField_type) { RepeatedField* self = ruby_to_RepeatedField(_self); RepeatedField* list_rptfield = ruby_to_RepeatedField(list); if (self->field_type != list_rptfield->field_type || self->field_type_class != list_rptfield->field_type_class) { rb_raise(rb_eArgError, "Attempt to append RepeatedField with different element type."); } for (int i = 0; i < list_rptfield->size; i++) { void* mem = RepeatedField_index_native(list, i); RepeatedField_push_native(dupped, mem); } } else { rb_raise(rb_eArgError, "Unknown type appending to RepeatedField"); } return dupped; }
void* unwrapTypedPtr(const VALUE &obj, rb_data_type_t* rbdata) { if(NIL_P(obj)) return NULL; if(!rbdata) { rb_raise(rb_eTypeError,"%" PRIsVALUE " unknown datatype", RB_OBJ_CLASSNAME(obj)); return NULL; } const rb_data_type_t* rbdata_obj = RTYPEDDATA_TYPE(obj); if(rbdata_obj != rbdata) { if(!rb_typeddata_inherited_p(rbdata_obj, rbdata)) { rb_warn("%s is not a %s but inherit from %s", rbdata_obj->wrap_struct_name, rbdata->wrap_struct_name, rbdata_obj->parent->wrap_struct_name ); } } void* data = Check_TypedStruct(obj, rbdata); if(!data) { rb_raise( rb_eRuntimeError, "destroyed object of %" PRIsVALUE, RB_OBJ_CLASSNAME(obj) ); return NULL; } return data; }
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type) { if (!RB_TYPE_P(obj, T_DATA) || !RTYPEDDATA_P(obj) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { return 0; } return 1; }
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type) { if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA || !RTYPEDDATA_P(obj) || RTYPEDDATA_TYPE(obj) != data_type) { return 0; } return 1; }
static inline const rb_data_type_t * threadptr_data_type(void) { static const rb_data_type_t *thread_data_type; if (!thread_data_type) { VALUE current_thread = rb_thread_current(); thread_data_type = RTYPEDDATA_TYPE(current_thread); } return thread_data_type; }
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type) { const char *etype; static const char mesg[] = "wrong argument type %s (expected %s)"; if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA) { Check_Type(obj, T_DATA); } if (!RTYPEDDATA_P(obj)) { etype = rb_obj_classname(obj); rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } else if (RTYPEDDATA_TYPE(obj) != data_type) { etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name; rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } return DATA_PTR(obj); }
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type) { const char *etype; static const char mesg[] = "wrong argument type %s (expected %s)"; if (!RB_TYPE_P(obj, T_DATA)) { etype = builtin_class_name(obj); rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } if (!RTYPEDDATA_P(obj)) { etype = rb_obj_classname(obj); rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name; rb_raise(rb_eTypeError, mesg, etype, data_type->wrap_struct_name); } return DATA_PTR(obj); }
void validate_type_class(upb_fieldtype_t type, VALUE klass) { if (rb_ivar_get(klass, descriptor_instancevar_interned) == Qnil) { rb_raise(rb_eArgError, "Type class has no descriptor. Please pass a " "class or enum as returned by the DescriptorPool."); } if (type == UPB_TYPE_MESSAGE) { VALUE desc = rb_ivar_get(klass, descriptor_instancevar_interned); if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) || RTYPEDDATA_TYPE(desc) != &_Descriptor_type) { rb_raise(rb_eArgError, "Descriptor has an incorrect type."); } if (rb_get_alloc_func(klass) != &Message_alloc) { rb_raise(rb_eArgError, "Message class was not returned by the DescriptorPool."); } } else if (type == UPB_TYPE_ENUM) { VALUE enumdesc = rb_ivar_get(klass, descriptor_instancevar_interned); if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) || RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) { rb_raise(rb_eArgError, "Descriptor has an incorrect type."); } } }
// Used only internally -- shared by #merge and #initialize. VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) { if (TYPE(hashmap) == T_HASH) { rb_hash_foreach(hashmap, merge_into_self_callback, _self); } else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) && RTYPEDDATA_TYPE(hashmap) == &Map_type) { Map* self = ruby_to_Map(_self); Map* other = ruby_to_Map(hashmap); upb_strtable_iter it; if (self->key_type != other->key_type || self->value_type != other->value_type || self->value_type_class != other->value_type_class) { rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types"); } for (upb_strtable_begin(&it, &other->table); !upb_strtable_done(&it); upb_strtable_next(&it)) { // Replace any existing value by issuing a 'remove' operation first. upb_value v; upb_value oldv; upb_strtable_remove2(&self->table, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it), &oldv); v = upb_strtable_iter_value(&it); upb_strtable_insert2(&self->table, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it), v); } } else { rb_raise(rb_eArgError, "Unknown type merging into Map"); } return _self; }
VALUE wrap(const T *arg) { VALUE result = rb_obj_freeze(wrap(const_cast<T*>(arg))); RTYPEDDATA_TYPE(result) = &datatypeholder_const[CLASS_OF(result)]; return result; }
static void init_ruby_threadptr_data_type() { VALUE thread = rb_thread_current(); p_ruby_threadptr_data_type = RTYPEDDATA_TYPE(thread); }