defs.c 56 KB

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