storage.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. #include <math.h>
  32. #include <ruby/encoding.h>
  33. // -----------------------------------------------------------------------------
  34. // Ruby <-> native slot management.
  35. // -----------------------------------------------------------------------------
  36. #define DEREF(memory, type) *(type*)(memory)
  37. size_t native_slot_size(upb_fieldtype_t type) {
  38. switch (type) {
  39. case UPB_TYPE_FLOAT: return 4;
  40. case UPB_TYPE_DOUBLE: return 8;
  41. case UPB_TYPE_BOOL: return 1;
  42. case UPB_TYPE_STRING: return sizeof(VALUE);
  43. case UPB_TYPE_BYTES: return sizeof(VALUE);
  44. case UPB_TYPE_MESSAGE: return sizeof(VALUE);
  45. case UPB_TYPE_ENUM: return 4;
  46. case UPB_TYPE_INT32: return 4;
  47. case UPB_TYPE_INT64: return 8;
  48. case UPB_TYPE_UINT32: return 4;
  49. case UPB_TYPE_UINT64: return 8;
  50. default: return 0;
  51. }
  52. }
  53. static bool is_ruby_num(VALUE value) {
  54. return (TYPE(value) == T_FLOAT ||
  55. TYPE(value) == T_FIXNUM ||
  56. TYPE(value) == T_BIGNUM);
  57. }
  58. void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) {
  59. if (!is_ruby_num(val)) {
  60. rb_raise(rb_eTypeError, "Expected number type for integral field.");
  61. }
  62. // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
  63. // bound; we just need to do precision checks (i.e., disallow rounding) and
  64. // check for < 0 on unsigned types.
  65. if (TYPE(val) == T_FLOAT) {
  66. double dbl_val = NUM2DBL(val);
  67. if (floor(dbl_val) != dbl_val) {
  68. rb_raise(rb_eRangeError,
  69. "Non-integral floating point value assigned to integer field.");
  70. }
  71. }
  72. if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
  73. if (NUM2DBL(val) < 0) {
  74. rb_raise(rb_eRangeError,
  75. "Assigning negative value to unsigned integer field.");
  76. }
  77. }
  78. }
  79. void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value) {
  80. bool bad_encoding = false;
  81. rb_encoding* string_encoding = rb_enc_from_index(ENCODING_GET(value));
  82. if (type == UPB_TYPE_STRING) {
  83. bad_encoding =
  84. string_encoding != kRubyStringUtf8Encoding &&
  85. string_encoding != kRubyStringASCIIEncoding;
  86. } else {
  87. bad_encoding =
  88. string_encoding != kRubyString8bitEncoding;
  89. }
  90. // Check that encoding is UTF-8 or ASCII (for string fields) or ASCII-8BIT
  91. // (for bytes fields).
  92. if (bad_encoding) {
  93. rb_raise(rb_eTypeError, "Encoding for '%s' fields must be %s (was %s)",
  94. (type == UPB_TYPE_STRING) ? "string" : "bytes",
  95. (type == UPB_TYPE_STRING) ? "UTF-8 or ASCII" : "ASCII-8BIT",
  96. rb_enc_name(string_encoding));
  97. }
  98. }
  99. void native_slot_set(upb_fieldtype_t type, VALUE type_class,
  100. void* memory, VALUE value) {
  101. native_slot_set_value_and_case(type, type_class, memory, value, NULL, 0);
  102. }
  103. void native_slot_set_value_and_case(upb_fieldtype_t type, VALUE type_class,
  104. void* memory, VALUE value,
  105. uint32_t* case_memory,
  106. uint32_t case_number) {
  107. // Note that in order to atomically change the value in memory and the case
  108. // value (w.r.t. Ruby VM calls), we must set the value at |memory| only after
  109. // all Ruby VM calls are complete. The case is then set at the bottom of this
  110. // function.
  111. switch (type) {
  112. case UPB_TYPE_FLOAT:
  113. if (!is_ruby_num(value)) {
  114. rb_raise(rb_eTypeError, "Expected number type for float field.");
  115. }
  116. DEREF(memory, float) = NUM2DBL(value);
  117. break;
  118. case UPB_TYPE_DOUBLE:
  119. if (!is_ruby_num(value)) {
  120. rb_raise(rb_eTypeError, "Expected number type for double field.");
  121. }
  122. DEREF(memory, double) = NUM2DBL(value);
  123. break;
  124. case UPB_TYPE_BOOL: {
  125. int8_t val = -1;
  126. if (value == Qtrue) {
  127. val = 1;
  128. } else if (value == Qfalse) {
  129. val = 0;
  130. } else {
  131. rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
  132. }
  133. DEREF(memory, int8_t) = val;
  134. break;
  135. }
  136. case UPB_TYPE_STRING:
  137. case UPB_TYPE_BYTES: {
  138. if (CLASS_OF(value) != rb_cString) {
  139. rb_raise(rb_eTypeError, "Invalid argument for string field.");
  140. }
  141. native_slot_validate_string_encoding(type, value);
  142. DEREF(memory, VALUE) = value;
  143. break;
  144. }
  145. case UPB_TYPE_MESSAGE: {
  146. if (CLASS_OF(value) == CLASS_OF(Qnil)) {
  147. value = Qnil;
  148. } else if (CLASS_OF(value) != type_class) {
  149. rb_raise(rb_eTypeError,
  150. "Invalid type %s to assign to submessage field.",
  151. rb_class2name(CLASS_OF(value)));
  152. }
  153. DEREF(memory, VALUE) = value;
  154. break;
  155. }
  156. case UPB_TYPE_ENUM: {
  157. if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  158. rb_raise(rb_eTypeError,
  159. "Expected number or symbol type for enum field.");
  160. }
  161. int32_t int_val = 0;
  162. if (TYPE(value) == T_SYMBOL) {
  163. // Ensure that the given symbol exists in the enum module.
  164. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  165. if (lookup == Qnil) {
  166. rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
  167. } else {
  168. int_val = NUM2INT(lookup);
  169. }
  170. } else {
  171. native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
  172. int_val = NUM2INT(value);
  173. }
  174. DEREF(memory, int32_t) = int_val;
  175. break;
  176. }
  177. case UPB_TYPE_INT32:
  178. case UPB_TYPE_INT64:
  179. case UPB_TYPE_UINT32:
  180. case UPB_TYPE_UINT64:
  181. native_slot_check_int_range_precision(type, value);
  182. switch (type) {
  183. case UPB_TYPE_INT32:
  184. DEREF(memory, int32_t) = NUM2INT(value);
  185. break;
  186. case UPB_TYPE_INT64:
  187. DEREF(memory, int64_t) = NUM2LL(value);
  188. break;
  189. case UPB_TYPE_UINT32:
  190. DEREF(memory, uint32_t) = NUM2UINT(value);
  191. break;
  192. case UPB_TYPE_UINT64:
  193. DEREF(memory, uint64_t) = NUM2ULL(value);
  194. break;
  195. default:
  196. break;
  197. }
  198. break;
  199. default:
  200. break;
  201. }
  202. if (case_memory != NULL) {
  203. *case_memory = case_number;
  204. }
  205. }
  206. VALUE native_slot_get(upb_fieldtype_t type,
  207. VALUE type_class,
  208. const void* memory) {
  209. switch (type) {
  210. case UPB_TYPE_FLOAT:
  211. return DBL2NUM(DEREF(memory, float));
  212. case UPB_TYPE_DOUBLE:
  213. return DBL2NUM(DEREF(memory, double));
  214. case UPB_TYPE_BOOL:
  215. return DEREF(memory, int8_t) ? Qtrue : Qfalse;
  216. case UPB_TYPE_STRING:
  217. case UPB_TYPE_BYTES:
  218. case UPB_TYPE_MESSAGE:
  219. return DEREF(memory, VALUE);
  220. case UPB_TYPE_ENUM: {
  221. int32_t val = DEREF(memory, int32_t);
  222. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  223. if (symbol == Qnil) {
  224. return INT2NUM(val);
  225. } else {
  226. return symbol;
  227. }
  228. }
  229. case UPB_TYPE_INT32:
  230. return INT2NUM(DEREF(memory, int32_t));
  231. case UPB_TYPE_INT64:
  232. return LL2NUM(DEREF(memory, int64_t));
  233. case UPB_TYPE_UINT32:
  234. return UINT2NUM(DEREF(memory, uint32_t));
  235. case UPB_TYPE_UINT64:
  236. return ULL2NUM(DEREF(memory, uint64_t));
  237. default:
  238. return Qnil;
  239. }
  240. }
  241. void native_slot_init(upb_fieldtype_t type, void* memory) {
  242. switch (type) {
  243. case UPB_TYPE_FLOAT:
  244. DEREF(memory, float) = 0.0;
  245. break;
  246. case UPB_TYPE_DOUBLE:
  247. DEREF(memory, double) = 0.0;
  248. break;
  249. case UPB_TYPE_BOOL:
  250. DEREF(memory, int8_t) = 0;
  251. break;
  252. case UPB_TYPE_STRING:
  253. case UPB_TYPE_BYTES:
  254. DEREF(memory, VALUE) = rb_str_new2("");
  255. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  256. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  257. break;
  258. case UPB_TYPE_MESSAGE:
  259. DEREF(memory, VALUE) = Qnil;
  260. break;
  261. case UPB_TYPE_ENUM:
  262. case UPB_TYPE_INT32:
  263. DEREF(memory, int32_t) = 0;
  264. break;
  265. case UPB_TYPE_INT64:
  266. DEREF(memory, int64_t) = 0;
  267. break;
  268. case UPB_TYPE_UINT32:
  269. DEREF(memory, uint32_t) = 0;
  270. break;
  271. case UPB_TYPE_UINT64:
  272. DEREF(memory, uint64_t) = 0;
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  279. switch (type) {
  280. case UPB_TYPE_STRING:
  281. case UPB_TYPE_BYTES:
  282. case UPB_TYPE_MESSAGE:
  283. rb_gc_mark(DEREF(memory, VALUE));
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  290. memcpy(to, from, native_slot_size(type));
  291. }
  292. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  293. switch (type) {
  294. case UPB_TYPE_STRING:
  295. case UPB_TYPE_BYTES: {
  296. VALUE from_val = DEREF(from, VALUE);
  297. DEREF(to, VALUE) = (from_val != Qnil) ?
  298. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  299. break;
  300. }
  301. case UPB_TYPE_MESSAGE: {
  302. VALUE from_val = DEREF(from, VALUE);
  303. DEREF(to, VALUE) = (from_val != Qnil) ?
  304. Message_deep_copy(from_val) : Qnil;
  305. break;
  306. }
  307. default:
  308. memcpy(to, from, native_slot_size(type));
  309. }
  310. }
  311. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  312. switch (type) {
  313. case UPB_TYPE_STRING:
  314. case UPB_TYPE_BYTES:
  315. case UPB_TYPE_MESSAGE: {
  316. VALUE val1 = DEREF(mem1, VALUE);
  317. VALUE val2 = DEREF(mem2, VALUE);
  318. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  319. return ret == Qtrue;
  320. }
  321. default:
  322. return !memcmp(mem1, mem2, native_slot_size(type));
  323. }
  324. }
  325. // -----------------------------------------------------------------------------
  326. // Map field utilities.
  327. // -----------------------------------------------------------------------------
  328. bool is_map_field(const upb_fielddef* field) {
  329. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  330. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  331. return false;
  332. }
  333. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  334. return upb_msgdef_mapentry(subdef);
  335. }
  336. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  337. assert(is_map_field(field));
  338. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  339. return map_entry_key(subdef);
  340. }
  341. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  342. assert(is_map_field(field));
  343. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  344. return map_entry_value(subdef);
  345. }
  346. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  347. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  348. assert(key_field != NULL);
  349. return key_field;
  350. }
  351. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  352. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  353. assert(value_field != NULL);
  354. return value_field;
  355. }
  356. // -----------------------------------------------------------------------------
  357. // Memory layout management.
  358. // -----------------------------------------------------------------------------
  359. static size_t align_up_to(size_t offset, size_t granularity) {
  360. // Granularity must be a power of two.
  361. return (offset + granularity - 1) & ~(granularity - 1);
  362. }
  363. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  364. MessageLayout* layout = ALLOC(MessageLayout);
  365. int nfields = upb_msgdef_numfields(msgdef);
  366. layout->fields = ALLOC_N(MessageField, nfields);
  367. upb_msg_field_iter it;
  368. size_t off = 0;
  369. for (upb_msg_field_begin(&it, msgdef);
  370. !upb_msg_field_done(&it);
  371. upb_msg_field_next(&it)) {
  372. const upb_fielddef* field = upb_msg_iter_field(&it);
  373. if (upb_fielddef_containingoneof(field)) {
  374. // Oneofs are handled separately below.
  375. continue;
  376. }
  377. // Allocate |field_size| bytes for this field in the layout.
  378. size_t field_size = 0;
  379. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  380. field_size = sizeof(VALUE);
  381. } else {
  382. field_size = native_slot_size(upb_fielddef_type(field));
  383. }
  384. // Align current offset up to |size| granularity.
  385. off = align_up_to(off, field_size);
  386. layout->fields[upb_fielddef_index(field)].offset = off;
  387. layout->fields[upb_fielddef_index(field)].case_offset = MESSAGE_FIELD_NO_CASE;
  388. off += field_size;
  389. }
  390. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  391. // one slot per oneof.
  392. //
  393. // We assign all value slots first, then pack the 'case' fields at the end,
  394. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  395. // bytes respectively and we want to avoid alignment overhead.
  396. //
  397. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  398. // space for oneof cases is conceptually as wide as field tag numbers. In
  399. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  400. // members (8 or 16 bits respectively), so conceivably we could assign
  401. // consecutive case numbers and then pick a smaller oneof case slot size, but
  402. // the complexity to implement this indirection is probably not worthwhile.
  403. upb_msg_oneof_iter oit;
  404. for (upb_msg_oneof_begin(&oit, msgdef);
  405. !upb_msg_oneof_done(&oit);
  406. upb_msg_oneof_next(&oit)) {
  407. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  408. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  409. // all fields.
  410. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  411. // Align the offset.
  412. off = align_up_to(off, field_size);
  413. // Assign all fields in the oneof this same offset.
  414. upb_oneof_iter fit;
  415. for (upb_oneof_begin(&fit, oneof);
  416. !upb_oneof_done(&fit);
  417. upb_oneof_next(&fit)) {
  418. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  419. layout->fields[upb_fielddef_index(field)].offset = off;
  420. }
  421. off += field_size;
  422. }
  423. // Now the case fields.
  424. for (upb_msg_oneof_begin(&oit, msgdef);
  425. !upb_msg_oneof_done(&oit);
  426. upb_msg_oneof_next(&oit)) {
  427. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  428. size_t field_size = sizeof(uint32_t);
  429. // Align the offset.
  430. off = (off + field_size - 1) & ~(field_size - 1);
  431. // Assign all fields in the oneof this same offset.
  432. upb_oneof_iter fit;
  433. for (upb_oneof_begin(&fit, oneof);
  434. !upb_oneof_done(&fit);
  435. upb_oneof_next(&fit)) {
  436. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  437. layout->fields[upb_fielddef_index(field)].case_offset = off;
  438. }
  439. off += field_size;
  440. }
  441. layout->size = off;
  442. layout->msgdef = msgdef;
  443. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  444. return layout;
  445. }
  446. void free_layout(MessageLayout* layout) {
  447. xfree(layout->fields);
  448. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  449. xfree(layout);
  450. }
  451. VALUE field_type_class(const upb_fielddef* field) {
  452. VALUE type_class = Qnil;
  453. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  454. VALUE submsgdesc =
  455. get_def_obj(upb_fielddef_subdef(field));
  456. type_class = Descriptor_msgclass(submsgdesc);
  457. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  458. VALUE subenumdesc =
  459. get_def_obj(upb_fielddef_subdef(field));
  460. type_class = EnumDescriptor_enummodule(subenumdesc);
  461. }
  462. return type_class;
  463. }
  464. static void* slot_memory(MessageLayout* layout,
  465. const void* storage,
  466. const upb_fielddef* field) {
  467. return ((uint8_t *)storage) +
  468. layout->fields[upb_fielddef_index(field)].offset;
  469. }
  470. static uint32_t* slot_oneof_case(MessageLayout* layout,
  471. const void* storage,
  472. const upb_fielddef* field) {
  473. return (uint32_t *)(((uint8_t *)storage) +
  474. layout->fields[upb_fielddef_index(field)].case_offset);
  475. }
  476. VALUE layout_get(MessageLayout* layout,
  477. const void* storage,
  478. const upb_fielddef* field) {
  479. void* memory = slot_memory(layout, storage, field);
  480. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  481. if (upb_fielddef_containingoneof(field)) {
  482. if (*oneof_case != upb_fielddef_number(field)) {
  483. return Qnil;
  484. }
  485. return native_slot_get(upb_fielddef_type(field),
  486. field_type_class(field),
  487. memory);
  488. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  489. return *((VALUE *)memory);
  490. } else {
  491. return native_slot_get(upb_fielddef_type(field),
  492. field_type_class(field),
  493. memory);
  494. }
  495. }
  496. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  497. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  498. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  499. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  500. rb_raise(rb_eTypeError, "Expected repeated field array");
  501. }
  502. RepeatedField* self = ruby_to_RepeatedField(val);
  503. if (self->field_type != upb_fielddef_type(field)) {
  504. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  505. }
  506. if (self->field_type == UPB_TYPE_MESSAGE ||
  507. self->field_type == UPB_TYPE_ENUM) {
  508. if (self->field_type_class !=
  509. get_def_obj(upb_fielddef_subdef(field))) {
  510. rb_raise(rb_eTypeError,
  511. "Repeated field array has wrong message/enum class");
  512. }
  513. }
  514. }
  515. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  516. assert(is_map_field(field));
  517. const upb_fielddef* key_field = map_field_key(field);
  518. const upb_fielddef* value_field = map_field_value(field);
  519. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  520. RTYPEDDATA_TYPE(val) != &Map_type) {
  521. rb_raise(rb_eTypeError, "Expected Map instance");
  522. }
  523. Map* self = ruby_to_Map(val);
  524. if (self->key_type != upb_fielddef_type(key_field)) {
  525. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  526. }
  527. if (self->value_type != upb_fielddef_type(value_field)) {
  528. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  529. }
  530. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  531. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  532. if (self->value_type_class !=
  533. get_def_obj(upb_fielddef_subdef(value_field))) {
  534. rb_raise(rb_eTypeError,
  535. "Map value type has wrong message/enum class");
  536. }
  537. }
  538. }
  539. void layout_set(MessageLayout* layout,
  540. void* storage,
  541. const upb_fielddef* field,
  542. VALUE val) {
  543. void* memory = slot_memory(layout, storage, field);
  544. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  545. if (upb_fielddef_containingoneof(field)) {
  546. if (val == Qnil) {
  547. // Assigning nil to a oneof field clears the oneof completely.
  548. *oneof_case = ONEOF_CASE_NONE;
  549. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  550. } else {
  551. // The transition between field types for a single oneof (union) slot is
  552. // somewhat complex because we need to ensure that a GC triggered at any
  553. // point by a call into the Ruby VM sees a valid state for this field and
  554. // does not either go off into the weeds (following what it thinks is a
  555. // VALUE but is actually a different field type) or miss an object (seeing
  556. // what it thinks is a primitive field but is actually a VALUE for the new
  557. // field type).
  558. //
  559. // In order for the transition to be safe, the oneof case slot must be in
  560. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  561. // use native_slot_set_value_and_case(), which ensures that both the value
  562. // and case number are altered atomically (w.r.t. the Ruby VM).
  563. native_slot_set_value_and_case(
  564. upb_fielddef_type(field), field_type_class(field),
  565. memory, val,
  566. oneof_case, upb_fielddef_number(field));
  567. }
  568. } else if (is_map_field(field)) {
  569. check_map_field_type(val, field);
  570. DEREF(memory, VALUE) = val;
  571. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  572. check_repeated_field_type(val, field);
  573. DEREF(memory, VALUE) = val;
  574. } else {
  575. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  576. memory, val);
  577. }
  578. }
  579. void layout_init(MessageLayout* layout,
  580. void* storage) {
  581. upb_msg_field_iter it;
  582. for (upb_msg_field_begin(&it, layout->msgdef);
  583. !upb_msg_field_done(&it);
  584. upb_msg_field_next(&it)) {
  585. const upb_fielddef* field = upb_msg_iter_field(&it);
  586. void* memory = slot_memory(layout, storage, field);
  587. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  588. if (upb_fielddef_containingoneof(field)) {
  589. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  590. *oneof_case = ONEOF_CASE_NONE;
  591. } else if (is_map_field(field)) {
  592. VALUE map = Qnil;
  593. const upb_fielddef* key_field = map_field_key(field);
  594. const upb_fielddef* value_field = map_field_value(field);
  595. VALUE type_class = field_type_class(value_field);
  596. if (type_class != Qnil) {
  597. VALUE args[3] = {
  598. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  599. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  600. type_class,
  601. };
  602. map = rb_class_new_instance(3, args, cMap);
  603. } else {
  604. VALUE args[2] = {
  605. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  606. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  607. };
  608. map = rb_class_new_instance(2, args, cMap);
  609. }
  610. DEREF(memory, VALUE) = map;
  611. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  612. VALUE ary = Qnil;
  613. VALUE type_class = field_type_class(field);
  614. if (type_class != Qnil) {
  615. VALUE args[2] = {
  616. fieldtype_to_ruby(upb_fielddef_type(field)),
  617. type_class,
  618. };
  619. ary = rb_class_new_instance(2, args, cRepeatedField);
  620. } else {
  621. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  622. ary = rb_class_new_instance(1, args, cRepeatedField);
  623. }
  624. DEREF(memory, VALUE) = ary;
  625. } else {
  626. native_slot_init(upb_fielddef_type(field), memory);
  627. }
  628. }
  629. }
  630. void layout_mark(MessageLayout* layout, void* storage) {
  631. upb_msg_field_iter it;
  632. for (upb_msg_field_begin(&it, layout->msgdef);
  633. !upb_msg_field_done(&it);
  634. upb_msg_field_next(&it)) {
  635. const upb_fielddef* field = upb_msg_iter_field(&it);
  636. void* memory = slot_memory(layout, storage, field);
  637. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  638. if (upb_fielddef_containingoneof(field)) {
  639. if (*oneof_case == upb_fielddef_number(field)) {
  640. native_slot_mark(upb_fielddef_type(field), memory);
  641. }
  642. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  643. rb_gc_mark(DEREF(memory, VALUE));
  644. } else {
  645. native_slot_mark(upb_fielddef_type(field), memory);
  646. }
  647. }
  648. }
  649. void layout_dup(MessageLayout* layout, void* to, void* from) {
  650. upb_msg_field_iter it;
  651. for (upb_msg_field_begin(&it, layout->msgdef);
  652. !upb_msg_field_done(&it);
  653. upb_msg_field_next(&it)) {
  654. const upb_fielddef* field = upb_msg_iter_field(&it);
  655. void* to_memory = slot_memory(layout, to, field);
  656. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  657. void* from_memory = slot_memory(layout, from, field);
  658. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  659. if (upb_fielddef_containingoneof(field)) {
  660. if (*from_oneof_case == upb_fielddef_number(field)) {
  661. *to_oneof_case = *from_oneof_case;
  662. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  663. }
  664. } else if (is_map_field(field)) {
  665. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  666. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  667. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  668. } else {
  669. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  670. }
  671. }
  672. }
  673. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  674. upb_msg_field_iter it;
  675. for (upb_msg_field_begin(&it, layout->msgdef);
  676. !upb_msg_field_done(&it);
  677. upb_msg_field_next(&it)) {
  678. const upb_fielddef* field = upb_msg_iter_field(&it);
  679. void* to_memory = slot_memory(layout, to, field);
  680. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  681. void* from_memory = slot_memory(layout, from, field);
  682. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  683. if (upb_fielddef_containingoneof(field)) {
  684. if (*from_oneof_case == upb_fielddef_number(field)) {
  685. *to_oneof_case = *from_oneof_case;
  686. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  687. }
  688. } else if (is_map_field(field)) {
  689. DEREF(to_memory, VALUE) =
  690. Map_deep_copy(DEREF(from_memory, VALUE));
  691. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  692. DEREF(to_memory, VALUE) =
  693. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  694. } else {
  695. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  696. }
  697. }
  698. }
  699. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  700. upb_msg_field_iter it;
  701. for (upb_msg_field_begin(&it, layout->msgdef);
  702. !upb_msg_field_done(&it);
  703. upb_msg_field_next(&it)) {
  704. const upb_fielddef* field = upb_msg_iter_field(&it);
  705. void* msg1_memory = slot_memory(layout, msg1, field);
  706. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, field);
  707. void* msg2_memory = slot_memory(layout, msg2, field);
  708. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, field);
  709. if (upb_fielddef_containingoneof(field)) {
  710. if (*msg1_oneof_case != *msg2_oneof_case ||
  711. (*msg1_oneof_case == upb_fielddef_number(field) &&
  712. !native_slot_eq(upb_fielddef_type(field),
  713. msg1_memory,
  714. msg2_memory))) {
  715. return Qfalse;
  716. }
  717. } else if (is_map_field(field)) {
  718. if (!Map_eq(DEREF(msg1_memory, VALUE),
  719. DEREF(msg2_memory, VALUE))) {
  720. return Qfalse;
  721. }
  722. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  723. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  724. DEREF(msg2_memory, VALUE))) {
  725. return Qfalse;
  726. }
  727. } else {
  728. if (!native_slot_eq(upb_fielddef_type(field),
  729. msg1_memory, msg2_memory)) {
  730. return Qfalse;
  731. }
  732. }
  733. }
  734. return Qtrue;
  735. }
  736. VALUE layout_hash(MessageLayout* layout, void* storage) {
  737. upb_msg_field_iter it;
  738. st_index_t h = rb_hash_start(0);
  739. VALUE hash_sym = rb_intern("hash");
  740. for (upb_msg_field_begin(&it, layout->msgdef);
  741. !upb_msg_field_done(&it);
  742. upb_msg_field_next(&it)) {
  743. const upb_fielddef* field = upb_msg_iter_field(&it);
  744. VALUE field_val = layout_get(layout, storage, field);
  745. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  746. }
  747. h = rb_hash_end(h);
  748. return INT2FIX(h);
  749. }
  750. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  751. VALUE str = rb_str_new2("");
  752. upb_msg_field_iter it;
  753. bool first = true;
  754. for (upb_msg_field_begin(&it, layout->msgdef);
  755. !upb_msg_field_done(&it);
  756. upb_msg_field_next(&it)) {
  757. const upb_fielddef* field = upb_msg_iter_field(&it);
  758. VALUE field_val = layout_get(layout, storage, field);
  759. if (!first) {
  760. str = rb_str_cat2(str, ", ");
  761. } else {
  762. first = false;
  763. }
  764. str = rb_str_cat2(str, upb_fielddef_name(field));
  765. str = rb_str_cat2(str, ": ");
  766. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  767. }
  768. return str;
  769. }