storage.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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 DEREF(memory, type) *(type*)(memory)
  37. size_t native_slot_size(upb_fieldtype_t type) {
  38. switch (type) {
  39. case UPB_TYPE_FLOAT: return 4;
  40. case UPB_TYPE_DOUBLE: return 8;
  41. case UPB_TYPE_BOOL: return 1;
  42. case UPB_TYPE_STRING: return sizeof(VALUE);
  43. case UPB_TYPE_BYTES: return sizeof(VALUE);
  44. case UPB_TYPE_MESSAGE: return sizeof(VALUE);
  45. case UPB_TYPE_ENUM: return 4;
  46. case UPB_TYPE_INT32: return 4;
  47. case UPB_TYPE_INT64: return 8;
  48. case UPB_TYPE_UINT32: return 4;
  49. case UPB_TYPE_UINT64: return 8;
  50. default: return 0;
  51. }
  52. }
  53. static bool is_ruby_num(VALUE value) {
  54. return (TYPE(value) == T_FLOAT ||
  55. TYPE(value) == T_FIXNUM ||
  56. TYPE(value) == T_BIGNUM);
  57. }
  58. void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) {
  59. if (!is_ruby_num(val)) {
  60. rb_raise(rb_eTypeError, "Expected number type for integral field.");
  61. }
  62. // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
  63. // bound; we just need to do precision checks (i.e., disallow rounding) and
  64. // check for < 0 on unsigned types.
  65. if (TYPE(val) == T_FLOAT) {
  66. double dbl_val = NUM2DBL(val);
  67. if (floor(dbl_val) != dbl_val) {
  68. rb_raise(rb_eRangeError,
  69. "Non-integral floating point value assigned to integer field.");
  70. }
  71. }
  72. if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
  73. if (NUM2DBL(val) < 0) {
  74. rb_raise(rb_eRangeError,
  75. "Assigning negative value to unsigned integer field.");
  76. }
  77. }
  78. }
  79. void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value) {
  80. bool bad_encoding = false;
  81. rb_encoding* string_encoding = rb_enc_from_index(ENCODING_GET(value));
  82. if (type == UPB_TYPE_STRING) {
  83. bad_encoding =
  84. string_encoding != kRubyStringUtf8Encoding &&
  85. string_encoding != kRubyStringASCIIEncoding;
  86. } else {
  87. bad_encoding =
  88. string_encoding != kRubyString8bitEncoding;
  89. }
  90. // Check that encoding is UTF-8 or ASCII (for string fields) or ASCII-8BIT
  91. // (for bytes fields).
  92. if (bad_encoding) {
  93. rb_raise(rb_eTypeError, "Encoding for '%s' fields must be %s (was %s)",
  94. (type == UPB_TYPE_STRING) ? "string" : "bytes",
  95. (type == UPB_TYPE_STRING) ? "UTF-8 or ASCII" : "ASCII-8BIT",
  96. rb_enc_name(string_encoding));
  97. }
  98. }
  99. void native_slot_set(upb_fieldtype_t type, VALUE type_class,
  100. void* memory, VALUE value) {
  101. switch (type) {
  102. case UPB_TYPE_FLOAT:
  103. if (!is_ruby_num(value)) {
  104. rb_raise(rb_eTypeError, "Expected number type for float field.");
  105. }
  106. DEREF(memory, float) = NUM2DBL(value);
  107. break;
  108. case UPB_TYPE_DOUBLE:
  109. if (!is_ruby_num(value)) {
  110. rb_raise(rb_eTypeError, "Expected number type for double field.");
  111. }
  112. DEREF(memory, double) = NUM2DBL(value);
  113. break;
  114. case UPB_TYPE_BOOL: {
  115. int8_t val = -1;
  116. if (value == Qtrue) {
  117. val = 1;
  118. } else if (value == Qfalse) {
  119. val = 0;
  120. } else {
  121. rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
  122. }
  123. DEREF(memory, int8_t) = val;
  124. break;
  125. }
  126. case UPB_TYPE_STRING:
  127. case UPB_TYPE_BYTES: {
  128. if (CLASS_OF(value) != rb_cString) {
  129. rb_raise(rb_eTypeError, "Invalid argument for string field.");
  130. }
  131. native_slot_validate_string_encoding(type, value);
  132. DEREF(memory, VALUE) = value;
  133. break;
  134. }
  135. case UPB_TYPE_MESSAGE: {
  136. if (CLASS_OF(value) != type_class) {
  137. rb_raise(rb_eTypeError,
  138. "Invalid type %s to assign to submessage field.",
  139. rb_class2name(CLASS_OF(value)));
  140. }
  141. DEREF(memory, VALUE) = value;
  142. break;
  143. }
  144. case UPB_TYPE_ENUM: {
  145. if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  146. rb_raise(rb_eTypeError,
  147. "Expected number or symbol type for enum field.");
  148. }
  149. int32_t int_val = 0;
  150. if (TYPE(value) == T_SYMBOL) {
  151. // Ensure that the given symbol exists in the enum module.
  152. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  153. if (lookup == Qnil) {
  154. rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
  155. } else {
  156. int_val = NUM2INT(lookup);
  157. }
  158. } else {
  159. native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
  160. int_val = NUM2INT(value);
  161. }
  162. DEREF(memory, int32_t) = int_val;
  163. break;
  164. }
  165. case UPB_TYPE_INT32:
  166. case UPB_TYPE_INT64:
  167. case UPB_TYPE_UINT32:
  168. case UPB_TYPE_UINT64:
  169. native_slot_check_int_range_precision(type, value);
  170. switch (type) {
  171. case UPB_TYPE_INT32:
  172. DEREF(memory, int32_t) = NUM2INT(value);
  173. break;
  174. case UPB_TYPE_INT64:
  175. DEREF(memory, int64_t) = NUM2LL(value);
  176. break;
  177. case UPB_TYPE_UINT32:
  178. DEREF(memory, uint32_t) = NUM2UINT(value);
  179. break;
  180. case UPB_TYPE_UINT64:
  181. DEREF(memory, uint64_t) = NUM2ULL(value);
  182. break;
  183. default:
  184. break;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. VALUE native_slot_get(upb_fieldtype_t type, VALUE type_class, void* memory) {
  192. switch (type) {
  193. case UPB_TYPE_FLOAT:
  194. return DBL2NUM(DEREF(memory, float));
  195. case UPB_TYPE_DOUBLE:
  196. return DBL2NUM(DEREF(memory, double));
  197. case UPB_TYPE_BOOL:
  198. return DEREF(memory, int8_t) ? Qtrue : Qfalse;
  199. case UPB_TYPE_STRING:
  200. case UPB_TYPE_BYTES:
  201. case UPB_TYPE_MESSAGE:
  202. return *((VALUE *)memory);
  203. case UPB_TYPE_ENUM: {
  204. int32_t val = DEREF(memory, int32_t);
  205. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  206. if (symbol == Qnil) {
  207. return INT2NUM(val);
  208. } else {
  209. return symbol;
  210. }
  211. }
  212. case UPB_TYPE_INT32:
  213. return INT2NUM(DEREF(memory, int32_t));
  214. case UPB_TYPE_INT64:
  215. return LL2NUM(DEREF(memory, int64_t));
  216. case UPB_TYPE_UINT32:
  217. return UINT2NUM(DEREF(memory, uint32_t));
  218. case UPB_TYPE_UINT64:
  219. return ULL2NUM(DEREF(memory, uint64_t));
  220. default:
  221. return Qnil;
  222. }
  223. }
  224. void native_slot_init(upb_fieldtype_t type, void* memory) {
  225. switch (type) {
  226. case UPB_TYPE_FLOAT:
  227. DEREF(memory, float) = 0.0;
  228. break;
  229. case UPB_TYPE_DOUBLE:
  230. DEREF(memory, double) = 0.0;
  231. break;
  232. case UPB_TYPE_BOOL:
  233. DEREF(memory, int8_t) = 0;
  234. break;
  235. case UPB_TYPE_STRING:
  236. case UPB_TYPE_BYTES:
  237. DEREF(memory, VALUE) = rb_str_new2("");
  238. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  239. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  240. break;
  241. case UPB_TYPE_MESSAGE:
  242. DEREF(memory, VALUE) = Qnil;
  243. break;
  244. case UPB_TYPE_ENUM:
  245. case UPB_TYPE_INT32:
  246. DEREF(memory, int32_t) = 0;
  247. break;
  248. case UPB_TYPE_INT64:
  249. DEREF(memory, int64_t) = 0;
  250. break;
  251. case UPB_TYPE_UINT32:
  252. DEREF(memory, uint32_t) = 0;
  253. break;
  254. case UPB_TYPE_UINT64:
  255. DEREF(memory, uint64_t) = 0;
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  262. switch (type) {
  263. case UPB_TYPE_STRING:
  264. case UPB_TYPE_BYTES:
  265. case UPB_TYPE_MESSAGE:
  266. rb_gc_mark(DEREF(memory, VALUE));
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  273. memcpy(to, from, native_slot_size(type));
  274. }
  275. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  276. switch (type) {
  277. case UPB_TYPE_STRING:
  278. case UPB_TYPE_BYTES: {
  279. VALUE from_val = DEREF(from, VALUE);
  280. DEREF(to, VALUE) = (from_val != Qnil) ?
  281. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  282. break;
  283. }
  284. case UPB_TYPE_MESSAGE: {
  285. VALUE from_val = DEREF(from, VALUE);
  286. DEREF(to, VALUE) = (from_val != Qnil) ?
  287. Message_deep_copy(from_val) : Qnil;
  288. break;
  289. }
  290. default:
  291. memcpy(to, from, native_slot_size(type));
  292. }
  293. }
  294. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  295. switch (type) {
  296. case UPB_TYPE_STRING:
  297. case UPB_TYPE_BYTES:
  298. case UPB_TYPE_MESSAGE: {
  299. VALUE val1 = DEREF(mem1, VALUE);
  300. VALUE val2 = DEREF(mem2, VALUE);
  301. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  302. return ret == Qtrue;
  303. }
  304. default:
  305. return !memcmp(mem1, mem2, native_slot_size(type));
  306. }
  307. }
  308. // -----------------------------------------------------------------------------
  309. // Map field utilities.
  310. // -----------------------------------------------------------------------------
  311. bool is_map_field(const upb_fielddef* field) {
  312. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  313. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  314. return false;
  315. }
  316. const upb_msgdef* subdef = (const upb_msgdef*)upb_fielddef_subdef(field);
  317. return upb_msgdef_mapentry(subdef);
  318. }
  319. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  320. assert(is_map_field(field));
  321. const upb_msgdef* subdef = (const upb_msgdef*)upb_fielddef_subdef(field);
  322. return map_entry_key(subdef);
  323. }
  324. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  325. assert(is_map_field(field));
  326. const upb_msgdef* subdef = (const upb_msgdef*)upb_fielddef_subdef(field);
  327. return map_entry_value(subdef);
  328. }
  329. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  330. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  331. assert(key_field != NULL);
  332. return key_field;
  333. }
  334. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  335. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  336. assert(value_field != NULL);
  337. return value_field;
  338. }
  339. // -----------------------------------------------------------------------------
  340. // Memory layout management.
  341. // -----------------------------------------------------------------------------
  342. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  343. MessageLayout* layout = ALLOC(MessageLayout);
  344. int nfields = upb_msgdef_numfields(msgdef);
  345. layout->offsets = ALLOC_N(size_t, nfields);
  346. upb_msg_iter it;
  347. size_t off = 0;
  348. for (upb_msg_begin(&it, msgdef); !upb_msg_done(&it); upb_msg_next(&it)) {
  349. const upb_fielddef* field = upb_msg_iter_field(&it);
  350. size_t field_size = 0;
  351. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  352. field_size = sizeof(VALUE);
  353. } else {
  354. field_size = native_slot_size(upb_fielddef_type(field));
  355. }
  356. // align current offset
  357. off = (off + field_size - 1) & ~(field_size - 1);
  358. layout->offsets[upb_fielddef_index(field)] = off;
  359. off += field_size;
  360. }
  361. layout->size = off;
  362. layout->msgdef = msgdef;
  363. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  364. return layout;
  365. }
  366. void free_layout(MessageLayout* layout) {
  367. xfree(layout->offsets);
  368. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  369. xfree(layout);
  370. }
  371. VALUE field_type_class(const upb_fielddef* field) {
  372. VALUE type_class = Qnil;
  373. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  374. VALUE submsgdesc =
  375. get_def_obj(upb_fielddef_subdef(field));
  376. type_class = Descriptor_msgclass(submsgdesc);
  377. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  378. VALUE subenumdesc =
  379. get_def_obj(upb_fielddef_subdef(field));
  380. type_class = EnumDescriptor_enummodule(subenumdesc);
  381. }
  382. return type_class;
  383. }
  384. VALUE layout_get(MessageLayout* layout,
  385. void* storage,
  386. const upb_fielddef* field) {
  387. void* memory = ((uint8_t *)storage) +
  388. layout->offsets[upb_fielddef_index(field)];
  389. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  390. return *((VALUE *)memory);
  391. } else {
  392. return native_slot_get(upb_fielddef_type(field),
  393. field_type_class(field),
  394. memory);
  395. }
  396. }
  397. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  398. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  399. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  400. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  401. rb_raise(rb_eTypeError, "Expected repeated field array");
  402. }
  403. RepeatedField* self = ruby_to_RepeatedField(val);
  404. if (self->field_type != upb_fielddef_type(field)) {
  405. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  406. }
  407. if (self->field_type == UPB_TYPE_MESSAGE ||
  408. self->field_type == UPB_TYPE_ENUM) {
  409. if (self->field_type_class !=
  410. get_def_obj(upb_fielddef_subdef(field))) {
  411. rb_raise(rb_eTypeError,
  412. "Repeated field array has wrong message/enum class");
  413. }
  414. }
  415. }
  416. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  417. assert(is_map_field(field));
  418. const upb_fielddef* key_field = map_field_key(field);
  419. const upb_fielddef* value_field = map_field_value(field);
  420. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  421. RTYPEDDATA_TYPE(val) != &Map_type) {
  422. rb_raise(rb_eTypeError, "Expected Map instance");
  423. }
  424. Map* self = ruby_to_Map(val);
  425. if (self->key_type != upb_fielddef_type(key_field)) {
  426. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  427. }
  428. if (self->value_type != upb_fielddef_type(value_field)) {
  429. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  430. }
  431. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  432. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  433. if (self->value_type_class !=
  434. get_def_obj(upb_fielddef_subdef(value_field))) {
  435. rb_raise(rb_eTypeError,
  436. "Map value type has wrong message/enum class");
  437. }
  438. }
  439. }
  440. void layout_set(MessageLayout* layout,
  441. void* storage,
  442. const upb_fielddef* field,
  443. VALUE val) {
  444. void* memory = ((uint8_t *)storage) +
  445. layout->offsets[upb_fielddef_index(field)];
  446. if (is_map_field(field)) {
  447. check_map_field_type(val, field);
  448. DEREF(memory, VALUE) = val;
  449. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  450. check_repeated_field_type(val, field);
  451. DEREF(memory, VALUE) = val;
  452. } else {
  453. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  454. memory, val);
  455. }
  456. }
  457. void layout_init(MessageLayout* layout,
  458. void* storage) {
  459. upb_msg_iter it;
  460. for (upb_msg_begin(&it, layout->msgdef);
  461. !upb_msg_done(&it);
  462. upb_msg_next(&it)) {
  463. const upb_fielddef* field = upb_msg_iter_field(&it);
  464. void* memory = ((uint8_t *)storage) +
  465. layout->offsets[upb_fielddef_index(field)];
  466. if (is_map_field(field)) {
  467. VALUE map = Qnil;
  468. const upb_fielddef* key_field = map_field_key(field);
  469. const upb_fielddef* value_field = map_field_value(field);
  470. VALUE type_class = field_type_class(value_field);
  471. if (type_class != Qnil) {
  472. VALUE args[3] = {
  473. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  474. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  475. type_class,
  476. };
  477. map = rb_class_new_instance(3, args, cMap);
  478. } else {
  479. VALUE args[2] = {
  480. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  481. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  482. };
  483. map = rb_class_new_instance(2, args, cMap);
  484. }
  485. DEREF(memory, VALUE) = map;
  486. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  487. VALUE ary = Qnil;
  488. VALUE type_class = field_type_class(field);
  489. if (type_class != Qnil) {
  490. VALUE args[2] = {
  491. fieldtype_to_ruby(upb_fielddef_type(field)),
  492. type_class,
  493. };
  494. ary = rb_class_new_instance(2, args, cRepeatedField);
  495. } else {
  496. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  497. ary = rb_class_new_instance(1, args, cRepeatedField);
  498. }
  499. DEREF(memory, VALUE) = ary;
  500. } else {
  501. native_slot_init(upb_fielddef_type(field), memory);
  502. }
  503. }
  504. }
  505. void layout_mark(MessageLayout* layout, void* storage) {
  506. upb_msg_iter it;
  507. for (upb_msg_begin(&it, layout->msgdef);
  508. !upb_msg_done(&it);
  509. upb_msg_next(&it)) {
  510. const upb_fielddef* field = upb_msg_iter_field(&it);
  511. void* memory = ((uint8_t *)storage) +
  512. layout->offsets[upb_fielddef_index(field)];
  513. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  514. rb_gc_mark(DEREF(memory, VALUE));
  515. } else {
  516. native_slot_mark(upb_fielddef_type(field), memory);
  517. }
  518. }
  519. }
  520. void layout_dup(MessageLayout* layout, void* to, void* from) {
  521. upb_msg_iter it;
  522. for (upb_msg_begin(&it, layout->msgdef);
  523. !upb_msg_done(&it);
  524. upb_msg_next(&it)) {
  525. const upb_fielddef* field = upb_msg_iter_field(&it);
  526. void* to_memory = ((uint8_t *)to) +
  527. layout->offsets[upb_fielddef_index(field)];
  528. void* from_memory = ((uint8_t *)from) +
  529. layout->offsets[upb_fielddef_index(field)];
  530. if (is_map_field(field)) {
  531. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  532. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  533. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  534. } else {
  535. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  536. }
  537. }
  538. }
  539. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  540. upb_msg_iter it;
  541. for (upb_msg_begin(&it, layout->msgdef);
  542. !upb_msg_done(&it);
  543. upb_msg_next(&it)) {
  544. const upb_fielddef* field = upb_msg_iter_field(&it);
  545. void* to_memory = ((uint8_t *)to) +
  546. layout->offsets[upb_fielddef_index(field)];
  547. void* from_memory = ((uint8_t *)from) +
  548. layout->offsets[upb_fielddef_index(field)];
  549. if (is_map_field(field)) {
  550. DEREF(to_memory, VALUE) =
  551. Map_deep_copy(DEREF(from_memory, VALUE));
  552. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  553. DEREF(to_memory, VALUE) =
  554. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  555. } else {
  556. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  557. }
  558. }
  559. }
  560. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  561. upb_msg_iter it;
  562. for (upb_msg_begin(&it, layout->msgdef);
  563. !upb_msg_done(&it);
  564. upb_msg_next(&it)) {
  565. const upb_fielddef* field = upb_msg_iter_field(&it);
  566. void* msg1_memory = ((uint8_t *)msg1) +
  567. layout->offsets[upb_fielddef_index(field)];
  568. void* msg2_memory = ((uint8_t *)msg2) +
  569. layout->offsets[upb_fielddef_index(field)];
  570. if (is_map_field(field)) {
  571. return Map_eq(DEREF(msg1_memory, VALUE),
  572. DEREF(msg2_memory, VALUE));
  573. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  574. return RepeatedField_eq(DEREF(msg1_memory, VALUE),
  575. DEREF(msg2_memory, VALUE));
  576. } else {
  577. if (!native_slot_eq(upb_fielddef_type(field),
  578. msg1_memory, msg2_memory)) {
  579. return Qfalse;
  580. }
  581. }
  582. }
  583. return Qtrue;
  584. }
  585. VALUE layout_hash(MessageLayout* layout, void* storage) {
  586. upb_msg_iter it;
  587. st_index_t h = rb_hash_start(0);
  588. VALUE hash_sym = rb_intern("hash");
  589. for (upb_msg_begin(&it, layout->msgdef);
  590. !upb_msg_done(&it);
  591. upb_msg_next(&it)) {
  592. const upb_fielddef* field = upb_msg_iter_field(&it);
  593. VALUE field_val = layout_get(layout, storage, field);
  594. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  595. }
  596. h = rb_hash_end(h);
  597. return INT2FIX(h);
  598. }
  599. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  600. VALUE str = rb_str_new2("");
  601. upb_msg_iter it;
  602. bool first = true;
  603. for (upb_msg_begin(&it, layout->msgdef);
  604. !upb_msg_done(&it);
  605. upb_msg_next(&it)) {
  606. const upb_fielddef* field = upb_msg_iter_field(&it);
  607. VALUE field_val = layout_get(layout, storage, field);
  608. if (!first) {
  609. str = rb_str_cat2(str, ", ");
  610. } else {
  611. first = false;
  612. }
  613. str = rb_str_cat2(str, upb_fielddef_name(field));
  614. str = rb_str_cat2(str, ": ");
  615. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  616. }
  617. return str;
  618. }