storage.c 30 KB

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