Go to the documentation of this file.
35 #include <ruby/encoding.h>
41 #define CHARPTR_AT(msg, ofs) ((char*)msg + ofs)
42 #define DEREF_OFFSET(msg, ofs, type) *(type*)CHARPTR_AT(msg, ofs)
43 #define DEREF(memory, type) *(type*)(memory)
70 rb_raise(
cTypeError,
"Expected number type for integral field '%s' (given %s).",
71 name, rb_class2name(CLASS_OF(
val)));
78 double dbl_val = NUM2DBL(
val);
79 if (floor(dbl_val) != dbl_val) {
80 rb_raise(rb_eRangeError,
81 "Non-integral floating point value assigned to integer field '%s' (given %s).",
82 name, rb_class2name(CLASS_OF(
val)));
86 if (NUM2DBL(
val) < 0) {
87 rb_raise(rb_eRangeError,
88 "Assigning negative value to unsigned integer field '%s' (given %s).",
89 name, rb_class2name(CLASS_OF(
val)));
97 VALUE desired_encoding_value = rb_enc_from_encoding(desired_encoding);
100 value = rb_str_encode(
value, desired_encoding_value, 0, Qnil);
103 rb_enc_str_coderange(
value) == ENC_CODERANGE_BROKEN) {
104 rb_raise(rb_eEncodingError,
"String is invalid UTF-8");
109 rb_obj_freeze(
value);
116 void* memory, VALUE
value) {
122 void* memory, VALUE
value,
123 uint32_t* case_memory,
124 uint32_t case_number) {
132 rb_raise(
cTypeError,
"Expected number type for float field '%s' (given %s).",
139 rb_raise(
cTypeError,
"Expected number type for double field '%s' (given %s).",
146 if (
value == Qtrue) {
148 }
else if (
value == Qfalse) {
151 rb_raise(
cTypeError,
"Invalid argument for boolean field '%s' (given %s).",
158 if (CLASS_OF(
value) == rb_cSymbol) {
159 value = rb_funcall(
value, rb_intern(
"to_s"), 0);
160 }
else if (CLASS_OF(
value) != rb_cString) {
161 rb_raise(
cTypeError,
"Invalid argument for string field '%s' (given %s).",
169 if (CLASS_OF(
value) != rb_cString) {
170 rb_raise(
cTypeError,
"Invalid argument for bytes field '%s' (given %s).",
178 if (CLASS_OF(
value) == CLASS_OF(Qnil)) {
180 }
else if (CLASS_OF(
value) != type_class) {
182 VALUE converted_value =
NULL;
183 char* field_type_name = rb_class2name(type_class);
185 if (strcmp(field_type_name,
"Google::Protobuf::Timestamp") == 0 &&
186 rb_obj_is_kind_of(
value, rb_cTime)) {
188 VALUE hash = rb_hash_new();
189 rb_hash_aset(hash, rb_str_new2(
"seconds"), rb_funcall(
value, rb_intern(
"to_i"), 0));
190 rb_hash_aset(hash, rb_str_new2(
"nanos"), rb_funcall(
value, rb_intern(
"nsec"), 0));
191 VALUE
args[1] = { hash };
192 converted_value = rb_class_new_instance(1,
args, type_class);
193 }
else if (strcmp(field_type_name,
"Google::Protobuf::Duration") == 0 &&
194 rb_obj_is_kind_of(
value, rb_cNumeric)) {
196 VALUE hash = rb_hash_new();
197 rb_hash_aset(hash, rb_str_new2(
"seconds"), rb_funcall(
value, rb_intern(
"to_i"), 0));
198 VALUE n_value = rb_funcall(
value, rb_intern(
"remainder"), 1, INT2NUM(1));
199 n_value = rb_funcall(n_value, rb_intern(
"*"), 1, INT2NUM(1000000000));
200 n_value = rb_funcall(n_value, rb_intern(
"round"), 0);
201 rb_hash_aset(hash, rb_str_new2(
"nanos"), n_value);
202 VALUE
args[1] = { hash };
203 converted_value = rb_class_new_instance(1,
args, type_class);
207 if (converted_value ==
NULL) {
209 "Invalid type %s to assign to submessage field '%s'.",
212 value = converted_value;
221 value = rb_funcall(
value, rb_intern(
"to_sym"), 0);
224 "Expected number or symbol type for enum field '%s'.",
name);
228 VALUE
lookup = rb_funcall(type_class, rb_intern(
"resolve"), 1,
value);
230 rb_raise(rb_eRangeError,
"Unknown symbol value for enum field '%s'.",
name);
232 int_val = NUM2INT(
lookup);
236 int_val = NUM2INT(
value);
238 DEREF(memory, int32_t) = int_val;
267 if (case_memory !=
NULL) {
268 *case_memory = case_number;
274 const void* memory) {
277 return DBL2NUM(
DEREF(memory,
float));
279 return DBL2NUM(
DEREF(memory,
double));
281 return DEREF(memory, int8_t) ? Qtrue : Qfalse;
285 return DEREF(memory, VALUE);
287 int32_t
val =
DEREF(memory, int32_t);
289 if (symbol == Qnil) {
296 return INT2NUM(
DEREF(memory, int32_t));
298 return LL2NUM(
DEREF(memory, int64_t));
300 return UINT2NUM(
DEREF(memory, uint32_t));
302 return ULL2NUM(
DEREF(memory, uint64_t));
311 DEREF(memory,
float) = 0.0;
314 DEREF(memory,
double) = 0.0;
317 DEREF(memory, int8_t) = 0;
321 DEREF(memory, VALUE) = rb_str_new2(
"");
326 DEREF(memory, VALUE) = Qnil;
330 DEREF(memory, int32_t) = 0;
333 DEREF(memory, int64_t) = 0;
336 DEREF(memory, uint32_t) = 0;
339 DEREF(memory, uint64_t) = 0;
351 rb_gc_mark(
DEREF(memory, VALUE));
366 VALUE from_val =
DEREF(from, VALUE);
367 DEREF(to, VALUE) = (from_val != Qnil) ?
368 rb_funcall(from_val, rb_intern(
"dup"), 0) : Qnil;
372 VALUE from_val =
DEREF(from, VALUE);
373 DEREF(to, VALUE) = (from_val != Qnil) ?
387 VALUE val1 =
DEREF(mem1, VALUE);
388 VALUE val2 =
DEREF(mem2, VALUE);
389 VALUE ret = rb_funcall(val1, rb_intern(
"=="), 1, val2);
419 if (subdef ==
NULL)
return false;
438 assert(key_field !=
NULL);
444 assert(value_field !=
NULL);
460 return (
offset + granularity - 1) & ~(granularity - 1);
486 off += (hasbit + 8 - 1) / 8;
503 field_size =
sizeof(VALUE);
556 size_t field_size =
sizeof(uint32_t);
558 off = (off + field_size - 1) & ~(field_size - 1);
584 VALUE type_class = Qnil;
600 return ((uint8_t *)storage) +
607 return (uint32_t *)(((uint8_t *)storage) +
617 ((uint8_t*)storage)[hasbit / 8] |= 1 << (hasbit % 8);
625 ((uint8_t*)storage)[hasbit / 8] &= ~(1 << (hasbit % 8));
637 (uint8_t*)storage, hasbit / 8,
char) & (1 << (hasbit % 8));
667 if (type_class != Qnil) {
688 if (type_class != Qnil) {
699 DEREF(memory, VALUE) = ary;
719 return ID2SYM(rb_intern(
label));
732 VALUE str_rb = rb_str_new(
str,
size);
736 rb_obj_freeze(str_rb);
739 default:
return Qnil;
764 return *((VALUE *)memory);
765 }
else if (!field_set) {
778 if (!RB_TYPE_P(
val, T_DATA) || !RTYPEDDATA_P(
val) ||
780 rb_raise(
cTypeError,
"Expected repeated field array");
785 rb_raise(
cTypeError,
"Repeated field array has wrong element type");
789 if (
self->field_type_class !=
792 "Repeated field array has wrong message class");
798 if (
self->field_type_class !=
801 "Repeated field array has wrong enum class");
811 if (!RB_TYPE_P(
val, T_DATA) || !RTYPEDDATA_P(
val) ||
813 rb_raise(
cTypeError,
"Expected Map instance");
818 rb_raise(
cTypeError,
"Map key type does not match field's key type");
821 rb_raise(
cTypeError,
"Map value type does not match field's value type");
825 if (
self->value_type_class !=
828 "Map value type has wrong message/enum class");
908 rb_gc_mark(
DEREF(memory, VALUE));
929 *to_oneof_case = *from_oneof_case;
961 *to_oneof_case = *from_oneof_case;
965 DEREF(to_memory, VALUE) =
968 DEREF(to_memory, VALUE) =
994 if (*msg1_oneof_case != *msg2_oneof_case ||
1003 DEREF(msg2_memory, VALUE))) {
1008 DEREF(msg2_memory, VALUE))) {
1015 msg1_memory, msg2_memory)) {
1025 st_index_t
h = rb_hash_start(0);
1026 VALUE hash_sym = rb_intern(
"hash");
1032 h = rb_hash_uint(
h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
1040 VALUE
str = rb_str_new2(
"");
1051 str = rb_str_cat2(
str,
", ");
1056 str = rb_str_cat2(
str,
": ");
1058 str = rb_str_append(
str, rb_funcall(field_val, rb_intern(
"inspect"), 0));
const upb_msgdef * msgdef
int64_t upb_fielddef_defaultint64(const upb_fielddef *f)
uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f)
const upb_oneofdef * upb_msg_iter_oneof(const upb_msg_oneof_iter *iter)
GLuint const GLchar * name
static void check_repeated_field_type(VALUE val, const upb_fielddef *field)
static int16_t lookup[CNT]
bool upb_msgdef_mapentry(const upb_msgdef *m)
void native_slot_check_int_range_precision(const char *name, upb_fieldtype_t type, VALUE val)
VALUE field_type_class(const upb_fielddef *field)
static void check_map_field_type(VALUE val, const upb_fielddef *field)
const char * upb_enumdef_iton(const upb_enumdef *def, int32_t num)
void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m)
VALUE layout_eq(MessageLayout *layout, void *msg1, void *msg2)
VALUE Map_dup(VALUE _self)
VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value)
VALUE layout_get_default(const upb_fielddef *field)
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
static void slot_clear_hasbit(MessageLayout *layout, const void *storage, const upb_fielddef *field)
VALUE EnumDescriptor_enummodule(VALUE _self)
GLuint GLsizei const GLchar * label
void layout_init(MessageLayout *layout, void *storage)
void layout_mark(MessageLayout *layout, void *storage)
const upb_fielddef * map_field_value(const upb_fielddef *field)
const upb_def * upb_fielddef_subdef(const upb_fielddef *f)
bool native_slot_eq(upb_fieldtype_t type, void *mem1, void *mem2)
static void * slot_memory(MessageLayout *layout, const void *storage, const upb_fielddef *field)
static void slot_set_hasbit(MessageLayout *layout, const void *storage, const upb_fielddef *field)
const upb_enumdef * upb_fielddef_enumsubdef(const upb_fielddef *f)
size_t native_slot_size(upb_fieldtype_t type)
static size_t align_up_to(size_t offset, size_t granularity)
VALUE layout_inspect(MessageLayout *layout, void *storage)
double upb_fielddef_defaultdouble(const upb_fielddef *f)
Map * ruby_to_Map(VALUE _self)
#define ALLOC(class_name)
float upb_fielddef_defaultfloat(const upb_fielddef *f)
MessageLayout * create_layout(const upb_msgdef *msgdef)
void layout_deep_copy(MessageLayout *layout, void *to, void *from)
#define ALLOC_N(class_name, n)
const upb_fielddef * map_entry_value(const upb_msgdef *msgdef)
rb_encoding * kRubyStringUtf8Encoding
#define MESSAGE_FIELD_NO_HASBIT
const upb_oneofdef * upb_fielddef_containingoneof(const upb_fielddef *f)
void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m)
void native_slot_mark(upb_fieldtype_t type, void *memory)
const char * upb_fielddef_name(const upb_fielddef *f)
void native_slot_set_value_and_case(const char *name, upb_fieldtype_t type, VALUE type_class, void *memory, VALUE value, uint32_t *case_memory, uint32_t case_number)
int32_t upb_fielddef_defaultint32(const upb_fielddef *f)
rb_encoding * kRubyString8bitEncoding
VALUE layout_hash(MessageLayout *layout, void *storage)
bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter)
const FieldDescriptor * field
void native_slot_deep_copy(upb_fieldtype_t type, void *to, void *from)
VALUE Map_eq(VALUE _self, VALUE _other)
const upb_fielddef * map_entry_key(const upb_msgdef *msgdef)
void upb_oneof_next(upb_oneof_iter *iter)
bool is_map_field(const upb_fielddef *field)
upb_fielddef * upb_msg_iter_field(const upb_msg_field_iter *iter)
upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f)
bool upb_fielddef_defaultbool(const upb_fielddef *f)
void layout_set(MessageLayout *layout, void *storage, const upb_fielddef *field, VALUE val)
upb_fielddef * upb_oneof_iter_field(const upb_oneof_iter *iter)
void native_slot_set(const char *name, upb_fieldtype_t type, VALUE type_class, void *memory, VALUE value)
VALUE RepeatedField_dup(VALUE _self)
void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o)
const upb_msgdef * tryget_map_entry_msgdef(const upb_fielddef *field)
static bool is_ruby_num(VALUE value)
const upb_msgdef * upb_fielddef_msgsubdef(const upb_fielddef *f)
void layout_dup(MessageLayout *layout, void *to, void *from)
void upb_msg_field_next(upb_msg_field_iter *iter)
const upb_msgdef * msgdef
PHP_PROTO_HASHTABLE_VALUE get_def_obj(const void *def)
bool field_contains_hasbit(MessageLayout *layout, const upb_fielddef *field)
static bool slot_is_hasbit_set(MessageLayout *layout, const void *storage, const upb_fielddef *field)
int upb_msgdef_numfields(const upb_msgdef *m)
VALUE fieldtype_to_ruby(upb_fieldtype_t type)
const rb_data_type_t RepeatedField_type
VALUE Descriptor_msgclass(VALUE _self)
#define NATIVE_SLOT_MAX_SIZE
#define DEREF(memory, type)
bool upb_msg_field_done(const upb_msg_field_iter *iter)
const upb_fielddef * upb_msgdef_itof(const upb_msgdef *m, uint32_t i)
const upb_fielddef * map_field_key(const upb_fielddef *field)
VALUE Message_deep_copy(VALUE _self)
#define MESSAGE_FIELD_NO_CASE
VALUE RepeatedField_eq(VALUE _self, VALUE _other)
RepeatedField * ruby_to_RepeatedField(VALUE value)
uint32_t upb_fielddef_number(const upb_fielddef *f)
upb_label_t upb_fielddef_label(const upb_fielddef *f)
void layout_clear(MessageLayout *layout, const void *storage, const upb_fielddef *field)
void free_layout(MessageLayout *layout)
bool upb_oneof_done(upb_oneof_iter *iter)
#define DEREF_OFFSET(msg, ofs, type)
void native_slot_init(upb_fieldtype_t type, void *memory)
const upb_msgdef * map_entry_msgdef(const upb_fielddef *field)
const rb_data_type_t Map_type
VALUE layout_get(MessageLayout *layout, const void *storage, const upb_fielddef *field)
GLsizei const GLfloat * value
VALUE RepeatedField_deep_copy(VALUE _self)
static uint32_t * slot_oneof_case(MessageLayout *layout, const void *storage, const upb_fielddef *field)
void upb_msg_oneof_next(upb_msg_oneof_iter *iter)
VALUE enum_lookup(VALUE self, VALUE number)
upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m)
const upb_enumdef * enumdef
void native_slot_dup(upb_fieldtype_t type, void *to, void *from)
VALUE layout_has(MessageLayout *layout, const void *storage, const upb_fielddef *field)
uint32_t upb_fielddef_index(const upb_fielddef *f)
VALUE native_slot_get(upb_fieldtype_t type, VALUE type_class, const void *memory)
bool upb_fielddef_haspresence(const upb_fielddef *f)
GLfloat GLfloat GLfloat GLfloat h
uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f)
const char * upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len)
VALUE Map_deep_copy(VALUE _self)
libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59