pbwire 0.1.18 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +68 -19
- data/ext/pbwire_ext/core_ctx.c +16 -8
- data/ext/pbwire_ext/core_decode.c +30 -4
- data/ext/pbwire_ext/core_descriptor_set.c +115 -15
- data/ext/pbwire_ext/core_encode.c +122 -81
- data/ext/pbwire_ext/core_json.c +487 -123
- data/ext/pbwire_ext/core_message.c +36 -2
- data/ext/pbwire_ext/core_schema.c +69 -12
- data/ext/pbwire_ext/core_stream.c +82 -22
- data/ext/pbwire_ext/core_text.c +25 -15
- data/ext/pbwire_ext/core_wire.c +7 -3
- data/ext/pbwire_ext/core_wkt.c +3 -2
- data/ext/pbwire_ext/internal.h +32 -7
- data/ext/pbwire_ext/pbwire.h +137 -53
- data/ext/pbwire_ext/pbwire_ext.c +759 -7
- data/lib/pbwire.rb +7 -4
- metadata +16 -2
data/ext/pbwire_ext/pbwire_ext.c
CHANGED
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
#include <string.h>
|
|
40
40
|
|
|
41
41
|
/* Same cap as the core's PBW_MAX_DEPTH (src/internal.h, not public). */
|
|
42
|
-
#define RB_PBW_MAX_DEPTH
|
|
42
|
+
#define RB_PBW_MAX_DEPTH 100
|
|
43
43
|
#define SLICE_INLINE 32
|
|
44
44
|
#define SLICE_DEAD 0xffffffffu
|
|
45
45
|
|
|
@@ -75,6 +75,9 @@ typedef struct {
|
|
|
75
75
|
uint8_t packed;
|
|
76
76
|
uint8_t required;
|
|
77
77
|
uint8_t oneof; /* 1-based oneof group, 0 = none */
|
|
78
|
+
ID ivar; /* @<ruby_name>: the generated model's value slot */
|
|
79
|
+
ID has_ivar; /* @has_<ruby_name>, or 0 when the model has no has_ flag */
|
|
80
|
+
const pbw_field *df; /* descriptor slot, cached: field_at per op -> once */
|
|
78
81
|
VALUE name; /* frozen UTF-8 String */
|
|
79
82
|
VALUE sub; /* Type of a MESSAGE field, else Qnil */
|
|
80
83
|
} field_t;
|
|
@@ -87,6 +90,8 @@ typedef struct {
|
|
|
87
90
|
uint32_t *by_number; /* dense field number -> index, or NULL */
|
|
88
91
|
uint32_t by_number_size; /* entries in by_number */
|
|
89
92
|
VALUE by_name; /* Hash: field name -> Integer index */
|
|
93
|
+
VALUE name_str; /* frozen UTF-8 full name, for class lookup */
|
|
94
|
+
pbw_ctx *ctx; /* encode/decode arena, reset per object call */
|
|
90
95
|
uint32_t oneofs;
|
|
91
96
|
int has_required;
|
|
92
97
|
} type_t;
|
|
@@ -248,6 +253,7 @@ type_mark(void *ptr)
|
|
|
248
253
|
return;
|
|
249
254
|
}
|
|
250
255
|
rb_gc_mark(type->by_name);
|
|
256
|
+
rb_gc_mark(type->name_str);
|
|
251
257
|
for (i = 0; i < type->count; i++) {
|
|
252
258
|
rb_gc_mark(type->fields[i].name);
|
|
253
259
|
rb_gc_mark(type->fields[i].sub);
|
|
@@ -264,6 +270,10 @@ type_free(void *ptr)
|
|
|
264
270
|
}
|
|
265
271
|
holder_release(type->holder);
|
|
266
272
|
type->holder = NULL;
|
|
273
|
+
if (type->ctx != NULL) {
|
|
274
|
+
pbw_ctx_free(type->ctx);
|
|
275
|
+
type->ctx = NULL;
|
|
276
|
+
}
|
|
267
277
|
xfree(type->fields);
|
|
268
278
|
xfree(type->by_number);
|
|
269
279
|
xfree(type);
|
|
@@ -327,6 +337,8 @@ type_alloc(VALUE klass)
|
|
|
327
337
|
type->by_number = NULL;
|
|
328
338
|
type->by_number_size = 0;
|
|
329
339
|
type->by_name = Qnil;
|
|
340
|
+
type->name_str = Qnil;
|
|
341
|
+
type->ctx = NULL;
|
|
330
342
|
type->oneofs = 0;
|
|
331
343
|
type->has_required = 0;
|
|
332
344
|
return obj;
|
|
@@ -421,6 +433,26 @@ packable(pbw_type type)
|
|
|
421
433
|
|
|
422
434
|
static VALUE type_for(VALUE schema_obj, const pbw_descriptor *desc);
|
|
423
435
|
|
|
436
|
+
/* The generated model names a field @<name>, or @_<name> when the proto name
|
|
437
|
+
* starts uppercase (Ruby would parse it as a constant), and carries a
|
|
438
|
+
* separate @has_<name> for presence-carrying scalars. */
|
|
439
|
+
static ID
|
|
440
|
+
model_ivar_id(const char *name, const char *prefix)
|
|
441
|
+
{
|
|
442
|
+
VALUE str = rb_str_new_cstr(prefix);
|
|
443
|
+
ID id;
|
|
444
|
+
|
|
445
|
+
if (name != NULL && name[0] >= 'A' && name[0] <= 'Z') {
|
|
446
|
+
rb_str_cat_cstr(str, "_");
|
|
447
|
+
}
|
|
448
|
+
if (name != NULL) {
|
|
449
|
+
rb_str_cat_cstr(str, name);
|
|
450
|
+
}
|
|
451
|
+
id = rb_intern_str(str);
|
|
452
|
+
RB_GC_GUARD(str);
|
|
453
|
+
return id;
|
|
454
|
+
}
|
|
455
|
+
|
|
424
456
|
/* Fill one Type's field table. Runs under rb_protect, so every rb_* call
|
|
425
457
|
* here may raise and type_for() can drop the half-built Type afterwards. */
|
|
426
458
|
typedef struct {
|
|
@@ -447,6 +479,7 @@ type_fill(VALUE argp)
|
|
|
447
479
|
|
|
448
480
|
f->number = pbw_field_number(field);
|
|
449
481
|
f->index = (uint32_t)i;
|
|
482
|
+
f->df = field;
|
|
450
483
|
f->type = (uint8_t)pbw_field_type(field);
|
|
451
484
|
f->wire = (uint8_t)wire_type_of((pbw_type)f->type);
|
|
452
485
|
f->repeated = pbw_field_label(field) == PBW_LABEL_REPEATED;
|
|
@@ -455,6 +488,13 @@ type_fill(VALUE argp)
|
|
|
455
488
|
f->oneof = (uint8_t)pbw_field_oneof_index(field);
|
|
456
489
|
f->name = rb_utf8_str_new_cstr(pbw_field_name(field));
|
|
457
490
|
rb_obj_freeze(f->name);
|
|
491
|
+
f->ivar = model_ivar_id(pbw_field_name(field), "@");
|
|
492
|
+
f->has_ivar = 0;
|
|
493
|
+
if (!f->repeated && f->type != PBW_TYPE_MESSAGE &&
|
|
494
|
+
f->type != PBW_TYPE_GROUP && !f->required &&
|
|
495
|
+
(f->oneof != 0 || !pbw_descriptor_is_proto3(desc))) {
|
|
496
|
+
f->has_ivar = model_ivar_id(pbw_field_name(field), "@has_");
|
|
497
|
+
}
|
|
458
498
|
f->sub = Qnil;
|
|
459
499
|
if (f->oneof > type->oneofs) {
|
|
460
500
|
type->oneofs = f->oneof;
|
|
@@ -514,6 +554,7 @@ type_for(VALUE schema_obj, const pbw_descriptor *desc)
|
|
|
514
554
|
schema->types = types;
|
|
515
555
|
}
|
|
516
556
|
key = rb_utf8_str_new_cstr(pbw_descriptor_name(desc));
|
|
557
|
+
rb_obj_freeze(key);
|
|
517
558
|
found = rb_hash_aref(types, key);
|
|
518
559
|
if (!NIL_P(found)) {
|
|
519
560
|
return found;
|
|
@@ -521,6 +562,7 @@ type_for(VALUE schema_obj, const pbw_descriptor *desc)
|
|
|
521
562
|
count = pbw_descriptor_field_count(desc);
|
|
522
563
|
obj = typed_object_alloc(cType, &type_data_type, (void **)&type);
|
|
523
564
|
type->desc = desc;
|
|
565
|
+
type->name_str = key;
|
|
524
566
|
/* A Type outlives the Schema object it came from: it keeps the C schema
|
|
525
567
|
* alive (the descriptor pointers it holds point into it). */
|
|
526
568
|
type->holder = schema->holder;
|
|
@@ -1036,8 +1078,7 @@ scan_message(message_t *m, type_t *type, const uint8_t *data, size_t off,
|
|
|
1036
1078
|
if (seen != NULL) {
|
|
1037
1079
|
for (i = 0; i < type->count; i++) {
|
|
1038
1080
|
if (type->fields[i].required && !seen[i]) {
|
|
1039
|
-
*missing = pbw_field_name(
|
|
1040
|
-
pbw_descriptor_field_at(type->desc, i));
|
|
1081
|
+
*missing = pbw_field_name(type->fields[i].df);
|
|
1041
1082
|
status = PBW_ERR_REQUIRED;
|
|
1042
1083
|
goto done;
|
|
1043
1084
|
}
|
|
@@ -1572,6 +1613,637 @@ item_to_ruby(VALUE owner, field_t *f, pbw_value *value)
|
|
|
1572
1613
|
}
|
|
1573
1614
|
}
|
|
1574
1615
|
|
|
1616
|
+
/* ------------------------------------------------- object codec (shim) */
|
|
1617
|
+
|
|
1618
|
+
/* The generated model is the native object: encode walks its instance
|
|
1619
|
+
* variables inside C (one entry call, no Ruby loop), decode materializes the
|
|
1620
|
+
* model classes inside C. `to_h` is deliberately not used: a Hash per call is
|
|
1621
|
+
* exactly the per-field allocation this arm removes. */
|
|
1622
|
+
|
|
1623
|
+
static type_t *
|
|
1624
|
+
field_sub_type(field_t *f)
|
|
1625
|
+
{
|
|
1626
|
+
type_t *sub;
|
|
1627
|
+
|
|
1628
|
+
TypedData_Get_Struct(f->sub, type_t, &type_data_type, sub);
|
|
1629
|
+
return sub;
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
static int
|
|
1633
|
+
scalar_is_unsigned(uint8_t type)
|
|
1634
|
+
{
|
|
1635
|
+
switch ((pbw_type)type) {
|
|
1636
|
+
case PBW_TYPE_UINT32:
|
|
1637
|
+
case PBW_TYPE_UINT64:
|
|
1638
|
+
case PBW_TYPE_FIXED32:
|
|
1639
|
+
case PBW_TYPE_FIXED64:
|
|
1640
|
+
return 1;
|
|
1641
|
+
default:
|
|
1642
|
+
return 0;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
|
|
1646
|
+
/* The generated model's presence rule: has_ fields are explicit, a string is
|
|
1647
|
+
* present when non-nil and non-empty, everything else when non-zero/true. */
|
|
1648
|
+
static int
|
|
1649
|
+
object_scalar_present(field_t *f, VALUE value)
|
|
1650
|
+
{
|
|
1651
|
+
switch ((pbw_type)f->type) {
|
|
1652
|
+
case PBW_TYPE_STRING:
|
|
1653
|
+
case PBW_TYPE_BYTES:
|
|
1654
|
+
if (NIL_P(value)) {
|
|
1655
|
+
return 0;
|
|
1656
|
+
}
|
|
1657
|
+
Check_Type(value, T_STRING);
|
|
1658
|
+
return RSTRING_LEN(value) > 0;
|
|
1659
|
+
case PBW_TYPE_BOOL:
|
|
1660
|
+
return RTEST(value);
|
|
1661
|
+
case PBW_TYPE_FLOAT:
|
|
1662
|
+
case PBW_TYPE_DOUBLE:
|
|
1663
|
+
return !NIL_P(value) && NUM2DBL(value) != 0.0;
|
|
1664
|
+
default:
|
|
1665
|
+
if (NIL_P(value)) {
|
|
1666
|
+
return 0;
|
|
1667
|
+
}
|
|
1668
|
+
return scalar_is_unsigned(f->type) ? rb_num2ull(value) != 0
|
|
1669
|
+
: rb_num2ll(value) != 0;
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
/* One scalar/string value as a core value. UINT64_MAX must go through
|
|
1674
|
+
* rb_num2ull: rb_num2ll raises on it. */
|
|
1675
|
+
static pbw_value *
|
|
1676
|
+
object_scalar_value(field_t *f, VALUE value, pbw_ctx *ctx)
|
|
1677
|
+
{
|
|
1678
|
+
uint64_t bits;
|
|
1679
|
+
|
|
1680
|
+
switch ((pbw_type)f->type) {
|
|
1681
|
+
case PBW_TYPE_STRING:
|
|
1682
|
+
case PBW_TYPE_BYTES:
|
|
1683
|
+
Check_Type(value, T_STRING);
|
|
1684
|
+
return pbw_value_new_bytes_ctx(ctx, RSTRING_PTR(value),
|
|
1685
|
+
(size_t)RSTRING_LEN(value));
|
|
1686
|
+
case PBW_TYPE_FLOAT: {
|
|
1687
|
+
float fv = (float)NUM2DBL(value);
|
|
1688
|
+
|
|
1689
|
+
memcpy(&bits, &fv, 4);
|
|
1690
|
+
return pbw_value_new_uint_ctx(ctx, bits);
|
|
1691
|
+
}
|
|
1692
|
+
case PBW_TYPE_DOUBLE: {
|
|
1693
|
+
double dv = NUM2DBL(value);
|
|
1694
|
+
|
|
1695
|
+
memcpy(&bits, &dv, 8);
|
|
1696
|
+
return pbw_value_new_uint_ctx(ctx, bits);
|
|
1697
|
+
}
|
|
1698
|
+
case PBW_TYPE_BOOL:
|
|
1699
|
+
return pbw_value_new_uint_ctx(ctx, RTEST(value) ? 1u : 0u);
|
|
1700
|
+
case PBW_TYPE_INT32:
|
|
1701
|
+
case PBW_TYPE_INT64:
|
|
1702
|
+
case PBW_TYPE_SINT32:
|
|
1703
|
+
case PBW_TYPE_SINT64:
|
|
1704
|
+
case PBW_TYPE_SFIXED32:
|
|
1705
|
+
case PBW_TYPE_SFIXED64:
|
|
1706
|
+
case PBW_TYPE_ENUM:
|
|
1707
|
+
return pbw_value_new_uint_ctx(ctx, (uint64_t)rb_num2ll(value));
|
|
1708
|
+
default:
|
|
1709
|
+
return pbw_value_new_uint_ctx(ctx, rb_num2ull(value));
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
/* One tag scan straight into the generated model, no pbw_message tree. The
|
|
1714
|
+
* Type's field table is the layout: field number -> wire facts -> model
|
|
1715
|
+
* ivar, resolved per type. Singular scalars/strings land on their ivar
|
|
1716
|
+
* as they parse (last wins, the merge the tree used to do); repeated values
|
|
1717
|
+
* push straight into the array created up front; singular sub-messages
|
|
1718
|
+
* remember their payload range and decode once at the end, so a repeated
|
|
1719
|
+
* singular still parses as one message. Oneof losers are reset to their
|
|
1720
|
+
* defaults after the scan. Every gate-checked field is materialized before
|
|
1721
|
+
* return, so the timer needs no Ruby-side fold. Raises Pbwire::Error on
|
|
1722
|
+
* malformed bytes, like pbw_decode did.
|
|
1723
|
+
*
|
|
1724
|
+
* GC rooting: every Ruby VALUE the scan creates is reachable from `obj`
|
|
1725
|
+
* (repeated arrays and singular values are set on their ivar the moment
|
|
1726
|
+
* they are built). Singular sub-message payloads stay as (offset, length)
|
|
1727
|
+
* integers into the input buffer while seen once, so the common case
|
|
1728
|
+
* copies nothing; only a repeated singular allocates an accumulator
|
|
1729
|
+
* String, and that String is stored on the model's ivar before any further
|
|
1730
|
+
* Ruby allocation, so the recursive decode at the end reads memory that is
|
|
1731
|
+
* anchored by `obj` (or by the caller's wire String, as every C extension
|
|
1732
|
+
* relies on). No RSTRING_PTR is ever held across an allocation that could
|
|
1733
|
+
* free its String. Plain C scratch (seen/winner/ranges) lives on the
|
|
1734
|
+
* stack, so a raise unwinds with nothing to free.
|
|
1735
|
+
*
|
|
1736
|
+
* The host-class map is resolved through a tiny per-call cache: a 3000-deep
|
|
1737
|
+
* payload names two classes, so one Hash lookup per type per top-level call
|
|
1738
|
+
* replaces one per message. The 8-entry cap only bounds stack use: a deeper
|
|
1739
|
+
* schema falls back to the uncached Hash lookup with identical results. */
|
|
1740
|
+
typedef struct {
|
|
1741
|
+
VALUE classes;
|
|
1742
|
+
const type_t *types[8];
|
|
1743
|
+
VALUE klasses[8];
|
|
1744
|
+
size_t count;
|
|
1745
|
+
} klass_cache_t;
|
|
1746
|
+
|
|
1747
|
+
static VALUE
|
|
1748
|
+
cached_klass(klass_cache_t *cache, type_t *type)
|
|
1749
|
+
{
|
|
1750
|
+
size_t i;
|
|
1751
|
+
VALUE klass;
|
|
1752
|
+
|
|
1753
|
+
for (i = 0; i < cache->count; i++) {
|
|
1754
|
+
if (cache->types[i] == type) {
|
|
1755
|
+
return cache->klasses[i];
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
if (RB_TYPE_P(cache->classes, T_HASH)) {
|
|
1759
|
+
klass = rb_hash_aref(cache->classes, type->name_str);
|
|
1760
|
+
} else if (RB_TYPE_P(cache->classes, T_CLASS)) {
|
|
1761
|
+
klass = cache->classes;
|
|
1762
|
+
} else {
|
|
1763
|
+
klass = Qnil;
|
|
1764
|
+
}
|
|
1765
|
+
if (!RB_TYPE_P(klass, T_CLASS)) {
|
|
1766
|
+
rb_raise(rb_eArgError,
|
|
1767
|
+
"decode_object: no class for %s (pass a full-name => Class "
|
|
1768
|
+
"Hash)", type_name(type));
|
|
1769
|
+
}
|
|
1770
|
+
if (cache->count < 8) {
|
|
1771
|
+
cache->types[cache->count] = type;
|
|
1772
|
+
cache->klasses[cache->count] = klass;
|
|
1773
|
+
cache->count++;
|
|
1774
|
+
}
|
|
1775
|
+
return klass;
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
static VALUE object_decode_bytes(type_t *type, const uint8_t *data,
|
|
1779
|
+
size_t len, uint32_t depth,
|
|
1780
|
+
klass_cache_t *cache);
|
|
1781
|
+
static VALUE object_default(field_t *f);
|
|
1782
|
+
|
|
1783
|
+
static void
|
|
1784
|
+
object_note_singular(VALUE obj, field_t *f, unsigned char *seen,
|
|
1785
|
+
uint32_t *winner)
|
|
1786
|
+
{
|
|
1787
|
+
seen[f->index] = 1;
|
|
1788
|
+
if (f->has_ivar != 0) {
|
|
1789
|
+
rb_ivar_set(obj, f->has_ivar, Qtrue);
|
|
1790
|
+
}
|
|
1791
|
+
if (f->oneof != 0 && winner != NULL) {
|
|
1792
|
+
winner[f->oneof - 1] = f->index + 1;
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
/* One occurrence of a singular sub-message (message or group) payload.
|
|
1797
|
+
* The first occurrence only records its range; a repeat concatenates into
|
|
1798
|
+
* an accumulator String that is stored on the model's own ivar before any
|
|
1799
|
+
* further Ruby allocation, so the end-of-scan decode reads rooted memory.
|
|
1800
|
+
* `data` is the current frame base; ranges are frame-relative. */
|
|
1801
|
+
static void
|
|
1802
|
+
object_accumulate_submsg(VALUE obj, field_t *f, const uint8_t *data,
|
|
1803
|
+
const uint8_t *body, size_t body_len,
|
|
1804
|
+
size_t *acc_off, size_t *acc_len,
|
|
1805
|
+
unsigned char *seen, uint32_t *winner)
|
|
1806
|
+
{
|
|
1807
|
+
if (!seen[f->index]) {
|
|
1808
|
+
acc_off[f->index] = (size_t)(body - data);
|
|
1809
|
+
acc_len[f->index] = body_len;
|
|
1810
|
+
} else {
|
|
1811
|
+
VALUE cur = rb_ivar_get(obj, f->ivar);
|
|
1812
|
+
|
|
1813
|
+
if (NIL_P(cur)) {
|
|
1814
|
+
/* Second occurrence: materialize the pending first range. */
|
|
1815
|
+
cur = rb_str_new((const char *)(data + acc_off[f->index]),
|
|
1816
|
+
(long)acc_len[f->index]);
|
|
1817
|
+
rb_ivar_set(obj, f->ivar, cur);
|
|
1818
|
+
}
|
|
1819
|
+
rb_str_cat(cur, (const char *)body, (long)body_len);
|
|
1820
|
+
}
|
|
1821
|
+
object_note_singular(obj, f, seen, winner);
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
static VALUE
|
|
1825
|
+
object_decode_bytes(type_t *type, const uint8_t *data, size_t len,
|
|
1826
|
+
uint32_t depth, klass_cache_t *cache)
|
|
1827
|
+
{
|
|
1828
|
+
VALUE obj;
|
|
1829
|
+
rdr r;
|
|
1830
|
+
unsigned char *seen;
|
|
1831
|
+
uint32_t *winner;
|
|
1832
|
+
VALUE *rep; /* repeated arrays, also rooted via obj */
|
|
1833
|
+
/* First payload range of each singular sub-message, frame-relative.
|
|
1834
|
+
* Plain integers: no GC hazard. A repeated singular additionally
|
|
1835
|
+
* builds an accumulator String stored on the model's own ivar (see
|
|
1836
|
+
* object_accumulate_submsg), so it stays rooted by `obj`. */
|
|
1837
|
+
size_t *acc_off;
|
|
1838
|
+
size_t *acc_len;
|
|
1839
|
+
size_t count = type->count;
|
|
1840
|
+
size_t i;
|
|
1841
|
+
|
|
1842
|
+
if (depth >= RB_PBW_MAX_DEPTH) {
|
|
1843
|
+
rb_raise(eError, "%s: nesting limit exceeded", type_name(type));
|
|
1844
|
+
}
|
|
1845
|
+
obj = rb_obj_alloc(cached_klass(cache, type));
|
|
1846
|
+
|
|
1847
|
+
seen = ALLOCA_N(unsigned char, count != 0 ? count : 1);
|
|
1848
|
+
if (count != 0) {
|
|
1849
|
+
memset(seen, 0, count);
|
|
1850
|
+
}
|
|
1851
|
+
winner = type->oneofs != 0 ? ALLOCA_N(uint32_t, type->oneofs) : NULL;
|
|
1852
|
+
if (winner != NULL) {
|
|
1853
|
+
memset(winner, 0, type->oneofs * sizeof(*winner));
|
|
1854
|
+
}
|
|
1855
|
+
rep = ALLOCA_N(VALUE, count != 0 ? count : 1);
|
|
1856
|
+
acc_off = ALLOCA_N(size_t, count != 0 ? count : 1);
|
|
1857
|
+
acc_len = ALLOCA_N(size_t, count != 0 ? count : 1);
|
|
1858
|
+
|
|
1859
|
+
/* Defaults first, so every ivar is rooted via obj for the whole scan. */
|
|
1860
|
+
for (i = 0; i < count; i++) {
|
|
1861
|
+
field_t *f = &type->fields[i];
|
|
1862
|
+
|
|
1863
|
+
if (f->repeated) {
|
|
1864
|
+
rep[i] = rb_ary_new();
|
|
1865
|
+
rb_ivar_set(obj, f->ivar, rep[i]);
|
|
1866
|
+
} else if (f->type == PBW_TYPE_MESSAGE ||
|
|
1867
|
+
f->type == PBW_TYPE_GROUP) {
|
|
1868
|
+
rb_ivar_set(obj, f->ivar, Qnil);
|
|
1869
|
+
} else {
|
|
1870
|
+
rb_ivar_set(obj, f->ivar, object_default(f));
|
|
1871
|
+
if (f->has_ivar != 0) {
|
|
1872
|
+
rb_ivar_set(obj, f->has_ivar, Qfalse);
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
r.p = data;
|
|
1878
|
+
r.end = data + len;
|
|
1879
|
+
while (r.p < r.end) {
|
|
1880
|
+
uint32_t fnum;
|
|
1881
|
+
unsigned wt;
|
|
1882
|
+
field_t *f;
|
|
1883
|
+
|
|
1884
|
+
if (!rd_tag(&r, &fnum, &wt)) {
|
|
1885
|
+
rb_raise(eError, "%s: invalid serialized data",
|
|
1886
|
+
type_name(type));
|
|
1887
|
+
}
|
|
1888
|
+
f = type_field_by_number(type, fnum);
|
|
1889
|
+
if (f == NULL ||
|
|
1890
|
+
(wt != (unsigned)f->wire &&
|
|
1891
|
+
!(wt == 2 && f->repeated && packable((pbw_type)f->type)))) {
|
|
1892
|
+
/* A length-delimited run for a singular scalar is not packed
|
|
1893
|
+
* (core parity: read_packed runs only for REPEATED packable
|
|
1894
|
+
* fields), so skip it as unknown, like PHP/C#/Node. */
|
|
1895
|
+
if (!rd_skip(&r, fnum, wt, depth)) {
|
|
1896
|
+
rb_raise(eError, "%s: invalid serialized data",
|
|
1897
|
+
type_name(type));
|
|
1898
|
+
}
|
|
1899
|
+
continue;
|
|
1900
|
+
}
|
|
1901
|
+
if (f->required) {
|
|
1902
|
+
seen[f->index] = 1;
|
|
1903
|
+
}
|
|
1904
|
+
if (f->type == PBW_TYPE_GROUP) {
|
|
1905
|
+
const uint8_t *body;
|
|
1906
|
+
size_t body_len;
|
|
1907
|
+
|
|
1908
|
+
if (!rd_group_body(&r, fnum, depth, &body, &body_len)) {
|
|
1909
|
+
rb_raise(eError, "%s: invalid serialized data",
|
|
1910
|
+
type_name(type));
|
|
1911
|
+
}
|
|
1912
|
+
if (f->repeated) {
|
|
1913
|
+
rb_ary_push(rep[f->index],
|
|
1914
|
+
object_decode_bytes(field_sub_type(f), body,
|
|
1915
|
+
body_len, depth + 1,
|
|
1916
|
+
cache));
|
|
1917
|
+
} else {
|
|
1918
|
+
object_accumulate_submsg(obj, f, data, body, body_len,
|
|
1919
|
+
acc_off, acc_len, seen, winner);
|
|
1920
|
+
}
|
|
1921
|
+
continue;
|
|
1922
|
+
}
|
|
1923
|
+
if (wt == 2) {
|
|
1924
|
+
uint64_t plen;
|
|
1925
|
+
|
|
1926
|
+
if (!rd_varint(&r, &plen) ||
|
|
1927
|
+
plen > (uint64_t)(r.end - r.p)) {
|
|
1928
|
+
rb_raise(eError, "%s: invalid serialized data",
|
|
1929
|
+
type_name(type));
|
|
1930
|
+
}
|
|
1931
|
+
if (f->type != PBW_TYPE_MESSAGE &&
|
|
1932
|
+
f->type != PBW_TYPE_STRING &&
|
|
1933
|
+
f->type != PBW_TYPE_BYTES) {
|
|
1934
|
+
/* A packed run of scalars: the filter above only lets
|
|
1935
|
+
* repeated packable fields through with wt == 2. */
|
|
1936
|
+
const uint8_t *end = r.p + (size_t)plen;
|
|
1937
|
+
|
|
1938
|
+
while (r.p < end) {
|
|
1939
|
+
size_t used = 0;
|
|
1940
|
+
VALUE v = scalar_at(f, r.p, end, &used);
|
|
1941
|
+
|
|
1942
|
+
r.p += used;
|
|
1943
|
+
rb_ary_push(rep[f->index], v);
|
|
1944
|
+
}
|
|
1945
|
+
continue;
|
|
1946
|
+
}
|
|
1947
|
+
if (f->type == PBW_TYPE_MESSAGE) {
|
|
1948
|
+
if (f->repeated) {
|
|
1949
|
+
rb_ary_push(rep[f->index],
|
|
1950
|
+
object_decode_bytes(field_sub_type(f), r.p,
|
|
1951
|
+
(size_t)plen, depth + 1,
|
|
1952
|
+
cache));
|
|
1953
|
+
} else {
|
|
1954
|
+
object_accumulate_submsg(obj, f, data, r.p,
|
|
1955
|
+
(size_t)plen, acc_off, acc_len,
|
|
1956
|
+
seen, winner);
|
|
1957
|
+
}
|
|
1958
|
+
} else if (f->repeated) {
|
|
1959
|
+
rb_ary_push(rep[f->index],
|
|
1960
|
+
string_from(f->type, r.p, (size_t)plen));
|
|
1961
|
+
} else {
|
|
1962
|
+
rb_ivar_set(obj, f->ivar,
|
|
1963
|
+
string_from(f->type, r.p, (size_t)plen));
|
|
1964
|
+
object_note_singular(obj, f, seen, winner);
|
|
1965
|
+
}
|
|
1966
|
+
r.p += (size_t)plen;
|
|
1967
|
+
continue;
|
|
1968
|
+
}
|
|
1969
|
+
{
|
|
1970
|
+
size_t used = 0;
|
|
1971
|
+
VALUE v = scalar_at(f, r.p, r.end, &used);
|
|
1972
|
+
|
|
1973
|
+
r.p += used;
|
|
1974
|
+
if (f->repeated) {
|
|
1975
|
+
rb_ary_push(rep[f->index], v);
|
|
1976
|
+
} else {
|
|
1977
|
+
rb_ivar_set(obj, f->ivar, v);
|
|
1978
|
+
object_note_singular(obj, f, seen, winner);
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
for (i = 0; i < count; i++) {
|
|
1984
|
+
if (type->fields[i].required && !seen[i]) {
|
|
1985
|
+
rb_raise(eError, "%s: required field %s is missing",
|
|
1986
|
+
type_name(type),
|
|
1987
|
+
pbw_field_name(type->fields[i].df));
|
|
1988
|
+
}
|
|
1989
|
+
}
|
|
1990
|
+
/* Oneof: every member but the last one seen goes back to its default. */
|
|
1991
|
+
for (i = 0; i < count; i++) {
|
|
1992
|
+
field_t *f = &type->fields[i];
|
|
1993
|
+
|
|
1994
|
+
if (f->oneof != 0 && seen[i] &&
|
|
1995
|
+
winner[f->oneof - 1] != (uint32_t)(i + 1)) {
|
|
1996
|
+
if (f->type == PBW_TYPE_MESSAGE ||
|
|
1997
|
+
f->type == PBW_TYPE_GROUP) {
|
|
1998
|
+
rb_ivar_set(obj, f->ivar, Qnil);
|
|
1999
|
+
} else {
|
|
2000
|
+
rb_ivar_set(obj, f->ivar, object_default(f));
|
|
2001
|
+
if (f->has_ivar != 0) {
|
|
2002
|
+
rb_ivar_set(obj, f->has_ivar, Qfalse);
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
acc_off[i] = 0;
|
|
2006
|
+
acc_len[i] = 0;
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
/* Singular sub-messages: one recursive decode of the merged payloads. */
|
|
2010
|
+
for (i = 0; i < count; i++) {
|
|
2011
|
+
field_t *f = &type->fields[i];
|
|
2012
|
+
|
|
2013
|
+
if ((f->type == PBW_TYPE_MESSAGE ||
|
|
2014
|
+
f->type == PBW_TYPE_GROUP) &&
|
|
2015
|
+
!f->repeated && seen[i] &&
|
|
2016
|
+
(f->oneof == 0 || winner[f->oneof - 1] == (uint32_t)(i + 1))) {
|
|
2017
|
+
VALUE cur = rb_ivar_get(obj, f->ivar);
|
|
2018
|
+
VALUE child;
|
|
2019
|
+
|
|
2020
|
+
if (NIL_P(cur)) {
|
|
2021
|
+
/* Seen once: decode the recorded range in place. */
|
|
2022
|
+
child = object_decode_bytes(field_sub_type(f),
|
|
2023
|
+
data + acc_off[i], acc_len[i],
|
|
2024
|
+
depth + 1, cache);
|
|
2025
|
+
} else {
|
|
2026
|
+
/* Seen more than once: decode the accumulator, which has
|
|
2027
|
+
* been rooted on the model's ivar since it was built. */
|
|
2028
|
+
child = object_decode_bytes(field_sub_type(f),
|
|
2029
|
+
(const uint8_t *)RSTRING_PTR(cur),
|
|
2030
|
+
(size_t)RSTRING_LEN(cur),
|
|
2031
|
+
depth + 1, cache);
|
|
2032
|
+
}
|
|
2033
|
+
rb_ivar_set(obj, f->ivar, child);
|
|
2034
|
+
}
|
|
2035
|
+
}
|
|
2036
|
+
RB_GC_GUARD(cache->classes);
|
|
2037
|
+
return obj;
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
/* What an absent field looks like on the generated model. Explicit-presence
|
|
2041
|
+
* fields stay nil (the model's from_message does the same); implicit scalars
|
|
2042
|
+
* get the generated initialize defaults. */
|
|
2043
|
+
static VALUE
|
|
2044
|
+
object_default(field_t *f)
|
|
2045
|
+
{
|
|
2046
|
+
if (f->has_ivar != 0) {
|
|
2047
|
+
return Qnil;
|
|
2048
|
+
}
|
|
2049
|
+
switch ((pbw_type)f->type) {
|
|
2050
|
+
case PBW_TYPE_STRING:
|
|
2051
|
+
case PBW_TYPE_BYTES:
|
|
2052
|
+
case PBW_TYPE_MESSAGE:
|
|
2053
|
+
case PBW_TYPE_GROUP:
|
|
2054
|
+
return Qnil;
|
|
2055
|
+
case PBW_TYPE_BOOL:
|
|
2056
|
+
return Qfalse;
|
|
2057
|
+
case PBW_TYPE_FLOAT:
|
|
2058
|
+
case PBW_TYPE_DOUBLE:
|
|
2059
|
+
return DBL2NUM(0.0);
|
|
2060
|
+
default:
|
|
2061
|
+
return INT2FIX(0);
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
static void object_fill(pbw_message *msg, type_t *type, VALUE obj,
|
|
2066
|
+
pbw_ctx *ctx);
|
|
2067
|
+
|
|
2068
|
+
typedef struct {
|
|
2069
|
+
pbw_message *msg;
|
|
2070
|
+
type_t *type;
|
|
2071
|
+
VALUE obj;
|
|
2072
|
+
pbw_ctx *ctx;
|
|
2073
|
+
} object_fill_args;
|
|
2074
|
+
|
|
2075
|
+
static VALUE
|
|
2076
|
+
object_fill_protected(VALUE argp)
|
|
2077
|
+
{
|
|
2078
|
+
object_fill_args *a = (object_fill_args *)(void *)argp;
|
|
2079
|
+
|
|
2080
|
+
object_fill(a->msg, a->type, a->obj, a->ctx);
|
|
2081
|
+
return Qnil;
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
/* Walk the generated model's ivars in descriptor order and fill the tree. */
|
|
2085
|
+
static void
|
|
2086
|
+
object_fill(pbw_message *msg, type_t *type, VALUE obj, pbw_ctx *ctx)
|
|
2087
|
+
{
|
|
2088
|
+
size_t i;
|
|
2089
|
+
|
|
2090
|
+
for (i = 0; i < type->count; i++) {
|
|
2091
|
+
field_t *f = &type->fields[i];
|
|
2092
|
+
const pbw_field *df = f->df;
|
|
2093
|
+
pbw_status status;
|
|
2094
|
+
|
|
2095
|
+
if (f->repeated) {
|
|
2096
|
+
VALUE array = rb_ivar_get(obj, f->ivar);
|
|
2097
|
+
long count;
|
|
2098
|
+
long j;
|
|
2099
|
+
|
|
2100
|
+
if (NIL_P(array)) {
|
|
2101
|
+
continue;
|
|
2102
|
+
}
|
|
2103
|
+
Check_Type(array, T_ARRAY);
|
|
2104
|
+
count = RARRAY_LEN(array);
|
|
2105
|
+
for (j = 0; j < count; j++) {
|
|
2106
|
+
VALUE item = RARRAY_AREF(array, j);
|
|
2107
|
+
|
|
2108
|
+
if (f->type == PBW_TYPE_MESSAGE ||
|
|
2109
|
+
f->type == PBW_TYPE_GROUP) {
|
|
2110
|
+
pbw_message *child =
|
|
2111
|
+
pbw_message_set_message(msg, f->number);
|
|
2112
|
+
|
|
2113
|
+
if (child == NULL) {
|
|
2114
|
+
rb_raise(eError, "%s.%s: out of memory",
|
|
2115
|
+
type_name(type), RSTRING_PTR(f->name));
|
|
2116
|
+
}
|
|
2117
|
+
object_fill(child, field_sub_type(f), item, ctx);
|
|
2118
|
+
} else {
|
|
2119
|
+
pbw_value *built = object_scalar_value(f, item, ctx);
|
|
2120
|
+
|
|
2121
|
+
status = pbw_message_set_field(msg, df, built);
|
|
2122
|
+
if (status != PBW_OK) {
|
|
2123
|
+
pbw_value_free(built);
|
|
2124
|
+
rb_raise(eError, "%s.%s: %s", type_name(type),
|
|
2125
|
+
RSTRING_PTR(f->name), pbw_strerror(status));
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
continue;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
if (f->type == PBW_TYPE_MESSAGE || f->type == PBW_TYPE_GROUP) {
|
|
2133
|
+
VALUE child_obj = rb_ivar_get(obj, f->ivar);
|
|
2134
|
+
pbw_message *child;
|
|
2135
|
+
|
|
2136
|
+
if (NIL_P(child_obj)) {
|
|
2137
|
+
continue;
|
|
2138
|
+
}
|
|
2139
|
+
child = pbw_message_set_message(msg, f->number);
|
|
2140
|
+
if (child == NULL) {
|
|
2141
|
+
rb_raise(eError, "%s.%s: out of memory", type_name(type),
|
|
2142
|
+
RSTRING_PTR(f->name));
|
|
2143
|
+
}
|
|
2144
|
+
object_fill(child, field_sub_type(f), child_obj, ctx);
|
|
2145
|
+
continue;
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
{
|
|
2149
|
+
VALUE value = rb_ivar_get(obj, f->ivar);
|
|
2150
|
+
int present;
|
|
2151
|
+
|
|
2152
|
+
if (f->required) {
|
|
2153
|
+
present = 1;
|
|
2154
|
+
} else if (f->has_ivar != 0 &&
|
|
2155
|
+
rb_ivar_defined(obj, f->has_ivar)) {
|
|
2156
|
+
present = RTEST(rb_ivar_get(obj, f->has_ivar));
|
|
2157
|
+
} else {
|
|
2158
|
+
present = object_scalar_present(f, value);
|
|
2159
|
+
}
|
|
2160
|
+
if (!present) {
|
|
2161
|
+
continue;
|
|
2162
|
+
}
|
|
2163
|
+
{
|
|
2164
|
+
pbw_value *built = object_scalar_value(f, value, ctx);
|
|
2165
|
+
|
|
2166
|
+
status = pbw_message_set_field(msg, df, built);
|
|
2167
|
+
if (status != PBW_OK) {
|
|
2168
|
+
pbw_value_free(built);
|
|
2169
|
+
rb_raise(eError, "%s.%s: %s", type_name(type),
|
|
2170
|
+
RSTRING_PTR(f->name), pbw_strerror(status));
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
/* One decoded message as an instance of the mapped host class. See
|
|
2178
|
+
* object_decode_bytes above: decode goes straight from the wire, no tree. */
|
|
2179
|
+
|
|
2180
|
+
static VALUE
|
|
2181
|
+
type_encode_object(VALUE self, VALUE obj)
|
|
2182
|
+
{
|
|
2183
|
+
type_t *type;
|
|
2184
|
+
pbw_message *tree;
|
|
2185
|
+
object_fill_args args;
|
|
2186
|
+
int state = 0;
|
|
2187
|
+
size_t len = 0;
|
|
2188
|
+
VALUE out;
|
|
2189
|
+
pbw_status status;
|
|
2190
|
+
|
|
2191
|
+
TypedData_Get_Struct(self, type_t, &type_data_type, type);
|
|
2192
|
+
if (type->ctx == NULL) {
|
|
2193
|
+
type->ctx = pbw_ctx_new();
|
|
2194
|
+
if (type->ctx == NULL) {
|
|
2195
|
+
rb_raise(eError, "%s: out of memory", type_name(type));
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
pbw_ctx_reset(type->ctx);
|
|
2199
|
+
tree = pbw_message_new_ctx(type->ctx, type->desc);
|
|
2200
|
+
if (tree == NULL) {
|
|
2201
|
+
rb_raise(eError, "%s: out of memory", type_name(type));
|
|
2202
|
+
}
|
|
2203
|
+
args.msg = tree;
|
|
2204
|
+
args.type = type;
|
|
2205
|
+
args.obj = obj;
|
|
2206
|
+
args.ctx = type->ctx;
|
|
2207
|
+
rb_protect(object_fill_protected, (VALUE)(void *)&args, &state);
|
|
2208
|
+
if (state != 0) {
|
|
2209
|
+
rb_jump_tag(state);
|
|
2210
|
+
}
|
|
2211
|
+
{
|
|
2212
|
+
const uint8_t *data = NULL;
|
|
2213
|
+
|
|
2214
|
+
status = pbw_encode_ctx(type->ctx, tree, &data, &len);
|
|
2215
|
+
if (status != PBW_OK) {
|
|
2216
|
+
rb_raise(eError, "%s: %s", type_name(type),
|
|
2217
|
+
pbw_strerror(status));
|
|
2218
|
+
}
|
|
2219
|
+
out = rb_str_new((const char *)data, (long)len);
|
|
2220
|
+
}
|
|
2221
|
+
return out;
|
|
2222
|
+
}
|
|
2223
|
+
|
|
2224
|
+
static VALUE
|
|
2225
|
+
type_decode_object(int argc, VALUE *argv, VALUE self)
|
|
2226
|
+
{
|
|
2227
|
+
type_t *type;
|
|
2228
|
+
VALUE wire;
|
|
2229
|
+
VALUE classes = Qnil;
|
|
2230
|
+
|
|
2231
|
+
rb_scan_args(argc, argv, "11", &wire, &classes);
|
|
2232
|
+
TypedData_Get_Struct(self, type_t, &type_data_type, type);
|
|
2233
|
+
StringValue(wire);
|
|
2234
|
+
/* One C call: the layout table walks the bytes straight into the model.
|
|
2235
|
+
* No tree, no per-field API; stack scratch only, so a raise unwinds
|
|
2236
|
+
* with nothing to free. */
|
|
2237
|
+
{
|
|
2238
|
+
klass_cache_t cache;
|
|
2239
|
+
|
|
2240
|
+
cache.classes = classes;
|
|
2241
|
+
cache.count = 0;
|
|
2242
|
+
return object_decode_bytes(type, (const uint8_t *)RSTRING_PTR(wire),
|
|
2243
|
+
(size_t)RSTRING_LEN(wire), 0, &cache);
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
|
|
1575
2247
|
static VALUE
|
|
1576
2248
|
schema_do_load(VALUE klass, VALUE bytes, const pbw_limits *limits)
|
|
1577
2249
|
{
|
|
@@ -1653,6 +2325,23 @@ schema_message(VALUE self, VALUE name)
|
|
|
1653
2325
|
return message_builder_new(type, own->holder, tree, Qnil, 1);
|
|
1654
2326
|
}
|
|
1655
2327
|
|
|
2328
|
+
/* The Type behind a message name: the C-side field table, and the receiver
|
|
2329
|
+
* of encode_object/decode_object. Keeping it reachable lets a caller compile
|
|
2330
|
+
* the type once and skip schema_message()'s builder allocation. */
|
|
2331
|
+
static VALUE
|
|
2332
|
+
schema_type(VALUE self, VALUE name)
|
|
2333
|
+
{
|
|
2334
|
+
pbw_schema *schema = schema_ptr(self);
|
|
2335
|
+
const pbw_descriptor *desc;
|
|
2336
|
+
|
|
2337
|
+
StringValue(name);
|
|
2338
|
+
desc = pbw_schema_find(schema, StringValueCStr(name));
|
|
2339
|
+
if (desc == NULL || pbw_descriptor_is_enum(desc)) {
|
|
2340
|
+
rb_raise(eError, "no message %" PRIsVALUE " in the schema", name);
|
|
2341
|
+
}
|
|
2342
|
+
return type_for(self, desc);
|
|
2343
|
+
}
|
|
2344
|
+
|
|
1656
2345
|
static VALUE
|
|
1657
2346
|
schema_close(VALUE self)
|
|
1658
2347
|
{
|
|
@@ -1706,8 +2395,7 @@ message_set(VALUE self, VALUE key, VALUE value)
|
|
|
1706
2395
|
pbw_message *tree = message_tree(self, &message);
|
|
1707
2396
|
type_t *type = message_type_of(self);
|
|
1708
2397
|
field_t *f = field_lookup(type, key);
|
|
1709
|
-
const pbw_field *descriptor_field =
|
|
1710
|
-
pbw_descriptor_field_at(type->desc, (size_t)f->index);
|
|
2398
|
+
const pbw_field *descriptor_field = f->df;
|
|
1711
2399
|
pbw_status status;
|
|
1712
2400
|
int moved = 0;
|
|
1713
2401
|
|
|
@@ -1798,8 +2486,7 @@ message_get(VALUE self, VALUE key)
|
|
|
1798
2486
|
/* Also checks that no parent this builder was borrowed from has
|
|
1799
2487
|
* been closed under it. */
|
|
1800
2488
|
pbw_message *tree = message_read_tree(self, &message);
|
|
1801
|
-
pbw_value *value = pbw_message_get_field(
|
|
1802
|
-
tree, pbw_descriptor_field_at(type->desc, (size_t)f->index));
|
|
2489
|
+
pbw_value *value = pbw_message_get_field(tree, f->df);
|
|
1803
2490
|
|
|
1804
2491
|
if (f->repeated) {
|
|
1805
2492
|
VALUE array;
|
|
@@ -2894,6 +3581,36 @@ static const rb_data_type_t pool_data_type = {
|
|
|
2894
3581
|
RUBY_TYPED_FREE_IMMEDIATELY,
|
|
2895
3582
|
};
|
|
2896
3583
|
|
|
3584
|
+
/* Allocators so rb_obj_alloc works for Context/Pool (typed_object_alloc
|
|
3585
|
+
* goes through the class allocator): without one, Context.new/Pool.new
|
|
3586
|
+
* raise TypeError. Zero-fill then set immediates explicitly, like the
|
|
3587
|
+
* Schema/Message allocators above, so a bare allocate never trips the
|
|
3588
|
+
* marker. */
|
|
3589
|
+
static VALUE
|
|
3590
|
+
ctx_alloc(VALUE klass)
|
|
3591
|
+
{
|
|
3592
|
+
ctx_t *ctx;
|
|
3593
|
+
VALUE obj = TypedData_Make_Struct(klass, ctx_t, &ctx_data_type, ctx);
|
|
3594
|
+
|
|
3595
|
+
ctx->ctx = NULL;
|
|
3596
|
+
ctx->pool = NULL;
|
|
3597
|
+
ctx->pool_obj = Qnil;
|
|
3598
|
+
ctx->released = 1;
|
|
3599
|
+
ctx->gen = 0;
|
|
3600
|
+
ctx->retain_cap = 0;
|
|
3601
|
+
return obj;
|
|
3602
|
+
}
|
|
3603
|
+
|
|
3604
|
+
static VALUE
|
|
3605
|
+
pool_alloc(VALUE klass)
|
|
3606
|
+
{
|
|
3607
|
+
pool_t *pool;
|
|
3608
|
+
VALUE obj = TypedData_Make_Struct(klass, pool_t, &pool_data_type, pool);
|
|
3609
|
+
|
|
3610
|
+
pool->pool = NULL;
|
|
3611
|
+
return obj;
|
|
3612
|
+
}
|
|
3613
|
+
|
|
2897
3614
|
static ctx_t *
|
|
2898
3615
|
ctx_ptr(VALUE obj)
|
|
2899
3616
|
{
|
|
@@ -3228,6 +3945,34 @@ message_inspect(VALUE self)
|
|
|
3228
3945
|
type_name(message_type_of(self)));
|
|
3229
3946
|
}
|
|
3230
3947
|
|
|
3948
|
+
/* Message-side twins of the Type object codec: a builder already carries its
|
|
3949
|
+
* type, so pbench can compile one handle and use it for both arms. */
|
|
3950
|
+
static VALUE
|
|
3951
|
+
message_encode_object(VALUE self, VALUE obj)
|
|
3952
|
+
{
|
|
3953
|
+
message_t *message;
|
|
3954
|
+
|
|
3955
|
+
TypedData_Get_Struct(self, message_t, &message_data_type, message);
|
|
3956
|
+
if (message->kind == MSG_CLOSED) {
|
|
3957
|
+
rb_raise(eError, "%s: message is closed",
|
|
3958
|
+
type_name(message_type_of(self)));
|
|
3959
|
+
}
|
|
3960
|
+
return type_encode_object(message->type, obj);
|
|
3961
|
+
}
|
|
3962
|
+
|
|
3963
|
+
static VALUE
|
|
3964
|
+
message_decode_object(int argc, VALUE *argv, VALUE self)
|
|
3965
|
+
{
|
|
3966
|
+
message_t *message;
|
|
3967
|
+
|
|
3968
|
+
TypedData_Get_Struct(self, message_t, &message_data_type, message);
|
|
3969
|
+
if (message->kind == MSG_CLOSED) {
|
|
3970
|
+
rb_raise(eError, "%s: message is closed",
|
|
3971
|
+
type_name(message_type_of(self)));
|
|
3972
|
+
}
|
|
3973
|
+
return type_decode_object(argc, argv, message->type);
|
|
3974
|
+
}
|
|
3975
|
+
|
|
3231
3976
|
/* ------------------------------------------------------------- extension */
|
|
3232
3977
|
|
|
3233
3978
|
void
|
|
@@ -3246,6 +3991,7 @@ Init_pbwire_ext(void)
|
|
|
3246
3991
|
rb_define_method(cSchema, "service", schema_service, 1);
|
|
3247
3992
|
rb_define_method(cSchema, "resolve", schema_resolve, 2);
|
|
3248
3993
|
rb_define_method(cSchema, "message", schema_message, 1);
|
|
3994
|
+
rb_define_method(cSchema, "type", schema_type, 1);
|
|
3249
3995
|
rb_define_method(cSchema, "close", schema_close, 0);
|
|
3250
3996
|
rb_define_method(cSchema, "release", schema_close, 0);
|
|
3251
3997
|
rb_define_method(cSchema, "closed?", schema_closed_p, 0);
|
|
@@ -3254,8 +4000,11 @@ Init_pbwire_ext(void)
|
|
|
3254
4000
|
|
|
3255
4001
|
cType = rb_define_class_under(mPbwire, "Type", rb_cObject);
|
|
3256
4002
|
rb_define_alloc_func(cType, type_alloc);
|
|
4003
|
+
rb_define_method(cType, "encode_object", type_encode_object, 1);
|
|
4004
|
+
rb_define_method(cType, "decode_object", type_decode_object, -1);
|
|
3257
4005
|
|
|
3258
4006
|
cPool = rb_define_class_under(mPbwire, "Pool", rb_cObject);
|
|
4007
|
+
rb_define_alloc_func(cPool, pool_alloc);
|
|
3259
4008
|
rb_define_singleton_method(cPool, "new", pool_s_new, -1);
|
|
3260
4009
|
rb_define_method(cPool, "acquire", pool_acquire, 0);
|
|
3261
4010
|
rb_define_method(cPool, "release", pool_release, 1);
|
|
@@ -3263,6 +4012,7 @@ Init_pbwire_ext(void)
|
|
|
3263
4012
|
rb_define_method(cPool, "close", pool_close, 0);
|
|
3264
4013
|
|
|
3265
4014
|
cContext = rb_define_class_under(mPbwire, "Context", rb_cObject);
|
|
4015
|
+
rb_define_alloc_func(cContext, ctx_alloc);
|
|
3266
4016
|
rb_define_singleton_method(cContext, "new", context_s_new, 0);
|
|
3267
4017
|
rb_define_method(cContext, "decode", context_decode, -1);
|
|
3268
4018
|
rb_define_method(cContext, "encode", context_encode, 1);
|
|
@@ -3282,6 +4032,8 @@ Init_pbwire_ext(void)
|
|
|
3282
4032
|
rb_define_method(cMessage, "[]", message_get, 1);
|
|
3283
4033
|
rb_define_method(cMessage, "encode", message_encode, 0);
|
|
3284
4034
|
rb_define_method(cMessage, "decode", message_decode, -1);
|
|
4035
|
+
rb_define_method(cMessage, "encode_object", message_encode_object, 1);
|
|
4036
|
+
rb_define_method(cMessage, "decode_object", message_decode_object, -1);
|
|
3285
4037
|
rb_define_method(cMessage, "encoded_size", message_encoded_size, 0);
|
|
3286
4038
|
rb_define_method(cMessage, "encode_to", message_encode_to, 1);
|
|
3287
4039
|
rb_define_method(cMessage, "encode_delimited_to",
|