defs.c 58 KB

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