google-protobuf 3.0.0.alpha.3 → 3.0.0.alpha.3.1.pre
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/ext/google/protobuf_c/defs.c +5 -3
- data/ext/google/protobuf_c/encode_decode.c +85 -75
- data/ext/google/protobuf_c/extconf.rb +1 -3
- data/ext/google/protobuf_c/message.c +9 -44
- data/ext/google/protobuf_c/protobuf.c +7 -10
- data/ext/google/protobuf_c/protobuf.h +9 -9
- data/ext/google/protobuf_c/repeated_field.c +55 -95
- data/ext/google/protobuf_c/storage.c +2 -5
- data/ext/google/protobuf_c/upb.c +248 -822
- data/ext/google/protobuf_c/upb.h +467 -511
- data/lib/google/protobuf.rb +0 -27
- data/tests/basic.rb +9 -119
- metadata +17 -27
- data/lib/google/protobuf/message_exts.rb +0 -53
- data/lib/google/protobuf/repeated_field.rb +0 -188
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca9558c2c42ccd56fd9d59a0bb5fbfce8bd1e859
|
4
|
+
data.tar.gz: a4dffb25e201de8de48abf95adbf826e08ae8bfc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a85bc6f101db5ec99d6c5200c705df4c7ec5bb330509c245e2007bea56f0ab2efcea450557b9bf0200d66c176cbdb6e5faf0ad4ee416fb4a09946895f15c9629
|
7
|
+
data.tar.gz: 368efb205aaa68d95a7633f39a9f37ce1002bb5789f3339cf56f0c73aec8a35c6066f2f802b0bbdcdcef032e77eb6866991ba6147c7b5eb1c7120c82524d69b0
|
@@ -34,6 +34,8 @@
|
|
34
34
|
// Common utilities.
|
35
35
|
// -----------------------------------------------------------------------------
|
36
36
|
|
37
|
+
const char* kDescriptorInstanceVar = "descriptor";
|
38
|
+
|
37
39
|
static const char* get_str(VALUE str) {
|
38
40
|
Check_Type(str, T_STRING);
|
39
41
|
return RSTRING_PTR(str);
|
@@ -1588,9 +1590,9 @@ VALUE Builder_add_message(VALUE _self, VALUE name) {
|
|
1588
1590
|
* call-seq:
|
1589
1591
|
* Builder.add_enum(name, &block)
|
1590
1592
|
*
|
1591
|
-
* Creates a new, empty enum descriptor with the given name, and invokes the
|
1592
|
-
*
|
1593
|
-
*
|
1593
|
+
* Creates a new, empty enum descriptor with the given name, and invokes the block in
|
1594
|
+
* the context of an EnumBuilderContext on that descriptor. The block can then
|
1595
|
+
* call EnumBuilderContext#add_value to define the enum values.
|
1594
1596
|
*
|
1595
1597
|
* This is the recommended, idiomatic way to build enum definitions.
|
1596
1598
|
*/
|
@@ -622,49 +622,6 @@ static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) {
|
|
622
622
|
return desc->fill_method;
|
623
623
|
}
|
624
624
|
|
625
|
-
|
626
|
-
// Stack-allocated context during an encode/decode operation. Contains the upb
|
627
|
-
// environment and its stack-based allocator, an initial buffer for allocations
|
628
|
-
// to avoid malloc() when possible, and a template for Ruby exception messages
|
629
|
-
// if any error occurs.
|
630
|
-
#define STACK_ENV_STACKBYTES 4096
|
631
|
-
typedef struct {
|
632
|
-
upb_env env;
|
633
|
-
upb_seededalloc alloc;
|
634
|
-
const char* ruby_error_template;
|
635
|
-
char allocbuf[STACK_ENV_STACKBYTES];
|
636
|
-
} stackenv;
|
637
|
-
|
638
|
-
static void stackenv_init(stackenv* se, const char* errmsg);
|
639
|
-
static void stackenv_uninit(stackenv* se);
|
640
|
-
|
641
|
-
// Callback invoked by upb if any error occurs during parsing or serialization.
|
642
|
-
static bool env_error_func(void* ud, const upb_status* status) {
|
643
|
-
stackenv* se = ud;
|
644
|
-
// Free the env -- rb_raise will longjmp up the stack past the encode/decode
|
645
|
-
// function so it would not otherwise have been freed.
|
646
|
-
stackenv_uninit(se);
|
647
|
-
rb_raise(rb_eRuntimeError, se->ruby_error_template,
|
648
|
-
upb_status_errmsg(status));
|
649
|
-
// Never reached: rb_raise() always longjmp()s up the stack, past all of our
|
650
|
-
// code, back to Ruby.
|
651
|
-
return false;
|
652
|
-
}
|
653
|
-
|
654
|
-
static void stackenv_init(stackenv* se, const char* errmsg) {
|
655
|
-
se->ruby_error_template = errmsg;
|
656
|
-
upb_env_init(&se->env);
|
657
|
-
upb_seededalloc_init(&se->alloc, &se->allocbuf, STACK_ENV_STACKBYTES);
|
658
|
-
upb_env_setallocfunc(
|
659
|
-
&se->env, upb_seededalloc_getallocfunc(&se->alloc), &se->alloc);
|
660
|
-
upb_env_seterrorfunc(&se->env, env_error_func, se);
|
661
|
-
}
|
662
|
-
|
663
|
-
static void stackenv_uninit(stackenv* se) {
|
664
|
-
upb_env_uninit(&se->env);
|
665
|
-
upb_seededalloc_uninit(&se->alloc);
|
666
|
-
}
|
667
|
-
|
668
625
|
/*
|
669
626
|
* call-seq:
|
670
627
|
* MessageClass.decode(data) => message
|
@@ -674,7 +631,7 @@ static void stackenv_uninit(stackenv* se) {
|
|
674
631
|
* and returns a message object with the corresponding field values.
|
675
632
|
*/
|
676
633
|
VALUE Message_decode(VALUE klass, VALUE data) {
|
677
|
-
VALUE descriptor =
|
634
|
+
VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
|
678
635
|
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
679
636
|
VALUE msgklass = Descriptor_msgclass(descriptor);
|
680
637
|
|
@@ -688,17 +645,21 @@ VALUE Message_decode(VALUE klass, VALUE data) {
|
|
688
645
|
|
689
646
|
const upb_pbdecodermethod* method = msgdef_decodermethod(desc);
|
690
647
|
const upb_handlers* h = upb_pbdecodermethod_desthandlers(method);
|
691
|
-
|
692
|
-
stackenv_init(&se, "Error occurred during parsing: %s");
|
693
|
-
|
648
|
+
upb_pbdecoder decoder;
|
694
649
|
upb_sink sink;
|
650
|
+
upb_status status = UPB_STATUS_INIT;
|
651
|
+
|
652
|
+
upb_pbdecoder_init(&decoder, method, &status);
|
695
653
|
upb_sink_reset(&sink, h, msg);
|
696
|
-
|
697
|
-
upb_pbdecoder_create(&se.env, method, &sink);
|
654
|
+
upb_pbdecoder_resetoutput(&decoder, &sink);
|
698
655
|
upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
|
699
|
-
upb_pbdecoder_input(decoder));
|
656
|
+
upb_pbdecoder_input(&decoder));
|
700
657
|
|
701
|
-
|
658
|
+
upb_pbdecoder_uninit(&decoder);
|
659
|
+
if (!upb_ok(&status)) {
|
660
|
+
rb_raise(rb_eRuntimeError, "Error occurred during parsing: %s.",
|
661
|
+
upb_status_errmsg(&status));
|
662
|
+
}
|
702
663
|
|
703
664
|
return msg_rb;
|
704
665
|
}
|
@@ -712,7 +673,7 @@ VALUE Message_decode(VALUE klass, VALUE data) {
|
|
712
673
|
* and returns a message object with the corresponding field values.
|
713
674
|
*/
|
714
675
|
VALUE Message_decode_json(VALUE klass, VALUE data) {
|
715
|
-
VALUE descriptor =
|
676
|
+
VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
|
716
677
|
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
717
678
|
VALUE msgklass = Descriptor_msgclass(descriptor);
|
718
679
|
|
@@ -727,16 +688,21 @@ VALUE Message_decode_json(VALUE klass, VALUE data) {
|
|
727
688
|
MessageHeader* msg;
|
728
689
|
TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
|
729
690
|
|
730
|
-
|
731
|
-
|
691
|
+
upb_status status = UPB_STATUS_INIT;
|
692
|
+
upb_json_parser parser;
|
693
|
+
upb_json_parser_init(&parser, &status);
|
732
694
|
|
733
695
|
upb_sink sink;
|
734
696
|
upb_sink_reset(&sink, get_fill_handlers(desc), msg);
|
735
|
-
|
697
|
+
upb_json_parser_resetoutput(&parser, &sink);
|
736
698
|
upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
|
737
|
-
upb_json_parser_input(parser));
|
699
|
+
upb_json_parser_input(&parser));
|
738
700
|
|
739
|
-
|
701
|
+
upb_json_parser_uninit(&parser);
|
702
|
+
if (!upb_ok(&status)) {
|
703
|
+
rb_raise(rb_eRuntimeError, "Error occurred during parsing: %s.",
|
704
|
+
upb_status_errmsg(&status));
|
705
|
+
}
|
740
706
|
|
741
707
|
return msg_rb;
|
742
708
|
}
|
@@ -847,7 +813,7 @@ static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink,
|
|
847
813
|
if (submsg == Qnil) return;
|
848
814
|
|
849
815
|
upb_sink subsink;
|
850
|
-
VALUE descriptor =
|
816
|
+
VALUE descriptor = rb_iv_get(submsg, kDescriptorInstanceVar);
|
851
817
|
Descriptor* subdesc = ruby_to_Descriptor(descriptor);
|
852
818
|
|
853
819
|
upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink);
|
@@ -969,8 +935,7 @@ static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink,
|
|
969
935
|
VALUE value = Map_iter_value(&it);
|
970
936
|
|
971
937
|
upb_sink entry_sink;
|
972
|
-
upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG),
|
973
|
-
&entry_sink);
|
938
|
+
upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG), &entry_sink);
|
974
939
|
upb_sink_startmsg(&entry_sink);
|
975
940
|
|
976
941
|
put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink);
|
@@ -991,7 +956,7 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
|
|
991
956
|
|
992
957
|
// Protect against cycles (possible because users may freely reassign message
|
993
958
|
// and repeated fields) by imposing a maximum recursion depth.
|
994
|
-
if (depth >
|
959
|
+
if (depth > UPB_SINK_MAX_NESTING) {
|
995
960
|
rb_raise(rb_eRuntimeError,
|
996
961
|
"Maximum recursion depth exceeded during encoding.");
|
997
962
|
}
|
@@ -1100,7 +1065,7 @@ static const upb_handlers* msgdef_json_serialize_handlers(Descriptor* desc) {
|
|
1100
1065
|
* wire format.
|
1101
1066
|
*/
|
1102
1067
|
VALUE Message_encode(VALUE klass, VALUE msg_rb) {
|
1103
|
-
VALUE descriptor =
|
1068
|
+
VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
|
1104
1069
|
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
1105
1070
|
|
1106
1071
|
stringsink sink;
|
@@ -1109,16 +1074,15 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb) {
|
|
1109
1074
|
const upb_handlers* serialize_handlers =
|
1110
1075
|
msgdef_pb_serialize_handlers(desc);
|
1111
1076
|
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink);
|
1077
|
+
upb_pb_encoder encoder;
|
1078
|
+
upb_pb_encoder_init(&encoder, serialize_handlers);
|
1079
|
+
upb_pb_encoder_resetoutput(&encoder, &sink.sink);
|
1116
1080
|
|
1117
|
-
putmsg(msg_rb, desc, upb_pb_encoder_input(encoder), 0);
|
1081
|
+
putmsg(msg_rb, desc, upb_pb_encoder_input(&encoder), 0);
|
1118
1082
|
|
1119
1083
|
VALUE ret = rb_str_new(sink.ptr, sink.len);
|
1120
1084
|
|
1121
|
-
|
1085
|
+
upb_pb_encoder_uninit(&encoder);
|
1122
1086
|
stringsink_uninit(&sink);
|
1123
1087
|
|
1124
1088
|
return ret;
|
@@ -1131,7 +1095,7 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb) {
|
|
1131
1095
|
* Encodes the given message object into its serialized JSON representation.
|
1132
1096
|
*/
|
1133
1097
|
VALUE Message_encode_json(VALUE klass, VALUE msg_rb) {
|
1134
|
-
VALUE descriptor =
|
1098
|
+
VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
|
1135
1099
|
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
1136
1100
|
|
1137
1101
|
stringsink sink;
|
@@ -1140,18 +1104,64 @@ VALUE Message_encode_json(VALUE klass, VALUE msg_rb) {
|
|
1140
1104
|
const upb_handlers* serialize_handlers =
|
1141
1105
|
msgdef_json_serialize_handlers(desc);
|
1142
1106
|
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
upb_json_printer_create(&se.env, serialize_handlers, &sink.sink);
|
1107
|
+
upb_json_printer printer;
|
1108
|
+
upb_json_printer_init(&printer, serialize_handlers);
|
1109
|
+
upb_json_printer_resetoutput(&printer, &sink.sink);
|
1147
1110
|
|
1148
|
-
putmsg(msg_rb, desc, upb_json_printer_input(printer), 0);
|
1111
|
+
putmsg(msg_rb, desc, upb_json_printer_input(&printer), 0);
|
1149
1112
|
|
1150
1113
|
VALUE ret = rb_str_new(sink.ptr, sink.len);
|
1151
1114
|
|
1152
|
-
|
1115
|
+
upb_json_printer_uninit(&printer);
|
1153
1116
|
stringsink_uninit(&sink);
|
1154
1117
|
|
1155
1118
|
return ret;
|
1156
1119
|
}
|
1157
1120
|
|
1121
|
+
/*
|
1122
|
+
* call-seq:
|
1123
|
+
* Google::Protobuf.encode(msg) => bytes
|
1124
|
+
*
|
1125
|
+
* Encodes the given message object to protocol buffers wire format. This is an
|
1126
|
+
* alternative to the #encode method on msg's class.
|
1127
|
+
*/
|
1128
|
+
VALUE Google_Protobuf_encode(VALUE self, VALUE msg_rb) {
|
1129
|
+
VALUE klass = CLASS_OF(msg_rb);
|
1130
|
+
return Message_encode(klass, msg_rb);
|
1131
|
+
}
|
1132
|
+
|
1133
|
+
/*
|
1134
|
+
* call-seq:
|
1135
|
+
* Google::Protobuf.encode_json(msg) => json_string
|
1136
|
+
*
|
1137
|
+
* Encodes the given message object to its JSON representation. This is an
|
1138
|
+
* alternative to the #encode_json method on msg's class.
|
1139
|
+
*/
|
1140
|
+
VALUE Google_Protobuf_encode_json(VALUE self, VALUE msg_rb) {
|
1141
|
+
VALUE klass = CLASS_OF(msg_rb);
|
1142
|
+
return Message_encode_json(klass, msg_rb);
|
1143
|
+
}
|
1144
|
+
|
1145
|
+
/*
|
1146
|
+
* call-seq:
|
1147
|
+
* Google::Protobuf.decode(class, bytes) => msg
|
1148
|
+
*
|
1149
|
+
* Decodes the given bytes as protocol buffers wire format under the
|
1150
|
+
* interpretation given by the given class's message definition. This is an
|
1151
|
+
* alternative to the #decode method on the given class.
|
1152
|
+
*/
|
1153
|
+
VALUE Google_Protobuf_decode(VALUE self, VALUE klass, VALUE msg_rb) {
|
1154
|
+
return Message_decode(klass, msg_rb);
|
1155
|
+
}
|
1156
|
+
|
1157
|
+
/*
|
1158
|
+
* call-seq:
|
1159
|
+
* Google::Protobuf.decode_json(class, json_string) => msg
|
1160
|
+
*
|
1161
|
+
* Decodes the given JSON string under the interpretation given by the given
|
1162
|
+
* class's message definition. This is an alternative to the #decode_json method
|
1163
|
+
* on the given class.
|
1164
|
+
*/
|
1165
|
+
VALUE Google_Protobuf_decode_json(VALUE self, VALUE klass, VALUE msg_rb) {
|
1166
|
+
return Message_decode_json(klass, msg_rb);
|
1167
|
+
}
|
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'mkmf'
|
4
4
|
|
5
|
-
$CFLAGS += " -O3 -std=c99 -Wno-unused-function "
|
6
|
-
"-Wno-declaration-after-statement -Wno-unused-variable " +
|
7
|
-
"-Wno-sign-compare -DNDEBUG "
|
5
|
+
$CFLAGS += " -O3 -std=c99 -Wno-unused-function -DNDEBUG "
|
8
6
|
|
9
7
|
$objs = ["protobuf.o", "defs.o", "storage.o", "message.o",
|
10
8
|
"repeated_field.o", "map.o", "encode_decode.o", "upb.o"]
|
@@ -53,7 +53,7 @@ rb_data_type_t Message_type = {
|
|
53
53
|
};
|
54
54
|
|
55
55
|
VALUE Message_alloc(VALUE klass) {
|
56
|
-
VALUE descriptor =
|
56
|
+
VALUE descriptor = rb_iv_get(klass, kDescriptorInstanceVar);
|
57
57
|
Descriptor* desc = ruby_to_Descriptor(descriptor);
|
58
58
|
MessageHeader* msg = (MessageHeader*)ALLOC_N(
|
59
59
|
uint8_t, sizeof(MessageHeader) + desc->layout->size);
|
@@ -63,7 +63,7 @@ VALUE Message_alloc(VALUE klass) {
|
|
63
63
|
// a collection happens during object creation in layout_init().
|
64
64
|
VALUE ret = TypedData_Wrap_Struct(klass, &Message_type, msg);
|
65
65
|
msg->descriptor = desc;
|
66
|
-
|
66
|
+
rb_iv_set(ret, kDescriptorInstanceVar, descriptor);
|
67
67
|
|
68
68
|
layout_init(desc->layout, Message_data(msg));
|
69
69
|
|
@@ -86,7 +86,7 @@ static VALUE which_oneof_field(MessageHeader* self, const upb_oneofdef* o) {
|
|
86
86
|
size_t case_ofs =
|
87
87
|
self->descriptor->layout->
|
88
88
|
fields[upb_fielddef_index(first_field)].case_offset;
|
89
|
-
uint32_t oneof_case = *((uint32_t*)(
|
89
|
+
uint32_t oneof_case = *((uint32_t*)(Message_data(self) + case_ofs));
|
90
90
|
|
91
91
|
if (oneof_case == ONEOF_CASE_NONE) {
|
92
92
|
return Qnil;
|
@@ -329,31 +329,6 @@ VALUE Message_inspect(VALUE _self) {
|
|
329
329
|
return str;
|
330
330
|
}
|
331
331
|
|
332
|
-
|
333
|
-
VALUE Message_to_h(VALUE _self) {
|
334
|
-
MessageHeader* self;
|
335
|
-
TypedData_Get_Struct(_self, MessageHeader, &Message_type, self);
|
336
|
-
|
337
|
-
VALUE hash = rb_hash_new();
|
338
|
-
|
339
|
-
upb_msg_field_iter it;
|
340
|
-
for (upb_msg_field_begin(&it, self->descriptor->msgdef);
|
341
|
-
!upb_msg_field_done(&it);
|
342
|
-
upb_msg_field_next(&it)) {
|
343
|
-
const upb_fielddef* field = upb_msg_iter_field(&it);
|
344
|
-
VALUE msg_value = layout_get(self->descriptor->layout, Message_data(self),
|
345
|
-
field);
|
346
|
-
VALUE msg_key = ID2SYM(rb_intern(upb_fielddef_name(field)));
|
347
|
-
if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
|
348
|
-
msg_value = RepeatedField_to_ary(msg_value);
|
349
|
-
}
|
350
|
-
rb_hash_aset(hash, msg_key, msg_value);
|
351
|
-
}
|
352
|
-
return hash;
|
353
|
-
}
|
354
|
-
|
355
|
-
|
356
|
-
|
357
332
|
/*
|
358
333
|
* call-seq:
|
359
334
|
* Message.[](index) => value
|
@@ -401,7 +376,7 @@ VALUE Message_index_set(VALUE _self, VALUE field_name, VALUE value) {
|
|
401
376
|
* message class's type.
|
402
377
|
*/
|
403
378
|
VALUE Message_descriptor(VALUE klass) {
|
404
|
-
return
|
379
|
+
return rb_iv_get(klass, kDescriptorInstanceVar);
|
405
380
|
}
|
406
381
|
|
407
382
|
VALUE build_class_from_descriptor(Descriptor* desc) {
|
@@ -422,14 +397,8 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
422
397
|
// their own toplevel constant class name.
|
423
398
|
rb_intern("Message"),
|
424
399
|
rb_cObject);
|
425
|
-
|
426
|
-
get_def_obj(desc->msgdef));
|
400
|
+
rb_iv_set(klass, kDescriptorInstanceVar, get_def_obj(desc->msgdef));
|
427
401
|
rb_define_alloc_func(klass, Message_alloc);
|
428
|
-
rb_require("google/protobuf/message_exts");
|
429
|
-
rb_include_module(klass, rb_eval_string("Google::Protobuf::MessageExts"));
|
430
|
-
rb_extend_object(
|
431
|
-
klass, rb_eval_string("Google::Protobuf::MessageExts::ClassMethods"));
|
432
|
-
|
433
402
|
rb_define_method(klass, "method_missing",
|
434
403
|
Message_method_missing, -1);
|
435
404
|
rb_define_method(klass, "initialize", Message_initialize, -1);
|
@@ -438,8 +407,6 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
438
407
|
rb_define_method(klass, "clone", Message_dup, 0);
|
439
408
|
rb_define_method(klass, "==", Message_eq, 1);
|
440
409
|
rb_define_method(klass, "hash", Message_hash, 0);
|
441
|
-
rb_define_method(klass, "to_h", Message_to_h, 0);
|
442
|
-
rb_define_method(klass, "to_hash", Message_to_h, 0);
|
443
410
|
rb_define_method(klass, "inspect", Message_inspect, 0);
|
444
411
|
rb_define_method(klass, "[]", Message_index, 1);
|
445
412
|
rb_define_method(klass, "[]=", Message_index_set, 2);
|
@@ -448,7 +415,6 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
448
415
|
rb_define_singleton_method(klass, "decode_json", Message_decode_json, 1);
|
449
416
|
rb_define_singleton_method(klass, "encode_json", Message_encode_json, 1);
|
450
417
|
rb_define_singleton_method(klass, "descriptor", Message_descriptor, 0);
|
451
|
-
|
452
418
|
return klass;
|
453
419
|
}
|
454
420
|
|
@@ -461,7 +427,7 @@ VALUE build_class_from_descriptor(Descriptor* desc) {
|
|
461
427
|
*/
|
462
428
|
VALUE enum_lookup(VALUE self, VALUE number) {
|
463
429
|
int32_t num = NUM2INT(number);
|
464
|
-
VALUE desc =
|
430
|
+
VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
|
465
431
|
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
|
466
432
|
|
467
433
|
const char* name = upb_enumdef_iton(enumdesc->enumdef, num);
|
@@ -481,7 +447,7 @@ VALUE enum_lookup(VALUE self, VALUE number) {
|
|
481
447
|
*/
|
482
448
|
VALUE enum_resolve(VALUE self, VALUE sym) {
|
483
449
|
const char* name = rb_id2name(SYM2ID(sym));
|
484
|
-
VALUE desc =
|
450
|
+
VALUE desc = rb_iv_get(self, kDescriptorInstanceVar);
|
485
451
|
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(desc);
|
486
452
|
|
487
453
|
int32_t num = 0;
|
@@ -501,7 +467,7 @@ VALUE enum_resolve(VALUE self, VALUE sym) {
|
|
501
467
|
* EnumDescriptor corresponding to this enum type.
|
502
468
|
*/
|
503
469
|
VALUE enum_descriptor(VALUE self) {
|
504
|
-
return
|
470
|
+
return rb_iv_get(self, kDescriptorInstanceVar);
|
505
471
|
}
|
506
472
|
|
507
473
|
VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
|
@@ -526,8 +492,7 @@ VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
|
|
526
492
|
rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
|
527
493
|
rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
|
528
494
|
rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
|
529
|
-
|
530
|
-
get_def_obj(enumdesc->enumdef));
|
495
|
+
rb_iv_set(mod, kDescriptorInstanceVar, get_def_obj(enumdesc->enumdef));
|
531
496
|
|
532
497
|
return mod;
|
533
498
|
}
|
@@ -64,15 +64,6 @@ rb_encoding* kRubyStringUtf8Encoding;
|
|
64
64
|
rb_encoding* kRubyStringASCIIEncoding;
|
65
65
|
rb_encoding* kRubyString8bitEncoding;
|
66
66
|
|
67
|
-
// Ruby-interned string: "descriptor". We use this identifier to store an
|
68
|
-
// instance variable on message classes we create in order to link them back to
|
69
|
-
// their descriptors.
|
70
|
-
//
|
71
|
-
// We intern this once at module load time then use the interned identifier at
|
72
|
-
// runtime in order to avoid the cost of repeatedly interning in hot paths.
|
73
|
-
const char* kDescriptorInstanceVar = "descriptor";
|
74
|
-
ID descriptor_instancevar_interned;
|
75
|
-
|
76
67
|
// -----------------------------------------------------------------------------
|
77
68
|
// Initialization/entry point.
|
78
69
|
// -----------------------------------------------------------------------------
|
@@ -80,7 +71,6 @@ ID descriptor_instancevar_interned;
|
|
80
71
|
// This must be named "Init_protobuf_c" because the Ruby module is named
|
81
72
|
// "protobuf_c" -- the VM looks for this symbol in our .so.
|
82
73
|
void Init_protobuf_c() {
|
83
|
-
descriptor_instancevar_interned = rb_intern(kDescriptorInstanceVar);
|
84
74
|
VALUE google = rb_define_module("Google");
|
85
75
|
VALUE protobuf = rb_define_module_under(google, "Protobuf");
|
86
76
|
VALUE internal = rb_define_module_under(protobuf, "Internal");
|
@@ -96,6 +86,13 @@ void Init_protobuf_c() {
|
|
96
86
|
RepeatedField_register(protobuf);
|
97
87
|
Map_register(protobuf);
|
98
88
|
|
89
|
+
rb_define_singleton_method(protobuf, "encode", Google_Protobuf_encode, 1);
|
90
|
+
rb_define_singleton_method(protobuf, "decode", Google_Protobuf_decode, 2);
|
91
|
+
rb_define_singleton_method(protobuf, "encode_json",
|
92
|
+
Google_Protobuf_encode_json, 1);
|
93
|
+
rb_define_singleton_method(protobuf, "decode_json",
|
94
|
+
Google_Protobuf_decode_json, 2);
|
95
|
+
|
99
96
|
rb_define_singleton_method(protobuf, "deep_copy",
|
100
97
|
Google_Protobuf_deep_copy, 1);
|
101
98
|
|
@@ -161,6 +161,8 @@ extern VALUE cOneofBuilderContext;
|
|
161
161
|
extern VALUE cEnumBuilderContext;
|
162
162
|
extern VALUE cBuilder;
|
163
163
|
|
164
|
+
extern const char* kDescriptorInstanceVar;
|
165
|
+
|
164
166
|
// We forward-declare all of the Ruby method implementations here because we
|
165
167
|
// sometimes call the methods directly across .c files, rather than going
|
166
168
|
// through Ruby's method dispatching (e.g. during message parse). It's cleaner
|
@@ -359,20 +361,19 @@ extern VALUE cRepeatedField;
|
|
359
361
|
RepeatedField* ruby_to_RepeatedField(VALUE value);
|
360
362
|
|
361
363
|
VALUE RepeatedField_each(VALUE _self);
|
362
|
-
VALUE RepeatedField_index(
|
364
|
+
VALUE RepeatedField_index(VALUE _self, VALUE _index);
|
363
365
|
void* RepeatedField_index_native(VALUE _self, int index);
|
364
366
|
VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val);
|
365
367
|
void RepeatedField_reserve(RepeatedField* self, int new_size);
|
366
368
|
VALUE RepeatedField_push(VALUE _self, VALUE val);
|
367
369
|
void RepeatedField_push_native(VALUE _self, void* data);
|
368
|
-
VALUE
|
370
|
+
VALUE RepeatedField_pop(VALUE _self);
|
369
371
|
VALUE RepeatedField_insert(int argc, VALUE* argv, VALUE _self);
|
370
372
|
VALUE RepeatedField_replace(VALUE _self, VALUE list);
|
371
373
|
VALUE RepeatedField_clear(VALUE _self);
|
372
374
|
VALUE RepeatedField_length(VALUE _self);
|
373
375
|
VALUE RepeatedField_dup(VALUE _self);
|
374
376
|
VALUE RepeatedField_deep_copy(VALUE _self);
|
375
|
-
VALUE RepeatedField_to_ary(VALUE _self);
|
376
377
|
VALUE RepeatedField_eq(VALUE _self, VALUE _other);
|
377
378
|
VALUE RepeatedField_hash(VALUE _self);
|
378
379
|
VALUE RepeatedField_inspect(VALUE _self);
|
@@ -496,6 +497,11 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb);
|
|
496
497
|
VALUE Message_decode_json(VALUE klass, VALUE data);
|
497
498
|
VALUE Message_encode_json(VALUE klass, VALUE msg_rb);
|
498
499
|
|
500
|
+
VALUE Google_Protobuf_encode(VALUE self, VALUE msg_rb);
|
501
|
+
VALUE Google_Protobuf_decode(VALUE self, VALUE klass, VALUE msg_rb);
|
502
|
+
VALUE Google_Protobuf_encode_json(VALUE self, VALUE msg_rb);
|
503
|
+
VALUE Google_Protobuf_decode_json(VALUE self, VALUE klass, VALUE msg_rb);
|
504
|
+
|
499
505
|
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
|
500
506
|
|
501
507
|
VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
|
@@ -505,10 +511,6 @@ VALUE enum_resolve(VALUE self, VALUE sym);
|
|
505
511
|
const upb_pbdecodermethod *new_fillmsg_decodermethod(
|
506
512
|
Descriptor* descriptor, const void *owner);
|
507
513
|
|
508
|
-
// Maximum depth allowed during encoding, to avoid stack overflows due to
|
509
|
-
// cycles.
|
510
|
-
#define ENCODE_MAX_NESTING 63
|
511
|
-
|
512
514
|
// -----------------------------------------------------------------------------
|
513
515
|
// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
|
514
516
|
// instances.
|
@@ -528,6 +530,4 @@ void check_upb_status(const upb_status* status, const char* msg);
|
|
528
530
|
check_upb_status(&status, msg); \
|
529
531
|
} while (0)
|
530
532
|
|
531
|
-
extern ID descriptor_instancevar_interned;
|
532
|
-
|
533
533
|
#endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
|