defs.c 58 KB

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