defs.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275
  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 <ctype.h>
  31. #include <errno.h>
  32. #include "protobuf.h"
  33. // -----------------------------------------------------------------------------
  34. // Common utilities.
  35. // -----------------------------------------------------------------------------
  36. static const char* get_str(VALUE str) {
  37. Check_Type(str, T_STRING);
  38. return RSTRING_PTR(str);
  39. }
  40. static VALUE rb_str_maybe_null(const char* s) {
  41. if (s == NULL) {
  42. s = "";
  43. }
  44. return rb_str_new2(s);
  45. }
  46. static void rewrite_enum_default(const upb_symtab* symtab,
  47. google_protobuf_FileDescriptorProto* file,
  48. google_protobuf_FieldDescriptorProto* field) {
  49. upb_strview defaultval;
  50. const char *type_name_str;
  51. char *end;
  52. long val;
  53. const upb_enumdef *e;
  54. upb_strview type_name;
  55. /* Look for TYPE_ENUM fields that have a default. */
  56. if (google_protobuf_FieldDescriptorProto_type(field) !=
  57. google_protobuf_FieldDescriptorProto_TYPE_ENUM ||
  58. !google_protobuf_FieldDescriptorProto_has_default_value(field) ||
  59. !google_protobuf_FieldDescriptorProto_has_type_name(field)) {
  60. return;
  61. }
  62. defaultval = google_protobuf_FieldDescriptorProto_default_value(field);
  63. type_name = google_protobuf_FieldDescriptorProto_type_name(field);
  64. if (defaultval.size == 0 || !isdigit(defaultval.data[0])) {
  65. return;
  66. }
  67. if (type_name.size == 0 || type_name.data[0] != '.') {
  68. return;
  69. }
  70. type_name_str = type_name.data + 1;
  71. errno = 0;
  72. val = strtol(defaultval.data, &end, 10);
  73. if (errno != 0 || *end != 0 || val < INT32_MIN || val > INT32_MAX) {
  74. return;
  75. }
  76. /* Now find the corresponding enum definition. */
  77. e = upb_symtab_lookupenum(symtab, type_name_str);
  78. if (e) {
  79. /* Look in previously loaded files. */
  80. const char *label = upb_enumdef_iton(e, val);
  81. if (!label) {
  82. return;
  83. }
  84. google_protobuf_FieldDescriptorProto_set_default_value(
  85. field, upb_strview_makez(label));
  86. } else {
  87. /* Look in enums defined in this file. */
  88. const google_protobuf_EnumDescriptorProto* matching_enum = NULL;
  89. size_t i, n;
  90. const google_protobuf_EnumDescriptorProto* const* enums =
  91. google_protobuf_FileDescriptorProto_enum_type(file, &n);
  92. const google_protobuf_EnumValueDescriptorProto* const* values;
  93. for (i = 0; i < n; i++) {
  94. if (upb_strview_eql(google_protobuf_EnumDescriptorProto_name(enums[i]),
  95. upb_strview_makez(type_name_str))) {
  96. matching_enum = enums[i];
  97. break;
  98. }
  99. }
  100. if (!matching_enum) {
  101. return;
  102. }
  103. values = google_protobuf_EnumDescriptorProto_value(matching_enum, &n);
  104. for (i = 0; i < n; i++) {
  105. if (google_protobuf_EnumValueDescriptorProto_number(values[i]) == val) {
  106. google_protobuf_FieldDescriptorProto_set_default_value(
  107. field, google_protobuf_EnumValueDescriptorProto_name(values[i]));
  108. return;
  109. }
  110. }
  111. /* We failed to find an enum default. But we'll just leave the enum
  112. * untouched and let the normal def-building code catch it. */
  113. }
  114. }
  115. /* Historically we allowed enum defaults to be specified as a number. In
  116. * retrospect this was a mistake as descriptors require defaults to be
  117. * specified as a label. This can make a difference if multiple labels have the
  118. * same number.
  119. *
  120. * Here we do a pass over all enum defaults and rewrite numeric defaults by
  121. * looking up their labels. This is compilcated by the fact that the enum
  122. * definition can live in either the symtab or the file_proto.
  123. * */
  124. static void rewrite_enum_defaults(
  125. const upb_symtab* symtab, google_protobuf_FileDescriptorProto* file_proto) {
  126. size_t i, n;
  127. google_protobuf_DescriptorProto** msgs =
  128. google_protobuf_FileDescriptorProto_mutable_message_type(file_proto, &n);
  129. for (i = 0; i < n; i++) {
  130. size_t j, m;
  131. google_protobuf_FieldDescriptorProto** fields =
  132. google_protobuf_DescriptorProto_mutable_field(msgs[i], &m);
  133. for (j = 0; j < m; j++) {
  134. rewrite_enum_default(symtab, file_proto, fields[j]);
  135. }
  136. }
  137. }
  138. static void remove_path(upb_strview *name) {
  139. const char* last = strrchr(name->data, '.');
  140. if (last) {
  141. size_t remove = last - name->data + 1;
  142. name->data += remove;
  143. name->size -= remove;
  144. }
  145. }
  146. static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
  147. google_protobuf_DescriptorProto* const* msgs,
  148. google_protobuf_EnumDescriptorProto* const* enums,
  149. upb_arena *arena) {
  150. VALUE submsgs = rb_hash_aref(msg_ent, ID2SYM(rb_intern("msgs")));
  151. VALUE enum_pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("enums")));
  152. int submsg_count;
  153. int enum_count;
  154. int i;
  155. google_protobuf_DescriptorProto** msg_msgs;
  156. google_protobuf_EnumDescriptorProto** msg_enums;
  157. Check_Type(submsgs, T_ARRAY);
  158. Check_Type(enum_pos, T_ARRAY);
  159. submsg_count = RARRAY_LEN(submsgs);
  160. enum_count = RARRAY_LEN(enum_pos);
  161. msg_msgs = google_protobuf_DescriptorProto_resize_nested_type(
  162. msg, submsg_count, arena);
  163. msg_enums =
  164. google_protobuf_DescriptorProto_resize_enum_type(msg, enum_count, arena);
  165. for (i = 0; i < submsg_count; i++) {
  166. VALUE submsg_ent = RARRAY_PTR(submsgs)[i];
  167. VALUE pos = rb_hash_aref(submsg_ent, ID2SYM(rb_intern("pos")));
  168. upb_strview name;
  169. msg_msgs[i] = msgs[NUM2INT(pos)];
  170. name = google_protobuf_DescriptorProto_name(msg_msgs[i]);
  171. remove_path(&name);
  172. google_protobuf_DescriptorProto_set_name(msg_msgs[i], name);
  173. rewrite_nesting(submsg_ent, msg_msgs[i], msgs, enums, arena);
  174. }
  175. for (i = 0; i < enum_count; i++) {
  176. VALUE pos = RARRAY_PTR(enum_pos)[i];
  177. msg_enums[i] = enums[NUM2INT(pos)];
  178. }
  179. }
  180. /* We have to do some relatively complicated logic here for backward
  181. * compatibility.
  182. *
  183. * In descriptor.proto, messages are nested inside other messages if that is
  184. * what the original .proto file looks like. For example, suppose we have this
  185. * foo.proto:
  186. *
  187. * package foo;
  188. * message Bar {
  189. * message Baz {}
  190. * }
  191. *
  192. * The descriptor for this must look like this:
  193. *
  194. * file {
  195. * name: "test.proto"
  196. * package: "foo"
  197. * message_type {
  198. * name: "Bar"
  199. * nested_type {
  200. * name: "Baz"
  201. * }
  202. * }
  203. * }
  204. *
  205. * However, the Ruby generated code has always generated messages in a flat,
  206. * non-nested way:
  207. *
  208. * Google::Protobuf::DescriptorPool.generated_pool.build do
  209. * add_message "foo.Bar" do
  210. * end
  211. * add_message "foo.Bar.Baz" do
  212. * end
  213. * end
  214. *
  215. * Here we need to do a translation where we turn this generated code into the
  216. * above descriptor. We need to infer that "foo" is the package name, and not
  217. * a message itself.
  218. *
  219. * We delegate to Ruby to compute the transformation, for more concice and
  220. * readable code than we can do in C */
  221. static void rewrite_names(VALUE _file_builder,
  222. google_protobuf_FileDescriptorProto* file_proto) {
  223. FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
  224. upb_arena *arena = file_builder->arena;
  225. // Build params (package, msg_names, enum_names).
  226. VALUE package = Qnil;
  227. VALUE msg_names = rb_ary_new();
  228. VALUE enum_names = rb_ary_new();
  229. size_t msg_count, enum_count, i;
  230. VALUE new_package, nesting, msg_ents, enum_ents;
  231. google_protobuf_DescriptorProto** msgs;
  232. google_protobuf_EnumDescriptorProto** enums;
  233. if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
  234. upb_strview package_str =
  235. google_protobuf_FileDescriptorProto_package(file_proto);
  236. package = rb_str_new(package_str.data, package_str.size);
  237. }
  238. msgs = google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
  239. &msg_count);
  240. for (i = 0; i < msg_count; i++) {
  241. upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
  242. rb_ary_push(msg_names, rb_str_new(name.data, name.size));
  243. }
  244. enums = google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
  245. &enum_count);
  246. for (i = 0; i < enum_count; i++) {
  247. upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
  248. rb_ary_push(enum_names, rb_str_new(name.data, name.size));
  249. }
  250. {
  251. // Call Ruby code to calculate package name and nesting.
  252. VALUE args[3] = { package, msg_names, enum_names };
  253. VALUE internal = rb_eval_string("Google::Protobuf::Internal");
  254. VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
  255. new_package = rb_ary_entry(ret, 0);
  256. nesting = rb_ary_entry(ret, 1);
  257. }
  258. // Rewrite package and names.
  259. if (new_package != Qnil) {
  260. upb_strview new_package_str =
  261. FileBuilderContext_strdup(_file_builder, new_package);
  262. google_protobuf_FileDescriptorProto_set_package(file_proto,
  263. new_package_str);
  264. }
  265. for (i = 0; i < msg_count; i++) {
  266. upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
  267. remove_path(&name);
  268. google_protobuf_DescriptorProto_set_name(msgs[i], name);
  269. }
  270. for (i = 0; i < enum_count; i++) {
  271. upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
  272. remove_path(&name);
  273. google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
  274. }
  275. // Rewrite nesting.
  276. msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
  277. enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
  278. Check_Type(msg_ents, T_ARRAY);
  279. Check_Type(enum_ents, T_ARRAY);
  280. for (i = 0; i < (size_t)RARRAY_LEN(msg_ents); i++) {
  281. VALUE msg_ent = rb_ary_entry(msg_ents, i);
  282. VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
  283. msgs[i] = msgs[NUM2INT(pos)];
  284. rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
  285. }
  286. for (i = 0; i < (size_t)RARRAY_LEN(enum_ents); i++) {
  287. VALUE enum_pos = rb_ary_entry(enum_ents, i);
  288. enums[i] = enums[NUM2INT(enum_pos)];
  289. }
  290. google_protobuf_FileDescriptorProto_resize_message_type(
  291. file_proto, RARRAY_LEN(msg_ents), arena);
  292. google_protobuf_FileDescriptorProto_resize_enum_type(
  293. file_proto, RARRAY_LEN(enum_ents), arena);
  294. }
  295. // -----------------------------------------------------------------------------
  296. // DescriptorPool.
  297. // -----------------------------------------------------------------------------
  298. #define DEFINE_CLASS(name, string_name) \
  299. VALUE c ## name = Qnil; \
  300. const rb_data_type_t _ ## name ## _type = { \
  301. string_name, \
  302. { name ## _mark, name ## _free, NULL }, \
  303. }; \
  304. name* ruby_to_ ## name(VALUE val) { \
  305. name* ret; \
  306. TypedData_Get_Struct(val, name, &_ ## name ## _type, ret); \
  307. return ret; \
  308. } \
  309. #define DEFINE_SELF(type, var, rb_var) \
  310. type* var = ruby_to_ ## type(rb_var)
  311. // Global singleton DescriptorPool. The user is free to create others, but this
  312. // is used by generated code.
  313. VALUE generated_pool = Qnil;
  314. DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
  315. void DescriptorPool_mark(void* _self) {
  316. DescriptorPool* self = _self;
  317. rb_gc_mark(self->def_to_descriptor);
  318. }
  319. void DescriptorPool_free(void* _self) {
  320. DescriptorPool* self = _self;
  321. upb_symtab_free(self->symtab);
  322. upb_handlercache_free(self->fill_handler_cache);
  323. upb_handlercache_free(self->pb_serialize_handler_cache);
  324. upb_handlercache_free(self->json_serialize_handler_cache);
  325. upb_handlercache_free(self->json_serialize_handler_preserve_cache);
  326. upb_pbcodecache_free(self->fill_method_cache);
  327. upb_json_codecache_free(self->json_fill_method_cache);
  328. xfree(self);
  329. }
  330. /*
  331. * call-seq:
  332. * DescriptorPool.new => pool
  333. *
  334. * Creates a new, empty, descriptor pool.
  335. */
  336. VALUE DescriptorPool_alloc(VALUE klass) {
  337. DescriptorPool* self = ALLOC(DescriptorPool);
  338. VALUE ret;
  339. self->def_to_descriptor = Qnil;
  340. ret = TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
  341. self->def_to_descriptor = rb_hash_new();
  342. self->symtab = upb_symtab_new();
  343. self->fill_handler_cache =
  344. upb_handlercache_new(add_handlers_for_message, (void*)ret);
  345. self->pb_serialize_handler_cache = upb_pb_encoder_newcache();
  346. self->json_serialize_handler_cache = upb_json_printer_newcache(false);
  347. self->json_serialize_handler_preserve_cache =
  348. upb_json_printer_newcache(true);
  349. self->fill_method_cache = upb_pbcodecache_new(self->fill_handler_cache);
  350. self->json_fill_method_cache = upb_json_codecache_new();
  351. return ret;
  352. }
  353. void DescriptorPool_register(VALUE module) {
  354. VALUE klass = rb_define_class_under(
  355. module, "DescriptorPool", rb_cObject);
  356. rb_define_alloc_func(klass, DescriptorPool_alloc);
  357. rb_define_method(klass, "build", DescriptorPool_build, -1);
  358. rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
  359. rb_define_singleton_method(klass, "generated_pool",
  360. DescriptorPool_generated_pool, 0);
  361. rb_gc_register_address(&cDescriptorPool);
  362. cDescriptorPool = klass;
  363. rb_gc_register_address(&generated_pool);
  364. generated_pool = rb_class_new_instance(0, NULL, klass);
  365. }
  366. /*
  367. * call-seq:
  368. * DescriptorPool.build(&block)
  369. *
  370. * Invokes the block with a Builder instance as self. All message and enum types
  371. * added within the block are committed to the pool atomically, and may refer
  372. * (co)recursively to each other. The user should call Builder#add_message and
  373. * Builder#add_enum within the block as appropriate. This is the recommended,
  374. * idiomatic way to define new message and enum types.
  375. */
  376. VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
  377. VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
  378. VALUE block = rb_block_proc();
  379. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  380. Builder_build(ctx);
  381. return Qnil;
  382. }
  383. /*
  384. * call-seq:
  385. * DescriptorPool.lookup(name) => descriptor
  386. *
  387. * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
  388. * exists with the given name.
  389. */
  390. VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
  391. DEFINE_SELF(DescriptorPool, self, _self);
  392. const char* name_str = get_str(name);
  393. const upb_msgdef* msgdef;
  394. const upb_enumdef* enumdef;
  395. msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
  396. if (msgdef) {
  397. return get_msgdef_obj(_self, msgdef);
  398. }
  399. enumdef = upb_symtab_lookupenum(self->symtab, name_str);
  400. if (enumdef) {
  401. return get_enumdef_obj(_self, enumdef);
  402. }
  403. return Qnil;
  404. }
  405. /*
  406. * call-seq:
  407. * DescriptorPool.generated_pool => descriptor_pool
  408. *
  409. * Class method that returns the global DescriptorPool. This is a singleton into
  410. * which generated-code message and enum types are registered. The user may also
  411. * register types in this pool for convenience so that they do not have to hold
  412. * a reference to a private pool instance.
  413. */
  414. VALUE DescriptorPool_generated_pool(VALUE _self) {
  415. return generated_pool;
  416. }
  417. // -----------------------------------------------------------------------------
  418. // Descriptor.
  419. // -----------------------------------------------------------------------------
  420. DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
  421. void Descriptor_mark(void* _self) {
  422. Descriptor* self = _self;
  423. rb_gc_mark(self->klass);
  424. rb_gc_mark(self->descriptor_pool);
  425. }
  426. void Descriptor_free(void* _self) {
  427. Descriptor* self = _self;
  428. if (self->layout) {
  429. free_layout(self->layout);
  430. }
  431. xfree(self);
  432. }
  433. /*
  434. * call-seq:
  435. * Descriptor.new => descriptor
  436. *
  437. * Creates a new, empty, message type descriptor. At a minimum, its name must be
  438. * set before it is added to a pool. It cannot be used to create messages until
  439. * it is added to a pool, after which it becomes immutable (as part of a
  440. * finalization process).
  441. */
  442. VALUE Descriptor_alloc(VALUE klass) {
  443. Descriptor* self = ALLOC(Descriptor);
  444. VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
  445. self->msgdef = NULL;
  446. self->klass = Qnil;
  447. self->descriptor_pool = Qnil;
  448. self->layout = NULL;
  449. return ret;
  450. }
  451. void Descriptor_register(VALUE module) {
  452. VALUE klass = rb_define_class_under(
  453. module, "Descriptor", rb_cObject);
  454. rb_define_alloc_func(klass, Descriptor_alloc);
  455. rb_define_method(klass, "initialize", Descriptor_initialize, 3);
  456. rb_define_method(klass, "each", Descriptor_each, 0);
  457. rb_define_method(klass, "lookup", Descriptor_lookup, 1);
  458. rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
  459. rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
  460. rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
  461. rb_define_method(klass, "name", Descriptor_name, 0);
  462. rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
  463. rb_include_module(klass, rb_mEnumerable);
  464. rb_gc_register_address(&cDescriptor);
  465. cDescriptor = klass;
  466. }
  467. /*
  468. * call-seq:
  469. * Descriptor.new(c_only_cookie, ptr) => Descriptor
  470. *
  471. * Creates a descriptor wrapper object. May only be called from C.
  472. */
  473. VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
  474. VALUE descriptor_pool, VALUE ptr) {
  475. DEFINE_SELF(Descriptor, self, _self);
  476. if (cookie != c_only_cookie) {
  477. rb_raise(rb_eRuntimeError,
  478. "Descriptor objects may not be created from Ruby.");
  479. }
  480. self->descriptor_pool = descriptor_pool;
  481. self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
  482. return Qnil;
  483. }
  484. /*
  485. * call-seq:
  486. * Descriptor.file_descriptor
  487. *
  488. * Returns the FileDescriptor object this message belongs to.
  489. */
  490. VALUE Descriptor_file_descriptor(VALUE _self) {
  491. DEFINE_SELF(Descriptor, self, _self);
  492. return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
  493. }
  494. /*
  495. * call-seq:
  496. * Descriptor.name => name
  497. *
  498. * Returns the name of this message type as a fully-qualfied string (e.g.,
  499. * My.Package.MessageType).
  500. */
  501. VALUE Descriptor_name(VALUE _self) {
  502. DEFINE_SELF(Descriptor, self, _self);
  503. return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
  504. }
  505. /*
  506. * call-seq:
  507. * Descriptor.each(&block)
  508. *
  509. * Iterates over fields in this message type, yielding to the block on each one.
  510. */
  511. VALUE Descriptor_each(VALUE _self) {
  512. DEFINE_SELF(Descriptor, self, _self);
  513. upb_msg_field_iter it;
  514. for (upb_msg_field_begin(&it, self->msgdef);
  515. !upb_msg_field_done(&it);
  516. upb_msg_field_next(&it)) {
  517. const upb_fielddef* field = upb_msg_iter_field(&it);
  518. VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
  519. rb_yield(obj);
  520. }
  521. return Qnil;
  522. }
  523. /*
  524. * call-seq:
  525. * Descriptor.lookup(name) => FieldDescriptor
  526. *
  527. * Returns the field descriptor for the field with the given name, if present,
  528. * or nil if none.
  529. */
  530. VALUE Descriptor_lookup(VALUE _self, VALUE name) {
  531. DEFINE_SELF(Descriptor, self, _self);
  532. const char* s = get_str(name);
  533. const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
  534. if (field == NULL) {
  535. return Qnil;
  536. }
  537. return get_fielddef_obj(self->descriptor_pool, field);
  538. }
  539. /*
  540. * call-seq:
  541. * Descriptor.each_oneof(&block) => nil
  542. *
  543. * Invokes the given block for each oneof in this message type, passing the
  544. * corresponding OneofDescriptor.
  545. */
  546. VALUE Descriptor_each_oneof(VALUE _self) {
  547. DEFINE_SELF(Descriptor, self, _self);
  548. upb_msg_oneof_iter it;
  549. for (upb_msg_oneof_begin(&it, self->msgdef);
  550. !upb_msg_oneof_done(&it);
  551. upb_msg_oneof_next(&it)) {
  552. const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
  553. VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
  554. rb_yield(obj);
  555. }
  556. return Qnil;
  557. }
  558. /*
  559. * call-seq:
  560. * Descriptor.lookup_oneof(name) => OneofDescriptor
  561. *
  562. * Returns the oneof descriptor for the oneof with the given name, if present,
  563. * or nil if none.
  564. */
  565. VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
  566. DEFINE_SELF(Descriptor, self, _self);
  567. const char* s = get_str(name);
  568. const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
  569. if (oneof == NULL) {
  570. return Qnil;
  571. }
  572. return get_oneofdef_obj(self->descriptor_pool, oneof);
  573. }
  574. /*
  575. * call-seq:
  576. * Descriptor.msgclass => message_klass
  577. *
  578. * Returns the Ruby class created for this message type.
  579. */
  580. VALUE Descriptor_msgclass(VALUE _self) {
  581. DEFINE_SELF(Descriptor, self, _self);
  582. if (self->klass == Qnil) {
  583. self->klass = build_class_from_descriptor(_self);
  584. }
  585. return self->klass;
  586. }
  587. // -----------------------------------------------------------------------------
  588. // FileDescriptor.
  589. // -----------------------------------------------------------------------------
  590. DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
  591. void FileDescriptor_mark(void* _self) {
  592. FileDescriptor* self = _self;
  593. rb_gc_mark(self->descriptor_pool);
  594. }
  595. void FileDescriptor_free(void* _self) {
  596. xfree(_self);
  597. }
  598. VALUE FileDescriptor_alloc(VALUE klass) {
  599. FileDescriptor* self = ALLOC(FileDescriptor);
  600. VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
  601. self->descriptor_pool = Qnil;
  602. self->filedef = NULL;
  603. return ret;
  604. }
  605. /*
  606. * call-seq:
  607. * FileDescriptor.new => file
  608. *
  609. * Returns a new file descriptor. The syntax must be set before it's passed
  610. * to a builder.
  611. */
  612. VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
  613. VALUE descriptor_pool, VALUE ptr) {
  614. DEFINE_SELF(FileDescriptor, self, _self);
  615. if (cookie != c_only_cookie) {
  616. rb_raise(rb_eRuntimeError,
  617. "Descriptor objects may not be created from Ruby.");
  618. }
  619. self->descriptor_pool = descriptor_pool;
  620. self->filedef = (const upb_filedef*)NUM2ULL(ptr);
  621. return Qnil;
  622. }
  623. void FileDescriptor_register(VALUE module) {
  624. VALUE klass = rb_define_class_under(
  625. module, "FileDescriptor", rb_cObject);
  626. rb_define_alloc_func(klass, FileDescriptor_alloc);
  627. rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
  628. rb_define_method(klass, "name", FileDescriptor_name, 0);
  629. rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
  630. rb_gc_register_address(&cFileDescriptor);
  631. cFileDescriptor = klass;
  632. }
  633. /*
  634. * call-seq:
  635. * FileDescriptor.name => name
  636. *
  637. * Returns the name of the file.
  638. */
  639. VALUE FileDescriptor_name(VALUE _self) {
  640. DEFINE_SELF(FileDescriptor, self, _self);
  641. const char* name = upb_filedef_name(self->filedef);
  642. return name == NULL ? Qnil : rb_str_new2(name);
  643. }
  644. /*
  645. * call-seq:
  646. * FileDescriptor.syntax => syntax
  647. *
  648. * Returns this file descriptors syntax.
  649. *
  650. * Valid syntax versions are:
  651. * :proto2 or :proto3.
  652. */
  653. VALUE FileDescriptor_syntax(VALUE _self) {
  654. DEFINE_SELF(FileDescriptor, self, _self);
  655. switch (upb_filedef_syntax(self->filedef)) {
  656. case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
  657. case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
  658. default: return Qnil;
  659. }
  660. }
  661. // -----------------------------------------------------------------------------
  662. // FieldDescriptor.
  663. // -----------------------------------------------------------------------------
  664. DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
  665. void FieldDescriptor_mark(void* _self) {
  666. FieldDescriptor* self = _self;
  667. rb_gc_mark(self->descriptor_pool);
  668. }
  669. void FieldDescriptor_free(void* _self) {
  670. xfree(_self);
  671. }
  672. /*
  673. * call-seq:
  674. * FieldDescriptor.new => field
  675. *
  676. * Returns a new field descriptor. Its name, type, etc. must be set before it is
  677. * added to a message type.
  678. */
  679. VALUE FieldDescriptor_alloc(VALUE klass) {
  680. FieldDescriptor* self = ALLOC(FieldDescriptor);
  681. VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
  682. self->fielddef = NULL;
  683. return ret;
  684. }
  685. void FieldDescriptor_register(VALUE module) {
  686. VALUE klass = rb_define_class_under(
  687. module, "FieldDescriptor", rb_cObject);
  688. rb_define_alloc_func(klass, FieldDescriptor_alloc);
  689. rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
  690. rb_define_method(klass, "name", FieldDescriptor_name, 0);
  691. rb_define_method(klass, "type", FieldDescriptor_type, 0);
  692. rb_define_method(klass, "default", FieldDescriptor_default, 0);
  693. rb_define_method(klass, "label", FieldDescriptor_label, 0);
  694. rb_define_method(klass, "number", FieldDescriptor_number, 0);
  695. rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
  696. rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
  697. rb_define_method(klass, "has?", FieldDescriptor_has, 1);
  698. rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
  699. rb_define_method(klass, "get", FieldDescriptor_get, 1);
  700. rb_define_method(klass, "set", FieldDescriptor_set, 2);
  701. rb_gc_register_address(&cFieldDescriptor);
  702. cFieldDescriptor = klass;
  703. }
  704. /*
  705. * call-seq:
  706. * EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
  707. *
  708. * Creates a descriptor wrapper object. May only be called from C.
  709. */
  710. VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
  711. VALUE descriptor_pool, VALUE ptr) {
  712. DEFINE_SELF(FieldDescriptor, self, _self);
  713. if (cookie != c_only_cookie) {
  714. rb_raise(rb_eRuntimeError,
  715. "Descriptor objects may not be created from Ruby.");
  716. }
  717. self->descriptor_pool = descriptor_pool;
  718. self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
  719. return Qnil;
  720. }
  721. /*
  722. * call-seq:
  723. * FieldDescriptor.name => name
  724. *
  725. * Returns the name of this field.
  726. */
  727. VALUE FieldDescriptor_name(VALUE _self) {
  728. DEFINE_SELF(FieldDescriptor, self, _self);
  729. return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
  730. }
  731. upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
  732. if (TYPE(type) != T_SYMBOL) {
  733. rb_raise(rb_eArgError, "Expected symbol for field type.");
  734. }
  735. #define CONVERT(upb, ruby) \
  736. if (SYM2ID(type) == rb_intern( # ruby )) { \
  737. return UPB_TYPE_ ## upb; \
  738. }
  739. CONVERT(FLOAT, float);
  740. CONVERT(DOUBLE, double);
  741. CONVERT(BOOL, bool);
  742. CONVERT(STRING, string);
  743. CONVERT(BYTES, bytes);
  744. CONVERT(MESSAGE, message);
  745. CONVERT(ENUM, enum);
  746. CONVERT(INT32, int32);
  747. CONVERT(INT64, int64);
  748. CONVERT(UINT32, uint32);
  749. CONVERT(UINT64, uint64);
  750. #undef CONVERT
  751. rb_raise(rb_eArgError, "Unknown field type.");
  752. return 0;
  753. }
  754. VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
  755. switch (type) {
  756. #define CONVERT(upb, ruby) \
  757. case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
  758. CONVERT(FLOAT, float);
  759. CONVERT(DOUBLE, double);
  760. CONVERT(BOOL, bool);
  761. CONVERT(STRING, string);
  762. CONVERT(BYTES, bytes);
  763. CONVERT(MESSAGE, message);
  764. CONVERT(ENUM, enum);
  765. CONVERT(INT32, int32);
  766. CONVERT(INT64, int64);
  767. CONVERT(UINT32, uint32);
  768. CONVERT(UINT64, uint64);
  769. #undef CONVERT
  770. }
  771. return Qnil;
  772. }
  773. upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
  774. if (TYPE(type) != T_SYMBOL) {
  775. rb_raise(rb_eArgError, "Expected symbol for field type.");
  776. }
  777. #define CONVERT(upb, ruby) \
  778. if (SYM2ID(type) == rb_intern( # ruby )) { \
  779. return UPB_DESCRIPTOR_TYPE_ ## upb; \
  780. }
  781. CONVERT(FLOAT, float);
  782. CONVERT(DOUBLE, double);
  783. CONVERT(BOOL, bool);
  784. CONVERT(STRING, string);
  785. CONVERT(BYTES, bytes);
  786. CONVERT(MESSAGE, message);
  787. CONVERT(GROUP, group);
  788. CONVERT(ENUM, enum);
  789. CONVERT(INT32, int32);
  790. CONVERT(INT64, int64);
  791. CONVERT(UINT32, uint32);
  792. CONVERT(UINT64, uint64);
  793. CONVERT(SINT32, sint32);
  794. CONVERT(SINT64, sint64);
  795. CONVERT(FIXED32, fixed32);
  796. CONVERT(FIXED64, fixed64);
  797. CONVERT(SFIXED32, sfixed32);
  798. CONVERT(SFIXED64, sfixed64);
  799. #undef CONVERT
  800. rb_raise(rb_eArgError, "Unknown field type.");
  801. return 0;
  802. }
  803. VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
  804. switch (type) {
  805. #define CONVERT(upb, ruby) \
  806. case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
  807. CONVERT(FLOAT, float);
  808. CONVERT(DOUBLE, double);
  809. CONVERT(BOOL, bool);
  810. CONVERT(STRING, string);
  811. CONVERT(BYTES, bytes);
  812. CONVERT(MESSAGE, message);
  813. CONVERT(GROUP, group);
  814. CONVERT(ENUM, enum);
  815. CONVERT(INT32, int32);
  816. CONVERT(INT64, int64);
  817. CONVERT(UINT32, uint32);
  818. CONVERT(UINT64, uint64);
  819. CONVERT(SINT32, sint32);
  820. CONVERT(SINT64, sint64);
  821. CONVERT(FIXED32, fixed32);
  822. CONVERT(FIXED64, fixed64);
  823. CONVERT(SFIXED32, sfixed32);
  824. CONVERT(SFIXED64, sfixed64);
  825. #undef CONVERT
  826. }
  827. return Qnil;
  828. }
  829. VALUE ruby_to_label(VALUE label) {
  830. upb_label_t upb_label;
  831. bool converted = false;
  832. #define CONVERT(upb, ruby) \
  833. if (SYM2ID(label) == rb_intern( # ruby )) { \
  834. upb_label = UPB_LABEL_ ## upb; \
  835. converted = true; \
  836. }
  837. CONVERT(OPTIONAL, optional);
  838. CONVERT(REQUIRED, required);
  839. CONVERT(REPEATED, repeated);
  840. #undef CONVERT
  841. if (!converted) {
  842. rb_raise(rb_eArgError, "Unknown field label.");
  843. }
  844. return upb_label;
  845. }
  846. /*
  847. * call-seq:
  848. * FieldDescriptor.type => type
  849. *
  850. * Returns this field's type, as a Ruby symbol, or nil if not yet set.
  851. *
  852. * Valid field types are:
  853. * :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
  854. * :bytes, :message.
  855. */
  856. VALUE FieldDescriptor_type(VALUE _self) {
  857. DEFINE_SELF(FieldDescriptor, self, _self);
  858. return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
  859. }
  860. /*
  861. * call-seq:
  862. * FieldDescriptor.default => default
  863. *
  864. * Returns this field's default, as a Ruby object, or nil if not yet set.
  865. */
  866. VALUE FieldDescriptor_default(VALUE _self) {
  867. DEFINE_SELF(FieldDescriptor, self, _self);
  868. return layout_get_default(self->fielddef);
  869. }
  870. /*
  871. * call-seq:
  872. * FieldDescriptor.label => label
  873. *
  874. * Returns this field's label (i.e., plurality), as a Ruby symbol.
  875. *
  876. * Valid field labels are:
  877. * :optional, :repeated
  878. */
  879. VALUE FieldDescriptor_label(VALUE _self) {
  880. DEFINE_SELF(FieldDescriptor, self, _self);
  881. switch (upb_fielddef_label(self->fielddef)) {
  882. #define CONVERT(upb, ruby) \
  883. case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
  884. CONVERT(OPTIONAL, optional);
  885. CONVERT(REQUIRED, required);
  886. CONVERT(REPEATED, repeated);
  887. #undef CONVERT
  888. }
  889. return Qnil;
  890. }
  891. /*
  892. * call-seq:
  893. * FieldDescriptor.number => number
  894. *
  895. * Returns the tag number for this field.
  896. */
  897. VALUE FieldDescriptor_number(VALUE _self) {
  898. DEFINE_SELF(FieldDescriptor, self, _self);
  899. return INT2NUM(upb_fielddef_number(self->fielddef));
  900. }
  901. /*
  902. * call-seq:
  903. * FieldDescriptor.submsg_name => submsg_name
  904. *
  905. * Returns the name of the message or enum type corresponding to this field, if
  906. * it is a message or enum field (respectively), or nil otherwise. This type
  907. * name will be resolved within the context of the pool to which the containing
  908. * message type is added.
  909. */
  910. VALUE FieldDescriptor_submsg_name(VALUE _self) {
  911. DEFINE_SELF(FieldDescriptor, self, _self);
  912. switch (upb_fielddef_type(self->fielddef)) {
  913. case UPB_TYPE_ENUM:
  914. return rb_str_new2(
  915. upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
  916. case UPB_TYPE_MESSAGE:
  917. return rb_str_new2(
  918. upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
  919. default:
  920. return Qnil;
  921. }
  922. }
  923. /*
  924. * call-seq:
  925. * FieldDescriptor.subtype => message_or_enum_descriptor
  926. *
  927. * Returns the message or enum descriptor corresponding to this field's type if
  928. * it is a message or enum field, respectively, or nil otherwise. Cannot be
  929. * called *until* the containing message type is added to a pool (and thus
  930. * resolved).
  931. */
  932. VALUE FieldDescriptor_subtype(VALUE _self) {
  933. DEFINE_SELF(FieldDescriptor, self, _self);
  934. switch (upb_fielddef_type(self->fielddef)) {
  935. case UPB_TYPE_ENUM:
  936. return get_enumdef_obj(self->descriptor_pool,
  937. upb_fielddef_enumsubdef(self->fielddef));
  938. case UPB_TYPE_MESSAGE:
  939. return get_msgdef_obj(self->descriptor_pool,
  940. upb_fielddef_msgsubdef(self->fielddef));
  941. default:
  942. return Qnil;
  943. }
  944. }
  945. /*
  946. * call-seq:
  947. * FieldDescriptor.get(message) => value
  948. *
  949. * Returns the value set for this field on the given message. Raises an
  950. * exception if message is of the wrong type.
  951. */
  952. VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
  953. DEFINE_SELF(FieldDescriptor, self, _self);
  954. MessageHeader* msg;
  955. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  956. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  957. rb_raise(cTypeError, "get method called on wrong message type");
  958. }
  959. return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
  960. }
  961. /*
  962. * call-seq:
  963. * FieldDescriptor.has?(message) => boolean
  964. *
  965. * Returns whether the value is set on the given message. Raises an
  966. * exception when calling with proto syntax 3.
  967. */
  968. VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
  969. DEFINE_SELF(FieldDescriptor, self, _self);
  970. MessageHeader* msg;
  971. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  972. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  973. rb_raise(cTypeError, "has method called on wrong message type");
  974. } else if (!upb_fielddef_haspresence(self->fielddef)) {
  975. rb_raise(rb_eArgError, "does not track presence");
  976. }
  977. return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef);
  978. }
  979. /*
  980. * call-seq:
  981. * FieldDescriptor.clear(message)
  982. *
  983. * Clears the field from the message if it's set.
  984. */
  985. VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
  986. DEFINE_SELF(FieldDescriptor, self, _self);
  987. MessageHeader* msg;
  988. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  989. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  990. rb_raise(cTypeError, "has method called on wrong message type");
  991. }
  992. layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef);
  993. return Qnil;
  994. }
  995. /*
  996. * call-seq:
  997. * FieldDescriptor.set(message, value)
  998. *
  999. * Sets the value corresponding to this field to the given value on the given
  1000. * message. Raises an exception if message is of the wrong type. Performs the
  1001. * ordinary type-checks for field setting.
  1002. */
  1003. VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
  1004. DEFINE_SELF(FieldDescriptor, self, _self);
  1005. MessageHeader* msg;
  1006. TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
  1007. if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
  1008. rb_raise(cTypeError, "set method called on wrong message type");
  1009. }
  1010. layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
  1011. return Qnil;
  1012. }
  1013. // -----------------------------------------------------------------------------
  1014. // OneofDescriptor.
  1015. // -----------------------------------------------------------------------------
  1016. DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
  1017. void OneofDescriptor_mark(void* _self) {
  1018. OneofDescriptor* self = _self;
  1019. rb_gc_mark(self->descriptor_pool);
  1020. }
  1021. void OneofDescriptor_free(void* _self) {
  1022. xfree(_self);
  1023. }
  1024. /*
  1025. * call-seq:
  1026. * OneofDescriptor.new => oneof_descriptor
  1027. *
  1028. * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
  1029. * to being added to a message descriptor which is subsequently added to a pool.
  1030. */
  1031. VALUE OneofDescriptor_alloc(VALUE klass) {
  1032. OneofDescriptor* self = ALLOC(OneofDescriptor);
  1033. VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
  1034. self->oneofdef = NULL;
  1035. self->descriptor_pool = Qnil;
  1036. return ret;
  1037. }
  1038. void OneofDescriptor_register(VALUE module) {
  1039. VALUE klass = rb_define_class_under(
  1040. module, "OneofDescriptor", rb_cObject);
  1041. rb_define_alloc_func(klass, OneofDescriptor_alloc);
  1042. rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
  1043. rb_define_method(klass, "name", OneofDescriptor_name, 0);
  1044. rb_define_method(klass, "each", OneofDescriptor_each, 0);
  1045. rb_include_module(klass, rb_mEnumerable);
  1046. rb_gc_register_address(&cOneofDescriptor);
  1047. cOneofDescriptor = klass;
  1048. }
  1049. /*
  1050. * call-seq:
  1051. * OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
  1052. *
  1053. * Creates a descriptor wrapper object. May only be called from C.
  1054. */
  1055. VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
  1056. VALUE descriptor_pool, VALUE ptr) {
  1057. DEFINE_SELF(OneofDescriptor, self, _self);
  1058. if (cookie != c_only_cookie) {
  1059. rb_raise(rb_eRuntimeError,
  1060. "Descriptor objects may not be created from Ruby.");
  1061. }
  1062. self->descriptor_pool = descriptor_pool;
  1063. self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
  1064. return Qnil;
  1065. }
  1066. /*
  1067. * call-seq:
  1068. * OneofDescriptor.name => name
  1069. *
  1070. * Returns the name of this oneof.
  1071. */
  1072. VALUE OneofDescriptor_name(VALUE _self) {
  1073. DEFINE_SELF(OneofDescriptor, self, _self);
  1074. return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
  1075. }
  1076. /*
  1077. * call-seq:
  1078. * OneofDescriptor.each(&block) => nil
  1079. *
  1080. * Iterates through fields in this oneof, yielding to the block on each one.
  1081. */
  1082. VALUE OneofDescriptor_each(VALUE _self, VALUE field) {
  1083. DEFINE_SELF(OneofDescriptor, self, _self);
  1084. upb_oneof_iter it;
  1085. for (upb_oneof_begin(&it, self->oneofdef);
  1086. !upb_oneof_done(&it);
  1087. upb_oneof_next(&it)) {
  1088. const upb_fielddef* f = upb_oneof_iter_field(&it);
  1089. VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
  1090. rb_yield(obj);
  1091. }
  1092. return Qnil;
  1093. }
  1094. // -----------------------------------------------------------------------------
  1095. // EnumDescriptor.
  1096. // -----------------------------------------------------------------------------
  1097. DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
  1098. void EnumDescriptor_mark(void* _self) {
  1099. EnumDescriptor* self = _self;
  1100. rb_gc_mark(self->module);
  1101. rb_gc_mark(self->descriptor_pool);
  1102. }
  1103. void EnumDescriptor_free(void* _self) {
  1104. xfree(_self);
  1105. }
  1106. VALUE EnumDescriptor_alloc(VALUE klass) {
  1107. EnumDescriptor* self = ALLOC(EnumDescriptor);
  1108. VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
  1109. self->enumdef = NULL;
  1110. self->module = Qnil;
  1111. self->descriptor_pool = Qnil;
  1112. return ret;
  1113. }
  1114. /*
  1115. * call-seq:
  1116. * EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
  1117. *
  1118. * Creates a descriptor wrapper object. May only be called from C.
  1119. */
  1120. VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
  1121. VALUE descriptor_pool, VALUE ptr) {
  1122. DEFINE_SELF(EnumDescriptor, self, _self);
  1123. if (cookie != c_only_cookie) {
  1124. rb_raise(rb_eRuntimeError,
  1125. "Descriptor objects may not be created from Ruby.");
  1126. }
  1127. self->descriptor_pool = descriptor_pool;
  1128. self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
  1129. return Qnil;
  1130. }
  1131. void EnumDescriptor_register(VALUE module) {
  1132. VALUE klass = rb_define_class_under(
  1133. module, "EnumDescriptor", rb_cObject);
  1134. rb_define_alloc_func(klass, EnumDescriptor_alloc);
  1135. rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
  1136. rb_define_method(klass, "name", EnumDescriptor_name, 0);
  1137. rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
  1138. rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
  1139. rb_define_method(klass, "each", EnumDescriptor_each, 0);
  1140. rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
  1141. rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
  1142. rb_include_module(klass, rb_mEnumerable);
  1143. rb_gc_register_address(&cEnumDescriptor);
  1144. cEnumDescriptor = klass;
  1145. }
  1146. /*
  1147. * call-seq:
  1148. * EnumDescriptor.file_descriptor
  1149. *
  1150. * Returns the FileDescriptor object this enum belongs to.
  1151. */
  1152. VALUE EnumDescriptor_file_descriptor(VALUE _self) {
  1153. DEFINE_SELF(EnumDescriptor, self, _self);
  1154. return get_filedef_obj(self->descriptor_pool,
  1155. upb_enumdef_file(self->enumdef));
  1156. }
  1157. /*
  1158. * call-seq:
  1159. * EnumDescriptor.name => name
  1160. *
  1161. * Returns the name of this enum type.
  1162. */
  1163. VALUE EnumDescriptor_name(VALUE _self) {
  1164. DEFINE_SELF(EnumDescriptor, self, _self);
  1165. return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
  1166. }
  1167. /*
  1168. * call-seq:
  1169. * EnumDescriptor.lookup_name(name) => value
  1170. *
  1171. * Returns the numeric value corresponding to the given key name (as a Ruby
  1172. * symbol), or nil if none.
  1173. */
  1174. VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
  1175. DEFINE_SELF(EnumDescriptor, self, _self);
  1176. const char* name_str= rb_id2name(SYM2ID(name));
  1177. int32_t val = 0;
  1178. if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
  1179. return INT2NUM(val);
  1180. } else {
  1181. return Qnil;
  1182. }
  1183. }
  1184. /*
  1185. * call-seq:
  1186. * EnumDescriptor.lookup_value(name) => value
  1187. *
  1188. * Returns the key name (as a Ruby symbol) corresponding to the integer value,
  1189. * or nil if none.
  1190. */
  1191. VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
  1192. DEFINE_SELF(EnumDescriptor, self, _self);
  1193. int32_t val = NUM2INT(number);
  1194. const char* name = upb_enumdef_iton(self->enumdef, val);
  1195. if (name != NULL) {
  1196. return ID2SYM(rb_intern(name));
  1197. } else {
  1198. return Qnil;
  1199. }
  1200. }
  1201. /*
  1202. * call-seq:
  1203. * EnumDescriptor.each(&block)
  1204. *
  1205. * Iterates over key => value mappings in this enum's definition, yielding to
  1206. * the block with (key, value) arguments for each one.
  1207. */
  1208. VALUE EnumDescriptor_each(VALUE _self) {
  1209. DEFINE_SELF(EnumDescriptor, self, _self);
  1210. upb_enum_iter it;
  1211. for (upb_enum_begin(&it, self->enumdef);
  1212. !upb_enum_done(&it);
  1213. upb_enum_next(&it)) {
  1214. VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
  1215. VALUE number = INT2NUM(upb_enum_iter_number(&it));
  1216. rb_yield_values(2, key, number);
  1217. }
  1218. return Qnil;
  1219. }
  1220. /*
  1221. * call-seq:
  1222. * EnumDescriptor.enummodule => module
  1223. *
  1224. * Returns the Ruby module corresponding to this enum type.
  1225. */
  1226. VALUE EnumDescriptor_enummodule(VALUE _self) {
  1227. DEFINE_SELF(EnumDescriptor, self, _self);
  1228. if (self->module == Qnil) {
  1229. self->module = build_module_from_enumdesc(_self);
  1230. }
  1231. return self->module;
  1232. }
  1233. // -----------------------------------------------------------------------------
  1234. // MessageBuilderContext.
  1235. // -----------------------------------------------------------------------------
  1236. DEFINE_CLASS(MessageBuilderContext,
  1237. "Google::Protobuf::Internal::MessageBuilderContext");
  1238. void MessageBuilderContext_mark(void* _self) {
  1239. MessageBuilderContext* self = _self;
  1240. rb_gc_mark(self->file_builder);
  1241. }
  1242. void MessageBuilderContext_free(void* _self) {
  1243. MessageBuilderContext* self = _self;
  1244. xfree(self);
  1245. }
  1246. VALUE MessageBuilderContext_alloc(VALUE klass) {
  1247. MessageBuilderContext* self = ALLOC(MessageBuilderContext);
  1248. VALUE ret = TypedData_Wrap_Struct(
  1249. klass, &_MessageBuilderContext_type, self);
  1250. self->file_builder = Qnil;
  1251. return ret;
  1252. }
  1253. void MessageBuilderContext_register(VALUE module) {
  1254. VALUE klass = rb_define_class_under(
  1255. module, "MessageBuilderContext", rb_cObject);
  1256. rb_define_alloc_func(klass, MessageBuilderContext_alloc);
  1257. rb_define_method(klass, "initialize",
  1258. MessageBuilderContext_initialize, 2);
  1259. rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
  1260. rb_define_method(klass, "required", MessageBuilderContext_required, -1);
  1261. rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
  1262. rb_define_method(klass, "map", MessageBuilderContext_map, -1);
  1263. rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
  1264. rb_gc_register_address(&cMessageBuilderContext);
  1265. cMessageBuilderContext = klass;
  1266. }
  1267. /*
  1268. * call-seq:
  1269. * MessageBuilderContext.new(file_builder, name) => context
  1270. *
  1271. * Create a new message builder context around the given message descriptor and
  1272. * builder context. This class is intended to serve as a DSL context to be used
  1273. * with #instance_eval.
  1274. */
  1275. VALUE MessageBuilderContext_initialize(VALUE _self,
  1276. VALUE _file_builder,
  1277. VALUE name) {
  1278. DEFINE_SELF(MessageBuilderContext, self, _self);
  1279. FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
  1280. google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
  1281. self->file_builder = _file_builder;
  1282. self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
  1283. file_proto, file_builder->arena);
  1284. google_protobuf_DescriptorProto_set_name(
  1285. self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
  1286. return Qnil;
  1287. }
  1288. static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
  1289. VALUE type, VALUE number, VALUE type_class,
  1290. VALUE options, int oneof_index) {
  1291. DEFINE_SELF(MessageBuilderContext, self, msgbuilder_rb);
  1292. FileBuilderContext* file_context =
  1293. ruby_to_FileBuilderContext(self->file_builder);
  1294. google_protobuf_FieldDescriptorProto* field_proto;
  1295. VALUE name_str;
  1296. field_proto = google_protobuf_DescriptorProto_add_field(self->msg_proto,
  1297. file_context->arena);
  1298. Check_Type(name, T_SYMBOL);
  1299. name_str = rb_id2str(SYM2ID(name));
  1300. google_protobuf_FieldDescriptorProto_set_name(
  1301. field_proto, FileBuilderContext_strdup(self->file_builder, name_str));
  1302. google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number));
  1303. google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label);
  1304. google_protobuf_FieldDescriptorProto_set_type(
  1305. field_proto, (int)ruby_to_descriptortype(type));
  1306. if (type_class != Qnil) {
  1307. Check_Type(type_class, T_STRING);
  1308. // Make it an absolute type name by prepending a dot.
  1309. type_class = rb_str_append(rb_str_new2("."), type_class);
  1310. google_protobuf_FieldDescriptorProto_set_type_name(
  1311. field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
  1312. }
  1313. if (options != Qnil) {
  1314. Check_Type(options, T_HASH);
  1315. if (rb_funcall(options, rb_intern("key?"), 1,
  1316. ID2SYM(rb_intern("default"))) == Qtrue) {
  1317. VALUE default_value =
  1318. rb_hash_lookup(options, ID2SYM(rb_intern("default")));
  1319. /* Call #to_s since all defaults are strings in the descriptor. */
  1320. default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
  1321. google_protobuf_FieldDescriptorProto_set_default_value(
  1322. field_proto,
  1323. FileBuilderContext_strdup(self->file_builder, default_value));
  1324. }
  1325. }
  1326. if (oneof_index >= 0) {
  1327. google_protobuf_FieldDescriptorProto_set_oneof_index(field_proto,
  1328. oneof_index);
  1329. }
  1330. }
  1331. static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
  1332. VALUE* argv) {
  1333. DEFINE_SELF(MessageBuilderContext, message_builder, _message_builder);
  1334. VALUE type_class = rb_ary_entry(types, 2);
  1335. FileBuilderContext* file_context =
  1336. ruby_to_FileBuilderContext(message_builder->file_builder);
  1337. google_protobuf_MessageOptions* options =
  1338. google_protobuf_DescriptorProto_mutable_options(
  1339. message_builder->msg_proto, file_context->arena);
  1340. google_protobuf_MessageOptions_set_map_entry(options, true);
  1341. // optional <type> key = 1;
  1342. rb_funcall(_message_builder, rb_intern("optional"), 3,
  1343. ID2SYM(rb_intern("key")), rb_ary_entry(types, 0), INT2NUM(1));
  1344. // optional <type> value = 2;
  1345. if (type_class == Qnil) {
  1346. rb_funcall(_message_builder, rb_intern("optional"), 3,
  1347. ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2));
  1348. } else {
  1349. rb_funcall(_message_builder, rb_intern("optional"), 4,
  1350. ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2),
  1351. type_class);
  1352. }
  1353. return Qnil;
  1354. }
  1355. /*
  1356. * call-seq:
  1357. * MessageBuilderContext.optional(name, type, number, type_class = nil,
  1358. * options = nil)
  1359. *
  1360. * Defines a new optional field on this message type with the given type, tag
  1361. * number, and type class (for message and enum fields). The type must be a Ruby
  1362. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1363. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1364. */
  1365. VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1366. VALUE name, type, number;
  1367. VALUE type_class, options = Qnil;
  1368. rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
  1369. // Allow passing (name, type, number, options) or
  1370. // (name, type, number, type_class, options)
  1371. if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
  1372. options = type_class;
  1373. type_class = Qnil;
  1374. }
  1375. msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class,
  1376. options, -1);
  1377. return Qnil;
  1378. }
  1379. /*
  1380. * call-seq:
  1381. * MessageBuilderContext.required(name, type, number, type_class = nil,
  1382. * options = nil)
  1383. *
  1384. * Defines a new required field on this message type with the given type, tag
  1385. * number, and type class (for message and enum fields). The type must be a Ruby
  1386. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1387. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1388. *
  1389. * Proto3 does not have required fields, but this method exists for
  1390. * completeness. Any attempt to add a message type with required fields to a
  1391. * pool will currently result in an error.
  1392. */
  1393. VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
  1394. VALUE name, type, number;
  1395. VALUE type_class, options = Qnil;
  1396. rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
  1397. // Allow passing (name, type, number, options) or
  1398. // (name, type, number, type_class, options)
  1399. if (argc == 4 && RB_TYPE_P(type_class, T_HASH)) {
  1400. options = type_class;
  1401. type_class = Qnil;
  1402. }
  1403. msgdef_add_field(_self, UPB_LABEL_REQUIRED, name, type, number, type_class,
  1404. options, -1);
  1405. return Qnil;
  1406. }
  1407. /*
  1408. * call-seq:
  1409. * MessageBuilderContext.repeated(name, type, number, type_class = nil)
  1410. *
  1411. * Defines a new repeated field on this message type with the given type, tag
  1412. * number, and type class (for message and enum fields). The type must be a Ruby
  1413. * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  1414. * string, if present (as accepted by FieldDescriptor#submsg_name=).
  1415. */
  1416. VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
  1417. VALUE name, type, number, type_class;
  1418. if (argc < 3) {
  1419. rb_raise(rb_eArgError, "Expected at least 3 arguments.");
  1420. }
  1421. name = argv[0];
  1422. type = argv[1];
  1423. number = argv[2];
  1424. type_class = (argc > 3) ? argv[3] : Qnil;
  1425. msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class,
  1426. Qnil, -1);
  1427. return Qnil;
  1428. }
  1429. /*
  1430. * call-seq:
  1431. * MessageBuilderContext.map(name, key_type, value_type, number,
  1432. * value_type_class = nil)
  1433. *
  1434. * Defines a new map field on this message type with the given key and value
  1435. * types, tag number, and type class (for message and enum value types). The key
  1436. * type must be :int32/:uint32/:int64/:uint64, :bool, or :string. The value type
  1437. * type must be a Ruby symbol (as accepted by FieldDescriptor#type=) and the
  1438. * type_class must be a string, if present (as accepted by
  1439. * FieldDescriptor#submsg_name=).
  1440. */
  1441. VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
  1442. DEFINE_SELF(MessageBuilderContext, self, _self);
  1443. VALUE name, key_type, value_type, number, type_class;
  1444. VALUE mapentry_desc_name;
  1445. FileBuilderContext* file_builder;
  1446. upb_strview msg_name;
  1447. if (argc < 4) {
  1448. rb_raise(rb_eArgError, "Expected at least 4 arguments.");
  1449. }
  1450. name = argv[0];
  1451. key_type = argv[1];
  1452. value_type = argv[2];
  1453. number = argv[3];
  1454. type_class = (argc > 4) ? argv[4] : Qnil;
  1455. // Validate the key type. We can't accept enums, messages, or floats/doubles
  1456. // as map keys. (We exclude these explicitly, and the field-descriptor setter
  1457. // below then ensures that the type is one of the remaining valid options.)
  1458. if (SYM2ID(key_type) == rb_intern("float") ||
  1459. SYM2ID(key_type) == rb_intern("double") ||
  1460. SYM2ID(key_type) == rb_intern("enum") ||
  1461. SYM2ID(key_type) == rb_intern("message")) {
  1462. rb_raise(rb_eArgError,
  1463. "Cannot add a map field with a float, double, enum, or message "
  1464. "type.");
  1465. }
  1466. file_builder = ruby_to_FileBuilderContext(self->file_builder);
  1467. // TODO(haberman): remove this restriction, maps are supported in proto2.
  1468. if (upb_strview_eql(
  1469. google_protobuf_FileDescriptorProto_syntax(file_builder->file_proto),
  1470. upb_strview_makez("proto2"))) {
  1471. rb_raise(rb_eArgError,
  1472. "Cannot add a native map field using proto2 syntax.");
  1473. }
  1474. // Create a new message descriptor for the map entry message, and create a
  1475. // repeated submessage field here with that type.
  1476. msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
  1477. mapentry_desc_name = rb_str_new(msg_name.data, msg_name.size);
  1478. mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
  1479. mapentry_desc_name =
  1480. rb_str_cat2(mapentry_desc_name, rb_id2name(SYM2ID(name)));
  1481. {
  1482. // message <msgname>_MapEntry_ { /* ... */ }
  1483. VALUE args[1] = {mapentry_desc_name};
  1484. VALUE types = rb_ary_new3(3, key_type, value_type, type_class);
  1485. rb_block_call(self->file_builder, rb_intern("add_message"), 1, args,
  1486. make_mapentry, types);
  1487. }
  1488. // If this file is in a package, we need to qualify the map entry type.
  1489. if (google_protobuf_FileDescriptorProto_has_package(file_builder->file_proto)) {
  1490. upb_strview package_view =
  1491. google_protobuf_FileDescriptorProto_package(file_builder->file_proto);
  1492. VALUE package = rb_str_new(package_view.data, package_view.size);
  1493. package = rb_str_cat2(package, ".");
  1494. mapentry_desc_name = rb_str_concat(package, mapentry_desc_name);
  1495. }
  1496. // repeated MapEntry <name> = <number>;
  1497. rb_funcall(_self, rb_intern("repeated"), 4, name,
  1498. ID2SYM(rb_intern("message")), number, mapentry_desc_name);
  1499. return Qnil;
  1500. }
  1501. /*
  1502. * call-seq:
  1503. * MessageBuilderContext.oneof(name, &block) => nil
  1504. *
  1505. * Creates a new OneofDescriptor with the given name, creates a
  1506. * OneofBuilderContext attached to that OneofDescriptor, evaluates the given
  1507. * block in the context of that OneofBuilderContext with #instance_eval, and
  1508. * then adds the oneof to the message.
  1509. *
  1510. * This is the recommended, idiomatic way to build oneof definitions.
  1511. */
  1512. VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
  1513. DEFINE_SELF(MessageBuilderContext, self, _self);
  1514. size_t oneof_count;
  1515. FileBuilderContext* file_context =
  1516. ruby_to_FileBuilderContext(self->file_builder);
  1517. google_protobuf_OneofDescriptorProto* oneof_proto;
  1518. // Existing oneof_count becomes oneof_index.
  1519. google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
  1520. // Create oneof_proto and set its name.
  1521. oneof_proto = google_protobuf_DescriptorProto_add_oneof_decl(
  1522. self->msg_proto, file_context->arena);
  1523. google_protobuf_OneofDescriptorProto_set_name(
  1524. oneof_proto, FileBuilderContext_strdup_sym(self->file_builder, name));
  1525. // Evaluate the block with the builder as argument.
  1526. {
  1527. VALUE args[2] = { INT2NUM(oneof_count), _self };
  1528. VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
  1529. VALUE block = rb_block_proc();
  1530. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1531. }
  1532. return Qnil;
  1533. }
  1534. // -----------------------------------------------------------------------------
  1535. // OneofBuilderContext.
  1536. // -----------------------------------------------------------------------------
  1537. DEFINE_CLASS(OneofBuilderContext,
  1538. "Google::Protobuf::Internal::OneofBuilderContext");
  1539. void OneofBuilderContext_mark(void* _self) {
  1540. OneofBuilderContext* self = _self;
  1541. rb_gc_mark(self->message_builder);
  1542. }
  1543. void OneofBuilderContext_free(void* _self) {
  1544. xfree(_self);
  1545. }
  1546. VALUE OneofBuilderContext_alloc(VALUE klass) {
  1547. OneofBuilderContext* self = ALLOC(OneofBuilderContext);
  1548. VALUE ret = TypedData_Wrap_Struct(
  1549. klass, &_OneofBuilderContext_type, self);
  1550. self->oneof_index = 0;
  1551. self->message_builder = Qnil;
  1552. return ret;
  1553. }
  1554. void OneofBuilderContext_register(VALUE module) {
  1555. VALUE klass = rb_define_class_under(
  1556. module, "OneofBuilderContext", rb_cObject);
  1557. rb_define_alloc_func(klass, OneofBuilderContext_alloc);
  1558. rb_define_method(klass, "initialize",
  1559. OneofBuilderContext_initialize, 2);
  1560. rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
  1561. rb_gc_register_address(&cOneofBuilderContext);
  1562. cOneofBuilderContext = klass;
  1563. }
  1564. /*
  1565. * call-seq:
  1566. * OneofBuilderContext.new(oneof_index, message_builder) => context
  1567. *
  1568. * Create a new oneof builder context around the given oneof descriptor and
  1569. * builder context. This class is intended to serve as a DSL context to be used
  1570. * with #instance_eval.
  1571. */
  1572. VALUE OneofBuilderContext_initialize(VALUE _self,
  1573. VALUE oneof_index,
  1574. VALUE message_builder) {
  1575. DEFINE_SELF(OneofBuilderContext, self, _self);
  1576. self->oneof_index = NUM2INT(oneof_index);
  1577. self->message_builder = message_builder;
  1578. return Qnil;
  1579. }
  1580. /*
  1581. * call-seq:
  1582. * OneofBuilderContext.optional(name, type, number, type_class = nil,
  1583. * default_value = nil)
  1584. *
  1585. * Defines a new optional field in this oneof with the given type, tag number,
  1586. * and type class (for message and enum fields). The type must be a Ruby symbol
  1587. * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
  1588. * if present (as accepted by FieldDescriptor#submsg_name=).
  1589. */
  1590. VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
  1591. DEFINE_SELF(OneofBuilderContext, self, _self);
  1592. VALUE name, type, number;
  1593. VALUE type_class, options = Qnil;
  1594. rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
  1595. msgdef_add_field(self->message_builder, UPB_LABEL_OPTIONAL, name, type,
  1596. number, type_class, options, self->oneof_index);
  1597. return Qnil;
  1598. }
  1599. // -----------------------------------------------------------------------------
  1600. // EnumBuilderContext.
  1601. // -----------------------------------------------------------------------------
  1602. DEFINE_CLASS(EnumBuilderContext,
  1603. "Google::Protobuf::Internal::EnumBuilderContext");
  1604. void EnumBuilderContext_mark(void* _self) {
  1605. EnumBuilderContext* self = _self;
  1606. rb_gc_mark(self->file_builder);
  1607. }
  1608. void EnumBuilderContext_free(void* _self) {
  1609. xfree(_self);
  1610. }
  1611. VALUE EnumBuilderContext_alloc(VALUE klass) {
  1612. EnumBuilderContext* self = ALLOC(EnumBuilderContext);
  1613. VALUE ret = TypedData_Wrap_Struct(
  1614. klass, &_EnumBuilderContext_type, self);
  1615. self->enum_proto = NULL;
  1616. self->file_builder = Qnil;
  1617. return ret;
  1618. }
  1619. void EnumBuilderContext_register(VALUE module) {
  1620. VALUE klass = rb_define_class_under(
  1621. module, "EnumBuilderContext", rb_cObject);
  1622. rb_define_alloc_func(klass, EnumBuilderContext_alloc);
  1623. rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
  1624. rb_define_method(klass, "value", EnumBuilderContext_value, 2);
  1625. rb_gc_register_address(&cEnumBuilderContext);
  1626. cEnumBuilderContext = klass;
  1627. }
  1628. /*
  1629. * call-seq:
  1630. * EnumBuilderContext.new(file_builder) => context
  1631. *
  1632. * Create a new builder context around the given enum descriptor. This class is
  1633. * intended to serve as a DSL context to be used with #instance_eval.
  1634. */
  1635. VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
  1636. VALUE name) {
  1637. DEFINE_SELF(EnumBuilderContext, self, _self);
  1638. FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
  1639. google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
  1640. self->file_builder = _file_builder;
  1641. self->enum_proto = google_protobuf_FileDescriptorProto_add_enum_type(
  1642. file_proto, file_builder->arena);
  1643. google_protobuf_EnumDescriptorProto_set_name(
  1644. self->enum_proto, FileBuilderContext_strdup(_file_builder, name));
  1645. return Qnil;
  1646. }
  1647. /*
  1648. * call-seq:
  1649. * EnumBuilder.add_value(name, number)
  1650. *
  1651. * Adds the given name => number mapping to the enum type. Name must be a Ruby
  1652. * symbol.
  1653. */
  1654. VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
  1655. DEFINE_SELF(EnumBuilderContext, self, _self);
  1656. FileBuilderContext* file_builder =
  1657. ruby_to_FileBuilderContext(self->file_builder);
  1658. google_protobuf_EnumValueDescriptorProto* enum_value;
  1659. enum_value = google_protobuf_EnumDescriptorProto_add_value(
  1660. self->enum_proto, file_builder->arena);
  1661. google_protobuf_EnumValueDescriptorProto_set_name(
  1662. enum_value, FileBuilderContext_strdup_sym(self->file_builder, name));
  1663. google_protobuf_EnumValueDescriptorProto_set_number(enum_value,
  1664. NUM2INT(number));
  1665. return Qnil;
  1666. }
  1667. // -----------------------------------------------------------------------------
  1668. // FileBuilderContext.
  1669. // -----------------------------------------------------------------------------
  1670. DEFINE_CLASS(FileBuilderContext,
  1671. "Google::Protobuf::Internal::FileBuilderContext");
  1672. void FileBuilderContext_mark(void* _self) {
  1673. FileBuilderContext* self = _self;
  1674. rb_gc_mark(self->descriptor_pool);
  1675. }
  1676. void FileBuilderContext_free(void* _self) {
  1677. FileBuilderContext* self = _self;
  1678. upb_arena_free(self->arena);
  1679. xfree(self);
  1680. }
  1681. upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
  1682. DEFINE_SELF(FileBuilderContext, self, _self);
  1683. upb_strview ret;
  1684. char *data;
  1685. ret.size = strlen(str);
  1686. data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
  1687. ret.data = data;
  1688. memcpy(data, str, ret.size);
  1689. /* Null-terminate required by rewrite_enum_defaults() above. */
  1690. data[ret.size] = '\0';
  1691. return ret;
  1692. }
  1693. upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
  1694. return FileBuilderContext_strdup2(_self, get_str(rb_str));
  1695. }
  1696. upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
  1697. Check_Type(rb_sym, T_SYMBOL);
  1698. return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
  1699. }
  1700. VALUE FileBuilderContext_alloc(VALUE klass) {
  1701. FileBuilderContext* self = ALLOC(FileBuilderContext);
  1702. VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self);
  1703. self->arena = upb_arena_new();
  1704. self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
  1705. self->descriptor_pool = Qnil;
  1706. return ret;
  1707. }
  1708. void FileBuilderContext_register(VALUE module) {
  1709. VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
  1710. rb_define_alloc_func(klass, FileBuilderContext_alloc);
  1711. rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
  1712. rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
  1713. rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
  1714. rb_gc_register_address(&cFileBuilderContext);
  1715. cFileBuilderContext = klass;
  1716. }
  1717. /*
  1718. * call-seq:
  1719. * FileBuilderContext.new(descriptor_pool) => context
  1720. *
  1721. * Create a new file builder context for the given file descriptor and
  1722. * builder context. This class is intended to serve as a DSL context to be used
  1723. * with #instance_eval.
  1724. */
  1725. VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
  1726. VALUE name, VALUE options) {
  1727. DEFINE_SELF(FileBuilderContext, self, _self);
  1728. self->descriptor_pool = descriptor_pool;
  1729. google_protobuf_FileDescriptorProto_set_name(
  1730. self->file_proto, FileBuilderContext_strdup(_self, name));
  1731. // Default syntax for Ruby is proto3.
  1732. google_protobuf_FileDescriptorProto_set_syntax(
  1733. self->file_proto,
  1734. FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
  1735. if (options != Qnil) {
  1736. VALUE syntax;
  1737. Check_Type(options, T_HASH);
  1738. syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
  1739. if (syntax != Qnil) {
  1740. VALUE syntax_str;
  1741. Check_Type(syntax, T_SYMBOL);
  1742. syntax_str = rb_id2str(SYM2ID(syntax));
  1743. google_protobuf_FileDescriptorProto_set_syntax(
  1744. self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
  1745. }
  1746. }
  1747. return Qnil;
  1748. }
  1749. /*
  1750. * call-seq:
  1751. * FileBuilderContext.add_message(name, &block)
  1752. *
  1753. * Creates a new, empty descriptor with the given name, and invokes the block in
  1754. * the context of a MessageBuilderContext on that descriptor. The block can then
  1755. * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
  1756. * methods to define the message fields.
  1757. *
  1758. * This is the recommended, idiomatic way to build message definitions.
  1759. */
  1760. VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
  1761. VALUE args[2] = { _self, name };
  1762. VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
  1763. VALUE block = rb_block_proc();
  1764. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1765. return Qnil;
  1766. }
  1767. /*
  1768. * call-seq:
  1769. * FileBuilderContext.add_enum(name, &block)
  1770. *
  1771. * Creates a new, empty enum descriptor with the given name, and invokes the
  1772. * block in the context of an EnumBuilderContext on that descriptor. The block
  1773. * can then call EnumBuilderContext#add_value to define the enum values.
  1774. *
  1775. * This is the recommended, idiomatic way to build enum definitions.
  1776. */
  1777. VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
  1778. VALUE args[2] = { _self, name };
  1779. VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
  1780. VALUE block = rb_block_proc();
  1781. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1782. return Qnil;
  1783. }
  1784. void FileBuilderContext_build(VALUE _self) {
  1785. DEFINE_SELF(FileBuilderContext, self, _self);
  1786. DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
  1787. upb_status status;
  1788. rewrite_enum_defaults(pool->symtab, self->file_proto);
  1789. rewrite_names(_self, self->file_proto);
  1790. upb_status_clear(&status);
  1791. if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
  1792. rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
  1793. upb_status_errmsg(&status));
  1794. }
  1795. }
  1796. // -----------------------------------------------------------------------------
  1797. // Builder.
  1798. // -----------------------------------------------------------------------------
  1799. DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
  1800. void Builder_mark(void* _self) {
  1801. Builder* self = _self;
  1802. rb_gc_mark(self->descriptor_pool);
  1803. rb_gc_mark(self->default_file_builder);
  1804. }
  1805. void Builder_free(void* _self) {
  1806. xfree(_self);
  1807. }
  1808. VALUE Builder_alloc(VALUE klass) {
  1809. Builder* self = ALLOC(Builder);
  1810. VALUE ret = TypedData_Wrap_Struct(
  1811. klass, &_Builder_type, self);
  1812. self->descriptor_pool = Qnil;
  1813. self->default_file_builder = Qnil;
  1814. return ret;
  1815. }
  1816. void Builder_register(VALUE module) {
  1817. VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
  1818. rb_define_alloc_func(klass, Builder_alloc);
  1819. rb_define_method(klass, "initialize", Builder_initialize, 1);
  1820. rb_define_method(klass, "add_file", Builder_add_file, -1);
  1821. rb_define_method(klass, "add_message", Builder_add_message, 1);
  1822. rb_define_method(klass, "add_enum", Builder_add_enum, 1);
  1823. rb_gc_register_address(&cBuilder);
  1824. cBuilder = klass;
  1825. }
  1826. /*
  1827. * call-seq:
  1828. * Builder.new(descriptor_pool) => builder
  1829. *
  1830. * Creates a new Builder. A Builder can accumulate a set of new message and enum
  1831. * descriptors and atomically register them into a pool in a way that allows for
  1832. * (co)recursive type references.
  1833. */
  1834. VALUE Builder_initialize(VALUE _self, VALUE pool) {
  1835. DEFINE_SELF(Builder, self, _self);
  1836. self->descriptor_pool = pool;
  1837. self->default_file_builder = Qnil; // Created lazily if needed.
  1838. return Qnil;
  1839. }
  1840. /*
  1841. * call-seq:
  1842. * Builder.add_file(name, options = nil, &block)
  1843. *
  1844. * Creates a new, file descriptor with the given name and options and invokes
  1845. * the block in the context of a FileBuilderContext on that descriptor. The
  1846. * block can then call FileBuilderContext#add_message or
  1847. * FileBuilderContext#add_enum to define new messages or enums, respectively.
  1848. *
  1849. * This is the recommended, idiomatic way to build file descriptors.
  1850. */
  1851. VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
  1852. DEFINE_SELF(Builder, self, _self);
  1853. VALUE name, options;
  1854. VALUE ctx;
  1855. VALUE block;
  1856. rb_scan_args(argc, argv, "11", &name, &options);
  1857. {
  1858. VALUE args[3] = { self->descriptor_pool, name, options };
  1859. ctx = rb_class_new_instance(3, args, cFileBuilderContext);
  1860. }
  1861. block = rb_block_proc();
  1862. rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
  1863. FileBuilderContext_build(ctx);
  1864. return Qnil;
  1865. }
  1866. static VALUE Builder_get_default_file(VALUE _self) {
  1867. DEFINE_SELF(Builder, self, _self);
  1868. /* Lazily create only if legacy builder-level methods are called. */
  1869. if (self->default_file_builder == Qnil) {
  1870. VALUE name = rb_str_new2("ruby_default_file.proto");
  1871. VALUE args [3] = { self->descriptor_pool, name, rb_hash_new() };
  1872. self->default_file_builder =
  1873. rb_class_new_instance(3, args, cFileBuilderContext);
  1874. }
  1875. return self->default_file_builder;
  1876. }
  1877. /*
  1878. * call-seq:
  1879. * Builder.add_message(name, &block)
  1880. *
  1881. * Old and deprecated way to create a new descriptor.
  1882. * See FileBuilderContext.add_message for the recommended way.
  1883. *
  1884. * Exists for backwards compatibility to allow building descriptor pool for
  1885. * files generated by protoc which don't add messages within "add_file" block.
  1886. * Descriptors created this way get assigned to a default empty FileDescriptor.
  1887. */
  1888. VALUE Builder_add_message(VALUE _self, VALUE name) {
  1889. VALUE file_builder = Builder_get_default_file(_self);
  1890. rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
  1891. rb_block_proc());
  1892. return Qnil;
  1893. }
  1894. /*
  1895. * call-seq:
  1896. * Builder.add_enum(name, &block)
  1897. *
  1898. * Old and deprecated way to create a new enum descriptor.
  1899. * See FileBuilderContext.add_enum for the recommended way.
  1900. *
  1901. * Exists for backwards compatibility to allow building descriptor pool for
  1902. * files generated by protoc which don't add enums within "add_file" block.
  1903. * Enum descriptors created this way get assigned to a default empty
  1904. * FileDescriptor.
  1905. */
  1906. VALUE Builder_add_enum(VALUE _self, VALUE name) {
  1907. VALUE file_builder = Builder_get_default_file(_self);
  1908. rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
  1909. rb_block_proc());
  1910. return Qnil;
  1911. }
  1912. /* This method is hidden from Ruby, and only called directly from
  1913. * DescriptorPool_build(). */
  1914. VALUE Builder_build(VALUE _self) {
  1915. DEFINE_SELF(Builder, self, _self);
  1916. if (self->default_file_builder != Qnil) {
  1917. FileBuilderContext_build(self->default_file_builder);
  1918. self->default_file_builder = Qnil;
  1919. }
  1920. return Qnil;
  1921. }
  1922. static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
  1923. DEFINE_SELF(DescriptorPool, descriptor_pool, _descriptor_pool);
  1924. VALUE key = ULL2NUM((intptr_t)ptr);
  1925. VALUE def;
  1926. def = rb_hash_aref(descriptor_pool->def_to_descriptor, key);
  1927. if (ptr == NULL) {
  1928. return Qnil;
  1929. }
  1930. if (def == Qnil) {
  1931. // Lazily create wrapper object.
  1932. VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
  1933. def = rb_class_new_instance(3, args, klass);
  1934. rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
  1935. // For message defs, we now eagerly get/create descriptors for all
  1936. // submessages. We will need these anyway to parse or serialize this
  1937. // message type. But more importantly, we must do this now so that
  1938. // add_handlers_for_message() (which calls get_msgdef_obj()) does *not*
  1939. // need to create a Ruby object or insert into a Ruby Hash. We need to
  1940. // avoid triggering GC, which can switch Ruby threads and re-enter our
  1941. // C extension from a different thread. This wreaks havoc on our state
  1942. // if we were in the middle of building handlers.
  1943. if (klass == cDescriptor) {
  1944. const upb_msgdef *m = ptr;
  1945. upb_msg_field_iter it;
  1946. for (upb_msg_field_begin(&it, m);
  1947. !upb_msg_field_done(&it);
  1948. upb_msg_field_next(&it)) {
  1949. const upb_fielddef* f = upb_msg_iter_field(&it);
  1950. if (upb_fielddef_issubmsg(f)) {
  1951. get_msgdef_obj(_descriptor_pool, upb_fielddef_msgsubdef(f));
  1952. }
  1953. }
  1954. }
  1955. }
  1956. return def;
  1957. }
  1958. VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
  1959. return get_def_obj(descriptor_pool, def, cDescriptor);
  1960. }
  1961. VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
  1962. return get_def_obj(descriptor_pool, def, cEnumDescriptor);
  1963. }
  1964. VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
  1965. return get_def_obj(descriptor_pool, def, cFieldDescriptor);
  1966. }
  1967. VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
  1968. return get_def_obj(descriptor_pool, def, cFileDescriptor);
  1969. }
  1970. VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
  1971. return get_def_obj(descriptor_pool, def, cOneofDescriptor);
  1972. }