storage.c 30 KB

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