storage.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. case UPB_TYPE_MESSAGE:
  275. return DEREF(memory, VALUE);
  276. case UPB_TYPE_ENUM: {
  277. int32_t val = DEREF(memory, int32_t);
  278. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  279. if (symbol == Qnil) {
  280. return INT2NUM(val);
  281. } else {
  282. return symbol;
  283. }
  284. }
  285. case UPB_TYPE_INT32:
  286. return INT2NUM(DEREF(memory, int32_t));
  287. case UPB_TYPE_INT64:
  288. return LL2NUM(DEREF(memory, int64_t));
  289. case UPB_TYPE_UINT32:
  290. return UINT2NUM(DEREF(memory, uint32_t));
  291. case UPB_TYPE_UINT64:
  292. return ULL2NUM(DEREF(memory, uint64_t));
  293. default:
  294. return Qnil;
  295. }
  296. }
  297. void native_slot_init(upb_fieldtype_t type, void* memory) {
  298. switch (type) {
  299. case UPB_TYPE_FLOAT:
  300. DEREF(memory, float) = 0.0;
  301. break;
  302. case UPB_TYPE_DOUBLE:
  303. DEREF(memory, double) = 0.0;
  304. break;
  305. case UPB_TYPE_BOOL:
  306. DEREF(memory, int8_t) = 0;
  307. break;
  308. case UPB_TYPE_STRING:
  309. case UPB_TYPE_BYTES:
  310. DEREF(memory, VALUE) = rb_str_new2("");
  311. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  312. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  313. break;
  314. case UPB_TYPE_MESSAGE:
  315. DEREF(memory, VALUE) = Qnil;
  316. break;
  317. case UPB_TYPE_ENUM:
  318. case UPB_TYPE_INT32:
  319. DEREF(memory, int32_t) = 0;
  320. break;
  321. case UPB_TYPE_INT64:
  322. DEREF(memory, int64_t) = 0;
  323. break;
  324. case UPB_TYPE_UINT32:
  325. DEREF(memory, uint32_t) = 0;
  326. break;
  327. case UPB_TYPE_UINT64:
  328. DEREF(memory, uint64_t) = 0;
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  335. switch (type) {
  336. case UPB_TYPE_STRING:
  337. case UPB_TYPE_BYTES:
  338. case UPB_TYPE_MESSAGE:
  339. rb_gc_mark(DEREF(memory, VALUE));
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  346. memcpy(to, from, native_slot_size(type));
  347. }
  348. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  349. switch (type) {
  350. case UPB_TYPE_STRING:
  351. case UPB_TYPE_BYTES: {
  352. VALUE from_val = DEREF(from, VALUE);
  353. DEREF(to, VALUE) = (from_val != Qnil) ?
  354. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  355. break;
  356. }
  357. case UPB_TYPE_MESSAGE: {
  358. VALUE from_val = DEREF(from, VALUE);
  359. DEREF(to, VALUE) = (from_val != Qnil) ?
  360. Message_deep_copy(from_val) : Qnil;
  361. break;
  362. }
  363. default:
  364. memcpy(to, from, native_slot_size(type));
  365. }
  366. }
  367. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  368. switch (type) {
  369. case UPB_TYPE_STRING:
  370. case UPB_TYPE_BYTES:
  371. case UPB_TYPE_MESSAGE: {
  372. VALUE val1 = DEREF(mem1, VALUE);
  373. VALUE val2 = DEREF(mem2, VALUE);
  374. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  375. return ret == Qtrue;
  376. }
  377. default:
  378. return !memcmp(mem1, mem2, native_slot_size(type));
  379. }
  380. }
  381. // -----------------------------------------------------------------------------
  382. // Map field utilities.
  383. // -----------------------------------------------------------------------------
  384. const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
  385. const upb_msgdef* subdef;
  386. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  387. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  388. return NULL;
  389. }
  390. subdef = upb_fielddef_msgsubdef(field);
  391. return upb_msgdef_mapentry(subdef) ? subdef : NULL;
  392. }
  393. const upb_msgdef *map_entry_msgdef(const upb_fielddef* field) {
  394. const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
  395. assert(subdef);
  396. return subdef;
  397. }
  398. bool is_map_field(const upb_fielddef *field) {
  399. const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
  400. if (subdef == NULL) return false;
  401. // Map fields are a proto3 feature.
  402. // If we're using proto2 syntax we need to fallback to the repeated field.
  403. return upb_msgdef_syntax(subdef) == UPB_SYNTAX_PROTO3;
  404. }
  405. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  406. const upb_msgdef* subdef = map_entry_msgdef(field);
  407. return map_entry_key(subdef);
  408. }
  409. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  410. const upb_msgdef* subdef = map_entry_msgdef(field);
  411. return map_entry_value(subdef);
  412. }
  413. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  414. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  415. assert(key_field != NULL);
  416. return key_field;
  417. }
  418. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  419. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  420. assert(value_field != NULL);
  421. return value_field;
  422. }
  423. // -----------------------------------------------------------------------------
  424. // Memory layout management.
  425. // -----------------------------------------------------------------------------
  426. bool field_contains_hasbit(MessageLayout* layout,
  427. const upb_fielddef* field) {
  428. return layout->fields[upb_fielddef_index(field)].hasbit !=
  429. MESSAGE_FIELD_NO_HASBIT;
  430. }
  431. static size_t align_up_to(size_t offset, size_t granularity) {
  432. // Granularity must be a power of two.
  433. return (offset + granularity - 1) & ~(granularity - 1);
  434. }
  435. bool is_value_field(const upb_fielddef* f) {
  436. return upb_fielddef_isseq(f) || upb_fielddef_issubmsg(f) ||
  437. upb_fielddef_isstring(f);
  438. }
  439. MessageLayout* create_layout(const Descriptor* desc) {
  440. const upb_msgdef *msgdef = desc->msgdef;
  441. MessageLayout* layout = ALLOC(MessageLayout);
  442. int nfields = upb_msgdef_numfields(msgdef);
  443. int noneofs = upb_msgdef_numoneofs(msgdef);
  444. upb_msg_field_iter it;
  445. upb_msg_oneof_iter oit;
  446. size_t off = 0;
  447. size_t hasbit = 0;
  448. layout->desc = desc;
  449. layout->fields = ALLOC_N(MessageField, nfields);
  450. layout->oneofs = NULL;
  451. if (noneofs > 0) {
  452. layout->oneofs = ALLOC_N(MessageOneof, noneofs);
  453. }
  454. for (upb_msg_field_begin(&it, msgdef);
  455. !upb_msg_field_done(&it);
  456. upb_msg_field_next(&it)) {
  457. const upb_fielddef* field = upb_msg_iter_field(&it);
  458. if (upb_fielddef_haspresence(field)) {
  459. layout->fields[upb_fielddef_index(field)].hasbit = hasbit++;
  460. } else {
  461. layout->fields[upb_fielddef_index(field)].hasbit =
  462. MESSAGE_FIELD_NO_HASBIT;
  463. }
  464. }
  465. if (hasbit != 0) {
  466. off += (hasbit + 8 - 1) / 8;
  467. }
  468. off = align_up_to(off, sizeof(VALUE));
  469. layout->value_offset = off;
  470. layout->value_count = 0;
  471. // Place all (non-oneof) VALUE fields first.
  472. for (upb_msg_field_begin(&it, msgdef);
  473. !upb_msg_field_done(&it);
  474. upb_msg_field_next(&it)) {
  475. const upb_fielddef* field = upb_msg_iter_field(&it);
  476. if (upb_fielddef_containingoneof(field) || !is_value_field(field)) {
  477. continue;
  478. }
  479. layout->fields[upb_fielddef_index(field)].offset = off;
  480. off += sizeof(VALUE);
  481. layout->value_count++;
  482. }
  483. // Now place all other (non-oneof) fields.
  484. for (upb_msg_field_begin(&it, msgdef);
  485. !upb_msg_field_done(&it);
  486. upb_msg_field_next(&it)) {
  487. const upb_fielddef* field = upb_msg_iter_field(&it);
  488. size_t field_size;
  489. if (upb_fielddef_containingoneof(field) || is_value_field(field)) {
  490. continue;
  491. }
  492. // Allocate |field_size| bytes for this field in the layout.
  493. field_size = native_slot_size(upb_fielddef_type(field));
  494. // Align current offset up to |size| granularity.
  495. off = align_up_to(off, field_size);
  496. layout->fields[upb_fielddef_index(field)].offset = off;
  497. off += field_size;
  498. }
  499. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  500. // one slot per oneof.
  501. //
  502. // We assign all value slots first, then pack the 'case' fields at the end,
  503. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  504. // bytes respectively and we want to avoid alignment overhead.
  505. //
  506. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  507. // space for oneof cases is conceptually as wide as field tag numbers. In
  508. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  509. // members (8 or 16 bits respectively), so conceivably we could assign
  510. // consecutive case numbers and then pick a smaller oneof case slot size, but
  511. // the complexity to implement this indirection is probably not worthwhile.
  512. for (upb_msg_oneof_begin(&oit, msgdef);
  513. !upb_msg_oneof_done(&oit);
  514. upb_msg_oneof_next(&oit)) {
  515. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  516. upb_oneof_iter fit;
  517. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  518. // all fields.
  519. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  520. // Align the offset.
  521. off = align_up_to(off, field_size);
  522. // Assign all fields in the oneof this same offset.
  523. for (upb_oneof_begin(&fit, oneof);
  524. !upb_oneof_done(&fit);
  525. upb_oneof_next(&fit)) {
  526. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  527. layout->fields[upb_fielddef_index(field)].offset = off;
  528. layout->oneofs[upb_oneofdef_index(oneof)].offset = off;
  529. }
  530. off += field_size;
  531. }
  532. // Now the case fields.
  533. for (upb_msg_oneof_begin(&oit, msgdef);
  534. !upb_msg_oneof_done(&oit);
  535. upb_msg_oneof_next(&oit)) {
  536. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  537. size_t field_size = sizeof(uint32_t);
  538. // Align the offset.
  539. off = (off + field_size - 1) & ~(field_size - 1);
  540. layout->oneofs[upb_oneofdef_index(oneof)].case_offset = off;
  541. off += field_size;
  542. }
  543. layout->size = off;
  544. layout->msgdef = msgdef;
  545. return layout;
  546. }
  547. void free_layout(MessageLayout* layout) {
  548. xfree(layout->fields);
  549. xfree(layout->oneofs);
  550. xfree(layout);
  551. }
  552. VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field) {
  553. VALUE type_class = Qnil;
  554. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  555. VALUE submsgdesc = get_msgdef_obj(layout->desc->descriptor_pool,
  556. upb_fielddef_msgsubdef(field));
  557. type_class = Descriptor_msgclass(submsgdesc);
  558. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  559. VALUE subenumdesc = get_enumdef_obj(layout->desc->descriptor_pool,
  560. upb_fielddef_enumsubdef(field));
  561. type_class = EnumDescriptor_enummodule(subenumdesc);
  562. }
  563. return type_class;
  564. }
  565. static void* slot_memory(MessageLayout* layout,
  566. const void* storage,
  567. const upb_fielddef* field) {
  568. return ((uint8_t *)storage) +
  569. layout->fields[upb_fielddef_index(field)].offset;
  570. }
  571. static uint32_t* slot_oneof_case(MessageLayout* layout,
  572. const void* storage,
  573. const upb_oneofdef* oneof) {
  574. return (uint32_t*)(((uint8_t*)storage) +
  575. layout->oneofs[upb_oneofdef_index(oneof)].case_offset);
  576. }
  577. uint32_t slot_read_oneof_case(MessageLayout* layout, const void* storage,
  578. const upb_oneofdef* oneof) {
  579. uint32_t* ptr = slot_oneof_case(layout, storage, oneof);
  580. return *ptr & ~ONEOF_CASE_MASK;
  581. }
  582. static void slot_set_hasbit(MessageLayout* layout,
  583. const void* storage,
  584. const upb_fielddef* field) {
  585. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  586. assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
  587. ((uint8_t*)storage)[hasbit / 8] |= 1 << (hasbit % 8);
  588. }
  589. static void slot_clear_hasbit(MessageLayout* layout,
  590. const void* storage,
  591. const upb_fielddef* field) {
  592. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  593. assert(hasbit != MESSAGE_FIELD_NO_HASBIT);
  594. ((uint8_t*)storage)[hasbit / 8] &= ~(1 << (hasbit % 8));
  595. }
  596. static bool slot_is_hasbit_set(MessageLayout* layout,
  597. const void* storage,
  598. const upb_fielddef* field) {
  599. size_t hasbit = layout->fields[upb_fielddef_index(field)].hasbit;
  600. if (hasbit == MESSAGE_FIELD_NO_HASBIT) {
  601. return false;
  602. }
  603. return DEREF_OFFSET(
  604. (uint8_t*)storage, hasbit / 8, char) & (1 << (hasbit % 8));
  605. }
  606. VALUE layout_has(MessageLayout* layout,
  607. const void* storage,
  608. const upb_fielddef* field) {
  609. assert(field_contains_hasbit(layout, field));
  610. return slot_is_hasbit_set(layout, storage, field) ? Qtrue : Qfalse;
  611. }
  612. void layout_clear(MessageLayout* layout,
  613. const void* storage,
  614. const upb_fielddef* field) {
  615. void* memory = slot_memory(layout, storage, field);
  616. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  617. if (field_contains_hasbit(layout, field)) {
  618. slot_clear_hasbit(layout, storage, field);
  619. }
  620. if (oneof) {
  621. uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
  622. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  623. *oneof_case = ONEOF_CASE_NONE;
  624. } else if (is_map_field(field)) {
  625. VALUE map = Qnil;
  626. const upb_fielddef* key_field = map_field_key(field);
  627. const upb_fielddef* value_field = map_field_value(field);
  628. VALUE type_class = field_type_class(layout, value_field);
  629. if (type_class != Qnil) {
  630. VALUE args[3] = {
  631. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  632. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  633. type_class,
  634. };
  635. map = rb_class_new_instance(3, args, cMap);
  636. } else {
  637. VALUE args[2] = {
  638. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  639. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  640. };
  641. map = rb_class_new_instance(2, args, cMap);
  642. }
  643. DEREF(memory, VALUE) = map;
  644. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  645. VALUE ary = Qnil;
  646. VALUE type_class = field_type_class(layout, field);
  647. if (type_class != Qnil) {
  648. VALUE args[2] = {
  649. fieldtype_to_ruby(upb_fielddef_type(field)),
  650. type_class,
  651. };
  652. ary = rb_class_new_instance(2, args, cRepeatedField);
  653. } else {
  654. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  655. ary = rb_class_new_instance(1, args, cRepeatedField);
  656. }
  657. DEREF(memory, VALUE) = ary;
  658. } else {
  659. native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
  660. field_type_class(layout, field), memory,
  661. layout_get_default(field));
  662. }
  663. }
  664. VALUE layout_get_default(const upb_fielddef *field) {
  665. switch (upb_fielddef_type(field)) {
  666. case UPB_TYPE_FLOAT: return DBL2NUM(upb_fielddef_defaultfloat(field));
  667. case UPB_TYPE_DOUBLE: return DBL2NUM(upb_fielddef_defaultdouble(field));
  668. case UPB_TYPE_BOOL:
  669. return upb_fielddef_defaultbool(field) ? Qtrue : Qfalse;
  670. case UPB_TYPE_MESSAGE: return Qnil;
  671. case UPB_TYPE_ENUM: {
  672. const upb_enumdef *enumdef = upb_fielddef_enumsubdef(field);
  673. int32_t num = upb_fielddef_defaultint32(field);
  674. const char *label = upb_enumdef_iton(enumdef, num);
  675. if (label) {
  676. return ID2SYM(rb_intern(label));
  677. } else {
  678. return INT2NUM(num);
  679. }
  680. }
  681. case UPB_TYPE_INT32: return INT2NUM(upb_fielddef_defaultint32(field));
  682. case UPB_TYPE_INT64: return LL2NUM(upb_fielddef_defaultint64(field));;
  683. case UPB_TYPE_UINT32: return UINT2NUM(upb_fielddef_defaultuint32(field));
  684. case UPB_TYPE_UINT64: return ULL2NUM(upb_fielddef_defaultuint64(field));
  685. case UPB_TYPE_STRING:
  686. case UPB_TYPE_BYTES: {
  687. size_t size;
  688. const char *str = upb_fielddef_defaultstr(field, &size);
  689. return get_frozen_string(str, size,
  690. upb_fielddef_type(field) == UPB_TYPE_BYTES);
  691. }
  692. default: return Qnil;
  693. }
  694. }
  695. VALUE layout_get(MessageLayout* layout,
  696. const void* storage,
  697. const upb_fielddef* field) {
  698. void* memory = slot_memory(layout, storage, field);
  699. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  700. bool field_set;
  701. if (field_contains_hasbit(layout, field)) {
  702. field_set = slot_is_hasbit_set(layout, storage, field);
  703. } else {
  704. field_set = true;
  705. }
  706. if (oneof) {
  707. uint32_t oneof_case = slot_read_oneof_case(layout, storage, oneof);
  708. if (oneof_case != upb_fielddef_number(field)) {
  709. return layout_get_default(field);
  710. }
  711. return native_slot_get(upb_fielddef_type(field),
  712. field_type_class(layout, field), memory);
  713. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  714. return *((VALUE *)memory);
  715. } else if (!field_set) {
  716. return layout_get_default(field);
  717. } else {
  718. return native_slot_get(upb_fielddef_type(field),
  719. field_type_class(layout, field), memory);
  720. }
  721. }
  722. static void check_repeated_field_type(const MessageLayout* layout, VALUE val,
  723. const upb_fielddef* field) {
  724. RepeatedField* self;
  725. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  726. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  727. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  728. rb_raise(cTypeError, "Expected repeated field array");
  729. }
  730. self = ruby_to_RepeatedField(val);
  731. if (self->field_type != upb_fielddef_type(field)) {
  732. rb_raise(cTypeError, "Repeated field array has wrong element type");
  733. }
  734. if (self->field_type_class != field_type_class(layout, field)) {
  735. rb_raise(cTypeError, "Repeated field array has wrong message/enum class");
  736. }
  737. }
  738. static void check_map_field_type(const MessageLayout* layout, VALUE val,
  739. const upb_fielddef* field) {
  740. const upb_fielddef* key_field = map_field_key(field);
  741. const upb_fielddef* value_field = map_field_value(field);
  742. Map* self;
  743. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  744. RTYPEDDATA_TYPE(val) != &Map_type) {
  745. rb_raise(cTypeError, "Expected Map instance");
  746. }
  747. self = ruby_to_Map(val);
  748. if (self->key_type != upb_fielddef_type(key_field)) {
  749. rb_raise(cTypeError, "Map key type does not match field's key type");
  750. }
  751. if (self->value_type != upb_fielddef_type(value_field)) {
  752. rb_raise(cTypeError, "Map value type does not match field's value type");
  753. }
  754. if (self->value_type_class != field_type_class(layout, value_field)) {
  755. rb_raise(cTypeError, "Map value type has wrong message/enum class");
  756. }
  757. }
  758. void layout_set(MessageLayout* layout,
  759. void* storage,
  760. const upb_fielddef* field,
  761. VALUE val) {
  762. void* memory = slot_memory(layout, storage, field);
  763. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  764. if (oneof) {
  765. uint32_t* oneof_case = slot_oneof_case(layout, storage, oneof);
  766. if (val == Qnil) {
  767. // Assigning nil to a oneof field clears the oneof completely.
  768. *oneof_case = ONEOF_CASE_NONE;
  769. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  770. } else {
  771. // The transition between field types for a single oneof (union) slot is
  772. // somewhat complex because we need to ensure that a GC triggered at any
  773. // point by a call into the Ruby VM sees a valid state for this field and
  774. // does not either go off into the weeds (following what it thinks is a
  775. // VALUE but is actually a different field type) or miss an object (seeing
  776. // what it thinks is a primitive field but is actually a VALUE for the new
  777. // field type).
  778. //
  779. // In order for the transition to be safe, the oneof case slot must be in
  780. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  781. // use native_slot_set_value_and_case(), which ensures that both the value
  782. // and case number are altered atomically (w.r.t. the Ruby VM).
  783. uint32_t case_value = upb_fielddef_number(field);
  784. if (upb_fielddef_issubmsg(field) || upb_fielddef_isstring(field)) {
  785. case_value |= ONEOF_CASE_MASK;
  786. }
  787. native_slot_set_value_and_case(
  788. upb_fielddef_name(field), upb_fielddef_type(field),
  789. field_type_class(layout, field), memory, val, oneof_case, case_value);
  790. }
  791. } else if (is_map_field(field)) {
  792. check_map_field_type(layout, val, field);
  793. DEREF(memory, VALUE) = val;
  794. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  795. check_repeated_field_type(layout, val, field);
  796. DEREF(memory, VALUE) = val;
  797. } else {
  798. native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field),
  799. field_type_class(layout, field), memory, val);
  800. }
  801. if (layout->fields[upb_fielddef_index(field)].hasbit !=
  802. MESSAGE_FIELD_NO_HASBIT) {
  803. slot_set_hasbit(layout, storage, field);
  804. }
  805. }
  806. void layout_init(MessageLayout* layout,
  807. void* storage) {
  808. upb_msg_field_iter it;
  809. for (upb_msg_field_begin(&it, layout->msgdef);
  810. !upb_msg_field_done(&it);
  811. upb_msg_field_next(&it)) {
  812. layout_clear(layout, storage, upb_msg_iter_field(&it));
  813. }
  814. }
  815. void layout_mark(MessageLayout* layout, void* storage) {
  816. VALUE* values = (VALUE*)CHARPTR_AT(storage, layout->value_offset);
  817. int noneofs = upb_msgdef_numoneofs(layout->msgdef);
  818. int i;
  819. for (i = 0; i < layout->value_count; i++) {
  820. rb_gc_mark(values[i]);
  821. }
  822. for (i = 0; i < noneofs; i++) {
  823. MessageOneof* oneof = &layout->oneofs[i];
  824. uint32_t* case_ptr = (uint32_t*)CHARPTR_AT(storage, oneof->case_offset);
  825. if (*case_ptr & ONEOF_CASE_MASK) {
  826. rb_gc_mark(DEREF_OFFSET(storage, oneof->offset, VALUE));
  827. }
  828. }
  829. }
  830. void layout_dup(MessageLayout* layout, void* to, void* from) {
  831. upb_msg_field_iter it;
  832. for (upb_msg_field_begin(&it, layout->msgdef);
  833. !upb_msg_field_done(&it);
  834. upb_msg_field_next(&it)) {
  835. const upb_fielddef* field = upb_msg_iter_field(&it);
  836. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  837. void* to_memory = slot_memory(layout, to, field);
  838. void* from_memory = slot_memory(layout, from, field);
  839. if (oneof) {
  840. uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
  841. uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
  842. if (slot_read_oneof_case(layout, from, oneof) ==
  843. upb_fielddef_number(field)) {
  844. *to_oneof_case = *from_oneof_case;
  845. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  846. }
  847. } else if (is_map_field(field)) {
  848. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  849. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  850. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  851. } else {
  852. if (field_contains_hasbit(layout, field)) {
  853. if (!slot_is_hasbit_set(layout, from, field)) continue;
  854. slot_set_hasbit(layout, to, field);
  855. }
  856. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  857. }
  858. }
  859. }
  860. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  861. upb_msg_field_iter it;
  862. for (upb_msg_field_begin(&it, layout->msgdef);
  863. !upb_msg_field_done(&it);
  864. upb_msg_field_next(&it)) {
  865. const upb_fielddef* field = upb_msg_iter_field(&it);
  866. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  867. void* to_memory = slot_memory(layout, to, field);
  868. void* from_memory = slot_memory(layout, from, field);
  869. if (oneof) {
  870. uint32_t* to_oneof_case = slot_oneof_case(layout, to, oneof);
  871. uint32_t* from_oneof_case = slot_oneof_case(layout, from, oneof);
  872. if (slot_read_oneof_case(layout, from, oneof) ==
  873. upb_fielddef_number(field)) {
  874. *to_oneof_case = *from_oneof_case;
  875. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  876. }
  877. } else if (is_map_field(field)) {
  878. DEREF(to_memory, VALUE) =
  879. Map_deep_copy(DEREF(from_memory, VALUE));
  880. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  881. DEREF(to_memory, VALUE) =
  882. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  883. } else {
  884. if (field_contains_hasbit(layout, field)) {
  885. if (!slot_is_hasbit_set(layout, from, field)) continue;
  886. slot_set_hasbit(layout, to, field);
  887. }
  888. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  889. }
  890. }
  891. }
  892. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  893. upb_msg_field_iter it;
  894. for (upb_msg_field_begin(&it, layout->msgdef);
  895. !upb_msg_field_done(&it);
  896. upb_msg_field_next(&it)) {
  897. const upb_fielddef* field = upb_msg_iter_field(&it);
  898. const upb_oneofdef* oneof = upb_fielddef_containingoneof(field);
  899. void* msg1_memory = slot_memory(layout, msg1, field);
  900. void* msg2_memory = slot_memory(layout, msg2, field);
  901. if (oneof) {
  902. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, oneof);
  903. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, oneof);
  904. if (*msg1_oneof_case != *msg2_oneof_case ||
  905. (slot_read_oneof_case(layout, msg1, oneof) ==
  906. upb_fielddef_number(field) &&
  907. !native_slot_eq(upb_fielddef_type(field), msg1_memory,
  908. msg2_memory))) {
  909. return Qfalse;
  910. }
  911. } else if (is_map_field(field)) {
  912. if (!Map_eq(DEREF(msg1_memory, VALUE),
  913. DEREF(msg2_memory, VALUE))) {
  914. return Qfalse;
  915. }
  916. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  917. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  918. DEREF(msg2_memory, VALUE))) {
  919. return Qfalse;
  920. }
  921. } else {
  922. if (slot_is_hasbit_set(layout, msg1, field) !=
  923. slot_is_hasbit_set(layout, msg2, field) ||
  924. !native_slot_eq(upb_fielddef_type(field), msg1_memory, msg2_memory)) {
  925. return Qfalse;
  926. }
  927. }
  928. }
  929. return Qtrue;
  930. }
  931. VALUE layout_hash(MessageLayout* layout, void* storage) {
  932. upb_msg_field_iter it;
  933. st_index_t h = rb_hash_start(0);
  934. VALUE hash_sym = rb_intern("hash");
  935. for (upb_msg_field_begin(&it, layout->msgdef);
  936. !upb_msg_field_done(&it);
  937. upb_msg_field_next(&it)) {
  938. const upb_fielddef* field = upb_msg_iter_field(&it);
  939. VALUE field_val = layout_get(layout, storage, field);
  940. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  941. }
  942. h = rb_hash_end(h);
  943. return INT2FIX(h);
  944. }
  945. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  946. VALUE str = rb_str_new2("");
  947. upb_msg_field_iter it;
  948. bool first = true;
  949. for (upb_msg_field_begin(&it, layout->msgdef);
  950. !upb_msg_field_done(&it);
  951. upb_msg_field_next(&it)) {
  952. const upb_fielddef* field = upb_msg_iter_field(&it);
  953. VALUE field_val = layout_get(layout, storage, field);
  954. if (!first) {
  955. str = rb_str_cat2(str, ", ");
  956. } else {
  957. first = false;
  958. }
  959. str = rb_str_cat2(str, upb_fielddef_name(field));
  960. str = rb_str_cat2(str, ": ");
  961. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  962. }
  963. return str;
  964. }