defs.c 58 KB

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