storage.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  345. MessageLayout* layout = ALLOC(MessageLayout);
  346. int nfields = upb_msgdef_numfields(msgdef);
  347. layout->fields = ALLOC_N(MessageField, nfields);
  348. upb_msg_field_iter it;
  349. size_t off = 0;
  350. for (upb_msg_field_begin(&it, msgdef);
  351. !upb_msg_field_done(&it);
  352. upb_msg_field_next(&it)) {
  353. const upb_fielddef* field = upb_msg_iter_field(&it);
  354. if (upb_fielddef_containingoneof(field)) {
  355. // Oneofs are handled separately below.
  356. continue;
  357. }
  358. // Allocate |field_size| bytes for this field in the layout.
  359. size_t field_size = 0;
  360. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  361. field_size = sizeof(VALUE);
  362. } else {
  363. field_size = native_slot_size(upb_fielddef_type(field));
  364. }
  365. // Align current offset up to |size| granularity.
  366. off = (off + field_size - 1) & ~(field_size - 1);
  367. layout->fields[upb_fielddef_index(field)].offset = off;
  368. layout->fields[upb_fielddef_index(field)].case_offset = MESSAGE_FIELD_NO_CASE;
  369. off += field_size;
  370. }
  371. // Handle oneofs now -- we iterate over oneofs specifically and allocate only
  372. // one slot per oneof.
  373. //
  374. // We assign all value slots first, then pack the 'case' fields at the end,
  375. // since in the common case (modern 64-bit platform) these are 8 bytes and 4
  376. // bytes respectively and we want to avoid alignment overhead.
  377. upb_msg_oneof_iter oit;
  378. for (upb_msg_oneof_begin(&oit, msgdef);
  379. !upb_msg_oneof_done(&oit);
  380. upb_msg_oneof_next(&oit)) {
  381. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  382. // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
  383. // all fields.
  384. size_t field_size = NATIVE_SLOT_MAX_SIZE;
  385. // Align the offset.
  386. off = (off + field_size - 1) & ~(field_size - 1);
  387. // Assign all fields in the oneof this same offset.
  388. upb_oneof_iter fit;
  389. for (upb_oneof_begin(&fit, oneof);
  390. !upb_oneof_done(&fit);
  391. upb_oneof_next(&fit)) {
  392. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  393. layout->fields[upb_fielddef_index(field)].offset = off;
  394. }
  395. off += field_size;
  396. }
  397. // Now the case fields.
  398. for (upb_msg_oneof_begin(&oit, msgdef);
  399. !upb_msg_oneof_done(&oit);
  400. upb_msg_oneof_next(&oit)) {
  401. const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
  402. size_t field_size = sizeof(uint32_t);
  403. // Align the offset.
  404. off = (off + field_size - 1) & ~(field_size - 1);
  405. // Assign all fields in the oneof this same offset.
  406. upb_oneof_iter fit;
  407. for (upb_oneof_begin(&fit, oneof);
  408. !upb_oneof_done(&fit);
  409. upb_oneof_next(&fit)) {
  410. const upb_fielddef* field = upb_oneof_iter_field(&fit);
  411. layout->fields[upb_fielddef_index(field)].case_offset = off;
  412. }
  413. off += field_size;
  414. }
  415. layout->size = off;
  416. layout->msgdef = msgdef;
  417. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  418. return layout;
  419. }
  420. void free_layout(MessageLayout* layout) {
  421. xfree(layout->fields);
  422. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  423. xfree(layout);
  424. }
  425. VALUE field_type_class(const upb_fielddef* field) {
  426. VALUE type_class = Qnil;
  427. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  428. VALUE submsgdesc =
  429. get_def_obj(upb_fielddef_subdef(field));
  430. type_class = Descriptor_msgclass(submsgdesc);
  431. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  432. VALUE subenumdesc =
  433. get_def_obj(upb_fielddef_subdef(field));
  434. type_class = EnumDescriptor_enummodule(subenumdesc);
  435. }
  436. return type_class;
  437. }
  438. static void* slot_memory(MessageLayout* layout,
  439. const void* storage,
  440. const upb_fielddef* field) {
  441. return ((uint8_t *)storage) +
  442. layout->fields[upb_fielddef_index(field)].offset;
  443. }
  444. static uint32_t* slot_oneof_case(MessageLayout* layout,
  445. const void* storage,
  446. const upb_fielddef* field) {
  447. return (uint32_t *)(((uint8_t *)storage) +
  448. layout->fields[upb_fielddef_index(field)].case_offset);
  449. }
  450. VALUE layout_get(MessageLayout* layout,
  451. const void* storage,
  452. const upb_fielddef* field) {
  453. void* memory = slot_memory(layout, storage, field);
  454. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  455. if (upb_fielddef_containingoneof(field)) {
  456. if (*oneof_case != upb_fielddef_number(field)) {
  457. return Qnil;
  458. }
  459. return native_slot_get(upb_fielddef_type(field),
  460. field_type_class(field),
  461. memory);
  462. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  463. return *((VALUE *)memory);
  464. } else {
  465. return native_slot_get(upb_fielddef_type(field),
  466. field_type_class(field),
  467. memory);
  468. }
  469. }
  470. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  471. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  472. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  473. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  474. rb_raise(rb_eTypeError, "Expected repeated field array");
  475. }
  476. RepeatedField* self = ruby_to_RepeatedField(val);
  477. if (self->field_type != upb_fielddef_type(field)) {
  478. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  479. }
  480. if (self->field_type == UPB_TYPE_MESSAGE ||
  481. self->field_type == UPB_TYPE_ENUM) {
  482. if (self->field_type_class !=
  483. get_def_obj(upb_fielddef_subdef(field))) {
  484. rb_raise(rb_eTypeError,
  485. "Repeated field array has wrong message/enum class");
  486. }
  487. }
  488. }
  489. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  490. assert(is_map_field(field));
  491. const upb_fielddef* key_field = map_field_key(field);
  492. const upb_fielddef* value_field = map_field_value(field);
  493. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  494. RTYPEDDATA_TYPE(val) != &Map_type) {
  495. rb_raise(rb_eTypeError, "Expected Map instance");
  496. }
  497. Map* self = ruby_to_Map(val);
  498. if (self->key_type != upb_fielddef_type(key_field)) {
  499. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  500. }
  501. if (self->value_type != upb_fielddef_type(value_field)) {
  502. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  503. }
  504. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  505. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  506. if (self->value_type_class !=
  507. get_def_obj(upb_fielddef_subdef(value_field))) {
  508. rb_raise(rb_eTypeError,
  509. "Map value type has wrong message/enum class");
  510. }
  511. }
  512. }
  513. void layout_set(MessageLayout* layout,
  514. void* storage,
  515. const upb_fielddef* field,
  516. VALUE val) {
  517. void* memory = slot_memory(layout, storage, field);
  518. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  519. if (upb_fielddef_containingoneof(field)) {
  520. if (val == Qnil) {
  521. // Assigning nil to a oneof field clears the oneof completely.
  522. *oneof_case = 0;
  523. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  524. } else {
  525. // Set the oneof case *first* in case a GC is triggered during
  526. // native_slot_set(): layout_mark() depends on oneof_case to know whether
  527. // the slot may be a Ruby VALUE and so we need that lifetime to start
  528. // before we could possibly stick a VALUE in it.
  529. *oneof_case = upb_fielddef_number(field);
  530. // We just overwrite the value directly if we changed oneof cases:
  531. // native_slot_set() does not depend on the old value in memory.
  532. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  533. memory, val);
  534. }
  535. } else if (is_map_field(field)) {
  536. check_map_field_type(val, field);
  537. DEREF(memory, VALUE) = val;
  538. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  539. check_repeated_field_type(val, field);
  540. DEREF(memory, VALUE) = val;
  541. } else {
  542. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  543. memory, val);
  544. }
  545. }
  546. void layout_init(MessageLayout* layout,
  547. void* storage) {
  548. upb_msg_field_iter it;
  549. for (upb_msg_field_begin(&it, layout->msgdef);
  550. !upb_msg_field_done(&it);
  551. upb_msg_field_next(&it)) {
  552. const upb_fielddef* field = upb_msg_iter_field(&it);
  553. void* memory = slot_memory(layout, storage, field);
  554. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  555. if (upb_fielddef_containingoneof(field)) {
  556. memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
  557. *oneof_case = 0;
  558. } else if (is_map_field(field)) {
  559. VALUE map = Qnil;
  560. const upb_fielddef* key_field = map_field_key(field);
  561. const upb_fielddef* value_field = map_field_value(field);
  562. VALUE type_class = field_type_class(value_field);
  563. if (type_class != Qnil) {
  564. VALUE args[3] = {
  565. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  566. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  567. type_class,
  568. };
  569. map = rb_class_new_instance(3, args, cMap);
  570. } else {
  571. VALUE args[2] = {
  572. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  573. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  574. };
  575. map = rb_class_new_instance(2, args, cMap);
  576. }
  577. DEREF(memory, VALUE) = map;
  578. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  579. VALUE ary = Qnil;
  580. VALUE type_class = field_type_class(field);
  581. if (type_class != Qnil) {
  582. VALUE args[2] = {
  583. fieldtype_to_ruby(upb_fielddef_type(field)),
  584. type_class,
  585. };
  586. ary = rb_class_new_instance(2, args, cRepeatedField);
  587. } else {
  588. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  589. ary = rb_class_new_instance(1, args, cRepeatedField);
  590. }
  591. DEREF(memory, VALUE) = ary;
  592. } else {
  593. native_slot_init(upb_fielddef_type(field), memory);
  594. }
  595. }
  596. }
  597. void layout_mark(MessageLayout* layout, void* storage) {
  598. upb_msg_field_iter it;
  599. for (upb_msg_field_begin(&it, layout->msgdef);
  600. !upb_msg_field_done(&it);
  601. upb_msg_field_next(&it)) {
  602. const upb_fielddef* field = upb_msg_iter_field(&it);
  603. void* memory = slot_memory(layout, storage, field);
  604. uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
  605. if (upb_fielddef_containingoneof(field)) {
  606. if (*oneof_case == upb_fielddef_number(field)) {
  607. native_slot_mark(upb_fielddef_type(field), memory);
  608. }
  609. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  610. rb_gc_mark(DEREF(memory, VALUE));
  611. } else {
  612. native_slot_mark(upb_fielddef_type(field), memory);
  613. }
  614. }
  615. }
  616. void layout_dup(MessageLayout* layout, void* to, void* from) {
  617. upb_msg_field_iter it;
  618. for (upb_msg_field_begin(&it, layout->msgdef);
  619. !upb_msg_field_done(&it);
  620. upb_msg_field_next(&it)) {
  621. const upb_fielddef* field = upb_msg_iter_field(&it);
  622. void* to_memory = slot_memory(layout, to, field);
  623. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  624. void* from_memory = slot_memory(layout, from, field);
  625. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  626. if (upb_fielddef_containingoneof(field)) {
  627. if (*from_oneof_case == upb_fielddef_number(field)) {
  628. *to_oneof_case = *from_oneof_case;
  629. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  630. }
  631. } else if (is_map_field(field)) {
  632. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  633. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  634. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  635. } else {
  636. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  637. }
  638. }
  639. }
  640. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  641. upb_msg_field_iter it;
  642. for (upb_msg_field_begin(&it, layout->msgdef);
  643. !upb_msg_field_done(&it);
  644. upb_msg_field_next(&it)) {
  645. const upb_fielddef* field = upb_msg_iter_field(&it);
  646. void* to_memory = slot_memory(layout, to, field);
  647. uint32_t* to_oneof_case = slot_oneof_case(layout, to, field);
  648. void* from_memory = slot_memory(layout, from, field);
  649. uint32_t* from_oneof_case = slot_oneof_case(layout, from, field);
  650. if (upb_fielddef_containingoneof(field)) {
  651. if (*from_oneof_case == upb_fielddef_number(field)) {
  652. *to_oneof_case = *from_oneof_case;
  653. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  654. }
  655. } else if (is_map_field(field)) {
  656. DEREF(to_memory, VALUE) =
  657. Map_deep_copy(DEREF(from_memory, VALUE));
  658. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  659. DEREF(to_memory, VALUE) =
  660. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  661. } else {
  662. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  663. }
  664. }
  665. }
  666. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  667. upb_msg_field_iter it;
  668. for (upb_msg_field_begin(&it, layout->msgdef);
  669. !upb_msg_field_done(&it);
  670. upb_msg_field_next(&it)) {
  671. const upb_fielddef* field = upb_msg_iter_field(&it);
  672. void* msg1_memory = slot_memory(layout, msg1, field);
  673. uint32_t* msg1_oneof_case = slot_oneof_case(layout, msg1, field);
  674. void* msg2_memory = slot_memory(layout, msg2, field);
  675. uint32_t* msg2_oneof_case = slot_oneof_case(layout, msg2, field);
  676. if (upb_fielddef_containingoneof(field)) {
  677. if (*msg1_oneof_case != *msg2_oneof_case ||
  678. (*msg1_oneof_case == upb_fielddef_number(field) &&
  679. !native_slot_eq(upb_fielddef_type(field),
  680. msg1_memory,
  681. msg2_memory))) {
  682. return Qfalse;
  683. }
  684. } else if (is_map_field(field)) {
  685. if (!Map_eq(DEREF(msg1_memory, VALUE),
  686. DEREF(msg2_memory, VALUE))) {
  687. return Qfalse;
  688. }
  689. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  690. if (!RepeatedField_eq(DEREF(msg1_memory, VALUE),
  691. DEREF(msg2_memory, VALUE))) {
  692. return Qfalse;
  693. }
  694. } else {
  695. if (!native_slot_eq(upb_fielddef_type(field),
  696. msg1_memory, msg2_memory)) {
  697. return Qfalse;
  698. }
  699. }
  700. }
  701. return Qtrue;
  702. }
  703. VALUE layout_hash(MessageLayout* layout, void* storage) {
  704. upb_msg_field_iter it;
  705. st_index_t h = rb_hash_start(0);
  706. VALUE hash_sym = rb_intern("hash");
  707. for (upb_msg_field_begin(&it, layout->msgdef);
  708. !upb_msg_field_done(&it);
  709. upb_msg_field_next(&it)) {
  710. const upb_fielddef* field = upb_msg_iter_field(&it);
  711. VALUE field_val = layout_get(layout, storage, field);
  712. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  713. }
  714. h = rb_hash_end(h);
  715. return INT2FIX(h);
  716. }
  717. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  718. VALUE str = rb_str_new2("");
  719. upb_msg_field_iter it;
  720. bool first = true;
  721. for (upb_msg_field_begin(&it, layout->msgdef);
  722. !upb_msg_field_done(&it);
  723. upb_msg_field_next(&it)) {
  724. const upb_fielddef* field = upb_msg_iter_field(&it);
  725. VALUE field_val = layout_get(layout, storage, field);
  726. if (!first) {
  727. str = rb_str_cat2(str, ", ");
  728. } else {
  729. first = false;
  730. }
  731. str = rb_str_cat2(str, upb_fielddef_name(field));
  732. str = rb_str_cat2(str, ": ");
  733. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  734. }
  735. return str;
  736. }