storage.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. #include <math.h>
  32. #include <ruby/encoding.h>
  33. // -----------------------------------------------------------------------------
  34. // Ruby <-> native slot management.
  35. // -----------------------------------------------------------------------------
  36. #define CHARPTR_AT(msg, ofs) ((char*)msg + ofs)
  37. #define DEREF_OFFSET(msg, ofs, type) *(type*)CHARPTR_AT(msg, ofs)
  38. #define DEREF(memory, type) *(type*)(memory)
  39. size_t native_slot_size(upb_fieldtype_t type) {
  40. switch (type) {
  41. case UPB_TYPE_FLOAT: return 4;
  42. case UPB_TYPE_DOUBLE: return 8;
  43. case UPB_TYPE_BOOL: return 1;
  44. case UPB_TYPE_STRING: return sizeof(VALUE);
  45. case UPB_TYPE_BYTES: return sizeof(VALUE);
  46. case UPB_TYPE_MESSAGE: return sizeof(VALUE);
  47. case UPB_TYPE_ENUM: return 4;
  48. case UPB_TYPE_INT32: return 4;
  49. case UPB_TYPE_INT64: return 8;
  50. case UPB_TYPE_UINT32: return 4;
  51. case UPB_TYPE_UINT64: return 8;
  52. default: return 0;
  53. }
  54. }
  55. static bool is_ruby_num(VALUE value) {
  56. return (TYPE(value) == T_FLOAT ||
  57. TYPE(value) == T_FIXNUM ||
  58. TYPE(value) == T_BIGNUM);
  59. }
  60. void native_slot_check_int_range_precision(const char* name, upb_fieldtype_t type, VALUE val) {
  61. if (!is_ruby_num(val)) {
  62. rb_raise(cTypeError, "Expected number type for integral field '%s' (given %s).",
  63. name, rb_class2name(CLASS_OF(val)));
  64. }
  65. // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
  66. // bound; we just need to do precision checks (i.e., disallow rounding) and
  67. // check for < 0 on unsigned types.
  68. if (TYPE(val) == T_FLOAT) {
  69. double dbl_val = NUM2DBL(val);
  70. if (floor(dbl_val) != dbl_val) {
  71. rb_raise(rb_eRangeError,
  72. "Non-integral floating point value assigned to integer field '%s' (given %s).",
  73. name, rb_class2name(CLASS_OF(val)));
  74. }
  75. }
  76. if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
  77. if (NUM2DBL(val) < 0) {
  78. rb_raise(rb_eRangeError,
  79. "Assigning negative value to unsigned integer field '%s' (given %s).",
  80. name, rb_class2name(CLASS_OF(val)));
  81. }
  82. }
  83. }
  84. VALUE native_slot_encode_and_freeze_string(upb_fieldtype_t type, VALUE value) {
  85. rb_encoding* desired_encoding = (type == UPB_TYPE_STRING) ?
  86. kRubyStringUtf8Encoding : kRubyString8bitEncoding;
  87. VALUE desired_encoding_value = rb_enc_from_encoding(desired_encoding);
  88. if (rb_obj_encoding(value) != desired_encoding_value || !OBJ_FROZEN(value)) {
  89. // Note: this will not duplicate underlying string data unless necessary.
  90. value = rb_str_encode(value, desired_encoding_value, 0, Qnil);
  91. if (type == UPB_TYPE_STRING &&
  92. rb_enc_str_coderange(value) == ENC_CODERANGE_BROKEN) {
  93. rb_raise(rb_eEncodingError, "String is invalid UTF-8");
  94. }
  95. // Ensure the data remains valid. Since we called #encode a moment ago,
  96. // this does not freeze the string the user assigned.
  97. rb_obj_freeze(value);
  98. }
  99. return value;
  100. }
  101. void native_slot_set(const char* name,
  102. upb_fieldtype_t type, VALUE type_class,
  103. void* memory, VALUE value) {
  104. native_slot_set_value_and_case(name, type, type_class, memory, value, NULL, 0);
  105. }
  106. void native_slot_set_value_and_case(const char* name,
  107. upb_fieldtype_t type, VALUE type_class,
  108. void* memory, VALUE value,
  109. uint32_t* case_memory,
  110. uint32_t case_number) {
  111. // Note that in order to atomically change the value in memory and the case
  112. // value (w.r.t. Ruby VM calls), we must set the value at |memory| only after
  113. // all Ruby VM calls are complete. The case is then set at the bottom of this
  114. // function.
  115. switch (type) {
  116. case UPB_TYPE_FLOAT:
  117. if (!is_ruby_num(value)) {
  118. rb_raise(cTypeError, "Expected number type for float field '%s' (given %s).",
  119. name, rb_class2name(CLASS_OF(value)));
  120. }
  121. DEREF(memory, float) = NUM2DBL(value);
  122. break;
  123. case UPB_TYPE_DOUBLE:
  124. if (!is_ruby_num(value)) {
  125. rb_raise(cTypeError, "Expected number type for double field '%s' (given %s).",
  126. name, rb_class2name(CLASS_OF(value)));
  127. }
  128. DEREF(memory, double) = NUM2DBL(value);
  129. break;
  130. case UPB_TYPE_BOOL: {
  131. int8_t val = -1;
  132. if (value == Qtrue) {
  133. val = 1;
  134. } else if (value == Qfalse) {
  135. val = 0;
  136. } else {
  137. rb_raise(cTypeError, "Invalid argument for boolean field '%s' (given %s).",
  138. name, rb_class2name(CLASS_OF(value)));
  139. }
  140. DEREF(memory, int8_t) = val;
  141. break;
  142. }
  143. case UPB_TYPE_STRING:
  144. if (CLASS_OF(value) == rb_cSymbol) {
  145. value = rb_funcall(value, rb_intern("to_s"), 0);
  146. } else if (CLASS_OF(value) != rb_cString) {
  147. rb_raise(cTypeError, "Invalid argument for string field '%s' (given %s).",
  148. name, rb_class2name(CLASS_OF(value)));
  149. }
  150. DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
  151. break;
  152. case UPB_TYPE_BYTES: {
  153. if (CLASS_OF(value) != rb_cString) {
  154. rb_raise(cTypeError, "Invalid argument for bytes field '%s' (given %s).",
  155. name, rb_class2name(CLASS_OF(value)));
  156. }
  157. DEREF(memory, VALUE) = native_slot_encode_and_freeze_string(type, value);
  158. break;
  159. }
  160. case UPB_TYPE_MESSAGE: {
  161. if (CLASS_OF(value) == CLASS_OF(Qnil)) {
  162. value = Qnil;
  163. } else if (CLASS_OF(value) != type_class) {
  164. // check for possible implicit conversions
  165. VALUE converted_value = Qnil;
  166. const char* field_type_name = rb_class2name(type_class);
  167. if (strcmp(field_type_name, "Google::Protobuf::Timestamp") == 0 &&
  168. rb_obj_is_kind_of(value, rb_cTime)) {
  169. // Time -> Google::Protobuf::Timestamp
  170. VALUE hash = rb_hash_new();
  171. rb_hash_aset(hash, rb_str_new2("seconds"),
  172. rb_funcall(value, rb_intern("to_i"), 0));
  173. rb_hash_aset(hash, rb_str_new2("nanos"),
  174. rb_funcall(value, rb_intern("nsec"), 0));
  175. {
  176. VALUE args[1] = {hash};
  177. converted_value = rb_class_new_instance(1, args, type_class);
  178. }
  179. } else if (strcmp(field_type_name, "Google::Protobuf::Duration") == 0 &&
  180. rb_obj_is_kind_of(value, rb_cNumeric)) {
  181. // Numeric -> Google::Protobuf::Duration
  182. VALUE hash = rb_hash_new();
  183. rb_hash_aset(hash, rb_str_new2("seconds"),
  184. rb_funcall(value, rb_intern("to_i"), 0));
  185. {
  186. VALUE n_value =
  187. rb_funcall(value, rb_intern("remainder"), 1, INT2NUM(1));
  188. n_value =
  189. rb_funcall(n_value, rb_intern("*"), 1, INT2NUM(1000000000));
  190. n_value = rb_funcall(n_value, rb_intern("round"), 0);
  191. rb_hash_aset(hash, rb_str_new2("nanos"), n_value);
  192. }
  193. {
  194. VALUE args[1] = { hash };
  195. converted_value = rb_class_new_instance(1, args, type_class);
  196. }
  197. }
  198. // raise if no suitable conversaion could be found
  199. if (converted_value == Qnil) {
  200. rb_raise(cTypeError,
  201. "Invalid type %s to assign to submessage field '%s'.",
  202. rb_class2name(CLASS_OF(value)), name);
  203. } else {
  204. value = converted_value;
  205. }
  206. }
  207. DEREF(memory, VALUE) = value;
  208. break;
  209. }
  210. case UPB_TYPE_ENUM: {
  211. int32_t int_val = 0;
  212. if (TYPE(value) == T_STRING) {
  213. value = rb_funcall(value, rb_intern("to_sym"), 0);
  214. } else if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  215. rb_raise(cTypeError,
  216. "Expected number or symbol type for enum field '%s'.", name);
  217. }
  218. if (TYPE(value) == T_SYMBOL) {
  219. // Ensure that the given symbol exists in the enum module.
  220. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  221. if (lookup == Qnil) {
  222. rb_raise(rb_eRangeError, "Unknown symbol value for enum field '%s'.", name);
  223. } else {
  224. int_val = NUM2INT(lookup);
  225. }
  226. } else {
  227. native_slot_check_int_range_precision(name, UPB_TYPE_INT32, value);
  228. int_val = NUM2INT(value);
  229. }
  230. DEREF(memory, int32_t) = int_val;
  231. break;
  232. }
  233. case UPB_TYPE_INT32:
  234. case UPB_TYPE_INT64:
  235. case UPB_TYPE_UINT32:
  236. case UPB_TYPE_UINT64:
  237. native_slot_check_int_range_precision(name, type, value);
  238. switch (type) {
  239. case UPB_TYPE_INT32:
  240. DEREF(memory, int32_t) = NUM2INT(value);
  241. break;
  242. case UPB_TYPE_INT64:
  243. DEREF(memory, int64_t) = NUM2LL(value);
  244. break;
  245. case UPB_TYPE_UINT32:
  246. DEREF(memory, uint32_t) = NUM2UINT(value);
  247. break;
  248. case UPB_TYPE_UINT64:
  249. DEREF(memory, uint64_t) = NUM2ULL(value);
  250. break;
  251. default:
  252. break;
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. if (case_memory != NULL) {
  259. *case_memory = case_number;
  260. }
  261. }
  262. VALUE native_slot_get(upb_fieldtype_t type,
  263. VALUE type_class,
  264. const void* memory) {
  265. switch (type) {
  266. case UPB_TYPE_FLOAT:
  267. return DBL2NUM(DEREF(memory, float));
  268. case UPB_TYPE_DOUBLE:
  269. return DBL2NUM(DEREF(memory, double));
  270. case UPB_TYPE_BOOL:
  271. return DEREF(memory, int8_t) ? Qtrue : Qfalse;
  272. case UPB_TYPE_STRING:
  273. case UPB_TYPE_BYTES:
  274. return DEREF(memory, VALUE);
  275. case UPB_TYPE_MESSAGE: {
  276. VALUE val = DEREF(memory, VALUE);
  277. // Lazily expand wrapper type if necessary.
  278. int type = TYPE(val);
  279. if (type != T_DATA && type != T_NIL) {
  280. // This must be a wrapper type.
  281. val = ruby_wrapper_type(type_class, val);
  282. DEREF(memory, VALUE) = val;
  283. }
  284. return val;
  285. }
  286. case UPB_TYPE_ENUM: {
  287. int32_t val = DEREF(memory, int32_t);
  288. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  289. if (symbol == Qnil) {
  290. return INT2NUM(val);
  291. } else {
  292. return symbol;
  293. }
  294. }
  295. case UPB_TYPE_INT32:
  296. return INT2NUM(DEREF(memory, int32_t));
  297. case UPB_TYPE_INT64:
  298. return LL2NUM(DEREF(memory, int64_t));
  299. case UPB_TYPE_UINT32:
  300. return UINT2NUM(DEREF(memory, uint32_t));
  301. case UPB_TYPE_UINT64:
  302. return ULL2NUM(DEREF(memory, uint64_t));
  303. default:
  304. return Qnil;
  305. }
  306. }
  307. void native_slot_init(upb_fieldtype_t type, void* memory) {
  308. switch (type) {
  309. case UPB_TYPE_FLOAT:
  310. DEREF(memory, float) = 0.0;
  311. break;
  312. case UPB_TYPE_DOUBLE:
  313. DEREF(memory, double) = 0.0;
  314. break;
  315. case UPB_TYPE_BOOL:
  316. DEREF(memory, int8_t) = 0;
  317. break;
  318. case UPB_TYPE_STRING:
  319. case UPB_TYPE_BYTES:
  320. DEREF(memory, VALUE) = rb_str_new2("");
  321. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  322. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  323. break;
  324. case UPB_TYPE_MESSAGE:
  325. DEREF(memory, VALUE) = Qnil;
  326. break;
  327. case UPB_TYPE_ENUM:
  328. case UPB_TYPE_INT32:
  329. DEREF(memory, int32_t) = 0;
  330. break;
  331. case UPB_TYPE_INT64:
  332. DEREF(memory, int64_t) = 0;
  333. break;
  334. case UPB_TYPE_UINT32:
  335. DEREF(memory, uint32_t) = 0;
  336. break;
  337. case UPB_TYPE_UINT64:
  338. DEREF(memory, uint64_t) = 0;
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  345. switch (type) {
  346. case UPB_TYPE_STRING:
  347. case UPB_TYPE_BYTES:
  348. case UPB_TYPE_MESSAGE:
  349. rb_gc_mark(DEREF(memory, VALUE));
  350. break;
  351. default:
  352. break;
  353. }
  354. }
  355. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  356. memcpy(to, from, native_slot_size(type));
  357. }
  358. void native_slot_deep_copy(upb_fieldtype_t type, VALUE type_class, void* to,
  359. void* from) {
  360. switch (type) {
  361. case UPB_TYPE_STRING:
  362. case UPB_TYPE_BYTES: {
  363. VALUE from_val = DEREF(from, VALUE);
  364. DEREF(to, VALUE) = (from_val != Qnil) ?
  365. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  366. break;
  367. }
  368. case UPB_TYPE_MESSAGE: {
  369. VALUE from_val = native_slot_get(type, type_class, from);
  370. DEREF(to, VALUE) = (from_val != Qnil) ?
  371. Message_deep_copy(from_val) : Qnil;
  372. break;
  373. }
  374. default:
  375. memcpy(to, from, native_slot_size(type));
  376. }
  377. }
  378. bool native_slot_eq(upb_fieldtype_t type, VALUE type_class, void* mem1,
  379. void* mem2) {
  380. switch (type) {
  381. case UPB_TYPE_STRING:
  382. case UPB_TYPE_BYTES:
  383. case UPB_TYPE_MESSAGE: {
  384. VALUE val1 = native_slot_get(type, type_class, mem1);
  385. VALUE val2 = native_slot_get(type, type_class, mem2);
  386. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  387. return ret == Qtrue;
  388. }
  389. default:
  390. return !memcmp(mem1, mem2, native_slot_size(type));
  391. }
  392. }
  393. // -----------------------------------------------------------------------------
  394. // Map field utilities.
  395. // -----------------------------------------------------------------------------
  396. const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
  397. const upb_msgdef* subdef;
  398. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  399. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  400. return NULL;
  401. }
  402. subdef = upb_fielddef_msgsubdef(field);
  403. return upb_msgdef_mapentry(subdef) ? subdef : NULL;
  404. }
  405. const upb_msgdef *map_entry_msgdef(const upb_fielddef* field) {
  406. const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
  407. assert(subdef);
  408. return subdef;
  409. }
  410. bool is_map_field(const upb_fielddef *field) {
  411. const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
  412. if (subdef == NULL) return false;
  413. // Map fields are a proto3 feature.
  414. // If we're using proto2 syntax we need to fallback to the repeated field.
  415. return upb_msgdef_syntax(subdef) == UPB_SYNTAX_PROTO3;
  416. }
  417. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  418. const upb_msgdef* subdef = map_entry_msgdef(field);
  419. return map_entry_key(subdef);
  420. }
  421. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  422. const upb_msgdef* subdef = map_entry_msgdef(field);
  423. return map_entry_value(subdef);
  424. }
  425. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  426. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  427. assert(key_field != NULL);
  428. return key_field;
  429. }
  430. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  431. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  432. assert(value_field != NULL);
  433. return value_field;
  434. }
  435. // -----------------------------------------------------------------------------
  436. // Memory layout management.
  437. // -----------------------------------------------------------------------------
  438. bool field_contains_hasbit(MessageLayout* layout,
  439. const upb_fielddef* field) {
  440. return layout->fields[upb_fielddef_index(field)].hasbit !=
  441. MESSAGE_FIELD_NO_HASBIT;
  442. }
  443. static size_t align_up_to(size_t offset, size_t granularity) {
  444. // Granularity must be a power of two.
  445. return (offset + granularity - 1) & ~(granularity - 1);
  446. }
  447. bool is_value_field(const upb_fielddef* f) {
  448. return upb_fielddef_isseq(f) || upb_fielddef_issubmsg(f) ||
  449. upb_fielddef_isstring(f);
  450. }
  451. void create_layout(Descriptor* desc) {
  452. const upb_msgdef *msgdef = desc->msgdef;
  453. MessageLayout* layout = ALLOC(MessageLayout);
  454. int nfields = upb_msgdef_numfields(msgdef);
  455. int noneofs = upb_msgdef_numoneofs(msgdef);
  456. upb_msg_field_iter it;
  457. upb_msg_oneof_iter oit;
  458. size_t off = 0;
  459. size_t hasbit = 0;
  460. layout->empty_template = NULL;
  461. layout->desc = desc;
  462. desc->layout = layout;
  463. layout->fields = ALLOC_N(MessageField, nfields);
  464. layout->oneofs = NULL;
  465. if (noneofs > 0) {
  466. layout->oneofs = ALLOC_N(MessageOneof, noneofs);
  467. }
  468. for (upb_msg_field_begin(&it, msgdef);
  469. !upb_msg_field_done(&it);
  470. upb_msg_field_next(&it)) {
  471. const upb_fielddef* field = upb_msg_iter_field(&it);
  472. if (upb_fielddef_haspresence(field)) {
  473. layout->fields[upb_fielddef_index(field)].hasbit = hasbit++;
  474. } else {
  475. layout->fields[upb_fielddef_index(field)].hasbit =
  476. MESSAGE_FIELD_NO_HASBIT;
  477. }
  478. }
  479. if (hasbit != 0) {
  480. off += (hasbit + 8 - 1) / 8;
  481. }
  482. off = align_up_to(off, sizeof(VALUE));
  483. layout->value_offset = off;
  484. layout->repeated_count = 0;
  485. layout->map_count = 0;
  486. layout->value_count = 0;
  487. // Place all VALUE fields for repeated fields.
  488. for (upb_msg_field_begin(&it, msgdef);
  489. !upb_msg_field_done(&it);
  490. upb_msg_field_next(&it)) {
  491. const upb_fielddef* field = upb_msg_iter_field(&it);
  492. if (upb_fielddef_containingoneof(field) || !upb_fielddef_isseq(field) ||
  493. upb_fielddef_ismap(field)) {
  494. continue;
  495. }
  496. layout->fields[upb_fielddef_index(field)].offset = off;
  497. off += sizeof(VALUE);
  498. layout->repeated_count++;
  499. }
  500. // Place all VALUE fields for map fields.
  501. for (upb_msg_field_begin(&it, msgdef);
  502. !upb_msg_field_done(&it);
  503. upb_msg_field_next(&it)) {
  504. const upb_fielddef* field = upb_msg_iter_field(&it);
  505. if (upb_fielddef_containingoneof(field) || !upb_fielddef_isseq(field) ||
  506. !upb_fielddef_ismap(field)) {
  507. continue;
  508. }
  509. layout->fields[upb_fielddef_index(field)].offset = off;
  510. off += sizeof(VALUE);
  511. layout->map_count++;
  512. }
  513. layout->value_count = layout->repeated_count + layout->map_count;
  514. // Next place all other (non-oneof) VALUE fields.
  515. for (upb_msg_field_begin(&it, msgdef);
  516. !upb_msg_field_done(&it);
  517. upb_msg_field_next(&it)) {
  518. const upb_fielddef* field = upb_msg_iter_field(&it);
  519. if (upb_fielddef_containingoneof(field) || !is_value_field(field) ||
  520. upb_fielddef_isseq(field)) {
  521. continue;
  522. }
  523. layout->fields[upb_fielddef_index(field)].offset = off;
  524. off += sizeof(VALUE);
  525. layout->value_count++;
  526. }
  527. // Now place all other (non-oneof) fields.
  528. for (upb_msg_field_begin(&it, msgdef);
  529. !upb_msg_field_done(&it);
  530. upb_msg_field_next(&it)) {
  531. const upb_fielddef* field = upb_msg_iter_field(&it);
  532. size_t field_size;
  533. if (upb_fielddef_containingoneof(field) || is_value_field(field)) {
  534. continue;
  535. }
  536. // Allocate |field_size| bytes for this field in the layout.
  537. field_size = native_slot_size(upb_fielddef_type(field));
  538. // Align current offset up to |size| granularity.
  539. off = align_up_to(off, field_size);
  540. layout->fields[upb_fielddef_index(field)].offset = off;
  541. off += field_size;
  542. }
  543. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  544. // one slot per oneof.
  545. //
  546. // We assign all value slots first, then pack the 'case' fields at the end,
  547. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  548. // bytes respectively and we want to avoid alignment overhead.
  549. //
  550. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  551. // space for oneof cases is conceptually as wide as field tag numbers. In
  552. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  553. // members (8 or 16 bits respectively), so conceivably we could assign
  554. // consecutive case numbers and then pick a smaller oneof case slot size, but
  555. // the complexity to implement this indirection is probably not worthwhile.
  556. for (upb_msg_oneof_begin(&oit, msgdef);
  557. !upb_msg_oneof_done(&oit);
  558. upb_msg_oneof_next(&oit)) {
  559. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  560. upb_oneof_iter fit;
  561. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  562. // all fields.
  563. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  564. // Align the offset.
  565. off = align_up_to(off, field_size);
  566. // Assign all fields in the oneof this same offset.
  567. for (upb_oneof_begin(&fit, oneof);
  568. !upb_oneof_done(&fit);
  569. upb_oneof_next(&fit)) {
  570. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  571. layout->fields[upb_fielddef_index(field)].offset = off;
  572. layout->oneofs[upb_oneofdef_index(oneof)].offset = off;
  573. }
  574. off += field_size;
  575. }
  576. // Now the case fields.
  577. for (upb_msg_oneof_begin(&oit, msgdef);
  578. !upb_msg_oneof_done(&oit);
  579. upb_msg_oneof_next(&oit)) {
  580. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  581. size_t field_size = sizeof(uint32_t);
  582. // Align the offset.
  583. off = (off + field_size - 1) & ~(field_size - 1);
  584. layout->oneofs[upb_oneofdef_index(oneof)].case_offset = off;
  585. off += field_size;
  586. }
  587. layout->size = off;
  588. layout->msgdef = msgdef;
  589. // Create the empty message template.
  590. layout->empty_template = ALLOC_N(char, layout->size);
  591. memset(layout->empty_template, 0, layout->size);
  592. for (upb_msg_field_begin(&it, layout->msgdef);
  593. !upb_msg_field_done(&it);
  594. upb_msg_field_next(&it)) {
  595. layout_clear(layout, layout->empty_template, upb_msg_iter_field(&it));
  596. }
  597. }
  598. void free_layout(MessageLayout* layout) {
  599. xfree(layout->empty_template);
  600. xfree(layout->fields);
  601. xfree(layout->oneofs);
  602. xfree(layout);
  603. }
  604. VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field) {
  605. VALUE type_class = Qnil;
  606. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  607. VALUE submsgdesc = get_msgdef_obj(layout->desc->descriptor_pool,
  608. upb_fielddef_msgsubdef(field));
  609. type_class = Descriptor_msgclass(submsgdesc);
  610. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  611. VALUE subenumdesc = get_enumdef_obj(layout->desc->descriptor_pool,
  612. upb_fielddef_enumsubdef(field));
  613. type_class = EnumDescriptor_enummodule(subenumdesc);
  614. }
  615. return type_class;
  616. }
  617. static void* slot_memory(MessageLayout* layout,
  618. const void* storage,
  619. const upb_fielddef* field) {
  620. return ((uint8_t *)storage) +
  621. layout->fields[upb_fielddef_index(field)].offset;
  622. }
  623. static uint32_t* slot_oneof_case(MessageLayout* layout,
  624. const void* storage,
  625. const upb_oneofdef* oneof) {
  626. return (uint32_t*)(((uint8_t*)storage) +
  627. layout->oneofs[upb_oneofdef_index(oneof)].case_offset);
  628. }
  629. uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
  630. const upb_oneofdef* oneof) {
  631. uint32_t* ptr = slot_oneof_case(layout, storage, oneof);
  632. return *ptr & ~ONEOF_CASE_MASK;
  633. }
  634. static void slot_set_hasbit(MessageLayout* layout,
  635. const void* storage,
  636. const upb_fielddef* field) {
  637. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  638. assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
  639. ((uint8_t*)storage)[hasbit / 8] |= 1 << (hasbit % 8);
  640. }
  641. static void slot_clear_hasbit(MessageLayout* layout,
  642. const void* storage,
  643. const upb_fielddef* field) {
  644. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  645. assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
  646. ((uint8_t*)storage)[hasbit / 8] &= ~(1 << (hasbit % 8));
  647. }
  648. static bool slot_is_hasbit_set(MessageLayout* layout,
  649. const void* storage,
  650. const upb_fielddef* field) {
  651. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  652. if (hasbit == MESSAGE_FIELD_NO_HASBIT) {
  653. return false;
  654. }
  655. return DEREF_OFFSET(
  656. (uint8_t*)storage, hasbit / 8, char) & (1 << (hasbit % 8));
  657. }
  658. VALUE layout_has(MessageLayout* layout,
  659. const void* storage,
  660. const upb_fielddef* field) {
  661. assert(field_contains_hasbit(layout, field));
  662. return slot_is_hasbit_set(layout, storage, field) ? Qtrue : Qfalse;
  663. }
  664. void layout_clear(MessageLayout* layout,
  665. const void* storage,
  666. const upb_fielddef* field) {
  667. void* memory = slot_memory(layout, storage, field);
  668. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  669. if (field_contains_hasbit(layout, field)) {
  670. slot_clear_hasbit(layout, storage, field);
  671. }
  672. if (oneof) {
  673. uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
  674. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  675. *oneof_case = ONEOF_CASE_NONE;
  676. } else if (is_map_field(field)) {
  677. VALUE map = Qnil;
  678. const upb_fielddef* key_field = map_field_key(field);
  679. const upb_fielddef* value_field = map_field_value(field);
  680. VALUE type_class = field_type_class(layout, value_field);
  681. if (type_class != Qnil) {
  682. VALUE args[3] = {
  683. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  684. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  685. type_class,
  686. };
  687. map = rb_class_new_instance(3, args, cMap);
  688. } else {
  689. VALUE args[2] = {
  690. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  691. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  692. };
  693. map = rb_class_new_instance(2, args, cMap);
  694. }
  695. DEREF(memory, VALUE) = map;
  696. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  697. VALUE ary = Qnil;
  698. VALUE type_class = field_type_class(layout, field);
  699. if (type_class != Qnil) {
  700. VALUE args[2] = {
  701. fieldtype_to_ruby(upb_fielddef_type(field)),
  702. type_class,
  703. };
  704. ary = rb_class_new_instance(2, args, cRepeatedField);
  705. } else {
  706. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  707. ary = rb_class_new_instance(1, args, cRepeatedField);
  708. }
  709. DEREF(memory, VALUE) = ary;
  710. } else {
  711. native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
  712. field_type_class(layout, field), memory,
  713. layout_get_default(field));
  714. }
  715. }
  716. VALUE layout_get_default(const upb_fielddef *field) {
  717. switch (upb_fielddef_type(field)) {
  718. case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field));
  719. case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field));
  720. case UPB_TYPE_BOOL:
  721. return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
  722. case UPB_TYPE_MESSAGE: return Qnil;
  723. case UPB_TYPE_ENUM: {
  724. const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
  725. int32_t num = upb_fielddef_defaultint32(field);
  726. const char *label = upb_enumdef_iton(enumdef, num);
  727. if (label) {
  728. return ID2SYM(rb_intern(label));
  729. } else {
  730. return INT2NUM(num);
  731. }
  732. }
  733. case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field));
  734. case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));;
  735. case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field));
  736. case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field));
  737. case UPB_TYPE_STRING:
  738. case UPB_TYPE_BYTES: {
  739. size_t size;
  740. const char *str = upb_fielddef_defaultstr(field, &size);
  741. return get_frozen_string(str, size,
  742. upb_fielddef_type(field) == UPB_TYPE_BYTES);
  743. }
  744. default: return Qnil;
  745. }
  746. }
  747. VALUE layout_get(MessageLayout* layout,
  748. const void* storage,
  749. const upb_fielddef* field) {
  750. void* memory = slot_memory(layout, storage, field);
  751. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  752. bool field_set;
  753. if (field_contains_hasbit(layout, field)) {
  754. field_set = slot_is_hasbit_set(layout, storage, field);
  755. } else {
  756. field_set = true;
  757. }
  758. if (oneof) {
  759. uint32_t oneof_case = slot_read_oneof_case(layout, storage, oneof);
  760. if (oneof_case != upb_fielddef_number(field)) {
  761. return layout_get_default(field);
  762. }
  763. return native_slot_get(upb_fielddef_type(field),
  764. field_type_class(layout, field), memory);
  765. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  766. return *((VALUE *)memory);
  767. } else if (!field_set) {
  768. return layout_get_default(field);
  769. } else {
  770. return native_slot_get(upb_fielddef_type(field),
  771. field_type_class(layout, field), memory);
  772. }
  773. }
  774. static void check_repeated_field_type(const MessageLayout* layout, VALUE val,
  775. const upb_fielddef* field) {
  776. RepeatedField* self;
  777. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  778. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  779. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  780. rb_raise(cTypeError, "Expected repeated field array");
  781. }
  782. self = ruby_to_RepeatedField(val);
  783. if (self->field_type != upb_fielddef_type(field)) {
  784. rb_raise(cTypeError, "Repeated field array has wrong element type");
  785. }
  786. if (self->field_type_class != field_type_class(layout, field)) {
  787. rb_raise(cTypeError, "Repeated field array has wrong message/enum class");
  788. }
  789. }
  790. static void check_map_field_type(const MessageLayout* layout, VALUE val,
  791. const upb_fielddef* field) {
  792. const upb_fielddef* key_field = map_field_key(field);
  793. const upb_fielddef* value_field = map_field_value(field);
  794. Map* self;
  795. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  796. RTYPEDDATA_TYPE(val) != &Map_type) {
  797. rb_raise(cTypeError, "Expected Map instance");
  798. }
  799. self = ruby_to_Map(val);
  800. if (self->key_type != upb_fielddef_type(key_field)) {
  801. rb_raise(cTypeError, "Map key type does not match field's key type");
  802. }
  803. if (self->value_type != upb_fielddef_type(value_field)) {
  804. rb_raise(cTypeError, "Map value type does not match field's value type");
  805. }
  806. if (self->value_type_class != field_type_class(layout, value_field)) {
  807. rb_raise(cTypeError, "Map value type has wrong message/enum class");
  808. }
  809. }
  810. void layout_set(MessageLayout* layout,
  811. void* storage,
  812. const upb_fielddef* field,
  813. VALUE val) {
  814. void* memory = slot_memory(layout, storage, field);
  815. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  816. if (oneof) {
  817. uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
  818. if (val == Qnil) {
  819. // Assigning nil to a oneof field clears the oneof completely.
  820. *oneof_case = ONEOF_CASE_NONE;
  821. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  822. } else {
  823. // The transition between field types for a single oneof (union) slot is
  824. // somewhat complex because we need to ensure that a GC triggered at any
  825. // point by a call into the Ruby VM sees a valid state for this field and
  826. // does not either go off into the weeds (following what it thinks is a
  827. // VALUE but is actually a different field type) or miss an object (seeing
  828. // what it thinks is a primitive field but is actually a VALUE for the new
  829. // field type).
  830. //
  831. // In order for the transition to be safe, the oneof case slot must be in
  832. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  833. // use native_slot_set_value_and_case(), which ensures that both the value
  834. // and case number are altered atomically (w.r.t. the Ruby VM).
  835. uint32_t case_value = upb_fielddef_number(field);
  836. if (upb_fielddef_issubmsg(field) || upb_fielddef_isstring(field)) {
  837. case_value |= ONEOF_CASE_MASK;
  838. }
  839. native_slot_set_value_and_case(
  840. upb_fielddef_name(field), upb_fielddef_type(field),
  841. field_type_class(layout, field), memory, val, oneof_case, case_value);
  842. }
  843. } else if (is_map_field(field)) {
  844. check_map_field_type(layout, val, field);
  845. DEREF(memory, VALUE) = val;
  846. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  847. check_repeated_field_type(layout, val, field);
  848. DEREF(memory, VALUE) = val;
  849. } else {
  850. native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
  851. field_type_class(layout, field), memory, val);
  852. }
  853. if (layout->fields[upb_fielddef_index(field)].hasbit !=
  854. MESSAGE_FIELD_NO_HASBIT) {
  855. slot_set_hasbit(layout, storage, field);
  856. }
  857. }
  858. void layout_init(MessageLayout* layout, void* storage) {
  859. VALUE* value = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
  860. int i;
  861. for (i = 0; i < layout->repeated_count; i++, value++) {
  862. *value = RepeatedField_new_this_type(*value);
  863. }
  864. for (i = 0; i < layout->map_count; i++, value++) {
  865. *value = Map_new_this_type(*value);
  866. }
  867. }
  868. void layout_mark(MessageLayout* layout, void* storage) {
  869. VALUE* values = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
  870. int noneofs = upb_msgdef_numoneofs(layout->msgdef);
  871. int i;
  872. for (i = 0; i < layout->value_count; i++) {
  873. rb_gc_mark(values[i]);
  874. }
  875. for (i = 0; i < noneofs; i++) {
  876. MessageOneof* oneof = &layout->oneofs[i];
  877. uint32_t* case_ptr = (uint32_t*)CHARPTR_AT(storage, oneof->case_offset);
  878. if (*case_ptr & ONEOF_CASE_MASK) {
  879. rb_gc_mark(DEREF_OFFSET(storage, oneof->offset, VALUE));
  880. }
  881. }
  882. }
  883. void layout_dup(MessageLayout* layout, void* to, void* from) {
  884. upb_msg_field_iter it;
  885. for (upb_msg_field_begin(&it, layout->msgdef);
  886. !upb_msg_field_done(&it);
  887. upb_msg_field_next(&it)) {
  888. const upb_fielddef* field = upb_msg_iter_field(&it);
  889. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  890. void* to_memory = slot_memory(layout, to, field);
  891. void* from_memory = slot_memory(layout, from, field);
  892. if (oneof) {
  893. uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
  894. uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
  895. if (slot_read_oneof_case(layout, from, oneof) ==
  896. upb_fielddef_number(field)) {
  897. *to_oneof_case = *from_oneof_case;
  898. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  899. }
  900. } else if (is_map_field(field)) {
  901. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  902. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  903. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  904. } else {
  905. if (field_contains_hasbit(layout, field)) {
  906. if (!slot_is_hasbit_set(layout, from, field)) continue;
  907. slot_set_hasbit(layout, to, field);
  908. }
  909. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  910. }
  911. }
  912. }
  913. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  914. upb_msg_field_iter it;
  915. for (upb_msg_field_begin(&it, layout->msgdef);
  916. !upb_msg_field_done(&it);
  917. upb_msg_field_next(&it)) {
  918. const upb_fielddef* field = upb_msg_iter_field(&it);
  919. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  920. void* to_memory = slot_memory(layout, to, field);
  921. void* from_memory = slot_memory(layout, from, field);
  922. if (oneof) {
  923. uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
  924. uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
  925. if (slot_read_oneof_case(layout, from, oneof) ==
  926. upb_fielddef_number(field)) {
  927. *to_oneof_case = *from_oneof_case;
  928. native_slot_deep_copy(upb_fielddef_type(field),
  929. field_type_class(layout, field), to_memory,
  930. from_memory);
  931. }
  932. } else if (is_map_field(field)) {
  933. DEREF(to_memory, VALUE) =
  934. Map_deep_copy(DEREF(from_memory, VALUE));
  935. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  936. DEREF(to_memory, VALUE) =
  937. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  938. } else {
  939. if (field_contains_hasbit(layout, field)) {
  940. if (!slot_is_hasbit_set(layout, from, field)) continue;
  941. slot_set_hasbit(layout, to, field);
  942. }
  943. native_slot_deep_copy(upb_fielddef_type(field),
  944. field_type_class(layout, field), to_memory,
  945. from_memory);
  946. }
  947. }
  948. }
  949. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  950. upb_msg_field_iter it;
  951. for (upb_msg_field_begin(&it, layout->msgdef);
  952. !upb_msg_field_done(&it);
  953. upb_msg_field_next(&it)) {
  954. const upb_fielddef* field = upb_msg_iter_field(&it);
  955. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  956. void* msg1_memory = slot_memory(layout, msg1, field);
  957. void* msg2_memory = slot_memory(layout, msg2, field);
  958. if (oneof) {
  959. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, oneof);
  960. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, oneof);
  961. if (*msg1_oneof_case != *msg2_oneof_case ||
  962. (slot_read_oneof_case(layout, msg1, oneof) ==
  963. upb_fielddef_number(field) &&
  964. !native_slot_eq(upb_fielddef_type(field),
  965. field_type_class(layout, field), msg1_memory,
  966. msg2_memory))) {
  967. return Qfalse;
  968. }
  969. } else if (is_map_field(field)) {
  970. if (!Map_eq(DEREF(msg1_memory, VALUE),
  971. DEREF(msg2_memory, VALUE))) {
  972. return Qfalse;
  973. }
  974. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  975. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  976. DEREF(msg2_memory, VALUE))) {
  977. return Qfalse;
  978. }
  979. } else {
  980. if (slot_is_hasbit_set(layout, msg1, field) !=
  981. slot_is_hasbit_set(layout, msg2, field) ||
  982. !native_slot_eq(upb_fielddef_type(field),
  983. field_type_class(layout, field), msg1_memory,
  984. msg2_memory)) {
  985. return Qfalse;
  986. }
  987. }
  988. }
  989. return Qtrue;
  990. }
  991. VALUE layout_hash(MessageLayout* layout, void* storage) {
  992. upb_msg_field_iter it;
  993. st_index_t h = rb_hash_start(0);
  994. VALUE hash_sym = rb_intern("hash");
  995. for (upb_msg_field_begin(&it, layout->msgdef);
  996. !upb_msg_field_done(&it);
  997. upb_msg_field_next(&it)) {
  998. const upb_fielddef* field = upb_msg_iter_field(&it);
  999. VALUE field_val = layout_get(layout, storage, field);
  1000. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  1001. }
  1002. h = rb_hash_end(h);
  1003. return INT2FIX(h);
  1004. }
  1005. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  1006. VALUE str = rb_str_new2("");
  1007. upb_msg_field_iter it;
  1008. bool first = true;
  1009. for (upb_msg_field_begin(&it, layout->msgdef);
  1010. !upb_msg_field_done(&it);
  1011. upb_msg_field_next(&it)) {
  1012. const upb_fielddef* field = upb_msg_iter_field(&it);
  1013. VALUE field_val = layout_get(layout, storage, field);
  1014. if (!first) {
  1015. str = rb_str_cat2(str, ", ");
  1016. } else {
  1017. first = false;
  1018. }
  1019. str = rb_str_cat2(str, upb_fielddef_name(field));
  1020. str = rb_str_cat2(str, ": ");
  1021. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  1022. }
  1023. return str;
  1024. }