defs.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  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. upb_label_t upb_label = -1;
  597. bool converted = false;
  598. if (TYPE(label) != T_SYMBOL) {
  599. rb_raise(rb_eArgError, "Expected symbol for field label.");
  600. }
  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. const char* str = get_str(value);
  670. if (!upb_fielddef_hassubdef(self->fielddef)) {
  671. rb_raise(rb_eTypeError, "FieldDescriptor does not have subdef.");
  672. }
  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. const upb_def* def;
  689. if (!upb_fielddef_hassubdef(self->fielddef)) {
  690. return Qnil;
  691. }
  692. 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. VALUE name, type, number, type_class;
  1078. if (argc < 3) {
  1079. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1080. }
  1081. name = argv[0];
  1082. type = argv[1];
  1083. number = argv[2];
  1084. type_class = (argc > 3) ? argv[3] : Qnil;
  1085. return msgdef_add_field(self->descriptor, "optional",
  1086. name, type, number, type_class);
  1087. }
  1088. /*
  1089. * call-seq:
  1090. * MessageBuilderContext.required(name, type, number, type_class = nil)
  1091. *
  1092. * Defines a new required field on this message type with the given type, tag
  1093. * number, and type class (for message and enum fields). The type must be a Ruby
  1094. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1095. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1096. *
  1097. * Proto3 does not have required fields, but this method exists for
  1098. * completeness. Any attempt to add a message type with required fields to a
  1099. * pool will currently result in an error.
  1100. */
  1101. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
  1102. DEFINE_SELF(MessageBuilderContext, self, _self);
  1103. VALUE name, type, number, type_class;
  1104. if (argc < 3) {
  1105. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1106. }
  1107. name = argv[0];
  1108. type = argv[1];
  1109. number = argv[2];
  1110. type_class = (argc > 3) ? argv[3] : Qnil;
  1111. return msgdef_add_field(self->descriptor, "required",
  1112. name, type, number, type_class);
  1113. }
  1114. /*
  1115. * call-seq:
  1116. * MessageBuilderContext.repeated(name, type, number, type_class = nil)
  1117. *
  1118. * Defines a new repeated field on this message type with the given type, tag
  1119. * number, and type class (for message and enum fields). The type must be a Ruby
  1120. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1121. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1122. */
  1123. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
  1124. DEFINE_SELF(MessageBuilderContext, self, _self);
  1125. VALUE name, type, number, type_class;
  1126. if (argc < 3) {
  1127. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1128. }
  1129. name = argv[0];
  1130. type = argv[1];
  1131. number = argv[2];
  1132. type_class = (argc > 3) ? argv[3] : Qnil;
  1133. return msgdef_add_field(self->descriptor, "repeated",
  1134. name, type, number, type_class);
  1135. }
  1136. /*
  1137. * call-seq:
  1138. * MessageBuilderContext.map(name, key_type, value_type, number,
  1139. * value_type_class = nil)
  1140. *
  1141. * Defines a new map field on this message type with the given key and value
  1142. * types, tag number, and type class (for message and enum value types). The key
  1143. * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
  1144. * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
  1145. * type_class must be a string, if present (as accepted by
  1146. * FieldDescriptor#submsg_name=).
  1147. */
  1148. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
  1149. DEFINE_SELF(MessageBuilderContext, self, _self);
  1150. VALUE name, key_type, value_type, number, type_class;
  1151. VALUE mapentry_desc, mapentry_desc_name;
  1152. if (argc < 4) {
  1153. rb_raise(rb_eArgError, "Expected at least 4 arguments.");
  1154. }
  1155. name = argv[0];
  1156. key_type = argv[1];
  1157. value_type = argv[2];
  1158. number = argv[3];
  1159. type_class = (argc > 4) ? argv[4] : Qnil;
  1160. // Validate the key type. We can't accept enums, messages, or floats/doubles
  1161. // as map keys. (We exclude these explicitly, and the field-descriptor setter
  1162. // below then ensures that the type is one of the remaining valid options.)
  1163. if (SYM2ID(key_type) == rb_intern("float") ||
  1164. SYM2ID(key_type) == rb_intern("double") ||
  1165. SYM2ID(key_type) == rb_intern("enum") ||
  1166. SYM2ID(key_type) == rb_intern("message")) {
  1167. rb_raise(rb_eArgError,
  1168. "Cannot add a map field with a float, double, enum, or message "
  1169. "type.");
  1170. }
  1171. // Create a new message descriptor for the map entry message, and create a
  1172. // repeated submessage field here with that type.
  1173. mapentry_desc = rb_class_new_instance(0, NULL, cDescriptor);
  1174. mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
  1175. mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
  1176. mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
  1177. rb_id2name(SYM2ID(name)));
  1178. Descriptor_name_set(mapentry_desc, mapentry_desc_name);
  1179. {
  1180. // The 'mapentry' attribute has no Ruby setter because we do not want the
  1181. // user attempting to DIY the setup below; we want to ensure that the fields
  1182. // are correct. So we reach into the msgdef here to set the bit manually.
  1183. Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
  1184. upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
  1185. }
  1186. {
  1187. // optional <type> key = 1;
  1188. VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1189. FieldDescriptor_name_set(key_field, rb_str_new2("key"));
  1190. FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
  1191. FieldDescriptor_number_set(key_field, INT2NUM(1));
  1192. FieldDescriptor_type_set(key_field, key_type);
  1193. Descriptor_add_field(mapentry_desc, key_field);
  1194. }
  1195. {
  1196. // optional <type> value = 2;
  1197. VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1198. FieldDescriptor_name_set(value_field, rb_str_new2("value"));
  1199. FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
  1200. FieldDescriptor_number_set(value_field, INT2NUM(2));
  1201. FieldDescriptor_type_set(value_field, value_type);
  1202. if (type_class != Qnil) {
  1203. VALUE submsg_name = rb_str_new2("."); // prepend '.' to make absolute.
  1204. submsg_name = rb_str_append(submsg_name, type_class);
  1205. FieldDescriptor_submsg_name_set(value_field, submsg_name);
  1206. }
  1207. Descriptor_add_field(mapentry_desc, value_field);
  1208. }
  1209. {
  1210. // Add the map-entry message type to the current builder, and use the type
  1211. // to create the map field itself.
  1212. Builder* builder_self = ruby_to_Builder(self->builder);
  1213. rb_ary_push(builder_self->pending_list, mapentry_desc);
  1214. }
  1215. {
  1216. VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1217. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1218. VALUE submsg_name;
  1219. FieldDescriptor_name_set(map_field, name_str);
  1220. FieldDescriptor_number_set(map_field, number);
  1221. FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
  1222. FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
  1223. submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
  1224. submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
  1225. FieldDescriptor_submsg_name_set(map_field, submsg_name);
  1226. Descriptor_add_field(self->descriptor, map_field);
  1227. }
  1228. return Qnil;
  1229. }
  1230. /*
  1231. * call-seq:
  1232. * MessageBuilderContext.oneof(name, &block) => nil
  1233. *
  1234. * Creates a new OneofDescriptor with the given name, creates a
  1235. * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
  1236. * block in the context of that OneofBuilderContext with #instance_eval, and
  1237. * then adds the oneof to the message.
  1238. *
  1239. * This is the recommended, idiomatic way to build oneof definitions.
  1240. */
  1241. VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
  1242. DEFINE_SELF(MessageBuilderContext, self, _self);
  1243. VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
  1244. VALUE args[2] = { oneofdef, self->builder };
  1245. VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
  1246. VALUE block = rb_block_proc();
  1247. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1248. rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
  1249. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1250. Descriptor_add_oneof(self->descriptor, oneofdef);
  1251. return Qnil;
  1252. }
  1253. // -----------------------------------------------------------------------------
  1254. // OneofBuilderContext.
  1255. // -----------------------------------------------------------------------------
  1256. DEFINE_CLASS(OneofBuilderContext,
  1257. "Google::Protobuf::Internal::OneofBuilderContext");
  1258. void OneofBuilderContext_mark(void* _self) {
  1259. OneofBuilderContext* self = _self;
  1260. rb_gc_mark(self->descriptor);
  1261. rb_gc_mark(self->builder);
  1262. }
  1263. void OneofBuilderContext_free(void* _self) {
  1264. OneofBuilderContext* self = _self;
  1265. xfree(self);
  1266. }
  1267. VALUE OneofBuilderContext_alloc(VALUE klass) {
  1268. OneofBuilderContext* self = ALLOC(OneofBuilderContext);
  1269. VALUE ret = TypedData_Wrap_Struct(
  1270. klass, &_OneofBuilderContext_type, self);
  1271. self->descriptor = Qnil;
  1272. self->builder = Qnil;
  1273. return ret;
  1274. }
  1275. void OneofBuilderContext_register(VALUE module) {
  1276. VALUE klass = rb_define_class_under(
  1277. module, "OneofBuilderContext", rb_cObject);
  1278. rb_define_alloc_func(klass, OneofBuilderContext_alloc);
  1279. rb_define_method(klass, "initialize",
  1280. OneofBuilderContext_initialize, 2);
  1281. rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
  1282. cOneofBuilderContext = klass;
  1283. rb_gc_register_address(&cOneofBuilderContext);
  1284. }
  1285. /*
  1286. * call-seq:
  1287. * OneofBuilderContext.new(desc, builder) => context
  1288. *
  1289. * Create a new oneof builder context around the given oneof descriptor and
  1290. * builder context. This class is intended to serve as a DSL context to be used
  1291. * with #instance_eval.
  1292. */
  1293. VALUE OneofBuilderContext_initialize(VALUE _self,
  1294. VALUE oneofdef,
  1295. VALUE builder) {
  1296. DEFINE_SELF(OneofBuilderContext, self, _self);
  1297. self->descriptor = oneofdef;
  1298. self->builder = builder;
  1299. return Qnil;
  1300. }
  1301. /*
  1302. * call-seq:
  1303. * OneofBuilderContext.optional(name, type, number, type_class = nil)
  1304. *
  1305. * Defines a new optional field in this oneof with the given type, tag number,
  1306. * and type class (for message and enum fields). The type must be a Ruby symbol
  1307. * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
  1308. * if present (as accepted by FieldDescriptor#submsg_name=).
  1309. */
  1310. VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1311. DEFINE_SELF(OneofBuilderContext, self, _self);
  1312. VALUE name, type, number, type_class;
  1313. if (argc < 3) {
  1314. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1315. }
  1316. name = argv[0];
  1317. type = argv[1];
  1318. number = argv[2];
  1319. type_class = (argc > 3) ? argv[3] : Qnil;
  1320. return msgdef_add_field(self->descriptor, "optional",
  1321. name, type, number, type_class);
  1322. }
  1323. // -----------------------------------------------------------------------------
  1324. // EnumBuilderContext.
  1325. // -----------------------------------------------------------------------------
  1326. DEFINE_CLASS(EnumBuilderContext,
  1327. "Google::Protobuf::Internal::EnumBuilderContext");
  1328. void EnumBuilderContext_mark(void* _self) {
  1329. EnumBuilderContext* self = _self;
  1330. rb_gc_mark(self->enumdesc);
  1331. }
  1332. void EnumBuilderContext_free(void* _self) {
  1333. EnumBuilderContext* self = _self;
  1334. xfree(self);
  1335. }
  1336. VALUE EnumBuilderContext_alloc(VALUE klass) {
  1337. EnumBuilderContext* self = ALLOC(EnumBuilderContext);
  1338. VALUE ret = TypedData_Wrap_Struct(
  1339. klass, &_EnumBuilderContext_type, self);
  1340. self->enumdesc = Qnil;
  1341. return ret;
  1342. }
  1343. void EnumBuilderContext_register(VALUE module) {
  1344. VALUE klass = rb_define_class_under(
  1345. module, "EnumBuilderContext", rb_cObject);
  1346. rb_define_alloc_func(klass, EnumBuilderContext_alloc);
  1347. rb_define_method(klass, "initialize",
  1348. EnumBuilderContext_initialize, 1);
  1349. rb_define_method(klass, "value", EnumBuilderContext_value, 2);
  1350. cEnumBuilderContext = klass;
  1351. rb_gc_register_address(&cEnumBuilderContext);
  1352. }
  1353. /*
  1354. * call-seq:
  1355. * EnumBuilderContext.new(enumdesc) => context
  1356. *
  1357. * Create a new builder context around the given enum descriptor. This class is
  1358. * intended to serve as a DSL context to be used with #instance_eval.
  1359. */
  1360. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
  1361. DEFINE_SELF(EnumBuilderContext, self, _self);
  1362. self->enumdesc = enumdef;
  1363. return Qnil;
  1364. }
  1365. static VALUE enumdef_add_value(VALUE enumdef,
  1366. VALUE name, VALUE number) {
  1367. rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
  1368. return Qnil;
  1369. }
  1370. /*
  1371. * call-seq:
  1372. * EnumBuilder.add_value(name, number)
  1373. *
  1374. * Adds the given name => number mapping to the enum type. Name must be a Ruby
  1375. * symbol.
  1376. */
  1377. VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
  1378. DEFINE_SELF(EnumBuilderContext, self, _self);
  1379. return enumdef_add_value(self->enumdesc, name, number);
  1380. }
  1381. // -----------------------------------------------------------------------------
  1382. // Builder.
  1383. // -----------------------------------------------------------------------------
  1384. DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
  1385. void Builder_mark(void* _self) {
  1386. Builder* self = _self;
  1387. rb_gc_mark(self->pending_list);
  1388. }
  1389. void Builder_free(void* _self) {
  1390. Builder* self = _self;
  1391. xfree(self->defs);
  1392. xfree(self);
  1393. }
  1394. /*
  1395. * call-seq:
  1396. * Builder.new => builder
  1397. *
  1398. * Creates a new Builder. A Builder can accumulate a set of new message and enum
  1399. * descriptors and atomically register them into a pool in a way that allows for
  1400. * (co)recursive type references.
  1401. */
  1402. VALUE Builder_alloc(VALUE klass) {
  1403. Builder* self = ALLOC(Builder);
  1404. VALUE ret = TypedData_Wrap_Struct(
  1405. klass, &_Builder_type, self);
  1406. self->pending_list = rb_ary_new();
  1407. self->defs = NULL;
  1408. return ret;
  1409. }
  1410. void Builder_register(VALUE module) {
  1411. VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
  1412. rb_define_alloc_func(klass, Builder_alloc);
  1413. rb_define_method(klass, "add_message", Builder_add_message, 1);
  1414. rb_define_method(klass, "add_enum", Builder_add_enum, 1);
  1415. rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
  1416. cBuilder = klass;
  1417. rb_gc_register_address(&cBuilder);
  1418. }
  1419. /*
  1420. * call-seq:
  1421. * Builder.add_message(name, &block)
  1422. *
  1423. * Creates a new, empty descriptor with the given name, and invokes the block in
  1424. * the context of a MessageBuilderContext on that descriptor. The block can then
  1425. * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
  1426. * methods to define the message fields.
  1427. *
  1428. * This is the recommended, idiomatic way to build message definitions.
  1429. */
  1430. VALUE Builder_add_message(VALUE _self, VALUE name) {
  1431. DEFINE_SELF(Builder, self, _self);
  1432. VALUE msgdef = rb_class_new_instance(0, NULL, cDescriptor);
  1433. VALUE args[2] = { msgdef, _self };
  1434. VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
  1435. VALUE block = rb_block_proc();
  1436. rb_funcall(msgdef, rb_intern("name="), 1, name);
  1437. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1438. rb_ary_push(self->pending_list, msgdef);
  1439. return Qnil;
  1440. }
  1441. /*
  1442. * call-seq:
  1443. * Builder.add_enum(name, &block)
  1444. *
  1445. * Creates a new, empty enum descriptor with the given name, and invokes the
  1446. * block in the context of an EnumBuilderContext on that descriptor. The block
  1447. * can then call EnumBuilderContext#add_value to define the enum values.
  1448. *
  1449. * This is the recommended, idiomatic way to build enum definitions.
  1450. */
  1451. VALUE Builder_add_enum(VALUE _self, VALUE name) {
  1452. DEFINE_SELF(Builder, self, _self);
  1453. VALUE enumdef = rb_class_new_instance(0, NULL, cEnumDescriptor);
  1454. VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
  1455. VALUE block = rb_block_proc();
  1456. rb_funcall(enumdef, rb_intern("name="), 1, name);
  1457. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1458. rb_ary_push(self->pending_list, enumdef);
  1459. return Qnil;
  1460. }
  1461. static void validate_msgdef(const upb_msgdef* msgdef) {
  1462. // Verify that no required fields exist. proto3 does not support these.
  1463. upb_msg_field_iter it;
  1464. for (upb_msg_field_begin(&it, msgdef);
  1465. !upb_msg_field_done(&it);
  1466. upb_msg_field_next(&it)) {
  1467. const upb_fielddef* field = upb_msg_iter_field(&it);
  1468. if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
  1469. rb_raise(rb_eTypeError, "Required fields are unsupported in proto3.");
  1470. }
  1471. }
  1472. }
  1473. static void validate_enumdef(const upb_enumdef* enumdef) {
  1474. // Verify that an entry exists with integer value 0. (This is the default
  1475. // value.)
  1476. const char* lookup = upb_enumdef_iton(enumdef, 0);
  1477. if (lookup == NULL) {
  1478. rb_raise(rb_eTypeError,
  1479. "Enum definition does not contain a value for '0'.");
  1480. }
  1481. }
  1482. /*
  1483. * call-seq:
  1484. * Builder.finalize_to_pool(pool)
  1485. *
  1486. * Adds all accumulated message and enum descriptors created in this builder
  1487. * context to the given pool. The operation occurs atomically, and all
  1488. * descriptors can refer to each other (including in cycles). This is the only
  1489. * way to build (co)recursive message definitions.
  1490. *
  1491. * This method is usually called automatically by DescriptorPool#build after it
  1492. * invokes the given user block in the context of the builder. The user should
  1493. * not normally need to call this manually because a Builder is not normally
  1494. * created manually.
  1495. */
  1496. VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
  1497. DEFINE_SELF(Builder, self, _self);
  1498. DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
  1499. REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
  1500. for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
  1501. VALUE def_rb = rb_ary_entry(self->pending_list, i);
  1502. if (CLASS_OF(def_rb) == cDescriptor) {
  1503. self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef;
  1504. validate_msgdef((const upb_msgdef*)self->defs[i]);
  1505. } else if (CLASS_OF(def_rb) == cEnumDescriptor) {
  1506. self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
  1507. validate_enumdef((const upb_enumdef*)self->defs[i]);
  1508. }
  1509. }
  1510. CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
  1511. RARRAY_LEN(self->pending_list), NULL, &status),
  1512. "Unable to add defs to DescriptorPool");
  1513. for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
  1514. VALUE def_rb = rb_ary_entry(self->pending_list, i);
  1515. add_def_obj(self->defs[i], def_rb);
  1516. }
  1517. self->pending_list = rb_ary_new();
  1518. return Qnil;
  1519. }