storage.c 28 KB

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