storage.c 40 KB

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