storage.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 void check_int_range_precision(upb_fieldtype_t type, VALUE val) {
  54. // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
  55. // bound; we just need to do precision checks (i.e., disallow rounding) and
  56. // check for < 0 on unsigned types.
  57. if (TYPE(val) == T_FLOAT) {
  58. double dbl_val = NUM2DBL(val);
  59. if (floor(dbl_val) != dbl_val) {
  60. rb_raise(rb_eRangeError,
  61. "Non-integral floating point value assigned to integer field.");
  62. }
  63. }
  64. if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
  65. if (NUM2DBL(val) < 0) {
  66. rb_raise(rb_eRangeError,
  67. "Assigning negative value to unsigned integer field.");
  68. }
  69. }
  70. }
  71. static bool is_ruby_num(VALUE value) {
  72. return (TYPE(value) == T_FLOAT ||
  73. TYPE(value) == T_FIXNUM ||
  74. TYPE(value) == T_BIGNUM);
  75. }
  76. void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value) {
  77. bool bad_encoding = false;
  78. rb_encoding* string_encoding = rb_enc_from_index(ENCODING_GET(value));
  79. if (type == UPB_TYPE_STRING) {
  80. bad_encoding =
  81. string_encoding != kRubyStringUtf8Encoding &&
  82. string_encoding != kRubyStringASCIIEncoding;
  83. } else {
  84. bad_encoding =
  85. string_encoding != kRubyString8bitEncoding;
  86. }
  87. // Check that encoding is UTF-8 or ASCII (for string fields) or ASCII-8BIT
  88. // (for bytes fields).
  89. if (bad_encoding) {
  90. rb_raise(rb_eTypeError, "Encoding for '%s' fields must be %s (was %s)",
  91. (type == UPB_TYPE_STRING) ? "string" : "bytes",
  92. (type == UPB_TYPE_STRING) ? "UTF-8 or ASCII" : "ASCII-8BIT",
  93. rb_enc_name(string_encoding));
  94. }
  95. }
  96. void native_slot_set(upb_fieldtype_t type, VALUE type_class,
  97. void* memory, VALUE value) {
  98. switch (type) {
  99. case UPB_TYPE_FLOAT:
  100. if (!is_ruby_num(value)) {
  101. rb_raise(rb_eTypeError, "Expected number type for float field.");
  102. }
  103. DEREF(memory, float) = NUM2DBL(value);
  104. break;
  105. case UPB_TYPE_DOUBLE:
  106. if (!is_ruby_num(value)) {
  107. rb_raise(rb_eTypeError, "Expected number type for double field.");
  108. }
  109. DEREF(memory, double) = NUM2DBL(value);
  110. break;
  111. case UPB_TYPE_BOOL: {
  112. int8_t val = -1;
  113. if (value == Qtrue) {
  114. val = 1;
  115. } else if (value == Qfalse) {
  116. val = 0;
  117. } else {
  118. rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
  119. }
  120. DEREF(memory, int8_t) = val;
  121. break;
  122. }
  123. case UPB_TYPE_STRING:
  124. case UPB_TYPE_BYTES: {
  125. if (CLASS_OF(value) != rb_cString) {
  126. rb_raise(rb_eTypeError, "Invalid argument for string field.");
  127. }
  128. native_slot_validate_string_encoding(type, value);
  129. DEREF(memory, VALUE) = value;
  130. break;
  131. }
  132. case UPB_TYPE_MESSAGE: {
  133. if (CLASS_OF(value) != type_class) {
  134. rb_raise(rb_eTypeError,
  135. "Invalid type %s to assign to submessage field.",
  136. rb_class2name(CLASS_OF(value)));
  137. }
  138. DEREF(memory, VALUE) = value;
  139. break;
  140. }
  141. case UPB_TYPE_ENUM: {
  142. if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  143. rb_raise(rb_eTypeError,
  144. "Expected number or symbol type for enum field.");
  145. }
  146. int32_t int_val = 0;
  147. if (TYPE(value) == T_SYMBOL) {
  148. // Ensure that the given symbol exists in the enum module.
  149. VALUE lookup = rb_const_get(type_class, SYM2ID(value));
  150. if (lookup == Qnil) {
  151. rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
  152. } else {
  153. int_val = NUM2INT(lookup);
  154. }
  155. } else {
  156. check_int_range_precision(UPB_TYPE_INT32, value);
  157. int_val = NUM2INT(value);
  158. }
  159. DEREF(memory, int32_t) = int_val;
  160. break;
  161. }
  162. case UPB_TYPE_INT32:
  163. case UPB_TYPE_INT64:
  164. case UPB_TYPE_UINT32:
  165. case UPB_TYPE_UINT64:
  166. if (!is_ruby_num(value)) {
  167. rb_raise(rb_eTypeError, "Expected number type for integral field.");
  168. }
  169. 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. // TODO(cfallin): set encoding appropriately
  238. DEREF(memory, VALUE) = rb_str_new2("");
  239. break;
  240. case UPB_TYPE_MESSAGE:
  241. DEREF(memory, VALUE) = Qnil;
  242. break;
  243. case UPB_TYPE_ENUM:
  244. case UPB_TYPE_INT32:
  245. DEREF(memory, int32_t) = 0;
  246. break;
  247. case UPB_TYPE_INT64:
  248. DEREF(memory, int64_t) = 0;
  249. break;
  250. case UPB_TYPE_UINT32:
  251. DEREF(memory, uint32_t) = 0;
  252. break;
  253. case UPB_TYPE_UINT64:
  254. DEREF(memory, uint64_t) = 0;
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  261. switch (type) {
  262. case UPB_TYPE_STRING:
  263. case UPB_TYPE_BYTES:
  264. case UPB_TYPE_MESSAGE:
  265. rb_gc_mark(DEREF(memory, VALUE));
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  272. memcpy(to, from, native_slot_size(type));
  273. }
  274. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  275. switch (type) {
  276. case UPB_TYPE_STRING:
  277. case UPB_TYPE_BYTES: {
  278. VALUE from_val = DEREF(from, VALUE);
  279. DEREF(to, VALUE) = (from_val != Qnil) ?
  280. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  281. break;
  282. }
  283. case UPB_TYPE_MESSAGE: {
  284. VALUE from_val = DEREF(from, VALUE);
  285. DEREF(to, VALUE) = (from_val != Qnil) ?
  286. Message_deep_copy(from_val) : Qnil;
  287. break;
  288. }
  289. default:
  290. memcpy(to, from, native_slot_size(type));
  291. }
  292. }
  293. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  294. switch (type) {
  295. case UPB_TYPE_STRING:
  296. case UPB_TYPE_BYTES:
  297. case UPB_TYPE_MESSAGE: {
  298. VALUE val1 = DEREF(mem1, VALUE);
  299. VALUE val2 = DEREF(mem2, VALUE);
  300. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  301. return ret == Qtrue;
  302. }
  303. default:
  304. return !memcmp(mem1, mem2, native_slot_size(type));
  305. }
  306. }
  307. // -----------------------------------------------------------------------------
  308. // Memory layout management.
  309. // -----------------------------------------------------------------------------
  310. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  311. MessageLayout* layout = ALLOC(MessageLayout);
  312. int nfields = upb_msgdef_numfields(msgdef);
  313. layout->offsets = ALLOC_N(size_t, nfields);
  314. upb_msg_iter it;
  315. size_t off = 0;
  316. for (upb_msg_begin(&it, msgdef); !upb_msg_done(&it); upb_msg_next(&it)) {
  317. const upb_fielddef* field = upb_msg_iter_field(&it);
  318. size_t field_size =
  319. (upb_fielddef_label(field) == UPB_LABEL_REPEATED) ?
  320. sizeof(VALUE) : native_slot_size(upb_fielddef_type(field));
  321. // align current offset
  322. off = (off + field_size - 1) & ~(field_size - 1);
  323. layout->offsets[upb_fielddef_index(field)] = off;
  324. off += field_size;
  325. }
  326. layout->size = off;
  327. layout->msgdef = msgdef;
  328. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  329. return layout;
  330. }
  331. void free_layout(MessageLayout* layout) {
  332. xfree(layout->offsets);
  333. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  334. xfree(layout);
  335. }
  336. static VALUE get_type_class(const upb_fielddef* field) {
  337. VALUE type_class = Qnil;
  338. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  339. VALUE submsgdesc =
  340. get_def_obj(upb_fielddef_subdef(field));
  341. type_class = Descriptor_msgclass(submsgdesc);
  342. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  343. VALUE subenumdesc =
  344. get_def_obj(upb_fielddef_subdef(field));
  345. type_class = EnumDescriptor_enummodule(subenumdesc);
  346. }
  347. return type_class;
  348. }
  349. VALUE layout_get(MessageLayout* layout,
  350. void* storage,
  351. const upb_fielddef* field) {
  352. void* memory = ((uint8_t *)storage) +
  353. layout->offsets[upb_fielddef_index(field)];
  354. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  355. return *((VALUE *)memory);
  356. } else {
  357. return native_slot_get(upb_fielddef_type(field),
  358. get_type_class(field),
  359. memory);
  360. }
  361. }
  362. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  363. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  364. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  365. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  366. rb_raise(rb_eTypeError, "Expected repeated field array");
  367. }
  368. RepeatedField* self = ruby_to_RepeatedField(val);
  369. if (self->field_type != upb_fielddef_type(field)) {
  370. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  371. }
  372. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE ||
  373. upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  374. RepeatedField* self = ruby_to_RepeatedField(val);
  375. if (self->field_type_class !=
  376. get_def_obj(upb_fielddef_subdef(field))) {
  377. rb_raise(rb_eTypeError,
  378. "Repeated field array has wrong message/enum class");
  379. }
  380. }
  381. }
  382. void layout_set(MessageLayout* layout,
  383. void* storage,
  384. const upb_fielddef* field,
  385. VALUE val) {
  386. void* memory = ((uint8_t *)storage) +
  387. layout->offsets[upb_fielddef_index(field)];
  388. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  389. check_repeated_field_type(val, field);
  390. *((VALUE *)memory) = val;
  391. } else {
  392. native_slot_set(upb_fielddef_type(field), get_type_class(field),
  393. memory, val);
  394. }
  395. }
  396. void layout_init(MessageLayout* layout,
  397. void* storage) {
  398. upb_msg_iter it;
  399. for (upb_msg_begin(&it, layout->msgdef);
  400. !upb_msg_done(&it);
  401. upb_msg_next(&it)) {
  402. const upb_fielddef* field = upb_msg_iter_field(&it);
  403. void* memory = ((uint8_t *)storage) +
  404. layout->offsets[upb_fielddef_index(field)];
  405. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  406. VALUE ary = Qnil;
  407. VALUE type_class = get_type_class(field);
  408. if (type_class != Qnil) {
  409. VALUE args[2] = {
  410. fieldtype_to_ruby(upb_fielddef_type(field)),
  411. type_class,
  412. };
  413. ary = rb_class_new_instance(2, args, cRepeatedField);
  414. } else {
  415. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  416. ary = rb_class_new_instance(1, args, cRepeatedField);
  417. }
  418. *((VALUE *)memory) = ary;
  419. } else {
  420. native_slot_init(upb_fielddef_type(field), memory);
  421. }
  422. }
  423. }
  424. void layout_mark(MessageLayout* layout, void* storage) {
  425. upb_msg_iter it;
  426. for (upb_msg_begin(&it, layout->msgdef);
  427. !upb_msg_done(&it);
  428. upb_msg_next(&it)) {
  429. const upb_fielddef* field = upb_msg_iter_field(&it);
  430. void* memory = ((uint8_t *)storage) +
  431. layout->offsets[upb_fielddef_index(field)];
  432. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  433. rb_gc_mark(*((VALUE *)memory));
  434. } else {
  435. native_slot_mark(upb_fielddef_type(field), memory);
  436. }
  437. }
  438. }
  439. void layout_dup(MessageLayout* layout, void* to, void* from) {
  440. upb_msg_iter it;
  441. for (upb_msg_begin(&it, layout->msgdef);
  442. !upb_msg_done(&it);
  443. upb_msg_next(&it)) {
  444. const upb_fielddef* field = upb_msg_iter_field(&it);
  445. void* to_memory = ((uint8_t *)to) +
  446. layout->offsets[upb_fielddef_index(field)];
  447. void* from_memory = ((uint8_t *)from) +
  448. layout->offsets[upb_fielddef_index(field)];
  449. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  450. *((VALUE *)to_memory) = RepeatedField_dup(*((VALUE *)from_memory));
  451. } else {
  452. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  453. }
  454. }
  455. }
  456. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  457. upb_msg_iter it;
  458. for (upb_msg_begin(&it, layout->msgdef);
  459. !upb_msg_done(&it);
  460. upb_msg_next(&it)) {
  461. const upb_fielddef* field = upb_msg_iter_field(&it);
  462. void* to_memory = ((uint8_t *)to) +
  463. layout->offsets[upb_fielddef_index(field)];
  464. void* from_memory = ((uint8_t *)from) +
  465. layout->offsets[upb_fielddef_index(field)];
  466. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  467. *((VALUE *)to_memory) = RepeatedField_deep_copy(*((VALUE *)from_memory));
  468. } else {
  469. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  470. }
  471. }
  472. }
  473. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  474. upb_msg_iter it;
  475. for (upb_msg_begin(&it, layout->msgdef);
  476. !upb_msg_done(&it);
  477. upb_msg_next(&it)) {
  478. const upb_fielddef* field = upb_msg_iter_field(&it);
  479. void* msg1_memory = ((uint8_t *)msg1) +
  480. layout->offsets[upb_fielddef_index(field)];
  481. void* msg2_memory = ((uint8_t *)msg2) +
  482. layout->offsets[upb_fielddef_index(field)];
  483. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  484. if (RepeatedField_eq(*((VALUE *)msg1_memory),
  485. *((VALUE *)msg2_memory)) == Qfalse) {
  486. return Qfalse;
  487. }
  488. } else {
  489. if (!native_slot_eq(upb_fielddef_type(field),
  490. msg1_memory, msg2_memory)) {
  491. return Qfalse;
  492. }
  493. }
  494. }
  495. return Qtrue;
  496. }
  497. VALUE layout_hash(MessageLayout* layout, void* storage) {
  498. upb_msg_iter it;
  499. st_index_t h = rb_hash_start(0);
  500. VALUE hash_sym = rb_intern("hash");
  501. for (upb_msg_begin(&it, layout->msgdef);
  502. !upb_msg_done(&it);
  503. upb_msg_next(&it)) {
  504. const upb_fielddef* field = upb_msg_iter_field(&it);
  505. VALUE field_val = layout_get(layout, storage, field);
  506. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  507. }
  508. h = rb_hash_end(h);
  509. return INT2FIX(h);
  510. }
  511. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  512. VALUE str = rb_str_new2("");
  513. upb_msg_iter it;
  514. bool first = true;
  515. for (upb_msg_begin(&it, layout->msgdef);
  516. !upb_msg_done(&it);
  517. upb_msg_next(&it)) {
  518. const upb_fielddef* field = upb_msg_iter_field(&it);
  519. VALUE field_val = layout_get(layout, storage, field);
  520. if (!first) {
  521. str = rb_str_cat2(str, ", ");
  522. } else {
  523. first = false;
  524. }
  525. str = rb_str_cat2(str, upb_fielddef_name(field));
  526. str = rb_str_cat2(str, ": ");
  527. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  528. }
  529. return str;
  530. }