storage.c 29 KB

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