storage.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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_numrealoneofs(msgdef);
  456. upb_msg_field_iter it;
  457. upb_msg_oneof_iter oit;
  458. size_t off = 0;
  459. size_t hasbit = 0;
  460. int i;
  461. (void)i;
  462. layout->empty_template = NULL;
  463. layout->desc = desc;
  464. desc->layout = layout;
  465. layout->fields = ALLOC_N(MessageField, nfields);
  466. layout->oneofs = NULL;
  467. if (noneofs > 0) {
  468. layout->oneofs = ALLOC_N(MessageOneof, noneofs);
  469. }
  470. #ifndef NDEBUG
  471. for (i = 0; i < nfields; i++) {
  472. layout->fields[i].offset = -1;
  473. }
  474. for (i = 0; i < noneofs; i++) {
  475. layout->oneofs[i].offset = -1;
  476. }
  477. #endif
  478. for (upb_msg_field_begin(&it, msgdef);
  479. !upb_msg_field_done(&it);
  480. upb_msg_field_next(&it)) {
  481. const upb_fielddef* field = upb_msg_iter_field(&it);
  482. if (upb_fielddef_haspresence(field) &&
  483. !upb_fielddef_realcontainingoneof(field)) {
  484. layout->fields[upb_fielddef_index(field)].hasbit = hasbit++;
  485. } else {
  486. layout->fields[upb_fielddef_index(field)].hasbit =
  487. MESSAGE_FIELD_NO_HASBIT;
  488. }
  489. }
  490. if (hasbit != 0) {
  491. off += (hasbit + 8 - 1) / 8;
  492. }
  493. off = align_up_to(off, sizeof(VALUE));
  494. layout->value_offset = off;
  495. layout->repeated_count = 0;
  496. layout->map_count = 0;
  497. layout->value_count = 0;
  498. // Place all VALUE fields for repeated fields.
  499. for (upb_msg_field_begin(&it, msgdef);
  500. !upb_msg_field_done(&it);
  501. upb_msg_field_next(&it)) {
  502. const upb_fielddef* field = upb_msg_iter_field(&it);
  503. if (upb_fielddef_realcontainingoneof(field) || !upb_fielddef_isseq(field) ||
  504. upb_fielddef_ismap(field)) {
  505. continue;
  506. }
  507. layout->fields[upb_fielddef_index(field)].offset = off;
  508. off += sizeof(VALUE);
  509. layout->repeated_count++;
  510. }
  511. // Place all VALUE fields for map fields.
  512. for (upb_msg_field_begin(&it, msgdef);
  513. !upb_msg_field_done(&it);
  514. upb_msg_field_next(&it)) {
  515. const upb_fielddef* field = upb_msg_iter_field(&it);
  516. if (upb_fielddef_realcontainingoneof(field) || !upb_fielddef_isseq(field) ||
  517. !upb_fielddef_ismap(field)) {
  518. continue;
  519. }
  520. layout->fields[upb_fielddef_index(field)].offset = off;
  521. off += sizeof(VALUE);
  522. layout->map_count++;
  523. }
  524. layout->value_count = layout->repeated_count + layout->map_count;
  525. // Next place all other (non-oneof) VALUE fields.
  526. for (upb_msg_field_begin(&it, msgdef);
  527. !upb_msg_field_done(&it);
  528. upb_msg_field_next(&it)) {
  529. const upb_fielddef* field = upb_msg_iter_field(&it);
  530. if (upb_fielddef_realcontainingoneof(field) || !is_value_field(field) ||
  531. upb_fielddef_isseq(field)) {
  532. continue;
  533. }
  534. layout->fields[upb_fielddef_index(field)].offset = off;
  535. off += sizeof(VALUE);
  536. layout->value_count++;
  537. }
  538. // Now place all other (non-oneof) fields.
  539. for (upb_msg_field_begin(&it, msgdef);
  540. !upb_msg_field_done(&it);
  541. upb_msg_field_next(&it)) {
  542. const upb_fielddef* field = upb_msg_iter_field(&it);
  543. size_t field_size;
  544. if (upb_fielddef_realcontainingoneof(field) || is_value_field(field)) {
  545. continue;
  546. }
  547. // Allocate |field_size| bytes for this field in the layout.
  548. field_size = native_slot_size(upb_fielddef_type(field));
  549. // Align current offset up to |size| granularity.
  550. off = align_up_to(off, field_size);
  551. layout->fields[upb_fielddef_index(field)].offset = off;
  552. off += field_size;
  553. }
  554. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  555. // one slot per oneof.
  556. //
  557. // We assign all value slots first, then pack the 'case' fields at the end,
  558. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  559. // bytes respectively and we want to avoid alignment overhead.
  560. //
  561. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  562. // space for oneof cases is conceptually as wide as field tag numbers. In
  563. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  564. // members (8 or 16 bits respectively), so conceivably we could assign
  565. // consecutive case numbers and then pick a smaller oneof case slot size, but
  566. // the complexity to implement this indirection is probably not worthwhile.
  567. for (upb_msg_oneof_begin(&oit, msgdef);
  568. !upb_msg_oneof_done(&oit);
  569. upb_msg_oneof_next(&oit)) {
  570. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  571. upb_oneof_iter fit;
  572. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  573. // all fields.
  574. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  575. if (upb_oneofdef_issynthetic(oneof)) continue;
  576. assert(upb_oneofdef_index(oneof) < noneofs);
  577. // Align the offset.
  578. off = align_up_to(off, field_size);
  579. // Assign all fields in the oneof this same offset.
  580. for (upb_oneof_begin(&fit, oneof);
  581. !upb_oneof_done(&fit);
  582. upb_oneof_next(&fit)) {
  583. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  584. layout->fields[upb_fielddef_index(field)].offset = off;
  585. layout->oneofs[upb_oneofdef_index(oneof)].offset = off;
  586. }
  587. off += field_size;
  588. }
  589. // Now the case fields.
  590. for (upb_msg_oneof_begin(&oit, msgdef);
  591. !upb_msg_oneof_done(&oit);
  592. upb_msg_oneof_next(&oit)) {
  593. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  594. size_t field_size = sizeof(uint32_t);
  595. if (upb_oneofdef_issynthetic(oneof)) continue;
  596. assert(upb_oneofdef_index(oneof) < noneofs);
  597. // Align the offset.
  598. off = (off + field_size - 1) & ~(field_size - 1);
  599. layout->oneofs[upb_oneofdef_index(oneof)].case_offset = off;
  600. off += field_size;
  601. }
  602. layout->size = off;
  603. layout->msgdef = msgdef;
  604. #ifndef NDEBUG
  605. for (i = 0; i < nfields; i++) {
  606. assert(layout->fields[i].offset != -1);
  607. }
  608. for (i = 0; i < noneofs; i++) {
  609. assert(layout->oneofs[i].offset != -1);
  610. }
  611. #endif
  612. // Create the empty message template.
  613. layout->empty_template = ALLOC_N(char, layout->size);
  614. memset(layout->empty_template, 0, layout->size);
  615. for (upb_msg_field_begin(&it, layout->msgdef);
  616. !upb_msg_field_done(&it);
  617. upb_msg_field_next(&it)) {
  618. layout_clear(layout, layout->empty_template, upb_msg_iter_field(&it));
  619. }
  620. }
  621. void free_layout(MessageLayout* layout) {
  622. xfree(layout->empty_template);
  623. xfree(layout->fields);
  624. xfree(layout->oneofs);
  625. xfree(layout);
  626. }
  627. VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field) {
  628. VALUE type_class = Qnil;
  629. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  630. VALUE submsgdesc = get_msgdef_obj(layout->desc->descriptor_pool,
  631. upb_fielddef_msgsubdef(field));
  632. type_class = Descriptor_msgclass(submsgdesc);
  633. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  634. VALUE subenumdesc = get_enumdef_obj(layout->desc->descriptor_pool,
  635. upb_fielddef_enumsubdef(field));
  636. type_class = EnumDescriptor_enummodule(subenumdesc);
  637. }
  638. return type_class;
  639. }
  640. static void* slot_memory(MessageLayout* layout,
  641. const void* storage,
  642. const upb_fielddef* field) {
  643. return ((uint8_t *)storage) +
  644. layout->fields[upb_fielddef_index(field)].offset;
  645. }
  646. static uint32_t* slot_oneof_case(MessageLayout* layout,
  647. const void* storage,
  648. const upb_oneofdef* oneof) {
  649. return (uint32_t*)(((uint8_t*)storage) +
  650. layout->oneofs[upb_oneofdef_index(oneof)].case_offset);
  651. }
  652. uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
  653. const upb_oneofdef* oneof) {
  654. uint32_t* ptr = slot_oneof_case(layout, storage, oneof);
  655. return *ptr & ~ONEOF_CASE_MASK;
  656. }
  657. static void slot_set_hasbit(MessageLayout* layout,
  658. const void* storage,
  659. const upb_fielddef* field) {
  660. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  661. assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
  662. ((uint8_t*)storage)[hasbit / 8] |= 1 << (hasbit % 8);
  663. }
  664. static void slot_clear_hasbit(MessageLayout* layout,
  665. const void* storage,
  666. const upb_fielddef* field) {
  667. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  668. assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
  669. ((uint8_t*)storage)[hasbit / 8] &= ~(1 << (hasbit % 8));
  670. }
  671. static bool slot_is_hasbit_set(MessageLayout* layout,
  672. const void* storage,
  673. const upb_fielddef* field) {
  674. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  675. assert(field_contains_hasbit(layout, field));
  676. return DEREF_OFFSET(
  677. (uint8_t*)storage, hasbit / 8, char) & (1 << (hasbit % 8));
  678. }
  679. VALUE layout_has(MessageLayout* layout,
  680. const void* storage,
  681. const upb_fielddef* field) {
  682. const upb_oneofdef* oneof = upb_fielddef_realcontainingoneof(field);
  683. assert(upb_fielddef_haspresence(field));
  684. if (oneof) {
  685. uint32_t oneof_case = slot_read_oneof_case(layout, storage, oneof);
  686. return oneof_case == upb_fielddef_number(field) ? Qtrue : Qfalse;
  687. } else {
  688. return slot_is_hasbit_set(layout, storage, field) ? Qtrue : Qfalse;
  689. }
  690. }
  691. void layout_clear(MessageLayout* layout,
  692. const void* storage,
  693. const upb_fielddef* field) {
  694. void* memory = slot_memory(layout, storage, field);
  695. const upb_oneofdef* oneof = upb_fielddef_realcontainingoneof(field);
  696. if (field_contains_hasbit(layout, field)) {
  697. slot_clear_hasbit(layout, storage, field);
  698. }
  699. if (oneof) {
  700. uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
  701. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  702. *oneof_case = ONEOF_CASE_NONE;
  703. } else if (is_map_field(field)) {
  704. VALUE map = Qnil;
  705. const upb_fielddef* key_field = map_field_key(field);
  706. const upb_fielddef* value_field = map_field_value(field);
  707. VALUE type_class = field_type_class(layout, value_field);
  708. if (type_class != Qnil) {
  709. VALUE args[3] = {
  710. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  711. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  712. type_class,
  713. };
  714. map = rb_class_new_instance(3, args, cMap);
  715. } else {
  716. VALUE args[2] = {
  717. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  718. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  719. };
  720. map = rb_class_new_instance(2, args, cMap);
  721. }
  722. DEREF(memory, VALUE) = map;
  723. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  724. VALUE ary = Qnil;
  725. VALUE type_class = field_type_class(layout, field);
  726. if (type_class != Qnil) {
  727. VALUE args[2] = {
  728. fieldtype_to_ruby(upb_fielddef_type(field)),
  729. type_class,
  730. };
  731. ary = rb_class_new_instance(2, args, cRepeatedField);
  732. } else {
  733. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  734. ary = rb_class_new_instance(1, args, cRepeatedField);
  735. }
  736. DEREF(memory, VALUE) = ary;
  737. } else {
  738. native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
  739. field_type_class(layout, field), memory,
  740. layout_get_default(field));
  741. }
  742. }
  743. VALUE layout_get_default(const upb_fielddef *field) {
  744. switch (upb_fielddef_type(field)) {
  745. case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field));
  746. case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field));
  747. case UPB_TYPE_BOOL:
  748. return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
  749. case UPB_TYPE_MESSAGE: return Qnil;
  750. case UPB_TYPE_ENUM: {
  751. const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
  752. int32_t num = upb_fielddef_defaultint32(field);
  753. const char *label = upb_enumdef_iton(enumdef, num);
  754. if (label) {
  755. return ID2SYM(rb_intern(label));
  756. } else {
  757. return INT2NUM(num);
  758. }
  759. }
  760. case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field));
  761. case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));;
  762. case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field));
  763. case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field));
  764. case UPB_TYPE_STRING:
  765. case UPB_TYPE_BYTES: {
  766. size_t size;
  767. const char *str = upb_fielddef_defaultstr(field, &size);
  768. return get_frozen_string(str, size,
  769. upb_fielddef_type(field) == UPB_TYPE_BYTES);
  770. }
  771. default: return Qnil;
  772. }
  773. }
  774. VALUE layout_get(MessageLayout* layout,
  775. const void* storage,
  776. const upb_fielddef* field) {
  777. void* memory = slot_memory(layout, storage, field);
  778. const upb_oneofdef* oneof = upb_fielddef_realcontainingoneof(field);
  779. bool field_set;
  780. if (field_contains_hasbit(layout, field)) {
  781. field_set = slot_is_hasbit_set(layout, storage, field);
  782. } else {
  783. field_set = true;
  784. }
  785. if (oneof) {
  786. uint32_t oneof_case = slot_read_oneof_case(layout, storage, oneof);
  787. if (oneof_case != upb_fielddef_number(field)) {
  788. return layout_get_default(field);
  789. }
  790. return native_slot_get(upb_fielddef_type(field),
  791. field_type_class(layout, field), memory);
  792. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  793. return *((VALUE *)memory);
  794. } else if (!field_set) {
  795. return layout_get_default(field);
  796. } else {
  797. return native_slot_get(upb_fielddef_type(field),
  798. field_type_class(layout, field), memory);
  799. }
  800. }
  801. static void check_repeated_field_type(const MessageLayout* layout, VALUE val,
  802. const upb_fielddef* field) {
  803. RepeatedField* self;
  804. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  805. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  806. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  807. rb_raise(cTypeError, "Expected repeated field array");
  808. }
  809. self = ruby_to_RepeatedField(val);
  810. if (self->field_type != upb_fielddef_type(field)) {
  811. rb_raise(cTypeError, "Repeated field array has wrong element type");
  812. }
  813. if (self->field_type_class != field_type_class(layout, field)) {
  814. rb_raise(cTypeError, "Repeated field array has wrong message/enum class");
  815. }
  816. }
  817. static void check_map_field_type(const MessageLayout* layout, VALUE val,
  818. const upb_fielddef* field) {
  819. const upb_fielddef* key_field = map_field_key(field);
  820. const upb_fielddef* value_field = map_field_value(field);
  821. Map* self;
  822. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  823. RTYPEDDATA_TYPE(val) != &Map_type) {
  824. rb_raise(cTypeError, "Expected Map instance");
  825. }
  826. self = ruby_to_Map(val);
  827. if (self->key_type != upb_fielddef_type(key_field)) {
  828. rb_raise(cTypeError, "Map key type does not match field's key type");
  829. }
  830. if (self->value_type != upb_fielddef_type(value_field)) {
  831. rb_raise(cTypeError, "Map value type does not match field's value type");
  832. }
  833. if (self->value_type_class != field_type_class(layout, value_field)) {
  834. rb_raise(cTypeError, "Map value type has wrong message/enum class");
  835. }
  836. }
  837. void layout_set(MessageLayout* layout,
  838. void* storage,
  839. const upb_fielddef* field,
  840. VALUE val) {
  841. void* memory = slot_memory(layout, storage, field);
  842. const upb_oneofdef* oneof = upb_fielddef_realcontainingoneof(field);
  843. if (oneof) {
  844. uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
  845. if (val == Qnil) {
  846. // Assigning nil to a oneof field clears the oneof completely.
  847. *oneof_case = ONEOF_CASE_NONE;
  848. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  849. } else {
  850. // The transition between field types for a single oneof (union) slot is
  851. // somewhat complex because we need to ensure that a GC triggered at any
  852. // point by a call into the Ruby VM sees a valid state for this field and
  853. // does not either go off into the weeds (following what it thinks is a
  854. // VALUE but is actually a different field type) or miss an object (seeing
  855. // what it thinks is a primitive field but is actually a VALUE for the new
  856. // field type).
  857. //
  858. // In order for the transition to be safe, the oneof case slot must be in
  859. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  860. // use native_slot_set_value_and_case(), which ensures that both the value
  861. // and case number are altered atomically (w.r.t. the Ruby VM).
  862. uint32_t case_value = upb_fielddef_number(field);
  863. if (upb_fielddef_issubmsg(field) || upb_fielddef_isstring(field)) {
  864. case_value |= ONEOF_CASE_MASK;
  865. }
  866. native_slot_set_value_and_case(
  867. upb_fielddef_name(field), upb_fielddef_type(field),
  868. field_type_class(layout, field), memory, val, oneof_case, case_value);
  869. }
  870. } else if (is_map_field(field)) {
  871. check_map_field_type(layout, val, field);
  872. DEREF(memory, VALUE) = val;
  873. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  874. check_repeated_field_type(layout, val, field);
  875. DEREF(memory, VALUE) = val;
  876. } else {
  877. native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
  878. field_type_class(layout, field), memory, val);
  879. }
  880. if (layout->fields[upb_fielddef_index(field)].hasbit !=
  881. MESSAGE_FIELD_NO_HASBIT) {
  882. if (val == Qnil) {
  883. // No other field type has a hasbit and allows nil assignment.
  884. if (upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  885. fprintf(stderr, "field: %s\n", upb_fielddef_fullname(field));
  886. }
  887. assert(upb_fielddef_type(field) == UPB_TYPE_MESSAGE);
  888. slot_clear_hasbit(layout, storage, field);
  889. } else {
  890. slot_set_hasbit(layout, storage, field);
  891. }
  892. }
  893. }
  894. void layout_init(MessageLayout* layout, void* storage) {
  895. VALUE* value = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
  896. int i;
  897. for (i = 0; i < layout->repeated_count; i++, value++) {
  898. *value = RepeatedField_new_this_type(*value);
  899. }
  900. for (i = 0; i < layout->map_count; i++, value++) {
  901. *value = Map_new_this_type(*value);
  902. }
  903. }
  904. void layout_mark(MessageLayout* layout, void* storage) {
  905. VALUE* values = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
  906. int noneofs = upb_msgdef_numrealoneofs(layout->msgdef);
  907. int i;
  908. for (i = 0; i < layout->value_count; i++) {
  909. rb_gc_mark(values[i]);
  910. }
  911. for (i = 0; i < noneofs; i++) {
  912. MessageOneof* oneof = &layout->oneofs[i];
  913. uint32_t* case_ptr = (uint32_t*)CHARPTR_AT(storage, oneof->case_offset);
  914. if (*case_ptr & ONEOF_CASE_MASK) {
  915. rb_gc_mark(DEREF_OFFSET(storage, oneof->offset, VALUE));
  916. }
  917. }
  918. }
  919. void layout_dup(MessageLayout* layout, void* to, void* from) {
  920. upb_msg_field_iter it;
  921. for (upb_msg_field_begin(&it, layout->msgdef);
  922. !upb_msg_field_done(&it);
  923. upb_msg_field_next(&it)) {
  924. const upb_fielddef* field = upb_msg_iter_field(&it);
  925. const upb_oneofdef* oneof = upb_fielddef_realcontainingoneof(field);
  926. void* to_memory = slot_memory(layout, to, field);
  927. void* from_memory = slot_memory(layout, from, field);
  928. if (oneof) {
  929. uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
  930. uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
  931. if (slot_read_oneof_case(layout, from, oneof) ==
  932. upb_fielddef_number(field)) {
  933. *to_oneof_case = *from_oneof_case;
  934. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  935. }
  936. } else if (is_map_field(field)) {
  937. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  938. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  939. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  940. } else {
  941. if (field_contains_hasbit(layout, field)) {
  942. if (!slot_is_hasbit_set(layout, from, field)) continue;
  943. slot_set_hasbit(layout, to, field);
  944. }
  945. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  946. }
  947. }
  948. }
  949. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  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_realcontainingoneof(field);
  956. void* to_memory = slot_memory(layout, to, field);
  957. void* from_memory = slot_memory(layout, from, field);
  958. if (oneof) {
  959. uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
  960. uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
  961. if (slot_read_oneof_case(layout, from, oneof) ==
  962. upb_fielddef_number(field)) {
  963. *to_oneof_case = *from_oneof_case;
  964. native_slot_deep_copy(upb_fielddef_type(field),
  965. field_type_class(layout, field), to_memory,
  966. from_memory);
  967. }
  968. } else if (is_map_field(field)) {
  969. DEREF(to_memory, VALUE) =
  970. Map_deep_copy(DEREF(from_memory, VALUE));
  971. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  972. DEREF(to_memory, VALUE) =
  973. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  974. } else {
  975. if (field_contains_hasbit(layout, field)) {
  976. if (!slot_is_hasbit_set(layout, from, field)) continue;
  977. slot_set_hasbit(layout, to, field);
  978. }
  979. native_slot_deep_copy(upb_fielddef_type(field),
  980. field_type_class(layout, field), to_memory,
  981. from_memory);
  982. }
  983. }
  984. }
  985. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  986. upb_msg_field_iter it;
  987. for (upb_msg_field_begin(&it, layout->msgdef);
  988. !upb_msg_field_done(&it);
  989. upb_msg_field_next(&it)) {
  990. const upb_fielddef* field = upb_msg_iter_field(&it);
  991. const upb_oneofdef* oneof = upb_fielddef_realcontainingoneof(field);
  992. void* msg1_memory = slot_memory(layout, msg1, field);
  993. void* msg2_memory = slot_memory(layout, msg2, field);
  994. if (oneof) {
  995. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, oneof);
  996. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, oneof);
  997. if (*msg1_oneof_case != *msg2_oneof_case ||
  998. (slot_read_oneof_case(layout, msg1, oneof) ==
  999. upb_fielddef_number(field) &&
  1000. !native_slot_eq(upb_fielddef_type(field),
  1001. field_type_class(layout, field), msg1_memory,
  1002. msg2_memory))) {
  1003. return Qfalse;
  1004. }
  1005. } else if (is_map_field(field)) {
  1006. if (!Map_eq(DEREF(msg1_memory, VALUE),
  1007. DEREF(msg2_memory, VALUE))) {
  1008. return Qfalse;
  1009. }
  1010. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  1011. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  1012. DEREF(msg2_memory, VALUE))) {
  1013. return Qfalse;
  1014. }
  1015. } else {
  1016. if (field_contains_hasbit(layout, field) &&
  1017. slot_is_hasbit_set(layout, msg1, field) !=
  1018. slot_is_hasbit_set(layout, msg2, field)) {
  1019. // TODO(haberman): I don't think we should actually care about hasbits
  1020. // here: an unset default should be able to equal a set default. But we
  1021. // can address this later (will also have to make sure defaults are
  1022. // being properly set when hasbit is clear).
  1023. return Qfalse;
  1024. }
  1025. if (!native_slot_eq(upb_fielddef_type(field),
  1026. field_type_class(layout, field), msg1_memory,
  1027. msg2_memory)) {
  1028. return Qfalse;
  1029. }
  1030. }
  1031. }
  1032. return Qtrue;
  1033. }
  1034. VALUE layout_hash(MessageLayout* layout, void* storage) {
  1035. upb_msg_field_iter it;
  1036. st_index_t h = rb_hash_start(0);
  1037. VALUE hash_sym = rb_intern("hash");
  1038. for (upb_msg_field_begin(&it, layout->msgdef);
  1039. !upb_msg_field_done(&it);
  1040. upb_msg_field_next(&it)) {
  1041. const upb_fielddef* field = upb_msg_iter_field(&it);
  1042. VALUE field_val = layout_get(layout, storage, field);
  1043. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  1044. }
  1045. h = rb_hash_end(h);
  1046. return INT2FIX(h);
  1047. }
  1048. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  1049. VALUE str = rb_str_new2("");
  1050. upb_msg_field_iter it;
  1051. bool first = true;
  1052. for (upb_msg_field_begin(&it, layout->msgdef);
  1053. !upb_msg_field_done(&it);
  1054. upb_msg_field_next(&it)) {
  1055. const upb_fielddef* field = upb_msg_iter_field(&it);
  1056. VALUE field_val = layout_get(layout, storage, field);
  1057. if (!first) {
  1058. str = rb_str_cat2(str, ", ");
  1059. } else {
  1060. first = false;
  1061. }
  1062. str = rb_str_cat2(str, upb_fielddef_name(field));
  1063. str = rb_str_cat2(str, ": ");
  1064. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  1065. }
  1066. return str;
  1067. }