defs.c 56 KB

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