| OLD | NEW |
| (Empty) |
| 1 // Protocol Buffers - Google's data interchange format | |
| 2 // Copyright 2014 Google Inc. All rights reserved. | |
| 3 // https://developers.google.com/protocol-buffers/ | |
| 4 // | |
| 5 // Redistribution and use in source and binary forms, with or without | |
| 6 // modification, are permitted provided that the following conditions are | |
| 7 // met: | |
| 8 // | |
| 9 // * Redistributions of source code must retain the above copyright | |
| 10 // notice, this list of conditions and the following disclaimer. | |
| 11 // * Redistributions in binary form must reproduce the above | |
| 12 // copyright notice, this list of conditions and the following disclaimer | |
| 13 // in the documentation and/or other materials provided with the | |
| 14 // distribution. | |
| 15 // * Neither the name of Google Inc. nor the names of its | |
| 16 // contributors may be used to endorse or promote products derived from | |
| 17 // this software without specific prior written permission. | |
| 18 // | |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 | |
| 31 #include "protobuf.h" | |
| 32 | |
| 33 // ----------------------------------------------------------------------------- | |
| 34 // Common utilities. | |
| 35 // ----------------------------------------------------------------------------- | |
| 36 | |
| 37 static const char* get_str(VALUE str) { | |
| 38 Check_Type(str, T_STRING); | |
| 39 return RSTRING_PTR(str); | |
| 40 } | |
| 41 | |
| 42 static VALUE rb_str_maybe_null(const char* s) { | |
| 43 if (s == NULL) { | |
| 44 s = ""; | |
| 45 } | |
| 46 return rb_str_new2(s); | |
| 47 } | |
| 48 | |
| 49 static upb_def* check_notfrozen(const upb_def* def) { | |
| 50 if (upb_def_isfrozen(def)) { | |
| 51 rb_raise(rb_eRuntimeError, | |
| 52 "Attempt to modify a frozen descriptor. Once descriptors are " | |
| 53 "added to the descriptor pool, they may not be modified."); | |
| 54 } | |
| 55 return (upb_def*)def; | |
| 56 } | |
| 57 | |
| 58 static upb_msgdef* check_msg_notfrozen(const upb_msgdef* def) { | |
| 59 return upb_downcast_msgdef_mutable(check_notfrozen((const upb_def*)def)); | |
| 60 } | |
| 61 | |
| 62 static upb_fielddef* check_field_notfrozen(const upb_fielddef* def) { | |
| 63 return upb_downcast_fielddef_mutable(check_notfrozen((const upb_def*)def)); | |
| 64 } | |
| 65 | |
| 66 static upb_oneofdef* check_oneof_notfrozen(const upb_oneofdef* def) { | |
| 67 return (upb_oneofdef*)check_notfrozen((const upb_def*)def); | |
| 68 } | |
| 69 | |
| 70 static upb_enumdef* check_enum_notfrozen(const upb_enumdef* def) { | |
| 71 return (upb_enumdef*)check_notfrozen((const upb_def*)def); | |
| 72 } | |
| 73 | |
| 74 // ----------------------------------------------------------------------------- | |
| 75 // DescriptorPool. | |
| 76 // ----------------------------------------------------------------------------- | |
| 77 | |
| 78 #define DEFINE_CLASS(name, string_name) \ | |
| 79 VALUE c ## name; \ | |
| 80 const rb_data_type_t _ ## name ## _type = { \ | |
| 81 string_name, \ | |
| 82 { name ## _mark, name ## _free, NULL }, \ | |
| 83 }; \ | |
| 84 name* ruby_to_ ## name(VALUE val) { \ | |
| 85 name* ret; \ | |
| 86 TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \ | |
| 87 return ret; \ | |
| 88 } \ | |
| 89 | |
| 90 #define DEFINE_SELF(type, var, rb_var) \ | |
| 91 type* var = ruby_to_ ## type(rb_var); | |
| 92 | |
| 93 // Global singleton DescriptorPool. The user is free to create others, but this | |
| 94 // is used by generated code. | |
| 95 VALUE generated_pool; | |
| 96 | |
| 97 DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool"); | |
| 98 | |
| 99 void DescriptorPool_mark(void* _self) { | |
| 100 } | |
| 101 | |
| 102 void DescriptorPool_free(void* _self) { | |
| 103 DescriptorPool* self = _self; | |
| 104 upb_symtab_unref(self->symtab, &self->symtab); | |
| 105 xfree(self); | |
| 106 } | |
| 107 | |
| 108 /* | |
| 109 * call-seq: | |
| 110 * DescriptorPool.new => pool | |
| 111 * | |
| 112 * Creates a new, empty, descriptor pool. | |
| 113 */ | |
| 114 VALUE DescriptorPool_alloc(VALUE klass) { | |
| 115 DescriptorPool* self = ALLOC(DescriptorPool); | |
| 116 self->symtab = upb_symtab_new(&self->symtab); | |
| 117 return TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self); | |
| 118 } | |
| 119 | |
| 120 void DescriptorPool_register(VALUE module) { | |
| 121 VALUE klass = rb_define_class_under( | |
| 122 module, "DescriptorPool", rb_cObject); | |
| 123 rb_define_alloc_func(klass, DescriptorPool_alloc); | |
| 124 rb_define_method(klass, "add", DescriptorPool_add, 1); | |
| 125 rb_define_method(klass, "build", DescriptorPool_build, 0); | |
| 126 rb_define_method(klass, "lookup", DescriptorPool_lookup, 1); | |
| 127 rb_define_singleton_method(klass, "generated_pool", | |
| 128 DescriptorPool_generated_pool, 0); | |
| 129 cDescriptorPool = klass; | |
| 130 rb_gc_register_address(&cDescriptorPool); | |
| 131 | |
| 132 generated_pool = rb_class_new_instance(0, NULL, klass); | |
| 133 rb_gc_register_address(&generated_pool); | |
| 134 } | |
| 135 | |
| 136 static void add_descriptor_to_pool(DescriptorPool* self, | |
| 137 Descriptor* descriptor) { | |
| 138 CHECK_UPB( | |
| 139 upb_symtab_add(self->symtab, (upb_def**)&descriptor->msgdef, 1, | |
| 140 NULL, &status), | |
| 141 "Adding Descriptor to DescriptorPool failed"); | |
| 142 } | |
| 143 | |
| 144 static void add_enumdesc_to_pool(DescriptorPool* self, | |
| 145 EnumDescriptor* enumdesc) { | |
| 146 CHECK_UPB( | |
| 147 upb_symtab_add(self->symtab, (upb_def**)&enumdesc->enumdef, 1, | |
| 148 NULL, &status), | |
| 149 "Adding EnumDescriptor to DescriptorPool failed"); | |
| 150 } | |
| 151 | |
| 152 /* | |
| 153 * call-seq: | |
| 154 * DescriptorPool.add(descriptor) | |
| 155 * | |
| 156 * Adds the given Descriptor or EnumDescriptor to this pool. All references to | |
| 157 * other types in a Descriptor's fields must be resolvable within this pool or | |
| 158 * an exception will be raised. | |
| 159 */ | |
| 160 VALUE DescriptorPool_add(VALUE _self, VALUE def) { | |
| 161 DEFINE_SELF(DescriptorPool, self, _self); | |
| 162 VALUE def_klass = rb_obj_class(def); | |
| 163 if (def_klass == cDescriptor) { | |
| 164 add_descriptor_to_pool(self, ruby_to_Descriptor(def)); | |
| 165 } else if (def_klass == cEnumDescriptor) { | |
| 166 add_enumdesc_to_pool(self, ruby_to_EnumDescriptor(def)); | |
| 167 } else { | |
| 168 rb_raise(rb_eArgError, | |
| 169 "Second argument must be a Descriptor or EnumDescriptor."); | |
| 170 } | |
| 171 return Qnil; | |
| 172 } | |
| 173 | |
| 174 /* | |
| 175 * call-seq: | |
| 176 * DescriptorPool.build(&block) | |
| 177 * | |
| 178 * Invokes the block with a Builder instance as self. All message and enum types | |
| 179 * added within the block are committed to the pool atomically, and may refer | |
| 180 * (co)recursively to each other. The user should call Builder#add_message and | |
| 181 * Builder#add_enum within the block as appropriate. This is the recommended, | |
| 182 * idiomatic way to define new message and enum types. | |
| 183 */ | |
| 184 VALUE DescriptorPool_build(VALUE _self) { | |
| 185 VALUE ctx = rb_class_new_instance(0, NULL, cBuilder); | |
| 186 VALUE block = rb_block_proc(); | |
| 187 rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); | |
| 188 rb_funcall(ctx, rb_intern("finalize_to_pool"), 1, _self); | |
| 189 return Qnil; | |
| 190 } | |
| 191 | |
| 192 /* | |
| 193 * call-seq: | |
| 194 * DescriptorPool.lookup(name) => descriptor | |
| 195 * | |
| 196 * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none | |
| 197 * exists with the given name. | |
| 198 */ | |
| 199 VALUE DescriptorPool_lookup(VALUE _self, VALUE name) { | |
| 200 DEFINE_SELF(DescriptorPool, self, _self); | |
| 201 const char* name_str = get_str(name); | |
| 202 const upb_def* def = upb_symtab_lookup(self->symtab, name_str); | |
| 203 if (!def) { | |
| 204 return Qnil; | |
| 205 } | |
| 206 return get_def_obj(def); | |
| 207 } | |
| 208 | |
| 209 /* | |
| 210 * call-seq: | |
| 211 * DescriptorPool.generated_pool => descriptor_pool | |
| 212 * | |
| 213 * Class method that returns the global DescriptorPool. This is a singleton into | |
| 214 * which generated-code message and enum types are registered. The user may also | |
| 215 * register types in this pool for convenience so that they do not have to hold | |
| 216 * a reference to a private pool instance. | |
| 217 */ | |
| 218 VALUE DescriptorPool_generated_pool(VALUE _self) { | |
| 219 return generated_pool; | |
| 220 } | |
| 221 | |
| 222 // ----------------------------------------------------------------------------- | |
| 223 // Descriptor. | |
| 224 // ----------------------------------------------------------------------------- | |
| 225 | |
| 226 DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor"); | |
| 227 | |
| 228 void Descriptor_mark(void* _self) { | |
| 229 Descriptor* self = _self; | |
| 230 rb_gc_mark(self->klass); | |
| 231 rb_gc_mark(self->typeclass_references); | |
| 232 } | |
| 233 | |
| 234 void Descriptor_free(void* _self) { | |
| 235 Descriptor* self = _self; | |
| 236 upb_msgdef_unref(self->msgdef, &self->msgdef); | |
| 237 if (self->layout) { | |
| 238 free_layout(self->layout); | |
| 239 } | |
| 240 if (self->fill_handlers) { | |
| 241 upb_handlers_unref(self->fill_handlers, &self->fill_handlers); | |
| 242 } | |
| 243 if (self->fill_method) { | |
| 244 upb_pbdecodermethod_unref(self->fill_method, &self->fill_method); | |
| 245 } | |
| 246 if (self->pb_serialize_handlers) { | |
| 247 upb_handlers_unref(self->pb_serialize_handlers, | |
| 248 &self->pb_serialize_handlers); | |
| 249 } | |
| 250 if (self->json_serialize_handlers) { | |
| 251 upb_handlers_unref(self->json_serialize_handlers, | |
| 252 &self->json_serialize_handlers); | |
| 253 } | |
| 254 xfree(self); | |
| 255 } | |
| 256 | |
| 257 /* | |
| 258 * call-seq: | |
| 259 * Descriptor.new => descriptor | |
| 260 * | |
| 261 * Creates a new, empty, message type descriptor. At a minimum, its name must be | |
| 262 * set before it is added to a pool. It cannot be used to create messages until | |
| 263 * it is added to a pool, after which it becomes immutable (as part of a | |
| 264 * finalization process). | |
| 265 */ | |
| 266 VALUE Descriptor_alloc(VALUE klass) { | |
| 267 Descriptor* self = ALLOC(Descriptor); | |
| 268 VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self); | |
| 269 self->msgdef = upb_msgdef_new(&self->msgdef); | |
| 270 self->klass = Qnil; | |
| 271 self->layout = NULL; | |
| 272 self->fill_handlers = NULL; | |
| 273 self->fill_method = NULL; | |
| 274 self->pb_serialize_handlers = NULL; | |
| 275 self->json_serialize_handlers = NULL; | |
| 276 self->typeclass_references = rb_ary_new(); | |
| 277 return ret; | |
| 278 } | |
| 279 | |
| 280 void Descriptor_register(VALUE module) { | |
| 281 VALUE klass = rb_define_class_under( | |
| 282 module, "Descriptor", rb_cObject); | |
| 283 rb_define_alloc_func(klass, Descriptor_alloc); | |
| 284 rb_define_method(klass, "each", Descriptor_each, 0); | |
| 285 rb_define_method(klass, "lookup", Descriptor_lookup, 1); | |
| 286 rb_define_method(klass, "add_field", Descriptor_add_field, 1); | |
| 287 rb_define_method(klass, "add_oneof", Descriptor_add_oneof, 1); | |
| 288 rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0); | |
| 289 rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1); | |
| 290 rb_define_method(klass, "msgclass", Descriptor_msgclass, 0); | |
| 291 rb_define_method(klass, "name", Descriptor_name, 0); | |
| 292 rb_define_method(klass, "name=", Descriptor_name_set, 1); | |
| 293 rb_include_module(klass, rb_mEnumerable); | |
| 294 cDescriptor = klass; | |
| 295 rb_gc_register_address(&cDescriptor); | |
| 296 } | |
| 297 | |
| 298 /* | |
| 299 * call-seq: | |
| 300 * Descriptor.name => name | |
| 301 * | |
| 302 * Returns the name of this message type as a fully-qualfied string (e.g., | |
| 303 * My.Package.MessageType). | |
| 304 */ | |
| 305 VALUE Descriptor_name(VALUE _self) { | |
| 306 DEFINE_SELF(Descriptor, self, _self); | |
| 307 return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef)); | |
| 308 } | |
| 309 | |
| 310 /* | |
| 311 * call-seq: | |
| 312 * Descriptor.name = name | |
| 313 * | |
| 314 * Assigns a name to this message type. The descriptor must not have been added | |
| 315 * to a pool yet. | |
| 316 */ | |
| 317 VALUE Descriptor_name_set(VALUE _self, VALUE str) { | |
| 318 DEFINE_SELF(Descriptor, self, _self); | |
| 319 upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef); | |
| 320 const char* name = get_str(str); | |
| 321 CHECK_UPB( | |
| 322 upb_msgdef_setfullname(mut_def, name, &status), | |
| 323 "Error setting Descriptor name"); | |
| 324 return Qnil; | |
| 325 } | |
| 326 | |
| 327 /* | |
| 328 * call-seq: | |
| 329 * Descriptor.each(&block) | |
| 330 * | |
| 331 * Iterates over fields in this message type, yielding to the block on each one. | |
| 332 */ | |
| 333 VALUE Descriptor_each(VALUE _self) { | |
| 334 DEFINE_SELF(Descriptor, self, _self); | |
| 335 | |
| 336 upb_msg_field_iter it; | |
| 337 for (upb_msg_field_begin(&it, self->msgdef); | |
| 338 !upb_msg_field_done(&it); | |
| 339 upb_msg_field_next(&it)) { | |
| 340 const upb_fielddef* field = upb_msg_iter_field(&it); | |
| 341 VALUE obj = get_def_obj(field); | |
| 342 rb_yield(obj); | |
| 343 } | |
| 344 return Qnil; | |
| 345 } | |
| 346 | |
| 347 /* | |
| 348 * call-seq: | |
| 349 * Descriptor.lookup(name) => FieldDescriptor | |
| 350 * | |
| 351 * Returns the field descriptor for the field with the given name, if present, | |
| 352 * or nil if none. | |
| 353 */ | |
| 354 VALUE Descriptor_lookup(VALUE _self, VALUE name) { | |
| 355 DEFINE_SELF(Descriptor, self, _self); | |
| 356 const char* s = get_str(name); | |
| 357 const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s); | |
| 358 if (field == NULL) { | |
| 359 return Qnil; | |
| 360 } | |
| 361 return get_def_obj(field); | |
| 362 } | |
| 363 | |
| 364 /* | |
| 365 * call-seq: | |
| 366 * Descriptor.add_field(field) => nil | |
| 367 * | |
| 368 * Adds the given FieldDescriptor to this message type. This descriptor must not | |
| 369 * have been added to a pool yet. Raises an exception if a field with the same | |
| 370 * name or number already exists. Sub-type references (e.g. for fields of type | |
| 371 * message) are not resolved at this point. | |
| 372 */ | |
| 373 VALUE Descriptor_add_field(VALUE _self, VALUE obj) { | |
| 374 DEFINE_SELF(Descriptor, self, _self); | |
| 375 upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef); | |
| 376 FieldDescriptor* def = ruby_to_FieldDescriptor(obj); | |
| 377 upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef); | |
| 378 CHECK_UPB( | |
| 379 upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status), | |
| 380 "Adding field to Descriptor failed"); | |
| 381 add_def_obj(def->fielddef, obj); | |
| 382 return Qnil; | |
| 383 } | |
| 384 | |
| 385 /* | |
| 386 * call-seq: | |
| 387 * Descriptor.add_oneof(oneof) => nil | |
| 388 * | |
| 389 * Adds the given OneofDescriptor to this message type. This descriptor must not | |
| 390 * have been added to a pool yet. Raises an exception if a oneof with the same | |
| 391 * name already exists, or if any of the oneof's fields' names or numbers | |
| 392 * conflict with an existing field in this message type. All fields in the oneof | |
| 393 * are added to the message descriptor. Sub-type references (e.g. for fields of | |
| 394 * type message) are not resolved at this point. | |
| 395 */ | |
| 396 VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) { | |
| 397 DEFINE_SELF(Descriptor, self, _self); | |
| 398 upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef); | |
| 399 OneofDescriptor* def = ruby_to_OneofDescriptor(obj); | |
| 400 upb_oneofdef* mut_oneof_def = check_oneof_notfrozen(def->oneofdef); | |
| 401 CHECK_UPB( | |
| 402 upb_msgdef_addoneof(mut_def, mut_oneof_def, NULL, &status), | |
| 403 "Adding oneof to Descriptor failed"); | |
| 404 add_def_obj(def->oneofdef, obj); | |
| 405 return Qnil; | |
| 406 } | |
| 407 | |
| 408 /* | |
| 409 * call-seq: | |
| 410 * Descriptor.each_oneof(&block) => nil | |
| 411 * | |
| 412 * Invokes the given block for each oneof in this message type, passing the | |
| 413 * corresponding OneofDescriptor. | |
| 414 */ | |
| 415 VALUE Descriptor_each_oneof(VALUE _self) { | |
| 416 DEFINE_SELF(Descriptor, self, _self); | |
| 417 | |
| 418 upb_msg_oneof_iter it; | |
| 419 for (upb_msg_oneof_begin(&it, self->msgdef); | |
| 420 !upb_msg_oneof_done(&it); | |
| 421 upb_msg_oneof_next(&it)) { | |
| 422 const upb_oneofdef* oneof = upb_msg_iter_oneof(&it); | |
| 423 VALUE obj = get_def_obj(oneof); | |
| 424 rb_yield(obj); | |
| 425 } | |
| 426 return Qnil; | |
| 427 } | |
| 428 | |
| 429 /* | |
| 430 * call-seq: | |
| 431 * Descriptor.lookup_oneof(name) => OneofDescriptor | |
| 432 * | |
| 433 * Returns the oneof descriptor for the oneof with the given name, if present, | |
| 434 * or nil if none. | |
| 435 */ | |
| 436 VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) { | |
| 437 DEFINE_SELF(Descriptor, self, _self); | |
| 438 const char* s = get_str(name); | |
| 439 const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s); | |
| 440 if (oneof == NULL) { | |
| 441 return Qnil; | |
| 442 } | |
| 443 return get_def_obj(oneof); | |
| 444 } | |
| 445 | |
| 446 /* | |
| 447 * call-seq: | |
| 448 * Descriptor.msgclass => message_klass | |
| 449 * | |
| 450 * Returns the Ruby class created for this message type. Valid only once the | |
| 451 * message type has been added to a pool. | |
| 452 */ | |
| 453 VALUE Descriptor_msgclass(VALUE _self) { | |
| 454 DEFINE_SELF(Descriptor, self, _self); | |
| 455 if (!upb_def_isfrozen((const upb_def*)self->msgdef)) { | |
| 456 rb_raise(rb_eRuntimeError, | |
| 457 "Cannot fetch message class from a Descriptor not yet in a pool."); | |
| 458 } | |
| 459 if (self->klass == Qnil) { | |
| 460 self->klass = build_class_from_descriptor(self); | |
| 461 } | |
| 462 return self->klass; | |
| 463 } | |
| 464 | |
| 465 // ----------------------------------------------------------------------------- | |
| 466 // FieldDescriptor. | |
| 467 // ----------------------------------------------------------------------------- | |
| 468 | |
| 469 DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor"); | |
| 470 | |
| 471 void FieldDescriptor_mark(void* _self) { | |
| 472 } | |
| 473 | |
| 474 void FieldDescriptor_free(void* _self) { | |
| 475 FieldDescriptor* self = _self; | |
| 476 upb_fielddef_unref(self->fielddef, &self->fielddef); | |
| 477 xfree(self); | |
| 478 } | |
| 479 | |
| 480 /* | |
| 481 * call-seq: | |
| 482 * FieldDescriptor.new => field | |
| 483 * | |
| 484 * Returns a new field descriptor. Its name, type, etc. must be set before it is | |
| 485 * added to a message type. | |
| 486 */ | |
| 487 VALUE FieldDescriptor_alloc(VALUE klass) { | |
| 488 FieldDescriptor* self = ALLOC(FieldDescriptor); | |
| 489 VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self); | |
| 490 upb_fielddef* fielddef = upb_fielddef_new(&self->fielddef); | |
| 491 upb_fielddef_setpacked(fielddef, false); | |
| 492 self->fielddef = fielddef; | |
| 493 return ret; | |
| 494 } | |
| 495 | |
| 496 void FieldDescriptor_register(VALUE module) { | |
| 497 VALUE klass = rb_define_class_under( | |
| 498 module, "FieldDescriptor", rb_cObject); | |
| 499 rb_define_alloc_func(klass, FieldDescriptor_alloc); | |
| 500 rb_define_method(klass, "name", FieldDescriptor_name, 0); | |
| 501 rb_define_method(klass, "name=", FieldDescriptor_name_set, 1); | |
| 502 rb_define_method(klass, "type", FieldDescriptor_type, 0); | |
| 503 rb_define_method(klass, "type=", FieldDescriptor_type_set, 1); | |
| 504 rb_define_method(klass, "label", FieldDescriptor_label, 0); | |
| 505 rb_define_method(klass, "label=", FieldDescriptor_label_set, 1); | |
| 506 rb_define_method(klass, "number", FieldDescriptor_number, 0); | |
| 507 rb_define_method(klass, "number=", FieldDescriptor_number_set, 1); | |
| 508 rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0); | |
| 509 rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1); | |
| 510 rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0); | |
| 511 rb_define_method(klass, "get", FieldDescriptor_get, 1); | |
| 512 rb_define_method(klass, "set", FieldDescriptor_set, 2); | |
| 513 cFieldDescriptor = klass; | |
| 514 rb_gc_register_address(&cFieldDescriptor); | |
| 515 } | |
| 516 | |
| 517 /* | |
| 518 * call-seq: | |
| 519 * FieldDescriptor.name => name | |
| 520 * | |
| 521 * Returns the name of this field. | |
| 522 */ | |
| 523 VALUE FieldDescriptor_name(VALUE _self) { | |
| 524 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 525 return rb_str_maybe_null(upb_fielddef_name(self->fielddef)); | |
| 526 } | |
| 527 | |
| 528 /* | |
| 529 * call-seq: | |
| 530 * FieldDescriptor.name = name | |
| 531 * | |
| 532 * Sets the name of this field. Cannot be called once the containing message | |
| 533 * type, if any, is added to a pool. | |
| 534 */ | |
| 535 VALUE FieldDescriptor_name_set(VALUE _self, VALUE str) { | |
| 536 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 537 upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); | |
| 538 const char* name = get_str(str); | |
| 539 CHECK_UPB(upb_fielddef_setname(mut_def, name, &status), | |
| 540 "Error setting FieldDescriptor name"); | |
| 541 return Qnil; | |
| 542 } | |
| 543 | |
| 544 upb_fieldtype_t ruby_to_fieldtype(VALUE type) { | |
| 545 if (TYPE(type) != T_SYMBOL) { | |
| 546 rb_raise(rb_eArgError, "Expected symbol for field type."); | |
| 547 } | |
| 548 | |
| 549 #define CONVERT(upb, ruby) \ | |
| 550 if (SYM2ID(type) == rb_intern( # ruby )) { \ | |
| 551 return UPB_TYPE_ ## upb; \ | |
| 552 } | |
| 553 | |
| 554 CONVERT(FLOAT, float); | |
| 555 CONVERT(DOUBLE, double); | |
| 556 CONVERT(BOOL, bool); | |
| 557 CONVERT(STRING, string); | |
| 558 CONVERT(BYTES, bytes); | |
| 559 CONVERT(MESSAGE, message); | |
| 560 CONVERT(ENUM, enum); | |
| 561 CONVERT(INT32, int32); | |
| 562 CONVERT(INT64, int64); | |
| 563 CONVERT(UINT32, uint32); | |
| 564 CONVERT(UINT64, uint64); | |
| 565 | |
| 566 #undef CONVERT | |
| 567 | |
| 568 rb_raise(rb_eArgError, "Unknown field type."); | |
| 569 return 0; | |
| 570 } | |
| 571 | |
| 572 VALUE fieldtype_to_ruby(upb_fieldtype_t type) { | |
| 573 switch (type) { | |
| 574 #define CONVERT(upb, ruby) \ | |
| 575 case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby )); | |
| 576 CONVERT(FLOAT, float); | |
| 577 CONVERT(DOUBLE, double); | |
| 578 CONVERT(BOOL, bool); | |
| 579 CONVERT(STRING, string); | |
| 580 CONVERT(BYTES, bytes); | |
| 581 CONVERT(MESSAGE, message); | |
| 582 CONVERT(ENUM, enum); | |
| 583 CONVERT(INT32, int32); | |
| 584 CONVERT(INT64, int64); | |
| 585 CONVERT(UINT32, uint32); | |
| 586 CONVERT(UINT64, uint64); | |
| 587 #undef CONVERT | |
| 588 } | |
| 589 return Qnil; | |
| 590 } | |
| 591 | |
| 592 upb_descriptortype_t ruby_to_descriptortype(VALUE type) { | |
| 593 if (TYPE(type) != T_SYMBOL) { | |
| 594 rb_raise(rb_eArgError, "Expected symbol for field type."); | |
| 595 } | |
| 596 | |
| 597 #define CONVERT(upb, ruby) \ | |
| 598 if (SYM2ID(type) == rb_intern( # ruby )) { \ | |
| 599 return UPB_DESCRIPTOR_TYPE_ ## upb; \ | |
| 600 } | |
| 601 | |
| 602 CONVERT(FLOAT, float); | |
| 603 CONVERT(DOUBLE, double); | |
| 604 CONVERT(BOOL, bool); | |
| 605 CONVERT(STRING, string); | |
| 606 CONVERT(BYTES, bytes); | |
| 607 CONVERT(MESSAGE, message); | |
| 608 CONVERT(GROUP, group); | |
| 609 CONVERT(ENUM, enum); | |
| 610 CONVERT(INT32, int32); | |
| 611 CONVERT(INT64, int64); | |
| 612 CONVERT(UINT32, uint32); | |
| 613 CONVERT(UINT64, uint64); | |
| 614 CONVERT(SINT32, sint32); | |
| 615 CONVERT(SINT64, sint64); | |
| 616 CONVERT(FIXED32, fixed32); | |
| 617 CONVERT(FIXED64, fixed64); | |
| 618 CONVERT(SFIXED32, sfixed32); | |
| 619 CONVERT(SFIXED64, sfixed64); | |
| 620 | |
| 621 #undef CONVERT | |
| 622 | |
| 623 rb_raise(rb_eArgError, "Unknown field type."); | |
| 624 return 0; | |
| 625 } | |
| 626 | |
| 627 VALUE descriptortype_to_ruby(upb_descriptortype_t type) { | |
| 628 switch (type) { | |
| 629 #define CONVERT(upb, ruby) \ | |
| 630 case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby )); | |
| 631 CONVERT(FLOAT, float); | |
| 632 CONVERT(DOUBLE, double); | |
| 633 CONVERT(BOOL, bool); | |
| 634 CONVERT(STRING, string); | |
| 635 CONVERT(BYTES, bytes); | |
| 636 CONVERT(MESSAGE, message); | |
| 637 CONVERT(GROUP, group); | |
| 638 CONVERT(ENUM, enum); | |
| 639 CONVERT(INT32, int32); | |
| 640 CONVERT(INT64, int64); | |
| 641 CONVERT(UINT32, uint32); | |
| 642 CONVERT(UINT64, uint64); | |
| 643 CONVERT(SINT32, sint32); | |
| 644 CONVERT(SINT64, sint64); | |
| 645 CONVERT(FIXED32, fixed32); | |
| 646 CONVERT(FIXED64, fixed64); | |
| 647 CONVERT(SFIXED32, sfixed32); | |
| 648 CONVERT(SFIXED64, sfixed64); | |
| 649 #undef CONVERT | |
| 650 } | |
| 651 return Qnil; | |
| 652 } | |
| 653 | |
| 654 /* | |
| 655 * call-seq: | |
| 656 * FieldDescriptor.type => type | |
| 657 * | |
| 658 * Returns this field's type, as a Ruby symbol, or nil if not yet set. | |
| 659 * | |
| 660 * Valid field types are: | |
| 661 * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string, | |
| 662 * :bytes, :message. | |
| 663 */ | |
| 664 VALUE FieldDescriptor_type(VALUE _self) { | |
| 665 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 666 if (!upb_fielddef_typeisset(self->fielddef)) { | |
| 667 return Qnil; | |
| 668 } | |
| 669 return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef)); | |
| 670 } | |
| 671 | |
| 672 /* | |
| 673 * call-seq: | |
| 674 * FieldDescriptor.type = type | |
| 675 * | |
| 676 * Sets this field's type. Cannot be called if field is part of a message type | |
| 677 * already in a pool. | |
| 678 */ | |
| 679 VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) { | |
| 680 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 681 upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); | |
| 682 upb_fielddef_setdescriptortype(mut_def, ruby_to_descriptortype(type)); | |
| 683 return Qnil; | |
| 684 } | |
| 685 | |
| 686 /* | |
| 687 * call-seq: | |
| 688 * FieldDescriptor.label => label | |
| 689 * | |
| 690 * Returns this field's label (i.e., plurality), as a Ruby symbol. | |
| 691 * | |
| 692 * Valid field labels are: | |
| 693 * :optional, :repeated | |
| 694 */ | |
| 695 VALUE FieldDescriptor_label(VALUE _self) { | |
| 696 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 697 switch (upb_fielddef_label(self->fielddef)) { | |
| 698 #define CONVERT(upb, ruby) \ | |
| 699 case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby )); | |
| 700 | |
| 701 CONVERT(OPTIONAL, optional); | |
| 702 CONVERT(REQUIRED, required); | |
| 703 CONVERT(REPEATED, repeated); | |
| 704 | |
| 705 #undef CONVERT | |
| 706 } | |
| 707 | |
| 708 return Qnil; | |
| 709 } | |
| 710 | |
| 711 /* | |
| 712 * call-seq: | |
| 713 * FieldDescriptor.label = label | |
| 714 * | |
| 715 * Sets the label on this field. Cannot be called if field is part of a message | |
| 716 * type already in a pool. | |
| 717 */ | |
| 718 VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) { | |
| 719 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 720 upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); | |
| 721 if (TYPE(label) != T_SYMBOL) { | |
| 722 rb_raise(rb_eArgError, "Expected symbol for field label."); | |
| 723 } | |
| 724 | |
| 725 upb_label_t upb_label = -1; | |
| 726 bool converted = false; | |
| 727 | |
| 728 #define CONVERT(upb, ruby) \ | |
| 729 if (SYM2ID(label) == rb_intern( # ruby )) { \ | |
| 730 upb_label = UPB_LABEL_ ## upb; \ | |
| 731 converted = true; \ | |
| 732 } | |
| 733 | |
| 734 CONVERT(OPTIONAL, optional); | |
| 735 CONVERT(REQUIRED, required); | |
| 736 CONVERT(REPEATED, repeated); | |
| 737 | |
| 738 #undef CONVERT | |
| 739 | |
| 740 if (!converted) { | |
| 741 rb_raise(rb_eArgError, "Unknown field label."); | |
| 742 } | |
| 743 | |
| 744 upb_fielddef_setlabel(mut_def, upb_label); | |
| 745 | |
| 746 return Qnil; | |
| 747 } | |
| 748 | |
| 749 /* | |
| 750 * call-seq: | |
| 751 * FieldDescriptor.number => number | |
| 752 * | |
| 753 * Returns the tag number for this field. | |
| 754 */ | |
| 755 VALUE FieldDescriptor_number(VALUE _self) { | |
| 756 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 757 return INT2NUM(upb_fielddef_number(self->fielddef)); | |
| 758 } | |
| 759 | |
| 760 /* | |
| 761 * call-seq: | |
| 762 * FieldDescriptor.number = number | |
| 763 * | |
| 764 * Sets the tag number for this field. Cannot be called if field is part of a | |
| 765 * message type already in a pool. | |
| 766 */ | |
| 767 VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) { | |
| 768 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 769 upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); | |
| 770 CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status), | |
| 771 "Error setting field number"); | |
| 772 return Qnil; | |
| 773 } | |
| 774 | |
| 775 /* | |
| 776 * call-seq: | |
| 777 * FieldDescriptor.submsg_name => submsg_name | |
| 778 * | |
| 779 * Returns the name of the message or enum type corresponding to this field, if | |
| 780 * it is a message or enum field (respectively), or nil otherwise. This type | |
| 781 * name will be resolved within the context of the pool to which the containing | |
| 782 * message type is added. | |
| 783 */ | |
| 784 VALUE FieldDescriptor_submsg_name(VALUE _self) { | |
| 785 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 786 if (!upb_fielddef_hassubdef(self->fielddef)) { | |
| 787 return Qnil; | |
| 788 } | |
| 789 return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef)); | |
| 790 } | |
| 791 | |
| 792 /* | |
| 793 * call-seq: | |
| 794 * FieldDescriptor.submsg_name = submsg_name | |
| 795 * | |
| 796 * Sets the name of the message or enum type corresponding to this field, if it | |
| 797 * is a message or enum field (respectively). This type name will be resolved | |
| 798 * within the context of the pool to which the containing message type is added. | |
| 799 * Cannot be called on field that are not of message or enum type, or on fields | |
| 800 * that are part of a message type already added to a pool. | |
| 801 */ | |
| 802 VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) { | |
| 803 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 804 upb_fielddef* mut_def = check_field_notfrozen(self->fielddef); | |
| 805 if (!upb_fielddef_hassubdef(self->fielddef)) { | |
| 806 rb_raise(rb_eTypeError, "FieldDescriptor does not have subdef."); | |
| 807 } | |
| 808 const char* str = get_str(value); | |
| 809 CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status), | |
| 810 "Error setting submessage name"); | |
| 811 return Qnil; | |
| 812 } | |
| 813 | |
| 814 /* | |
| 815 * call-seq: | |
| 816 * FieldDescriptor.subtype => message_or_enum_descriptor | |
| 817 * | |
| 818 * Returns the message or enum descriptor corresponding to this field's type if | |
| 819 * it is a message or enum field, respectively, or nil otherwise. Cannot be | |
| 820 * called *until* the containing message type is added to a pool (and thus | |
| 821 * resolved). | |
| 822 */ | |
| 823 VALUE FieldDescriptor_subtype(VALUE _self) { | |
| 824 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 825 if (!upb_fielddef_hassubdef(self->fielddef)) { | |
| 826 return Qnil; | |
| 827 } | |
| 828 const upb_def* def = upb_fielddef_subdef(self->fielddef); | |
| 829 if (def == NULL) { | |
| 830 return Qnil; | |
| 831 } | |
| 832 return get_def_obj(def); | |
| 833 } | |
| 834 | |
| 835 /* | |
| 836 * call-seq: | |
| 837 * FieldDescriptor.get(message) => value | |
| 838 * | |
| 839 * Returns the value set for this field on the given message. Raises an | |
| 840 * exception if message is of the wrong type. | |
| 841 */ | |
| 842 VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) { | |
| 843 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 844 MessageHeader* msg; | |
| 845 TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); | |
| 846 if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) { | |
| 847 rb_raise(rb_eTypeError, "get method called on wrong message type"); | |
| 848 } | |
| 849 return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef); | |
| 850 } | |
| 851 | |
| 852 /* | |
| 853 * call-seq: | |
| 854 * FieldDescriptor.set(message, value) | |
| 855 * | |
| 856 * Sets the value corresponding to this field to the given value on the given | |
| 857 * message. Raises an exception if message is of the wrong type. Performs the | |
| 858 * ordinary type-checks for field setting. | |
| 859 */ | |
| 860 VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) { | |
| 861 DEFINE_SELF(FieldDescriptor, self, _self); | |
| 862 MessageHeader* msg; | |
| 863 TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg); | |
| 864 if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) { | |
| 865 rb_raise(rb_eTypeError, "set method called on wrong message type"); | |
| 866 } | |
| 867 layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value); | |
| 868 return Qnil; | |
| 869 } | |
| 870 | |
| 871 // ----------------------------------------------------------------------------- | |
| 872 // OneofDescriptor. | |
| 873 // ----------------------------------------------------------------------------- | |
| 874 | |
| 875 DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor"); | |
| 876 | |
| 877 void OneofDescriptor_mark(void* _self) { | |
| 878 } | |
| 879 | |
| 880 void OneofDescriptor_free(void* _self) { | |
| 881 OneofDescriptor* self = _self; | |
| 882 upb_oneofdef_unref(self->oneofdef, &self->oneofdef); | |
| 883 xfree(self); | |
| 884 } | |
| 885 | |
| 886 /* | |
| 887 * call-seq: | |
| 888 * OneofDescriptor.new => oneof_descriptor | |
| 889 * | |
| 890 * Creates a new, empty, oneof descriptor. The oneof may only be modified prior | |
| 891 * to being added to a message descriptor which is subsequently added to a pool. | |
| 892 */ | |
| 893 VALUE OneofDescriptor_alloc(VALUE klass) { | |
| 894 OneofDescriptor* self = ALLOC(OneofDescriptor); | |
| 895 VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self); | |
| 896 self->oneofdef = upb_oneofdef_new(&self->oneofdef); | |
| 897 return ret; | |
| 898 } | |
| 899 | |
| 900 void OneofDescriptor_register(VALUE module) { | |
| 901 VALUE klass = rb_define_class_under( | |
| 902 module, "OneofDescriptor", rb_cObject); | |
| 903 rb_define_alloc_func(klass, OneofDescriptor_alloc); | |
| 904 rb_define_method(klass, "name", OneofDescriptor_name, 0); | |
| 905 rb_define_method(klass, "name=", OneofDescriptor_name_set, 1); | |
| 906 rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1); | |
| 907 rb_define_method(klass, "each", OneofDescriptor_each, 0); | |
| 908 rb_include_module(klass, rb_mEnumerable); | |
| 909 cOneofDescriptor = klass; | |
| 910 rb_gc_register_address(&cOneofDescriptor); | |
| 911 } | |
| 912 | |
| 913 /* | |
| 914 * call-seq: | |
| 915 * OneofDescriptor.name => name | |
| 916 * | |
| 917 * Returns the name of this oneof. | |
| 918 */ | |
| 919 VALUE OneofDescriptor_name(VALUE _self) { | |
| 920 DEFINE_SELF(OneofDescriptor, self, _self); | |
| 921 return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef)); | |
| 922 } | |
| 923 | |
| 924 /* | |
| 925 * call-seq: | |
| 926 * OneofDescriptor.name = name | |
| 927 * | |
| 928 * Sets a new name for this oneof. The oneof must not have been added to a | |
| 929 * message descriptor yet. | |
| 930 */ | |
| 931 VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) { | |
| 932 DEFINE_SELF(OneofDescriptor, self, _self); | |
| 933 upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef); | |
| 934 const char* str = get_str(value); | |
| 935 CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status), | |
| 936 "Error setting oneof name"); | |
| 937 return Qnil; | |
| 938 } | |
| 939 | |
| 940 /* | |
| 941 * call-seq: | |
| 942 * OneofDescriptor.add_field(field) => nil | |
| 943 * | |
| 944 * Adds a field to this oneof. The field may have been added to this oneof in | |
| 945 * the past, or the message to which this oneof belongs (if any), but may not | |
| 946 * have already been added to any other oneof or message. Otherwise, an | |
| 947 * exception is raised. | |
| 948 * | |
| 949 * All fields added to the oneof via this method will be automatically added to | |
| 950 * the message to which this oneof belongs, if it belongs to one currently, or | |
| 951 * else will be added to any message to which the oneof is later added at the | |
| 952 * time that it is added. | |
| 953 */ | |
| 954 VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) { | |
| 955 DEFINE_SELF(OneofDescriptor, self, _self); | |
| 956 upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef); | |
| 957 FieldDescriptor* def = ruby_to_FieldDescriptor(obj); | |
| 958 upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef); | |
| 959 CHECK_UPB( | |
| 960 upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status), | |
| 961 "Adding field to OneofDescriptor failed"); | |
| 962 add_def_obj(def->fielddef, obj); | |
| 963 return Qnil; | |
| 964 } | |
| 965 | |
| 966 /* | |
| 967 * call-seq: | |
| 968 * OneofDescriptor.each(&block) => nil | |
| 969 * | |
| 970 * Iterates through fields in this oneof, yielding to the block on each one. | |
| 971 */ | |
| 972 VALUE OneofDescriptor_each(VALUE _self, VALUE field) { | |
| 973 DEFINE_SELF(OneofDescriptor, self, _self); | |
| 974 upb_oneof_iter it; | |
| 975 for (upb_oneof_begin(&it, self->oneofdef); | |
| 976 !upb_oneof_done(&it); | |
| 977 upb_oneof_next(&it)) { | |
| 978 const upb_fielddef* f = upb_oneof_iter_field(&it); | |
| 979 VALUE obj = get_def_obj(f); | |
| 980 rb_yield(obj); | |
| 981 } | |
| 982 return Qnil; | |
| 983 } | |
| 984 | |
| 985 // ----------------------------------------------------------------------------- | |
| 986 // EnumDescriptor. | |
| 987 // ----------------------------------------------------------------------------- | |
| 988 | |
| 989 DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor"); | |
| 990 | |
| 991 void EnumDescriptor_mark(void* _self) { | |
| 992 EnumDescriptor* self = _self; | |
| 993 rb_gc_mark(self->module); | |
| 994 } | |
| 995 | |
| 996 void EnumDescriptor_free(void* _self) { | |
| 997 EnumDescriptor* self = _self; | |
| 998 upb_enumdef_unref(self->enumdef, &self->enumdef); | |
| 999 xfree(self); | |
| 1000 } | |
| 1001 | |
| 1002 /* | |
| 1003 * call-seq: | |
| 1004 * EnumDescriptor.new => enum_descriptor | |
| 1005 * | |
| 1006 * Creates a new, empty, enum descriptor. Must be added to a pool before the | |
| 1007 * enum type can be used. The enum type may only be modified prior to adding to | |
| 1008 * a pool. | |
| 1009 */ | |
| 1010 VALUE EnumDescriptor_alloc(VALUE klass) { | |
| 1011 EnumDescriptor* self = ALLOC(EnumDescriptor); | |
| 1012 VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self); | |
| 1013 self->enumdef = upb_enumdef_new(&self->enumdef); | |
| 1014 self->module = Qnil; | |
| 1015 return ret; | |
| 1016 } | |
| 1017 | |
| 1018 void EnumDescriptor_register(VALUE module) { | |
| 1019 VALUE klass = rb_define_class_under( | |
| 1020 module, "EnumDescriptor", rb_cObject); | |
| 1021 rb_define_alloc_func(klass, EnumDescriptor_alloc); | |
| 1022 rb_define_method(klass, "name", EnumDescriptor_name, 0); | |
| 1023 rb_define_method(klass, "name=", EnumDescriptor_name_set, 1); | |
| 1024 rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2); | |
| 1025 rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1); | |
| 1026 rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1); | |
| 1027 rb_define_method(klass, "each", EnumDescriptor_each, 0); | |
| 1028 rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0); | |
| 1029 rb_include_module(klass, rb_mEnumerable); | |
| 1030 cEnumDescriptor = klass; | |
| 1031 rb_gc_register_address(&cEnumDescriptor); | |
| 1032 } | |
| 1033 | |
| 1034 /* | |
| 1035 * call-seq: | |
| 1036 * EnumDescriptor.name => name | |
| 1037 * | |
| 1038 * Returns the name of this enum type. | |
| 1039 */ | |
| 1040 VALUE EnumDescriptor_name(VALUE _self) { | |
| 1041 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1042 return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef)); | |
| 1043 } | |
| 1044 | |
| 1045 /* | |
| 1046 * call-seq: | |
| 1047 * EnumDescriptor.name = name | |
| 1048 * | |
| 1049 * Sets the name of this enum type. Cannot be called if the enum type has | |
| 1050 * already been added to a pool. | |
| 1051 */ | |
| 1052 VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) { | |
| 1053 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1054 upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef); | |
| 1055 const char* name = get_str(str); | |
| 1056 CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status), | |
| 1057 "Error setting EnumDescriptor name"); | |
| 1058 return Qnil; | |
| 1059 } | |
| 1060 | |
| 1061 /* | |
| 1062 * call-seq: | |
| 1063 * EnumDescriptor.add_value(key, value) | |
| 1064 * | |
| 1065 * Adds a new key => value mapping to this enum type. Key must be given as a | |
| 1066 * Ruby symbol. Cannot be called if the enum type has already been added to a | |
| 1067 * pool. Will raise an exception if the key or value is already in use. | |
| 1068 */ | |
| 1069 VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) { | |
| 1070 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1071 upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef); | |
| 1072 const char* name_str = rb_id2name(SYM2ID(name)); | |
| 1073 int32_t val = NUM2INT(number); | |
| 1074 CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status), | |
| 1075 "Error adding value to enum"); | |
| 1076 return Qnil; | |
| 1077 } | |
| 1078 | |
| 1079 /* | |
| 1080 * call-seq: | |
| 1081 * EnumDescriptor.lookup_name(name) => value | |
| 1082 * | |
| 1083 * Returns the numeric value corresponding to the given key name (as a Ruby | |
| 1084 * symbol), or nil if none. | |
| 1085 */ | |
| 1086 VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) { | |
| 1087 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1088 const char* name_str= rb_id2name(SYM2ID(name)); | |
| 1089 int32_t val = 0; | |
| 1090 if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) { | |
| 1091 return INT2NUM(val); | |
| 1092 } else { | |
| 1093 return Qnil; | |
| 1094 } | |
| 1095 } | |
| 1096 | |
| 1097 /* | |
| 1098 * call-seq: | |
| 1099 * EnumDescriptor.lookup_value(name) => value | |
| 1100 * | |
| 1101 * Returns the key name (as a Ruby symbol) corresponding to the integer value, | |
| 1102 * or nil if none. | |
| 1103 */ | |
| 1104 VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) { | |
| 1105 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1106 int32_t val = NUM2INT(number); | |
| 1107 const char* name = upb_enumdef_iton(self->enumdef, val); | |
| 1108 if (name != NULL) { | |
| 1109 return ID2SYM(rb_intern(name)); | |
| 1110 } else { | |
| 1111 return Qnil; | |
| 1112 } | |
| 1113 } | |
| 1114 | |
| 1115 /* | |
| 1116 * call-seq: | |
| 1117 * EnumDescriptor.each(&block) | |
| 1118 * | |
| 1119 * Iterates over key => value mappings in this enum's definition, yielding to | |
| 1120 * the block with (key, value) arguments for each one. | |
| 1121 */ | |
| 1122 VALUE EnumDescriptor_each(VALUE _self) { | |
| 1123 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1124 | |
| 1125 upb_enum_iter it; | |
| 1126 for (upb_enum_begin(&it, self->enumdef); | |
| 1127 !upb_enum_done(&it); | |
| 1128 upb_enum_next(&it)) { | |
| 1129 VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it))); | |
| 1130 VALUE number = INT2NUM(upb_enum_iter_number(&it)); | |
| 1131 rb_yield_values(2, key, number); | |
| 1132 } | |
| 1133 | |
| 1134 return Qnil; | |
| 1135 } | |
| 1136 | |
| 1137 /* | |
| 1138 * call-seq: | |
| 1139 * EnumDescriptor.enummodule => module | |
| 1140 * | |
| 1141 * Returns the Ruby module corresponding to this enum type. Cannot be called | |
| 1142 * until the enum descriptor has been added to a pool. | |
| 1143 */ | |
| 1144 VALUE EnumDescriptor_enummodule(VALUE _self) { | |
| 1145 DEFINE_SELF(EnumDescriptor, self, _self); | |
| 1146 if (!upb_def_isfrozen((const upb_def*)self->enumdef)) { | |
| 1147 rb_raise(rb_eRuntimeError, | |
| 1148 "Cannot fetch enum module from an EnumDescriptor not yet " | |
| 1149 "in a pool."); | |
| 1150 } | |
| 1151 if (self->module == Qnil) { | |
| 1152 self->module = build_module_from_enumdesc(self); | |
| 1153 } | |
| 1154 return self->module; | |
| 1155 } | |
| 1156 | |
| 1157 // ----------------------------------------------------------------------------- | |
| 1158 // MessageBuilderContext. | |
| 1159 // ----------------------------------------------------------------------------- | |
| 1160 | |
| 1161 DEFINE_CLASS(MessageBuilderContext, | |
| 1162 "Google::Protobuf::Internal::MessageBuilderContext"); | |
| 1163 | |
| 1164 void MessageBuilderContext_mark(void* _self) { | |
| 1165 MessageBuilderContext* self = _self; | |
| 1166 rb_gc_mark(self->descriptor); | |
| 1167 rb_gc_mark(self->builder); | |
| 1168 } | |
| 1169 | |
| 1170 void MessageBuilderContext_free(void* _self) { | |
| 1171 MessageBuilderContext* self = _self; | |
| 1172 xfree(self); | |
| 1173 } | |
| 1174 | |
| 1175 VALUE MessageBuilderContext_alloc(VALUE klass) { | |
| 1176 MessageBuilderContext* self = ALLOC(MessageBuilderContext); | |
| 1177 VALUE ret = TypedData_Wrap_Struct( | |
| 1178 klass, &_MessageBuilderContext_type, self); | |
| 1179 self->descriptor = Qnil; | |
| 1180 self->builder = Qnil; | |
| 1181 return ret; | |
| 1182 } | |
| 1183 | |
| 1184 void MessageBuilderContext_register(VALUE module) { | |
| 1185 VALUE klass = rb_define_class_under( | |
| 1186 module, "MessageBuilderContext", rb_cObject); | |
| 1187 rb_define_alloc_func(klass, MessageBuilderContext_alloc); | |
| 1188 rb_define_method(klass, "initialize", | |
| 1189 MessageBuilderContext_initialize, 2); | |
| 1190 rb_define_method(klass, "optional", MessageBuilderContext_optional, -1); | |
| 1191 rb_define_method(klass, "required", MessageBuilderContext_required, -1); | |
| 1192 rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1); | |
| 1193 rb_define_method(klass, "map", MessageBuilderContext_map, -1); | |
| 1194 rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1); | |
| 1195 cMessageBuilderContext = klass; | |
| 1196 rb_gc_register_address(&cMessageBuilderContext); | |
| 1197 } | |
| 1198 | |
| 1199 /* | |
| 1200 * call-seq: | |
| 1201 * MessageBuilderContext.new(desc, builder) => context | |
| 1202 * | |
| 1203 * Create a new message builder context around the given message descriptor and | |
| 1204 * builder context. This class is intended to serve as a DSL context to be used | |
| 1205 * with #instance_eval. | |
| 1206 */ | |
| 1207 VALUE MessageBuilderContext_initialize(VALUE _self, | |
| 1208 VALUE msgdef, | |
| 1209 VALUE builder) { | |
| 1210 DEFINE_SELF(MessageBuilderContext, self, _self); | |
| 1211 self->descriptor = msgdef; | |
| 1212 self->builder = builder; | |
| 1213 return Qnil; | |
| 1214 } | |
| 1215 | |
| 1216 static VALUE msgdef_add_field(VALUE msgdef, | |
| 1217 const char* label, VALUE name, | |
| 1218 VALUE type, VALUE number, | |
| 1219 VALUE type_class) { | |
| 1220 VALUE fielddef = rb_class_new_instance(0, NULL, cFieldDescriptor); | |
| 1221 VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name))); | |
| 1222 | |
| 1223 rb_funcall(fielddef, rb_intern("label="), 1, ID2SYM(rb_intern(label))); | |
| 1224 rb_funcall(fielddef, rb_intern("name="), 1, name_str); | |
| 1225 rb_funcall(fielddef, rb_intern("type="), 1, type); | |
| 1226 rb_funcall(fielddef, rb_intern("number="), 1, number); | |
| 1227 | |
| 1228 if (type_class != Qnil) { | |
| 1229 if (TYPE(type_class) != T_STRING) { | |
| 1230 rb_raise(rb_eArgError, "Expected string for type class"); | |
| 1231 } | |
| 1232 // Make it an absolute type name by prepending a dot. | |
| 1233 type_class = rb_str_append(rb_str_new2("."), type_class); | |
| 1234 rb_funcall(fielddef, rb_intern("submsg_name="), 1, type_class); | |
| 1235 } | |
| 1236 | |
| 1237 rb_funcall(msgdef, rb_intern("add_field"), 1, fielddef); | |
| 1238 return fielddef; | |
| 1239 } | |
| 1240 | |
| 1241 /* | |
| 1242 * call-seq: | |
| 1243 * MessageBuilderContext.optional(name, type, number, type_class = nil) | |
| 1244 * | |
| 1245 * Defines a new optional field on this message type with the given type, tag | |
| 1246 * number, and type class (for message and enum fields). The type must be a Ruby | |
| 1247 * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a | |
| 1248 * string, if present (as accepted by FieldDescriptor#submsg_name=). | |
| 1249 */ | |
| 1250 VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { | |
| 1251 DEFINE_SELF(MessageBuilderContext, self, _self); | |
| 1252 | |
| 1253 if (argc < 3) { | |
| 1254 rb_raise(rb_eArgError, "Expected at least 3 arguments."); | |
| 1255 } | |
| 1256 VALUE name = argv[0]; | |
| 1257 VALUE type = argv[1]; | |
| 1258 VALUE number = argv[2]; | |
| 1259 VALUE type_class = (argc > 3) ? argv[3] : Qnil; | |
| 1260 | |
| 1261 return msgdef_add_field(self->descriptor, "optional", | |
| 1262 name, type, number, type_class); | |
| 1263 } | |
| 1264 | |
| 1265 /* | |
| 1266 * call-seq: | |
| 1267 * MessageBuilderContext.required(name, type, number, type_class = nil) | |
| 1268 * | |
| 1269 * Defines a new required field on this message type with the given type, tag | |
| 1270 * number, and type class (for message and enum fields). The type must be a Ruby | |
| 1271 * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a | |
| 1272 * string, if present (as accepted by FieldDescriptor#submsg_name=). | |
| 1273 * | |
| 1274 * Proto3 does not have required fields, but this method exists for | |
| 1275 * completeness. Any attempt to add a message type with required fields to a | |
| 1276 * pool will currently result in an error. | |
| 1277 */ | |
| 1278 VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) { | |
| 1279 DEFINE_SELF(MessageBuilderContext, self, _self); | |
| 1280 | |
| 1281 if (argc < 3) { | |
| 1282 rb_raise(rb_eArgError, "Expected at least 3 arguments."); | |
| 1283 } | |
| 1284 VALUE name = argv[0]; | |
| 1285 VALUE type = argv[1]; | |
| 1286 VALUE number = argv[2]; | |
| 1287 VALUE type_class = (argc > 3) ? argv[3] : Qnil; | |
| 1288 | |
| 1289 return msgdef_add_field(self->descriptor, "required", | |
| 1290 name, type, number, type_class); | |
| 1291 } | |
| 1292 | |
| 1293 /* | |
| 1294 * call-seq: | |
| 1295 * MessageBuilderContext.repeated(name, type, number, type_class = nil) | |
| 1296 * | |
| 1297 * Defines a new repeated field on this message type with the given type, tag | |
| 1298 * number, and type class (for message and enum fields). The type must be a Ruby | |
| 1299 * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a | |
| 1300 * string, if present (as accepted by FieldDescriptor#submsg_name=). | |
| 1301 */ | |
| 1302 VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) { | |
| 1303 DEFINE_SELF(MessageBuilderContext, self, _self); | |
| 1304 | |
| 1305 if (argc < 3) { | |
| 1306 rb_raise(rb_eArgError, "Expected at least 3 arguments."); | |
| 1307 } | |
| 1308 VALUE name = argv[0]; | |
| 1309 VALUE type = argv[1]; | |
| 1310 VALUE number = argv[2]; | |
| 1311 VALUE type_class = (argc > 3) ? argv[3] : Qnil; | |
| 1312 | |
| 1313 return msgdef_add_field(self->descriptor, "repeated", | |
| 1314 name, type, number, type_class); | |
| 1315 } | |
| 1316 | |
| 1317 /* | |
| 1318 * call-seq: | |
| 1319 * MessageBuilderContext.map(name, key_type, value_type, number, | |
| 1320 * value_type_class = nil) | |
| 1321 * | |
| 1322 * Defines a new map field on this message type with the given key and value | |
| 1323 * types, tag number, and type class (for message and enum value types). The key | |
| 1324 * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type | |
| 1325 * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the | |
| 1326 * type_class must be a string, if present (as accepted by | |
| 1327 * FieldDescriptor#submsg_name=). | |
| 1328 */ | |
| 1329 VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) { | |
| 1330 DEFINE_SELF(MessageBuilderContext, self, _self); | |
| 1331 | |
| 1332 if (argc < 4) { | |
| 1333 rb_raise(rb_eArgError, "Expected at least 4 arguments."); | |
| 1334 } | |
| 1335 VALUE name = argv[0]; | |
| 1336 VALUE key_type = argv[1]; | |
| 1337 VALUE value_type = argv[2]; | |
| 1338 VALUE number = argv[3]; | |
| 1339 VALUE type_class = (argc > 4) ? argv[4] : Qnil; | |
| 1340 | |
| 1341 // Validate the key type. We can't accept enums, messages, or floats/doubles | |
| 1342 // as map keys. (We exclude these explicitly, and the field-descriptor setter | |
| 1343 // below then ensures that the type is one of the remaining valid options.) | |
| 1344 if (SYM2ID(key_type) == rb_intern("float") || | |
| 1345 SYM2ID(key_type) == rb_intern("double") || | |
| 1346 SYM2ID(key_type) == rb_intern("enum") || | |
| 1347 SYM2ID(key_type) == rb_intern("message")) { | |
| 1348 rb_raise(rb_eArgError, | |
| 1349 "Cannot add a map field with a float, double, enum, or message " | |
| 1350 "type."); | |
| 1351 } | |
| 1352 | |
| 1353 // Create a new message descriptor for the map entry message, and create a | |
| 1354 // repeated submessage field here with that type. | |
| 1355 VALUE mapentry_desc = rb_class_new_instance(0, NULL, cDescriptor); | |
| 1356 VALUE mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0); | |
| 1357 mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_"); | |
| 1358 mapentry_desc_name = rb_str_cat2(mapentry_desc_name, | |
| 1359 rb_id2name(SYM2ID(name))); | |
| 1360 Descriptor_name_set(mapentry_desc, mapentry_desc_name); | |
| 1361 | |
| 1362 // The 'mapentry' attribute has no Ruby setter because we do not want the user | |
| 1363 // attempting to DIY the setup below; we want to ensure that the fields are | |
| 1364 // correct. So we reach into the msgdef here to set the bit manually. | |
| 1365 Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc); | |
| 1366 upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true); | |
| 1367 | |
| 1368 // optional <type> key = 1; | |
| 1369 VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor); | |
| 1370 FieldDescriptor_name_set(key_field, rb_str_new2("key")); | |
| 1371 FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional"))); | |
| 1372 FieldDescriptor_number_set(key_field, INT2NUM(1)); | |
| 1373 FieldDescriptor_type_set(key_field, key_type); | |
| 1374 Descriptor_add_field(mapentry_desc, key_field); | |
| 1375 | |
| 1376 // optional <type> value = 2; | |
| 1377 VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor); | |
| 1378 FieldDescriptor_name_set(value_field, rb_str_new2("value")); | |
| 1379 FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional"))); | |
| 1380 FieldDescriptor_number_set(value_field, INT2NUM(2)); | |
| 1381 FieldDescriptor_type_set(value_field, value_type); | |
| 1382 if (type_class != Qnil) { | |
| 1383 VALUE submsg_name = rb_str_new2("."); // prepend '.' to make name absolute. | |
| 1384 submsg_name = rb_str_append(submsg_name, type_class); | |
| 1385 FieldDescriptor_submsg_name_set(value_field, submsg_name); | |
| 1386 } | |
| 1387 Descriptor_add_field(mapentry_desc, value_field); | |
| 1388 | |
| 1389 // Add the map-entry message type to the current builder, and use the type to | |
| 1390 // create the map field itself. | |
| 1391 Builder* builder_self = ruby_to_Builder(self->builder); | |
| 1392 rb_ary_push(builder_self->pending_list, mapentry_desc); | |
| 1393 | |
| 1394 VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor); | |
| 1395 VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name))); | |
| 1396 FieldDescriptor_name_set(map_field, name_str); | |
| 1397 FieldDescriptor_number_set(map_field, number); | |
| 1398 FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated"))); | |
| 1399 FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message"))); | |
| 1400 VALUE submsg_name = rb_str_new2("."); // prepend '.' to make name absolute. | |
| 1401 submsg_name = rb_str_append(submsg_name, mapentry_desc_name); | |
| 1402 FieldDescriptor_submsg_name_set(map_field, submsg_name); | |
| 1403 Descriptor_add_field(self->descriptor, map_field); | |
| 1404 | |
| 1405 return Qnil; | |
| 1406 } | |
| 1407 | |
| 1408 /* | |
| 1409 * call-seq: | |
| 1410 * MessageBuilderContext.oneof(name, &block) => nil | |
| 1411 * | |
| 1412 * Creates a new OneofDescriptor with the given name, creates a | |
| 1413 * OneofBuilderContext attached to that OneofDescriptor, evaluates the given | |
| 1414 * block in the context of that OneofBuilderContext with #instance_eval, and | |
| 1415 * then adds the oneof to the message. | |
| 1416 * | |
| 1417 * This is the recommended, idiomatic way to build oneof definitions. | |
| 1418 */ | |
| 1419 VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) { | |
| 1420 DEFINE_SELF(MessageBuilderContext, self, _self); | |
| 1421 VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor); | |
| 1422 VALUE args[2] = { oneofdef, self->builder }; | |
| 1423 VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext); | |
| 1424 VALUE block = rb_block_proc(); | |
| 1425 VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name))); | |
| 1426 rb_funcall(oneofdef, rb_intern("name="), 1, name_str); | |
| 1427 rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); | |
| 1428 Descriptor_add_oneof(self->descriptor, oneofdef); | |
| 1429 | |
| 1430 return Qnil; | |
| 1431 } | |
| 1432 | |
| 1433 // ----------------------------------------------------------------------------- | |
| 1434 // OneofBuilderContext. | |
| 1435 // ----------------------------------------------------------------------------- | |
| 1436 | |
| 1437 DEFINE_CLASS(OneofBuilderContext, | |
| 1438 "Google::Protobuf::Internal::OneofBuilderContext"); | |
| 1439 | |
| 1440 void OneofBuilderContext_mark(void* _self) { | |
| 1441 OneofBuilderContext* self = _self; | |
| 1442 rb_gc_mark(self->descriptor); | |
| 1443 rb_gc_mark(self->builder); | |
| 1444 } | |
| 1445 | |
| 1446 void OneofBuilderContext_free(void* _self) { | |
| 1447 OneofBuilderContext* self = _self; | |
| 1448 xfree(self); | |
| 1449 } | |
| 1450 | |
| 1451 VALUE OneofBuilderContext_alloc(VALUE klass) { | |
| 1452 OneofBuilderContext* self = ALLOC(OneofBuilderContext); | |
| 1453 VALUE ret = TypedData_Wrap_Struct( | |
| 1454 klass, &_OneofBuilderContext_type, self); | |
| 1455 self->descriptor = Qnil; | |
| 1456 self->builder = Qnil; | |
| 1457 return ret; | |
| 1458 } | |
| 1459 | |
| 1460 void OneofBuilderContext_register(VALUE module) { | |
| 1461 VALUE klass = rb_define_class_under( | |
| 1462 module, "OneofBuilderContext", rb_cObject); | |
| 1463 rb_define_alloc_func(klass, OneofBuilderContext_alloc); | |
| 1464 rb_define_method(klass, "initialize", | |
| 1465 OneofBuilderContext_initialize, 2); | |
| 1466 rb_define_method(klass, "optional", OneofBuilderContext_optional, -1); | |
| 1467 cOneofBuilderContext = klass; | |
| 1468 rb_gc_register_address(&cOneofBuilderContext); | |
| 1469 } | |
| 1470 | |
| 1471 /* | |
| 1472 * call-seq: | |
| 1473 * OneofBuilderContext.new(desc, builder) => context | |
| 1474 * | |
| 1475 * Create a new oneof builder context around the given oneof descriptor and | |
| 1476 * builder context. This class is intended to serve as a DSL context to be used | |
| 1477 * with #instance_eval. | |
| 1478 */ | |
| 1479 VALUE OneofBuilderContext_initialize(VALUE _self, | |
| 1480 VALUE oneofdef, | |
| 1481 VALUE builder) { | |
| 1482 DEFINE_SELF(OneofBuilderContext, self, _self); | |
| 1483 self->descriptor = oneofdef; | |
| 1484 self->builder = builder; | |
| 1485 return Qnil; | |
| 1486 } | |
| 1487 | |
| 1488 /* | |
| 1489 * call-seq: | |
| 1490 * OneofBuilderContext.optional(name, type, number, type_class = nil) | |
| 1491 * | |
| 1492 * Defines a new optional field in this oneof with the given type, tag number, | |
| 1493 * and type class (for message and enum fields). The type must be a Ruby symbol | |
| 1494 * (as accepted by FieldDescriptor#type=) and the type_class must be a string, | |
| 1495 * if present (as accepted by FieldDescriptor#submsg_name=). | |
| 1496 */ | |
| 1497 VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { | |
| 1498 DEFINE_SELF(OneofBuilderContext, self, _self); | |
| 1499 | |
| 1500 if (argc < 3) { | |
| 1501 rb_raise(rb_eArgError, "Expected at least 3 arguments."); | |
| 1502 } | |
| 1503 VALUE name = argv[0]; | |
| 1504 VALUE type = argv[1]; | |
| 1505 VALUE number = argv[2]; | |
| 1506 VALUE type_class = (argc > 3) ? argv[3] : Qnil; | |
| 1507 | |
| 1508 return msgdef_add_field(self->descriptor, "optional", | |
| 1509 name, type, number, type_class); | |
| 1510 } | |
| 1511 | |
| 1512 // ----------------------------------------------------------------------------- | |
| 1513 // EnumBuilderContext. | |
| 1514 // ----------------------------------------------------------------------------- | |
| 1515 | |
| 1516 DEFINE_CLASS(EnumBuilderContext, | |
| 1517 "Google::Protobuf::Internal::EnumBuilderContext"); | |
| 1518 | |
| 1519 void EnumBuilderContext_mark(void* _self) { | |
| 1520 EnumBuilderContext* self = _self; | |
| 1521 rb_gc_mark(self->enumdesc); | |
| 1522 } | |
| 1523 | |
| 1524 void EnumBuilderContext_free(void* _self) { | |
| 1525 EnumBuilderContext* self = _self; | |
| 1526 xfree(self); | |
| 1527 } | |
| 1528 | |
| 1529 VALUE EnumBuilderContext_alloc(VALUE klass) { | |
| 1530 EnumBuilderContext* self = ALLOC(EnumBuilderContext); | |
| 1531 VALUE ret = TypedData_Wrap_Struct( | |
| 1532 klass, &_EnumBuilderContext_type, self); | |
| 1533 self->enumdesc = Qnil; | |
| 1534 return ret; | |
| 1535 } | |
| 1536 | |
| 1537 void EnumBuilderContext_register(VALUE module) { | |
| 1538 VALUE klass = rb_define_class_under( | |
| 1539 module, "EnumBuilderContext", rb_cObject); | |
| 1540 rb_define_alloc_func(klass, EnumBuilderContext_alloc); | |
| 1541 rb_define_method(klass, "initialize", | |
| 1542 EnumBuilderContext_initialize, 1); | |
| 1543 rb_define_method(klass, "value", EnumBuilderContext_value, 2); | |
| 1544 cEnumBuilderContext = klass; | |
| 1545 rb_gc_register_address(&cEnumBuilderContext); | |
| 1546 } | |
| 1547 | |
| 1548 /* | |
| 1549 * call-seq: | |
| 1550 * EnumBuilderContext.new(enumdesc) => context | |
| 1551 * | |
| 1552 * Create a new builder context around the given enum descriptor. This class is | |
| 1553 * intended to serve as a DSL context to be used with #instance_eval. | |
| 1554 */ | |
| 1555 VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) { | |
| 1556 DEFINE_SELF(EnumBuilderContext, self, _self); | |
| 1557 self->enumdesc = enumdef; | |
| 1558 return Qnil; | |
| 1559 } | |
| 1560 | |
| 1561 static VALUE enumdef_add_value(VALUE enumdef, | |
| 1562 VALUE name, VALUE number) { | |
| 1563 rb_funcall(enumdef, rb_intern("add_value"), 2, name, number); | |
| 1564 return Qnil; | |
| 1565 } | |
| 1566 | |
| 1567 /* | |
| 1568 * call-seq: | |
| 1569 * EnumBuilder.add_value(name, number) | |
| 1570 * | |
| 1571 * Adds the given name => number mapping to the enum type. Name must be a Ruby | |
| 1572 * symbol. | |
| 1573 */ | |
| 1574 VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) { | |
| 1575 DEFINE_SELF(EnumBuilderContext, self, _self); | |
| 1576 return enumdef_add_value(self->enumdesc, name, number); | |
| 1577 } | |
| 1578 | |
| 1579 // ----------------------------------------------------------------------------- | |
| 1580 // Builder. | |
| 1581 // ----------------------------------------------------------------------------- | |
| 1582 | |
| 1583 DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder"); | |
| 1584 | |
| 1585 void Builder_mark(void* _self) { | |
| 1586 Builder* self = _self; | |
| 1587 rb_gc_mark(self->pending_list); | |
| 1588 } | |
| 1589 | |
| 1590 void Builder_free(void* _self) { | |
| 1591 Builder* self = _self; | |
| 1592 xfree(self->defs); | |
| 1593 xfree(self); | |
| 1594 } | |
| 1595 | |
| 1596 /* | |
| 1597 * call-seq: | |
| 1598 * Builder.new => builder | |
| 1599 * | |
| 1600 * Creates a new Builder. A Builder can accumulate a set of new message and enum | |
| 1601 * descriptors and atomically register them into a pool in a way that allows for | |
| 1602 * (co)recursive type references. | |
| 1603 */ | |
| 1604 VALUE Builder_alloc(VALUE klass) { | |
| 1605 Builder* self = ALLOC(Builder); | |
| 1606 VALUE ret = TypedData_Wrap_Struct( | |
| 1607 klass, &_Builder_type, self); | |
| 1608 self->pending_list = rb_ary_new(); | |
| 1609 self->defs = NULL; | |
| 1610 return ret; | |
| 1611 } | |
| 1612 | |
| 1613 void Builder_register(VALUE module) { | |
| 1614 VALUE klass = rb_define_class_under(module, "Builder", rb_cObject); | |
| 1615 rb_define_alloc_func(klass, Builder_alloc); | |
| 1616 rb_define_method(klass, "add_message", Builder_add_message, 1); | |
| 1617 rb_define_method(klass, "add_enum", Builder_add_enum, 1); | |
| 1618 rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1); | |
| 1619 cBuilder = klass; | |
| 1620 rb_gc_register_address(&cBuilder); | |
| 1621 } | |
| 1622 | |
| 1623 /* | |
| 1624 * call-seq: | |
| 1625 * Builder.add_message(name, &block) | |
| 1626 * | |
| 1627 * Creates a new, empty descriptor with the given name, and invokes the block in | |
| 1628 * the context of a MessageBuilderContext on that descriptor. The block can then | |
| 1629 * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated | |
| 1630 * methods to define the message fields. | |
| 1631 * | |
| 1632 * This is the recommended, idiomatic way to build message definitions. | |
| 1633 */ | |
| 1634 VALUE Builder_add_message(VALUE _self, VALUE name) { | |
| 1635 DEFINE_SELF(Builder, self, _self); | |
| 1636 VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor); | |
| 1637 VALUE args[2] = { msgdef, _self }; | |
| 1638 VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext); | |
| 1639 VALUE block = rb_block_proc(); | |
| 1640 rb_funcall(msgdef, rb_intern("name="), 1, name); | |
| 1641 rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); | |
| 1642 rb_ary_push(self->pending_list, msgdef); | |
| 1643 return Qnil; | |
| 1644 } | |
| 1645 | |
| 1646 /* | |
| 1647 * call-seq: | |
| 1648 * Builder.add_enum(name, &block) | |
| 1649 * | |
| 1650 * Creates a new, empty enum descriptor with the given name, and invokes the | |
| 1651 * block in the context of an EnumBuilderContext on that descriptor. The block | |
| 1652 * can then call EnumBuilderContext#add_value to define the enum values. | |
| 1653 * | |
| 1654 * This is the recommended, idiomatic way to build enum definitions. | |
| 1655 */ | |
| 1656 VALUE Builder_add_enum(VALUE _self, VALUE name) { | |
| 1657 DEFINE_SELF(Builder, self, _self); | |
| 1658 VALUE enumdef = rb_class_new_instance(0, NULL, cEnumDescriptor); | |
| 1659 VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext); | |
| 1660 VALUE block = rb_block_proc(); | |
| 1661 rb_funcall(enumdef, rb_intern("name="), 1, name); | |
| 1662 rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); | |
| 1663 rb_ary_push(self->pending_list, enumdef); | |
| 1664 return Qnil; | |
| 1665 } | |
| 1666 | |
| 1667 static void validate_msgdef(const upb_msgdef* msgdef) { | |
| 1668 // Verify that no required fields exist. proto3 does not support these. | |
| 1669 upb_msg_field_iter it; | |
| 1670 for (upb_msg_field_begin(&it, msgdef); | |
| 1671 !upb_msg_field_done(&it); | |
| 1672 upb_msg_field_next(&it)) { | |
| 1673 const upb_fielddef* field = upb_msg_iter_field(&it); | |
| 1674 if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) { | |
| 1675 rb_raise(rb_eTypeError, "Required fields are unsupported in proto3."); | |
| 1676 } | |
| 1677 } | |
| 1678 } | |
| 1679 | |
| 1680 static void validate_enumdef(const upb_enumdef* enumdef) { | |
| 1681 // Verify that an entry exists with integer value 0. (This is the default | |
| 1682 // value.) | |
| 1683 const char* lookup = upb_enumdef_iton(enumdef, 0); | |
| 1684 if (lookup == NULL) { | |
| 1685 rb_raise(rb_eTypeError, | |
| 1686 "Enum definition does not contain a value for '0'."); | |
| 1687 } | |
| 1688 } | |
| 1689 | |
| 1690 /* | |
| 1691 * call-seq: | |
| 1692 * Builder.finalize_to_pool(pool) | |
| 1693 * | |
| 1694 * Adds all accumulated message and enum descriptors created in this builder | |
| 1695 * context to the given pool. The operation occurs atomically, and all | |
| 1696 * descriptors can refer to each other (including in cycles). This is the only | |
| 1697 * way to build (co)recursive message definitions. | |
| 1698 * | |
| 1699 * This method is usually called automatically by DescriptorPool#build after it | |
| 1700 * invokes the given user block in the context of the builder. The user should | |
| 1701 * not normally need to call this manually because a Builder is not normally | |
| 1702 * created manually. | |
| 1703 */ | |
| 1704 VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) { | |
| 1705 DEFINE_SELF(Builder, self, _self); | |
| 1706 | |
| 1707 DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb); | |
| 1708 | |
| 1709 REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list)); | |
| 1710 | |
| 1711 for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) { | |
| 1712 VALUE def_rb = rb_ary_entry(self->pending_list, i); | |
| 1713 if (CLASS_OF(def_rb) == cDescriptor) { | |
| 1714 self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef; | |
| 1715 validate_msgdef((const upb_msgdef*)self->defs[i]); | |
| 1716 } else if (CLASS_OF(def_rb) == cEnumDescriptor) { | |
| 1717 self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef; | |
| 1718 validate_enumdef((const upb_enumdef*)self->defs[i]); | |
| 1719 } | |
| 1720 } | |
| 1721 | |
| 1722 CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs, | |
| 1723 RARRAY_LEN(self->pending_list), NULL, &status), | |
| 1724 "Unable to add defs to DescriptorPool"); | |
| 1725 | |
| 1726 for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) { | |
| 1727 VALUE def_rb = rb_ary_entry(self->pending_list, i); | |
| 1728 add_def_obj(self->defs[i], def_rb); | |
| 1729 } | |
| 1730 | |
| 1731 self->pending_list = rb_ary_new(); | |
| 1732 return Qnil; | |
| 1733 } | |
| OLD | NEW |