defs.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257
  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 = Qnil; \
  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_free(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();
  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, -1);
  108. rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
  109. rb_define_singleton_method(klass, "generated_pool",
  110. DescriptorPool_generated_pool, 0);
  111. rb_gc_register_address(&cDescriptorPool);
  112. cDescriptorPool = klass;
  113. rb_gc_register_address(&generated_pool);
  114. generated_pool = rb_class_new_instance(0, NULL, klass);
  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(int argc, VALUE* argv, 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. }
  204. void Descriptor_free(void* _self) {
  205. Descriptor* self = _self;
  206. upb_msgdef_unref(self->msgdef, &self->msgdef);
  207. if (self->layout) {
  208. free_layout(self->layout);
  209. }
  210. if (self->fill_handlers) {
  211. upb_handlers_unref(self->fill_handlers, &self->fill_handlers);
  212. }
  213. if (self->fill_method) {
  214. upb_pbdecodermethod_unref(self->fill_method, &self->fill_method);
  215. }
  216. if (self->json_fill_method) {
  217. upb_json_parsermethod_unref(self->json_fill_method,
  218. &self->json_fill_method);
  219. }
  220. if (self->pb_serialize_handlers) {
  221. upb_handlers_unref(self->pb_serialize_handlers,
  222. &self->pb_serialize_handlers);
  223. }
  224. if (self->json_serialize_handlers) {
  225. upb_handlers_unref(self->json_serialize_handlers,
  226. &self->json_serialize_handlers);
  227. }
  228. if (self->json_serialize_handlers_preserve) {
  229. upb_handlers_unref(self->json_serialize_handlers_preserve,
  230. &self->json_serialize_handlers_preserve);
  231. }
  232. xfree(self);
  233. }
  234. /*
  235. * call-seq:
  236. * Descriptor.new => descriptor
  237. *
  238. * Creates a new, empty, message type descriptor. At a minimum, its name must be
  239. * set before it is added to a pool. It cannot be used to create messages until
  240. * it is added to a pool, after which it becomes immutable (as part of a
  241. * finalization process).
  242. */
  243. VALUE Descriptor_alloc(VALUE klass) {
  244. Descriptor* self = ALLOC(Descriptor);
  245. VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
  246. self->msgdef = upb_msgdef_new(&self->msgdef);
  247. self->klass = Qnil;
  248. self->layout = NULL;
  249. self->fill_handlers = NULL;
  250. self->fill_method = NULL;
  251. self->json_fill_method = NULL;
  252. self->pb_serialize_handlers = NULL;
  253. self->json_serialize_handlers = NULL;
  254. self->json_serialize_handlers_preserve = NULL;
  255. return ret;
  256. }
  257. void Descriptor_register(VALUE module) {
  258. VALUE klass = rb_define_class_under(
  259. module, "Descriptor", rb_cObject);
  260. rb_define_alloc_func(klass, Descriptor_alloc);
  261. rb_define_method(klass, "initialize", Descriptor_initialize, 1);
  262. rb_define_method(klass, "each", Descriptor_each, 0);
  263. rb_define_method(klass, "lookup", Descriptor_lookup, 1);
  264. rb_define_method(klass, "add_field", Descriptor_add_field, 1);
  265. rb_define_method(klass, "add_oneof", Descriptor_add_oneof, 1);
  266. rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
  267. rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
  268. rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
  269. rb_define_method(klass, "name", Descriptor_name, 0);
  270. rb_define_method(klass, "name=", Descriptor_name_set, 1);
  271. rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
  272. rb_include_module(klass, rb_mEnumerable);
  273. rb_gc_register_address(&cDescriptor);
  274. cDescriptor = klass;
  275. }
  276. /*
  277. * call-seq:
  278. * Descriptor.new(file_descriptor)
  279. *
  280. * Initializes a new descriptor and assigns a file descriptor to it.
  281. */
  282. VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
  283. DEFINE_SELF(Descriptor, self, _self);
  284. FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb);
  285. CHECK_UPB(
  286. upb_filedef_addmsg(file_descriptor->filedef, self->msgdef, NULL, &status),
  287. "Failed to associate message to file descriptor.");
  288. add_def_obj(file_descriptor->filedef, file_descriptor_rb);
  289. return Qnil;
  290. }
  291. /*
  292. * call-seq:
  293. * Descriptor.file_descriptor
  294. *
  295. * Returns the FileDescriptor object this message belongs to.
  296. */
  297. VALUE Descriptor_file_descriptor(VALUE _self) {
  298. DEFINE_SELF(Descriptor, self, _self);
  299. return get_def_obj(upb_def_file(self->msgdef));
  300. }
  301. /*
  302. * call-seq:
  303. * Descriptor.name => name
  304. *
  305. * Returns the name of this message type as a fully-qualfied string (e.g.,
  306. * My.Package.MessageType).
  307. */
  308. VALUE Descriptor_name(VALUE _self) {
  309. DEFINE_SELF(Descriptor, self, _self);
  310. return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
  311. }
  312. /*
  313. * call-seq:
  314. * Descriptor.name = name
  315. *
  316. * Assigns a name to this message type. The descriptor must not have been added
  317. * to a pool yet.
  318. */
  319. VALUE Descriptor_name_set(VALUE _self, VALUE str) {
  320. DEFINE_SELF(Descriptor, self, _self);
  321. upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
  322. const char* name = get_str(str);
  323. CHECK_UPB(
  324. upb_msgdef_setfullname(mut_def, name, &status),
  325. "Error setting Descriptor name");
  326. return Qnil;
  327. }
  328. /*
  329. * call-seq:
  330. * Descriptor.each(&block)
  331. *
  332. * Iterates over fields in this message type, yielding to the block on each one.
  333. */
  334. VALUE Descriptor_each(VALUE _self) {
  335. DEFINE_SELF(Descriptor, self, _self);
  336. upb_msg_field_iter it;
  337. for (upb_msg_field_begin(&it, self->msgdef);
  338. !upb_msg_field_done(&it);
  339. upb_msg_field_next(&it)) {
  340. const upb_fielddef* field = upb_msg_iter_field(&it);
  341. VALUE obj = get_def_obj(field);
  342. rb_yield(obj);
  343. }
  344. return Qnil;
  345. }
  346. /*
  347. * call-seq:
  348. * Descriptor.lookup(name) => FieldDescriptor
  349. *
  350. * Returns the field descriptor for the field with the given name, if present,
  351. * or nil if none.
  352. */
  353. VALUE Descriptor_lookup(VALUE _self, VALUE name) {
  354. DEFINE_SELF(Descriptor, self, _self);
  355. const char* s = get_str(name);
  356. const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
  357. if (field == NULL) {
  358. return Qnil;
  359. }
  360. return get_def_obj(field);
  361. }
  362. /*
  363. * call-seq:
  364. * Descriptor.add_field(field) => nil
  365. *
  366. * Adds the given FieldDescriptor to this message type. This descriptor must not
  367. * have been added to a pool yet. Raises an exception if a field with the same
  368. * name or number already exists. Sub-type references (e.g. for fields of type
  369. * message) are not resolved at this point.
  370. */
  371. VALUE Descriptor_add_field(VALUE _self, VALUE obj) {
  372. DEFINE_SELF(Descriptor, self, _self);
  373. upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
  374. FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
  375. upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
  376. CHECK_UPB(
  377. upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status),
  378. "Adding field to Descriptor failed");
  379. add_def_obj(def->fielddef, obj);
  380. return Qnil;
  381. }
  382. /*
  383. * call-seq:
  384. * Descriptor.add_oneof(oneof) => nil
  385. *
  386. * Adds the given OneofDescriptor to this message type. This descriptor must not
  387. * have been added to a pool yet. Raises an exception if a oneof with the same
  388. * name already exists, or if any of the oneof's fields' names or numbers
  389. * conflict with an existing field in this message type. All fields in the oneof
  390. * are added to the message descriptor. Sub-type references (e.g. for fields of
  391. * type message) are not resolved at this point.
  392. */
  393. VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
  394. DEFINE_SELF(Descriptor, self, _self);
  395. upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
  396. OneofDescriptor* def = ruby_to_OneofDescriptor(obj);
  397. upb_oneofdef* mut_oneof_def = check_oneof_notfrozen(def->oneofdef);
  398. CHECK_UPB(
  399. upb_msgdef_addoneof(mut_def, mut_oneof_def, NULL, &status),
  400. "Adding oneof to Descriptor failed");
  401. add_def_obj(def->oneofdef, obj);
  402. return Qnil;
  403. }
  404. /*
  405. * call-seq:
  406. * Descriptor.each_oneof(&block) => nil
  407. *
  408. * Invokes the given block for each oneof in this message type, passing the
  409. * corresponding OneofDescriptor.
  410. */
  411. VALUE Descriptor_each_oneof(VALUE _self) {
  412. DEFINE_SELF(Descriptor, self, _self);
  413. upb_msg_oneof_iter it;
  414. for (upb_msg_oneof_begin(&it, self->msgdef);
  415. !upb_msg_oneof_done(&it);
  416. upb_msg_oneof_next(&it)) {
  417. const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
  418. VALUE obj = get_def_obj(oneof);
  419. rb_yield(obj);
  420. }
  421. return Qnil;
  422. }
  423. /*
  424. * call-seq:
  425. * Descriptor.lookup_oneof(name) => OneofDescriptor
  426. *
  427. * Returns the oneof descriptor for the oneof with the given name, if present,
  428. * or nil if none.
  429. */
  430. VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
  431. DEFINE_SELF(Descriptor, self, _self);
  432. const char* s = get_str(name);
  433. const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
  434. if (oneof == NULL) {
  435. return Qnil;
  436. }
  437. return get_def_obj(oneof);
  438. }
  439. /*
  440. * call-seq:
  441. * Descriptor.msgclass => message_klass
  442. *
  443. * Returns the Ruby class created for this message type. Valid only once the
  444. * message type has been added to a pool.
  445. */
  446. VALUE Descriptor_msgclass(VALUE _self) {
  447. DEFINE_SELF(Descriptor, self, _self);
  448. if (!upb_def_isfrozen((const upb_def*)self->msgdef)) {
  449. rb_raise(rb_eRuntimeError,
  450. "Cannot fetch message class from a Descriptor not yet in a pool.");
  451. }
  452. if (self->klass == Qnil) {
  453. self->klass = build_class_from_descriptor(self);
  454. }
  455. return self->klass;
  456. }
  457. // -----------------------------------------------------------------------------
  458. // FileDescriptor.
  459. // -----------------------------------------------------------------------------
  460. DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
  461. void FileDescriptor_mark(void* _self) {
  462. }
  463. void FileDescriptor_free(void* _self) {
  464. FileDescriptor* self = _self;
  465. upb_filedef_unref(self->filedef, &self->filedef);
  466. xfree(self);
  467. }
  468. /*
  469. * call-seq:
  470. * FileDescriptor.new => file
  471. *
  472. * Returns a new file descriptor. The syntax must be set before it's passed
  473. * to a builder.
  474. */
  475. VALUE FileDescriptor_alloc(VALUE klass) {
  476. FileDescriptor* self = ALLOC(FileDescriptor);
  477. VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
  478. upb_filedef* filedef = upb_filedef_new(&self->filedef);
  479. self->filedef = filedef;
  480. return ret;
  481. }
  482. void FileDescriptor_register(VALUE module) {
  483. VALUE klass = rb_define_class_under(
  484. module, "FileDescriptor", rb_cObject);
  485. rb_define_alloc_func(klass, FileDescriptor_alloc);
  486. rb_define_method(klass, "initialize", FileDescriptor_initialize, -1);
  487. rb_define_method(klass, "name", FileDescriptor_name, 0);
  488. rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
  489. rb_define_method(klass, "syntax=", FileDescriptor_syntax_set, 1);
  490. rb_gc_register_address(&cFileDescriptor);
  491. cFileDescriptor = klass;
  492. }
  493. /*
  494. * call-seq:
  495. * FileDescriptor.new(name, options = nil) => file
  496. *
  497. * Initializes a new file descriptor with the given file name.
  498. * Also accepts an optional "options" hash, specifying other optional
  499. * metadata about the file. The options hash currently accepts the following
  500. * * "syntax": :proto2 or :proto3 (default: :proto3)
  501. */
  502. VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self) {
  503. DEFINE_SELF(FileDescriptor, self, _self);
  504. VALUE name_rb;
  505. VALUE options = Qnil;
  506. rb_scan_args(argc, argv, "11", &name_rb, &options);
  507. if (name_rb != Qnil) {
  508. Check_Type(name_rb, T_STRING);
  509. const char* name = get_str(name_rb);
  510. CHECK_UPB(upb_filedef_setname(self->filedef, name, &status),
  511. "Error setting file name");
  512. }
  513. // Default syntax is proto3.
  514. VALUE syntax = ID2SYM(rb_intern("proto3"));
  515. if (options != Qnil) {
  516. Check_Type(options, T_HASH);
  517. if (rb_funcall(options, rb_intern("key?"), 1,
  518. ID2SYM(rb_intern("syntax"))) == Qtrue) {
  519. syntax = rb_hash_lookup(options, ID2SYM(rb_intern("syntax")));
  520. }
  521. }
  522. FileDescriptor_syntax_set(_self, syntax);
  523. return Qnil;
  524. }
  525. /*
  526. * call-seq:
  527. * FileDescriptor.name => name
  528. *
  529. * Returns the name of the file.
  530. */
  531. VALUE FileDescriptor_name(VALUE _self) {
  532. DEFINE_SELF(FileDescriptor, self, _self);
  533. const char* name = upb_filedef_name(self->filedef);
  534. return name == NULL ? Qnil : rb_str_new2(name);
  535. }
  536. /*
  537. * call-seq:
  538. * FileDescriptor.syntax => syntax
  539. *
  540. * Returns this file descriptors syntax.
  541. *
  542. * Valid syntax versions are:
  543. * :proto2 or :proto3.
  544. */
  545. VALUE FileDescriptor_syntax(VALUE _self) {
  546. DEFINE_SELF(FileDescriptor, self, _self);
  547. switch (upb_filedef_syntax(self->filedef)) {
  548. case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
  549. case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
  550. default: return Qnil;
  551. }
  552. }
  553. /*
  554. * call-seq:
  555. * FileDescriptor.syntax = version
  556. *
  557. * Sets this file descriptor's syntax, can be :proto3 or :proto2.
  558. */
  559. VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax_rb) {
  560. DEFINE_SELF(FileDescriptor, self, _self);
  561. Check_Type(syntax_rb, T_SYMBOL);
  562. upb_syntax_t syntax;
  563. if (SYM2ID(syntax_rb) == rb_intern("proto3")) {
  564. syntax = UPB_SYNTAX_PROTO3;
  565. } else if (SYM2ID(syntax_rb) == rb_intern("proto2")) {
  566. syntax = UPB_SYNTAX_PROTO2;
  567. } else {
  568. rb_raise(rb_eArgError, "Expected :proto3 or :proto3, received '%s'",
  569. rb_id2name(SYM2ID(syntax_rb)));
  570. }
  571. CHECK_UPB(upb_filedef_setsyntax(self->filedef, syntax, &status),
  572. "Error setting file syntax for proto");
  573. return Qnil;
  574. }
  575. // -----------------------------------------------------------------------------
  576. // FieldDescriptor.
  577. // -----------------------------------------------------------------------------
  578. DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
  579. void FieldDescriptor_mark(void* _self) {
  580. }
  581. void FieldDescriptor_free(void* _self) {
  582. FieldDescriptor* self = _self;
  583. upb_fielddef_unref(self->fielddef, &self->fielddef);
  584. xfree(self);
  585. }
  586. /*
  587. * call-seq:
  588. * FieldDescriptor.new => field
  589. *
  590. * Returns a new field descriptor. Its name, type, etc. must be set before it is
  591. * added to a message type.
  592. */
  593. VALUE FieldDescriptor_alloc(VALUE klass) {
  594. FieldDescriptor* self = ALLOC(FieldDescriptor);
  595. VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
  596. upb_fielddef* fielddef = upb_fielddef_new(&self->fielddef);
  597. upb_fielddef_setpacked(fielddef, false);
  598. self->fielddef = fielddef;
  599. return ret;
  600. }
  601. void FieldDescriptor_register(VALUE module) {
  602. VALUE klass = rb_define_class_under(
  603. module, "FieldDescriptor", rb_cObject);
  604. rb_define_alloc_func(klass, FieldDescriptor_alloc);
  605. rb_define_method(klass, "name", FieldDescriptor_name, 0);
  606. rb_define_method(klass, "name=", FieldDescriptor_name_set, 1);
  607. rb_define_method(klass, "type", FieldDescriptor_type, 0);
  608. rb_define_method(klass, "type=", FieldDescriptor_type_set, 1);
  609. rb_define_method(klass, "default", FieldDescriptor_default, 0);
  610. rb_define_method(klass, "default=", FieldDescriptor_default_set, 1);
  611. rb_define_method(klass, "label", FieldDescriptor_label, 0);
  612. rb_define_method(klass, "label=", FieldDescriptor_label_set, 1);
  613. rb_define_method(klass, "number", FieldDescriptor_number, 0);
  614. rb_define_method(klass, "number=", FieldDescriptor_number_set, 1);
  615. rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
  616. rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1);
  617. rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
  618. rb_define_method(klass, "has?", FieldDescriptor_has, 1);
  619. rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
  620. rb_define_method(klass, "get", FieldDescriptor_get, 1);
  621. rb_define_method(klass, "set", FieldDescriptor_set, 2);
  622. rb_gc_register_address(&cFieldDescriptor);
  623. cFieldDescriptor = klass;
  624. }
  625. /*
  626. * call-seq:
  627. * FieldDescriptor.name => name
  628. *
  629. * Returns the name of this field.
  630. */
  631. VALUE FieldDescriptor_name(VALUE _self) {
  632. DEFINE_SELF(FieldDescriptor, self, _self);
  633. return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
  634. }
  635. /*
  636. * call-seq:
  637. * FieldDescriptor.name = name
  638. *
  639. * Sets the name of this field. Cannot be called once the containing message
  640. * type, if any, is added to a pool.
  641. */
  642. VALUE FieldDescriptor_name_set(VALUE _self, VALUE str) {
  643. DEFINE_SELF(FieldDescriptor, self, _self);
  644. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  645. const char* name = get_str(str);
  646. CHECK_UPB(upb_fielddef_setname(mut_def, name, &status),
  647. "Error setting FieldDescriptor name");
  648. return Qnil;
  649. }
  650. upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
  651. if (TYPE(type) != T_SYMBOL) {
  652. rb_raise(rb_eArgError, "Expected symbol for field type.");
  653. }
  654. #define CONVERT(upb, ruby) \
  655. if (SYM2ID(type) == rb_intern( # ruby )) { \
  656. return UPB_TYPE_ ## upb; \
  657. }
  658. CONVERT(FLOAT, float);
  659. CONVERT(DOUBLE, double);
  660. CONVERT(BOOL, bool);
  661. CONVERT(STRING, string);
  662. CONVERT(BYTES, bytes);
  663. CONVERT(MESSAGE, message);
  664. CONVERT(ENUM, enum);
  665. CONVERT(INT32, int32);
  666. CONVERT(INT64, int64);
  667. CONVERT(UINT32, uint32);
  668. CONVERT(UINT64, uint64);
  669. #undef CONVERT
  670. rb_raise(rb_eArgError, "Unknown field type.");
  671. return 0;
  672. }
  673. VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
  674. switch (type) {
  675. #define CONVERT(upb, ruby) \
  676. case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
  677. CONVERT(FLOAT, float);
  678. CONVERT(DOUBLE, double);
  679. CONVERT(BOOL, bool);
  680. CONVERT(STRING, string);
  681. CONVERT(BYTES, bytes);
  682. CONVERT(MESSAGE, message);
  683. CONVERT(ENUM, enum);
  684. CONVERT(INT32, int32);
  685. CONVERT(INT64, int64);
  686. CONVERT(UINT32, uint32);
  687. CONVERT(UINT64, uint64);
  688. #undef CONVERT
  689. }
  690. return Qnil;
  691. }
  692. upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
  693. if (TYPE(type) != T_SYMBOL) {
  694. rb_raise(rb_eArgError, "Expected symbol for field type.");
  695. }
  696. #define CONVERT(upb, ruby) \
  697. if (SYM2ID(type) == rb_intern( # ruby )) { \
  698. return UPB_DESCRIPTOR_TYPE_ ## upb; \
  699. }
  700. CONVERT(FLOAT, float);
  701. CONVERT(DOUBLE, double);
  702. CONVERT(BOOL, bool);
  703. CONVERT(STRING, string);
  704. CONVERT(BYTES, bytes);
  705. CONVERT(MESSAGE, message);
  706. CONVERT(GROUP, group);
  707. CONVERT(ENUM, enum);
  708. CONVERT(INT32, int32);
  709. CONVERT(INT64, int64);
  710. CONVERT(UINT32, uint32);
  711. CONVERT(UINT64, uint64);
  712. CONVERT(SINT32, sint32);
  713. CONVERT(SINT64, sint64);
  714. CONVERT(FIXED32, fixed32);
  715. CONVERT(FIXED64, fixed64);
  716. CONVERT(SFIXED32, sfixed32);
  717. CONVERT(SFIXED64, sfixed64);
  718. #undef CONVERT
  719. rb_raise(rb_eArgError, "Unknown field type.");
  720. return 0;
  721. }
  722. VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
  723. switch (type) {
  724. #define CONVERT(upb, ruby) \
  725. case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
  726. CONVERT(FLOAT, float);
  727. CONVERT(DOUBLE, double);
  728. CONVERT(BOOL, bool);
  729. CONVERT(STRING, string);
  730. CONVERT(BYTES, bytes);
  731. CONVERT(MESSAGE, message);
  732. CONVERT(GROUP, group);
  733. CONVERT(ENUM, enum);
  734. CONVERT(INT32, int32);
  735. CONVERT(INT64, int64);
  736. CONVERT(UINT32, uint32);
  737. CONVERT(UINT64, uint64);
  738. CONVERT(SINT32, sint32);
  739. CONVERT(SINT64, sint64);
  740. CONVERT(FIXED32, fixed32);
  741. CONVERT(FIXED64, fixed64);
  742. CONVERT(SFIXED32, sfixed32);
  743. CONVERT(SFIXED64, sfixed64);
  744. #undef CONVERT
  745. }
  746. return Qnil;
  747. }
  748. /*
  749. * call-seq:
  750. * FieldDescriptor.type => type
  751. *
  752. * Returns this field's type, as a Ruby symbol, or nil if not yet set.
  753. *
  754. * Valid field types are:
  755. * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
  756. * :bytes, :message.
  757. */
  758. VALUE FieldDescriptor_type(VALUE _self) {
  759. DEFINE_SELF(FieldDescriptor, self, _self);
  760. if (!upb_fielddef_typeisset(self->fielddef)) {
  761. return Qnil;
  762. }
  763. return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
  764. }
  765. /*
  766. * call-seq:
  767. * FieldDescriptor.type = type
  768. *
  769. * Sets this field's type. Cannot be called if field is part of a message type
  770. * already in a pool.
  771. */
  772. VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
  773. DEFINE_SELF(FieldDescriptor, self, _self);
  774. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  775. upb_fielddef_setdescriptortype(mut_def, ruby_to_descriptortype(type));
  776. return Qnil;
  777. }
  778. /*
  779. * call-seq:
  780. * FieldDescriptor.default => default
  781. *
  782. * Returns this field's default, as a Ruby object, or nil if not yet set.
  783. */
  784. VALUE FieldDescriptor_default(VALUE _self) {
  785. DEFINE_SELF(FieldDescriptor, self, _self);
  786. return layout_get_default(self->fielddef);
  787. }
  788. /*
  789. * call-seq:
  790. * FieldDescriptor.default = default
  791. *
  792. * Sets this field's default value. Raises an exception when calling with
  793. * proto syntax 3.
  794. */
  795. VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value) {
  796. DEFINE_SELF(FieldDescriptor, self, _self);
  797. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  798. switch (upb_fielddef_type(mut_def)) {
  799. case UPB_TYPE_FLOAT:
  800. upb_fielddef_setdefaultfloat(mut_def, NUM2DBL(default_value));
  801. break;
  802. case UPB_TYPE_DOUBLE:
  803. upb_fielddef_setdefaultdouble(mut_def, NUM2DBL(default_value));
  804. break;
  805. case UPB_TYPE_BOOL:
  806. if (!RB_TYPE_P(default_value, T_TRUE) &&
  807. !RB_TYPE_P(default_value, T_FALSE) &&
  808. !RB_TYPE_P(default_value, T_NIL)) {
  809. rb_raise(cTypeError, "Expected boolean for default value.");
  810. }
  811. upb_fielddef_setdefaultbool(mut_def, RTEST(default_value));
  812. break;
  813. case UPB_TYPE_ENUM:
  814. case UPB_TYPE_INT32:
  815. upb_fielddef_setdefaultint32(mut_def, NUM2INT(default_value));
  816. break;
  817. case UPB_TYPE_INT64:
  818. upb_fielddef_setdefaultint64(mut_def, NUM2INT(default_value));
  819. break;
  820. case UPB_TYPE_UINT32:
  821. upb_fielddef_setdefaultuint32(mut_def, NUM2UINT(default_value));
  822. break;
  823. case UPB_TYPE_UINT64:
  824. upb_fielddef_setdefaultuint64(mut_def, NUM2UINT(default_value));
  825. break;
  826. case UPB_TYPE_STRING:
  827. case UPB_TYPE_BYTES:
  828. CHECK_UPB(upb_fielddef_setdefaultcstr(mut_def, StringValuePtr(default_value),
  829. &status),
  830. "Error setting default string");
  831. break;
  832. default:
  833. rb_raise(rb_eArgError, "Defaults not supported on field %s.%s",
  834. upb_fielddef_fullname(mut_def), upb_fielddef_name(mut_def));
  835. }
  836. return Qnil;
  837. }
  838. /*
  839. * call-seq:
  840. * FieldDescriptor.label => label
  841. *
  842. * Returns this field's label (i.e., plurality), as a Ruby symbol.
  843. *
  844. * Valid field labels are:
  845. * :optional, :repeated
  846. */
  847. VALUE FieldDescriptor_label(VALUE _self) {
  848. DEFINE_SELF(FieldDescriptor, self, _self);
  849. switch (upb_fielddef_label(self->fielddef)) {
  850. #define CONVERT(upb, ruby) \
  851. case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
  852. CONVERT(OPTIONAL, optional);
  853. CONVERT(REQUIRED, required);
  854. CONVERT(REPEATED, repeated);
  855. #undef CONVERT
  856. }
  857. return Qnil;
  858. }
  859. /*
  860. * call-seq:
  861. * FieldDescriptor.label = label
  862. *
  863. * Sets the label on this field. Cannot be called if field is part of a message
  864. * type already in a pool.
  865. */
  866. VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
  867. DEFINE_SELF(FieldDescriptor, self, _self);
  868. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  869. upb_label_t upb_label = -1;
  870. bool converted = false;
  871. if (TYPE(label) != T_SYMBOL) {
  872. rb_raise(rb_eArgError, "Expected symbol for field label.");
  873. }
  874. #define CONVERT(upb, ruby) \
  875. if (SYM2ID(label) == rb_intern( # ruby )) { \
  876. upb_label = UPB_LABEL_ ## upb; \
  877. converted = true; \
  878. }
  879. CONVERT(OPTIONAL, optional);
  880. CONVERT(REQUIRED, required);
  881. CONVERT(REPEATED, repeated);
  882. #undef CONVERT
  883. if (!converted) {
  884. rb_raise(rb_eArgError, "Unknown field label.");
  885. }
  886. upb_fielddef_setlabel(mut_def, upb_label);
  887. return Qnil;
  888. }
  889. /*
  890. * call-seq:
  891. * FieldDescriptor.number => number
  892. *
  893. * Returns the tag number for this field.
  894. */
  895. VALUE FieldDescriptor_number(VALUE _self) {
  896. DEFINE_SELF(FieldDescriptor, self, _self);
  897. return INT2NUM(upb_fielddef_number(self->fielddef));
  898. }
  899. /*
  900. * call-seq:
  901. * FieldDescriptor.number = number
  902. *
  903. * Sets the tag number for this field. Cannot be called if field is part of a
  904. * message type already in a pool.
  905. */
  906. VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
  907. DEFINE_SELF(FieldDescriptor, self, _self);
  908. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  909. CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
  910. "Error setting field number");
  911. return Qnil;
  912. }
  913. /*
  914. * call-seq:
  915. * FieldDescriptor.submsg_name => submsg_name
  916. *
  917. * Returns the name of the message or enum type corresponding to this field, if
  918. * it is a message or enum field (respectively), or nil otherwise. This type
  919. * name will be resolved within the context of the pool to which the containing
  920. * message type is added.
  921. */
  922. VALUE FieldDescriptor_submsg_name(VALUE _self) {
  923. DEFINE_SELF(FieldDescriptor, self, _self);
  924. if (!upb_fielddef_hassubdef(self->fielddef)) {
  925. return Qnil;
  926. }
  927. return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
  928. }
  929. /*
  930. * call-seq:
  931. * FieldDescriptor.submsg_name = submsg_name
  932. *
  933. * Sets the name of the message or enum type corresponding to this field, if it
  934. * is a message or enum field (respectively). This type name will be resolved
  935. * within the context of the pool to which the containing message type is added.
  936. * Cannot be called on field that are not of message or enum type, or on fields
  937. * that are part of a message type already added to a pool.
  938. */
  939. VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
  940. DEFINE_SELF(FieldDescriptor, self, _self);
  941. upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
  942. const char* str = get_str(value);
  943. if (!upb_fielddef_hassubdef(self->fielddef)) {
  944. rb_raise(cTypeError, "FieldDescriptor does not have subdef.");
  945. }
  946. CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
  947. "Error setting submessage name");
  948. return Qnil;
  949. }
  950. /*
  951. * call-seq:
  952. * FieldDescriptor.subtype => message_or_enum_descriptor
  953. *
  954. * Returns the message or enum descriptor corresponding to this field's type if
  955. * it is a message or enum field, respectively, or nil otherwise. Cannot be
  956. * called *until* the containing message type is added to a pool (and thus
  957. * resolved).
  958. */
  959. VALUE FieldDescriptor_subtype(VALUE _self) {
  960. DEFINE_SELF(FieldDescriptor, self, _self);
  961. const upb_def* def;
  962. if (!upb_fielddef_hassubdef(self->fielddef)) {
  963. return Qnil;
  964. }
  965. def = upb_fielddef_subdef(self->fielddef);
  966. if (def == NULL) {
  967. return Qnil;
  968. }
  969. return get_def_obj(def);
  970. }
  971. /*
  972. * call-seq:
  973. * FieldDescriptor.get(message) => value
  974. *
  975. * Returns the value set for this field on the given message. Raises an
  976. * exception if message is of the wrong type.
  977. */
  978. VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
  979. DEFINE_SELF(FieldDescriptor, self, _self);
  980. MessageHeader* msg;
  981. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  982. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  983. rb_raise(cTypeError, "get method called on wrong message type");
  984. }
  985. return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
  986. }
  987. /*
  988. * call-seq:
  989. * FieldDescriptor.has?(message) => boolean
  990. *
  991. * Returns whether the value is set on the given message. Raises an
  992. * exception when calling with proto syntax 3.
  993. */
  994. VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
  995. DEFINE_SELF(FieldDescriptor, self, _self);
  996. MessageHeader* msg;
  997. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  998. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  999. rb_raise(cTypeError, "has method called on wrong message type");
  1000. } else if (!upb_fielddef_haspresence(self->fielddef)) {
  1001. rb_raise(rb_eArgError, "does not track presence");
  1002. }
  1003. return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef);
  1004. }
  1005. /*
  1006. * call-seq:
  1007. * FieldDescriptor.clear(message)
  1008. *
  1009. * Clears the field from the message if it's set.
  1010. */
  1011. VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
  1012. DEFINE_SELF(FieldDescriptor, self, _self);
  1013. MessageHeader* msg;
  1014. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  1015. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  1016. rb_raise(cTypeError, "has method called on wrong message type");
  1017. }
  1018. layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef);
  1019. return Qnil;
  1020. }
  1021. /*
  1022. * call-seq:
  1023. * FieldDescriptor.set(message, value)
  1024. *
  1025. * Sets the value corresponding to this field to the given value on the given
  1026. * message. Raises an exception if message is of the wrong type. Performs the
  1027. * ordinary type-checks for field setting.
  1028. */
  1029. VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
  1030. DEFINE_SELF(FieldDescriptor, self, _self);
  1031. MessageHeader* msg;
  1032. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  1033. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  1034. rb_raise(cTypeError, "set method called on wrong message type");
  1035. }
  1036. layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
  1037. return Qnil;
  1038. }
  1039. // -----------------------------------------------------------------------------
  1040. // OneofDescriptor.
  1041. // -----------------------------------------------------------------------------
  1042. DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
  1043. void OneofDescriptor_mark(void* _self) {
  1044. }
  1045. void OneofDescriptor_free(void* _self) {
  1046. OneofDescriptor* self = _self;
  1047. upb_oneofdef_unref(self->oneofdef, &self->oneofdef);
  1048. xfree(self);
  1049. }
  1050. /*
  1051. * call-seq:
  1052. * OneofDescriptor.new => oneof_descriptor
  1053. *
  1054. * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
  1055. * to being added to a message descriptor which is subsequently added to a pool.
  1056. */
  1057. VALUE OneofDescriptor_alloc(VALUE klass) {
  1058. OneofDescriptor* self = ALLOC(OneofDescriptor);
  1059. VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
  1060. self->oneofdef = upb_oneofdef_new(&self->oneofdef);
  1061. return ret;
  1062. }
  1063. void OneofDescriptor_register(VALUE module) {
  1064. VALUE klass = rb_define_class_under(
  1065. module, "OneofDescriptor", rb_cObject);
  1066. rb_define_alloc_func(klass, OneofDescriptor_alloc);
  1067. rb_define_method(klass, "name", OneofDescriptor_name, 0);
  1068. rb_define_method(klass, "name=", OneofDescriptor_name_set, 1);
  1069. rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1);
  1070. rb_define_method(klass, "each", OneofDescriptor_each, 0);
  1071. rb_include_module(klass, rb_mEnumerable);
  1072. rb_gc_register_address(&cOneofDescriptor);
  1073. cOneofDescriptor = klass;
  1074. }
  1075. /*
  1076. * call-seq:
  1077. * OneofDescriptor.name => name
  1078. *
  1079. * Returns the name of this oneof.
  1080. */
  1081. VALUE OneofDescriptor_name(VALUE _self) {
  1082. DEFINE_SELF(OneofDescriptor, self, _self);
  1083. return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
  1084. }
  1085. /*
  1086. * call-seq:
  1087. * OneofDescriptor.name = name
  1088. *
  1089. * Sets a new name for this oneof. The oneof must not have been added to a
  1090. * message descriptor yet.
  1091. */
  1092. VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) {
  1093. DEFINE_SELF(OneofDescriptor, self, _self);
  1094. upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
  1095. const char* str = get_str(value);
  1096. CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status),
  1097. "Error setting oneof name");
  1098. return Qnil;
  1099. }
  1100. /*
  1101. * call-seq:
  1102. * OneofDescriptor.add_field(field) => nil
  1103. *
  1104. * Adds a field to this oneof. The field may have been added to this oneof in
  1105. * the past, or the message to which this oneof belongs (if any), but may not
  1106. * have already been added to any other oneof or message. Otherwise, an
  1107. * exception is raised.
  1108. *
  1109. * All fields added to the oneof via this method will be automatically added to
  1110. * the message to which this oneof belongs, if it belongs to one currently, or
  1111. * else will be added to any message to which the oneof is later added at the
  1112. * time that it is added.
  1113. */
  1114. VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
  1115. DEFINE_SELF(OneofDescriptor, self, _self);
  1116. upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
  1117. FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
  1118. upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
  1119. CHECK_UPB(
  1120. upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status),
  1121. "Adding field to OneofDescriptor failed");
  1122. add_def_obj(def->fielddef, obj);
  1123. return Qnil;
  1124. }
  1125. /*
  1126. * call-seq:
  1127. * OneofDescriptor.each(&block) => nil
  1128. *
  1129. * Iterates through fields in this oneof, yielding to the block on each one.
  1130. */
  1131. VALUE OneofDescriptor_each(VALUE _self, VALUE field) {
  1132. DEFINE_SELF(OneofDescriptor, self, _self);
  1133. upb_oneof_iter it;
  1134. for (upb_oneof_begin(&it, self->oneofdef);
  1135. !upb_oneof_done(&it);
  1136. upb_oneof_next(&it)) {
  1137. const upb_fielddef* f = upb_oneof_iter_field(&it);
  1138. VALUE obj = get_def_obj(f);
  1139. rb_yield(obj);
  1140. }
  1141. return Qnil;
  1142. }
  1143. // -----------------------------------------------------------------------------
  1144. // EnumDescriptor.
  1145. // -----------------------------------------------------------------------------
  1146. DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
  1147. void EnumDescriptor_mark(void* _self) {
  1148. EnumDescriptor* self = _self;
  1149. rb_gc_mark(self->module);
  1150. }
  1151. void EnumDescriptor_free(void* _self) {
  1152. EnumDescriptor* self = _self;
  1153. upb_enumdef_unref(self->enumdef, &self->enumdef);
  1154. xfree(self);
  1155. }
  1156. /*
  1157. * call-seq:
  1158. * EnumDescriptor.new => enum_descriptor
  1159. *
  1160. * Creates a new, empty, enum descriptor. Must be added to a pool before the
  1161. * enum type can be used. The enum type may only be modified prior to adding to
  1162. * a pool.
  1163. */
  1164. VALUE EnumDescriptor_alloc(VALUE klass) {
  1165. EnumDescriptor* self = ALLOC(EnumDescriptor);
  1166. VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
  1167. self->enumdef = upb_enumdef_new(&self->enumdef);
  1168. self->module = Qnil;
  1169. return ret;
  1170. }
  1171. void EnumDescriptor_register(VALUE module) {
  1172. VALUE klass = rb_define_class_under(
  1173. module, "EnumDescriptor", rb_cObject);
  1174. rb_define_alloc_func(klass, EnumDescriptor_alloc);
  1175. rb_define_method(klass, "initialize", EnumDescriptor_initialize, 1);
  1176. rb_define_method(klass, "name", EnumDescriptor_name, 0);
  1177. rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
  1178. rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
  1179. rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
  1180. rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
  1181. rb_define_method(klass, "each", EnumDescriptor_each, 0);
  1182. rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
  1183. rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
  1184. rb_include_module(klass, rb_mEnumerable);
  1185. rb_gc_register_address(&cEnumDescriptor);
  1186. cEnumDescriptor = klass;
  1187. }
  1188. /*
  1189. * call-seq:
  1190. * Descriptor.new(file_descriptor)
  1191. *
  1192. * Initializes a new descriptor and assigns a file descriptor to it.
  1193. */
  1194. VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
  1195. DEFINE_SELF(EnumDescriptor, self, _self);
  1196. FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb);
  1197. CHECK_UPB(
  1198. upb_filedef_addenum(file_descriptor->filedef, self->enumdef,
  1199. NULL, &status),
  1200. "Failed to associate enum to file descriptor.");
  1201. add_def_obj(file_descriptor->filedef, file_descriptor_rb);
  1202. return Qnil;
  1203. }
  1204. /*
  1205. * call-seq:
  1206. * Descriptor.file_descriptor
  1207. *
  1208. * Returns the FileDescriptor object this enum belongs to.
  1209. */
  1210. VALUE EnumDescriptor_file_descriptor(VALUE _self) {
  1211. DEFINE_SELF(EnumDescriptor, self, _self);
  1212. return get_def_obj(upb_def_file(self->enumdef));
  1213. }
  1214. /*
  1215. * call-seq:
  1216. * EnumDescriptor.name => name
  1217. *
  1218. * Returns the name of this enum type.
  1219. */
  1220. VALUE EnumDescriptor_name(VALUE _self) {
  1221. DEFINE_SELF(EnumDescriptor, self, _self);
  1222. return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
  1223. }
  1224. /*
  1225. * call-seq:
  1226. * EnumDescriptor.name = name
  1227. *
  1228. * Sets the name of this enum type. Cannot be called if the enum type has
  1229. * already been added to a pool.
  1230. */
  1231. VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) {
  1232. DEFINE_SELF(EnumDescriptor, self, _self);
  1233. upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
  1234. const char* name = get_str(str);
  1235. CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
  1236. "Error setting EnumDescriptor name");
  1237. return Qnil;
  1238. }
  1239. /*
  1240. * call-seq:
  1241. * EnumDescriptor.add_value(key, value)
  1242. *
  1243. * Adds a new key => value mapping to this enum type. Key must be given as a
  1244. * Ruby symbol. Cannot be called if the enum type has already been added to a
  1245. * pool. Will raise an exception if the key or value is already in use.
  1246. */
  1247. VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
  1248. DEFINE_SELF(EnumDescriptor, self, _self);
  1249. upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
  1250. const char* name_str = rb_id2name(SYM2ID(name));
  1251. int32_t val = NUM2INT(number);
  1252. CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
  1253. "Error adding value to enum");
  1254. return Qnil;
  1255. }
  1256. /*
  1257. * call-seq:
  1258. * EnumDescriptor.lookup_name(name) => value
  1259. *
  1260. * Returns the numeric value corresponding to the given key name (as a Ruby
  1261. * symbol), or nil if none.
  1262. */
  1263. VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
  1264. DEFINE_SELF(EnumDescriptor, self, _self);
  1265. const char* name_str= rb_id2name(SYM2ID(name));
  1266. int32_t val = 0;
  1267. if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
  1268. return INT2NUM(val);
  1269. } else {
  1270. return Qnil;
  1271. }
  1272. }
  1273. /*
  1274. * call-seq:
  1275. * EnumDescriptor.lookup_value(name) => value
  1276. *
  1277. * Returns the key name (as a Ruby symbol) corresponding to the integer value,
  1278. * or nil if none.
  1279. */
  1280. VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
  1281. DEFINE_SELF(EnumDescriptor, self, _self);
  1282. int32_t val = NUM2INT(number);
  1283. const char* name = upb_enumdef_iton(self->enumdef, val);
  1284. if (name != NULL) {
  1285. return ID2SYM(rb_intern(name));
  1286. } else {
  1287. return Qnil;
  1288. }
  1289. }
  1290. /*
  1291. * call-seq:
  1292. * EnumDescriptor.each(&block)
  1293. *
  1294. * Iterates over key => value mappings in this enum's definition, yielding to
  1295. * the block with (key, value) arguments for each one.
  1296. */
  1297. VALUE EnumDescriptor_each(VALUE _self) {
  1298. DEFINE_SELF(EnumDescriptor, self, _self);
  1299. upb_enum_iter it;
  1300. for (upb_enum_begin(&it, self->enumdef);
  1301. !upb_enum_done(&it);
  1302. upb_enum_next(&it)) {
  1303. VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
  1304. VALUE number = INT2NUM(upb_enum_iter_number(&it));
  1305. rb_yield_values(2, key, number);
  1306. }
  1307. return Qnil;
  1308. }
  1309. /*
  1310. * call-seq:
  1311. * EnumDescriptor.enummodule => module
  1312. *
  1313. * Returns the Ruby module corresponding to this enum type. Cannot be called
  1314. * until the enum descriptor has been added to a pool.
  1315. */
  1316. VALUE EnumDescriptor_enummodule(VALUE _self) {
  1317. DEFINE_SELF(EnumDescriptor, self, _self);
  1318. if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
  1319. rb_raise(rb_eRuntimeError,
  1320. "Cannot fetch enum module from an EnumDescriptor not yet "
  1321. "in a pool.");
  1322. }
  1323. if (self->module == Qnil) {
  1324. self->module = build_module_from_enumdesc(self);
  1325. }
  1326. return self->module;
  1327. }
  1328. // -----------------------------------------------------------------------------
  1329. // MessageBuilderContext.
  1330. // -----------------------------------------------------------------------------
  1331. DEFINE_CLASS(MessageBuilderContext,
  1332. "Google::Protobuf::Internal::MessageBuilderContext");
  1333. void MessageBuilderContext_mark(void* _self) {
  1334. MessageBuilderContext* self = _self;
  1335. rb_gc_mark(self->descriptor);
  1336. rb_gc_mark(self->builder);
  1337. }
  1338. void MessageBuilderContext_free(void* _self) {
  1339. MessageBuilderContext* self = _self;
  1340. xfree(self);
  1341. }
  1342. VALUE MessageBuilderContext_alloc(VALUE klass) {
  1343. MessageBuilderContext* self = ALLOC(MessageBuilderContext);
  1344. VALUE ret = TypedData_Wrap_Struct(
  1345. klass, &_MessageBuilderContext_type, self);
  1346. self->descriptor = Qnil;
  1347. self->builder = Qnil;
  1348. return ret;
  1349. }
  1350. void MessageBuilderContext_register(VALUE module) {
  1351. VALUE klass = rb_define_class_under(
  1352. module, "MessageBuilderContext", rb_cObject);
  1353. rb_define_alloc_func(klass, MessageBuilderContext_alloc);
  1354. rb_define_method(klass, "initialize",
  1355. MessageBuilderContext_initialize, 2);
  1356. rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
  1357. rb_define_method(klass, "required", MessageBuilderContext_required, -1);
  1358. rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
  1359. rb_define_method(klass, "map", MessageBuilderContext_map, -1);
  1360. rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
  1361. rb_gc_register_address(&cMessageBuilderContext);
  1362. cMessageBuilderContext = klass;
  1363. }
  1364. /*
  1365. * call-seq:
  1366. * MessageBuilderContext.new(desc, builder) => context
  1367. *
  1368. * Create a new message builder context around the given message descriptor and
  1369. * builder context. This class is intended to serve as a DSL context to be used
  1370. * with #instance_eval.
  1371. */
  1372. VALUE MessageBuilderContext_initialize(VALUE _self,
  1373. VALUE msgdef,
  1374. VALUE builder) {
  1375. DEFINE_SELF(MessageBuilderContext, self, _self);
  1376. self->descriptor = msgdef;
  1377. self->builder = builder;
  1378. return Qnil;
  1379. }
  1380. static VALUE msgdef_add_field(VALUE msgdef_rb,
  1381. const char* label, VALUE name,
  1382. VALUE type, VALUE number,
  1383. VALUE type_class,
  1384. VALUE options) {
  1385. VALUE fielddef_rb = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1386. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1387. rb_funcall(fielddef_rb, rb_intern("label="), 1, ID2SYM(rb_intern(label)));
  1388. rb_funcall(fielddef_rb, rb_intern("name="), 1, name_str);
  1389. rb_funcall(fielddef_rb, rb_intern("type="), 1, type);
  1390. rb_funcall(fielddef_rb, rb_intern("number="), 1, number);
  1391. if (type_class != Qnil) {
  1392. Check_Type(type_class, T_STRING);
  1393. // Make it an absolute type name by prepending a dot.
  1394. type_class = rb_str_append(rb_str_new2("."), type_class);
  1395. rb_funcall(fielddef_rb, rb_intern("submsg_name="), 1, type_class);
  1396. }
  1397. if (options != Qnil) {
  1398. Check_Type(options, T_HASH);
  1399. if (rb_funcall(options, rb_intern("key?"), 1,
  1400. ID2SYM(rb_intern("default"))) == Qtrue) {
  1401. Descriptor* msgdef = ruby_to_Descriptor(msgdef_rb);
  1402. if (upb_msgdef_syntax((upb_msgdef*)msgdef->msgdef) == UPB_SYNTAX_PROTO3) {
  1403. rb_raise(rb_eArgError, "Cannot set :default when using proto3 syntax.");
  1404. }
  1405. FieldDescriptor* fielddef = ruby_to_FieldDescriptor(fielddef_rb);
  1406. if (!upb_fielddef_haspresence((upb_fielddef*)fielddef->fielddef) ||
  1407. upb_fielddef_issubmsg((upb_fielddef*)fielddef->fielddef)) {
  1408. rb_raise(rb_eArgError, "Cannot set :default on this kind of field.");
  1409. }
  1410. rb_funcall(fielddef_rb, rb_intern("default="), 1,
  1411. rb_hash_lookup(options, ID2SYM(rb_intern("default"))));
  1412. }
  1413. }
  1414. rb_funcall(msgdef_rb, rb_intern("add_field"), 1, fielddef_rb);
  1415. return fielddef_rb;
  1416. }
  1417. /*
  1418. * call-seq:
  1419. * MessageBuilderContext.optional(name, type, number, type_class = nil,
  1420. * options = nil)
  1421. *
  1422. * Defines a new optional field on this message type with the given type, tag
  1423. * number, and type class (for message and enum fields). The type must be a Ruby
  1424. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1425. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1426. */
  1427. VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1428. DEFINE_SELF(MessageBuilderContext, self, _self);
  1429. VALUE name, type, number;
  1430. VALUE type_class, options = Qnil;
  1431. rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
  1432. // Allow passing (name, type, number, options) or
  1433. // (name, type, number, type_class, options)
  1434. if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
  1435. options = type_class;
  1436. type_class = Qnil;
  1437. }
  1438. return msgdef_add_field(self->descriptor, "optional",
  1439. name, type, number, type_class, options);
  1440. }
  1441. /*
  1442. * call-seq:
  1443. * MessageBuilderContext.required(name, type, number, type_class = nil,
  1444. * options = nil)
  1445. *
  1446. * Defines a new required field on this message type with the given type, tag
  1447. * number, and type class (for message and enum fields). The type must be a Ruby
  1448. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1449. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1450. *
  1451. * Proto3 does not have required fields, but this method exists for
  1452. * completeness. Any attempt to add a message type with required fields to a
  1453. * pool will currently result in an error.
  1454. */
  1455. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
  1456. DEFINE_SELF(MessageBuilderContext, self, _self);
  1457. VALUE name, type, number;
  1458. VALUE type_class, options = Qnil;
  1459. rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
  1460. // Allow passing (name, type, number, options) or
  1461. // (name, type, number, type_class, options)
  1462. if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
  1463. options = type_class;
  1464. type_class = Qnil;
  1465. }
  1466. return msgdef_add_field(self->descriptor, "required",
  1467. name, type, number, type_class, options);
  1468. }
  1469. /*
  1470. * call-seq:
  1471. * MessageBuilderContext.repeated(name, type, number, type_class = nil)
  1472. *
  1473. * Defines a new repeated field on this message type with the given type, tag
  1474. * number, and type class (for message and enum fields). The type must be a Ruby
  1475. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1476. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1477. */
  1478. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
  1479. DEFINE_SELF(MessageBuilderContext, self, _self);
  1480. VALUE name, type, number, type_class;
  1481. if (argc < 3) {
  1482. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1483. }
  1484. name = argv[0];
  1485. type = argv[1];
  1486. number = argv[2];
  1487. type_class = (argc > 3) ? argv[3] : Qnil;
  1488. return msgdef_add_field(self->descriptor, "repeated",
  1489. name, type, number, type_class, Qnil);
  1490. }
  1491. /*
  1492. * call-seq:
  1493. * MessageBuilderContext.map(name, key_type, value_type, number,
  1494. * value_type_class = nil)
  1495. *
  1496. * Defines a new map field on this message type with the given key and value
  1497. * types, tag number, and type class (for message and enum value types). The key
  1498. * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
  1499. * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
  1500. * type_class must be a string, if present (as accepted by
  1501. * FieldDescriptor#submsg_name=).
  1502. */
  1503. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
  1504. DEFINE_SELF(MessageBuilderContext, self, _self);
  1505. VALUE name, key_type, value_type, number, type_class;
  1506. VALUE mapentry_desc, mapentry_desc_name;
  1507. if (argc < 4) {
  1508. rb_raise(rb_eArgError, "Expected at least 4 arguments.");
  1509. }
  1510. name = argv[0];
  1511. key_type = argv[1];
  1512. value_type = argv[2];
  1513. number = argv[3];
  1514. type_class = (argc > 4) ? argv[4] : Qnil;
  1515. // Validate the key type. We can't accept enums, messages, or floats/doubles
  1516. // as map keys. (We exclude these explicitly, and the field-descriptor setter
  1517. // below then ensures that the type is one of the remaining valid options.)
  1518. if (SYM2ID(key_type) == rb_intern("float") ||
  1519. SYM2ID(key_type) == rb_intern("double") ||
  1520. SYM2ID(key_type) == rb_intern("enum") ||
  1521. SYM2ID(key_type) == rb_intern("message")) {
  1522. rb_raise(rb_eArgError,
  1523. "Cannot add a map field with a float, double, enum, or message "
  1524. "type.");
  1525. }
  1526. Descriptor* descriptor = ruby_to_Descriptor(self->descriptor);
  1527. if (upb_msgdef_syntax(descriptor->msgdef) == UPB_SYNTAX_PROTO2) {
  1528. rb_raise(rb_eArgError,
  1529. "Cannot add a native map field using proto2 syntax.");
  1530. }
  1531. // Create a new message descriptor for the map entry message, and create a
  1532. // repeated submessage field here with that type.
  1533. VALUE file_descriptor_rb =
  1534. rb_funcall(self->descriptor, rb_intern("file_descriptor"), 0);
  1535. mapentry_desc = rb_class_new_instance(1, &file_descriptor_rb, cDescriptor);
  1536. mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
  1537. mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
  1538. mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
  1539. rb_id2name(SYM2ID(name)));
  1540. Descriptor_name_set(mapentry_desc, mapentry_desc_name);
  1541. {
  1542. // The 'mapentry' attribute has no Ruby setter because we do not want the
  1543. // user attempting to DIY the setup below; we want to ensure that the fields
  1544. // are correct. So we reach into the msgdef here to set the bit manually.
  1545. Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
  1546. upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
  1547. }
  1548. {
  1549. // optional <type> key = 1;
  1550. VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1551. FieldDescriptor_name_set(key_field, rb_str_new2("key"));
  1552. FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
  1553. FieldDescriptor_number_set(key_field, INT2NUM(1));
  1554. FieldDescriptor_type_set(key_field, key_type);
  1555. Descriptor_add_field(mapentry_desc, key_field);
  1556. }
  1557. {
  1558. // optional <type> value = 2;
  1559. VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1560. FieldDescriptor_name_set(value_field, rb_str_new2("value"));
  1561. FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
  1562. FieldDescriptor_number_set(value_field, INT2NUM(2));
  1563. FieldDescriptor_type_set(value_field, value_type);
  1564. if (type_class != Qnil) {
  1565. VALUE submsg_name = rb_str_new2("."); // prepend '.' to make absolute.
  1566. submsg_name = rb_str_append(submsg_name, type_class);
  1567. FieldDescriptor_submsg_name_set(value_field, submsg_name);
  1568. }
  1569. Descriptor_add_field(mapentry_desc, value_field);
  1570. }
  1571. {
  1572. // Add the map-entry message type to the current builder, and use the type
  1573. // to create the map field itself.
  1574. Builder* builder = ruby_to_Builder(self->builder);
  1575. rb_ary_push(builder->pending_list, mapentry_desc);
  1576. }
  1577. {
  1578. VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
  1579. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1580. VALUE submsg_name;
  1581. FieldDescriptor_name_set(map_field, name_str);
  1582. FieldDescriptor_number_set(map_field, number);
  1583. FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
  1584. FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
  1585. submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
  1586. submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
  1587. FieldDescriptor_submsg_name_set(map_field, submsg_name);
  1588. Descriptor_add_field(self->descriptor, map_field);
  1589. }
  1590. return Qnil;
  1591. }
  1592. /*
  1593. * call-seq:
  1594. * MessageBuilderContext.oneof(name, &block) => nil
  1595. *
  1596. * Creates a new OneofDescriptor with the given name, creates a
  1597. * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
  1598. * block in the context of that OneofBuilderContext with #instance_eval, and
  1599. * then adds the oneof to the message.
  1600. *
  1601. * This is the recommended, idiomatic way to build oneof definitions.
  1602. */
  1603. VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
  1604. DEFINE_SELF(MessageBuilderContext, self, _self);
  1605. VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
  1606. VALUE args[2] = { oneofdef, self->builder };
  1607. VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
  1608. VALUE block = rb_block_proc();
  1609. VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
  1610. rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
  1611. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1612. Descriptor_add_oneof(self->descriptor, oneofdef);
  1613. return Qnil;
  1614. }
  1615. // -----------------------------------------------------------------------------
  1616. // OneofBuilderContext.
  1617. // -----------------------------------------------------------------------------
  1618. DEFINE_CLASS(OneofBuilderContext,
  1619. "Google::Protobuf::Internal::OneofBuilderContext");
  1620. void OneofBuilderContext_mark(void* _self) {
  1621. OneofBuilderContext* self = _self;
  1622. rb_gc_mark(self->descriptor);
  1623. rb_gc_mark(self->builder);
  1624. }
  1625. void OneofBuilderContext_free(void* _self) {
  1626. OneofBuilderContext* self = _self;
  1627. xfree(self);
  1628. }
  1629. VALUE OneofBuilderContext_alloc(VALUE klass) {
  1630. OneofBuilderContext* self = ALLOC(OneofBuilderContext);
  1631. VALUE ret = TypedData_Wrap_Struct(
  1632. klass, &_OneofBuilderContext_type, self);
  1633. self->descriptor = Qnil;
  1634. self->builder = Qnil;
  1635. return ret;
  1636. }
  1637. void OneofBuilderContext_register(VALUE module) {
  1638. VALUE klass = rb_define_class_under(
  1639. module, "OneofBuilderContext", rb_cObject);
  1640. rb_define_alloc_func(klass, OneofBuilderContext_alloc);
  1641. rb_define_method(klass, "initialize",
  1642. OneofBuilderContext_initialize, 2);
  1643. rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
  1644. rb_gc_register_address(&cOneofBuilderContext);
  1645. cOneofBuilderContext = klass;
  1646. }
  1647. /*
  1648. * call-seq:
  1649. * OneofBuilderContext.new(desc, builder) => context
  1650. *
  1651. * Create a new oneof builder context around the given oneof descriptor and
  1652. * builder context. This class is intended to serve as a DSL context to be used
  1653. * with #instance_eval.
  1654. */
  1655. VALUE OneofBuilderContext_initialize(VALUE _self,
  1656. VALUE oneofdef,
  1657. VALUE builder) {
  1658. DEFINE_SELF(OneofBuilderContext, self, _self);
  1659. self->descriptor = oneofdef;
  1660. self->builder = builder;
  1661. return Qnil;
  1662. }
  1663. /*
  1664. * call-seq:
  1665. * OneofBuilderContext.optional(name, type, number, type_class = nil,
  1666. * default_value = nil)
  1667. *
  1668. * Defines a new optional field in this oneof with the given type, tag number,
  1669. * and type class (for message and enum fields). The type must be a Ruby symbol
  1670. * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
  1671. * if present (as accepted by FieldDescriptor#submsg_name=).
  1672. */
  1673. VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1674. DEFINE_SELF(OneofBuilderContext, self, _self);
  1675. VALUE name, type, number;
  1676. VALUE type_class, options = Qnil;
  1677. rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
  1678. return msgdef_add_field(self->descriptor, "optional",
  1679. name, type, number, type_class, options);
  1680. }
  1681. // -----------------------------------------------------------------------------
  1682. // EnumBuilderContext.
  1683. // -----------------------------------------------------------------------------
  1684. DEFINE_CLASS(EnumBuilderContext,
  1685. "Google::Protobuf::Internal::EnumBuilderContext");
  1686. void EnumBuilderContext_mark(void* _self) {
  1687. EnumBuilderContext* self = _self;
  1688. rb_gc_mark(self->enumdesc);
  1689. }
  1690. void EnumBuilderContext_free(void* _self) {
  1691. EnumBuilderContext* self = _self;
  1692. xfree(self);
  1693. }
  1694. VALUE EnumBuilderContext_alloc(VALUE klass) {
  1695. EnumBuilderContext* self = ALLOC(EnumBuilderContext);
  1696. VALUE ret = TypedData_Wrap_Struct(
  1697. klass, &_EnumBuilderContext_type, self);
  1698. self->enumdesc = Qnil;
  1699. return ret;
  1700. }
  1701. void EnumBuilderContext_register(VALUE module) {
  1702. VALUE klass = rb_define_class_under(
  1703. module, "EnumBuilderContext", rb_cObject);
  1704. rb_define_alloc_func(klass, EnumBuilderContext_alloc);
  1705. rb_define_method(klass, "initialize",
  1706. EnumBuilderContext_initialize, 1);
  1707. rb_define_method(klass, "value", EnumBuilderContext_value, 2);
  1708. rb_gc_register_address(&cEnumBuilderContext);
  1709. cEnumBuilderContext = klass;
  1710. }
  1711. /*
  1712. * call-seq:
  1713. * EnumBuilderContext.new(enumdesc) => context
  1714. *
  1715. * Create a new builder context around the given enum descriptor. This class is
  1716. * intended to serve as a DSL context to be used with #instance_eval.
  1717. */
  1718. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
  1719. DEFINE_SELF(EnumBuilderContext, self, _self);
  1720. self->enumdesc = enumdef;
  1721. return Qnil;
  1722. }
  1723. static VALUE enumdef_add_value(VALUE enumdef,
  1724. VALUE name, VALUE number) {
  1725. rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
  1726. return Qnil;
  1727. }
  1728. /*
  1729. * call-seq:
  1730. * EnumBuilder.add_value(name, number)
  1731. *
  1732. * Adds the given name => number mapping to the enum type. Name must be a Ruby
  1733. * symbol.
  1734. */
  1735. VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
  1736. DEFINE_SELF(EnumBuilderContext, self, _self);
  1737. return enumdef_add_value(self->enumdesc, name, number);
  1738. }
  1739. // -----------------------------------------------------------------------------
  1740. // FileBuilderContext.
  1741. // -----------------------------------------------------------------------------
  1742. DEFINE_CLASS(FileBuilderContext,
  1743. "Google::Protobuf::Internal::FileBuilderContext");
  1744. void FileBuilderContext_mark(void* _self) {
  1745. FileBuilderContext* self = _self;
  1746. rb_gc_mark(self->pending_list);
  1747. rb_gc_mark(self->file_descriptor);
  1748. rb_gc_mark(self->builder);
  1749. }
  1750. void FileBuilderContext_free(void* _self) {
  1751. FileBuilderContext* self = _self;
  1752. xfree(self);
  1753. }
  1754. VALUE FileBuilderContext_alloc(VALUE klass) {
  1755. FileBuilderContext* self = ALLOC(FileBuilderContext);
  1756. VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self);
  1757. self->pending_list = Qnil;
  1758. self->file_descriptor = Qnil;
  1759. self->builder = Qnil;
  1760. return ret;
  1761. }
  1762. void FileBuilderContext_register(VALUE module) {
  1763. VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
  1764. rb_define_alloc_func(klass, FileBuilderContext_alloc);
  1765. rb_define_method(klass, "initialize", FileBuilderContext_initialize, 2);
  1766. rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
  1767. rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
  1768. rb_gc_register_address(&cFileBuilderContext);
  1769. cFileBuilderContext = klass;
  1770. }
  1771. /*
  1772. * call-seq:
  1773. * FileBuilderContext.new(file_descriptor, builder) => context
  1774. *
  1775. * Create a new file builder context for the given file descriptor and
  1776. * builder context. This class is intended to serve as a DSL context to be used
  1777. * with #instance_eval.
  1778. */
  1779. VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
  1780. VALUE builder) {
  1781. DEFINE_SELF(FileBuilderContext, self, _self);
  1782. self->pending_list = rb_ary_new();
  1783. self->file_descriptor = file_descriptor;
  1784. self->builder = builder;
  1785. return Qnil;
  1786. }
  1787. /*
  1788. * call-seq:
  1789. * FileBuilderContext.add_message(name, &block)
  1790. *
  1791. * Creates a new, empty descriptor with the given name, and invokes the block in
  1792. * the context of a MessageBuilderContext on that descriptor. The block can then
  1793. * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
  1794. * methods to define the message fields.
  1795. *
  1796. * This is the recommended, idiomatic way to build message definitions.
  1797. */
  1798. VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
  1799. DEFINE_SELF(FileBuilderContext, self, _self);
  1800. VALUE msgdef = rb_class_new_instance(1, &self->file_descriptor, cDescriptor);
  1801. VALUE args[2] = { msgdef, self->builder };
  1802. VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
  1803. VALUE block = rb_block_proc();
  1804. rb_funcall(msgdef, rb_intern("name="), 1, name);
  1805. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1806. rb_ary_push(self->pending_list, msgdef);
  1807. return Qnil;
  1808. }
  1809. /*
  1810. * call-seq:
  1811. * FileBuilderContext.add_enum(name, &block)
  1812. *
  1813. * Creates a new, empty enum descriptor with the given name, and invokes the
  1814. * block in the context of an EnumBuilderContext on that descriptor. The block
  1815. * can then call EnumBuilderContext#add_value to define the enum values.
  1816. *
  1817. * This is the recommended, idiomatic way to build enum definitions.
  1818. */
  1819. VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
  1820. DEFINE_SELF(FileBuilderContext, self, _self);
  1821. VALUE enumdef =
  1822. rb_class_new_instance(1, &self->file_descriptor, cEnumDescriptor);
  1823. VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
  1824. VALUE block = rb_block_proc();
  1825. rb_funcall(enumdef, rb_intern("name="), 1, name);
  1826. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1827. rb_ary_push(self->pending_list, enumdef);
  1828. return Qnil;
  1829. }
  1830. VALUE FileBuilderContext_pending_descriptors(VALUE _self) {
  1831. DEFINE_SELF(FileBuilderContext, self, _self);
  1832. return self->pending_list;
  1833. }
  1834. // -----------------------------------------------------------------------------
  1835. // Builder.
  1836. // -----------------------------------------------------------------------------
  1837. DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
  1838. void Builder_mark(void* _self) {
  1839. Builder* self = _self;
  1840. rb_gc_mark(self->pending_list);
  1841. rb_gc_mark(self->default_file_descriptor);
  1842. }
  1843. void Builder_free(void* _self) {
  1844. Builder* self = _self;
  1845. xfree(self->defs);
  1846. xfree(self);
  1847. }
  1848. /*
  1849. * call-seq:
  1850. * Builder.new => builder
  1851. *
  1852. * Creates a new Builder. A Builder can accumulate a set of new message and enum
  1853. * descriptors and atomically register them into a pool in a way that allows for
  1854. * (co)recursive type references.
  1855. */
  1856. VALUE Builder_alloc(VALUE klass) {
  1857. Builder* self = ALLOC(Builder);
  1858. VALUE ret = TypedData_Wrap_Struct(
  1859. klass, &_Builder_type, self);
  1860. self->pending_list = Qnil;
  1861. self->defs = NULL;
  1862. self->default_file_descriptor = Qnil;
  1863. return ret;
  1864. }
  1865. void Builder_register(VALUE module) {
  1866. VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
  1867. rb_define_alloc_func(klass, Builder_alloc);
  1868. rb_define_method(klass, "initialize", Builder_initialize, 0);
  1869. rb_define_method(klass, "add_file", Builder_add_file, -1);
  1870. rb_define_method(klass, "add_message", Builder_add_message, 1);
  1871. rb_define_method(klass, "add_enum", Builder_add_enum, 1);
  1872. rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
  1873. rb_gc_register_address(&cBuilder);
  1874. cBuilder = klass;
  1875. }
  1876. /*
  1877. * call-seq:
  1878. * Builder.new
  1879. *
  1880. * Initializes a new builder.
  1881. */
  1882. VALUE Builder_initialize(VALUE _self) {
  1883. DEFINE_SELF(Builder, self, _self);
  1884. self->pending_list = rb_ary_new();
  1885. VALUE file_name = Qnil;
  1886. self->default_file_descriptor =
  1887. rb_class_new_instance(1, &file_name, cFileDescriptor);
  1888. return Qnil;
  1889. }
  1890. /*
  1891. * call-seq:
  1892. * Builder.add_file(name, options = nil, &block)
  1893. *
  1894. * Creates a new, file descriptor with the given name and options and invokes
  1895. * the block in the context of a FileBuilderContext on that descriptor. The
  1896. * block can then call FileBuilderContext#add_message or
  1897. * FileBuilderContext#add_enum to define new messages or enums, respectively.
  1898. *
  1899. * This is the recommended, idiomatic way to build file descriptors.
  1900. */
  1901. VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
  1902. DEFINE_SELF(Builder, self, _self);
  1903. VALUE file_descriptor = rb_class_new_instance(argc, argv, cFileDescriptor);
  1904. VALUE args[2] = { file_descriptor, _self };
  1905. VALUE ctx = rb_class_new_instance(2, args, cFileBuilderContext);
  1906. VALUE block = rb_block_proc();
  1907. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1908. rb_ary_concat(self->pending_list,
  1909. FileBuilderContext_pending_descriptors(ctx));
  1910. return Qnil;
  1911. }
  1912. /*
  1913. * call-seq:
  1914. * Builder.add_message(name, &block)
  1915. *
  1916. * Old and deprecated way to create a new descriptor.
  1917. * See FileBuilderContext.add_message for the recommended way.
  1918. *
  1919. * Exists for backwards compatibility to allow building descriptor pool for
  1920. * files generated by protoc which don't add messages within "add_file" block.
  1921. * Descriptors created this way get assigned to a default empty FileDescriptor.
  1922. */
  1923. VALUE Builder_add_message(VALUE _self, VALUE name) {
  1924. DEFINE_SELF(Builder, self, _self);
  1925. VALUE msgdef =
  1926. rb_class_new_instance(1, &self->default_file_descriptor, cDescriptor);
  1927. VALUE args[2] = { msgdef, _self };
  1928. VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
  1929. VALUE block = rb_block_proc();
  1930. rb_funcall(msgdef, rb_intern("name="), 1, name);
  1931. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1932. rb_ary_push(self->pending_list, msgdef);
  1933. return Qnil;
  1934. }
  1935. /*
  1936. * call-seq:
  1937. * Builder.add_enum(name, &block)
  1938. *
  1939. * Old and deprecated way to create a new enum descriptor.
  1940. * See FileBuilderContext.add_enum for the recommended way.
  1941. *
  1942. * Exists for backwards compatibility to allow building descriptor pool for
  1943. * files generated by protoc which don't add enums within "add_file" block.
  1944. * Enum descriptors created this way get assigned to a default empty
  1945. * FileDescriptor.
  1946. */
  1947. VALUE Builder_add_enum(VALUE _self, VALUE name) {
  1948. DEFINE_SELF(Builder, self, _self);
  1949. VALUE enumdef =
  1950. rb_class_new_instance(1, &self->default_file_descriptor, cEnumDescriptor);
  1951. VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
  1952. VALUE block = rb_block_proc();
  1953. rb_funcall(enumdef, rb_intern("name="), 1, name);
  1954. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1955. rb_ary_push(self->pending_list, enumdef);
  1956. return Qnil;
  1957. }
  1958. static void proto3_validate_msgdef(const upb_msgdef* msgdef) {
  1959. // Verify that no required fields exist. proto3 does not support these.
  1960. upb_msg_field_iter it;
  1961. for (upb_msg_field_begin(&it, msgdef);
  1962. !upb_msg_field_done(&it);
  1963. upb_msg_field_next(&it)) {
  1964. const upb_fielddef* field = upb_msg_iter_field(&it);
  1965. if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
  1966. rb_raise(cTypeError, "Required fields are unsupported in proto3.");
  1967. }
  1968. }
  1969. }
  1970. static void proto3_validate_enumdef(const upb_enumdef* enumdef) {
  1971. // Verify that an entry exists with integer value 0. (This is the default
  1972. // value.)
  1973. const char* lookup = upb_enumdef_iton(enumdef, 0);
  1974. if (lookup == NULL) {
  1975. rb_raise(cTypeError,
  1976. "Enum definition does not contain a value for '0'.");
  1977. }
  1978. }
  1979. /*
  1980. * call-seq:
  1981. * Builder.finalize_to_pool(pool)
  1982. *
  1983. * Adds all accumulated message and enum descriptors created in this builder
  1984. * context to the given pool. The operation occurs atomically, and all
  1985. * descriptors can refer to each other (including in cycles). This is the only
  1986. * way to build (co)recursive message definitions.
  1987. *
  1988. * This method is usually called automatically by DescriptorPool#build after it
  1989. * invokes the given user block in the context of the builder. The user should
  1990. * not normally need to call this manually because a Builder is not normally
  1991. * created manually.
  1992. */
  1993. VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
  1994. DEFINE_SELF(Builder, self, _self);
  1995. DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
  1996. REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
  1997. for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
  1998. VALUE def_rb = rb_ary_entry(self->pending_list, i);
  1999. if (CLASS_OF(def_rb) == cDescriptor) {
  2000. self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef;
  2001. if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) {
  2002. proto3_validate_msgdef((const upb_msgdef*)self->defs[i]);
  2003. }
  2004. } else if (CLASS_OF(def_rb) == cEnumDescriptor) {
  2005. self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
  2006. if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) {
  2007. proto3_validate_enumdef((const upb_enumdef*)self->defs[i]);
  2008. }
  2009. }
  2010. }
  2011. CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
  2012. RARRAY_LEN(self->pending_list), NULL, &status),
  2013. "Unable to add defs to DescriptorPool");
  2014. for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
  2015. VALUE def_rb = rb_ary_entry(self->pending_list, i);
  2016. add_def_obj(self->defs[i], def_rb);
  2017. }
  2018. self->pending_list = rb_ary_new();
  2019. return Qnil;
  2020. }