defs.c 57 KB

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