storage.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 =
  388. MESSAGE_FIELD_NO_CASE;
  389. off += field_size;
  390. }
  391. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  392. // one slot per oneof.
  393. //
  394. // We assign all value slots first, then pack the 'case' fields at the end,
  395. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  396. // bytes respectively and we want to avoid alignment overhead.
  397. //
  398. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  399. // space for oneof cases is conceptually as wide as field tag numbers. In
  400. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  401. // members (8 or 16 bits respectively), so conceivably we could assign
  402. // consecutive case numbers and then pick a smaller oneof case slot size, but
  403. // the complexity to implement this indirection is probably not worthwhile.
  404. upb_msg_oneof_iter oit;
  405. for (upb_msg_oneof_begin(&oit, msgdef);
  406. !upb_msg_oneof_done(&oit);
  407. upb_msg_oneof_next(&oit)) {
  408. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  409. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  410. // all fields.
  411. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  412. // Align the offset.
  413. off = align_up_to(off, field_size);
  414. // Assign all fields in the oneof this same offset.
  415. upb_oneof_iter fit;
  416. for (upb_oneof_begin(&fit, oneof);
  417. !upb_oneof_done(&fit);
  418. upb_oneof_next(&fit)) {
  419. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  420. layout->fields[upb_fielddef_index(field)].offset = off;
  421. }
  422. off += field_size;
  423. }
  424. // Now the case fields.
  425. for (upb_msg_oneof_begin(&oit, msgdef);
  426. !upb_msg_oneof_done(&oit);
  427. upb_msg_oneof_next(&oit)) {
  428. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  429. size_t field_size = sizeof(uint32_t);
  430. // Align the offset.
  431. off = (off + field_size - 1) & ~(field_size - 1);
  432. // Assign all fields in the oneof this same offset.
  433. upb_oneof_iter fit;
  434. for (upb_oneof_begin(&fit, oneof);
  435. !upb_oneof_done(&fit);
  436. upb_oneof_next(&fit)) {
  437. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  438. layout->fields[upb_fielddef_index(field)].case_offset = off;
  439. }
  440. off += field_size;
  441. }
  442. layout->size = off;
  443. layout->msgdef = msgdef;
  444. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  445. return layout;
  446. }
  447. void free_layout(MessageLayout* layout) {
  448. xfree(layout->fields);
  449. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  450. xfree(layout);
  451. }
  452. VALUE field_type_class(const upb_fielddef* field) {
  453. VALUE type_class = Qnil;
  454. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  455. VALUE submsgdesc =
  456. get_def_obj(upb_fielddef_subdef(field));
  457. type_class = Descriptor_msgclass(submsgdesc);
  458. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  459. VALUE subenumdesc =
  460. get_def_obj(upb_fielddef_subdef(field));
  461. type_class = EnumDescriptor_enummodule(subenumdesc);
  462. }
  463. return type_class;
  464. }
  465. static void* slot_memory(MessageLayout* layout,
  466. const void* storage,
  467. const upb_fielddef* field) {
  468. return ((uint8_t *)storage) +
  469. layout->fields[upb_fielddef_index(field)].offset;
  470. }
  471. static uint32_t* slot_oneof_case(MessageLayout* layout,
  472. const void* storage,
  473. const upb_fielddef* field) {
  474. return (uint32_t *)(((uint8_t *)storage) +
  475. layout->fields[upb_fielddef_index(field)].case_offset);
  476. }
  477. VALUE layout_get(MessageLayout* layout,
  478. const void* storage,
  479. const upb_fielddef* field) {
  480. void* memory = slot_memory(layout, storage, field);
  481. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  482. if (upb_fielddef_containingoneof(field)) {
  483. if (*oneof_case != upb_fielddef_number(field)) {
  484. return Qnil;
  485. }
  486. return native_slot_get(upb_fielddef_type(field),
  487. field_type_class(field),
  488. memory);
  489. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  490. return *((VALUE *)memory);
  491. } else {
  492. return native_slot_get(upb_fielddef_type(field),
  493. field_type_class(field),
  494. memory);
  495. }
  496. }
  497. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  498. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  499. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  500. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  501. rb_raise(rb_eTypeError, "Expected repeated field array");
  502. }
  503. RepeatedField* self = ruby_to_RepeatedField(val);
  504. if (self->field_type != upb_fielddef_type(field)) {
  505. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  506. }
  507. if (self->field_type == UPB_TYPE_MESSAGE ||
  508. self->field_type == UPB_TYPE_ENUM) {
  509. if (self->field_type_class !=
  510. get_def_obj(upb_fielddef_subdef(field))) {
  511. rb_raise(rb_eTypeError,
  512. "Repeated field array has wrong message/enum class");
  513. }
  514. }
  515. }
  516. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  517. assert(is_map_field(field));
  518. const upb_fielddef* key_field = map_field_key(field);
  519. const upb_fielddef* value_field = map_field_value(field);
  520. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  521. RTYPEDDATA_TYPE(val) != &Map_type) {
  522. rb_raise(rb_eTypeError, "Expected Map instance");
  523. }
  524. Map* self = ruby_to_Map(val);
  525. if (self->key_type != upb_fielddef_type(key_field)) {
  526. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  527. }
  528. if (self->value_type != upb_fielddef_type(value_field)) {
  529. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  530. }
  531. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  532. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  533. if (self->value_type_class !=
  534. get_def_obj(upb_fielddef_subdef(value_field))) {
  535. rb_raise(rb_eTypeError,
  536. "Map value type has wrong message/enum class");
  537. }
  538. }
  539. }
  540. void layout_set(MessageLayout* layout,
  541. void* storage,
  542. const upb_fielddef* field,
  543. VALUE val) {
  544. void* memory = slot_memory(layout, storage, field);
  545. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  546. if (upb_fielddef_containingoneof(field)) {
  547. if (val == Qnil) {
  548. // Assigning nil to a oneof field clears the oneof completely.
  549. *oneof_case = ONEOF_CASE_NONE;
  550. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  551. } else {
  552. // The transition between field types for a single oneof (union) slot is
  553. // somewhat complex because we need to ensure that a GC triggered at any
  554. // point by a call into the Ruby VM sees a valid state for this field and
  555. // does not either go off into the weeds (following what it thinks is a
  556. // VALUE but is actually a different field type) or miss an object (seeing
  557. // what it thinks is a primitive field but is actually a VALUE for the new
  558. // field type).
  559. //
  560. // In order for the transition to be safe, the oneof case slot must be in
  561. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  562. // use native_slot_set_value_and_case(), which ensures that both the value
  563. // and case number are altered atomically (w.r.t. the Ruby VM).
  564. native_slot_set_value_and_case(
  565. upb_fielddef_type(field), field_type_class(field),
  566. memory, val,
  567. oneof_case, upb_fielddef_number(field));
  568. }
  569. } else if (is_map_field(field)) {
  570. check_map_field_type(val, field);
  571. DEREF(memory, VALUE) = val;
  572. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  573. check_repeated_field_type(val, field);
  574. DEREF(memory, VALUE) = val;
  575. } else {
  576. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  577. memory, val);
  578. }
  579. }
  580. void layout_init(MessageLayout* layout,
  581. void* storage) {
  582. upb_msg_field_iter it;
  583. for (upb_msg_field_begin(&it, layout->msgdef);
  584. !upb_msg_field_done(&it);
  585. upb_msg_field_next(&it)) {
  586. const upb_fielddef* field = upb_msg_iter_field(&it);
  587. void* memory = slot_memory(layout, storage, field);
  588. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  589. if (upb_fielddef_containingoneof(field)) {
  590. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  591. *oneof_case = ONEOF_CASE_NONE;
  592. } else if (is_map_field(field)) {
  593. VALUE map = Qnil;
  594. const upb_fielddef* key_field = map_field_key(field);
  595. const upb_fielddef* value_field = map_field_value(field);
  596. VALUE type_class = field_type_class(value_field);
  597. if (type_class != Qnil) {
  598. VALUE args[3] = {
  599. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  600. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  601. type_class,
  602. };
  603. map = rb_class_new_instance(3, args, cMap);
  604. } else {
  605. VALUE args[2] = {
  606. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  607. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  608. };
  609. map = rb_class_new_instance(2, args, cMap);
  610. }
  611. DEREF(memory, VALUE) = map;
  612. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  613. VALUE ary = Qnil;
  614. VALUE type_class = field_type_class(field);
  615. if (type_class != Qnil) {
  616. VALUE args[2] = {
  617. fieldtype_to_ruby(upb_fielddef_type(field)),
  618. type_class,
  619. };
  620. ary = rb_class_new_instance(2, args, cRepeatedField);
  621. } else {
  622. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  623. ary = rb_class_new_instance(1, args, cRepeatedField);
  624. }
  625. DEREF(memory, VALUE) = ary;
  626. } else {
  627. native_slot_init(upb_fielddef_type(field), memory);
  628. }
  629. }
  630. }
  631. void layout_mark(MessageLayout* layout, void* storage) {
  632. upb_msg_field_iter it;
  633. for (upb_msg_field_begin(&it, layout->msgdef);
  634. !upb_msg_field_done(&it);
  635. upb_msg_field_next(&it)) {
  636. const upb_fielddef* field = upb_msg_iter_field(&it);
  637. void* memory = slot_memory(layout, storage, field);
  638. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  639. if (upb_fielddef_containingoneof(field)) {
  640. if (*oneof_case == upb_fielddef_number(field)) {
  641. native_slot_mark(upb_fielddef_type(field), memory);
  642. }
  643. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  644. rb_gc_mark(DEREF(memory, VALUE));
  645. } else {
  646. native_slot_mark(upb_fielddef_type(field), memory);
  647. }
  648. }
  649. }
  650. void layout_dup(MessageLayout* layout, void* to, void* from) {
  651. upb_msg_field_iter it;
  652. for (upb_msg_field_begin(&it, layout->msgdef);
  653. !upb_msg_field_done(&it);
  654. upb_msg_field_next(&it)) {
  655. const upb_fielddef* field = upb_msg_iter_field(&it);
  656. void* to_memory = slot_memory(layout, to, field);
  657. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  658. void* from_memory = slot_memory(layout, from, field);
  659. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  660. if (upb_fielddef_containingoneof(field)) {
  661. if (*from_oneof_case == upb_fielddef_number(field)) {
  662. *to_oneof_case = *from_oneof_case;
  663. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  664. }
  665. } else if (is_map_field(field)) {
  666. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  667. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  668. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  669. } else {
  670. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  671. }
  672. }
  673. }
  674. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  675. upb_msg_field_iter it;
  676. for (upb_msg_field_begin(&it, layout->msgdef);
  677. !upb_msg_field_done(&it);
  678. upb_msg_field_next(&it)) {
  679. const upb_fielddef* field = upb_msg_iter_field(&it);
  680. void* to_memory = slot_memory(layout, to, field);
  681. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  682. void* from_memory = slot_memory(layout, from, field);
  683. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  684. if (upb_fielddef_containingoneof(field)) {
  685. if (*from_oneof_case == upb_fielddef_number(field)) {
  686. *to_oneof_case = *from_oneof_case;
  687. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  688. }
  689. } else if (is_map_field(field)) {
  690. DEREF(to_memory, VALUE) =
  691. Map_deep_copy(DEREF(from_memory, VALUE));
  692. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  693. DEREF(to_memory, VALUE) =
  694. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  695. } else {
  696. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  697. }
  698. }
  699. }
  700. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  701. upb_msg_field_iter it;
  702. for (upb_msg_field_begin(&it, layout->msgdef);
  703. !upb_msg_field_done(&it);
  704. upb_msg_field_next(&it)) {
  705. const upb_fielddef* field = upb_msg_iter_field(&it);
  706. void* msg1_memory = slot_memory(layout, msg1, field);
  707. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, field);
  708. void* msg2_memory = slot_memory(layout, msg2, field);
  709. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, field);
  710. if (upb_fielddef_containingoneof(field)) {
  711. if (*msg1_oneof_case != *msg2_oneof_case ||
  712. (*msg1_oneof_case == upb_fielddef_number(field) &&
  713. !native_slot_eq(upb_fielddef_type(field),
  714. msg1_memory,
  715. msg2_memory))) {
  716. return Qfalse;
  717. }
  718. } else if (is_map_field(field)) {
  719. if (!Map_eq(DEREF(msg1_memory, VALUE),
  720. DEREF(msg2_memory, VALUE))) {
  721. return Qfalse;
  722. }
  723. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  724. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  725. DEREF(msg2_memory, VALUE))) {
  726. return Qfalse;
  727. }
  728. } else {
  729. if (!native_slot_eq(upb_fielddef_type(field),
  730. msg1_memory, msg2_memory)) {
  731. return Qfalse;
  732. }
  733. }
  734. }
  735. return Qtrue;
  736. }
  737. VALUE layout_hash(MessageLayout* layout, void* storage) {
  738. upb_msg_field_iter it;
  739. st_index_t h = rb_hash_start(0);
  740. VALUE hash_sym = rb_intern("hash");
  741. for (upb_msg_field_begin(&it, layout->msgdef);
  742. !upb_msg_field_done(&it);
  743. upb_msg_field_next(&it)) {
  744. const upb_fielddef* field = upb_msg_iter_field(&it);
  745. VALUE field_val = layout_get(layout, storage, field);
  746. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  747. }
  748. h = rb_hash_end(h);
  749. return INT2FIX(h);
  750. }
  751. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  752. VALUE str = rb_str_new2("");
  753. upb_msg_field_iter it;
  754. bool first = true;
  755. for (upb_msg_field_begin(&it, layout->msgdef);
  756. !upb_msg_field_done(&it);
  757. upb_msg_field_next(&it)) {
  758. const upb_fielddef* field = upb_msg_iter_field(&it);
  759. VALUE field_val = layout_get(layout, storage, field);
  760. if (!first) {
  761. str = rb_str_cat2(str, ", ");
  762. } else {
  763. first = false;
  764. }
  765. str = rb_str_cat2(str, upb_fielddef_name(field));
  766. str = rb_str_cat2(str, ": ");
  767. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  768. }
  769. return str;
  770. }