defs.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. #define CONVERT(upb, ruby) \
  498. if (SYM2ID(type) == rb_intern( # ruby )) { \
  499. return UPB_TYPE_ ## upb; \
  500. }
  501. CONVERT(FLOAT, float);
  502. CONVERT(DOUBLE, double);
  503. CONVERT(BOOL, bool);
  504. CONVERT(STRING, string);
  505. CONVERT(BYTES, bytes);
  506. CONVERT(MESSAGE, message);
  507. CONVERT(ENUM, enum);
  508. CONVERT(INT32, int32);
  509. CONVERT(INT64, int64);
  510. CONVERT(UINT32, uint32);
  511. CONVERT(UINT64, uint64);
  512. #undef CONVERT
  513. rb_raise(rb_eArgError, "Unknown field type.");
  514. return 0;
  515. }
  516. VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
  517. switch (type) {
  518. #define CONVERT(upb, ruby) \
  519. case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
  520. CONVERT(FLOAT, float);
  521. CONVERT(DOUBLE, double);
  522. CONVERT(BOOL, bool);
  523. CONVERT(STRING, string);
  524. CONVERT(BYTES, bytes);
  525. CONVERT(MESSAGE, message);
  526. CONVERT(ENUM, enum);
  527. CONVERT(INT32, int32);
  528. CONVERT(INT64, int64);
  529. CONVERT(UINT32, uint32);
  530. CONVERT(UINT64, uint64);
  531. #undef CONVERT
  532. }
  533. return Qnil;
  534. }
  535. /*
  536. * call-seq:
  537. * FieldDescriptor.type => type
  538. *
  539. * Returns this field's type, as a Ruby symbol, or nil if not yet set.
  540. *
  541. * Valid field types are:
  542. * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
  543. * :bytes, :message.
  544. */
  545. VALUE FieldDescriptor_type(VALUE _self) {
  546. DEFINE_SELF(FieldDescriptor, self, _self);
  547. if (!upb_fielddef_typeisset(self->fielddef)) {
  548. return Qnil;
  549. }
  550. return fieldtype_to_ruby(upb_fielddef_type(self->fielddef));
  551. }
  552. /*
  553. * call-seq:
  554. * FieldDescriptor.type = type
  555. *
  556. * Sets this field's type. Cannot be called if field is part of a message type
  557. * already in a pool.
  558. */
  559. VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
  560. DEFINE_SELF(FieldDescriptor, self, _self);
  561. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  562. upb_fielddef_settype(mut_def, ruby_to_fieldtype(type));
  563. return Qnil;
  564. }
  565. /*
  566. * call-seq:
  567. * FieldDescriptor.label => label
  568. *
  569. * Returns this field's label (i.e., plurality), as a Ruby symbol.
  570. *
  571. * Valid field labels are:
  572. * :optional, :repeated
  573. */
  574. VALUE FieldDescriptor_label(VALUE _self) {
  575. DEFINE_SELF(FieldDescriptor, self, _self);
  576. switch (upb_fielddef_label(self->fielddef)) {
  577. #define CONVERT(upb, ruby) \
  578. case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
  579. CONVERT(OPTIONAL, optional);
  580. CONVERT(REQUIRED, required);
  581. CONVERT(REPEATED, repeated);
  582. #undef CONVERT
  583. }
  584. return Qnil;
  585. }
  586. /*
  587. * call-seq:
  588. * FieldDescriptor.label = label
  589. *
  590. * Sets the label on this field. Cannot be called if field is part of a message
  591. * type already in a pool.
  592. */
  593. VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
  594. DEFINE_SELF(FieldDescriptor, self, _self);
  595. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  596. if (TYPE(label) != T_SYMBOL) {
  597. rb_raise(rb_eArgError, "Expected symbol for field label.");
  598. }
  599. upb_label_t upb_label = -1;
  600. bool converted = false;
  601. #define CONVERT(upb, ruby) \
  602. if (SYM2ID(label) == rb_intern( # ruby )) { \
  603. upb_label = UPB_LABEL_ ## upb; \
  604. converted = true; \
  605. }
  606. CONVERT(OPTIONAL, optional);
  607. CONVERT(REQUIRED, required);
  608. CONVERT(REPEATED, repeated);
  609. #undef CONVERT
  610. if (!converted) {
  611. rb_raise(rb_eArgError, "Unknown field label.");
  612. }
  613. upb_fielddef_setlabel(mut_def, upb_label);
  614. return Qnil;
  615. }
  616. /*
  617. * call-seq:
  618. * FieldDescriptor.number => number
  619. *
  620. * Returns the tag number for this field.
  621. */
  622. VALUE FieldDescriptor_number(VALUE _self) {
  623. DEFINE_SELF(FieldDescriptor, self, _self);
  624. return INT2NUM(upb_fielddef_number(self->fielddef));
  625. }
  626. /*
  627. * call-seq:
  628. * FieldDescriptor.number = number
  629. *
  630. * Sets the tag number for this field. Cannot be called if field is part of a
  631. * message type already in a pool.
  632. */
  633. VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
  634. DEFINE_SELF(FieldDescriptor, self, _self);
  635. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  636. CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
  637. "Error setting field number");
  638. return Qnil;
  639. }
  640. /*
  641. * call-seq:
  642. * FieldDescriptor.submsg_name => submsg_name
  643. *
  644. * Returns the name of the message or enum type corresponding to this field, if
  645. * it is a message or enum field (respectively), or nil otherwise. This type
  646. * name will be resolved within the context of the pool to which the containing
  647. * message type is added.
  648. */
  649. VALUE FieldDescriptor_submsg_name(VALUE _self) {
  650. DEFINE_SELF(FieldDescriptor, self, _self);
  651. if (!upb_fielddef_hassubdef(self->fielddef)) {
  652. return Qnil;
  653. }
  654. return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
  655. }
  656. /*
  657. * call-seq:
  658. * FieldDescriptor.submsg_name = submsg_name
  659. *
  660. * Sets the name of the message or enum type corresponding to this field, if it
  661. * is a message or enum field (respectively). This type name will be resolved
  662. * within the context of the pool to which the containing message type is added.
  663. * Cannot be called on field that are not of message or enum type, or on fields
  664. * that are part of a message type already added to a pool.
  665. */
  666. VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
  667. DEFINE_SELF(FieldDescriptor, self, _self);
  668. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  669. if (!upb_fielddef_hassubdef(self->fielddef)) {
  670. rb_raise(rb_eTypeError, "FieldDescriptor does not have subdef.");
  671. }
  672. const char* str = get_str(value);
  673. CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
  674. "Error setting submessage name");
  675. return Qnil;
  676. }
  677. /*
  678. * call-seq:
  679. * FieldDescriptor.subtype => message_or_enum_descriptor
  680. *
  681. * Returns the message or enum descriptor corresponding to this field's type if
  682. * it is a message or enum field, respectively, or nil otherwise. Cannot be
  683. * called *until* the containing message type is added to a pool (and thus
  684. * resolved).
  685. */
  686. VALUE FieldDescriptor_subtype(VALUE _self) {
  687. DEFINE_SELF(FieldDescriptor, self, _self);
  688. if (!upb_fielddef_hassubdef(self->fielddef)) {
  689. return Qnil;
  690. }
  691. const upb_def* def = upb_fielddef_subdef(self->fielddef);
  692. if (def == NULL) {
  693. return Qnil;
  694. }
  695. return get_def_obj(def);
  696. }
  697. /*
  698. * call-seq:
  699. * FieldDescriptor.get(message) => value
  700. *
  701. * Returns the value set for this field on the given message. Raises an
  702. * exception if message is of the wrong type.
  703. */
  704. VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
  705. DEFINE_SELF(FieldDescriptor, self, _self);
  706. MessageHeader* msg;
  707. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  708. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  709. rb_raise(rb_eTypeError, "get method called on wrong message type");
  710. }
  711. return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
  712. }
  713. /*
  714. * call-seq:
  715. * FieldDescriptor.set(message, value)
  716. *
  717. * Sets the value corresponding to this field to the given value on the given
  718. * message. Raises an exception if message is of the wrong type. Performs the
  719. * ordinary type-checks for field setting.
  720. */
  721. VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
  722. DEFINE_SELF(FieldDescriptor, self, _self);
  723. MessageHeader* msg;
  724. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  725. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  726. rb_raise(rb_eTypeError, "set method called on wrong message type");
  727. }
  728. layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
  729. return Qnil;
  730. }
  731. // -----------------------------------------------------------------------------
  732. // OneofDescriptor.
  733. // -----------------------------------------------------------------------------
  734. DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
  735. void OneofDescriptor_mark(void* _self) {
  736. }
  737. void OneofDescriptor_free(void* _self) {
  738. OneofDescriptor* self = _self;
  739. upb_oneofdef_unref(self->oneofdef, &self->oneofdef);
  740. xfree(self);
  741. }
  742. /*
  743. * call-seq:
  744. * OneofDescriptor.new => oneof_descriptor
  745. *
  746. * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
  747. * to being added to a message descriptor which is subsequently added to a pool.
  748. */
  749. VALUE OneofDescriptor_alloc(VALUE klass) {
  750. OneofDescriptor* self = ALLOC(OneofDescriptor);
  751. VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
  752. self->oneofdef = upb_oneofdef_new(&self->oneofdef);
  753. return ret;
  754. }
  755. void OneofDescriptor_register(VALUE module) {
  756. VALUE klass = rb_define_class_under(
  757. module, "OneofDescriptor", rb_cObject);
  758. rb_define_alloc_func(klass, OneofDescriptor_alloc);
  759. rb_define_method(klass, "name", OneofDescriptor_name, 0);
  760. rb_define_method(klass, "name=", OneofDescriptor_name_set, 1);
  761. rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1);
  762. rb_define_method(klass, "each", OneofDescriptor_each, 0);
  763. rb_include_module(klass, rb_mEnumerable);
  764. cOneofDescriptor = klass;
  765. rb_gc_register_address(&cOneofDescriptor);
  766. }
  767. /*
  768. * call-seq:
  769. * OneofDescriptor.name => name
  770. *
  771. * Returns the name of this oneof.
  772. */
  773. VALUE OneofDescriptor_name(VALUE _self) {
  774. DEFINE_SELF(OneofDescriptor, self, _self);
  775. return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
  776. }
  777. /*
  778. * call-seq:
  779. * OneofDescriptor.name = name
  780. *
  781. * Sets a new name for this oneof. The oneof must not have been added to a
  782. * message descriptor yet.
  783. */
  784. VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) {
  785. DEFINE_SELF(OneofDescriptor, self, _self);
  786. upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
  787. const char* str = get_str(value);
  788. CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status),
  789. "Error setting oneof name");
  790. return Qnil;
  791. }
  792. /*
  793. * call-seq:
  794. * OneofDescriptor.add_field(field) => nil
  795. *
  796. * Adds a field to this oneof. The field may have been added to this oneof in
  797. * the past, or the message to which this oneof belongs (if any), but may not
  798. * have already been added to any other oneof or message. Otherwise, an
  799. * exception is raised.
  800. *
  801. * All fields added to the oneof via this method will be automatically added to
  802. * the message to which this oneof belongs, if it belongs to one currently, or
  803. * else will be added to any message to which the oneof is later added at the
  804. * time that it is added.
  805. */
  806. VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
  807. DEFINE_SELF(OneofDescriptor, self, _self);
  808. upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
  809. FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
  810. upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
  811. CHECK_UPB(
  812. upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status),
  813. "Adding field to OneofDescriptor failed");
  814. add_def_obj(def->fielddef, obj);
  815. return Qnil;
  816. }
  817. /*
  818. * call-seq:
  819. * OneofDescriptor.each(&block) => nil
  820. *
  821. * Iterates through fields in this oneof, yielding to the block on each one.
  822. */
  823. VALUE OneofDescriptor_each(VALUE _self, VALUE field) {
  824. DEFINE_SELF(OneofDescriptor, self, _self);
  825. upb_oneof_iter it;
  826. for (upb_oneof_begin(&it, self->oneofdef);
  827. !upb_oneof_done(&it);
  828. upb_oneof_next(&it)) {
  829. const upb_fielddef* f = upb_oneof_iter_field(&it);
  830. VALUE obj = get_def_obj(f);
  831. rb_yield(obj);
  832. }
  833. return Qnil;
  834. }
  835. // -----------------------------------------------------------------------------
  836. // EnumDescriptor.
  837. // -----------------------------------------------------------------------------
  838. DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
  839. void EnumDescriptor_mark(void* _self) {
  840. EnumDescriptor* self = _self;
  841. rb_gc_mark(self->module);
  842. }
  843. void EnumDescriptor_free(void* _self) {
  844. EnumDescriptor* self = _self;
  845. upb_enumdef_unref(self->enumdef, &self->enumdef);
  846. xfree(self);
  847. }
  848. /*
  849. * call-seq:
  850. * EnumDescriptor.new => enum_descriptor
  851. *
  852. * Creates a new, empty, enum descriptor. Must be added to a pool before the
  853. * enum type can be used. The enum type may only be modified prior to adding to
  854. * a pool.
  855. */
  856. VALUE EnumDescriptor_alloc(VALUE klass) {
  857. EnumDescriptor* self = ALLOC(EnumDescriptor);
  858. VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
  859. self->enumdef = upb_enumdef_new(&self->enumdef);
  860. self->module = Qnil;
  861. return ret;
  862. }
  863. void EnumDescriptor_register(VALUE module) {
  864. VALUE klass = rb_define_class_under(
  865. module, "EnumDescriptor", rb_cObject);
  866. rb_define_alloc_func(klass, EnumDescriptor_alloc);
  867. rb_define_method(klass, "name", EnumDescriptor_name, 0);
  868. rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
  869. rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
  870. rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
  871. rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
  872. rb_define_method(klass, "each", EnumDescriptor_each, 0);
  873. rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
  874. rb_include_module(klass, rb_mEnumerable);
  875. cEnumDescriptor = klass;
  876. rb_gc_register_address(&cEnumDescriptor);
  877. }
  878. /*
  879. * call-seq:
  880. * EnumDescriptor.name => name
  881. *
  882. * Returns the name of this enum type.
  883. */
  884. VALUE EnumDescriptor_name(VALUE _self) {
  885. DEFINE_SELF(EnumDescriptor, self, _self);
  886. return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
  887. }
  888. /*
  889. * call-seq:
  890. * EnumDescriptor.name = name
  891. *
  892. * Sets the name of this enum type. Cannot be called if the enum type has
  893. * already been added to a pool.
  894. */
  895. VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) {
  896. DEFINE_SELF(EnumDescriptor, self, _self);
  897. upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
  898. const char* name = get_str(str);
  899. CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
  900. "Error setting EnumDescriptor name");
  901. return Qnil;
  902. }
  903. /*
  904. * call-seq:
  905. * EnumDescriptor.add_value(key, value)
  906. *
  907. * Adds a new key => value mapping to this enum type. Key must be given as a
  908. * Ruby symbol. Cannot be called if the enum type has already been added to a
  909. * pool. Will raise an exception if the key or value is already in use.
  910. */
  911. VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
  912. DEFINE_SELF(EnumDescriptor, self, _self);
  913. upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
  914. const char* name_str = rb_id2name(SYM2ID(name));
  915. int32_t val = NUM2INT(number);
  916. CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
  917. "Error adding value to enum");
  918. return Qnil;
  919. }
  920. /*
  921. * call-seq:
  922. * EnumDescriptor.lookup_name(name) => value
  923. *
  924. * Returns the numeric value corresponding to the given key name (as a Ruby
  925. * symbol), or nil if none.
  926. */
  927. VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
  928. DEFINE_SELF(EnumDescriptor, self, _self);
  929. const char* name_str= rb_id2name(SYM2ID(name));
  930. int32_t val = 0;
  931. if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
  932. return INT2NUM(val);
  933. } else {
  934. return Qnil;
  935. }
  936. }
  937. /*
  938. * call-seq:
  939. * EnumDescriptor.lookup_value(name) => value
  940. *
  941. * Returns the key name (as a Ruby symbol) corresponding to the integer value,
  942. * or nil if none.
  943. */
  944. VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
  945. DEFINE_SELF(EnumDescriptor, self, _self);
  946. int32_t val = NUM2INT(number);
  947. const char* name = upb_enumdef_iton(self->enumdef, val);
  948. if (name != NULL) {
  949. return ID2SYM(rb_intern(name));
  950. } else {
  951. return Qnil;
  952. }
  953. }
  954. /*
  955. * call-seq:
  956. * EnumDescriptor.each(&block)
  957. *
  958. * Iterates over key => value mappings in this enum's definition, yielding to
  959. * the block with (key, value) arguments for each one.
  960. */
  961. VALUE EnumDescriptor_each(VALUE _self) {
  962. DEFINE_SELF(EnumDescriptor, self, _self);
  963. upb_enum_iter it;
  964. for (upb_enum_begin(&it, self->enumdef);
  965. !upb_enum_done(&it);
  966. upb_enum_next(&it)) {
  967. VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
  968. VALUE number = INT2NUM(upb_enum_iter_number(&it));
  969. rb_yield_values(2, key, number);
  970. }
  971. return Qnil;
  972. }
  973. /*
  974. * call-seq:
  975. * EnumDescriptor.enummodule => module
  976. *
  977. * Returns the Ruby module corresponding to this enum type. Cannot be called
  978. * until the enum descriptor has been added to a pool.
  979. */
  980. VALUE EnumDescriptor_enummodule(VALUE _self) {
  981. DEFINE_SELF(EnumDescriptor, self, _self);
  982. if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
  983. rb_raise(rb_eRuntimeError,
  984. "Cannot fetch enum module from an EnumDescriptor not yet "
  985. "in a pool.");
  986. }
  987. if (self->module == Qnil) {
  988. self->module = build_module_from_enumdesc(self);
  989. }
  990. return self->module;
  991. }
  992. // -----------------------------------------------------------------------------
  993. // MessageBuilderContext.
  994. // -----------------------------------------------------------------------------
  995. DEFINE_CLASS(MessageBuilderContext,
  996. "Google::Protobuf::Internal::MessageBuilderContext");
  997. void MessageBuilderContext_mark(void* _self) {
  998. MessageBuilderContext* self = _self;
  999. rb_gc_mark(self->descriptor);
  1000. rb_gc_mark(self->builder);
  1001. }
  1002. void MessageBuilderContext_free(void* _self) {
  1003. MessageBuilderContext* self = _self;
  1004. xfree(self);
  1005. }
  1006. VALUE MessageBuilderContext_alloc(VALUE klass) {
  1007. MessageBuilderContext* self = ALLOC(MessageBuilderContext);
  1008. VALUE ret = TypedData_Wrap_Struct(
  1009. klass, &_MessageBuilderContext_type, self);
  1010. self->descriptor = Qnil;
  1011. self->builder = Qnil;
  1012. return ret;
  1013. }
  1014. void MessageBuilderContext_register(VALUE module) {
  1015. VALUE klass = rb_define_class_under(
  1016. module, "MessageBuilderContext", rb_cObject);
  1017. rb_define_alloc_func(klass, MessageBuilderContext_alloc);
  1018. rb_define_method(klass, "initialize",
  1019. MessageBuilderContext_initialize, 2);
  1020. rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
  1021. rb_define_method(klass, "required", MessageBuilderContext_required, -1);
  1022. rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
  1023. rb_define_method(klass, "map", MessageBuilderContext_map, -1);
  1024. rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
  1025. cMessageBuilderContext = klass;
  1026. rb_gc_register_address(&cMessageBuilderContext);
  1027. }
  1028. /*
  1029. * call-seq:
  1030. * MessageBuilderContext.new(desc, builder) => context
  1031. *
  1032. * Create a new message builder context around the given message descriptor and
  1033. * builder context. This class is intended to serve as a DSL context to be used
  1034. * with #instance_eval.
  1035. */
  1036. VALUE MessageBuilderContext_initialize(VALUE _self,
  1037. VALUE msgdef,
  1038. VALUE builder) {
  1039. DEFINE_SELF(MessageBuilderContext, self, _self);
  1040. self->descriptor = msgdef;
  1041. self->builder = builder;
  1042. return Qnil;
  1043. }
  1044. static VALUE msgdef_add_field(VALUE msgdef,
  1045. const char* label, VALUE name,
  1046. VALUE type, VALUE number,
  1047. VALUE type_class) {
  1048. VALUE fielddef = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1049. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1050. rb_funcall(fielddef, rb_intern("label="), 1, ID2SYM(rb_intern(label)));
  1051. rb_funcall(fielddef, rb_intern("name="), 1, name_str);
  1052. rb_funcall(fielddef, rb_intern("type="), 1, type);
  1053. rb_funcall(fielddef, rb_intern("number="), 1, number);
  1054. if (type_class != Qnil) {
  1055. if (TYPE(type_class) != T_STRING) {
  1056. rb_raise(rb_eArgError, "Expected string for type class");
  1057. }
  1058. // Make it an absolute type name by prepending a dot.
  1059. type_class = rb_str_append(rb_str_new2("."), type_class);
  1060. rb_funcall(fielddef, rb_intern("submsg_name="), 1, type_class);
  1061. }
  1062. rb_funcall(msgdef, rb_intern("add_field"), 1, fielddef);
  1063. return fielddef;
  1064. }
  1065. /*
  1066. * call-seq:
  1067. * MessageBuilderContext.optional(name, type, number, type_class = nil)
  1068. *
  1069. * Defines a new optional field on this message type with the given type, tag
  1070. * number, and type class (for message and enum fields). The type must be a Ruby
  1071. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1072. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1073. */
  1074. VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1075. DEFINE_SELF(MessageBuilderContext, self, _self);
  1076. if (argc < 3) {
  1077. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1078. }
  1079. VALUE name = argv[0];
  1080. VALUE type = argv[1];
  1081. VALUE number = argv[2];
  1082. VALUE type_class = (argc > 3) ? argv[3] : Qnil;
  1083. return msgdef_add_field(self->descriptor, "optional",
  1084. name, type, number, type_class);
  1085. }
  1086. /*
  1087. * call-seq:
  1088. * MessageBuilderContext.required(name, type, number, type_class = nil)
  1089. *
  1090. * Defines a new required field on this message type with the given type, tag
  1091. * number, and type class (for message and enum fields). The type must be a Ruby
  1092. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1093. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1094. *
  1095. * Proto3 does not have required fields, but this method exists for
  1096. * completeness. Any attempt to add a message type with required fields to a
  1097. * pool will currently result in an error.
  1098. */
  1099. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
  1100. DEFINE_SELF(MessageBuilderContext, self, _self);
  1101. if (argc < 3) {
  1102. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1103. }
  1104. VALUE name = argv[0];
  1105. VALUE type = argv[1];
  1106. VALUE number = argv[2];
  1107. VALUE type_class = (argc > 3) ? argv[3] : Qnil;
  1108. return msgdef_add_field(self->descriptor, "required",
  1109. name, type, number, type_class);
  1110. }
  1111. /*
  1112. * call-seq:
  1113. * MessageBuilderContext.repeated(name, type, number, type_class = nil)
  1114. *
  1115. * Defines a new repeated field on this message type with the given type, tag
  1116. * number, and type class (for message and enum fields). The type must be a Ruby
  1117. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1118. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1119. */
  1120. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
  1121. DEFINE_SELF(MessageBuilderContext, self, _self);
  1122. if (argc < 3) {
  1123. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1124. }
  1125. VALUE name = argv[0];
  1126. VALUE type = argv[1];
  1127. VALUE number = argv[2];
  1128. VALUE type_class = (argc > 3) ? argv[3] : Qnil;
  1129. return msgdef_add_field(self->descriptor, "repeated",
  1130. name, type, number, type_class);
  1131. }
  1132. /*
  1133. * call-seq:
  1134. * MessageBuilderContext.map(name, key_type, value_type, number,
  1135. * value_type_class = nil)
  1136. *
  1137. * Defines a new map field on this message type with the given key and value
  1138. * types, tag number, and type class (for message and enum value types). The key
  1139. * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
  1140. * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
  1141. * type_class must be a string, if present (as accepted by
  1142. * FieldDescriptor#submsg_name=).
  1143. */
  1144. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
  1145. DEFINE_SELF(MessageBuilderContext, self, _self);
  1146. if (argc < 4) {
  1147. rb_raise(rb_eArgError, "Expected at least 4 arguments.");
  1148. }
  1149. VALUE name = argv[0];
  1150. VALUE key_type = argv[1];
  1151. VALUE value_type = argv[2];
  1152. VALUE number = argv[3];
  1153. VALUE type_class = (argc > 4) ? argv[4] : Qnil;
  1154. // Validate the key type. We can't accept enums, messages, or floats/doubles
  1155. // as map keys. (We exclude these explicitly, and the field-descriptor setter
  1156. // below then ensures that the type is one of the remaining valid options.)
  1157. if (SYM2ID(key_type) == rb_intern("float") ||
  1158. SYM2ID(key_type) == rb_intern("double") ||
  1159. SYM2ID(key_type) == rb_intern("enum") ||
  1160. SYM2ID(key_type) == rb_intern("message")) {
  1161. rb_raise(rb_eArgError,
  1162. "Cannot add a map field with a float, double, enum, or message "
  1163. "type.");
  1164. }
  1165. // Create a new message descriptor for the map entry message, and create a
  1166. // repeated submessage field here with that type.
  1167. VALUE mapentry_desc = rb_class_new_instance(0, NULL, cDescriptor);
  1168. VALUE mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
  1169. mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
  1170. mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
  1171. rb_id2name(SYM2ID(name)));
  1172. Descriptor_name_set(mapentry_desc, mapentry_desc_name);
  1173. // The 'mapentry' attribute has no Ruby setter because we do not want the user
  1174. // attempting to DIY the setup below; we want to ensure that the fields are
  1175. // correct. So we reach into the msgdef here to set the bit manually.
  1176. Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
  1177. upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
  1178. // optional <type> key = 1;
  1179. VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1180. FieldDescriptor_name_set(key_field, rb_str_new2("key"));
  1181. FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
  1182. FieldDescriptor_number_set(key_field, INT2NUM(1));
  1183. FieldDescriptor_type_set(key_field, key_type);
  1184. Descriptor_add_field(mapentry_desc, key_field);
  1185. // optional <type> value = 2;
  1186. VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1187. FieldDescriptor_name_set(value_field, rb_str_new2("value"));
  1188. FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
  1189. FieldDescriptor_number_set(value_field, INT2NUM(2));
  1190. FieldDescriptor_type_set(value_field, value_type);
  1191. if (type_class != Qnil) {
  1192. VALUE submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
  1193. submsg_name = rb_str_append(submsg_name, type_class);
  1194. FieldDescriptor_submsg_name_set(value_field, submsg_name);
  1195. }
  1196. Descriptor_add_field(mapentry_desc, value_field);
  1197. // Add the map-entry message type to the current builder, and use the type to
  1198. // create the map field itself.
  1199. Builder* builder_self = ruby_to_Builder(self->builder);
  1200. rb_ary_push(builder_self->pending_list, mapentry_desc);
  1201. VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1202. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1203. FieldDescriptor_name_set(map_field, name_str);
  1204. FieldDescriptor_number_set(map_field, number);
  1205. FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
  1206. FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
  1207. VALUE submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
  1208. submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
  1209. FieldDescriptor_submsg_name_set(map_field, submsg_name);
  1210. Descriptor_add_field(self->descriptor, map_field);
  1211. return Qnil;
  1212. }
  1213. /*
  1214. * call-seq:
  1215. * MessageBuilderContext.oneof(name, &block) => nil
  1216. *
  1217. * Creates a new OneofDescriptor with the given name, creates a
  1218. * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
  1219. * block in the context of that OneofBuilderContext with #instance_eval, and
  1220. * then adds the oneof to the message.
  1221. *
  1222. * This is the recommended, idiomatic way to build oneof definitions.
  1223. */
  1224. VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
  1225. DEFINE_SELF(MessageBuilderContext, self, _self);
  1226. VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
  1227. VALUE args[2] = { oneofdef, self->builder };
  1228. VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
  1229. VALUE block = rb_block_proc();
  1230. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1231. rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
  1232. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1233. Descriptor_add_oneof(self->descriptor, oneofdef);
  1234. return Qnil;
  1235. }
  1236. // -----------------------------------------------------------------------------
  1237. // OneofBuilderContext.
  1238. // -----------------------------------------------------------------------------
  1239. DEFINE_CLASS(OneofBuilderContext,
  1240. "Google::Protobuf::Internal::OneofBuilderContext");
  1241. void OneofBuilderContext_mark(void* _self) {
  1242. OneofBuilderContext* self = _self;
  1243. rb_gc_mark(self->descriptor);
  1244. rb_gc_mark(self->builder);
  1245. }
  1246. void OneofBuilderContext_free(void* _self) {
  1247. OneofBuilderContext* self = _self;
  1248. xfree(self);
  1249. }
  1250. VALUE OneofBuilderContext_alloc(VALUE klass) {
  1251. OneofBuilderContext* self = ALLOC(OneofBuilderContext);
  1252. VALUE ret = TypedData_Wrap_Struct(
  1253. klass, &_OneofBuilderContext_type, self);
  1254. self->descriptor = Qnil;
  1255. self->builder = Qnil;
  1256. return ret;
  1257. }
  1258. void OneofBuilderContext_register(VALUE module) {
  1259. VALUE klass = rb_define_class_under(
  1260. module, "OneofBuilderContext", rb_cObject);
  1261. rb_define_alloc_func(klass, OneofBuilderContext_alloc);
  1262. rb_define_method(klass, "initialize",
  1263. OneofBuilderContext_initialize, 2);
  1264. rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
  1265. cOneofBuilderContext = klass;
  1266. rb_gc_register_address(&cOneofBuilderContext);
  1267. }
  1268. /*
  1269. * call-seq:
  1270. * OneofBuilderContext.new(desc, builder) => context
  1271. *
  1272. * Create a new oneof builder context around the given oneof descriptor and
  1273. * builder context. This class is intended to serve as a DSL context to be used
  1274. * with #instance_eval.
  1275. */
  1276. VALUE OneofBuilderContext_initialize(VALUE _self,
  1277. VALUE oneofdef,
  1278. VALUE builder) {
  1279. DEFINE_SELF(OneofBuilderContext, self, _self);
  1280. self->descriptor = oneofdef;
  1281. self->builder = builder;
  1282. return Qnil;
  1283. }
  1284. /*
  1285. * call-seq:
  1286. * OneofBuilderContext.optional(name, type, number, type_class = nil)
  1287. *
  1288. * Defines a new optional field in this oneof with the given type, tag number,
  1289. * and type class (for message and enum fields). The type must be a Ruby symbol
  1290. * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
  1291. * if present (as accepted by FieldDescriptor#submsg_name=).
  1292. */
  1293. VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1294. DEFINE_SELF(OneofBuilderContext, self, _self);
  1295. if (argc < 3) {
  1296. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1297. }
  1298. VALUE name = argv[0];
  1299. VALUE type = argv[1];
  1300. VALUE number = argv[2];
  1301. VALUE type_class = (argc > 3) ? argv[3] : Qnil;
  1302. return msgdef_add_field(self->descriptor, "optional",
  1303. name, type, number, type_class);
  1304. }
  1305. // -----------------------------------------------------------------------------
  1306. // EnumBuilderContext.
  1307. // -----------------------------------------------------------------------------
  1308. DEFINE_CLASS(EnumBuilderContext,
  1309. "Google::Protobuf::Internal::EnumBuilderContext");
  1310. void EnumBuilderContext_mark(void* _self) {
  1311. EnumBuilderContext* self = _self;
  1312. rb_gc_mark(self->enumdesc);
  1313. }
  1314. void EnumBuilderContext_free(void* _self) {
  1315. EnumBuilderContext* self = _self;
  1316. xfree(self);
  1317. }
  1318. VALUE EnumBuilderContext_alloc(VALUE klass) {
  1319. EnumBuilderContext* self = ALLOC(EnumBuilderContext);
  1320. VALUE ret = TypedData_Wrap_Struct(
  1321. klass, &_EnumBuilderContext_type, self);
  1322. self->enumdesc = Qnil;
  1323. return ret;
  1324. }
  1325. void EnumBuilderContext_register(VALUE module) {
  1326. VALUE klass = rb_define_class_under(
  1327. module, "EnumBuilderContext", rb_cObject);
  1328. rb_define_alloc_func(klass, EnumBuilderContext_alloc);
  1329. rb_define_method(klass, "initialize",
  1330. EnumBuilderContext_initialize, 1);
  1331. rb_define_method(klass, "value", EnumBuilderContext_value, 2);
  1332. cEnumBuilderContext = klass;
  1333. rb_gc_register_address(&cEnumBuilderContext);
  1334. }
  1335. /*
  1336. * call-seq:
  1337. * EnumBuilderContext.new(enumdesc) => context
  1338. *
  1339. * Create a new builder context around the given enum descriptor. This class is
  1340. * intended to serve as a DSL context to be used with #instance_eval.
  1341. */
  1342. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
  1343. DEFINE_SELF(EnumBuilderContext, self, _self);
  1344. self->enumdesc = enumdef;
  1345. return Qnil;
  1346. }
  1347. static VALUE enumdef_add_value(VALUE enumdef,
  1348. VALUE name, VALUE number) {
  1349. rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
  1350. return Qnil;
  1351. }
  1352. /*
  1353. * call-seq:
  1354. * EnumBuilder.add_value(name, number)
  1355. *
  1356. * Adds the given name => number mapping to the enum type. Name must be a Ruby
  1357. * symbol.
  1358. */
  1359. VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
  1360. DEFINE_SELF(EnumBuilderContext, self, _self);
  1361. return enumdef_add_value(self->enumdesc, name, number);
  1362. }
  1363. // -----------------------------------------------------------------------------
  1364. // Builder.
  1365. // -----------------------------------------------------------------------------
  1366. DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
  1367. void Builder_mark(void* _self) {
  1368. Builder* self = _self;
  1369. rb_gc_mark(self->pending_list);
  1370. }
  1371. void Builder_free(void* _self) {
  1372. Builder* self = _self;
  1373. xfree(self->defs);
  1374. xfree(self);
  1375. }
  1376. /*
  1377. * call-seq:
  1378. * Builder.new => builder
  1379. *
  1380. * Creates a new Builder. A Builder can accumulate a set of new message and enum
  1381. * descriptors and atomically register them into a pool in a way that allows for
  1382. * (co)recursive type references.
  1383. */
  1384. VALUE Builder_alloc(VALUE klass) {
  1385. Builder* self = ALLOC(Builder);
  1386. VALUE ret = TypedData_Wrap_Struct(
  1387. klass, &_Builder_type, self);
  1388. self->pending_list = rb_ary_new();
  1389. self->defs = NULL;
  1390. return ret;
  1391. }
  1392. void Builder_register(VALUE module) {
  1393. VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
  1394. rb_define_alloc_func(klass, Builder_alloc);
  1395. rb_define_method(klass, "add_message", Builder_add_message, 1);
  1396. rb_define_method(klass, "add_enum", Builder_add_enum, 1);
  1397. rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
  1398. cBuilder = klass;
  1399. rb_gc_register_address(&cBuilder);
  1400. }
  1401. /*
  1402. * call-seq:
  1403. * Builder.add_message(name, &block)
  1404. *
  1405. * Creates a new, empty descriptor with the given name, and invokes the block in
  1406. * the context of a MessageBuilderContext on that descriptor. The block can then
  1407. * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
  1408. * methods to define the message fields.
  1409. *
  1410. * This is the recommended, idiomatic way to build message definitions.
  1411. */
  1412. VALUE Builder_add_message(VALUE _self, VALUE name) {
  1413. DEFINE_SELF(Builder, self, _self);
  1414. VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor);
  1415. VALUE args[2] = { msgdef, _self };
  1416. VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
  1417. VALUE block = rb_block_proc();
  1418. rb_funcall(msgdef, rb_intern("name="), 1, name);
  1419. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1420. rb_ary_push(self->pending_list, msgdef);
  1421. return Qnil;
  1422. }
  1423. /*
  1424. * call-seq:
  1425. * Builder.add_enum(name, &block)
  1426. *
  1427. * Creates a new, empty enum descriptor with the given name, and invokes the
  1428. * block in the context of an EnumBuilderContext on that descriptor. The block
  1429. * can then call EnumBuilderContext#add_value to define the enum values.
  1430. *
  1431. * This is the recommended, idiomatic way to build enum definitions.
  1432. */
  1433. VALUE Builder_add_enum(VALUE _self, VALUE name) {
  1434. DEFINE_SELF(Builder, self, _self);
  1435. VALUE enumdef = rb_class_new_instance(0, NULL, cEnumDescriptor);
  1436. VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
  1437. VALUE block = rb_block_proc();
  1438. rb_funcall(enumdef, rb_intern("name="), 1, name);
  1439. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1440. rb_ary_push(self->pending_list, enumdef);
  1441. return Qnil;
  1442. }
  1443. static void validate_msgdef(const upb_msgdef* msgdef) {
  1444. // Verify that no required fields exist. proto3 does not support these.
  1445. upb_msg_field_iter it;
  1446. for (upb_msg_field_begin(&it, msgdef);
  1447. !upb_msg_field_done(&it);
  1448. upb_msg_field_next(&it)) {
  1449. const upb_fielddef* field = upb_msg_iter_field(&it);
  1450. if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
  1451. rb_raise(rb_eTypeError, "Required fields are unsupported in proto3.");
  1452. }
  1453. }
  1454. }
  1455. static void validate_enumdef(const upb_enumdef* enumdef) {
  1456. // Verify that an entry exists with integer value 0. (This is the default
  1457. // value.)
  1458. const char* lookup = upb_enumdef_iton(enumdef, 0);
  1459. if (lookup == NULL) {
  1460. rb_raise(rb_eTypeError,
  1461. "Enum definition does not contain a value for '0'.");
  1462. }
  1463. }
  1464. /*
  1465. * call-seq:
  1466. * Builder.finalize_to_pool(pool)
  1467. *
  1468. * Adds all accumulated message and enum descriptors created in this builder
  1469. * context to the given pool. The operation occurs atomically, and all
  1470. * descriptors can refer to each other (including in cycles). This is the only
  1471. * way to build (co)recursive message definitions.
  1472. *
  1473. * This method is usually called automatically by DescriptorPool#build after it
  1474. * invokes the given user block in the context of the builder. The user should
  1475. * not normally need to call this manually because a Builder is not normally
  1476. * created manually.
  1477. */
  1478. VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
  1479. DEFINE_SELF(Builder, self, _self);
  1480. DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
  1481. REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
  1482. for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
  1483. VALUE def_rb = rb_ary_entry(self->pending_list, i);
  1484. if (CLASS_OF(def_rb) == cDescriptor) {
  1485. self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef;
  1486. validate_msgdef((const upb_msgdef*)self->defs[i]);
  1487. } else if (CLASS_OF(def_rb) == cEnumDescriptor) {
  1488. self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
  1489. validate_enumdef((const upb_enumdef*)self->defs[i]);
  1490. }
  1491. }
  1492. CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
  1493. RARRAY_LEN(self->pending_list), NULL, &status),
  1494. "Unable to add defs to DescriptorPool");
  1495. for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
  1496. VALUE def_rb = rb_ary_entry(self->pending_list, i);
  1497. add_def_obj(self->defs[i], def_rb);
  1498. }
  1499. self->pending_list = rb_ary_new();
  1500. return Qnil;
  1501. }