Unverified Commit 1568deab authored by Joshua Haberman's avatar Joshua Haberman Committed by GitHub

Revert "Updated upb from defcleanup branch and modified Ruby to use it (#5539)" (#5848)

This reverts commit 37581380.
parent d0f91c86
...@@ -28,8 +28,6 @@ ...@@ -28,8 +28,6 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <ctype.h>
#include <errno.h>
#include "protobuf.h" #include "protobuf.h"
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -48,292 +46,29 @@ static VALUE rb_str_maybe_null(const char* s) { ...@@ -48,292 +46,29 @@ static VALUE rb_str_maybe_null(const char* s) {
return rb_str_new2(s); return rb_str_new2(s);
} }
static void rewrite_enum_default(const upb_symtab* symtab, static upb_def* check_notfrozen(const upb_def* def) {
google_protobuf_FileDescriptorProto* file, if (upb_def_isfrozen(def)) {
google_protobuf_FieldDescriptorProto* field) { rb_raise(rb_eRuntimeError,
/* Look for TYPE_ENUM fields that have a default. */ "Attempt to modify a frozen descriptor. Once descriptors are "
if (google_protobuf_FieldDescriptorProto_type(field) != "added to the descriptor pool, they may not be modified.");
google_protobuf_FieldDescriptorProto_TYPE_ENUM ||
!google_protobuf_FieldDescriptorProto_has_default_value(field) ||
!google_protobuf_FieldDescriptorProto_has_type_name(field)) {
return;
}
upb_strview defaultval =
google_protobuf_FieldDescriptorProto_default_value(field);
upb_strview type_name = google_protobuf_FieldDescriptorProto_type_name(field);
if (defaultval.size == 0 || !isdigit(defaultval.data[0])) {
return;
}
if (type_name.size == 0 || type_name.data[0] != '.') {
return;
}
const char *type_name_str = type_name.data + 1;
char *end;
errno = 0;
long val = strtol(defaultval.data, &end, 10);
if (errno != 0 || *end != 0 || val < INT32_MIN || val > INT32_MAX) {
return;
}
/* Now find the corresponding enum definition. */
const upb_enumdef *e = upb_symtab_lookupenum(symtab, type_name_str);
if (e) {
/* Look in previously loaded files. */
const char *label = upb_enumdef_iton(e, val);
if (!label) {
return;
}
google_protobuf_FieldDescriptorProto_set_default_value(
field, upb_strview_makez(label));
} else {
/* Look in enums defined in this file. */
const google_protobuf_EnumDescriptorProto* matching_enum = NULL;
size_t i, n;
const google_protobuf_EnumDescriptorProto* const* enums =
google_protobuf_FileDescriptorProto_enum_type(file, &n);
for (i = 0; i < n; i++) {
if (upb_strview_eql(google_protobuf_EnumDescriptorProto_name(enums[i]),
upb_strview_makez(type_name_str))) {
matching_enum = enums[i];
break;
}
}
if (!matching_enum) {
return;
}
const google_protobuf_EnumValueDescriptorProto* const* values =
google_protobuf_EnumDescriptorProto_value(matching_enum, &n);
for (i = 0; i < n; i++) {
if (google_protobuf_EnumValueDescriptorProto_number(values[i]) == val) {
google_protobuf_FieldDescriptorProto_set_default_value(
field, google_protobuf_EnumValueDescriptorProto_name(values[i]));
return;
}
}
/* We failed to find an enum default. But we'll just leave the enum
* untouched and let the normal def-building code catch it. */
} }
return (upb_def*)def;
} }
/* Historically we allowed enum defaults to be specified as a number. In static upb_msgdef* check_msg_notfrozen(const upb_msgdef* def) {
* retrospect this was a mistake as descriptors require defaults to be return upb_downcast_msgdef_mutable(check_notfrozen((const upb_def*)def));
* specified as a label. This can make a difference if multiple labels have the
* same number.
*
* Here we do a pass over all enum defaults and rewrite numeric defaults by
* looking up their labels. This is compilcated by the fact that the enum
* definition can live in either the symtab or the file_proto.
* */
static void rewrite_enum_defaults(
const upb_symtab* symtab, google_protobuf_FileDescriptorProto* file_proto) {
size_t i, n;
google_protobuf_DescriptorProto** msgs =
google_protobuf_FileDescriptorProto_mutable_message_type(file_proto, &n);
for (i = 0; i < n; i++) {
size_t j, m;
google_protobuf_FieldDescriptorProto** fields =
google_protobuf_DescriptorProto_mutable_field(msgs[i], &m);
for (j = 0; j < m; j++) {
rewrite_enum_default(symtab, file_proto, fields[j]);
}
}
} }
static bool has_prefix(upb_strview str, upb_strview prefix) { static upb_fielddef* check_field_notfrozen(const upb_fielddef* def) {
return str.size >= prefix.size && return upb_downcast_fielddef_mutable(check_notfrozen((const upb_def*)def));
memcmp(str.data, prefix.data, prefix.size) == 0;
} }
static void remove_package(upb_strview *name, upb_strview package) { static upb_oneofdef* check_oneof_notfrozen(const upb_oneofdef* def) {
size_t prefix_len = package.size + 1; return (upb_oneofdef*)check_notfrozen((const upb_def*)def);
if (!has_prefix(*name, package) || prefix_len >= name->size || }
name->data[package.size] != '.') {
rb_raise(cTypeError,
"Bad package name, wasn't prefix: " UPB_STRVIEW_FORMAT
", " UPB_STRVIEW_FORMAT,
UPB_STRVIEW_ARGS(*name), UPB_STRVIEW_ARGS(package));
}
name->data += prefix_len;
name->size -= prefix_len;
}
static void remove_path(upb_strview *name) {
const char* last = strrchr(name->data, '.');
if (last) {
size_t remove = last - name->data + 1;
name->data += remove;
name->size -= remove;
}
}
static void rewrite_nesting(VALUE msg_ent, google_protobuf_DescriptorProto* msg,
google_protobuf_DescriptorProto* const* msgs,
google_protobuf_EnumDescriptorProto* const* enums,
upb_arena *arena) {
VALUE submsgs = rb_hash_aref(msg_ent, ID2SYM(rb_intern("msgs")));
VALUE enum_pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("enums")));
Check_Type(submsgs, T_ARRAY);
Check_Type(enum_pos, T_ARRAY);
int submsg_count = RARRAY_LEN(submsgs);
int enum_count = RARRAY_LEN(enum_pos);
google_protobuf_DescriptorProto** msg_msgs =
google_protobuf_DescriptorProto_resize_nested_type(msg, submsg_count,
arena);
google_protobuf_EnumDescriptorProto** msg_enums =
google_protobuf_DescriptorProto_resize_enum_type(msg, enum_count, arena);
for (int i = 0; i < submsg_count; i++) {
VALUE submsg_ent = RARRAY_PTR(submsgs)[i];
VALUE pos = rb_hash_aref(submsg_ent, ID2SYM(rb_intern("pos")));
msg_msgs[i] = msgs[NUM2INT(pos)];
upb_strview name = google_protobuf_DescriptorProto_name(msg_msgs[i]);
remove_path(&name);
google_protobuf_DescriptorProto_set_name(msg_msgs[i], name);
rewrite_nesting(submsg_ent, msg_msgs[i], msgs, enums, arena);
}
for (int i = 0; i < enum_count; i++) {
VALUE pos = RARRAY_PTR(enum_pos)[i];
msg_enums[i] = enums[NUM2INT(pos)];
}
}
/* We have to do some relatively complicated logic here for backward
* compatibility.
*
* In descriptor.proto, messages are nested inside other messages if that is
* what the original .proto file looks like. For example, suppose we have this
* foo.proto:
*
* package foo;
* message Bar {
* message Baz {}
* }
*
* The descriptor for this must look like this:
*
* file {
* name: "test.proto"
* package: "foo"
* message_type {
* name: "Bar"
* nested_type {
* name: "Baz"
* }
* }
* }
*
* However, the Ruby generated code has always generated messages in a flat,
* non-nested way:
*
* Google::Protobuf::DescriptorPool.generated_pool.build do
* add_message "foo.Bar" do
* end
* add_message "foo.Bar.Baz" do
* end
* end
*
* Here we need to do a translation where we turn this generated code into the
* above descriptor. We need to infer that "foo" is the package name, and not
* a message itself.
*
* We delegate to Ruby to compute the transformation, for more concice and
* readable code than we can do in C */
static void rewrite_names(VALUE _file_builder,
google_protobuf_FileDescriptorProto* file_proto) {
FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
upb_arena *arena = file_builder->arena;
// Build params (package, msg_names, enum_names).
VALUE package = Qnil;
VALUE msg_names = rb_ary_new();
VALUE enum_names = rb_ary_new();
size_t msg_count, enum_count, i;
if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
upb_strview package_str =
google_protobuf_FileDescriptorProto_package(file_proto);
package = rb_str_new(package_str.data, package_str.size);
}
google_protobuf_DescriptorProto** msgs =
google_protobuf_FileDescriptorProto_mutable_message_type(file_proto,
&msg_count);
for (i = 0; i < msg_count; i++) {
upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
rb_ary_push(msg_names, rb_str_new(name.data, name.size));
}
google_protobuf_EnumDescriptorProto** enums =
google_protobuf_FileDescriptorProto_mutable_enum_type(file_proto,
&enum_count);
for (i = 0; i < enum_count; i++) {
upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
rb_ary_push(enum_names, rb_str_new(name.data, name.size));
}
// Call Ruby code to calculate package name and nesting.
VALUE args[3] = { package, msg_names, enum_names };
VALUE internal = rb_eval_string("Google::Protobuf::Internal");
VALUE ret = rb_funcallv(internal, rb_intern("fixup_descriptor"), 3, args);
VALUE new_package = rb_ary_entry(ret, 0);
VALUE nesting = rb_ary_entry(ret, 1);
if (package == Qnil && new_package != Qnil) {
// We inferred a package name; set this package name on the file and remove
// the prefix from all msg/enum names.
upb_strview new_package_str =
FileBuilderContext_strdup(_file_builder, new_package);
google_protobuf_FileDescriptorProto_set_package(file_proto,
new_package_str);
for (i = 0; i < msg_count; i++) {
upb_strview name = google_protobuf_DescriptorProto_name(msgs[i]);
remove_package(&name, new_package_str);
google_protobuf_DescriptorProto_set_name(msgs[i], name);
}
for (i = 0; i < enum_count; i++) {
upb_strview name = google_protobuf_EnumDescriptorProto_name(enums[i]);
remove_package(&name, new_package_str);
google_protobuf_EnumDescriptorProto_set_name(enums[i], name);
}
}
VALUE msg_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("msgs")));
VALUE enum_ents = rb_hash_aref(nesting, ID2SYM(rb_intern("enums")));
Check_Type(msg_ents, T_ARRAY);
Check_Type(enum_ents, T_ARRAY);
for (i = 0; i < RARRAY_LEN(msg_ents); i++) {
VALUE msg_ent = rb_ary_entry(msg_ents, i);
VALUE pos = rb_hash_aref(msg_ent, ID2SYM(rb_intern("pos")));
msgs[i] = msgs[NUM2INT(pos)];
rewrite_nesting(msg_ent, msgs[i], msgs, enums, arena);
}
for (i = 0; i < RARRAY_LEN(enum_ents); i++) {
VALUE enum_pos = rb_ary_entry(enum_ents, i);
enums[i] = enums[NUM2INT(enum_pos)];
}
google_protobuf_FileDescriptorProto_resize_message_type( static upb_enumdef* check_enum_notfrozen(const upb_enumdef* def) {
file_proto, RARRAY_LEN(msg_ents), arena); return (upb_enumdef*)check_notfrozen((const upb_def*)def);
google_protobuf_FileDescriptorProto_resize_enum_type(
file_proto, RARRAY_LEN(enum_ents), arena);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -362,21 +97,11 @@ VALUE generated_pool; ...@@ -362,21 +97,11 @@ VALUE generated_pool;
DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool"); DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
void DescriptorPool_mark(void* _self) { void DescriptorPool_mark(void* _self) {
DescriptorPool* self = _self;
rb_gc_mark(self->def_to_descriptor);
} }
void DescriptorPool_free(void* _self) { void DescriptorPool_free(void* _self) {
DescriptorPool* self = _self; DescriptorPool* self = _self;
upb_symtab_free(self->symtab); upb_symtab_free(self->symtab);
upb_handlercache_free(self->fill_handler_cache);
upb_handlercache_free(self->pb_serialize_handler_cache);
upb_handlercache_free(self->json_serialize_handler_cache);
upb_handlercache_free(self->json_serialize_handler_preserve_cache);
upb_pbcodecache_free(self->fill_method_cache);
upb_json_codecache_free(self->json_fill_method_cache);
xfree(self); xfree(self);
} }
...@@ -388,26 +113,15 @@ void DescriptorPool_free(void* _self) { ...@@ -388,26 +113,15 @@ void DescriptorPool_free(void* _self) {
*/ */
VALUE DescriptorPool_alloc(VALUE klass) { VALUE DescriptorPool_alloc(VALUE klass) {
DescriptorPool* self = ALLOC(DescriptorPool); DescriptorPool* self = ALLOC(DescriptorPool);
self->def_to_descriptor = rb_hash_new();
VALUE ret = TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
self->symtab = upb_symtab_new(); self->symtab = upb_symtab_new();
self->fill_handler_cache = return TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
upb_handlercache_new(add_handlers_for_message, (void*)ret);
self->pb_serialize_handler_cache = upb_pb_encoder_newcache();
self->json_serialize_handler_cache = upb_json_printer_newcache(false);
self->json_serialize_handler_preserve_cache =
upb_json_printer_newcache(true);
self->fill_method_cache = upb_pbcodecache_new(self->fill_handler_cache);
self->json_fill_method_cache = upb_json_codecache_new();
return ret;
} }
void DescriptorPool_register(VALUE module) { void DescriptorPool_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "DescriptorPool", rb_cObject); module, "DescriptorPool", rb_cObject);
rb_define_alloc_func(klass, DescriptorPool_alloc); rb_define_alloc_func(klass, DescriptorPool_alloc);
rb_define_method(klass, "add", DescriptorPool_add, 1);
rb_define_method(klass, "build", DescriptorPool_build, -1); rb_define_method(klass, "build", DescriptorPool_build, -1);
rb_define_method(klass, "lookup", DescriptorPool_lookup, 1); rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
rb_define_singleton_method(klass, "generated_pool", rb_define_singleton_method(klass, "generated_pool",
...@@ -419,6 +133,44 @@ void DescriptorPool_register(VALUE module) { ...@@ -419,6 +133,44 @@ void DescriptorPool_register(VALUE module) {
generated_pool = rb_class_new_instance(0, NULL, klass); generated_pool = rb_class_new_instance(0, NULL, klass);
} }
static void add_descriptor_to_pool(DescriptorPool* self,
Descriptor* descriptor) {
CHECK_UPB(
upb_symtab_add(self->symtab, (upb_def**)&descriptor->msgdef, 1,
NULL, &status),
"Adding Descriptor to DescriptorPool failed");
}
static void add_enumdesc_to_pool(DescriptorPool* self,
EnumDescriptor* enumdesc) {
CHECK_UPB(
upb_symtab_add(self->symtab, (upb_def**)&enumdesc->enumdef, 1,
NULL, &status),
"Adding EnumDescriptor to DescriptorPool failed");
}
/*
* call-seq:
* DescriptorPool.add(descriptor)
*
* Adds the given Descriptor or EnumDescriptor to this pool. All references to
* other types in a Descriptor's fields must be resolvable within this pool or
* an exception will be raised.
*/
VALUE DescriptorPool_add(VALUE _self, VALUE def) {
DEFINE_SELF(DescriptorPool, self, _self);
VALUE def_klass = rb_obj_class(def);
if (def_klass == cDescriptor) {
add_descriptor_to_pool(self, ruby_to_Descriptor(def));
} else if (def_klass == cEnumDescriptor) {
add_enumdesc_to_pool(self, ruby_to_EnumDescriptor(def));
} else {
rb_raise(rb_eArgError,
"Second argument must be a Descriptor or EnumDescriptor.");
}
return Qnil;
}
/* /*
* call-seq: * call-seq:
* DescriptorPool.build(&block) * DescriptorPool.build(&block)
...@@ -430,10 +182,10 @@ void DescriptorPool_register(VALUE module) { ...@@ -430,10 +182,10 @@ void DescriptorPool_register(VALUE module) {
* idiomatic way to define new message and enum types. * idiomatic way to define new message and enum types.
*/ */
VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) { VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
VALUE ctx = rb_class_new_instance(1, &_self, cBuilder); VALUE ctx = rb_class_new_instance(0, NULL, cBuilder);
VALUE block = rb_block_proc(); VALUE block = rb_block_proc();
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
Builder_build(ctx); rb_funcall(ctx, rb_intern("finalize_to_pool"), 1, _self);
return Qnil; return Qnil;
} }
...@@ -447,18 +199,11 @@ VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) { ...@@ -447,18 +199,11 @@ VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
VALUE DescriptorPool_lookup(VALUE _self, VALUE name) { VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
DEFINE_SELF(DescriptorPool, self, _self); DEFINE_SELF(DescriptorPool, self, _self);
const char* name_str = get_str(name); const char* name_str = get_str(name);
const upb_def* def = upb_symtab_lookup(self->symtab, name_str);
const upb_msgdef* msgdef = upb_symtab_lookupmsg(self->symtab, name_str); if (!def) {
if (msgdef) {
return get_msgdef_obj(_self, msgdef);
}
const upb_enumdef* enumdef = upb_symtab_lookupenum(self->symtab, name_str);
if (enumdef) {
return get_enumdef_obj(_self, enumdef);
}
return Qnil; return Qnil;
}
return get_def_obj(def);
} }
/* /*
...@@ -483,14 +228,36 @@ DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor"); ...@@ -483,14 +228,36 @@ DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
void Descriptor_mark(void* _self) { void Descriptor_mark(void* _self) {
Descriptor* self = _self; Descriptor* self = _self;
rb_gc_mark(self->klass); rb_gc_mark(self->klass);
rb_gc_mark(self->descriptor_pool);
} }
void Descriptor_free(void* _self) { void Descriptor_free(void* _self) {
Descriptor* self = _self; Descriptor* self = _self;
upb_msgdef_unref(self->msgdef, &self->msgdef);
if (self->layout) { if (self->layout) {
free_layout(self->layout); free_layout(self->layout);
} }
if (self->fill_handlers) {
upb_handlers_unref(self->fill_handlers, &self->fill_handlers);
}
if (self->fill_method) {
upb_pbdecodermethod_unref(self->fill_method, &self->fill_method);
}
if (self->json_fill_method) {
upb_json_parsermethod_unref(self->json_fill_method,
&self->json_fill_method);
}
if (self->pb_serialize_handlers) {
upb_handlers_unref(self->pb_serialize_handlers,
&self->pb_serialize_handlers);
}
if (self->json_serialize_handlers) {
upb_handlers_unref(self->json_serialize_handlers,
&self->json_serialize_handlers);
}
if (self->json_serialize_handlers_preserve) {
upb_handlers_unref(self->json_serialize_handlers_preserve,
&self->json_serialize_handlers_preserve);
}
xfree(self); xfree(self);
} }
...@@ -506,10 +273,15 @@ void Descriptor_free(void* _self) { ...@@ -506,10 +273,15 @@ void Descriptor_free(void* _self) {
VALUE Descriptor_alloc(VALUE klass) { VALUE Descriptor_alloc(VALUE klass) {
Descriptor* self = ALLOC(Descriptor); Descriptor* self = ALLOC(Descriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self); VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
self->msgdef = NULL; self->msgdef = upb_msgdef_new(&self->msgdef);
self->klass = Qnil; self->klass = Qnil;
self->descriptor_pool = Qnil;
self->layout = NULL; self->layout = NULL;
self->fill_handlers = NULL;
self->fill_method = NULL;
self->json_fill_method = NULL;
self->pb_serialize_handlers = NULL;
self->json_serialize_handlers = NULL;
self->json_serialize_handlers_preserve = NULL;
return ret; return ret;
} }
...@@ -517,13 +289,16 @@ void Descriptor_register(VALUE module) { ...@@ -517,13 +289,16 @@ void Descriptor_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "Descriptor", rb_cObject); module, "Descriptor", rb_cObject);
rb_define_alloc_func(klass, Descriptor_alloc); rb_define_alloc_func(klass, Descriptor_alloc);
rb_define_method(klass, "initialize", Descriptor_initialize, 3); rb_define_method(klass, "initialize", Descriptor_initialize, 1);
rb_define_method(klass, "each", Descriptor_each, 0); rb_define_method(klass, "each", Descriptor_each, 0);
rb_define_method(klass, "lookup", Descriptor_lookup, 1); rb_define_method(klass, "lookup", Descriptor_lookup, 1);
rb_define_method(klass, "add_field", Descriptor_add_field, 1);
rb_define_method(klass, "add_oneof", Descriptor_add_oneof, 1);
rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0); rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1); rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
rb_define_method(klass, "msgclass", Descriptor_msgclass, 0); rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
rb_define_method(klass, "name", Descriptor_name, 0); rb_define_method(klass, "name", Descriptor_name, 0);
rb_define_method(klass, "name=", Descriptor_name_set, 1);
rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0); rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
rb_include_module(klass, rb_mEnumerable); rb_include_module(klass, rb_mEnumerable);
rb_gc_register_address(&cDescriptor); rb_gc_register_address(&cDescriptor);
...@@ -532,21 +307,19 @@ void Descriptor_register(VALUE module) { ...@@ -532,21 +307,19 @@ void Descriptor_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* Descriptor.new(c_only_cookie, ptr) => Descriptor * Descriptor.new(file_descriptor)
* *
* Creates a descriptor wrapper object. May only be called from C. * Initializes a new descriptor and assigns a file descriptor to it.
*/ */
VALUE Descriptor_initialize(VALUE _self, VALUE cookie, VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
VALUE descriptor_pool, VALUE ptr) {
DEFINE_SELF(Descriptor, self, _self); DEFINE_SELF(Descriptor, self, _self);
if (cookie != c_only_cookie) { FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb);
rb_raise(rb_eRuntimeError,
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool; CHECK_UPB(
self->msgdef = (const upb_msgdef*)NUM2ULL(ptr); upb_filedef_addmsg(file_descriptor->filedef, self->msgdef, NULL, &status),
"Failed to associate message to file descriptor.");
add_def_obj(file_descriptor->filedef, file_descriptor_rb);
return Qnil; return Qnil;
} }
...@@ -559,7 +332,7 @@ VALUE Descriptor_initialize(VALUE _self, VALUE cookie, ...@@ -559,7 +332,7 @@ VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
*/ */
VALUE Descriptor_file_descriptor(VALUE _self) { VALUE Descriptor_file_descriptor(VALUE _self) {
DEFINE_SELF(Descriptor, self, _self); DEFINE_SELF(Descriptor, self, _self);
return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef)); return get_def_obj(upb_def_file(self->msgdef));
} }
/* /*
...@@ -574,6 +347,23 @@ VALUE Descriptor_name(VALUE _self) { ...@@ -574,6 +347,23 @@ VALUE Descriptor_name(VALUE _self) {
return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef)); return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
} }
/*
* call-seq:
* Descriptor.name = name
*
* Assigns a name to this message type. The descriptor must not have been added
* to a pool yet.
*/
VALUE Descriptor_name_set(VALUE _self, VALUE str) {
DEFINE_SELF(Descriptor, self, _self);
upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
const char* name = get_str(str);
CHECK_UPB(
upb_msgdef_setfullname(mut_def, name, &status),
"Error setting Descriptor name");
return Qnil;
}
/* /*
* call-seq: * call-seq:
* Descriptor.each(&block) * Descriptor.each(&block)
...@@ -588,7 +378,7 @@ VALUE Descriptor_each(VALUE _self) { ...@@ -588,7 +378,7 @@ VALUE Descriptor_each(VALUE _self) {
!upb_msg_field_done(&it); !upb_msg_field_done(&it);
upb_msg_field_next(&it)) { upb_msg_field_next(&it)) {
const upb_fielddef* field = upb_msg_iter_field(&it); const upb_fielddef* field = upb_msg_iter_field(&it);
VALUE obj = get_fielddef_obj(self->descriptor_pool, field); VALUE obj = get_def_obj(field);
rb_yield(obj); rb_yield(obj);
} }
return Qnil; return Qnil;
...@@ -608,7 +398,51 @@ VALUE Descriptor_lookup(VALUE _self, VALUE name) { ...@@ -608,7 +398,51 @@ VALUE Descriptor_lookup(VALUE _self, VALUE name) {
if (field == NULL) { if (field == NULL) {
return Qnil; return Qnil;
} }
return get_fielddef_obj(self->descriptor_pool, field); return get_def_obj(field);
}
/*
* call-seq:
* Descriptor.add_field(field) => nil
*
* Adds the given FieldDescriptor to this message type. This descriptor must not
* have been added to a pool yet. Raises an exception if a field with the same
* name or number already exists. Sub-type references (e.g. for fields of type
* message) are not resolved at this point.
*/
VALUE Descriptor_add_field(VALUE _self, VALUE obj) {
DEFINE_SELF(Descriptor, self, _self);
upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
CHECK_UPB(
upb_msgdef_addfield(mut_def, mut_field_def, NULL, &status),
"Adding field to Descriptor failed");
add_def_obj(def->fielddef, obj);
return Qnil;
}
/*
* call-seq:
* Descriptor.add_oneof(oneof) => nil
*
* Adds the given OneofDescriptor to this message type. This descriptor must not
* have been added to a pool yet. Raises an exception if a oneof with the same
* name already exists, or if any of the oneof's fields' names or numbers
* conflict with an existing field in this message type. All fields in the oneof
* are added to the message descriptor. Sub-type references (e.g. for fields of
* type message) are not resolved at this point.
*/
VALUE Descriptor_add_oneof(VALUE _self, VALUE obj) {
DEFINE_SELF(Descriptor, self, _self);
upb_msgdef* mut_def = check_msg_notfrozen(self->msgdef);
OneofDescriptor* def = ruby_to_OneofDescriptor(obj);
upb_oneofdef* mut_oneof_def = check_oneof_notfrozen(def->oneofdef);
CHECK_UPB(
upb_msgdef_addoneof(mut_def, mut_oneof_def, NULL, &status),
"Adding oneof to Descriptor failed");
add_def_obj(def->oneofdef, obj);
return Qnil;
} }
/* /*
...@@ -626,7 +460,7 @@ VALUE Descriptor_each_oneof(VALUE _self) { ...@@ -626,7 +460,7 @@ VALUE Descriptor_each_oneof(VALUE _self) {
!upb_msg_oneof_done(&it); !upb_msg_oneof_done(&it);
upb_msg_oneof_next(&it)) { upb_msg_oneof_next(&it)) {
const upb_oneofdef* oneof = upb_msg_iter_oneof(&it); const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof); VALUE obj = get_def_obj(oneof);
rb_yield(obj); rb_yield(obj);
} }
return Qnil; return Qnil;
...@@ -646,19 +480,24 @@ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) { ...@@ -646,19 +480,24 @@ VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
if (oneof == NULL) { if (oneof == NULL) {
return Qnil; return Qnil;
} }
return get_oneofdef_obj(self->descriptor_pool, oneof); return get_def_obj(oneof);
} }
/* /*
* call-seq: * call-seq:
* Descriptor.msgclass => message_klass * Descriptor.msgclass => message_klass
* *
* Returns the Ruby class created for this message type. * Returns the Ruby class created for this message type. Valid only once the
* message type has been added to a pool.
*/ */
VALUE Descriptor_msgclass(VALUE _self) { VALUE Descriptor_msgclass(VALUE _self) {
DEFINE_SELF(Descriptor, self, _self); DEFINE_SELF(Descriptor, self, _self);
if (!upb_def_isfrozen((const upb_def*)self->msgdef)) {
rb_raise(rb_eRuntimeError,
"Cannot fetch message class from a Descriptor not yet in a pool.");
}
if (self->klass == Qnil) { if (self->klass == Qnil) {
self->klass = build_class_from_descriptor(_self); self->klass = build_class_from_descriptor(self);
} }
return self->klass; return self->klass;
} }
...@@ -670,20 +509,12 @@ VALUE Descriptor_msgclass(VALUE _self) { ...@@ -670,20 +509,12 @@ VALUE Descriptor_msgclass(VALUE _self) {
DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor"); DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
void FileDescriptor_mark(void* _self) { void FileDescriptor_mark(void* _self) {
FileDescriptor* self = _self;
rb_gc_mark(self->descriptor_pool);
} }
void FileDescriptor_free(void* _self) { void FileDescriptor_free(void* _self) {
xfree(_self); FileDescriptor* self = _self;
} upb_filedef_unref(self->filedef, &self->filedef);
xfree(self);
VALUE FileDescriptor_alloc(VALUE klass) {
FileDescriptor* self = ALLOC(FileDescriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
self->descriptor_pool = Qnil;
self->filedef = NULL;
return ret;
} }
/* /*
...@@ -693,32 +524,64 @@ VALUE FileDescriptor_alloc(VALUE klass) { ...@@ -693,32 +524,64 @@ VALUE FileDescriptor_alloc(VALUE klass) {
* Returns a new file descriptor. The syntax must be set before it's passed * Returns a new file descriptor. The syntax must be set before it's passed
* to a builder. * to a builder.
*/ */
VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie, VALUE FileDescriptor_alloc(VALUE klass) {
VALUE descriptor_pool, VALUE ptr) { FileDescriptor* self = ALLOC(FileDescriptor);
DEFINE_SELF(FileDescriptor, self, _self); VALUE ret = TypedData_Wrap_Struct(klass, &_FileDescriptor_type, self);
upb_filedef* filedef = upb_filedef_new(&self->filedef);
if (cookie != c_only_cookie) { self->filedef = filedef;
rb_raise(rb_eRuntimeError, return ret;
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool;
self->filedef = (const upb_filedef*)NUM2ULL(ptr);
return Qnil;
} }
void FileDescriptor_register(VALUE module) { void FileDescriptor_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "FileDescriptor", rb_cObject); module, "FileDescriptor", rb_cObject);
rb_define_alloc_func(klass, FileDescriptor_alloc); rb_define_alloc_func(klass, FileDescriptor_alloc);
rb_define_method(klass, "initialize", FileDescriptor_initialize, 3); rb_define_method(klass, "initialize", FileDescriptor_initialize, -1);
rb_define_method(klass, "name", FileDescriptor_name, 0); rb_define_method(klass, "name", FileDescriptor_name, 0);
rb_define_method(klass, "syntax", FileDescriptor_syntax, 0); rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
rb_define_method(klass, "syntax=", FileDescriptor_syntax_set, 1);
rb_gc_register_address(&cFileDescriptor); rb_gc_register_address(&cFileDescriptor);
cFileDescriptor = klass; cFileDescriptor = klass;
} }
/*
* call-seq:
* FileDescriptor.new(name, options = nil) => file
*
* Initializes a new file descriptor with the given file name.
* Also accepts an optional "options" hash, specifying other optional
* metadata about the file. The options hash currently accepts the following
* * "syntax": :proto2 or :proto3 (default: :proto3)
*/
VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self) {
DEFINE_SELF(FileDescriptor, self, _self);
VALUE name_rb;
VALUE options = Qnil;
rb_scan_args(argc, argv, "11", &name_rb, &options);
if (name_rb != Qnil) {
Check_Type(name_rb, T_STRING);
const char* name = get_str(name_rb);
CHECK_UPB(upb_filedef_setname(self->filedef, name, &status),
"Error setting file name");
}
// Default syntax is proto3.
VALUE syntax = ID2SYM(rb_intern("proto3"));
if (options != Qnil) {
Check_Type(options, T_HASH);
if (rb_funcall(options, rb_intern("key?"), 1,
ID2SYM(rb_intern("syntax"))) == Qtrue) {
syntax = rb_hash_lookup(options, ID2SYM(rb_intern("syntax")));
}
}
FileDescriptor_syntax_set(_self, syntax);
return Qnil;
}
/* /*
* call-seq: * call-seq:
* FileDescriptor.name => name * FileDescriptor.name => name
...@@ -750,6 +613,31 @@ VALUE FileDescriptor_syntax(VALUE _self) { ...@@ -750,6 +613,31 @@ VALUE FileDescriptor_syntax(VALUE _self) {
} }
} }
/*
* call-seq:
* FileDescriptor.syntax = version
*
* Sets this file descriptor's syntax, can be :proto3 or :proto2.
*/
VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax_rb) {
DEFINE_SELF(FileDescriptor, self, _self);
Check_Type(syntax_rb, T_SYMBOL);
upb_syntax_t syntax;
if (SYM2ID(syntax_rb) == rb_intern("proto3")) {
syntax = UPB_SYNTAX_PROTO3;
} else if (SYM2ID(syntax_rb) == rb_intern("proto2")) {
syntax = UPB_SYNTAX_PROTO2;
} else {
rb_raise(rb_eArgError, "Expected :proto3 or :proto3, received '%s'",
rb_id2name(SYM2ID(syntax_rb)));
}
CHECK_UPB(upb_filedef_setsyntax(self->filedef, syntax, &status),
"Error setting file syntax for proto");
return Qnil;
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// FieldDescriptor. // FieldDescriptor.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -757,12 +645,12 @@ VALUE FileDescriptor_syntax(VALUE _self) { ...@@ -757,12 +645,12 @@ VALUE FileDescriptor_syntax(VALUE _self) {
DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor"); DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
void FieldDescriptor_mark(void* _self) { void FieldDescriptor_mark(void* _self) {
FieldDescriptor* self = _self;
rb_gc_mark(self->descriptor_pool);
} }
void FieldDescriptor_free(void* _self) { void FieldDescriptor_free(void* _self) {
xfree(_self); FieldDescriptor* self = _self;
upb_fielddef_unref(self->fielddef, &self->fielddef);
xfree(self);
} }
/* /*
...@@ -775,7 +663,9 @@ void FieldDescriptor_free(void* _self) { ...@@ -775,7 +663,9 @@ void FieldDescriptor_free(void* _self) {
VALUE FieldDescriptor_alloc(VALUE klass) { VALUE FieldDescriptor_alloc(VALUE klass) {
FieldDescriptor* self = ALLOC(FieldDescriptor); FieldDescriptor* self = ALLOC(FieldDescriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self); VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
self->fielddef = NULL; upb_fielddef* fielddef = upb_fielddef_new(&self->fielddef);
upb_fielddef_setpacked(fielddef, false);
self->fielddef = fielddef;
return ret; return ret;
} }
...@@ -783,13 +673,18 @@ void FieldDescriptor_register(VALUE module) { ...@@ -783,13 +673,18 @@ void FieldDescriptor_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "FieldDescriptor", rb_cObject); module, "FieldDescriptor", rb_cObject);
rb_define_alloc_func(klass, FieldDescriptor_alloc); rb_define_alloc_func(klass, FieldDescriptor_alloc);
rb_define_method(klass, "initialize", FieldDescriptor_initialize, 3);
rb_define_method(klass, "name", FieldDescriptor_name, 0); rb_define_method(klass, "name", FieldDescriptor_name, 0);
rb_define_method(klass, "name=", FieldDescriptor_name_set, 1);
rb_define_method(klass, "type", FieldDescriptor_type, 0); rb_define_method(klass, "type", FieldDescriptor_type, 0);
rb_define_method(klass, "type=", FieldDescriptor_type_set, 1);
rb_define_method(klass, "default", FieldDescriptor_default, 0); rb_define_method(klass, "default", FieldDescriptor_default, 0);
rb_define_method(klass, "default=", FieldDescriptor_default_set, 1);
rb_define_method(klass, "label", FieldDescriptor_label, 0); rb_define_method(klass, "label", FieldDescriptor_label, 0);
rb_define_method(klass, "label=", FieldDescriptor_label_set, 1);
rb_define_method(klass, "number", FieldDescriptor_number, 0); rb_define_method(klass, "number", FieldDescriptor_number, 0);
rb_define_method(klass, "number=", FieldDescriptor_number_set, 1);
rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0); rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
rb_define_method(klass, "submsg_name=", FieldDescriptor_submsg_name_set, 1);
rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0); rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
rb_define_method(klass, "has?", FieldDescriptor_has, 1); rb_define_method(klass, "has?", FieldDescriptor_has, 1);
rb_define_method(klass, "clear", FieldDescriptor_clear, 1); rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
...@@ -801,34 +696,29 @@ void FieldDescriptor_register(VALUE module) { ...@@ -801,34 +696,29 @@ void FieldDescriptor_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor * FieldDescriptor.name => name
* *
* Creates a descriptor wrapper object. May only be called from C. * Returns the name of this field.
*/ */
VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie, VALUE FieldDescriptor_name(VALUE _self) {
VALUE descriptor_pool, VALUE ptr) {
DEFINE_SELF(FieldDescriptor, self, _self); DEFINE_SELF(FieldDescriptor, self, _self);
return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
if (cookie != c_only_cookie) {
rb_raise(rb_eRuntimeError,
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool;
self->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
return Qnil;
} }
/* /*
* call-seq: * call-seq:
* FieldDescriptor.name => name * FieldDescriptor.name = name
* *
* Returns the name of this field. * Sets the name of this field. Cannot be called once the containing message
* type, if any, is added to a pool.
*/ */
VALUE FieldDescriptor_name(VALUE _self) { VALUE FieldDescriptor_name_set(VALUE _self, VALUE str) {
DEFINE_SELF(FieldDescriptor, self, _self); DEFINE_SELF(FieldDescriptor, self, _self);
return rb_str_maybe_null(upb_fielddef_name(self->fielddef)); upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
const char* name = get_str(str);
CHECK_UPB(upb_fielddef_setname(mut_def, name, &status),
"Error setting FieldDescriptor name");
return Qnil;
} }
upb_fieldtype_t ruby_to_fieldtype(VALUE type) { upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
...@@ -941,29 +831,6 @@ VALUE descriptortype_to_ruby(upb_descriptortype_t type) { ...@@ -941,29 +831,6 @@ VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
return Qnil; return Qnil;
} }
VALUE ruby_to_label(VALUE label) {
upb_label_t upb_label;
bool converted = false;
#define CONVERT(upb, ruby) \
if (SYM2ID(label) == rb_intern( # ruby )) { \
upb_label = UPB_LABEL_ ## upb; \
converted = true; \
}
CONVERT(OPTIONAL, optional);
CONVERT(REQUIRED, required);
CONVERT(REPEATED, repeated);
#undef CONVERT
if (!converted) {
rb_raise(rb_eArgError, "Unknown field label.");
}
return upb_label;
}
/* /*
* call-seq: * call-seq:
* FieldDescriptor.type => type * FieldDescriptor.type => type
...@@ -976,9 +843,26 @@ VALUE ruby_to_label(VALUE label) { ...@@ -976,9 +843,26 @@ VALUE ruby_to_label(VALUE label) {
*/ */
VALUE FieldDescriptor_type(VALUE _self) { VALUE FieldDescriptor_type(VALUE _self) {
DEFINE_SELF(FieldDescriptor, self, _self); DEFINE_SELF(FieldDescriptor, self, _self);
if (!upb_fielddef_typeisset(self->fielddef)) {
return Qnil;
}
return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef)); return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
} }
/*
* call-seq:
* FieldDescriptor.type = type
*
* Sets this field's type. Cannot be called if field is part of a message type
* already in a pool.
*/
VALUE FieldDescriptor_type_set(VALUE _self, VALUE type) {
DEFINE_SELF(FieldDescriptor, self, _self);
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
upb_fielddef_setdescriptortype(mut_def, ruby_to_descriptortype(type));
return Qnil;
}
/* /*
* call-seq: * call-seq:
* FieldDescriptor.default => default * FieldDescriptor.default => default
...@@ -990,6 +874,60 @@ VALUE FieldDescriptor_default(VALUE _self) { ...@@ -990,6 +874,60 @@ VALUE FieldDescriptor_default(VALUE _self) {
return layout_get_default(self->fielddef); return layout_get_default(self->fielddef);
} }
/*
* call-seq:
* FieldDescriptor.default = default
*
* Sets this field's default value. Raises an exception when calling with
* proto syntax 3.
*/
VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value) {
DEFINE_SELF(FieldDescriptor, self, _self);
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
switch (upb_fielddef_type(mut_def)) {
case UPB_TYPE_FLOAT:
upb_fielddef_setdefaultfloat(mut_def, NUM2DBL(default_value));
break;
case UPB_TYPE_DOUBLE:
upb_fielddef_setdefaultdouble(mut_def, NUM2DBL(default_value));
break;
case UPB_TYPE_BOOL:
if (!RB_TYPE_P(default_value, T_TRUE) &&
!RB_TYPE_P(default_value, T_FALSE) &&
!RB_TYPE_P(default_value, T_NIL)) {
rb_raise(cTypeError, "Expected boolean for default value.");
}
upb_fielddef_setdefaultbool(mut_def, RTEST(default_value));
break;
case UPB_TYPE_ENUM:
case UPB_TYPE_INT32:
upb_fielddef_setdefaultint32(mut_def, NUM2INT(default_value));
break;
case UPB_TYPE_INT64:
upb_fielddef_setdefaultint64(mut_def, NUM2INT(default_value));
break;
case UPB_TYPE_UINT32:
upb_fielddef_setdefaultuint32(mut_def, NUM2UINT(default_value));
break;
case UPB_TYPE_UINT64:
upb_fielddef_setdefaultuint64(mut_def, NUM2UINT(default_value));
break;
case UPB_TYPE_STRING:
case UPB_TYPE_BYTES:
CHECK_UPB(upb_fielddef_setdefaultcstr(mut_def, StringValuePtr(default_value),
&status),
"Error setting default string");
break;
default:
rb_raise(rb_eArgError, "Defaults not supported on field %s.%s",
upb_fielddef_fullname(mut_def), upb_fielddef_name(mut_def));
}
return Qnil;
}
/* /*
* call-seq: * call-seq:
* FieldDescriptor.label => label * FieldDescriptor.label => label
...@@ -1015,6 +953,44 @@ VALUE FieldDescriptor_label(VALUE _self) { ...@@ -1015,6 +953,44 @@ VALUE FieldDescriptor_label(VALUE _self) {
return Qnil; return Qnil;
} }
/*
* call-seq:
* FieldDescriptor.label = label
*
* Sets the label on this field. Cannot be called if field is part of a message
* type already in a pool.
*/
VALUE FieldDescriptor_label_set(VALUE _self, VALUE label) {
DEFINE_SELF(FieldDescriptor, self, _self);
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
upb_label_t upb_label = -1;
bool converted = false;
if (TYPE(label) != T_SYMBOL) {
rb_raise(rb_eArgError, "Expected symbol for field label.");
}
#define CONVERT(upb, ruby) \
if (SYM2ID(label) == rb_intern( # ruby )) { \
upb_label = UPB_LABEL_ ## upb; \
converted = true; \
}
CONVERT(OPTIONAL, optional);
CONVERT(REQUIRED, required);
CONVERT(REPEATED, repeated);
#undef CONVERT
if (!converted) {
rb_raise(rb_eArgError, "Unknown field label.");
}
upb_fielddef_setlabel(mut_def, upb_label);
return Qnil;
}
/* /*
* call-seq: * call-seq:
* FieldDescriptor.number => number * FieldDescriptor.number => number
...@@ -1026,6 +1002,21 @@ VALUE FieldDescriptor_number(VALUE _self) { ...@@ -1026,6 +1002,21 @@ VALUE FieldDescriptor_number(VALUE _self) {
return INT2NUM(upb_fielddef_number(self->fielddef)); return INT2NUM(upb_fielddef_number(self->fielddef));
} }
/*
* call-seq:
* FieldDescriptor.number = number
*
* Sets the tag number for this field. Cannot be called if field is part of a
* message type already in a pool.
*/
VALUE FieldDescriptor_number_set(VALUE _self, VALUE number) {
DEFINE_SELF(FieldDescriptor, self, _self);
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
CHECK_UPB(upb_fielddef_setnumber(mut_def, NUM2INT(number), &status),
"Error setting field number");
return Qnil;
}
/* /*
* call-seq: * call-seq:
* FieldDescriptor.submsg_name => submsg_name * FieldDescriptor.submsg_name => submsg_name
...@@ -1037,16 +1028,32 @@ VALUE FieldDescriptor_number(VALUE _self) { ...@@ -1037,16 +1028,32 @@ VALUE FieldDescriptor_number(VALUE _self) {
*/ */
VALUE FieldDescriptor_submsg_name(VALUE _self) { VALUE FieldDescriptor_submsg_name(VALUE _self) {
DEFINE_SELF(FieldDescriptor, self, _self); DEFINE_SELF(FieldDescriptor, self, _self);
switch (upb_fielddef_type(self->fielddef)) { if (!upb_fielddef_hassubdef(self->fielddef)) {
case UPB_TYPE_ENUM:
return rb_str_new2(
upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
case UPB_TYPE_MESSAGE:
return rb_str_new2(
upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
default:
return Qnil; return Qnil;
} }
return rb_str_maybe_null(upb_fielddef_subdefname(self->fielddef));
}
/*
* call-seq:
* FieldDescriptor.submsg_name = submsg_name
*
* Sets the name of the message or enum type corresponding to this field, if it
* is a message or enum field (respectively). This type name will be resolved
* within the context of the pool to which the containing message type is added.
* Cannot be called on field that are not of message or enum type, or on fields
* that are part of a message type already added to a pool.
*/
VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value) {
DEFINE_SELF(FieldDescriptor, self, _self);
upb_fielddef* mut_def = check_field_notfrozen(self->fielddef);
const char* str = get_str(value);
if (!upb_fielddef_hassubdef(self->fielddef)) {
rb_raise(cTypeError, "FieldDescriptor does not have subdef.");
}
CHECK_UPB(upb_fielddef_setsubdefname(mut_def, str, &status),
"Error setting submessage name");
return Qnil;
} }
/* /*
...@@ -1060,16 +1067,16 @@ VALUE FieldDescriptor_submsg_name(VALUE _self) { ...@@ -1060,16 +1067,16 @@ VALUE FieldDescriptor_submsg_name(VALUE _self) {
*/ */
VALUE FieldDescriptor_subtype(VALUE _self) { VALUE FieldDescriptor_subtype(VALUE _self) {
DEFINE_SELF(FieldDescriptor, self, _self); DEFINE_SELF(FieldDescriptor, self, _self);
switch (upb_fielddef_type(self->fielddef)) { const upb_def* def;
case UPB_TYPE_ENUM:
return get_enumdef_obj(self->descriptor_pool, if (!upb_fielddef_hassubdef(self->fielddef)) {
upb_fielddef_enumsubdef(self->fielddef));
case UPB_TYPE_MESSAGE:
return get_msgdef_obj(self->descriptor_pool,
upb_fielddef_msgsubdef(self->fielddef));
default:
return Qnil; return Qnil;
} }
def = upb_fielddef_subdef(self->fielddef);
if (def == NULL) {
return Qnil;
}
return get_def_obj(def);
} }
/* /*
...@@ -1153,12 +1160,12 @@ VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) { ...@@ -1153,12 +1160,12 @@ VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor"); DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
void OneofDescriptor_mark(void* _self) { void OneofDescriptor_mark(void* _self) {
OneofDescriptor* self = _self;
rb_gc_mark(self->descriptor_pool);
} }
void OneofDescriptor_free(void* _self) { void OneofDescriptor_free(void* _self) {
xfree(_self); OneofDescriptor* self = _self;
upb_oneofdef_unref(self->oneofdef, &self->oneofdef);
xfree(self);
} }
/* /*
...@@ -1171,8 +1178,7 @@ void OneofDescriptor_free(void* _self) { ...@@ -1171,8 +1178,7 @@ void OneofDescriptor_free(void* _self) {
VALUE OneofDescriptor_alloc(VALUE klass) { VALUE OneofDescriptor_alloc(VALUE klass) {
OneofDescriptor* self = ALLOC(OneofDescriptor); OneofDescriptor* self = ALLOC(OneofDescriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self); VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
self->oneofdef = NULL; self->oneofdef = upb_oneofdef_new(&self->oneofdef);
self->descriptor_pool = Qnil;
return ret; return ret;
} }
...@@ -1180,8 +1186,9 @@ void OneofDescriptor_register(VALUE module) { ...@@ -1180,8 +1186,9 @@ void OneofDescriptor_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "OneofDescriptor", rb_cObject); module, "OneofDescriptor", rb_cObject);
rb_define_alloc_func(klass, OneofDescriptor_alloc); rb_define_alloc_func(klass, OneofDescriptor_alloc);
rb_define_method(klass, "initialize", OneofDescriptor_initialize, 3);
rb_define_method(klass, "name", OneofDescriptor_name, 0); rb_define_method(klass, "name", OneofDescriptor_name, 0);
rb_define_method(klass, "name=", OneofDescriptor_name_set, 1);
rb_define_method(klass, "add_field", OneofDescriptor_add_field, 1);
rb_define_method(klass, "each", OneofDescriptor_each, 0); rb_define_method(klass, "each", OneofDescriptor_each, 0);
rb_include_module(klass, rb_mEnumerable); rb_include_module(klass, rb_mEnumerable);
rb_gc_register_address(&cOneofDescriptor); rb_gc_register_address(&cOneofDescriptor);
...@@ -1190,34 +1197,55 @@ void OneofDescriptor_register(VALUE module) { ...@@ -1190,34 +1197,55 @@ void OneofDescriptor_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor * OneofDescriptor.name => name
* *
* Creates a descriptor wrapper object. May only be called from C. * Returns the name of this oneof.
*/ */
VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie, VALUE OneofDescriptor_name(VALUE _self) {
VALUE descriptor_pool, VALUE ptr) {
DEFINE_SELF(OneofDescriptor, self, _self); DEFINE_SELF(OneofDescriptor, self, _self);
return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
}
if (cookie != c_only_cookie) { /*
rb_raise(rb_eRuntimeError, * call-seq:
"Descriptor objects may not be created from Ruby."); * OneofDescriptor.name = name
} *
* Sets a new name for this oneof. The oneof must not have been added to a
self->descriptor_pool = descriptor_pool; * message descriptor yet.
self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr); */
VALUE OneofDescriptor_name_set(VALUE _self, VALUE value) {
DEFINE_SELF(OneofDescriptor, self, _self);
upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
const char* str = get_str(value);
CHECK_UPB(upb_oneofdef_setname(mut_def, str, &status),
"Error setting oneof name");
return Qnil; return Qnil;
} }
/* /*
* call-seq: * call-seq:
* OneofDescriptor.name => name * OneofDescriptor.add_field(field) => nil
* *
* Returns the name of this oneof. * Adds a field to this oneof. The field may have been added to this oneof in
* the past, or the message to which this oneof belongs (if any), but may not
* have already been added to any other oneof or message. Otherwise, an
* exception is raised.
*
* All fields added to the oneof via this method will be automatically added to
* the message to which this oneof belongs, if it belongs to one currently, or
* else will be added to any message to which the oneof is later added at the
* time that it is added.
*/ */
VALUE OneofDescriptor_name(VALUE _self) { VALUE OneofDescriptor_add_field(VALUE _self, VALUE obj) {
DEFINE_SELF(OneofDescriptor, self, _self); DEFINE_SELF(OneofDescriptor, self, _self);
return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef)); upb_oneofdef* mut_def = check_oneof_notfrozen(self->oneofdef);
FieldDescriptor* def = ruby_to_FieldDescriptor(obj);
upb_fielddef* mut_field_def = check_field_notfrozen(def->fielddef);
CHECK_UPB(
upb_oneofdef_addfield(mut_def, mut_field_def, NULL, &status),
"Adding field to OneofDescriptor failed");
add_def_obj(def->fielddef, obj);
return Qnil;
} }
/* /*
...@@ -1233,7 +1261,7 @@ VALUE OneofDescriptor_each(VALUE _self, VALUE field) { ...@@ -1233,7 +1261,7 @@ VALUE OneofDescriptor_each(VALUE _self, VALUE field) {
!upb_oneof_done(&it); !upb_oneof_done(&it);
upb_oneof_next(&it)) { upb_oneof_next(&it)) {
const upb_fielddef* f = upb_oneof_iter_field(&it); const upb_fielddef* f = upb_oneof_iter_field(&it);
VALUE obj = get_fielddef_obj(self->descriptor_pool, f); VALUE obj = get_def_obj(f);
rb_yield(obj); rb_yield(obj);
} }
return Qnil; return Qnil;
...@@ -1248,49 +1276,38 @@ DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor"); ...@@ -1248,49 +1276,38 @@ DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
void EnumDescriptor_mark(void* _self) { void EnumDescriptor_mark(void* _self) {
EnumDescriptor* self = _self; EnumDescriptor* self = _self;
rb_gc_mark(self->module); rb_gc_mark(self->module);
rb_gc_mark(self->descriptor_pool);
} }
void EnumDescriptor_free(void* _self) { void EnumDescriptor_free(void* _self) {
xfree(_self); EnumDescriptor* self = _self;
upb_enumdef_unref(self->enumdef, &self->enumdef);
xfree(self);
} }
/*
* call-seq:
* EnumDescriptor.new => enum_descriptor
*
* Creates a new, empty, enum descriptor. Must be added to a pool before the
* enum type can be used. The enum type may only be modified prior to adding to
* a pool.
*/
VALUE EnumDescriptor_alloc(VALUE klass) { VALUE EnumDescriptor_alloc(VALUE klass) {
EnumDescriptor* self = ALLOC(EnumDescriptor); EnumDescriptor* self = ALLOC(EnumDescriptor);
VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self); VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
self->enumdef = NULL; self->enumdef = upb_enumdef_new(&self->enumdef);
self->module = Qnil; self->module = Qnil;
self->descriptor_pool = Qnil;
return ret; return ret;
} }
/*
* call-seq:
* EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
*
* Creates a descriptor wrapper object. May only be called from C.
*/
VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr) {
DEFINE_SELF(EnumDescriptor, self, _self);
if (cookie != c_only_cookie) {
rb_raise(rb_eRuntimeError,
"Descriptor objects may not be created from Ruby.");
}
self->descriptor_pool = descriptor_pool;
self->enumdef = (const upb_enumdef*)NUM2ULL(ptr);
return Qnil;
}
void EnumDescriptor_register(VALUE module) { void EnumDescriptor_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "EnumDescriptor", rb_cObject); module, "EnumDescriptor", rb_cObject);
rb_define_alloc_func(klass, EnumDescriptor_alloc); rb_define_alloc_func(klass, EnumDescriptor_alloc);
rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3); rb_define_method(klass, "initialize", EnumDescriptor_initialize, 1);
rb_define_method(klass, "name", EnumDescriptor_name, 0); rb_define_method(klass, "name", EnumDescriptor_name, 0);
rb_define_method(klass, "name=", EnumDescriptor_name_set, 1);
rb_define_method(klass, "add_value", EnumDescriptor_add_value, 2);
rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1); rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1); rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
rb_define_method(klass, "each", EnumDescriptor_each, 0); rb_define_method(klass, "each", EnumDescriptor_each, 0);
...@@ -1303,14 +1320,31 @@ void EnumDescriptor_register(VALUE module) { ...@@ -1303,14 +1320,31 @@ void EnumDescriptor_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* EnumDescriptor.file_descriptor * Descriptor.new(file_descriptor)
*
* Initializes a new descriptor and assigns a file descriptor to it.
*/
VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb) {
DEFINE_SELF(EnumDescriptor, self, _self);
FileDescriptor* file_descriptor = ruby_to_FileDescriptor(file_descriptor_rb);
CHECK_UPB(
upb_filedef_addenum(file_descriptor->filedef, self->enumdef,
NULL, &status),
"Failed to associate enum to file descriptor.");
add_def_obj(file_descriptor->filedef, file_descriptor_rb);
return Qnil;
}
/*
* call-seq:
* Descriptor.file_descriptor
* *
* Returns the FileDescriptor object this enum belongs to. * Returns the FileDescriptor object this enum belongs to.
*/ */
VALUE EnumDescriptor_file_descriptor(VALUE _self) { VALUE EnumDescriptor_file_descriptor(VALUE _self) {
DEFINE_SELF(EnumDescriptor, self, _self); DEFINE_SELF(EnumDescriptor, self, _self);
return get_filedef_obj(self->descriptor_pool, return get_def_obj(upb_def_file(self->enumdef));
upb_enumdef_file(self->enumdef));
} }
/* /*
...@@ -1324,6 +1358,40 @@ VALUE EnumDescriptor_name(VALUE _self) { ...@@ -1324,6 +1358,40 @@ VALUE EnumDescriptor_name(VALUE _self) {
return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef)); return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
} }
/*
* call-seq:
* EnumDescriptor.name = name
*
* Sets the name of this enum type. Cannot be called if the enum type has
* already been added to a pool.
*/
VALUE EnumDescriptor_name_set(VALUE _self, VALUE str) {
DEFINE_SELF(EnumDescriptor, self, _self);
upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
const char* name = get_str(str);
CHECK_UPB(upb_enumdef_setfullname(mut_def, name, &status),
"Error setting EnumDescriptor name");
return Qnil;
}
/*
* call-seq:
* EnumDescriptor.add_value(key, value)
*
* Adds a new key => value mapping to this enum type. Key must be given as a
* Ruby symbol. Cannot be called if the enum type has already been added to a
* pool. Will raise an exception if the key or value is already in use.
*/
VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number) {
DEFINE_SELF(EnumDescriptor, self, _self);
upb_enumdef* mut_def = check_enum_notfrozen(self->enumdef);
const char* name_str = rb_id2name(SYM2ID(name));
int32_t val = NUM2INT(number);
CHECK_UPB(upb_enumdef_addval(mut_def, name_str, val, &status),
"Error adding value to enum");
return Qnil;
}
/* /*
* call-seq: * call-seq:
* EnumDescriptor.lookup_name(name) => value * EnumDescriptor.lookup_name(name) => value
...@@ -1386,12 +1454,18 @@ VALUE EnumDescriptor_each(VALUE _self) { ...@@ -1386,12 +1454,18 @@ VALUE EnumDescriptor_each(VALUE _self) {
* call-seq: * call-seq:
* EnumDescriptor.enummodule => module * EnumDescriptor.enummodule => module
* *
* Returns the Ruby module corresponding to this enum type. * Returns the Ruby module corresponding to this enum type. Cannot be called
* until the enum descriptor has been added to a pool.
*/ */
VALUE EnumDescriptor_enummodule(VALUE _self) { VALUE EnumDescriptor_enummodule(VALUE _self) {
DEFINE_SELF(EnumDescriptor, self, _self); DEFINE_SELF(EnumDescriptor, self, _self);
if (!upb_def_isfrozen((const upb_def*)self->enumdef)) {
rb_raise(rb_eRuntimeError,
"Cannot fetch enum module from an EnumDescriptor not yet "
"in a pool.");
}
if (self->module == Qnil) { if (self->module == Qnil) {
self->module = build_module_from_enumdesc(_self); self->module = build_module_from_enumdesc(self);
} }
return self->module; return self->module;
} }
...@@ -1405,7 +1479,8 @@ DEFINE_CLASS(MessageBuilderContext, ...@@ -1405,7 +1479,8 @@ DEFINE_CLASS(MessageBuilderContext,
void MessageBuilderContext_mark(void* _self) { void MessageBuilderContext_mark(void* _self) {
MessageBuilderContext* self = _self; MessageBuilderContext* self = _self;
rb_gc_mark(self->file_builder); rb_gc_mark(self->descriptor);
rb_gc_mark(self->builder);
} }
void MessageBuilderContext_free(void* _self) { void MessageBuilderContext_free(void* _self) {
...@@ -1417,7 +1492,8 @@ VALUE MessageBuilderContext_alloc(VALUE klass) { ...@@ -1417,7 +1492,8 @@ VALUE MessageBuilderContext_alloc(VALUE klass) {
MessageBuilderContext* self = ALLOC(MessageBuilderContext); MessageBuilderContext* self = ALLOC(MessageBuilderContext);
VALUE ret = TypedData_Wrap_Struct( VALUE ret = TypedData_Wrap_Struct(
klass, &_MessageBuilderContext_type, self); klass, &_MessageBuilderContext_type, self);
self->file_builder = Qnil; self->descriptor = Qnil;
self->builder = Qnil;
return ret; return ret;
} }
...@@ -1438,57 +1514,40 @@ void MessageBuilderContext_register(VALUE module) { ...@@ -1438,57 +1514,40 @@ void MessageBuilderContext_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* MessageBuilderContext.new(file_builder, name) => context * MessageBuilderContext.new(desc, builder) => context
* *
* Create a new message builder context around the given message descriptor and * Create a new message builder context around the given message descriptor and
* builder context. This class is intended to serve as a DSL context to be used * builder context. This class is intended to serve as a DSL context to be used
* with #instance_eval. * with #instance_eval.
*/ */
VALUE MessageBuilderContext_initialize(VALUE _self, VALUE MessageBuilderContext_initialize(VALUE _self,
VALUE _file_builder, VALUE msgdef,
VALUE name) { VALUE builder) {
DEFINE_SELF(MessageBuilderContext, self, _self); DEFINE_SELF(MessageBuilderContext, self, _self);
FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder); self->descriptor = msgdef;
google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto; self->builder = builder;
self->file_builder = _file_builder;
self->msg_proto = google_protobuf_FileDescriptorProto_add_message_type(
file_proto, file_builder->arena);
google_protobuf_DescriptorProto_set_name(
self->msg_proto, FileBuilderContext_strdup(_file_builder, name));
return Qnil; return Qnil;
} }
static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name, static VALUE msgdef_add_field(VALUE msgdef_rb,
VALUE type, VALUE number, VALUE type_class, const char* label, VALUE name,
VALUE options, int oneof_index) { VALUE type, VALUE number,
DEFINE_SELF(MessageBuilderContext, self, msgbuilder_rb); VALUE type_class,
FileBuilderContext* file_context = VALUE options) {
ruby_to_FileBuilderContext(self->file_builder); VALUE fielddef_rb = rb_class_new_instance(0, NULL, cFieldDescriptor);
google_protobuf_FieldDescriptorProto* field_proto = VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
google_protobuf_DescriptorProto_add_field(self->msg_proto,
file_context->arena);
Check_Type(name, T_SYMBOL);
VALUE name_str = rb_id2str(SYM2ID(name));
google_protobuf_FieldDescriptorProto_set_name( rb_funcall(fielddef_rb, rb_intern("label="), 1, ID2SYM(rb_intern(label)));
field_proto, FileBuilderContext_strdup(self->file_builder, name_str)); rb_funcall(fielddef_rb, rb_intern("name="), 1, name_str);
google_protobuf_FieldDescriptorProto_set_number(field_proto, NUM2INT(number)); rb_funcall(fielddef_rb, rb_intern("type="), 1, type);
google_protobuf_FieldDescriptorProto_set_label(field_proto, (int)label); rb_funcall(fielddef_rb, rb_intern("number="), 1, number);
google_protobuf_FieldDescriptorProto_set_type(
field_proto, (int)ruby_to_descriptortype(type));
if (type_class != Qnil) { if (type_class != Qnil) {
Check_Type(type_class, T_STRING); Check_Type(type_class, T_STRING);
// Make it an absolute type name by prepending a dot. // Make it an absolute type name by prepending a dot.
type_class = rb_str_append(rb_str_new2("."), type_class); type_class = rb_str_append(rb_str_new2("."), type_class);
google_protobuf_FieldDescriptorProto_set_type_name( rb_funcall(fielddef_rb, rb_intern("submsg_name="), 1, type_class);
field_proto, FileBuilderContext_strdup(self->file_builder, type_class));
} }
if (options != Qnil) { if (options != Qnil) {
...@@ -1496,51 +1555,24 @@ static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name, ...@@ -1496,51 +1555,24 @@ static void msgdef_add_field(VALUE msgbuilder_rb, upb_label_t label, VALUE name,
if (rb_funcall(options, rb_intern("key?"), 1, if (rb_funcall(options, rb_intern("key?"), 1,
ID2SYM(rb_intern("default"))) == Qtrue) { ID2SYM(rb_intern("default"))) == Qtrue) {
VALUE default_value = Descriptor* msgdef = ruby_to_Descriptor(msgdef_rb);
rb_hash_lookup(options, ID2SYM(rb_intern("default"))); if (upb_msgdef_syntax((upb_msgdef*)msgdef->msgdef) == UPB_SYNTAX_PROTO3) {
rb_raise(rb_eArgError, "Cannot set :default when using proto3 syntax.");
/* Call #to_s since all defaults are strings in the descriptor. */
default_value = rb_funcall(default_value, rb_intern("to_s"), 0);
google_protobuf_FieldDescriptorProto_set_default_value(
field_proto,
FileBuilderContext_strdup(self->file_builder, default_value));
}
} }
if (oneof_index >= 0) { FieldDescriptor* fielddef = ruby_to_FieldDescriptor(fielddef_rb);
google_protobuf_FieldDescriptorProto_set_oneof_index(field_proto, if (!upb_fielddef_haspresence((upb_fielddef*)fielddef->fielddef) ||
oneof_index); upb_fielddef_issubmsg((upb_fielddef*)fielddef->fielddef)) {
rb_raise(rb_eArgError, "Cannot set :default on this kind of field.");
} }
}
static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
VALUE* argv) {
DEFINE_SELF(MessageBuilderContext, message_builder, _message_builder);
VALUE type_class = rb_ary_entry(types, 2);
FileBuilderContext* file_context =
ruby_to_FileBuilderContext(message_builder->file_builder);
google_protobuf_MessageOptions* options =
google_protobuf_DescriptorProto_mutable_options(
message_builder->msg_proto, file_context->arena);
google_protobuf_MessageOptions_set_map_entry(options, true); rb_funcall(fielddef_rb, rb_intern("default="), 1,
rb_hash_lookup(options, ID2SYM(rb_intern("default"))));
// optional <type> key = 1; }
rb_funcall(_message_builder, rb_intern("optional"), 3,
ID2SYM(rb_intern("key")), rb_ary_entry(types, 0), INT2NUM(1));
// optional <type> value = 2;
if (type_class == Qnil) {
rb_funcall(_message_builder, rb_intern("optional"), 3,
ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2));
} else {
rb_funcall(_message_builder, rb_intern("optional"), 4,
ID2SYM(rb_intern("value")), rb_ary_entry(types, 1), INT2NUM(2),
type_class);
} }
return Qnil; rb_funcall(msgdef_rb, rb_intern("add_field"), 1, fielddef_rb);
return fielddef_rb;
} }
/* /*
...@@ -1567,10 +1599,8 @@ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { ...@@ -1567,10 +1599,8 @@ VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
type_class = Qnil; type_class = Qnil;
} }
msgdef_add_field(_self, UPB_LABEL_OPTIONAL, name, type, number, type_class, return msgdef_add_field(self->descriptor, "optional",
options, -1); name, type, number, type_class, options);
return Qnil;
} }
/* /*
...@@ -1601,10 +1631,8 @@ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) { ...@@ -1601,10 +1631,8 @@ VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
type_class = Qnil; type_class = Qnil;
} }
msgdef_add_field(_self, UPB_LABEL_REQUIRED, name, type, number, type_class, return msgdef_add_field(self->descriptor, "required",
options, -1); name, type, number, type_class, options);
return Qnil;
} }
/* /*
...@@ -1628,10 +1656,8 @@ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) { ...@@ -1628,10 +1656,8 @@ VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
number = argv[2]; number = argv[2];
type_class = (argc > 3) ? argv[3] : Qnil; type_class = (argc > 3) ? argv[3] : Qnil;
msgdef_add_field(_self, UPB_LABEL_REPEATED, name, type, number, type_class, return msgdef_add_field(self->descriptor, "repeated",
Qnil, -1); name, type, number, type_class, Qnil);
return Qnil;
} }
/* /*
...@@ -1672,43 +1698,77 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) { ...@@ -1672,43 +1698,77 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
"type."); "type.");
} }
FileBuilderContext* file_builder = Descriptor* descriptor = ruby_to_Descriptor(self->descriptor);
ruby_to_FileBuilderContext(self->file_builder); if (upb_msgdef_syntax(descriptor->msgdef) == UPB_SYNTAX_PROTO2) {
// TODO(haberman): remove this restriction, maps are supported in proto2.
if (upb_strview_eql(
google_protobuf_FileDescriptorProto_syntax(file_builder->file_proto),
upb_strview_makez("proto2"))) {
rb_raise(rb_eArgError, rb_raise(rb_eArgError,
"Cannot add a native map field using proto2 syntax."); "Cannot add a native map field using proto2 syntax.");
} }
// Create a new message descriptor for the map entry message, and create a // Create a new message descriptor for the map entry message, and create a
// repeated submessage field here with that type. // repeated submessage field here with that type.
upb_strview msg_name = google_protobuf_DescriptorProto_name(self->msg_proto); VALUE file_descriptor_rb =
mapentry_desc_name = rb_str_new(msg_name.data, msg_name.size); rb_funcall(self->descriptor, rb_intern("file_descriptor"), 0);
mapentry_desc = rb_class_new_instance(1, &file_descriptor_rb, cDescriptor);
mapentry_desc_name = rb_funcall(self->descriptor, rb_intern("name"), 0);
mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_"); mapentry_desc_name = rb_str_cat2(mapentry_desc_name, "_MapEntry_");
mapentry_desc_name = mapentry_desc_name = rb_str_cat2(mapentry_desc_name,
rb_str_cat2(mapentry_desc_name, rb_id2name(SYM2ID(name))); rb_id2name(SYM2ID(name)));
Descriptor_name_set(mapentry_desc, mapentry_desc_name);
// message <msgname>_MapEntry_ { /* ... */ } {
VALUE args[1] = { mapentry_desc_name }; // The 'mapentry' attribute has no Ruby setter because we do not want the
VALUE types = rb_ary_new3(3, key_type, value_type, type_class); // user attempting to DIY the setup below; we want to ensure that the fields
rb_block_call(self->file_builder, rb_intern("add_message"), 1, args, // are correct. So we reach into the msgdef here to set the bit manually.
make_mapentry, types); Descriptor* mapentry_desc_self = ruby_to_Descriptor(mapentry_desc);
upb_msgdef_setmapentry((upb_msgdef*)mapentry_desc_self->msgdef, true);
}
// If this file is in a package, we need to qualify the map entry type. {
if (google_protobuf_FileDescriptorProto_has_package(file_builder->file_proto)) { // optional <type> key = 1;
upb_strview package_view = VALUE key_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
google_protobuf_FileDescriptorProto_package(file_builder->file_proto); FieldDescriptor_name_set(key_field, rb_str_new2("key"));
VALUE package = rb_str_new(package_view.data, package_view.size); FieldDescriptor_label_set(key_field, ID2SYM(rb_intern("optional")));
package = rb_str_cat2(package, "."); FieldDescriptor_number_set(key_field, INT2NUM(1));
mapentry_desc_name = rb_str_concat(package, mapentry_desc_name); FieldDescriptor_type_set(key_field, key_type);
Descriptor_add_field(mapentry_desc, key_field);
} }
// repeated MapEntry <name> = <number>; {
rb_funcall(_self, rb_intern("repeated"), 4, name, // optional <type> value = 2;
ID2SYM(rb_intern("message")), number, mapentry_desc_name); VALUE value_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
FieldDescriptor_name_set(value_field, rb_str_new2("value"));
FieldDescriptor_label_set(value_field, ID2SYM(rb_intern("optional")));
FieldDescriptor_number_set(value_field, INT2NUM(2));
FieldDescriptor_type_set(value_field, value_type);
if (type_class != Qnil) {
VALUE submsg_name = rb_str_new2("."); // prepend '.' to make absolute.
submsg_name = rb_str_append(submsg_name, type_class);
FieldDescriptor_submsg_name_set(value_field, submsg_name);
}
Descriptor_add_field(mapentry_desc, value_field);
}
{
// Add the map-entry message type to the current builder, and use the type
// to create the map field itself.
Builder* builder = ruby_to_Builder(self->builder);
rb_ary_push(builder->pending_list, mapentry_desc);
}
{
VALUE map_field = rb_class_new_instance(0, NULL, cFieldDescriptor);
VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
VALUE submsg_name;
FieldDescriptor_name_set(map_field, name_str);
FieldDescriptor_number_set(map_field, number);
FieldDescriptor_label_set(map_field, ID2SYM(rb_intern("repeated")));
FieldDescriptor_type_set(map_field, ID2SYM(rb_intern("message")));
submsg_name = rb_str_new2("."); // prepend '.' to make name absolute.
submsg_name = rb_str_append(submsg_name, mapentry_desc_name);
FieldDescriptor_submsg_name_set(map_field, submsg_name);
Descriptor_add_field(self->descriptor, map_field);
}
return Qnil; return Qnil;
} }
...@@ -1726,26 +1786,14 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) { ...@@ -1726,26 +1786,14 @@ VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
*/ */
VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) { VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
DEFINE_SELF(MessageBuilderContext, self, _self); DEFINE_SELF(MessageBuilderContext, self, _self);
size_t oneof_count; VALUE oneofdef = rb_class_new_instance(0, NULL, cOneofDescriptor);
FileBuilderContext* file_context = VALUE args[2] = { oneofdef, self->builder };
ruby_to_FileBuilderContext(self->file_builder);
// Existing oneof_count becomes oneof_index.
google_protobuf_DescriptorProto_oneof_decl(self->msg_proto, &oneof_count);
// Create oneof_proto and set its name.
google_protobuf_OneofDescriptorProto* oneof_proto =
google_protobuf_DescriptorProto_add_oneof_decl(self->msg_proto,
file_context->arena);
VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
upb_strview name_strview = upb_strview_makez(StringValueCStr(name_str));
google_protobuf_OneofDescriptorProto_set_name(oneof_proto, name_strview);
// Evaluate the block with the builder as argument.
VALUE args[2] = { INT2NUM(oneof_count), _self };
VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext); VALUE ctx = rb_class_new_instance(2, args, cOneofBuilderContext);
VALUE block = rb_block_proc(); VALUE block = rb_block_proc();
VALUE name_str = rb_str_new2(rb_id2name(SYM2ID(name)));
rb_funcall(oneofdef, rb_intern("name="), 1, name_str);
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
Descriptor_add_oneof(self->descriptor, oneofdef);
return Qnil; return Qnil;
} }
...@@ -1759,19 +1807,21 @@ DEFINE_CLASS(OneofBuilderContext, ...@@ -1759,19 +1807,21 @@ DEFINE_CLASS(OneofBuilderContext,
void OneofBuilderContext_mark(void* _self) { void OneofBuilderContext_mark(void* _self) {
OneofBuilderContext* self = _self; OneofBuilderContext* self = _self;
rb_gc_mark(self->message_builder); rb_gc_mark(self->descriptor);
rb_gc_mark(self->builder);
} }
void OneofBuilderContext_free(void* _self) { void OneofBuilderContext_free(void* _self) {
xfree(_self); OneofBuilderContext* self = _self;
xfree(self);
} }
VALUE OneofBuilderContext_alloc(VALUE klass) { VALUE OneofBuilderContext_alloc(VALUE klass) {
OneofBuilderContext* self = ALLOC(OneofBuilderContext); OneofBuilderContext* self = ALLOC(OneofBuilderContext);
VALUE ret = TypedData_Wrap_Struct( VALUE ret = TypedData_Wrap_Struct(
klass, &_OneofBuilderContext_type, self); klass, &_OneofBuilderContext_type, self);
self->oneof_index = 0; self->descriptor = Qnil;
self->message_builder = Qnil; self->builder = Qnil;
return ret; return ret;
} }
...@@ -1788,18 +1838,18 @@ void OneofBuilderContext_register(VALUE module) { ...@@ -1788,18 +1838,18 @@ void OneofBuilderContext_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* OneofBuilderContext.new(oneof_index, message_builder) => context * OneofBuilderContext.new(desc, builder) => context
* *
* Create a new oneof builder context around the given oneof descriptor and * Create a new oneof builder context around the given oneof descriptor and
* builder context. This class is intended to serve as a DSL context to be used * builder context. This class is intended to serve as a DSL context to be used
* with #instance_eval. * with #instance_eval.
*/ */
VALUE OneofBuilderContext_initialize(VALUE _self, VALUE OneofBuilderContext_initialize(VALUE _self,
VALUE oneof_index, VALUE oneofdef,
VALUE message_builder) { VALUE builder) {
DEFINE_SELF(OneofBuilderContext, self, _self); DEFINE_SELF(OneofBuilderContext, self, _self);
self->oneof_index = NUM2INT(oneof_index); self->descriptor = oneofdef;
self->message_builder = message_builder; self->builder = builder;
return Qnil; return Qnil;
} }
...@@ -1820,10 +1870,8 @@ VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) { ...@@ -1820,10 +1870,8 @@ VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options); rb_scan_args(argc, argv, "32", &name, &type, &number, &type_class, &options);
msgdef_add_field(self->message_builder, UPB_LABEL_OPTIONAL, name, type, return msgdef_add_field(self->descriptor, "optional",
number, type_class, options, self->oneof_index); name, type, number, type_class, options);
return Qnil;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -1835,19 +1883,19 @@ DEFINE_CLASS(EnumBuilderContext, ...@@ -1835,19 +1883,19 @@ DEFINE_CLASS(EnumBuilderContext,
void EnumBuilderContext_mark(void* _self) { void EnumBuilderContext_mark(void* _self) {
EnumBuilderContext* self = _self; EnumBuilderContext* self = _self;
rb_gc_mark(self->file_builder); rb_gc_mark(self->enumdesc);
} }
void EnumBuilderContext_free(void* _self) { void EnumBuilderContext_free(void* _self) {
xfree(_self); EnumBuilderContext* self = _self;
xfree(self);
} }
VALUE EnumBuilderContext_alloc(VALUE klass) { VALUE EnumBuilderContext_alloc(VALUE klass) {
EnumBuilderContext* self = ALLOC(EnumBuilderContext); EnumBuilderContext* self = ALLOC(EnumBuilderContext);
VALUE ret = TypedData_Wrap_Struct( VALUE ret = TypedData_Wrap_Struct(
klass, &_EnumBuilderContext_type, self); klass, &_EnumBuilderContext_type, self);
self->enum_proto = NULL; self->enumdesc = Qnil;
self->file_builder = Qnil;
return ret; return ret;
} }
...@@ -1855,7 +1903,8 @@ void EnumBuilderContext_register(VALUE module) { ...@@ -1855,7 +1903,8 @@ void EnumBuilderContext_register(VALUE module) {
VALUE klass = rb_define_class_under( VALUE klass = rb_define_class_under(
module, "EnumBuilderContext", rb_cObject); module, "EnumBuilderContext", rb_cObject);
rb_define_alloc_func(klass, EnumBuilderContext_alloc); rb_define_alloc_func(klass, EnumBuilderContext_alloc);
rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2); rb_define_method(klass, "initialize",
EnumBuilderContext_initialize, 1);
rb_define_method(klass, "value", EnumBuilderContext_value, 2); rb_define_method(klass, "value", EnumBuilderContext_value, 2);
rb_gc_register_address(&cEnumBuilderContext); rb_gc_register_address(&cEnumBuilderContext);
cEnumBuilderContext = klass; cEnumBuilderContext = klass;
...@@ -1863,24 +1912,20 @@ void EnumBuilderContext_register(VALUE module) { ...@@ -1863,24 +1912,20 @@ void EnumBuilderContext_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* EnumBuilderContext.new(file_builder) => context * EnumBuilderContext.new(enumdesc) => context
* *
* Create a new builder context around the given enum descriptor. This class is * Create a new builder context around the given enum descriptor. This class is
* intended to serve as a DSL context to be used with #instance_eval. * intended to serve as a DSL context to be used with #instance_eval.
*/ */
VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder, VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdef) {
VALUE name) {
DEFINE_SELF(EnumBuilderContext, self, _self); DEFINE_SELF(EnumBuilderContext, self, _self);
FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder); self->enumdesc = enumdef;
google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto; return Qnil;
}
self->file_builder = _file_builder;
self->enum_proto = google_protobuf_FileDescriptorProto_add_enum_type(
file_proto, file_builder->arena);
google_protobuf_EnumDescriptorProto_set_name(
self->enum_proto, FileBuilderContext_strdup(_file_builder, name));
static VALUE enumdef_add_value(VALUE enumdef,
VALUE name, VALUE number) {
rb_funcall(enumdef, rb_intern("add_value"), 2, name, number);
return Qnil; return Qnil;
} }
...@@ -1893,21 +1938,7 @@ VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder, ...@@ -1893,21 +1938,7 @@ VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
*/ */
VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) { VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
DEFINE_SELF(EnumBuilderContext, self, _self); DEFINE_SELF(EnumBuilderContext, self, _self);
FileBuilderContext* file_builder = return enumdef_add_value(self->enumdesc, name, number);
ruby_to_FileBuilderContext(self->file_builder);
Check_Type(name, T_SYMBOL);
VALUE name_str = rb_id2str(SYM2ID(name));
google_protobuf_EnumValueDescriptorProto* enum_value =
google_protobuf_EnumDescriptorProto_add_value(self->enum_proto,
file_builder->arena);
google_protobuf_EnumValueDescriptorProto_set_name(
enum_value, FileBuilderContext_strdup(self->file_builder, name_str));
google_protobuf_EnumValueDescriptorProto_set_number(enum_value,
NUM2INT(number));
return Qnil;
} }
...@@ -1920,45 +1951,29 @@ DEFINE_CLASS(FileBuilderContext, ...@@ -1920,45 +1951,29 @@ DEFINE_CLASS(FileBuilderContext,
void FileBuilderContext_mark(void* _self) { void FileBuilderContext_mark(void* _self) {
FileBuilderContext* self = _self; FileBuilderContext* self = _self;
rb_gc_mark(self->descriptor_pool); rb_gc_mark(self->pending_list);
rb_gc_mark(self->file_descriptor);
rb_gc_mark(self->builder);
} }
void FileBuilderContext_free(void* _self) { void FileBuilderContext_free(void* _self) {
FileBuilderContext* self = _self; FileBuilderContext* self = _self;
upb_arena_free(self->arena);
xfree(self); xfree(self);
} }
upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
DEFINE_SELF(FileBuilderContext, self, _self);
upb_strview ret;
ret.size = strlen(str);
char *data = upb_malloc(upb_arena_alloc(self->arena), ret.size + 1);
ret.data = data;
memcpy(data, str, ret.size);
/* Null-terminate required by rewrite_enum_defaults() above. */
data[ret.size] = '\0';
return ret;
}
upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
const char *str = get_str(rb_str);
return FileBuilderContext_strdup2(_self, str);
}
VALUE FileBuilderContext_alloc(VALUE klass) { VALUE FileBuilderContext_alloc(VALUE klass) {
FileBuilderContext* self = ALLOC(FileBuilderContext); FileBuilderContext* self = ALLOC(FileBuilderContext);
VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self); VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self);
self->arena = upb_arena_new(); self->pending_list = Qnil;
self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena); self->file_descriptor = Qnil;
self->descriptor_pool = Qnil; self->builder = Qnil;
return ret; return ret;
} }
void FileBuilderContext_register(VALUE module) { void FileBuilderContext_register(VALUE module) {
VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject); VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
rb_define_alloc_func(klass, FileBuilderContext_alloc); rb_define_alloc_func(klass, FileBuilderContext_alloc);
rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3); rb_define_method(klass, "initialize", FileBuilderContext_initialize, 2);
rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1); rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1); rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
rb_gc_register_address(&cFileBuilderContext); rb_gc_register_address(&cFileBuilderContext);
...@@ -1967,38 +1982,18 @@ void FileBuilderContext_register(VALUE module) { ...@@ -1967,38 +1982,18 @@ void FileBuilderContext_register(VALUE module) {
/* /*
* call-seq: * call-seq:
* FileBuilderContext.new(descriptor_pool) => context * FileBuilderContext.new(file_descriptor, builder) => context
* *
* Create a new file builder context for the given file descriptor and * Create a new file builder context for the given file descriptor and
* builder context. This class is intended to serve as a DSL context to be used * builder context. This class is intended to serve as a DSL context to be used
* with #instance_eval. * with #instance_eval.
*/ */
VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool, VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
VALUE name, VALUE options) { VALUE builder) {
DEFINE_SELF(FileBuilderContext, self, _self); DEFINE_SELF(FileBuilderContext, self, _self);
self->descriptor_pool = descriptor_pool; self->pending_list = rb_ary_new();
self->file_descriptor = file_descriptor;
google_protobuf_FileDescriptorProto_set_name( self->builder = builder;
self->file_proto, FileBuilderContext_strdup(_self, name));
// Default syntax for Ruby is proto3.
google_protobuf_FileDescriptorProto_set_syntax(
self->file_proto,
FileBuilderContext_strdup(_self, rb_str_new2("proto3")));
if (options != Qnil) {
Check_Type(options, T_HASH);
VALUE syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
if (syntax != Qnil) {
Check_Type(syntax, T_SYMBOL);
VALUE syntax_str = rb_id2str(SYM2ID(syntax));
google_protobuf_FileDescriptorProto_set_syntax(
self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
}
}
return Qnil; return Qnil;
} }
...@@ -2015,10 +2010,13 @@ VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool, ...@@ -2015,10 +2010,13 @@ VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
*/ */
VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) { VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
DEFINE_SELF(FileBuilderContext, self, _self); DEFINE_SELF(FileBuilderContext, self, _self);
VALUE args[2] = { _self, name }; VALUE msgdef = rb_class_new_instance(1, &self->file_descriptor, cDescriptor);
VALUE args[2] = { msgdef, self->builder };
VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext); VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
VALUE block = rb_block_proc(); VALUE block = rb_block_proc();
rb_funcall(msgdef, rb_intern("name="), 1, name);
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
rb_ary_push(self->pending_list, msgdef);
return Qnil; return Qnil;
} }
...@@ -2034,26 +2032,19 @@ VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) { ...@@ -2034,26 +2032,19 @@ VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
*/ */
VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) { VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
DEFINE_SELF(FileBuilderContext, self, _self); DEFINE_SELF(FileBuilderContext, self, _self);
VALUE args[2] = { _self, name }; VALUE enumdef =
VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext); rb_class_new_instance(1, &self->file_descriptor, cEnumDescriptor);
VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
VALUE block = rb_block_proc(); VALUE block = rb_block_proc();
rb_funcall(enumdef, rb_intern("name="), 1, name);
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
rb_ary_push(self->pending_list, enumdef);
return Qnil; return Qnil;
} }
void FileBuilderContext_build(VALUE _self) { VALUE FileBuilderContext_pending_descriptors(VALUE _self) {
DEFINE_SELF(FileBuilderContext, self, _self); DEFINE_SELF(FileBuilderContext, self, _self);
DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool); return self->pending_list;
rewrite_enum_defaults(pool->symtab, self->file_proto);
rewrite_names(_self, self->file_proto);
upb_status status;
upb_status_clear(&status);
if (!upb_symtab_addfile(pool->symtab, self->file_proto, &status)) {
rb_raise(cTypeError, "Unable to add defs to DescriptorPool: %s",
upb_status_errmsg(&status));
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -2064,46 +2055,58 @@ DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder"); ...@@ -2064,46 +2055,58 @@ DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
void Builder_mark(void* _self) { void Builder_mark(void* _self) {
Builder* self = _self; Builder* self = _self;
rb_gc_mark(self->descriptor_pool); rb_gc_mark(self->pending_list);
rb_gc_mark(self->default_file_builder); rb_gc_mark(self->default_file_descriptor);
} }
void Builder_free(void* _self) { void Builder_free(void* _self) {
xfree(_self); Builder* self = _self;
xfree(self->defs);
xfree(self);
} }
/*
* call-seq:
* Builder.new => builder
*
* Creates a new Builder. A Builder can accumulate a set of new message and enum
* descriptors and atomically register them into a pool in a way that allows for
* (co)recursive type references.
*/
VALUE Builder_alloc(VALUE klass) { VALUE Builder_alloc(VALUE klass) {
Builder* self = ALLOC(Builder); Builder* self = ALLOC(Builder);
VALUE ret = TypedData_Wrap_Struct( VALUE ret = TypedData_Wrap_Struct(
klass, &_Builder_type, self); klass, &_Builder_type, self);
self->descriptor_pool = Qnil; self->pending_list = Qnil;
self->default_file_builder = Qnil; self->defs = NULL;
self->default_file_descriptor = Qnil;
return ret; return ret;
} }
void Builder_register(VALUE module) { void Builder_register(VALUE module) {
VALUE klass = rb_define_class_under(module, "Builder", rb_cObject); VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
rb_define_alloc_func(klass, Builder_alloc); rb_define_alloc_func(klass, Builder_alloc);
rb_define_method(klass, "initialize", Builder_initialize, 1); rb_define_method(klass, "initialize", Builder_initialize, 0);
rb_define_method(klass, "add_file", Builder_add_file, -1); rb_define_method(klass, "add_file", Builder_add_file, -1);
rb_define_method(klass, "add_message", Builder_add_message, 1); rb_define_method(klass, "add_message", Builder_add_message, 1);
rb_define_method(klass, "add_enum", Builder_add_enum, 1); rb_define_method(klass, "add_enum", Builder_add_enum, 1);
rb_define_method(klass, "finalize_to_pool", Builder_finalize_to_pool, 1);
rb_gc_register_address(&cBuilder); rb_gc_register_address(&cBuilder);
cBuilder = klass; cBuilder = klass;
} }
/* /*
* call-seq: * call-seq:
* Builder.new(descriptor_pool) => builder * Builder.new
* *
* Creates a new Builder. A Builder can accumulate a set of new message and enum * Initializes a new builder.
* descriptors and atomically register them into a pool in a way that allows for
* (co)recursive type references.
*/ */
VALUE Builder_initialize(VALUE _self, VALUE pool) { VALUE Builder_initialize(VALUE _self) {
DEFINE_SELF(Builder, self, _self); DEFINE_SELF(Builder, self, _self);
self->descriptor_pool = pool; self->pending_list = rb_ary_new();
self->default_file_builder = Qnil; // Created lazily if needed. VALUE file_name = Qnil;
self->default_file_descriptor =
rb_class_new_instance(1, &file_name, cFileDescriptor);
return Qnil; return Qnil;
} }
...@@ -2120,34 +2123,17 @@ VALUE Builder_initialize(VALUE _self, VALUE pool) { ...@@ -2120,34 +2123,17 @@ VALUE Builder_initialize(VALUE _self, VALUE pool) {
*/ */
VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) { VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
DEFINE_SELF(Builder, self, _self); DEFINE_SELF(Builder, self, _self);
VALUE name, options; VALUE file_descriptor = rb_class_new_instance(argc, argv, cFileDescriptor);
VALUE args[2] = { file_descriptor, _self };
rb_scan_args(argc, argv, "11", &name, &options); VALUE ctx = rb_class_new_instance(2, args, cFileBuilderContext);
VALUE args[3] = { self->descriptor_pool, name, options };
VALUE ctx = rb_class_new_instance(3, args, cFileBuilderContext);
VALUE block = rb_block_proc(); VALUE block = rb_block_proc();
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block); rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
FileBuilderContext_build(ctx);
rb_ary_concat(self->pending_list,
FileBuilderContext_pending_descriptors(ctx));
return Qnil; return Qnil;
} }
static VALUE Builder_get_default_file(VALUE _self) {
DEFINE_SELF(Builder, self, _self);
/* Lazily create only if legacy builder-level methods are called. */
if (self->default_file_builder == Qnil) {
VALUE name = rb_str_new2("ruby_default_file.proto");
VALUE args [3] = { self->descriptor_pool, name, rb_hash_new() };
self->default_file_builder =
rb_class_new_instance(3, args, cFileBuilderContext);
}
return self->default_file_builder;
}
/* /*
* call-seq: * call-seq:
* Builder.add_message(name, &block) * Builder.add_message(name, &block)
...@@ -2161,9 +2147,14 @@ static VALUE Builder_get_default_file(VALUE _self) { ...@@ -2161,9 +2147,14 @@ static VALUE Builder_get_default_file(VALUE _self) {
*/ */
VALUE Builder_add_message(VALUE _self, VALUE name) { VALUE Builder_add_message(VALUE _self, VALUE name) {
DEFINE_SELF(Builder, self, _self); DEFINE_SELF(Builder, self, _self);
VALUE file_builder = Builder_get_default_file(_self); VALUE msgdef =
rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name, rb_class_new_instance(1, &self->default_file_descriptor, cDescriptor);
rb_block_proc()); VALUE args[2] = { msgdef, _self };
VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
VALUE block = rb_block_proc();
rb_funcall(msgdef, rb_intern("name="), 1, name);
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
rb_ary_push(self->pending_list, msgdef);
return Qnil; return Qnil;
} }
...@@ -2181,60 +2172,86 @@ VALUE Builder_add_message(VALUE _self, VALUE name) { ...@@ -2181,60 +2172,86 @@ VALUE Builder_add_message(VALUE _self, VALUE name) {
*/ */
VALUE Builder_add_enum(VALUE _self, VALUE name) { VALUE Builder_add_enum(VALUE _self, VALUE name) {
DEFINE_SELF(Builder, self, _self); DEFINE_SELF(Builder, self, _self);
VALUE file_builder = Builder_get_default_file(_self); VALUE enumdef =
rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name, rb_class_new_instance(1, &self->default_file_descriptor, cEnumDescriptor);
rb_block_proc()); VALUE ctx = rb_class_new_instance(1, &enumdef, cEnumBuilderContext);
VALUE block = rb_block_proc();
rb_funcall(enumdef, rb_intern("name="), 1, name);
rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
rb_ary_push(self->pending_list, enumdef);
return Qnil; return Qnil;
} }
/* This method is hidden from Ruby, and only called directly from static void proto3_validate_msgdef(const upb_msgdef* msgdef) {
* DescriptorPool_build(). */ // Verify that no required fields exist. proto3 does not support these.
VALUE Builder_build(VALUE _self) { upb_msg_field_iter it;
DEFINE_SELF(Builder, self, _self); for (upb_msg_field_begin(&it, msgdef);
!upb_msg_field_done(&it);
if (self->default_file_builder != Qnil) { upb_msg_field_next(&it)) {
FileBuilderContext_build(self->default_file_builder); const upb_fielddef* field = upb_msg_iter_field(&it);
self->default_file_builder = Qnil; if (upb_fielddef_label(field) == UPB_LABEL_REQUIRED) {
rb_raise(cTypeError, "Required fields are unsupported in proto3.");
}
} }
}
return Qnil; static void proto3_validate_enumdef(const upb_enumdef* enumdef) {
// Verify that an entry exists with integer value 0. (This is the default
// value.)
const char* lookup = upb_enumdef_iton(enumdef, 0);
if (lookup == NULL) {
rb_raise(cTypeError,
"Enum definition does not contain a value for '0'.");
}
} }
static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) { /*
DEFINE_SELF(DescriptorPool, descriptor_pool, _descriptor_pool); * call-seq:
VALUE key = ULL2NUM((intptr_t)ptr); * Builder.finalize_to_pool(pool)
VALUE def = rb_hash_aref(descriptor_pool->def_to_descriptor, key); *
* Adds all accumulated message and enum descriptors created in this builder
* context to the given pool. The operation occurs atomically, and all
* descriptors can refer to each other (including in cycles). This is the only
* way to build (co)recursive message definitions.
*
* This method is usually called automatically by DescriptorPool#build after it
* invokes the given user block in the context of the builder. The user should
* not normally need to call this manually because a Builder is not normally
* created manually.
*/
VALUE Builder_finalize_to_pool(VALUE _self, VALUE pool_rb) {
DEFINE_SELF(Builder, self, _self);
if (ptr == NULL) { DescriptorPool* pool = ruby_to_DescriptorPool(pool_rb);
return Qnil;
}
if (def == Qnil) { REALLOC_N(self->defs, upb_def*, RARRAY_LEN(self->pending_list));
// Lazily create wrapper object.
VALUE args[3] = { c_only_cookie, _descriptor_pool, key };
def = rb_class_new_instance(3, args, klass);
rb_hash_aset(descriptor_pool->def_to_descriptor, key, def);
}
return def; for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
} VALUE def_rb = rb_ary_entry(self->pending_list, i);
if (CLASS_OF(def_rb) == cDescriptor) {
self->defs[i] = (upb_def*)ruby_to_Descriptor(def_rb)->msgdef;
VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) { if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) {
return get_def_obj(descriptor_pool, def, cDescriptor); proto3_validate_msgdef((const upb_msgdef*)self->defs[i]);
} }
} else if (CLASS_OF(def_rb) == cEnumDescriptor) {
self->defs[i] = (upb_def*)ruby_to_EnumDescriptor(def_rb)->enumdef;
VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) { if (upb_filedef_syntax(upb_def_file(self->defs[i])) == UPB_SYNTAX_PROTO3) {
return get_def_obj(descriptor_pool, def, cEnumDescriptor); proto3_validate_enumdef((const upb_enumdef*)self->defs[i]);
} }
}
}
VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) { CHECK_UPB(upb_symtab_add(pool->symtab, (upb_def**)self->defs,
return get_def_obj(descriptor_pool, def, cFieldDescriptor); RARRAY_LEN(self->pending_list), NULL, &status),
} "Unable to add defs to DescriptorPool");
VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) { for (int i = 0; i < RARRAY_LEN(self->pending_list); i++) {
return get_def_obj(descriptor_pool, def, cFileDescriptor); VALUE def_rb = rb_ary_entry(self->pending_list, i);
} add_def_obj(self->defs[i], def_rb);
}
VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) { self->pending_list = rb_ary_new();
return get_def_obj(descriptor_pool, def, cOneofDescriptor); return Qnil;
} }
...@@ -117,18 +117,18 @@ static const void* newhandlerdata(upb_handlers* h, uint32_t ofs, int32_t hasbit) ...@@ -117,18 +117,18 @@ static const void* newhandlerdata(upb_handlers* h, uint32_t ofs, int32_t hasbit)
typedef struct { typedef struct {
size_t ofs; size_t ofs;
int32_t hasbit; int32_t hasbit;
VALUE subklass; const upb_msgdef *md;
} submsg_handlerdata_t; } submsg_handlerdata_t;
// Creates a handlerdata that contains offset and submessage type information. // Creates a handlerdata that contains offset and submessage type information.
static const void *newsubmsghandlerdata(upb_handlers* h, static const void *newsubmsghandlerdata(upb_handlers* h,
uint32_t ofs, uint32_t ofs,
int32_t hasbit, int32_t hasbit,
VALUE subklass) { const upb_fielddef* f) {
submsg_handlerdata_t *hd = ALLOC(submsg_handlerdata_t); submsg_handlerdata_t *hd = ALLOC(submsg_handlerdata_t);
hd->ofs = ofs; hd->ofs = ofs;
hd->hasbit = hasbit; hd->hasbit = hasbit;
hd->subklass = subklass; hd->md = upb_fielddef_msgsubdef(f);
upb_handlers_addcleanup(h, hd, xfree); upb_handlers_addcleanup(h, hd, xfree);
return hd; return hd;
} }
...@@ -137,14 +137,13 @@ typedef struct { ...@@ -137,14 +137,13 @@ typedef struct {
size_t ofs; // union data slot size_t ofs; // union data slot
size_t case_ofs; // oneof_case field size_t case_ofs; // oneof_case field
uint32_t oneof_case_num; // oneof-case number to place in oneof_case field uint32_t oneof_case_num; // oneof-case number to place in oneof_case field
VALUE subklass; const upb_msgdef *md; // msgdef, for oneof submessage handler
} oneof_handlerdata_t; } oneof_handlerdata_t;
static const void *newoneofhandlerdata(upb_handlers *h, static const void *newoneofhandlerdata(upb_handlers *h,
uint32_t ofs, uint32_t ofs,
uint32_t case_ofs, uint32_t case_ofs,
const upb_fielddef *f, const upb_fielddef *f) {
const Descriptor* desc) {
oneof_handlerdata_t *hd = ALLOC(oneof_handlerdata_t); oneof_handlerdata_t *hd = ALLOC(oneof_handlerdata_t);
hd->ofs = ofs; hd->ofs = ofs;
hd->case_ofs = case_ofs; hd->case_ofs = case_ofs;
...@@ -155,7 +154,11 @@ static const void *newoneofhandlerdata(upb_handlers *h, ...@@ -155,7 +154,11 @@ static const void *newoneofhandlerdata(upb_handlers *h,
// create a separate ID space. In addition, using the field tag number here // create a separate ID space. In addition, using the field tag number here
// lets us easily look up the field in the oneof accessor. // lets us easily look up the field in the oneof accessor.
hd->oneof_case_num = upb_fielddef_number(f); hd->oneof_case_num = upb_fielddef_number(f);
hd->subklass = field_type_class(desc->layout, f); if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE) {
hd->md = upb_fielddef_msgsubdef(f);
} else {
hd->md = NULL;
}
upb_handlers_addcleanup(h, hd, xfree); upb_handlers_addcleanup(h, hd, xfree);
return hd; return hd;
} }
...@@ -251,13 +254,13 @@ static size_t stringdata_handler(void* closure, const void* hd, ...@@ -251,13 +254,13 @@ static size_t stringdata_handler(void* closure, const void* hd,
} }
static bool stringdata_end_handler(void* closure, const void* hd) { static bool stringdata_end_handler(void* closure, const void* hd) {
VALUE rb_str = (VALUE)closure; VALUE rb_str = closure;
rb_obj_freeze(rb_str); rb_obj_freeze(rb_str);
return true; return true;
} }
static bool appendstring_end_handler(void* closure, const void* hd) { static bool appendstring_end_handler(void* closure, const void* hd) {
VALUE rb_str = (VALUE)closure; VALUE rb_str = closure;
rb_obj_freeze(rb_str); rb_obj_freeze(rb_str);
return true; return true;
} }
...@@ -266,9 +269,12 @@ static bool appendstring_end_handler(void* closure, const void* hd) { ...@@ -266,9 +269,12 @@ static bool appendstring_end_handler(void* closure, const void* hd) {
static void *appendsubmsg_handler(void *closure, const void *hd) { static void *appendsubmsg_handler(void *closure, const void *hd) {
VALUE ary = (VALUE)closure; VALUE ary = (VALUE)closure;
const submsg_handlerdata_t *submsgdata = hd; const submsg_handlerdata_t *submsgdata = hd;
VALUE subdesc =
get_def_obj((void*)submsgdata->md);
VALUE subklass = Descriptor_msgclass(subdesc);
MessageHeader* submsg; MessageHeader* submsg;
VALUE submsg_rb = rb_class_new_instance(0, NULL, submsgdata->subklass); VALUE submsg_rb = rb_class_new_instance(0, NULL, subklass);
RepeatedField_push(ary, submsg_rb); RepeatedField_push(ary, submsg_rb);
TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg); TypedData_Get_Struct(submsg_rb, MessageHeader, &Message_type, submsg);
...@@ -279,12 +285,15 @@ static void *appendsubmsg_handler(void *closure, const void *hd) { ...@@ -279,12 +285,15 @@ static void *appendsubmsg_handler(void *closure, const void *hd) {
static void *submsg_handler(void *closure, const void *hd) { static void *submsg_handler(void *closure, const void *hd) {
MessageHeader* msg = closure; MessageHeader* msg = closure;
const submsg_handlerdata_t* submsgdata = hd; const submsg_handlerdata_t* submsgdata = hd;
VALUE subdesc =
get_def_obj((void*)submsgdata->md);
VALUE subklass = Descriptor_msgclass(subdesc);
VALUE submsg_rb; VALUE submsg_rb;
MessageHeader* submsg; MessageHeader* submsg;
if (DEREF(msg, submsgdata->ofs, VALUE) == Qnil) { if (DEREF(msg, submsgdata->ofs, VALUE) == Qnil) {
DEREF(msg, submsgdata->ofs, VALUE) = DEREF(msg, submsgdata->ofs, VALUE) =
rb_class_new_instance(0, NULL, submsgdata->subklass); rb_class_new_instance(0, NULL, subklass);
} }
set_hasbit(closure, submsgdata->hasbit); set_hasbit(closure, submsgdata->hasbit);
...@@ -300,7 +309,11 @@ typedef struct { ...@@ -300,7 +309,11 @@ typedef struct {
size_t ofs; size_t ofs;
upb_fieldtype_t key_field_type; upb_fieldtype_t key_field_type;
upb_fieldtype_t value_field_type; upb_fieldtype_t value_field_type;
VALUE subklass;
// We know that we can hold this reference because the handlerdata has the
// same lifetime as the upb_handlers struct, and the upb_handlers struct holds
// a reference to the upb_msgdef, which in turn has references to its subdefs.
const upb_def* value_field_subdef;
} map_handlerdata_t; } map_handlerdata_t;
// Temporary frame for map parsing: at the beginning of a map entry message, a // Temporary frame for map parsing: at the beginning of a map entry message, a
...@@ -375,7 +388,7 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) { ...@@ -375,7 +388,7 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) {
if (mapdata->value_field_type == UPB_TYPE_MESSAGE || if (mapdata->value_field_type == UPB_TYPE_MESSAGE ||
mapdata->value_field_type == UPB_TYPE_ENUM) { mapdata->value_field_type == UPB_TYPE_ENUM) {
value_field_typeclass = mapdata->subklass; value_field_typeclass = get_def_obj(mapdata->value_field_subdef);
} }
value = native_slot_get( value = native_slot_get(
...@@ -398,7 +411,7 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) { ...@@ -398,7 +411,7 @@ static bool endmap_handler(void *closure, const void *hd, upb_status* s) {
static map_handlerdata_t* new_map_handlerdata( static map_handlerdata_t* new_map_handlerdata(
size_t ofs, size_t ofs,
const upb_msgdef* mapentry_def, const upb_msgdef* mapentry_def,
const Descriptor* desc) { Descriptor* desc) {
const upb_fielddef* key_field; const upb_fielddef* key_field;
const upb_fielddef* value_field; const upb_fielddef* value_field;
map_handlerdata_t* hd = ALLOC(map_handlerdata_t); map_handlerdata_t* hd = ALLOC(map_handlerdata_t);
...@@ -409,7 +422,7 @@ static map_handlerdata_t* new_map_handlerdata( ...@@ -409,7 +422,7 @@ static map_handlerdata_t* new_map_handlerdata(
value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD); value_field = upb_msgdef_itof(mapentry_def, MAP_VALUE_FIELD);
assert(value_field != NULL); assert(value_field != NULL);
hd->value_field_type = upb_fielddef_type(value_field); hd->value_field_type = upb_fielddef_type(value_field);
hd->subklass = field_type_class(desc->layout, value_field); hd->value_field_subdef = upb_fielddef_subdef(value_field);
return hd; return hd;
} }
...@@ -475,13 +488,16 @@ static void *oneofsubmsg_handler(void *closure, ...@@ -475,13 +488,16 @@ static void *oneofsubmsg_handler(void *closure,
const oneof_handlerdata_t *oneofdata = hd; const oneof_handlerdata_t *oneofdata = hd;
uint32_t oldcase = DEREF(msg, oneofdata->case_ofs, uint32_t); uint32_t oldcase = DEREF(msg, oneofdata->case_ofs, uint32_t);
VALUE subdesc =
get_def_obj((void*)oneofdata->md);
VALUE subklass = Descriptor_msgclass(subdesc);
VALUE submsg_rb; VALUE submsg_rb;
MessageHeader* submsg; MessageHeader* submsg;
if (oldcase != oneofdata->oneof_case_num || if (oldcase != oneofdata->oneof_case_num ||
DEREF(msg, oneofdata->ofs, VALUE) == Qnil) { DEREF(msg, oneofdata->ofs, VALUE) == Qnil) {
DEREF(msg, oneofdata->ofs, VALUE) = DEREF(msg, oneofdata->ofs, VALUE) =
rb_class_new_instance(0, NULL, oneofdata->subklass); rb_class_new_instance(0, NULL, subklass);
} }
// Set the oneof case *after* allocating the new class instance -- otherwise, // Set the oneof case *after* allocating the new class instance -- otherwise,
// if the Ruby GC is invoked as part of a call into the VM, it might invoke // if the Ruby GC is invoked as part of a call into the VM, it might invoke
...@@ -499,12 +515,12 @@ static void *oneofsubmsg_handler(void *closure, ...@@ -499,12 +515,12 @@ static void *oneofsubmsg_handler(void *closure,
// Set up handlers for a repeated field. // Set up handlers for a repeated field.
static void add_handlers_for_repeated_field(upb_handlers *h, static void add_handlers_for_repeated_field(upb_handlers *h,
const Descriptor* desc,
const upb_fielddef *f, const upb_fielddef *f,
size_t offset) { size_t offset) {
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
attr.handler_data = newhandlerdata(h, offset, -1); upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset, -1));
upb_handlers_setstartseq(h, f, startseq_handler, &attr); upb_handlers_setstartseq(h, f, startseq_handler, &attr);
upb_handlerattr_uninit(&attr);
switch (upb_fielddef_type(f)) { switch (upb_fielddef_type(f)) {
...@@ -535,20 +551,20 @@ static void add_handlers_for_repeated_field(upb_handlers *h, ...@@ -535,20 +551,20 @@ static void add_handlers_for_repeated_field(upb_handlers *h,
break; break;
} }
case UPB_TYPE_MESSAGE: { case UPB_TYPE_MESSAGE: {
VALUE subklass = field_type_class(desc->layout, f); upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr_sethandlerdata(&attr, newsubmsghandlerdata(h, 0, -1, f));
attr.handler_data = newsubmsghandlerdata(h, 0, -1, subklass);
upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr); upb_handlers_setstartsubmsg(h, f, appendsubmsg_handler, &attr);
upb_handlerattr_uninit(&attr);
break; break;
} }
} }
} }
// Set up handlers for a singular field. // Set up handlers for a singular field.
static void add_handlers_for_singular_field(const Descriptor* desc, static void add_handlers_for_singular_field(upb_handlers *h,
upb_handlers* h, const upb_fielddef *f,
const upb_fielddef* f, size_t offset,
size_t offset, size_t hasbit_off) { size_t hasbit_off) {
// The offset we pass to UPB points to the start of the Message, // The offset we pass to UPB points to the start of the Message,
// rather than the start of where our data is stored. // rather than the start of where our data is stored.
int32_t hasbit = -1; int32_t hasbit = -1;
...@@ -570,20 +586,23 @@ static void add_handlers_for_singular_field(const Descriptor* desc, ...@@ -570,20 +586,23 @@ static void add_handlers_for_singular_field(const Descriptor* desc,
case UPB_TYPE_STRING: case UPB_TYPE_STRING:
case UPB_TYPE_BYTES: { case UPB_TYPE_BYTES: {
bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES; bool is_bytes = upb_fielddef_type(f) == UPB_TYPE_BYTES;
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
attr.handler_data = newhandlerdata(h, offset, hasbit); upb_handlerattr_sethandlerdata(&attr, newhandlerdata(h, offset, hasbit));
upb_handlers_setstartstr(h, f, upb_handlers_setstartstr(h, f,
is_bytes ? bytes_handler : str_handler, is_bytes ? bytes_handler : str_handler,
&attr); &attr);
upb_handlers_setstring(h, f, stringdata_handler, &attr); upb_handlers_setstring(h, f, stringdata_handler, &attr);
upb_handlers_setendstr(h, f, stringdata_end_handler, &attr); upb_handlers_setendstr(h, f, stringdata_end_handler, &attr);
upb_handlerattr_uninit(&attr);
break; break;
} }
case UPB_TYPE_MESSAGE: { case UPB_TYPE_MESSAGE: {
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
attr.handler_data = newsubmsghandlerdata( upb_handlerattr_sethandlerdata(&attr,
h, offset, hasbit, field_type_class(desc->layout, f)); newsubmsghandlerdata(h, offset,
hasbit, f));
upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr); upb_handlers_setstartsubmsg(h, f, submsg_handler, &attr);
upb_handlerattr_uninit(&attr);
break; break;
} }
} }
...@@ -593,34 +612,36 @@ static void add_handlers_for_singular_field(const Descriptor* desc, ...@@ -593,34 +612,36 @@ static void add_handlers_for_singular_field(const Descriptor* desc,
static void add_handlers_for_mapfield(upb_handlers* h, static void add_handlers_for_mapfield(upb_handlers* h,
const upb_fielddef* fielddef, const upb_fielddef* fielddef,
size_t offset, size_t offset,
const Descriptor* desc) { Descriptor* desc) {
const upb_msgdef* map_msgdef = upb_fielddef_msgsubdef(fielddef); const upb_msgdef* map_msgdef = upb_fielddef_msgsubdef(fielddef);
map_handlerdata_t* hd = new_map_handlerdata(offset, map_msgdef, desc); map_handlerdata_t* hd = new_map_handlerdata(offset, map_msgdef, desc);
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
upb_handlers_addcleanup(h, hd, xfree); upb_handlers_addcleanup(h, hd, xfree);
attr.handler_data = hd; upb_handlerattr_sethandlerdata(&attr, hd);
upb_handlers_setstartsubmsg(h, fielddef, startmapentry_handler, &attr); upb_handlers_setstartsubmsg(h, fielddef, startmapentry_handler, &attr);
upb_handlerattr_uninit(&attr);
} }
// Adds handlers to a map-entry msgdef. // Adds handlers to a map-entry msgdef.
static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h, static void add_handlers_for_mapentry(const upb_msgdef* msgdef,
const Descriptor* desc) { upb_handlers* h,
Descriptor* desc) {
const upb_fielddef* key_field = map_entry_key(msgdef); const upb_fielddef* key_field = map_entry_key(msgdef);
const upb_fielddef* value_field = map_entry_value(msgdef); const upb_fielddef* value_field = map_entry_value(msgdef);
map_handlerdata_t* hd = new_map_handlerdata(0, msgdef, desc); map_handlerdata_t* hd = new_map_handlerdata(0, msgdef, desc);
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
upb_handlers_addcleanup(h, hd, xfree); upb_handlers_addcleanup(h, hd, xfree);
attr.handler_data = hd; upb_handlerattr_sethandlerdata(&attr, hd);
upb_handlers_setendmsg(h, endmap_handler, &attr); upb_handlers_setendmsg(h, endmap_handler, &attr);
add_handlers_for_singular_field( add_handlers_for_singular_field(
desc, h, key_field, h, key_field,
offsetof(map_parse_frame_t, key_storage), offsetof(map_parse_frame_t, key_storage),
MESSAGE_FIELD_NO_HASBIT); MESSAGE_FIELD_NO_HASBIT);
add_handlers_for_singular_field( add_handlers_for_singular_field(
desc, h, value_field, h, value_field,
offsetof(map_parse_frame_t, value_storage), offsetof(map_parse_frame_t, value_storage),
MESSAGE_FIELD_NO_HASBIT); MESSAGE_FIELD_NO_HASBIT);
} }
...@@ -629,11 +650,11 @@ static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h, ...@@ -629,11 +650,11 @@ static void add_handlers_for_mapentry(const upb_msgdef* msgdef, upb_handlers* h,
static void add_handlers_for_oneof_field(upb_handlers *h, static void add_handlers_for_oneof_field(upb_handlers *h,
const upb_fielddef *f, const upb_fielddef *f,
size_t offset, size_t offset,
size_t oneof_case_offset, size_t oneof_case_offset) {
const Descriptor* desc) {
upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
attr.handler_data = upb_handlerattr_sethandlerdata(
newoneofhandlerdata(h, offset, oneof_case_offset, f, desc); &attr, newoneofhandlerdata(h, offset, oneof_case_offset, f));
switch (upb_fielddef_type(f)) { switch (upb_fielddef_type(f)) {
...@@ -668,6 +689,8 @@ static void add_handlers_for_oneof_field(upb_handlers *h, ...@@ -668,6 +689,8 @@ static void add_handlers_for_oneof_field(upb_handlers *h,
break; break;
} }
} }
upb_handlerattr_uninit(&attr);
} }
static bool unknown_field_handler(void* closure, const void* hd, static bool unknown_field_handler(void* closure, const void* hd,
...@@ -685,21 +708,11 @@ static bool unknown_field_handler(void* closure, const void* hd, ...@@ -685,21 +708,11 @@ static bool unknown_field_handler(void* closure, const void* hd,
return true; return true;
} }
void add_handlers_for_message(const void *closure, upb_handlers *h) { static void add_handlers_for_message(const void *closure, upb_handlers *h) {
const VALUE descriptor_pool = (VALUE)closure;
const upb_msgdef* msgdef = upb_handlers_msgdef(h); const upb_msgdef* msgdef = upb_handlers_msgdef(h);
Descriptor* desc = Descriptor* desc = ruby_to_Descriptor(get_def_obj((void*)msgdef));
ruby_to_Descriptor(get_msgdef_obj(descriptor_pool, msgdef));
upb_msg_field_iter i; upb_msg_field_iter i;
// Ensure layout exists. We may be invoked to create handlers for a given
// message if we are included as a submsg of another message type before our
// class is actually built, so to work around this, we just create the layout
// (and handlers, in the class-building function) on-demand.
if (desc->layout == NULL) {
desc->layout = create_layout(desc);
}
// If this is a mapentry message type, set up a special set of handlers and // If this is a mapentry message type, set up a special set of handlers and
// bail out of the normal (user-defined) message type handling. // bail out of the normal (user-defined) message type handling.
if (upb_msgdef_mapentry(msgdef)) { if (upb_msgdef_mapentry(msgdef)) {
...@@ -707,7 +720,15 @@ void add_handlers_for_message(const void *closure, upb_handlers *h) { ...@@ -707,7 +720,15 @@ void add_handlers_for_message(const void *closure, upb_handlers *h) {
return; return;
} }
upb_handlerattr attr = UPB_HANDLERATTR_INIT; // Ensure layout exists. We may be invoked to create handlers for a given
// message if we are included as a submsg of another message type before our
// class is actually built, so to work around this, we just create the layout
// (and handlers, in the class-building function) on-demand.
if (desc->layout == NULL) {
desc->layout = create_layout(desc->msgdef);
}
upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
upb_handlers_setunknown(h, unknown_field_handler, &attr); upb_handlers_setunknown(h, unknown_field_handler, &attr);
for (upb_msg_field_begin(&i, desc->msgdef); for (upb_msg_field_begin(&i, desc->msgdef);
...@@ -721,51 +742,64 @@ void add_handlers_for_message(const void *closure, upb_handlers *h) { ...@@ -721,51 +742,64 @@ void add_handlers_for_message(const void *closure, upb_handlers *h) {
size_t oneof_case_offset = size_t oneof_case_offset =
desc->layout->fields[upb_fielddef_index(f)].case_offset + desc->layout->fields[upb_fielddef_index(f)].case_offset +
sizeof(MessageHeader); sizeof(MessageHeader);
add_handlers_for_oneof_field(h, f, offset, oneof_case_offset, desc); add_handlers_for_oneof_field(h, f, offset, oneof_case_offset);
} else if (is_map_field(f)) { } else if (is_map_field(f)) {
add_handlers_for_mapfield(h, f, offset, desc); add_handlers_for_mapfield(h, f, offset, desc);
} else if (upb_fielddef_isseq(f)) { } else if (upb_fielddef_isseq(f)) {
add_handlers_for_repeated_field(h, desc, f, offset); add_handlers_for_repeated_field(h, f, offset);
} else { } else {
add_handlers_for_singular_field( add_handlers_for_singular_field(
desc, h, f, offset, h, f, offset, desc->layout->fields[upb_fielddef_index(f)].hasbit);
desc->layout->fields[upb_fielddef_index(f)].hasbit);
} }
} }
} }
// Creates upb handlers for populating a message.
static const upb_handlers *new_fill_handlers(Descriptor* desc,
const void* owner) {
// TODO(cfallin, haberman): once upb gets a caching/memoization layer for
// handlers, reuse subdef handlers so that e.g. if we already parse
// B-with-field-of-type-C, we don't have to rebuild the whole hierarchy to
// parse A-with-field-of-type-B-with-field-of-type-C.
return upb_handlers_newfrozen(desc->msgdef, owner,
add_handlers_for_message, NULL);
}
// Constructs the handlers for filling a message's data into an in-memory // Constructs the handlers for filling a message's data into an in-memory
// object. // object.
const upb_handlers* get_fill_handlers(Descriptor* desc) { const upb_handlers* get_fill_handlers(Descriptor* desc) {
DescriptorPool* pool = ruby_to_DescriptorPool(desc->descriptor_pool); if (!desc->fill_handlers) {
return upb_handlercache_get(pool->fill_handler_cache, desc->msgdef); desc->fill_handlers =
new_fill_handlers(desc, &desc->fill_handlers);
}
return desc->fill_handlers;
} }
static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) { // Constructs the upb decoder method for parsing messages of this type.
DescriptorPool* pool = ruby_to_DescriptorPool(desc->descriptor_pool); // This is called from the message class creation code.
return upb_pbcodecache_get(pool->fill_method_cache, desc->msgdef); const upb_pbdecodermethod *new_fillmsg_decodermethod(Descriptor* desc,
} const void* owner) {
const upb_handlers* handlers = get_fill_handlers(desc);
upb_pbdecodermethodopts opts;
upb_pbdecodermethodopts_init(&opts, handlers);
static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) { return upb_pbdecodermethod_new(&opts, owner);
DescriptorPool* pool = ruby_to_DescriptorPool(desc->descriptor_pool);
return upb_json_codecache_get(pool->json_fill_method_cache, desc->msgdef);
} }
static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) { static const upb_pbdecodermethod *msgdef_decodermethod(Descriptor* desc) {
DescriptorPool* pool = ruby_to_DescriptorPool(desc->descriptor_pool); if (desc->fill_method == NULL) {
return upb_handlercache_get(pool->pb_serialize_handler_cache, desc->msgdef); desc->fill_method = new_fillmsg_decodermethod(
desc, &desc->fill_method);
}
return desc->fill_method;
} }
static const upb_handlers* msgdef_json_serialize_handlers( static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) {
Descriptor* desc, bool preserve_proto_fieldnames) { if (desc->json_fill_method == NULL) {
DescriptorPool* pool = ruby_to_DescriptorPool(desc->descriptor_pool); desc->json_fill_method =
if (preserve_proto_fieldnames) { upb_json_parsermethod_new(desc->msgdef, &desc->json_fill_method);
return upb_handlercache_get(pool->json_serialize_handler_preserve_cache,
desc->msgdef);
} else {
return upb_handlercache_get(pool->json_serialize_handler_cache,
desc->msgdef);
} }
return desc->json_fill_method;
} }
...@@ -775,8 +809,7 @@ static const upb_handlers* msgdef_json_serialize_handlers( ...@@ -775,8 +809,7 @@ static const upb_handlers* msgdef_json_serialize_handlers(
// if any error occurs. // if any error occurs.
#define STACK_ENV_STACKBYTES 4096 #define STACK_ENV_STACKBYTES 4096
typedef struct { typedef struct {
upb_arena *arena; upb_env env;
upb_status status;
const char* ruby_error_template; const char* ruby_error_template;
char allocbuf[STACK_ENV_STACKBYTES]; char allocbuf[STACK_ENV_STACKBYTES];
} stackenv; } stackenv;
...@@ -784,22 +817,29 @@ typedef struct { ...@@ -784,22 +817,29 @@ typedef struct {
static void stackenv_init(stackenv* se, const char* errmsg); static void stackenv_init(stackenv* se, const char* errmsg);
static void stackenv_uninit(stackenv* se); static void stackenv_uninit(stackenv* se);
// Callback invoked by upb if any error occurs during parsing or serialization.
static bool env_error_func(void* ud, const upb_status* status) {
stackenv* se = ud;
// Free the env -- rb_raise will longjmp up the stack past the encode/decode
// function so it would not otherwise have been freed.
stackenv_uninit(se);
// TODO(haberman): have a way to verify that this is actually a parse error,
// instead of just throwing "parse error" unconditionally.
rb_raise(cParseError, se->ruby_error_template, upb_status_errmsg(status));
// Never reached: rb_raise() always longjmp()s up the stack, past all of our
// code, back to Ruby.
return false;
}
static void stackenv_init(stackenv* se, const char* errmsg) { static void stackenv_init(stackenv* se, const char* errmsg) {
se->ruby_error_template = errmsg; se->ruby_error_template = errmsg;
se->arena = upb_env_init2(&se->env, se->allocbuf, sizeof(se->allocbuf), NULL);
upb_arena_init(se->allocbuf, sizeof(se->allocbuf), &upb_alloc_global); upb_env_seterrorfunc(&se->env, env_error_func, se);
upb_status_clear(&se->status);
} }
static void stackenv_uninit(stackenv* se) { static void stackenv_uninit(stackenv* se) {
upb_arena_free(se->arena); upb_env_uninit(&se->env);
if (!upb_ok(&se->status)) {
// TODO(haberman): have a way to verify that this is actually a parse error,
// instead of just throwing "parse error" unconditionally.
VALUE errmsg = rb_str_new2(upb_status_errmsg(&se->status));
rb_raise(cParseError, se->ruby_error_template, errmsg);
}
} }
/* /*
...@@ -830,10 +870,10 @@ VALUE Message_decode(VALUE klass, VALUE data) { ...@@ -830,10 +870,10 @@ VALUE Message_decode(VALUE klass, VALUE data) {
stackenv se; stackenv se;
upb_sink sink; upb_sink sink;
upb_pbdecoder* decoder; upb_pbdecoder* decoder;
stackenv_init(&se, "Error occurred during parsing: %" PRIsVALUE); stackenv_init(&se, "Error occurred during parsing: %s");
upb_sink_reset(&sink, h, msg); upb_sink_reset(&sink, h, msg);
decoder = upb_pbdecoder_create(se.arena, method, sink, &se.status); decoder = upb_pbdecoder_create(&se.env, method, &sink);
upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data), upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
upb_pbdecoder_input(decoder)); upb_pbdecoder_input(decoder));
...@@ -852,8 +892,7 @@ VALUE Message_decode(VALUE klass, VALUE data) { ...@@ -852,8 +892,7 @@ VALUE Message_decode(VALUE klass, VALUE data) {
* and returns a message object with the corresponding field values. * and returns a message object with the corresponding field values.
* *
* @param options [Hash] options for the decoder * @param options [Hash] options for the decoder
* ignore_unknown_fields: set true to ignore unknown fields (default is to * ignore_unknown_fields: set true to ignore unknown fields (default is to raise an error)
* raise an error)
*/ */
VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) { VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned); VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
...@@ -881,7 +920,6 @@ VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) { ...@@ -881,7 +920,6 @@ VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
if (TYPE(data) != T_STRING) { if (TYPE(data) != T_STRING) {
rb_raise(rb_eArgError, "Expected string for JSON data."); rb_raise(rb_eArgError, "Expected string for JSON data.");
} }
// TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to // TODO(cfallin): Check and respect string encoding. If not UTF-8, we need to
// convert, because string handlers pass data directly to message string // convert, because string handlers pass data directly to message string
// fields. // fields.
...@@ -895,11 +933,11 @@ VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) { ...@@ -895,11 +933,11 @@ VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
upb_sink sink; upb_sink sink;
upb_json_parser* parser; upb_json_parser* parser;
DescriptorPool* pool = ruby_to_DescriptorPool(generated_pool); DescriptorPool* pool = ruby_to_DescriptorPool(generated_pool);
stackenv_init(&se, "Error occurred during parsing: %" PRIsVALUE); stackenv_init(&se, "Error occurred during parsing: %s");
upb_sink_reset(&sink, get_fill_handlers(desc), msg); upb_sink_reset(&sink, get_fill_handlers(desc), msg);
parser = upb_json_parser_create(se.arena, method, pool->symtab, sink, parser = upb_json_parser_create(&se.env, method, pool->symtab,
&se.status, RTEST(ignore_unknown_fields)); &sink, ignore_unknown_fields);
upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data), upb_bufsrc_putbuf(RSTRING_PTR(data), RSTRING_LEN(data),
upb_json_parser_input(parser)); upb_json_parser_input(parser));
...@@ -915,8 +953,9 @@ VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) { ...@@ -915,8 +953,9 @@ VALUE Message_decode_json(int argc, VALUE* argv, VALUE klass) {
/* msgvisitor *****************************************************************/ /* msgvisitor *****************************************************************/
static void putmsg(VALUE msg, const Descriptor* desc, upb_sink sink, int depth, static void putmsg(VALUE msg, const Descriptor* desc,
bool emit_defaults, bool is_json, bool open_msg); upb_sink *sink, int depth, bool emit_defaults,
bool is_json, bool open_msg);
static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) { static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) {
upb_selector_t ret; upb_selector_t ret;
...@@ -925,7 +964,7 @@ static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) { ...@@ -925,7 +964,7 @@ static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) {
return ret; return ret;
} }
static void putstr(VALUE str, const upb_fielddef *f, upb_sink sink) { static void putstr(VALUE str, const upb_fielddef *f, upb_sink *sink) {
upb_sink subsink; upb_sink subsink;
if (str == Qnil) return; if (str == Qnil) return;
...@@ -942,12 +981,12 @@ static void putstr(VALUE str, const upb_fielddef *f, upb_sink sink) { ...@@ -942,12 +981,12 @@ static void putstr(VALUE str, const upb_fielddef *f, upb_sink sink) {
upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), RSTRING_LEN(str), upb_sink_startstr(sink, getsel(f, UPB_HANDLER_STARTSTR), RSTRING_LEN(str),
&subsink); &subsink);
upb_sink_putstring(subsink, getsel(f, UPB_HANDLER_STRING), RSTRING_PTR(str), upb_sink_putstring(&subsink, getsel(f, UPB_HANDLER_STRING), RSTRING_PTR(str),
RSTRING_LEN(str), NULL); RSTRING_LEN(str), NULL);
upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR)); upb_sink_endstr(sink, getsel(f, UPB_HANDLER_ENDSTR));
} }
static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink sink, static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink *sink,
int depth, bool emit_defaults, bool is_json) { int depth, bool emit_defaults, bool is_json) {
upb_sink subsink; upb_sink subsink;
VALUE descriptor; VALUE descriptor;
...@@ -959,12 +998,12 @@ static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink sink, ...@@ -959,12 +998,12 @@ static void putsubmsg(VALUE submsg, const upb_fielddef *f, upb_sink sink,
subdesc = ruby_to_Descriptor(descriptor); subdesc = ruby_to_Descriptor(descriptor);
upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink); upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink);
putmsg(submsg, subdesc, subsink, depth + 1, emit_defaults, is_json, true); putmsg(submsg, subdesc, &subsink, depth + 1, emit_defaults, is_json, true);
upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG)); upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG));
} }
static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth, static void putary(VALUE ary, const upb_fielddef *f, upb_sink *sink,
bool emit_defaults, bool is_json) { int depth, bool emit_defaults, bool is_json) {
upb_sink subsink; upb_sink subsink;
upb_fieldtype_t type = upb_fielddef_type(f); upb_fieldtype_t type = upb_fielddef_type(f);
upb_selector_t sel = 0; upb_selector_t sel = 0;
...@@ -987,7 +1026,7 @@ static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth, ...@@ -987,7 +1026,7 @@ static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth,
switch (type) { switch (type) {
#define T(upbtypeconst, upbtype, ctype) \ #define T(upbtypeconst, upbtype, ctype) \
case upbtypeconst: \ case upbtypeconst: \
upb_sink_put##upbtype(subsink, sel, *((ctype*)memory)); \ upb_sink_put##upbtype(&subsink, sel, *((ctype *)memory)); \
break; break;
T(UPB_TYPE_FLOAT, float, float) T(UPB_TYPE_FLOAT, float, float)
...@@ -1001,10 +1040,11 @@ static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth, ...@@ -1001,10 +1040,11 @@ static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth,
case UPB_TYPE_STRING: case UPB_TYPE_STRING:
case UPB_TYPE_BYTES: case UPB_TYPE_BYTES:
putstr(*((VALUE *)memory), f, subsink); putstr(*((VALUE *)memory), f, &subsink);
break; break;
case UPB_TYPE_MESSAGE: case UPB_TYPE_MESSAGE:
putsubmsg(*((VALUE*)memory), f, subsink, depth, emit_defaults, is_json); putsubmsg(*((VALUE *)memory), f, &subsink, depth,
emit_defaults, is_json);
break; break;
#undef T #undef T
...@@ -1014,8 +1054,12 @@ static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth, ...@@ -1014,8 +1054,12 @@ static void putary(VALUE ary, const upb_fielddef* f, upb_sink sink, int depth,
upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
} }
static void put_ruby_value(VALUE value, const upb_fielddef* f, VALUE type_class, static void put_ruby_value(VALUE value,
int depth, upb_sink sink, bool emit_defaults, const upb_fielddef *f,
VALUE type_class,
int depth,
upb_sink *sink,
bool emit_defaults,
bool is_json) { bool is_json) {
if (depth > ENCODE_MAX_NESTING) { if (depth > ENCODE_MAX_NESTING) {
rb_raise(rb_eRuntimeError, rb_raise(rb_eRuntimeError,
...@@ -1065,8 +1109,8 @@ static void put_ruby_value(VALUE value, const upb_fielddef* f, VALUE type_class, ...@@ -1065,8 +1109,8 @@ static void put_ruby_value(VALUE value, const upb_fielddef* f, VALUE type_class,
} }
} }
static void putmap(VALUE map, const upb_fielddef* f, upb_sink sink, int depth, static void putmap(VALUE map, const upb_fielddef *f, upb_sink *sink,
bool emit_defaults, bool is_json) { int depth, bool emit_defaults, bool is_json) {
Map* self; Map* self;
upb_sink subsink; upb_sink subsink;
const upb_fielddef* key_field; const upb_fielddef* key_field;
...@@ -1090,17 +1134,17 @@ static void putmap(VALUE map, const upb_fielddef* f, upb_sink sink, int depth, ...@@ -1090,17 +1134,17 @@ static void putmap(VALUE map, const upb_fielddef* f, upb_sink sink, int depth,
upb_status status; upb_status status;
upb_sink entry_sink; upb_sink entry_sink;
upb_sink_startsubmsg(subsink, getsel(f, UPB_HANDLER_STARTSUBMSG), upb_sink_startsubmsg(&subsink, getsel(f, UPB_HANDLER_STARTSUBMSG),
&entry_sink); &entry_sink);
upb_sink_startmsg(entry_sink); upb_sink_startmsg(&entry_sink);
put_ruby_value(key, key_field, Qnil, depth + 1, entry_sink, emit_defaults, put_ruby_value(key, key_field, Qnil, depth + 1, &entry_sink,
is_json); emit_defaults, is_json);
put_ruby_value(value, value_field, self->value_type_class, depth + 1, put_ruby_value(value, value_field, self->value_type_class, depth + 1,
entry_sink, emit_defaults, is_json); &entry_sink, emit_defaults, is_json);
upb_sink_endmsg(entry_sink, &status); upb_sink_endmsg(&entry_sink, &status);
upb_sink_endsubmsg(subsink, getsel(f, UPB_HANDLER_ENDSUBMSG)); upb_sink_endsubmsg(&subsink, getsel(f, UPB_HANDLER_ENDSUBMSG));
} }
upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ)); upb_sink_endseq(sink, getsel(f, UPB_HANDLER_ENDSEQ));
...@@ -1109,8 +1153,8 @@ static void putmap(VALUE map, const upb_fielddef* f, upb_sink sink, int depth, ...@@ -1109,8 +1153,8 @@ static void putmap(VALUE map, const upb_fielddef* f, upb_sink sink, int depth,
static const upb_handlers* msgdef_json_serialize_handlers( static const upb_handlers* msgdef_json_serialize_handlers(
Descriptor* desc, bool preserve_proto_fieldnames); Descriptor* desc, bool preserve_proto_fieldnames);
static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink, static void putjsonany(VALUE msg_rb, const Descriptor* desc,
int depth, bool emit_defaults) { upb_sink* sink, int depth, bool emit_defaults) {
upb_status status; upb_status status;
MessageHeader* msg = NULL; MessageHeader* msg = NULL;
const upb_fielddef* type_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_TYPE); const upb_fielddef* type_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_TYPE);
...@@ -1166,7 +1210,7 @@ static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink, ...@@ -1166,7 +1210,7 @@ static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink,
value_len = RSTRING_LEN(value_str_rb); value_len = RSTRING_LEN(value_str_rb);
if (value_len > 0) { if (value_len > 0) {
VALUE payload_desc_rb = get_msgdef_obj(generated_pool, payload_type); VALUE payload_desc_rb = get_def_obj(payload_type);
Descriptor* payload_desc = ruby_to_Descriptor(payload_desc_rb); Descriptor* payload_desc = ruby_to_Descriptor(payload_desc_rb);
VALUE payload_class = Descriptor_msgclass(payload_desc_rb); VALUE payload_class = Descriptor_msgclass(payload_desc_rb);
upb_sink subsink; upb_sink subsink;
...@@ -1184,8 +1228,8 @@ static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink, ...@@ -1184,8 +1228,8 @@ static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink,
subsink.handlers = subsink.handlers =
msgdef_json_serialize_handlers(payload_desc, true); msgdef_json_serialize_handlers(payload_desc, true);
subsink.closure = sink.closure; subsink.closure = sink->closure;
putmsg(payload_msg_rb, payload_desc, subsink, depth, emit_defaults, true, putmsg(payload_msg_rb, payload_desc, &subsink, depth, emit_defaults, true,
is_wellknown); is_wellknown);
} }
} }
...@@ -1193,8 +1237,9 @@ static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink, ...@@ -1193,8 +1237,9 @@ static void putjsonany(VALUE msg_rb, const Descriptor* desc, upb_sink sink,
upb_sink_endmsg(sink, &status); upb_sink_endmsg(sink, &status);
} }
static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink sink, static void putmsg(VALUE msg_rb, const Descriptor* desc,
int depth, bool emit_defaults, bool is_json, bool open_msg) { upb_sink *sink, int depth, bool emit_defaults,
bool is_json, bool open_msg) {
MessageHeader* msg; MessageHeader* msg;
upb_msg_field_iter i; upb_msg_field_iter i;
upb_status status; upb_status status;
...@@ -1289,7 +1334,8 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink sink, ...@@ -1289,7 +1334,8 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink sink,
if (is_matching_oneof || emit_defaults || !is_default) { \ if (is_matching_oneof || emit_defaults || !is_default) { \
upb_sink_put##upbtype(sink, sel, value); \ upb_sink_put##upbtype(sink, sel, value); \
} \ } \
} break; } \
break;
switch (upb_fielddef_type(f)) { switch (upb_fielddef_type(f)) {
T(UPB_TYPE_FLOAT, float, float, 0.0) T(UPB_TYPE_FLOAT, float, float, 0.0)
...@@ -1321,6 +1367,33 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink sink, ...@@ -1321,6 +1367,33 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc, upb_sink sink,
} }
} }
static const upb_handlers* msgdef_pb_serialize_handlers(Descriptor* desc) {
if (desc->pb_serialize_handlers == NULL) {
desc->pb_serialize_handlers =
upb_pb_encoder_newhandlers(desc->msgdef, &desc->pb_serialize_handlers);
}
return desc->pb_serialize_handlers;
}
static const upb_handlers* msgdef_json_serialize_handlers(
Descriptor* desc, bool preserve_proto_fieldnames) {
if (preserve_proto_fieldnames) {
if (desc->json_serialize_handlers == NULL) {
desc->json_serialize_handlers =
upb_json_printer_newhandlers(
desc->msgdef, true, &desc->json_serialize_handlers);
}
return desc->json_serialize_handlers;
} else {
if (desc->json_serialize_handlers_preserve == NULL) {
desc->json_serialize_handlers_preserve =
upb_json_printer_newhandlers(
desc->msgdef, false, &desc->json_serialize_handlers_preserve);
}
return desc->json_serialize_handlers_preserve;
}
}
/* /*
* call-seq: * call-seq:
* MessageClass.encode(msg) => bytes * MessageClass.encode(msg) => bytes
...@@ -1343,8 +1416,8 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb) { ...@@ -1343,8 +1416,8 @@ VALUE Message_encode(VALUE klass, VALUE msg_rb) {
upb_pb_encoder* encoder; upb_pb_encoder* encoder;
VALUE ret; VALUE ret;
stackenv_init(&se, "Error occurred during encoding: %" PRIsVALUE); stackenv_init(&se, "Error occurred during encoding: %s");
encoder = upb_pb_encoder_create(se.arena, serialize_handlers, sink.sink); encoder = upb_pb_encoder_create(&se.env, serialize_handlers, &sink.sink);
putmsg(msg_rb, desc, upb_pb_encoder_input(encoder), 0, false, false, true); putmsg(msg_rb, desc, upb_pb_encoder_input(encoder), 0, false, false, true);
...@@ -1401,8 +1474,8 @@ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) { ...@@ -1401,8 +1474,8 @@ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass) {
stackenv se; stackenv se;
VALUE ret; VALUE ret;
stackenv_init(&se, "Error occurred during encoding: %" PRIsVALUE); stackenv_init(&se, "Error occurred during encoding: %s");
printer = upb_json_printer_create(se.arena, serialize_handlers, sink.sink); printer = upb_json_printer_create(&se.env, serialize_handlers, &sink.sink);
putmsg(msg_rb, desc, upb_json_printer_input(printer), 0, putmsg(msg_rb, desc, upb_json_printer_input(printer), 0,
RTEST(emit_defaults), true, true); RTEST(emit_defaults), true, true);
......
...@@ -60,11 +60,6 @@ rb_data_type_t Message_type = { ...@@ -60,11 +60,6 @@ rb_data_type_t Message_type = {
VALUE Message_alloc(VALUE klass) { VALUE Message_alloc(VALUE klass) {
VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned); VALUE descriptor = rb_ivar_get(klass, descriptor_instancevar_interned);
Descriptor* desc = ruby_to_Descriptor(descriptor); Descriptor* desc = ruby_to_Descriptor(descriptor);
if (desc->layout == NULL) {
desc->layout = create_layout(desc);
}
MessageHeader* msg = (MessageHeader*)ALLOC_N( MessageHeader* msg = (MessageHeader*)ALLOC_N(
uint8_t, sizeof(MessageHeader) + desc->layout->size); uint8_t, sizeof(MessageHeader) + desc->layout->size);
VALUE ret; VALUE ret;
...@@ -281,7 +276,7 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) { ...@@ -281,7 +276,7 @@ VALUE Message_method_missing(int argc, VALUE* argv, VALUE _self) {
} else if (accessor_type == METHOD_PRESENCE) { } else if (accessor_type == METHOD_PRESENCE) {
return layout_has(self->descriptor->layout, Message_data(self), f); return layout_has(self->descriptor->layout, Message_data(self), f);
} else if (accessor_type == METHOD_ENUM_GETTER) { } else if (accessor_type == METHOD_ENUM_GETTER) {
VALUE enum_type = field_type_class(self->descriptor->layout, f); VALUE enum_type = field_type_class(f);
VALUE method = rb_intern("const_get"); VALUE method = rb_intern("const_get");
VALUE raw_value = layout_get(self->descriptor->layout, Message_data(self), f); VALUE raw_value = layout_get(self->descriptor->layout, Message_data(self), f);
...@@ -325,13 +320,15 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) { ...@@ -325,13 +320,15 @@ VALUE Message_respond_to_missing(int argc, VALUE* argv, VALUE _self) {
} }
} }
VALUE create_submsg_from_hash(const MessageLayout* layout, VALUE create_submsg_from_hash(const upb_fielddef *f, VALUE hash) {
const upb_fielddef* f, VALUE hash) { const upb_def *d = upb_fielddef_subdef(f);
const upb_msgdef *d = upb_fielddef_msgsubdef(f);
assert(d != NULL); assert(d != NULL);
VALUE descriptor = get_def_obj(d);
VALUE msgclass = rb_funcall(descriptor, rb_intern("msgclass"), 0, NULL);
VALUE args[1] = { hash }; VALUE args[1] = { hash };
return rb_class_new_instance(1, args, field_type_class(layout, f)); return rb_class_new_instance(1, args, msgclass);
} }
int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
...@@ -381,14 +378,14 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) { ...@@ -381,14 +378,14 @@ int Message_initialize_kwarg(VALUE key, VALUE val, VALUE _self) {
for (int i = 0; i < RARRAY_LEN(val); i++) { for (int i = 0; i < RARRAY_LEN(val); i++) {
VALUE entry = rb_ary_entry(val, i); VALUE entry = rb_ary_entry(val, i);
if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) { if (TYPE(entry) == T_HASH && upb_fielddef_issubmsg(f)) {
entry = create_submsg_from_hash(self->descriptor->layout, f, entry); entry = create_submsg_from_hash(f, entry);
} }
RepeatedField_push(ary, entry); RepeatedField_push(ary, entry);
} }
} else { } else {
if (TYPE(val) == T_HASH && upb_fielddef_issubmsg(f)) { if (TYPE(val) == T_HASH && upb_fielddef_issubmsg(f)) {
val = create_submsg_from_hash(self->descriptor->layout, f, val); val = create_submsg_from_hash(f, val);
} }
layout_set(self->descriptor->layout, Message_data(self), f, val); layout_set(self->descriptor->layout, Message_data(self), f, val);
...@@ -633,11 +630,17 @@ VALUE Message_descriptor(VALUE klass) { ...@@ -633,11 +630,17 @@ VALUE Message_descriptor(VALUE klass) {
return rb_ivar_get(klass, descriptor_instancevar_interned); return rb_ivar_get(klass, descriptor_instancevar_interned);
} }
VALUE build_class_from_descriptor(VALUE descriptor) { VALUE build_class_from_descriptor(Descriptor* desc) {
Descriptor* desc = ruby_to_Descriptor(descriptor);
const char *name; const char *name;
VALUE klass; VALUE klass;
if (desc->layout == NULL) {
desc->layout = create_layout(desc->msgdef);
}
if (desc->fill_method == NULL) {
desc->fill_method = new_fillmsg_decodermethod(desc, &desc->fill_method);
}
name = upb_msgdef_fullname(desc->msgdef); name = upb_msgdef_fullname(desc->msgdef);
if (name == NULL) { if (name == NULL) {
rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name."); rb_raise(rb_eRuntimeError, "Descriptor does not have assigned name.");
...@@ -648,7 +651,8 @@ VALUE build_class_from_descriptor(VALUE descriptor) { ...@@ -648,7 +651,8 @@ VALUE build_class_from_descriptor(VALUE descriptor) {
// their own toplevel constant class name. // their own toplevel constant class name.
rb_intern("Message"), rb_intern("Message"),
rb_cObject); rb_cObject);
rb_ivar_set(klass, descriptor_instancevar_interned, descriptor); rb_ivar_set(klass, descriptor_instancevar_interned,
get_def_obj(desc->msgdef));
rb_define_alloc_func(klass, Message_alloc); rb_define_alloc_func(klass, Message_alloc);
rb_require("google/protobuf/message_exts"); rb_require("google/protobuf/message_exts");
rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts")); rb_include_module(klass, rb_eval_string("::Google::Protobuf::MessageExts"));
...@@ -733,8 +737,7 @@ VALUE enum_descriptor(VALUE self) { ...@@ -733,8 +737,7 @@ VALUE enum_descriptor(VALUE self) {
return rb_ivar_get(self, descriptor_instancevar_interned); return rb_ivar_get(self, descriptor_instancevar_interned);
} }
VALUE build_module_from_enumdesc(VALUE _enumdesc) { VALUE build_module_from_enumdesc(EnumDescriptor* enumdesc) {
EnumDescriptor* enumdesc = ruby_to_EnumDescriptor(_enumdesc);
VALUE mod = rb_define_module_id( VALUE mod = rb_define_module_id(
rb_intern(upb_enumdef_fullname(enumdesc->enumdef))); rb_intern(upb_enumdef_fullname(enumdesc->enumdef)));
...@@ -755,7 +758,8 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc) { ...@@ -755,7 +758,8 @@ VALUE build_module_from_enumdesc(VALUE _enumdesc) {
rb_define_singleton_method(mod, "lookup", enum_lookup, 1); rb_define_singleton_method(mod, "lookup", enum_lookup, 1);
rb_define_singleton_method(mod, "resolve", enum_resolve, 1); rb_define_singleton_method(mod, "resolve", enum_resolve, 1);
rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0); rb_define_singleton_method(mod, "descriptor", enum_descriptor, 0);
rb_ivar_set(mod, descriptor_instancevar_interned, _enumdesc); rb_ivar_set(mod, descriptor_instancevar_interned,
get_def_obj(enumdesc->enumdef));
return mod; return mod;
} }
......
...@@ -30,10 +30,26 @@ ...@@ -30,10 +30,26 @@
#include "protobuf.h" #include "protobuf.h"
// -----------------------------------------------------------------------------
// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
// instances.
// -----------------------------------------------------------------------------
// This is a hash table from def objects (encoded by converting pointers to
// Ruby integers) to MessageDef/EnumDef instances (as Ruby values).
VALUE upb_def_to_ruby_obj_map;
VALUE cError; VALUE cError;
VALUE cParseError; VALUE cParseError;
VALUE cTypeError; VALUE cTypeError;
VALUE c_only_cookie;
void add_def_obj(const void* def, VALUE value) {
rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value);
}
VALUE get_def_obj(const void* def) {
return rb_hash_aref(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def));
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Utilities. // Utilities.
...@@ -100,6 +116,6 @@ void Init_protobuf_c() { ...@@ -100,6 +116,6 @@ void Init_protobuf_c() {
kRubyStringASCIIEncoding = rb_usascii_encoding(); kRubyStringASCIIEncoding = rb_usascii_encoding();
kRubyString8bitEncoding = rb_ascii8bit_encoding(); kRubyString8bitEncoding = rb_ascii8bit_encoding();
rb_gc_register_address(&c_only_cookie); rb_gc_register_address(&upb_def_to_ruby_obj_map);
c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject); upb_def_to_ruby_obj_map = rb_hash_new();
} }
...@@ -107,68 +107,62 @@ typedef struct Builder Builder; ...@@ -107,68 +107,62 @@ typedef struct Builder Builder;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
struct DescriptorPool { struct DescriptorPool {
VALUE def_to_descriptor; // Hash table of def* -> Ruby descriptor.
upb_symtab* symtab; upb_symtab* symtab;
upb_handlercache* fill_handler_cache;
upb_handlercache* pb_serialize_handler_cache;
upb_handlercache* json_serialize_handler_cache;
upb_handlercache* json_serialize_handler_preserve_cache;
upb_pbcodecache* fill_method_cache;
upb_json_codecache* json_fill_method_cache;
}; };
struct Descriptor { struct Descriptor {
const upb_msgdef* msgdef; const upb_msgdef* msgdef;
MessageLayout* layout; MessageLayout* layout;
VALUE klass; VALUE klass; // begins as nil
VALUE descriptor_pool; const upb_handlers* fill_handlers;
const upb_pbdecodermethod* fill_method;
const upb_json_parsermethod* json_fill_method;
const upb_handlers* pb_serialize_handlers;
const upb_handlers* json_serialize_handlers;
const upb_handlers* json_serialize_handlers_preserve;
}; };
struct FileDescriptor { struct FileDescriptor {
const upb_filedef* filedef; const upb_filedef* filedef;
VALUE descriptor_pool; // Owns the upb_filedef.
}; };
struct FieldDescriptor { struct FieldDescriptor {
const upb_fielddef* fielddef; const upb_fielddef* fielddef;
VALUE descriptor_pool; // Owns the upb_fielddef.
}; };
struct OneofDescriptor { struct OneofDescriptor {
const upb_oneofdef* oneofdef; const upb_oneofdef* oneofdef;
VALUE descriptor_pool; // Owns the upb_oneofdef.
}; };
struct EnumDescriptor { struct EnumDescriptor {
const upb_enumdef* enumdef; const upb_enumdef* enumdef;
VALUE module; // begins as nil VALUE module; // begins as nil
VALUE descriptor_pool; // Owns the upb_enumdef.
}; };
struct MessageBuilderContext { struct MessageBuilderContext {
google_protobuf_DescriptorProto* msg_proto; VALUE descriptor;
VALUE file_builder; VALUE builder;
}; };
struct OneofBuilderContext { struct OneofBuilderContext {
int oneof_index; VALUE descriptor;
VALUE message_builder; VALUE builder;
}; };
struct EnumBuilderContext { struct EnumBuilderContext {
google_protobuf_EnumDescriptorProto* enum_proto; VALUE enumdesc;
VALUE file_builder;
}; };
struct FileBuilderContext { struct FileBuilderContext {
upb_arena *arena; VALUE pending_list;
google_protobuf_FileDescriptorProto* file_proto; VALUE file_descriptor;
VALUE descriptor_pool; VALUE builder;
}; };
struct Builder { struct Builder {
VALUE descriptor_pool; VALUE pending_list;
VALUE default_file_builder; VALUE default_file_descriptor;
upb_def** defs; // used only while finalizing
}; };
extern VALUE cDescriptorPool; extern VALUE cDescriptorPool;
...@@ -197,6 +191,7 @@ void DescriptorPool_free(void* _self); ...@@ -197,6 +191,7 @@ void DescriptorPool_free(void* _self);
VALUE DescriptorPool_alloc(VALUE klass); VALUE DescriptorPool_alloc(VALUE klass);
void DescriptorPool_register(VALUE module); void DescriptorPool_register(VALUE module);
DescriptorPool* ruby_to_DescriptorPool(VALUE value); DescriptorPool* ruby_to_DescriptorPool(VALUE value);
VALUE DescriptorPool_add(VALUE _self, VALUE def);
VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self); VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self);
VALUE DescriptorPool_lookup(VALUE _self, VALUE name); VALUE DescriptorPool_lookup(VALUE _self, VALUE name);
VALUE DescriptorPool_generated_pool(VALUE _self); VALUE DescriptorPool_generated_pool(VALUE _self);
...@@ -208,11 +203,13 @@ void Descriptor_free(void* _self); ...@@ -208,11 +203,13 @@ void Descriptor_free(void* _self);
VALUE Descriptor_alloc(VALUE klass); VALUE Descriptor_alloc(VALUE klass);
void Descriptor_register(VALUE module); void Descriptor_register(VALUE module);
Descriptor* ruby_to_Descriptor(VALUE value); Descriptor* ruby_to_Descriptor(VALUE value);
VALUE Descriptor_initialize(VALUE _self, VALUE cookie, VALUE descriptor_pool, VALUE Descriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
VALUE ptr);
VALUE Descriptor_name(VALUE _self); VALUE Descriptor_name(VALUE _self);
VALUE Descriptor_name_set(VALUE _self, VALUE str);
VALUE Descriptor_each(VALUE _self); VALUE Descriptor_each(VALUE _self);
VALUE Descriptor_lookup(VALUE _self, VALUE name); VALUE Descriptor_lookup(VALUE _self, VALUE name);
VALUE Descriptor_add_field(VALUE _self, VALUE obj);
VALUE Descriptor_add_oneof(VALUE _self, VALUE obj);
VALUE Descriptor_each_oneof(VALUE _self); VALUE Descriptor_each_oneof(VALUE _self);
VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name); VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name);
VALUE Descriptor_msgclass(VALUE _self); VALUE Descriptor_msgclass(VALUE _self);
...@@ -224,24 +221,28 @@ void FileDescriptor_free(void* _self); ...@@ -224,24 +221,28 @@ void FileDescriptor_free(void* _self);
VALUE FileDescriptor_alloc(VALUE klass); VALUE FileDescriptor_alloc(VALUE klass);
void FileDescriptor_register(VALUE module); void FileDescriptor_register(VALUE module);
FileDescriptor* ruby_to_FileDescriptor(VALUE value); FileDescriptor* ruby_to_FileDescriptor(VALUE value);
VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie, VALUE FileDescriptor_initialize(int argc, VALUE* argv, VALUE _self);
VALUE descriptor_pool, VALUE ptr);
VALUE FileDescriptor_name(VALUE _self); VALUE FileDescriptor_name(VALUE _self);
VALUE FileDescriptor_syntax(VALUE _self); VALUE FileDescriptor_syntax(VALUE _self);
VALUE FileDescriptor_syntax_set(VALUE _self, VALUE syntax);
void FieldDescriptor_mark(void* _self); void FieldDescriptor_mark(void* _self);
void FieldDescriptor_free(void* _self); void FieldDescriptor_free(void* _self);
VALUE FieldDescriptor_alloc(VALUE klass); VALUE FieldDescriptor_alloc(VALUE klass);
void FieldDescriptor_register(VALUE module); void FieldDescriptor_register(VALUE module);
FieldDescriptor* ruby_to_FieldDescriptor(VALUE value); FieldDescriptor* ruby_to_FieldDescriptor(VALUE value);
VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr);
VALUE FieldDescriptor_name(VALUE _self); VALUE FieldDescriptor_name(VALUE _self);
VALUE FieldDescriptor_name_set(VALUE _self, VALUE str);
VALUE FieldDescriptor_type(VALUE _self); VALUE FieldDescriptor_type(VALUE _self);
VALUE FieldDescriptor_type_set(VALUE _self, VALUE type);
VALUE FieldDescriptor_default(VALUE _self); VALUE FieldDescriptor_default(VALUE _self);
VALUE FieldDescriptor_default_set(VALUE _self, VALUE default_value);
VALUE FieldDescriptor_label(VALUE _self); VALUE FieldDescriptor_label(VALUE _self);
VALUE FieldDescriptor_label_set(VALUE _self, VALUE label);
VALUE FieldDescriptor_number(VALUE _self); VALUE FieldDescriptor_number(VALUE _self);
VALUE FieldDescriptor_number_set(VALUE _self, VALUE number);
VALUE FieldDescriptor_submsg_name(VALUE _self); VALUE FieldDescriptor_submsg_name(VALUE _self);
VALUE FieldDescriptor_submsg_name_set(VALUE _self, VALUE value);
VALUE FieldDescriptor_subtype(VALUE _self); VALUE FieldDescriptor_subtype(VALUE _self);
VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb); VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb);
VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb); VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb);
...@@ -255,20 +256,21 @@ void OneofDescriptor_free(void* _self); ...@@ -255,20 +256,21 @@ void OneofDescriptor_free(void* _self);
VALUE OneofDescriptor_alloc(VALUE klass); VALUE OneofDescriptor_alloc(VALUE klass);
void OneofDescriptor_register(VALUE module); void OneofDescriptor_register(VALUE module);
OneofDescriptor* ruby_to_OneofDescriptor(VALUE value); OneofDescriptor* ruby_to_OneofDescriptor(VALUE value);
VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr);
VALUE OneofDescriptor_name(VALUE _self); VALUE OneofDescriptor_name(VALUE _self);
VALUE OneofDescriptor_name_set(VALUE _self, VALUE value);
VALUE OneofDescriptor_add_field(VALUE _self, VALUE field);
VALUE OneofDescriptor_each(VALUE _self, VALUE field); VALUE OneofDescriptor_each(VALUE _self, VALUE field);
void EnumDescriptor_mark(void* _self); void EnumDescriptor_mark(void* _self);
void EnumDescriptor_free(void* _self); void EnumDescriptor_free(void* _self);
VALUE EnumDescriptor_alloc(VALUE klass); VALUE EnumDescriptor_alloc(VALUE klass);
VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
VALUE descriptor_pool, VALUE ptr);
void EnumDescriptor_register(VALUE module); void EnumDescriptor_register(VALUE module);
EnumDescriptor* ruby_to_EnumDescriptor(VALUE value); EnumDescriptor* ruby_to_EnumDescriptor(VALUE value);
VALUE EnumDescriptor_initialize(VALUE _self, VALUE file_descriptor_rb);
VALUE EnumDescriptor_file_descriptor(VALUE _self); VALUE EnumDescriptor_file_descriptor(VALUE _self);
VALUE EnumDescriptor_name(VALUE _self); VALUE EnumDescriptor_name(VALUE _self);
VALUE EnumDescriptor_name_set(VALUE _self, VALUE str);
VALUE EnumDescriptor_add_value(VALUE _self, VALUE name, VALUE number);
VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name); VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name);
VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number); VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number);
VALUE EnumDescriptor_each(VALUE _self); VALUE EnumDescriptor_each(VALUE _self);
...@@ -281,8 +283,8 @@ VALUE MessageBuilderContext_alloc(VALUE klass); ...@@ -281,8 +283,8 @@ VALUE MessageBuilderContext_alloc(VALUE klass);
void MessageBuilderContext_register(VALUE module); void MessageBuilderContext_register(VALUE module);
MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value); MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE value);
VALUE MessageBuilderContext_initialize(VALUE _self, VALUE MessageBuilderContext_initialize(VALUE _self,
VALUE _file_builder, VALUE descriptor,
VALUE name); VALUE builder);
VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self); VALUE MessageBuilderContext_optional(int argc, VALUE* argv, VALUE _self);
VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self); VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self);
VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self); VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self);
...@@ -304,19 +306,15 @@ void EnumBuilderContext_free(void* _self); ...@@ -304,19 +306,15 @@ void EnumBuilderContext_free(void* _self);
VALUE EnumBuilderContext_alloc(VALUE klass); VALUE EnumBuilderContext_alloc(VALUE klass);
void EnumBuilderContext_register(VALUE module); void EnumBuilderContext_register(VALUE module);
EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value); EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE value);
VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder, VALUE EnumBuilderContext_initialize(VALUE _self, VALUE enumdesc);
VALUE name);
VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number); VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number);
void FileBuilderContext_mark(void* _self); void FileBuilderContext_mark(void* _self);
void FileBuilderContext_free(void* _self); void FileBuilderContext_free(void* _self);
VALUE FileBuilderContext_alloc(VALUE klass); VALUE FileBuilderContext_alloc(VALUE klass);
void FileBuilderContext_register(VALUE module); void FileBuilderContext_register(VALUE module);
FileBuilderContext* ruby_to_FileBuilderContext(VALUE _self); VALUE FileBuilderContext_initialize(VALUE _self, VALUE file_descriptor,
upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str); VALUE builder);
upb_strview FileBuilderContext_strdup_name(VALUE _self, VALUE rb_str);
VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
VALUE name, VALUE options);
VALUE FileBuilderContext_add_message(VALUE _self, VALUE name); VALUE FileBuilderContext_add_message(VALUE _self, VALUE name);
VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name); VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name);
VALUE FileBuilderContext_pending_descriptors(VALUE _self); VALUE FileBuilderContext_pending_descriptors(VALUE _self);
...@@ -326,8 +324,7 @@ void Builder_free(void* _self); ...@@ -326,8 +324,7 @@ void Builder_free(void* _self);
VALUE Builder_alloc(VALUE klass); VALUE Builder_alloc(VALUE klass);
void Builder_register(VALUE module); void Builder_register(VALUE module);
Builder* ruby_to_Builder(VALUE value); Builder* ruby_to_Builder(VALUE value);
VALUE Builder_build(VALUE _self); VALUE Builder_initialize(VALUE _self);
VALUE Builder_initialize(VALUE _self, VALUE descriptor_pool);
VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self); VALUE Builder_add_file(int argc, VALUE *argv, VALUE _self);
VALUE Builder_add_message(VALUE _self, VALUE name); VALUE Builder_add_message(VALUE _self, VALUE name);
VALUE Builder_add_enum(VALUE _self, VALUE name); VALUE Builder_add_enum(VALUE _self, VALUE name);
...@@ -371,7 +368,7 @@ extern rb_encoding* kRubyStringUtf8Encoding; ...@@ -371,7 +368,7 @@ extern rb_encoding* kRubyStringUtf8Encoding;
extern rb_encoding* kRubyStringASCIIEncoding; extern rb_encoding* kRubyStringASCIIEncoding;
extern rb_encoding* kRubyString8bitEncoding; extern rb_encoding* kRubyString8bitEncoding;
VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field); VALUE field_type_class(const upb_fielddef* field);
#define MAP_KEY_FIELD 1 #define MAP_KEY_FIELD 1
#define MAP_VALUE_FIELD 2 #define MAP_VALUE_FIELD 2
...@@ -504,15 +501,13 @@ struct MessageField { ...@@ -504,15 +501,13 @@ struct MessageField {
size_t hasbit; size_t hasbit;
}; };
// MessageLayout is owned by the enclosing Descriptor, which must outlive us.
struct MessageLayout { struct MessageLayout {
const Descriptor* desc;
const upb_msgdef* msgdef; const upb_msgdef* msgdef;
MessageField* fields; MessageField* fields;
size_t size; size_t size;
}; };
MessageLayout* create_layout(const Descriptor* desc); MessageLayout* create_layout(const upb_msgdef* msgdef);
void free_layout(MessageLayout* layout); void free_layout(MessageLayout* layout);
bool field_contains_hasbit(MessageLayout* layout, bool field_contains_hasbit(MessageLayout* layout,
const upb_fielddef* field); const upb_fielddef* field);
...@@ -561,7 +556,7 @@ struct MessageHeader { ...@@ -561,7 +556,7 @@ struct MessageHeader {
extern rb_data_type_t Message_type; extern rb_data_type_t Message_type;
VALUE build_class_from_descriptor(VALUE descriptor); VALUE build_class_from_descriptor(Descriptor* descriptor);
void* Message_data(void* msg); void* Message_data(void* msg);
void Message_mark(void* self); void Message_mark(void* self);
void Message_free(void* self); void Message_free(void* self);
...@@ -585,13 +580,12 @@ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass); ...@@ -585,13 +580,12 @@ VALUE Message_encode_json(int argc, VALUE* argv, VALUE klass);
VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb); VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb);
VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj); VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj);
VALUE build_module_from_enumdesc(VALUE _enumdesc); VALUE build_module_from_enumdesc(EnumDescriptor* enumdef);
VALUE enum_lookup(VALUE self, VALUE number); VALUE enum_lookup(VALUE self, VALUE number);
VALUE enum_resolve(VALUE self, VALUE sym); VALUE enum_resolve(VALUE self, VALUE sym);
const upb_pbdecodermethod *new_fillmsg_decodermethod( const upb_pbdecodermethod *new_fillmsg_decodermethod(
Descriptor* descriptor, const void *owner); Descriptor* descriptor, const void *owner);
void add_handlers_for_message(const void *closure, upb_handlers *h);
// Maximum depth allowed during encoding, to avoid stack overflows due to // Maximum depth allowed during encoding, to avoid stack overflows due to
// cycles. // cycles.
...@@ -601,11 +595,8 @@ void add_handlers_for_message(const void *closure, upb_handlers *h); ...@@ -601,11 +595,8 @@ void add_handlers_for_message(const void *closure, upb_handlers *h);
// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
// instances. // instances.
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def); void add_def_obj(const void* def, VALUE value);
VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def); VALUE get_def_obj(const void* def);
VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Utilities. // Utilities.
...@@ -621,9 +612,4 @@ void check_upb_status(const upb_status* status, const char* msg); ...@@ -621,9 +612,4 @@ void check_upb_status(const upb_status* status, const char* msg);
extern ID descriptor_instancevar_interned; extern ID descriptor_instancevar_interned;
// A distinct object that is not accessible from Ruby. We use this as a
// constructor argument to enforce that certain objects cannot be created from
// Ruby.
extern VALUE c_only_cookie;
#endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__ #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
...@@ -430,10 +430,8 @@ static size_t align_up_to(size_t offset, size_t granularity) { ...@@ -430,10 +430,8 @@ static size_t align_up_to(size_t offset, size_t granularity) {
return (offset + granularity - 1) & ~(granularity - 1); return (offset + granularity - 1) & ~(granularity - 1);
} }
MessageLayout* create_layout(const Descriptor* desc) { MessageLayout* create_layout(const upb_msgdef* msgdef) {
const upb_msgdef *msgdef = desc->msgdef;
MessageLayout* layout = ALLOC(MessageLayout); MessageLayout* layout = ALLOC(MessageLayout);
layout->desc = desc;
int nfields = upb_msgdef_numfields(msgdef); int nfields = upb_msgdef_numfields(msgdef);
upb_msg_field_iter it; upb_msg_field_iter it;
upb_msg_oneof_iter oit; upb_msg_oneof_iter oit;
...@@ -539,25 +537,28 @@ MessageLayout* create_layout(const Descriptor* desc) { ...@@ -539,25 +537,28 @@ MessageLayout* create_layout(const Descriptor* desc) {
} }
layout->size = off; layout->size = off;
layout->msgdef = msgdef; layout->msgdef = msgdef;
upb_msgdef_ref(layout->msgdef, &layout->msgdef);
return layout; return layout;
} }
void free_layout(MessageLayout* layout) { void free_layout(MessageLayout* layout) {
xfree(layout->fields); xfree(layout->fields);
upb_msgdef_unref(layout->msgdef, &layout->msgdef);
xfree(layout); xfree(layout);
} }
VALUE field_type_class(const MessageLayout* layout, const upb_fielddef* field) { VALUE field_type_class(const upb_fielddef* field) {
VALUE type_class = Qnil; VALUE type_class = Qnil;
if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) { if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
VALUE submsgdesc = get_msgdef_obj(layout->desc->descriptor_pool, VALUE submsgdesc =
upb_fielddef_msgsubdef(field)); get_def_obj(upb_fielddef_subdef(field));
type_class = Descriptor_msgclass(submsgdesc); type_class = Descriptor_msgclass(submsgdesc);
} else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) { } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
VALUE subenumdesc = get_enumdef_obj(layout->desc->descriptor_pool, VALUE subenumdesc =
upb_fielddef_enumsubdef(field)); get_def_obj(upb_fielddef_subdef(field));
type_class = EnumDescriptor_enummodule(subenumdesc); type_class = EnumDescriptor_enummodule(subenumdesc);
} }
return type_class; return type_class;
...@@ -631,7 +632,7 @@ void layout_clear(MessageLayout* layout, ...@@ -631,7 +632,7 @@ void layout_clear(MessageLayout* layout,
const upb_fielddef* key_field = map_field_key(field); const upb_fielddef* key_field = map_field_key(field);
const upb_fielddef* value_field = map_field_value(field); const upb_fielddef* value_field = map_field_value(field);
VALUE type_class = field_type_class(layout, value_field); VALUE type_class = field_type_class(value_field);
if (type_class != Qnil) { if (type_class != Qnil) {
VALUE args[3] = { VALUE args[3] = {
...@@ -652,7 +653,7 @@ void layout_clear(MessageLayout* layout, ...@@ -652,7 +653,7 @@ void layout_clear(MessageLayout* layout,
} else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
VALUE ary = Qnil; VALUE ary = Qnil;
VALUE type_class = field_type_class(layout, field); VALUE type_class = field_type_class(field);
if (type_class != Qnil) { if (type_class != Qnil) {
VALUE args[2] = { VALUE args[2] = {
...@@ -667,9 +668,9 @@ void layout_clear(MessageLayout* layout, ...@@ -667,9 +668,9 @@ void layout_clear(MessageLayout* layout,
DEREF(memory, VALUE) = ary; DEREF(memory, VALUE) = ary;
} else { } else {
native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field), native_slot_set(upb_fielddef_name(field),
field_type_class(layout, field), memory, upb_fielddef_type(field), field_type_class(field),
layout_get_default(field)); memory, layout_get_default(field));
} }
} }
...@@ -727,19 +728,20 @@ VALUE layout_get(MessageLayout* layout, ...@@ -727,19 +728,20 @@ VALUE layout_get(MessageLayout* layout,
return layout_get_default(field); return layout_get_default(field);
} }
return native_slot_get(upb_fielddef_type(field), return native_slot_get(upb_fielddef_type(field),
field_type_class(layout, field), memory); field_type_class(field),
memory);
} else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
return *((VALUE *)memory); return *((VALUE *)memory);
} else if (!field_set) { } else if (!field_set) {
return layout_get_default(field); return layout_get_default(field);
} else { } else {
return native_slot_get(upb_fielddef_type(field), return native_slot_get(upb_fielddef_type(field),
field_type_class(layout, field), memory); field_type_class(field),
memory);
} }
} }
static void check_repeated_field_type(const MessageLayout* layout, VALUE val, static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
const upb_fielddef* field) {
RepeatedField* self; RepeatedField* self;
assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED); assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
...@@ -753,13 +755,25 @@ static void check_repeated_field_type(const MessageLayout* layout, VALUE val, ...@@ -753,13 +755,25 @@ static void check_repeated_field_type(const MessageLayout* layout, VALUE val,
rb_raise(cTypeError, "Repeated field array has wrong element type"); rb_raise(cTypeError, "Repeated field array has wrong element type");
} }
if (self->field_type_class != field_type_class(layout, field)) { if (self->field_type == UPB_TYPE_MESSAGE) {
rb_raise(cTypeError, "Repeated field array has wrong message/enum class"); if (self->field_type_class !=
Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) {
rb_raise(cTypeError,
"Repeated field array has wrong message class");
}
}
if (self->field_type == UPB_TYPE_ENUM) {
if (self->field_type_class !=
EnumDescriptor_enummodule(get_def_obj(upb_fielddef_subdef(field)))) {
rb_raise(cTypeError,
"Repeated field array has wrong enum class");
}
} }
} }
static void check_map_field_type(const MessageLayout* layout, VALUE val, static void check_map_field_type(VALUE val, const upb_fielddef* field) {
const upb_fielddef* field) {
const upb_fielddef* key_field = map_field_key(field); const upb_fielddef* key_field = map_field_key(field);
const upb_fielddef* value_field = map_field_value(field); const upb_fielddef* value_field = map_field_value(field);
Map* self; Map* self;
...@@ -776,11 +790,17 @@ static void check_map_field_type(const MessageLayout* layout, VALUE val, ...@@ -776,11 +790,17 @@ static void check_map_field_type(const MessageLayout* layout, VALUE val,
if (self->value_type != upb_fielddef_type(value_field)) { if (self->value_type != upb_fielddef_type(value_field)) {
rb_raise(cTypeError, "Map value type does not match field's value type"); rb_raise(cTypeError, "Map value type does not match field's value type");
} }
if (self->value_type_class != field_type_class(layout, value_field)) { if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
rb_raise(cTypeError, "Map value type has wrong message/enum class"); upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
if (self->value_type_class !=
get_def_obj(upb_fielddef_subdef(value_field))) {
rb_raise(cTypeError,
"Map value type has wrong message/enum class");
}
} }
} }
void layout_set(MessageLayout* layout, void layout_set(MessageLayout* layout,
void* storage, void* storage,
const upb_fielddef* field, const upb_fielddef* field,
...@@ -808,19 +828,20 @@ void layout_set(MessageLayout* layout, ...@@ -808,19 +828,20 @@ void layout_set(MessageLayout* layout,
// and case number are altered atomically (w.r.t. the Ruby VM). // and case number are altered atomically (w.r.t. the Ruby VM).
native_slot_set_value_and_case( native_slot_set_value_and_case(
upb_fielddef_name(field), upb_fielddef_name(field),
upb_fielddef_type(field), field_type_class(layout, field), upb_fielddef_type(field), field_type_class(field),
memory, val, memory, val,
oneof_case, upb_fielddef_number(field)); oneof_case, upb_fielddef_number(field));
} }
} else if (is_map_field(field)) { } else if (is_map_field(field)) {
check_map_field_type(layout, val, field); check_map_field_type(val, field);
DEREF(memory, VALUE) = val; DEREF(memory, VALUE) = val;
} else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
check_repeated_field_type(layout, val, field); check_repeated_field_type(val, field);
DEREF(memory, VALUE) = val; DEREF(memory, VALUE) = val;
} else { } else {
native_slot_set(upb_fielddef_name(field), upb_fielddef_type(field), native_slot_set(upb_fielddef_name(field),
field_type_class(layout, field), memory, val); upb_fielddef_type(field), field_type_class(field),
memory, val);
} }
if (layout->fields[upb_fielddef_index(field)].hasbit != if (layout->fields[upb_fielddef_index(field)].hasbit !=
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -50,72 +50,6 @@ else ...@@ -50,72 +50,6 @@ else
rescue LoadError rescue LoadError
require 'google/protobuf_c' require 'google/protobuf_c'
end end
module Google
module Protobuf
module Internal
def self.infer_package(names)
# Package is longest common prefix ending in '.', if any.
min, max = names.minmax
last_common_dot = nil
min.size.times { |i|
if min[i] != max[i] then break end
if min[i] == ?. then last_common_dot = i end
}
if last_common_dot
return min.slice(0, last_common_dot)
end
end
class NestingBuilder
def initialize(msg_names, enum_names)
@to_pos = {nil=>nil}
@msg_children = Hash.new { |hash, key| hash[key] = [] }
@enum_children = Hash.new { |hash, key| hash[key] = [] }
msg_names.each_with_index { |name, idx| @to_pos[name] = idx }
enum_names.each_with_index { |name, idx| @to_pos[name] = idx }
msg_names.each { |name| @msg_children[parent(name)] << name }
enum_names.each { |name| @enum_children[parent(name)] << name }
end
def build(package)
return build_msg(package)
end
private
def build_msg(msg)
return {
:pos => @to_pos[msg],
:msgs => @msg_children[msg].map { |child| build_msg(child) },
:enums => @enum_children[msg].map { |child| @to_pos[child] },
}
end
private
def parent(name)
idx = name.rindex(?.)
if idx
return name.slice(0, idx)
else
return nil
end
end
end
def self.fixup_descriptor(package, msg_names, enum_names)
if package.nil?
package = self.infer_package(msg_names + enum_names)
end
nesting = NestingBuilder.new(msg_names, enum_names).build(package)
return package, nesting
end
end
end
end
end end
require 'google/protobuf/repeated_field' require 'google/protobuf/repeated_field'
......
...@@ -17,6 +17,7 @@ module BasicTest ...@@ -17,6 +17,7 @@ module BasicTest
add_message "BadFieldNames" do add_message "BadFieldNames" do
optional :dup, :int32, 1 optional :dup, :int32, 1
optional :class, :int32, 2 optional :class, :int32, 2
optional :"a.b", :int32, 3
end end
end end
...@@ -350,6 +351,11 @@ module BasicTest ...@@ -350,6 +351,11 @@ module BasicTest
assert nil != file_descriptor assert nil != file_descriptor
assert_equal "tests/basic_test.proto", file_descriptor.name assert_equal "tests/basic_test.proto", file_descriptor.name
assert_equal :proto3, file_descriptor.syntax assert_equal :proto3, file_descriptor.syntax
file_descriptor = BadFieldNames.descriptor.file_descriptor
assert nil != file_descriptor
assert_equal nil, file_descriptor.name
assert_equal :proto3, file_descriptor.syntax
end end
def test_map_freeze def test_map_freeze
......
...@@ -18,6 +18,7 @@ module BasicTestProto2 ...@@ -18,6 +18,7 @@ module BasicTestProto2
add_message "BadFieldNames" do add_message "BadFieldNames" do
optional :dup, :int32, 1 optional :dup, :int32, 1
optional :class, :int32, 2 optional :class, :int32, 2
optional :"a.b", :int32, 3
end end
end end
end end
......
...@@ -807,7 +807,7 @@ module CommonTests ...@@ -807,7 +807,7 @@ module CommonTests
proto_module::TestMessage2.new(:foo => 2)]) proto_module::TestMessage2.new(:foo => 2)])
data = proto_module::TestMessage.encode m data = proto_module::TestMessage.encode m
m2 = proto_module::TestMessage.decode data m2 = proto_module::TestMessage.decode data
assert_equal m, m2 assert m == m2
data = Google::Protobuf.encode m data = Google::Protobuf.encode m
m2 = Google::Protobuf.decode(proto_module::TestMessage, data) m2 = Google::Protobuf.decode(proto_module::TestMessage, data)
...@@ -902,6 +902,8 @@ module CommonTests ...@@ -902,6 +902,8 @@ module CommonTests
assert m['class'] == 2 assert m['class'] == 2
m['dup'] = 3 m['dup'] = 3
assert m['dup'] == 3 assert m['dup'] == 3
m['a.b'] = 4
assert m['a.b'] == 4
end end
def test_int_ranges def test_int_ranges
...@@ -1082,7 +1084,9 @@ module CommonTests ...@@ -1082,7 +1084,9 @@ module CommonTests
json_text = proto_module::TestMessage.encode_json(m) json_text = proto_module::TestMessage.encode_json(m)
m2 = proto_module::TestMessage.decode_json(json_text) m2 = proto_module::TestMessage.decode_json(json_text)
assert_equal m, m2 puts m.inspect
puts m2.inspect
assert m == m2
# Crash case from GitHub issue 283. # Crash case from GitHub issue 283.
bar = proto_module::Bar.new(msg: "bar") bar = proto_module::Bar.new(msg: "bar")
...@@ -1128,7 +1132,7 @@ module CommonTests ...@@ -1128,7 +1132,7 @@ module CommonTests
actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true) actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
assert_equal expected, JSON.parse(actual, :symbolize_names => true) assert JSON.parse(actual, :symbolize_names => true) == expected
end end
def test_json_emit_defaults_submsg def test_json_emit_defaults_submsg
...@@ -1163,7 +1167,7 @@ module CommonTests ...@@ -1163,7 +1167,7 @@ module CommonTests
actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true) actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
assert_equal expected, JSON.parse(actual, :symbolize_names => true) assert JSON.parse(actual, :symbolize_names => true) == expected
end end
def test_json_emit_defaults_repeated_submsg def test_json_emit_defaults_repeated_submsg
...@@ -1197,7 +1201,7 @@ module CommonTests ...@@ -1197,7 +1201,7 @@ module CommonTests
actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true) actual = proto_module::TestMessage.encode_json(m, :emit_defaults => true)
assert_equal expected, JSON.parse(actual, :symbolize_names => true) assert JSON.parse(actual, :symbolize_names => true) == expected
end end
def value_from_ruby(value) def value_from_ruby(value)
......
...@@ -10,11 +10,11 @@ require 'generated_code_pb' ...@@ -10,11 +10,11 @@ require 'generated_code_pb'
class TestTypeErrors < Test::Unit::TestCase class TestTypeErrors < Test::Unit::TestCase
def test_bad_string def test_bad_string
check_error Google::Protobuf::TypeError, check_error Google::Protobuf::TypeError,
"Invalid argument for string field 'optional_string' (given Fixnum)." do "Invalid argument for string field 'optional_string' (given Integer)." do
A::B::C::TestMessage.new(optional_string: 4) A::B::C::TestMessage.new(optional_string: 4)
end end
check_error Google::Protobuf::TypeError, check_error Google::Protobuf::TypeError,
"Invalid argument for string field 'oneof_string' (given Fixnum)." do "Invalid argument for string field 'oneof_string' (given Integer)." do
A::B::C::TestMessage.new(oneof_string: 4) A::B::C::TestMessage.new(oneof_string: 4)
end end
check_error ArgumentError, check_error ArgumentError,
...@@ -151,7 +151,7 @@ class TestTypeErrors < Test::Unit::TestCase ...@@ -151,7 +151,7 @@ class TestTypeErrors < Test::Unit::TestCase
def test_bad_msg def test_bad_msg
check_error Google::Protobuf::TypeError, check_error Google::Protobuf::TypeError,
"Invalid type Fixnum to assign to submessage field 'optional_msg'." do "Invalid type Integer to assign to submessage field 'optional_msg'." do
A::B::C::TestMessage.new(optional_msg: 2) A::B::C::TestMessage.new(optional_msg: 2)
end end
check_error Google::Protobuf::TypeError, check_error Google::Protobuf::TypeError,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment