storage.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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) != type_class) {
  147. rb_raise(rb_eTypeError,
  148. "Invalid type %s to assign to submessage field.",
  149. rb_class2name(CLASS_OF(value)));
  150. }
  151. DEREF(memory, VALUE) = value;
  152. break;
  153. }
  154. case UPB_TYPE_ENUM: {
  155. if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  156. rb_raise(rb_eTypeError,
  157. "Expected number or symbol type for enum field.");
  158. }
  159. int32_t int_val = 0;
  160. if (TYPE(value) == T_SYMBOL) {
  161. // Ensure that the given symbol exists in the enum module.
  162. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  163. if (lookup == Qnil) {
  164. rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
  165. } else {
  166. int_val = NUM2INT(lookup);
  167. }
  168. } else {
  169. native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
  170. int_val = NUM2INT(value);
  171. }
  172. DEREF(memory, int32_t) = int_val;
  173. break;
  174. }
  175. case UPB_TYPE_INT32:
  176. case UPB_TYPE_INT64:
  177. case UPB_TYPE_UINT32:
  178. case UPB_TYPE_UINT64:
  179. native_slot_check_int_range_precision(type, value);
  180. switch (type) {
  181. case UPB_TYPE_INT32:
  182. DEREF(memory, int32_t) = NUM2INT(value);
  183. break;
  184. case UPB_TYPE_INT64:
  185. DEREF(memory, int64_t) = NUM2LL(value);
  186. break;
  187. case UPB_TYPE_UINT32:
  188. DEREF(memory, uint32_t) = NUM2UINT(value);
  189. break;
  190. case UPB_TYPE_UINT64:
  191. DEREF(memory, uint64_t) = NUM2ULL(value);
  192. break;
  193. default:
  194. break;
  195. }
  196. break;
  197. default:
  198. break;
  199. }
  200. if (case_memory != NULL) {
  201. *case_memory = case_number;
  202. }
  203. }
  204. VALUE native_slot_get(upb_fieldtype_t type,
  205. VALUE type_class,
  206. const void* memory) {
  207. switch (type) {
  208. case UPB_TYPE_FLOAT:
  209. return DBL2NUM(DEREF(memory, float));
  210. case UPB_TYPE_DOUBLE:
  211. return DBL2NUM(DEREF(memory, double));
  212. case UPB_TYPE_BOOL:
  213. return DEREF(memory, int8_t) ? Qtrue : Qfalse;
  214. case UPB_TYPE_STRING:
  215. case UPB_TYPE_BYTES:
  216. case UPB_TYPE_MESSAGE:
  217. return DEREF(memory, VALUE);
  218. case UPB_TYPE_ENUM: {
  219. int32_t val = DEREF(memory, int32_t);
  220. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  221. if (symbol == Qnil) {
  222. return INT2NUM(val);
  223. } else {
  224. return symbol;
  225. }
  226. }
  227. case UPB_TYPE_INT32:
  228. return INT2NUM(DEREF(memory, int32_t));
  229. case UPB_TYPE_INT64:
  230. return LL2NUM(DEREF(memory, int64_t));
  231. case UPB_TYPE_UINT32:
  232. return UINT2NUM(DEREF(memory, uint32_t));
  233. case UPB_TYPE_UINT64:
  234. return ULL2NUM(DEREF(memory, uint64_t));
  235. default:
  236. return Qnil;
  237. }
  238. }
  239. void native_slot_init(upb_fieldtype_t type, void* memory) {
  240. switch (type) {
  241. case UPB_TYPE_FLOAT:
  242. DEREF(memory, float) = 0.0;
  243. break;
  244. case UPB_TYPE_DOUBLE:
  245. DEREF(memory, double) = 0.0;
  246. break;
  247. case UPB_TYPE_BOOL:
  248. DEREF(memory, int8_t) = 0;
  249. break;
  250. case UPB_TYPE_STRING:
  251. case UPB_TYPE_BYTES:
  252. DEREF(memory, VALUE) = rb_str_new2("");
  253. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  254. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  255. break;
  256. case UPB_TYPE_MESSAGE:
  257. DEREF(memory, VALUE) = Qnil;
  258. break;
  259. case UPB_TYPE_ENUM:
  260. case UPB_TYPE_INT32:
  261. DEREF(memory, int32_t) = 0;
  262. break;
  263. case UPB_TYPE_INT64:
  264. DEREF(memory, int64_t) = 0;
  265. break;
  266. case UPB_TYPE_UINT32:
  267. DEREF(memory, uint32_t) = 0;
  268. break;
  269. case UPB_TYPE_UINT64:
  270. DEREF(memory, uint64_t) = 0;
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  277. switch (type) {
  278. case UPB_TYPE_STRING:
  279. case UPB_TYPE_BYTES:
  280. case UPB_TYPE_MESSAGE:
  281. rb_gc_mark(DEREF(memory, VALUE));
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  288. memcpy(to, from, native_slot_size(type));
  289. }
  290. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  291. switch (type) {
  292. case UPB_TYPE_STRING:
  293. case UPB_TYPE_BYTES: {
  294. VALUE from_val = DEREF(from, VALUE);
  295. DEREF(to, VALUE) = (from_val != Qnil) ?
  296. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  297. break;
  298. }
  299. case UPB_TYPE_MESSAGE: {
  300. VALUE from_val = DEREF(from, VALUE);
  301. DEREF(to, VALUE) = (from_val != Qnil) ?
  302. Message_deep_copy(from_val) : Qnil;
  303. break;
  304. }
  305. default:
  306. memcpy(to, from, native_slot_size(type));
  307. }
  308. }
  309. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  310. switch (type) {
  311. case UPB_TYPE_STRING:
  312. case UPB_TYPE_BYTES:
  313. case UPB_TYPE_MESSAGE: {
  314. VALUE val1 = DEREF(mem1, VALUE);
  315. VALUE val2 = DEREF(mem2, VALUE);
  316. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  317. return ret == Qtrue;
  318. }
  319. default:
  320. return !memcmp(mem1, mem2, native_slot_size(type));
  321. }
  322. }
  323. // -----------------------------------------------------------------------------
  324. // Map field utilities.
  325. // -----------------------------------------------------------------------------
  326. bool is_map_field(const upb_fielddef* field) {
  327. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  328. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  329. return false;
  330. }
  331. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  332. return upb_msgdef_mapentry(subdef);
  333. }
  334. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  335. assert(is_map_field(field));
  336. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  337. return map_entry_key(subdef);
  338. }
  339. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  340. assert(is_map_field(field));
  341. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  342. return map_entry_value(subdef);
  343. }
  344. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  345. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  346. assert(key_field != NULL);
  347. return key_field;
  348. }
  349. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  350. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  351. assert(value_field != NULL);
  352. return value_field;
  353. }
  354. // -----------------------------------------------------------------------------
  355. // Memory layout management.
  356. // -----------------------------------------------------------------------------
  357. static size_t align_up_to(size_t offset, size_t granularity) {
  358. // Granularity must be a power of two.
  359. return (offset + granularity - 1) & ~(granularity - 1);
  360. }
  361. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  362. MessageLayout* layout = ALLOC(MessageLayout);
  363. int nfields = upb_msgdef_numfields(msgdef);
  364. layout->fields = ALLOC_N(MessageField, nfields);
  365. upb_msg_field_iter it;
  366. size_t off = 0;
  367. for (upb_msg_field_begin(&it, msgdef);
  368. !upb_msg_field_done(&it);
  369. upb_msg_field_next(&it)) {
  370. const upb_fielddef* field = upb_msg_iter_field(&it);
  371. if (upb_fielddef_containingoneof(field)) {
  372. // Oneofs are handled separately below.
  373. continue;
  374. }
  375. // Allocate |field_size| bytes for this field in the layout.
  376. size_t field_size = 0;
  377. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  378. field_size = sizeof(VALUE);
  379. } else {
  380. field_size = native_slot_size(upb_fielddef_type(field));
  381. }
  382. // Align current offset up to |size| granularity.
  383. off = align_up_to(off, field_size);
  384. layout->fields[upb_fielddef_index(field)].offset = off;
  385. layout->fields[upb_fielddef_index(field)].case_offset = MESSAGE_FIELD_NO_CASE;
  386. off += field_size;
  387. }
  388. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  389. // one slot per oneof.
  390. //
  391. // We assign all value slots first, then pack the 'case' fields at the end,
  392. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  393. // bytes respectively and we want to avoid alignment overhead.
  394. //
  395. // Note that we reserve 4 bytes (a uint32) per 'case' slot because the value
  396. // space for oneof cases is conceptually as wide as field tag numbers. In
  397. // practice, it's unlikely that a oneof would have more than e.g. 256 or 64K
  398. // members (8 or 16 bits respectively), so conceivably we could assign
  399. // consecutive case numbers and then pick a smaller oneof case slot size, but
  400. // the complexity to implement this indirection is probably not worthwhile.
  401. upb_msg_oneof_iter oit;
  402. for (upb_msg_oneof_begin(&oit, msgdef);
  403. !upb_msg_oneof_done(&oit);
  404. upb_msg_oneof_next(&oit)) {
  405. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  406. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  407. // all fields.
  408. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  409. // Align the offset.
  410. off = align_up_to(off, field_size);
  411. // Assign all fields in the oneof this same offset.
  412. upb_oneof_iter fit;
  413. for (upb_oneof_begin(&fit, oneof);
  414. !upb_oneof_done(&fit);
  415. upb_oneof_next(&fit)) {
  416. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  417. layout->fields[upb_fielddef_index(field)].offset = off;
  418. }
  419. off += field_size;
  420. }
  421. // Now the case fields.
  422. for (upb_msg_oneof_begin(&oit, msgdef);
  423. !upb_msg_oneof_done(&oit);
  424. upb_msg_oneof_next(&oit)) {
  425. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  426. size_t field_size = sizeof(uint32_t);
  427. // Align the offset.
  428. off = (off + field_size - 1) & ~(field_size - 1);
  429. // Assign all fields in the oneof this same offset.
  430. upb_oneof_iter fit;
  431. for (upb_oneof_begin(&fit, oneof);
  432. !upb_oneof_done(&fit);
  433. upb_oneof_next(&fit)) {
  434. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  435. layout->fields[upb_fielddef_index(field)].case_offset = off;
  436. }
  437. off += field_size;
  438. }
  439. layout->size = off;
  440. layout->msgdef = msgdef;
  441. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  442. return layout;
  443. }
  444. void free_layout(MessageLayout* layout) {
  445. xfree(layout->fields);
  446. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  447. xfree(layout);
  448. }
  449. VALUE field_type_class(const upb_fielddef* field) {
  450. VALUE type_class = Qnil;
  451. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  452. VALUE submsgdesc =
  453. get_def_obj(upb_fielddef_subdef(field));
  454. type_class = Descriptor_msgclass(submsgdesc);
  455. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  456. VALUE subenumdesc =
  457. get_def_obj(upb_fielddef_subdef(field));
  458. type_class = EnumDescriptor_enummodule(subenumdesc);
  459. }
  460. return type_class;
  461. }
  462. static void* slot_memory(MessageLayout* layout,
  463. const void* storage,
  464. const upb_fielddef* field) {
  465. return ((uint8_t *)storage) +
  466. layout->fields[upb_fielddef_index(field)].offset;
  467. }
  468. static uint32_t* slot_oneof_case(MessageLayout* layout,
  469. const void* storage,
  470. const upb_fielddef* field) {
  471. return (uint32_t *)(((uint8_t *)storage) +
  472. layout->fields[upb_fielddef_index(field)].case_offset);
  473. }
  474. VALUE layout_get(MessageLayout* layout,
  475. const void* storage,
  476. const upb_fielddef* field) {
  477. void* memory = slot_memory(layout, storage, field);
  478. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  479. if (upb_fielddef_containingoneof(field)) {
  480. if (*oneof_case != upb_fielddef_number(field)) {
  481. return Qnil;
  482. }
  483. return native_slot_get(upb_fielddef_type(field),
  484. field_type_class(field),
  485. memory);
  486. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  487. return *((VALUE *)memory);
  488. } else {
  489. return native_slot_get(upb_fielddef_type(field),
  490. field_type_class(field),
  491. memory);
  492. }
  493. }
  494. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  495. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  496. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  497. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  498. rb_raise(rb_eTypeError, "Expected repeated field array");
  499. }
  500. RepeatedField* self = ruby_to_RepeatedField(val);
  501. if (self->field_type != upb_fielddef_type(field)) {
  502. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  503. }
  504. if (self->field_type == UPB_TYPE_MESSAGE ||
  505. self->field_type == UPB_TYPE_ENUM) {
  506. if (self->field_type_class !=
  507. get_def_obj(upb_fielddef_subdef(field))) {
  508. rb_raise(rb_eTypeError,
  509. "Repeated field array has wrong message/enum class");
  510. }
  511. }
  512. }
  513. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  514. assert(is_map_field(field));
  515. const upb_fielddef* key_field = map_field_key(field);
  516. const upb_fielddef* value_field = map_field_value(field);
  517. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  518. RTYPEDDATA_TYPE(val) != &Map_type) {
  519. rb_raise(rb_eTypeError, "Expected Map instance");
  520. }
  521. Map* self = ruby_to_Map(val);
  522. if (self->key_type != upb_fielddef_type(key_field)) {
  523. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  524. }
  525. if (self->value_type != upb_fielddef_type(value_field)) {
  526. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  527. }
  528. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  529. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  530. if (self->value_type_class !=
  531. get_def_obj(upb_fielddef_subdef(value_field))) {
  532. rb_raise(rb_eTypeError,
  533. "Map value type has wrong message/enum class");
  534. }
  535. }
  536. }
  537. void layout_set(MessageLayout* layout,
  538. void* storage,
  539. const upb_fielddef* field,
  540. VALUE val) {
  541. void* memory = slot_memory(layout, storage, field);
  542. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  543. if (upb_fielddef_containingoneof(field)) {
  544. if (val == Qnil) {
  545. // Assigning nil to a oneof field clears the oneof completely.
  546. *oneof_case = ONEOF_CASE_NONE;
  547. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  548. } else {
  549. // The transition between field types for a single oneof (union) slot is
  550. // somewhat complex because we need to ensure that a GC triggered at any
  551. // point by a call into the Ruby VM sees a valid state for this field and
  552. // does not either go off into the weeds (following what it thinks is a
  553. // VALUE but is actually a different field type) or miss an object (seeing
  554. // what it thinks is a primitive field but is actually a VALUE for the new
  555. // field type).
  556. //
  557. // In order for the transition to be safe, the oneof case slot must be in
  558. // sync with the value slot whenever the Ruby VM has been called. Thus, we
  559. // use native_slot_set_value_and_case(), which ensures that both the value
  560. // and case number are altered atomically (w.r.t. the Ruby VM).
  561. native_slot_set_value_and_case(
  562. upb_fielddef_type(field), field_type_class(field),
  563. memory, val,
  564. oneof_case, upb_fielddef_number(field));
  565. }
  566. } else if (is_map_field(field)) {
  567. check_map_field_type(val, field);
  568. DEREF(memory, VALUE) = val;
  569. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  570. check_repeated_field_type(val, field);
  571. DEREF(memory, VALUE) = val;
  572. } else {
  573. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  574. memory, val);
  575. }
  576. }
  577. void layout_init(MessageLayout* layout,
  578. void* storage) {
  579. upb_msg_field_iter it;
  580. for (upb_msg_field_begin(&it, layout->msgdef);
  581. !upb_msg_field_done(&it);
  582. upb_msg_field_next(&it)) {
  583. const upb_fielddef* field = upb_msg_iter_field(&it);
  584. void* memory = slot_memory(layout, storage, field);
  585. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  586. if (upb_fielddef_containingoneof(field)) {
  587. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  588. *oneof_case = ONEOF_CASE_NONE;
  589. } else if (is_map_field(field)) {
  590. VALUE map = Qnil;
  591. const upb_fielddef* key_field = map_field_key(field);
  592. const upb_fielddef* value_field = map_field_value(field);
  593. VALUE type_class = field_type_class(value_field);
  594. if (type_class != Qnil) {
  595. VALUE args[3] = {
  596. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  597. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  598. type_class,
  599. };
  600. map = rb_class_new_instance(3, args, cMap);
  601. } else {
  602. VALUE args[2] = {
  603. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  604. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  605. };
  606. map = rb_class_new_instance(2, args, cMap);
  607. }
  608. DEREF(memory, VALUE) = map;
  609. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  610. VALUE ary = Qnil;
  611. VALUE type_class = field_type_class(field);
  612. if (type_class != Qnil) {
  613. VALUE args[2] = {
  614. fieldtype_to_ruby(upb_fielddef_type(field)),
  615. type_class,
  616. };
  617. ary = rb_class_new_instance(2, args, cRepeatedField);
  618. } else {
  619. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  620. ary = rb_class_new_instance(1, args, cRepeatedField);
  621. }
  622. DEREF(memory, VALUE) = ary;
  623. } else {
  624. native_slot_init(upb_fielddef_type(field), memory);
  625. }
  626. }
  627. }
  628. void layout_mark(MessageLayout* layout, void* storage) {
  629. upb_msg_field_iter it;
  630. for (upb_msg_field_begin(&it, layout->msgdef);
  631. !upb_msg_field_done(&it);
  632. upb_msg_field_next(&it)) {
  633. const upb_fielddef* field = upb_msg_iter_field(&it);
  634. void* memory = slot_memory(layout, storage, field);
  635. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  636. if (upb_fielddef_containingoneof(field)) {
  637. if (*oneof_case == upb_fielddef_number(field)) {
  638. native_slot_mark(upb_fielddef_type(field), memory);
  639. }
  640. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  641. rb_gc_mark(DEREF(memory, VALUE));
  642. } else {
  643. native_slot_mark(upb_fielddef_type(field), memory);
  644. }
  645. }
  646. }
  647. void layout_dup(MessageLayout* layout, void* to, void* from) {
  648. upb_msg_field_iter it;
  649. for (upb_msg_field_begin(&it, layout->msgdef);
  650. !upb_msg_field_done(&it);
  651. upb_msg_field_next(&it)) {
  652. const upb_fielddef* field = upb_msg_iter_field(&it);
  653. void* to_memory = slot_memory(layout, to, field);
  654. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  655. void* from_memory = slot_memory(layout, from, field);
  656. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  657. if (upb_fielddef_containingoneof(field)) {
  658. if (*from_oneof_case == upb_fielddef_number(field)) {
  659. *to_oneof_case = *from_oneof_case;
  660. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  661. }
  662. } else if (is_map_field(field)) {
  663. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  664. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  665. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  666. } else {
  667. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  668. }
  669. }
  670. }
  671. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  672. upb_msg_field_iter it;
  673. for (upb_msg_field_begin(&it, layout->msgdef);
  674. !upb_msg_field_done(&it);
  675. upb_msg_field_next(&it)) {
  676. const upb_fielddef* field = upb_msg_iter_field(&it);
  677. void* to_memory = slot_memory(layout, to, field);
  678. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  679. void* from_memory = slot_memory(layout, from, field);
  680. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  681. if (upb_fielddef_containingoneof(field)) {
  682. if (*from_oneof_case == upb_fielddef_number(field)) {
  683. *to_oneof_case = *from_oneof_case;
  684. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  685. }
  686. } else if (is_map_field(field)) {
  687. DEREF(to_memory, VALUE) =
  688. Map_deep_copy(DEREF(from_memory, VALUE));
  689. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  690. DEREF(to_memory, VALUE) =
  691. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  692. } else {
  693. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  694. }
  695. }
  696. }
  697. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  698. upb_msg_field_iter it;
  699. for (upb_msg_field_begin(&it, layout->msgdef);
  700. !upb_msg_field_done(&it);
  701. upb_msg_field_next(&it)) {
  702. const upb_fielddef* field = upb_msg_iter_field(&it);
  703. void* msg1_memory = slot_memory(layout, msg1, field);
  704. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, field);
  705. void* msg2_memory = slot_memory(layout, msg2, field);
  706. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, field);
  707. if (upb_fielddef_containingoneof(field)) {
  708. if (*msg1_oneof_case != *msg2_oneof_case ||
  709. (*msg1_oneof_case == upb_fielddef_number(field) &&
  710. !native_slot_eq(upb_fielddef_type(field),
  711. msg1_memory,
  712. msg2_memory))) {
  713. return Qfalse;
  714. }
  715. } else if (is_map_field(field)) {
  716. if (!Map_eq(DEREF(msg1_memory, VALUE),
  717. DEREF(msg2_memory, VALUE))) {
  718. return Qfalse;
  719. }
  720. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  721. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  722. DEREF(msg2_memory, VALUE))) {
  723. return Qfalse;
  724. }
  725. } else {
  726. if (!native_slot_eq(upb_fielddef_type(field),
  727. msg1_memory, msg2_memory)) {
  728. return Qfalse;
  729. }
  730. }
  731. }
  732. return Qtrue;
  733. }
  734. VALUE layout_hash(MessageLayout* layout, void* storage) {
  735. upb_msg_field_iter it;
  736. st_index_t h = rb_hash_start(0);
  737. VALUE hash_sym = rb_intern("hash");
  738. for (upb_msg_field_begin(&it, layout->msgdef);
  739. !upb_msg_field_done(&it);
  740. upb_msg_field_next(&it)) {
  741. const upb_fielddef* field = upb_msg_iter_field(&it);
  742. VALUE field_val = layout_get(layout, storage, field);
  743. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  744. }
  745. h = rb_hash_end(h);
  746. return INT2FIX(h);
  747. }
  748. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  749. VALUE str = rb_str_new2("");
  750. upb_msg_field_iter it;
  751. bool first = true;
  752. for (upb_msg_field_begin(&it, layout->msgdef);
  753. !upb_msg_field_done(&it);
  754. upb_msg_field_next(&it)) {
  755. const upb_fielddef* field = upb_msg_iter_field(&it);
  756. VALUE field_val = layout_get(layout, storage, field);
  757. if (!first) {
  758. str = rb_str_cat2(str, ", ");
  759. } else {
  760. first = false;
  761. }
  762. str = rb_str_cat2(str, upb_fielddef_name(field));
  763. str = rb_str_cat2(str, ": ");
  764. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  765. }
  766. return str;
  767. }