Ported Ruby extension to upb_msg (#8184)

* WIP.

* WIP.

* WIP.

* WIP.

* WIP.

* WIP.

* Added some missing files.

* WIP.

* WIP.

* Updated upb.

* Extension loads, but crashes immediately.

* Gets through the test suite without SEGV!

Still a lot of bugs to fix, but it is a major step!

214 tests, 378 assertions, 37 failures, 147 errors, 0 pendings, 0 omissions, 0 notifications
14.0187% passed

* Test and build for Ruby 3.0

* Fixed a few more bugs, efficient #inspect is almost done.

214 tests, 134243 assertions, 30 failures, 144 errors, 0 pendings, 0 omissions, 0 notifications
18.6916% passed

* Fixed message hash initialization and encode depth checking.

214 tests, 124651 assertions, 53 failures, 70 errors, 0 pendings, 0 omissions, 0 notifications
42.5234% passed

* A bunch of fixes to failing tests, now 70% passing.

214 tests, 202091 assertions, 41 failures, 23 errors, 0 pendings, 0 omissions, 0 notifications
70.0935% passed

* More than 80% of tests are passing now.

214 tests, 322331 assertions, 30 failures, 9 errors, 0 pendings, 0 omissions, 0 notifications
81.7757% passed

Unfortunately there is also a sporadic bug/segfault hanging around
that appears to be GC-related.

* Add linux/ruby30 and macos/ruby30

* Use rvm master for 3.0.0-preview2

* Over 90% of tests are passing!

214 tests, 349898 assertions, 15 failures, 1 errors, 0 pendings, 0 omissions, 0 notifications
92.5234% passed

* Passes all tests!

214 tests, 369388 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 0 notifications
100% passed

* A bunch of cleanup.

1. Removed a bunch of internal-only symbols from headers.
2. Required a frozen check to get a non-const pointer to a map or array.
3. De-duplicated the code to get a type argument for Map/RepeatedField.

* Removed a bunch more stuff from protobuf.h.  There is an intermittent assert failure.

Intermittent failure:

ruby: ../../../../ext/google/protobuf_c/protobuf.c:263: ObjectCache_Add: Assertion `rb_funcall(obj_cache2, (__builtin_constant_p("[]") ? __extension__ ({ static ID rb_intern_id_cache; if (!rb_intern_id_cache) rb_intern_id_cache = rb_intern2((("[]")
), (long)strlen(("[]"))); (ID) rb_intern_id_cache; }) : rb_intern("[]")), 1, key_rb) == val' failed

* Removed a few more things from protobuf.h.

* Ruby 3.0.0-preview2 to 3.0.0

* Require rake-compiler-dock >= 1.1.0

* More progress, fighting with the object cache.

* Passes on all Ruby versions!

* Updated and clarified comment regarding WeakMap.

* Fixed the wyhash compile.

* Fixed conformance tests for Ruby.

Conformance results now look like:

RUBYLIB=../ruby/lib:. ./conformance-test-runner --enforce_recommended --failure_list failure_list_ruby.txt --text_format_failure_list text_format_failure_list_ruby.txt ./conformance_ruby.rb

CONFORMANCE TEST BEGIN ====================================

CONFORMANCE SUITE PASSED: 1955 successes, 0 skipped, 58 expected failures, 0 unexpected failures.

CONFORMANCE TEST BEGIN ====================================

CONFORMANCE SUITE PASSED: 0 successes, 111 skipped, 8 expected failures, 0 unexpected failures.

Fixes include:

- Changed Ruby compiler to no longer reject proto2 maps.
- Changed Ruby compiler to emit a warning when proto2 extensions are
  present instead of rejecting the .proto file completely.
- Fixed conformance tests to allow proto2 and look up message by name
  instead of hardcoding a specific list of messages.
- Fixed conformance test to support the "ignore unknown" option for
  JSON.
- Fixed conformance test to properly report serialization errors.

* Removed debug printf and fixed #inspect for floats.

* Fixed compatibility test to have proper semantics for #to_json.

* Updated Makefile.am with new file list.

* Don't try to copy wyhash when inside Docker.

* Fixed bug where we would forget that a sub-object is frozen in Ruby >=2.7.

* Avoid exporting unneeded symbols and refactored a bit of code.

* Some more refactoring.

* Simplified and added more comments.

* Some more comments and simplification. Added a missing license block.

Co-authored-by: Masaki Hara <hara@wantedly.com>
diff --git a/ruby/ext/google/protobuf_c/defs.c b/ruby/ext/google/protobuf_c/defs.c
index 1a09cc5..6cf8174 100644
--- a/ruby/ext/google/protobuf_c/defs.c
+++ b/ruby/ext/google/protobuf_c/defs.c
@@ -30,8 +30,35 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <ruby/version.h>
+
+#include "convert.h"
+#include "message.h"
 #include "protobuf.h"
 
+static VALUE Builder_build(VALUE _self);
+
+static VALUE cMessageBuilderContext;
+static VALUE cOneofBuilderContext;
+static VALUE cEnumBuilderContext;
+static VALUE cBuilder;
+
+// -----------------------------------------------------------------------------
+// Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
+// instances.
+// -----------------------------------------------------------------------------
+
+static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def);
+static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def);
+static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def);
+static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def);
+static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def);
+
+// 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.
+VALUE c_only_cookie = Qnil;
+
 // -----------------------------------------------------------------------------
 // Common utilities.
 // -----------------------------------------------------------------------------
@@ -48,6 +75,10 @@
   return rb_str_new2(s);
 }
 
+// -----------------------------------------------------------------------------
+// Backward compatibility code.
+// -----------------------------------------------------------------------------
+
 static void rewrite_enum_default(const upb_symtab* symtab,
                                  google_protobuf_FileDescriptorProto* file,
                                  google_protobuf_FieldDescriptorProto* field) {
@@ -205,6 +236,1239 @@
   }
 }
 
+// -----------------------------------------------------------------------------
+// DescriptorPool.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  VALUE def_to_descriptor;  // Hash table of def* -> Ruby descriptor.
+  upb_symtab* symtab;
+} DescriptorPool;
+
+VALUE cDescriptorPool = Qnil;
+
+// Global singleton DescriptorPool. The user is free to create others, but this
+// is used by generated code.
+VALUE generated_pool = Qnil;
+
+static void DescriptorPool_mark(void* _self) {
+  DescriptorPool* self = _self;
+  rb_gc_mark(self->def_to_descriptor);
+}
+
+static void DescriptorPool_free(void* _self) {
+  DescriptorPool* self = _self;
+  upb_symtab_free(self->symtab);
+  xfree(self);
+}
+
+static const rb_data_type_t DescriptorPool_type = {
+  "Google::Protobuf::DescriptorPool",
+  {DescriptorPool_mark, DescriptorPool_free, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static DescriptorPool* ruby_to_DescriptorPool(VALUE val) {
+  DescriptorPool* ret;
+  TypedData_Get_Struct(val, DescriptorPool, &DescriptorPool_type, ret);
+  return ret;
+}
+
+// Exposed to other modules in defs.h.
+const upb_symtab *DescriptorPool_GetSymtab(VALUE desc_pool_rb) {
+  DescriptorPool *pool = ruby_to_DescriptorPool(desc_pool_rb);
+  return pool->symtab;
+}
+
+/*
+ * call-seq:
+ *     DescriptorPool.new => pool
+ *
+ * Creates a new, empty, descriptor pool.
+ */
+static VALUE DescriptorPool_alloc(VALUE klass) {
+  DescriptorPool* self = ALLOC(DescriptorPool);
+  VALUE ret;
+
+  self->def_to_descriptor = Qnil;
+  ret = TypedData_Wrap_Struct(klass, &DescriptorPool_type, self);
+
+  self->def_to_descriptor = rb_hash_new();
+  self->symtab = upb_symtab_new();
+  ObjectCache_Add(self->symtab, ret, _upb_symtab_arena(self->symtab));
+
+  return ret;
+}
+
+/*
+ * call-seq:
+ *     DescriptorPool.build(&block)
+ *
+ * Invokes the block with a Builder instance as self. All message and enum types
+ * added within the block are committed to the pool atomically, and may refer
+ * (co)recursively to each other. The user should call Builder#add_message and
+ * Builder#add_enum within the block as appropriate.  This is the recommended,
+ * idiomatic way to define new message and enum types.
+ */
+static VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
+  VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
+  VALUE block = rb_block_proc();
+  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
+  Builder_build(ctx);
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     DescriptorPool.lookup(name) => descriptor
+ *
+ * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
+ * exists with the given name.
+ */
+static VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
+  DescriptorPool* self = ruby_to_DescriptorPool(_self);
+  const char* name_str = get_str(name);
+  const upb_msgdef* msgdef;
+  const upb_enumdef* enumdef;
+
+  msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
+  if (msgdef) {
+    return get_msgdef_obj(_self, msgdef);
+  }
+
+  enumdef = upb_symtab_lookupenum(self->symtab, name_str);
+  if (enumdef) {
+    return get_enumdef_obj(_self, enumdef);
+  }
+
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     DescriptorPool.generated_pool => descriptor_pool
+ *
+ * Class method that returns the global DescriptorPool. This is a singleton into
+ * which generated-code message and enum types are registered. The user may also
+ * register types in this pool for convenience so that they do not have to hold
+ * a reference to a private pool instance.
+ */
+static VALUE DescriptorPool_generated_pool(VALUE _self) {
+  return generated_pool;
+}
+
+static void DescriptorPool_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "DescriptorPool", rb_cObject);
+  rb_define_alloc_func(klass, DescriptorPool_alloc);
+  rb_define_method(klass, "build", DescriptorPool_build, -1);
+  rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
+  rb_define_singleton_method(klass, "generated_pool",
+                             DescriptorPool_generated_pool, 0);
+  rb_gc_register_address(&cDescriptorPool);
+  cDescriptorPool = klass;
+
+  rb_gc_register_address(&generated_pool);
+  generated_pool = rb_class_new_instance(0, NULL, klass);
+}
+
+// -----------------------------------------------------------------------------
+// Descriptor.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  const upb_msgdef* msgdef;
+  VALUE klass;
+  VALUE descriptor_pool;
+} Descriptor;
+
+VALUE cDescriptor = Qnil;
+
+static void Descriptor_mark(void* _self) {
+  Descriptor* self = _self;
+  rb_gc_mark(self->klass);
+  rb_gc_mark(self->descriptor_pool);
+}
+
+static const rb_data_type_t Descriptor_type = {
+  "Google::Protobuf::Descriptor",
+  {Descriptor_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static Descriptor* ruby_to_Descriptor(VALUE val) {
+  Descriptor* ret;
+  TypedData_Get_Struct(val, Descriptor, &Descriptor_type, ret);
+  return ret;
+}
+
+/*
+ * call-seq:
+ *     Descriptor.new => descriptor
+ *
+ * Creates a new, empty, message type descriptor. At a minimum, its name must be
+ * set before it is added to a pool. It cannot be used to create messages until
+ * it is added to a pool, after which it becomes immutable (as part of a
+ * finalization process).
+ */
+static VALUE Descriptor_alloc(VALUE klass) {
+  Descriptor* self = ALLOC(Descriptor);
+  VALUE ret = TypedData_Wrap_Struct(klass, &Descriptor_type, self);
+  self->msgdef = NULL;
+  self->klass = Qnil;
+  self->descriptor_pool = Qnil;
+  return ret;
+}
+
+/*
+ * call-seq:
+ *    Descriptor.new(c_only_cookie, ptr) => Descriptor
+ *
+ * Creates a descriptor wrapper object.  May only be called from C.
+ */
+static VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
+                                   VALUE descriptor_pool, VALUE ptr) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+
+  if (cookie != c_only_cookie) {
+    rb_raise(rb_eRuntimeError,
+             "Descriptor objects may not be created from Ruby.");
+  }
+
+  self->descriptor_pool = descriptor_pool;
+  self->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
+
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *    Descriptor.file_descriptor
+ *
+ * Returns the FileDescriptor object this message belongs to.
+ */
+static VALUE Descriptor_file_descriptor(VALUE _self) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+  return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
+}
+
+/*
+ * call-seq:
+ *     Descriptor.name => name
+ *
+ * Returns the name of this message type as a fully-qualified string (e.g.,
+ * My.Package.MessageType).
+ */
+static VALUE Descriptor_name(VALUE _self) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+  return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
+}
+
+/*
+ * call-seq:
+ *     Descriptor.each(&block)
+ *
+ * Iterates over fields in this message type, yielding to the block on each one.
+ */
+static VALUE Descriptor_each(VALUE _self) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+
+  upb_msg_field_iter it;
+  for (upb_msg_field_begin(&it, self->msgdef);
+       !upb_msg_field_done(&it);
+       upb_msg_field_next(&it)) {
+    const upb_fielddef* field = upb_msg_iter_field(&it);
+    VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
+    rb_yield(obj);
+  }
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     Descriptor.lookup(name) => FieldDescriptor
+ *
+ * Returns the field descriptor for the field with the given name, if present,
+ * or nil if none.
+ */
+static VALUE Descriptor_lookup(VALUE _self, VALUE name) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+  const char* s = get_str(name);
+  const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
+  if (field == NULL) {
+    return Qnil;
+  }
+  return get_fielddef_obj(self->descriptor_pool, field);
+}
+
+/*
+ * call-seq:
+ *     Descriptor.each_oneof(&block) => nil
+ *
+ * Invokes the given block for each oneof in this message type, passing the
+ * corresponding OneofDescriptor.
+ */
+static VALUE Descriptor_each_oneof(VALUE _self) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+
+  upb_msg_oneof_iter it;
+  for (upb_msg_oneof_begin(&it, self->msgdef);
+       !upb_msg_oneof_done(&it);
+       upb_msg_oneof_next(&it)) {
+    const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
+    VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
+    rb_yield(obj);
+  }
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     Descriptor.lookup_oneof(name) => OneofDescriptor
+ *
+ * Returns the oneof descriptor for the oneof with the given name, if present,
+ * or nil if none.
+ */
+static VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+  const char* s = get_str(name);
+  const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
+  if (oneof == NULL) {
+    return Qnil;
+  }
+  return get_oneofdef_obj(self->descriptor_pool, oneof);
+}
+
+/*
+ * call-seq:
+ *     Descriptor.msgclass => message_klass
+ *
+ * Returns the Ruby class created for this message type.
+ */
+static VALUE Descriptor_msgclass(VALUE _self) {
+  Descriptor* self = ruby_to_Descriptor(_self);
+  if (self->klass == Qnil) {
+    self->klass = build_class_from_descriptor(_self);
+  }
+  return self->klass;
+}
+
+static void Descriptor_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "Descriptor", rb_cObject);
+  rb_define_alloc_func(klass, Descriptor_alloc);
+  rb_define_method(klass, "initialize", Descriptor_initialize, 3);
+  rb_define_method(klass, "each", Descriptor_each, 0);
+  rb_define_method(klass, "lookup", Descriptor_lookup, 1);
+  rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
+  rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
+  rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
+  rb_define_method(klass, "name", Descriptor_name, 0);
+  rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
+  rb_include_module(klass, rb_mEnumerable);
+  rb_gc_register_address(&cDescriptor);
+  cDescriptor = klass;
+}
+
+// -----------------------------------------------------------------------------
+// FileDescriptor.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  const upb_filedef* filedef;
+  VALUE descriptor_pool;  // Owns the upb_filedef.
+} FileDescriptor;
+
+static VALUE cFileDescriptor = Qnil;
+
+static void FileDescriptor_mark(void* _self) {
+  FileDescriptor* self = _self;
+  rb_gc_mark(self->descriptor_pool);
+}
+
+static const rb_data_type_t FileDescriptor_type = {
+  "Google::Protobuf::FileDescriptor",
+  {FileDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static FileDescriptor* ruby_to_FileDescriptor(VALUE val) {
+  FileDescriptor* ret;
+  TypedData_Get_Struct(val, FileDescriptor, &FileDescriptor_type, ret);
+  return ret;
+}
+
+static 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;
+}
+
+/*
+ * call-seq:
+ *     FileDescriptor.new => file
+ *
+ * Returns a new file descriptor. The syntax must be set before it's passed
+ * to a builder.
+ */
+static VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
+                                VALUE descriptor_pool, VALUE ptr) {
+  FileDescriptor* self = ruby_to_FileDescriptor(_self);
+
+  if (cookie != c_only_cookie) {
+    rb_raise(rb_eRuntimeError,
+             "Descriptor objects may not be created from Ruby.");
+  }
+
+  self->descriptor_pool = descriptor_pool;
+  self->filedef = (const upb_filedef*)NUM2ULL(ptr);
+
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     FileDescriptor.name => name
+ *
+ * Returns the name of the file.
+ */
+static VALUE FileDescriptor_name(VALUE _self) {
+  FileDescriptor* self = ruby_to_FileDescriptor(_self);
+  const char* name = upb_filedef_name(self->filedef);
+  return name == NULL ? Qnil : rb_str_new2(name);
+}
+
+/*
+ * call-seq:
+ *     FileDescriptor.syntax => syntax
+ *
+ * Returns this file descriptors syntax.
+ *
+ * Valid syntax versions are:
+ *     :proto2 or :proto3.
+ */
+static VALUE FileDescriptor_syntax(VALUE _self) {
+  FileDescriptor* self = ruby_to_FileDescriptor(_self);
+
+  switch (upb_filedef_syntax(self->filedef)) {
+    case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
+    case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
+    default: return Qnil;
+  }
+}
+
+static void FileDescriptor_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "FileDescriptor", rb_cObject);
+  rb_define_alloc_func(klass, FileDescriptor_alloc);
+  rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
+  rb_define_method(klass, "name", FileDescriptor_name, 0);
+  rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
+  rb_gc_register_address(&cFileDescriptor);
+  cFileDescriptor = klass;
+}
+
+// -----------------------------------------------------------------------------
+// FieldDescriptor.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  const upb_fielddef* fielddef;
+  VALUE descriptor_pool;  // Owns the upb_fielddef.
+} FieldDescriptor;
+
+static VALUE cFieldDescriptor = Qnil;
+
+static void FieldDescriptor_mark(void* _self) {
+  FieldDescriptor* self = _self;
+  rb_gc_mark(self->descriptor_pool);
+}
+
+static const rb_data_type_t FieldDescriptor_type = {
+  "Google::Protobuf::FieldDescriptor",
+  {FieldDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static FieldDescriptor* ruby_to_FieldDescriptor(VALUE val) {
+  FieldDescriptor* ret;
+  TypedData_Get_Struct(val, FieldDescriptor, &FieldDescriptor_type, ret);
+  return ret;
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.new => field
+ *
+ * Returns a new field descriptor. Its name, type, etc. must be set before it is
+ * added to a message type.
+ */
+static VALUE FieldDescriptor_alloc(VALUE klass) {
+  FieldDescriptor* self = ALLOC(FieldDescriptor);
+  VALUE ret = TypedData_Wrap_Struct(klass, &FieldDescriptor_type, self);
+  self->fielddef = NULL;
+  return ret;
+}
+
+/*
+ * call-seq:
+ *    EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
+ *
+ * Creates a descriptor wrapper object.  May only be called from C.
+ */
+static VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
+                                        VALUE descriptor_pool, VALUE ptr) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+
+  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:
+ *     FieldDescriptor.name => name
+ *
+ * Returns the name of this field.
+ */
+static VALUE FieldDescriptor_name(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
+}
+
+// Non-static, exposed to other .c files.
+upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
+  if (TYPE(type) != T_SYMBOL) {
+    rb_raise(rb_eArgError, "Expected symbol for field type.");
+  }
+
+#define CONVERT(upb, ruby)                                           \
+  if (SYM2ID(type) == rb_intern( # ruby )) {                         \
+    return UPB_TYPE_ ## upb;                                         \
+  }
+
+  CONVERT(FLOAT, float);
+  CONVERT(DOUBLE, double);
+  CONVERT(BOOL, bool);
+  CONVERT(STRING, string);
+  CONVERT(BYTES, bytes);
+  CONVERT(MESSAGE, message);
+  CONVERT(ENUM, enum);
+  CONVERT(INT32, int32);
+  CONVERT(INT64, int64);
+  CONVERT(UINT32, uint32);
+  CONVERT(UINT64, uint64);
+
+#undef CONVERT
+
+  rb_raise(rb_eArgError, "Unknown field type.");
+  return 0;
+}
+
+static upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
+  if (TYPE(type) != T_SYMBOL) {
+    rb_raise(rb_eArgError, "Expected symbol for field type.");
+  }
+
+#define CONVERT(upb, ruby)                                           \
+  if (SYM2ID(type) == rb_intern( # ruby )) {                         \
+    return UPB_DESCRIPTOR_TYPE_ ## upb;                              \
+  }
+
+  CONVERT(FLOAT, float);
+  CONVERT(DOUBLE, double);
+  CONVERT(BOOL, bool);
+  CONVERT(STRING, string);
+  CONVERT(BYTES, bytes);
+  CONVERT(MESSAGE, message);
+  CONVERT(GROUP, group);
+  CONVERT(ENUM, enum);
+  CONVERT(INT32, int32);
+  CONVERT(INT64, int64);
+  CONVERT(UINT32, uint32);
+  CONVERT(UINT64, uint64);
+  CONVERT(SINT32, sint32);
+  CONVERT(SINT64, sint64);
+  CONVERT(FIXED32, fixed32);
+  CONVERT(FIXED64, fixed64);
+  CONVERT(SFIXED32, sfixed32);
+  CONVERT(SFIXED64, sfixed64);
+
+#undef CONVERT
+
+  rb_raise(rb_eArgError, "Unknown field type.");
+  return 0;
+}
+
+static VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
+  switch (type) {
+#define CONVERT(upb, ruby)                                           \
+    case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
+    CONVERT(FLOAT, float);
+    CONVERT(DOUBLE, double);
+    CONVERT(BOOL, bool);
+    CONVERT(STRING, string);
+    CONVERT(BYTES, bytes);
+    CONVERT(MESSAGE, message);
+    CONVERT(GROUP, group);
+    CONVERT(ENUM, enum);
+    CONVERT(INT32, int32);
+    CONVERT(INT64, int64);
+    CONVERT(UINT32, uint32);
+    CONVERT(UINT64, uint64);
+    CONVERT(SINT32, sint32);
+    CONVERT(SINT64, sint64);
+    CONVERT(FIXED32, fixed32);
+    CONVERT(FIXED64, fixed64);
+    CONVERT(SFIXED32, sfixed32);
+    CONVERT(SFIXED64, sfixed64);
+#undef CONVERT
+  }
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.type => type
+ *
+ * Returns this field's type, as a Ruby symbol, or nil if not yet set.
+ *
+ * Valid field types are:
+ *     :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
+ *     :bytes, :message.
+ */
+static VALUE FieldDescriptor__type(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.default => default
+ *
+ * Returns this field's default, as a Ruby object, or nil if not yet set.
+ */
+static VALUE FieldDescriptor_default(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  const upb_fielddef *f = self->fielddef;
+  upb_msgval default_val = {0};
+  if (upb_fielddef_issubmsg(f)) {
+    return Qnil;
+  } else if (!upb_fielddef_isseq(f)) {
+    default_val = upb_fielddef_default(f);
+  }
+  return Convert_UpbToRuby(default_val, TypeInfo_get(self->fielddef), Qnil);
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.label => label
+ *
+ * Returns this field's label (i.e., plurality), as a Ruby symbol.
+ *
+ * Valid field labels are:
+ *     :optional, :repeated
+ */
+static VALUE FieldDescriptor_label(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  switch (upb_fielddef_label(self->fielddef)) {
+#define CONVERT(upb, ruby)                                           \
+    case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
+
+    CONVERT(OPTIONAL, optional);
+    CONVERT(REQUIRED, required);
+    CONVERT(REPEATED, repeated);
+
+#undef CONVERT
+  }
+
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.number => number
+ *
+ * Returns the tag number for this field.
+ */
+static VALUE FieldDescriptor_number(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  return INT2NUM(upb_fielddef_number(self->fielddef));
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.submsg_name => submsg_name
+ *
+ * Returns the name of the message or enum type corresponding to this field, if
+ * it is a message or enum field (respectively), or nil otherwise. This type
+ * name will be resolved within the context of the pool to which the containing
+ * message type is added.
+ */
+static VALUE FieldDescriptor_submsg_name(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  switch (upb_fielddef_type(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;
+  }
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.subtype => message_or_enum_descriptor
+ *
+ * Returns the message or enum descriptor corresponding to this field's type if
+ * it is a message or enum field, respectively, or nil otherwise. Cannot be
+ * called *until* the containing message type is added to a pool (and thus
+ * resolved).
+ */
+static VALUE FieldDescriptor_subtype(VALUE _self) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  switch (upb_fielddef_type(self->fielddef)) {
+    case UPB_TYPE_ENUM:
+      return get_enumdef_obj(self->descriptor_pool,
+                             upb_fielddef_enumsubdef(self->fielddef));
+    case UPB_TYPE_MESSAGE:
+      return get_msgdef_obj(self->descriptor_pool,
+                            upb_fielddef_msgsubdef(self->fielddef));
+    default:
+      return Qnil;
+  }
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.get(message) => value
+ *
+ * Returns the value set for this field on the given message. Raises an
+ * exception if message is of the wrong type.
+ */
+static VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  const upb_msgdef *m;
+  const upb_msgdef *msg = Message_Get(msg_rb, &m);
+  VALUE arena = Message_GetArena(msg_rb);
+  upb_msgval msgval;
+
+  if (m != upb_fielddef_containingtype(self->fielddef)) {
+    rb_raise(cTypeError, "get method called on wrong message type");
+  }
+
+  msgval = upb_msg_get(msg, self->fielddef);
+  return Convert_UpbToRuby(msgval, TypeInfo_get(self->fielddef), arena);
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.has?(message) => boolean
+ *
+ * Returns whether the value is set on the given message. Raises an
+ * exception when calling for fields that do not have presence.
+ */
+static VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  const upb_msgdef *m;
+  const upb_msgdef *msg = Message_Get(msg_rb, &m);
+
+  if (m != upb_fielddef_containingtype(self->fielddef)) {
+    rb_raise(cTypeError, "has method called on wrong message type");
+  } else if (!upb_fielddef_haspresence(self->fielddef)) {
+    rb_raise(rb_eArgError, "does not track presence");
+  }
+
+  return upb_msg_has(msg, self->fielddef) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.clear(message)
+ *
+ * Clears the field from the message if it's set.
+ */
+static VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  const upb_msgdef *m;
+  upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
+
+  if (m != upb_fielddef_containingtype(self->fielddef)) {
+    rb_raise(cTypeError, "has method called on wrong message type");
+  }
+
+  upb_msg_clearfield(msg, self->fielddef);
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     FieldDescriptor.set(message, value)
+ *
+ * Sets the value corresponding to this field to the given value on the given
+ * message. Raises an exception if message is of the wrong type. Performs the
+ * ordinary type-checks for field setting.
+ */
+static VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
+  FieldDescriptor* self = ruby_to_FieldDescriptor(_self);
+  const upb_msgdef *m;
+  upb_msgdef *msg = Message_GetMutable(msg_rb, &m);
+  upb_arena *arena = Arena_get(Message_GetArena(msg_rb));
+  upb_msgval msgval;
+
+  if (m != upb_fielddef_containingtype(self->fielddef)) {
+    rb_raise(cTypeError, "set method called on wrong message type");
+  }
+
+  msgval = Convert_RubyToUpb(value, upb_fielddef_name(self->fielddef),
+                             TypeInfo_get(self->fielddef), arena);
+  upb_msg_set(msg, self->fielddef, msgval, arena);
+  return Qnil;
+}
+
+static void FieldDescriptor_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "FieldDescriptor", rb_cObject);
+  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, "type", FieldDescriptor__type, 0);
+  rb_define_method(klass, "default", FieldDescriptor_default, 0);
+  rb_define_method(klass, "label", FieldDescriptor_label, 0);
+  rb_define_method(klass, "number", FieldDescriptor_number, 0);
+  rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
+  rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
+  rb_define_method(klass, "has?", FieldDescriptor_has, 1);
+  rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
+  rb_define_method(klass, "get", FieldDescriptor_get, 1);
+  rb_define_method(klass, "set", FieldDescriptor_set, 2);
+  rb_gc_register_address(&cFieldDescriptor);
+  cFieldDescriptor = klass;
+}
+
+// -----------------------------------------------------------------------------
+// OneofDescriptor.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  const upb_oneofdef* oneofdef;
+  VALUE descriptor_pool;  // Owns the upb_oneofdef.
+} OneofDescriptor;
+
+static VALUE cOneofDescriptor = Qnil;
+
+static void OneofDescriptor_mark(void* _self) {
+  OneofDescriptor* self = _self;
+  rb_gc_mark(self->descriptor_pool);
+}
+
+static const rb_data_type_t OneofDescriptor_type = {
+    "Google::Protobuf::OneofDescriptor",
+    {OneofDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
+    .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static OneofDescriptor* ruby_to_OneofDescriptor(VALUE val) {
+  OneofDescriptor* ret;
+  TypedData_Get_Struct(val, OneofDescriptor, &OneofDescriptor_type, ret);
+  return ret;
+}
+
+/*
+ * call-seq:
+ *     OneofDescriptor.new => oneof_descriptor
+ *
+ * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
+ * to being added to a message descriptor which is subsequently added to a pool.
+ */
+static VALUE OneofDescriptor_alloc(VALUE klass) {
+  OneofDescriptor* self = ALLOC(OneofDescriptor);
+  VALUE ret = TypedData_Wrap_Struct(klass, &OneofDescriptor_type, self);
+  self->oneofdef = NULL;
+  self->descriptor_pool = Qnil;
+  return ret;
+}
+
+/*
+ * call-seq:
+ *    OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
+ *
+ * Creates a descriptor wrapper object.  May only be called from C.
+ */
+static VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
+                                 VALUE descriptor_pool, VALUE ptr) {
+  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
+
+  if (cookie != c_only_cookie) {
+    rb_raise(rb_eRuntimeError,
+             "Descriptor objects may not be created from Ruby.");
+  }
+
+  self->descriptor_pool = descriptor_pool;
+  self->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
+
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     OneofDescriptor.name => name
+ *
+ * Returns the name of this oneof.
+ */
+static VALUE OneofDescriptor_name(VALUE _self) {
+  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
+  return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
+}
+
+/*
+ * call-seq:
+ *     OneofDescriptor.each(&block) => nil
+ *
+ * Iterates through fields in this oneof, yielding to the block on each one.
+ */
+static VALUE OneofDescriptor_each(VALUE _self) {
+  OneofDescriptor* self = ruby_to_OneofDescriptor(_self);
+  upb_oneof_iter it;
+  for (upb_oneof_begin(&it, self->oneofdef);
+       !upb_oneof_done(&it);
+       upb_oneof_next(&it)) {
+    const upb_fielddef* f = upb_oneof_iter_field(&it);
+    VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
+    rb_yield(obj);
+  }
+  return Qnil;
+}
+
+static void OneofDescriptor_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "OneofDescriptor", rb_cObject);
+  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, "each", OneofDescriptor_each, 0);
+  rb_include_module(klass, rb_mEnumerable);
+  rb_gc_register_address(&cOneofDescriptor);
+  cOneofDescriptor = klass;
+}
+
+// -----------------------------------------------------------------------------
+// EnumDescriptor.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  const upb_enumdef* enumdef;
+  VALUE module;  // begins as nil
+  VALUE descriptor_pool;  // Owns the upb_enumdef.
+} EnumDescriptor;
+
+static VALUE cEnumDescriptor = Qnil;
+
+static void EnumDescriptor_mark(void* _self) {
+  EnumDescriptor* self = _self;
+  rb_gc_mark(self->module);
+  rb_gc_mark(self->descriptor_pool);
+}
+
+static const rb_data_type_t EnumDescriptor_type = {
+  "Google::Protobuf::EnumDescriptor",
+  {EnumDescriptor_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static EnumDescriptor* ruby_to_EnumDescriptor(VALUE val) {
+  EnumDescriptor* ret;
+  TypedData_Get_Struct(val, EnumDescriptor, &EnumDescriptor_type, ret);
+  return ret;
+}
+
+static VALUE EnumDescriptor_alloc(VALUE klass) {
+  EnumDescriptor* self = ALLOC(EnumDescriptor);
+  VALUE ret = TypedData_Wrap_Struct(klass, &EnumDescriptor_type, self);
+  self->enumdef = NULL;
+  self->module = Qnil;
+  self->descriptor_pool = Qnil;
+  return ret;
+}
+
+// Exposed to other modules in defs.h.
+const upb_enumdef *EnumDescriptor_GetEnumDef(VALUE enum_desc_rb) {
+  EnumDescriptor *desc = ruby_to_EnumDescriptor(enum_desc_rb);
+  return desc->enumdef;
+}
+
+/*
+ * call-seq:
+ *    EnumDescriptor.new(c_only_cookie, ptr) => EnumDescriptor
+ *
+ * Creates a descriptor wrapper object.  May only be called from C.
+ */
+static VALUE EnumDescriptor_initialize(VALUE _self, VALUE cookie,
+                                       VALUE descriptor_pool, VALUE ptr) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_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;
+}
+
+/*
+ * call-seq:
+ *    EnumDescriptor.file_descriptor
+ *
+ * Returns the FileDescriptor object this enum belongs to.
+ */
+static VALUE EnumDescriptor_file_descriptor(VALUE _self) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
+  return get_filedef_obj(self->descriptor_pool,
+                         upb_enumdef_file(self->enumdef));
+}
+
+/*
+ * call-seq:
+ *     EnumDescriptor.name => name
+ *
+ * Returns the name of this enum type.
+ */
+static VALUE EnumDescriptor_name(VALUE _self) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
+  return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
+}
+
+/*
+ * call-seq:
+ *     EnumDescriptor.lookup_name(name) => value
+ *
+ * Returns the numeric value corresponding to the given key name (as a Ruby
+ * symbol), or nil if none.
+ */
+static VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
+  const char* name_str= rb_id2name(SYM2ID(name));
+  int32_t val = 0;
+  if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
+    return INT2NUM(val);
+  } else {
+    return Qnil;
+  }
+}
+
+/*
+ * call-seq:
+ *     EnumDescriptor.lookup_value(name) => value
+ *
+ * Returns the key name (as a Ruby symbol) corresponding to the integer value,
+ * or nil if none.
+ */
+static VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
+  int32_t val = NUM2INT(number);
+  const char* name = upb_enumdef_iton(self->enumdef, val);
+  if (name != NULL) {
+    return ID2SYM(rb_intern(name));
+  } else {
+    return Qnil;
+  }
+}
+
+/*
+ * call-seq:
+ *     EnumDescriptor.each(&block)
+ *
+ * Iterates over key => value mappings in this enum's definition, yielding to
+ * the block with (key, value) arguments for each one.
+ */
+static VALUE EnumDescriptor_each(VALUE _self) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
+
+  upb_enum_iter it;
+  for (upb_enum_begin(&it, self->enumdef);
+       !upb_enum_done(&it);
+       upb_enum_next(&it)) {
+    VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
+    VALUE number = INT2NUM(upb_enum_iter_number(&it));
+    rb_yield_values(2, key, number);
+  }
+
+  return Qnil;
+}
+
+/*
+ * call-seq:
+ *     EnumDescriptor.enummodule => module
+ *
+ * Returns the Ruby module corresponding to this enum type.
+ */
+static VALUE EnumDescriptor_enummodule(VALUE _self) {
+  EnumDescriptor* self = ruby_to_EnumDescriptor(_self);
+  if (self->module == Qnil) {
+    self->module = build_module_from_enumdesc(_self);
+  }
+  return self->module;
+}
+
+static void EnumDescriptor_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "EnumDescriptor", rb_cObject);
+  rb_define_alloc_func(klass, EnumDescriptor_alloc);
+  rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
+  rb_define_method(klass, "name", EnumDescriptor_name, 0);
+  rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
+  rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
+  rb_define_method(klass, "each", EnumDescriptor_each, 0);
+  rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
+  rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
+  rb_include_module(klass, rb_mEnumerable);
+  rb_gc_register_address(&cEnumDescriptor);
+  cEnumDescriptor = klass;
+}
+
+// -----------------------------------------------------------------------------
+// FileBuilderContext.
+// -----------------------------------------------------------------------------
+
+typedef struct {
+  upb_arena *arena;
+  google_protobuf_FileDescriptorProto* file_proto;
+  VALUE descriptor_pool;
+} FileBuilderContext;
+
+static VALUE cFileBuilderContext = Qnil;
+
+static void FileBuilderContext_mark(void* _self) {
+  FileBuilderContext* self = _self;
+  rb_gc_mark(self->descriptor_pool);
+}
+
+static void FileBuilderContext_free(void* _self) {
+  FileBuilderContext* self = _self;
+  upb_arena_free(self->arena);
+  xfree(self);
+}
+
+static const rb_data_type_t FileBuilderContext_type = {
+  "Google::Protobuf::Internal::FileBuilderContext",
+  {FileBuilderContext_mark, FileBuilderContext_free, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
+
+static FileBuilderContext* ruby_to_FileBuilderContext(VALUE val) {
+  FileBuilderContext* ret;
+  TypedData_Get_Struct(val, FileBuilderContext, &FileBuilderContext_type, ret);
+  return ret;
+}
+
+static upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
+  FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
+  upb_strview ret;
+  char *data;
+
+  ret.size = strlen(str);
+  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;
+}
+
+static upb_strview FileBuilderContext_strdup(VALUE _self, VALUE rb_str) {
+  return FileBuilderContext_strdup2(_self, get_str(rb_str));
+}
+
+static upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
+  Check_Type(rb_sym, T_SYMBOL);
+  return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
+}
+
+static VALUE FileBuilderContext_alloc(VALUE klass) {
+  FileBuilderContext* self = ALLOC(FileBuilderContext);
+  VALUE ret = TypedData_Wrap_Struct(klass, &FileBuilderContext_type, self);
+  self->arena = upb_arena_new();
+  self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
+  self->descriptor_pool = Qnil;
+  return ret;
+}
+
+/*
+ * call-seq:
+ *     FileBuilderContext.new(descriptor_pool) => context
+ *
+ * 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
+ * with #instance_eval.
+ */
+static VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
+                                           VALUE name, VALUE options) {
+  FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
+  self->descriptor_pool = descriptor_pool;
+
+  google_protobuf_FileDescriptorProto_set_name(
+      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) {
+    VALUE syntax;
+
+    Check_Type(options, T_HASH);
+    syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
+
+    if (syntax != Qnil) {
+      VALUE syntax_str;
+
+      Check_Type(syntax, T_SYMBOL);
+      syntax_str = rb_id2str(SYM2ID(syntax));
+      google_protobuf_FileDescriptorProto_set_syntax(
+          self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
+    }
+  }
+
+  return Qnil;
+}
+
+static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self);
+
+/*
+ * call-seq:
+ *     FileBuilderContext.add_message(name, &block)
+ *
+ * Creates a new, empty descriptor with the given name, and invokes the block in
+ * the context of a MessageBuilderContext on that descriptor. The block can then
+ * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
+ * methods to define the message fields.
+ *
+ * This is the recommended, idiomatic way to build message definitions.
+ */
+static VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
+  VALUE args[2] = { _self, name };
+  VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
+  VALUE block = rb_block_proc();
+  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
+  MessageBuilderContext_add_synthetic_oneofs(ctx);
+  return Qnil;
+}
+
 /* We have to do some relatively complicated logic here for backward
  * compatibility.
  *
@@ -334,1113 +1598,83 @@
       file_proto, RARRAY_LEN(enum_ents), arena);
 }
 
-// -----------------------------------------------------------------------------
-// DescriptorPool.
-// -----------------------------------------------------------------------------
-
-#define DEFINE_CLASS(name, string_name)                             \
-    VALUE c ## name = Qnil;                                         \
-    const rb_data_type_t _ ## name ## _type = {                     \
-      string_name,                                                  \
-      { name ## _mark, name ## _free, NULL },                       \
-    };                                                              \
-    name* ruby_to_ ## name(VALUE val) {                             \
-      name* ret;                                                    \
-      TypedData_Get_Struct(val, name, &_ ## name ## _type, ret);    \
-      return ret;                                                   \
-    }                                                               \
-
-#define DEFINE_SELF(type, var, rb_var)                              \
-    type* var = ruby_to_ ## type(rb_var)
-
-// Global singleton DescriptorPool. The user is free to create others, but this
-// is used by generated code.
-VALUE generated_pool = Qnil;
-
-DEFINE_CLASS(DescriptorPool, "Google::Protobuf::DescriptorPool");
-
-void DescriptorPool_mark(void* _self) {
-  DescriptorPool* self = _self;
-  rb_gc_mark(self->def_to_descriptor);
-}
-
-void DescriptorPool_free(void* _self) {
-  DescriptorPool* self = _self;
-
-  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);
-}
-
 /*
  * call-seq:
- *     DescriptorPool.new => pool
+ *     FileBuilderContext.add_enum(name, &block)
  *
- * Creates a new, empty, descriptor pool.
- */
-VALUE DescriptorPool_alloc(VALUE klass) {
-  DescriptorPool* self = ALLOC(DescriptorPool);
-  VALUE ret;
-
-  self->def_to_descriptor = Qnil;
-  ret = TypedData_Wrap_Struct(klass, &_DescriptorPool_type, self);
-
-  self->def_to_descriptor = rb_hash_new();
-  self->symtab = upb_symtab_new();
-  self->fill_handler_cache =
-      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) {
-  VALUE klass = rb_define_class_under(
-      module, "DescriptorPool", rb_cObject);
-  rb_define_alloc_func(klass, DescriptorPool_alloc);
-  rb_define_method(klass, "build", DescriptorPool_build, -1);
-  rb_define_method(klass, "lookup", DescriptorPool_lookup, 1);
-  rb_define_singleton_method(klass, "generated_pool",
-                             DescriptorPool_generated_pool, 0);
-  rb_gc_register_address(&cDescriptorPool);
-  cDescriptorPool = klass;
-
-  rb_gc_register_address(&generated_pool);
-  generated_pool = rb_class_new_instance(0, NULL, klass);
-}
-
-/*
- * call-seq:
- *     DescriptorPool.build(&block)
+ * Creates a new, empty enum descriptor with the given name, and invokes the
+ * block in the context of an EnumBuilderContext on that descriptor. The block
+ * can then call EnumBuilderContext#add_value to define the enum values.
  *
- * Invokes the block with a Builder instance as self. All message and enum types
- * added within the block are committed to the pool atomically, and may refer
- * (co)recursively to each other. The user should call Builder#add_message and
- * Builder#add_enum within the block as appropriate.  This is the recommended,
- * idiomatic way to define new message and enum types.
+ * This is the recommended, idiomatic way to build enum definitions.
  */
-VALUE DescriptorPool_build(int argc, VALUE* argv, VALUE _self) {
-  VALUE ctx = rb_class_new_instance(1, &_self, cBuilder);
+static VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
+  VALUE args[2] = { _self, name };
+  VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
   VALUE block = rb_block_proc();
   rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
-  Builder_build(ctx);
   return Qnil;
 }
 
-/*
- * call-seq:
- *     DescriptorPool.lookup(name) => descriptor
- *
- * Finds a Descriptor or EnumDescriptor by name and returns it, or nil if none
- * exists with the given name.
- */
-VALUE DescriptorPool_lookup(VALUE _self, VALUE name) {
-  DEFINE_SELF(DescriptorPool, self, _self);
-  const char* name_str = get_str(name);
-  const upb_msgdef* msgdef;
-  const upb_enumdef* enumdef;
+static void FileBuilderContext_build(VALUE _self) {
+  FileBuilderContext* self = ruby_to_FileBuilderContext(_self);
+  DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
+  upb_status status;
 
-  msgdef = upb_symtab_lookupmsg(self->symtab, name_str);
-  if (msgdef) {
-    return get_msgdef_obj(_self, msgdef);
-  }
+  rewrite_enum_defaults(pool->symtab, self->file_proto);
+  rewrite_names(_self, self->file_proto);
 
-  enumdef = upb_symtab_lookupenum(self->symtab, name_str);
-  if (enumdef) {
-    return get_enumdef_obj(_self, enumdef);
-  }
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     DescriptorPool.generated_pool => descriptor_pool
- *
- * Class method that returns the global DescriptorPool. This is a singleton into
- * which generated-code message and enum types are registered. The user may also
- * register types in this pool for convenience so that they do not have to hold
- * a reference to a private pool instance.
- */
-VALUE DescriptorPool_generated_pool(VALUE _self) {
-  return generated_pool;
-}
-
-// -----------------------------------------------------------------------------
-// Descriptor.
-// -----------------------------------------------------------------------------
-
-DEFINE_CLASS(Descriptor, "Google::Protobuf::Descriptor");
-
-void Descriptor_mark(void* _self) {
-  Descriptor* self = _self;
-  rb_gc_mark(self->klass);
-  rb_gc_mark(self->descriptor_pool);
-  if (self->layout && self->layout->empty_template) {
-    layout_mark(self->layout, self->layout->empty_template);
+  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));
   }
 }
 
-void Descriptor_free(void* _self) {
-  Descriptor* self = _self;
-  if (self->layout) {
-    free_layout(self->layout);
-  }
-  xfree(self);
-}
-
-/*
- * call-seq:
- *     Descriptor.new => descriptor
- *
- * Creates a new, empty, message type descriptor. At a minimum, its name must be
- * set before it is added to a pool. It cannot be used to create messages until
- * it is added to a pool, after which it becomes immutable (as part of a
- * finalization process).
- */
-VALUE Descriptor_alloc(VALUE klass) {
-  Descriptor* self = ALLOC(Descriptor);
-  VALUE ret = TypedData_Wrap_Struct(klass, &_Descriptor_type, self);
-  self->msgdef = NULL;
-  self->klass = Qnil;
-  self->descriptor_pool = Qnil;
-  self->layout = NULL;
-  return ret;
-}
-
-void Descriptor_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "Descriptor", rb_cObject);
-  rb_define_alloc_func(klass, Descriptor_alloc);
-  rb_define_method(klass, "initialize", Descriptor_initialize, 3);
-  rb_define_method(klass, "each", Descriptor_each, 0);
-  rb_define_method(klass, "lookup", Descriptor_lookup, 1);
-  rb_define_method(klass, "each_oneof", Descriptor_each_oneof, 0);
-  rb_define_method(klass, "lookup_oneof", Descriptor_lookup_oneof, 1);
-  rb_define_method(klass, "msgclass", Descriptor_msgclass, 0);
-  rb_define_method(klass, "name", Descriptor_name, 0);
-  rb_define_method(klass, "file_descriptor", Descriptor_file_descriptor, 0);
-  rb_include_module(klass, rb_mEnumerable);
-  rb_gc_register_address(&cDescriptor);
-  cDescriptor = klass;
-}
-
-/*
- * call-seq:
- *    Descriptor.new(c_only_cookie, ptr) => Descriptor
- *
- * Creates a descriptor wrapper object.  May only be called from C.
- */
-VALUE Descriptor_initialize(VALUE _self, VALUE cookie,
-                            VALUE descriptor_pool, VALUE ptr) {
-  DEFINE_SELF(Descriptor, 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->msgdef = (const upb_msgdef*)NUM2ULL(ptr);
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *    Descriptor.file_descriptor
- *
- * Returns the FileDescriptor object this message belongs to.
- */
-VALUE Descriptor_file_descriptor(VALUE _self) {
-  DEFINE_SELF(Descriptor, self, _self);
-  return get_filedef_obj(self->descriptor_pool, upb_msgdef_file(self->msgdef));
-}
-
-/*
- * call-seq:
- *     Descriptor.name => name
- *
- * Returns the name of this message type as a fully-qualified string (e.g.,
- * My.Package.MessageType).
- */
-VALUE Descriptor_name(VALUE _self) {
-  DEFINE_SELF(Descriptor, self, _self);
-  return rb_str_maybe_null(upb_msgdef_fullname(self->msgdef));
-}
-
-/*
- * call-seq:
- *     Descriptor.each(&block)
- *
- * Iterates over fields in this message type, yielding to the block on each one.
- */
-VALUE Descriptor_each(VALUE _self) {
-  DEFINE_SELF(Descriptor, self, _self);
-
-  upb_msg_field_iter it;
-  for (upb_msg_field_begin(&it, self->msgdef);
-       !upb_msg_field_done(&it);
-       upb_msg_field_next(&it)) {
-    const upb_fielddef* field = upb_msg_iter_field(&it);
-    VALUE obj = get_fielddef_obj(self->descriptor_pool, field);
-    rb_yield(obj);
-  }
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     Descriptor.lookup(name) => FieldDescriptor
- *
- * Returns the field descriptor for the field with the given name, if present,
- * or nil if none.
- */
-VALUE Descriptor_lookup(VALUE _self, VALUE name) {
-  DEFINE_SELF(Descriptor, self, _self);
-  const char* s = get_str(name);
-  const upb_fielddef* field = upb_msgdef_ntofz(self->msgdef, s);
-  if (field == NULL) {
-    return Qnil;
-  }
-  return get_fielddef_obj(self->descriptor_pool, field);
-}
-
-/*
- * call-seq:
- *     Descriptor.each_oneof(&block) => nil
- *
- * Invokes the given block for each oneof in this message type, passing the
- * corresponding OneofDescriptor.
- */
-VALUE Descriptor_each_oneof(VALUE _self) {
-  DEFINE_SELF(Descriptor, self, _self);
-
-  upb_msg_oneof_iter it;
-  for (upb_msg_oneof_begin(&it, self->msgdef);
-       !upb_msg_oneof_done(&it);
-       upb_msg_oneof_next(&it)) {
-    const upb_oneofdef* oneof = upb_msg_iter_oneof(&it);
-    VALUE obj = get_oneofdef_obj(self->descriptor_pool, oneof);
-    rb_yield(obj);
-  }
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     Descriptor.lookup_oneof(name) => OneofDescriptor
- *
- * Returns the oneof descriptor for the oneof with the given name, if present,
- * or nil if none.
- */
-VALUE Descriptor_lookup_oneof(VALUE _self, VALUE name) {
-  DEFINE_SELF(Descriptor, self, _self);
-  const char* s = get_str(name);
-  const upb_oneofdef* oneof = upb_msgdef_ntooz(self->msgdef, s);
-  if (oneof == NULL) {
-    return Qnil;
-  }
-  return get_oneofdef_obj(self->descriptor_pool, oneof);
-}
-
-/*
- * call-seq:
- *     Descriptor.msgclass => message_klass
- *
- * Returns the Ruby class created for this message type.
- */
-VALUE Descriptor_msgclass(VALUE _self) {
-  DEFINE_SELF(Descriptor, self, _self);
-  if (self->klass == Qnil) {
-    self->klass = build_class_from_descriptor(_self);
-  }
-  return self->klass;
-}
-
-// -----------------------------------------------------------------------------
-// FileDescriptor.
-// -----------------------------------------------------------------------------
-
-DEFINE_CLASS(FileDescriptor, "Google::Protobuf::FileDescriptor");
-
-void FileDescriptor_mark(void* _self) {
-  FileDescriptor* self = _self;
-  rb_gc_mark(self->descriptor_pool);
-}
-
-void FileDescriptor_free(void* _self) {
-  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;
-}
-
-/*
- * call-seq:
- *     FileDescriptor.new => file
- *
- * Returns a new file descriptor. The syntax must be set before it's passed
- * to a builder.
- */
-VALUE FileDescriptor_initialize(VALUE _self, VALUE cookie,
-                                VALUE descriptor_pool, VALUE ptr) {
-  DEFINE_SELF(FileDescriptor, 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->filedef = (const upb_filedef*)NUM2ULL(ptr);
-
-  return Qnil;
-}
-
-void FileDescriptor_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "FileDescriptor", rb_cObject);
-  rb_define_alloc_func(klass, FileDescriptor_alloc);
-  rb_define_method(klass, "initialize", FileDescriptor_initialize, 3);
-  rb_define_method(klass, "name", FileDescriptor_name, 0);
-  rb_define_method(klass, "syntax", FileDescriptor_syntax, 0);
-  rb_gc_register_address(&cFileDescriptor);
-  cFileDescriptor = klass;
-}
-
-/*
- * call-seq:
- *     FileDescriptor.name => name
- *
- * Returns the name of the file.
- */
-VALUE FileDescriptor_name(VALUE _self) {
-  DEFINE_SELF(FileDescriptor, self, _self);
-  const char* name = upb_filedef_name(self->filedef);
-  return name == NULL ? Qnil : rb_str_new2(name);
-}
-
-/*
- * call-seq:
- *     FileDescriptor.syntax => syntax
- *
- * Returns this file descriptors syntax.
- *
- * Valid syntax versions are:
- *     :proto2 or :proto3.
- */
-VALUE FileDescriptor_syntax(VALUE _self) {
-  DEFINE_SELF(FileDescriptor, self, _self);
-
-  switch (upb_filedef_syntax(self->filedef)) {
-    case UPB_SYNTAX_PROTO3: return ID2SYM(rb_intern("proto3"));
-    case UPB_SYNTAX_PROTO2: return ID2SYM(rb_intern("proto2"));
-    default: return Qnil;
-  }
-}
-
-// -----------------------------------------------------------------------------
-// FieldDescriptor.
-// -----------------------------------------------------------------------------
-
-DEFINE_CLASS(FieldDescriptor, "Google::Protobuf::FieldDescriptor");
-
-void FieldDescriptor_mark(void* _self) {
-  FieldDescriptor* self = _self;
-  rb_gc_mark(self->descriptor_pool);
-}
-
-void FieldDescriptor_free(void* _self) {
-  xfree(_self);
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.new => field
- *
- * Returns a new field descriptor. Its name, type, etc. must be set before it is
- * added to a message type.
- */
-VALUE FieldDescriptor_alloc(VALUE klass) {
-  FieldDescriptor* self = ALLOC(FieldDescriptor);
-  VALUE ret = TypedData_Wrap_Struct(klass, &_FieldDescriptor_type, self);
-  self->fielddef = NULL;
-  return ret;
-}
-
-void FieldDescriptor_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "FieldDescriptor", rb_cObject);
-  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, "type", FieldDescriptor_type, 0);
-  rb_define_method(klass, "default", FieldDescriptor_default, 0);
-  rb_define_method(klass, "label", FieldDescriptor_label, 0);
-  rb_define_method(klass, "number", FieldDescriptor_number, 0);
-  rb_define_method(klass, "submsg_name", FieldDescriptor_submsg_name, 0);
-  rb_define_method(klass, "subtype", FieldDescriptor_subtype, 0);
-  rb_define_method(klass, "has?", FieldDescriptor_has, 1);
-  rb_define_method(klass, "clear", FieldDescriptor_clear, 1);
-  rb_define_method(klass, "get", FieldDescriptor_get, 1);
-  rb_define_method(klass, "set", FieldDescriptor_set, 2);
-  rb_gc_register_address(&cFieldDescriptor);
-  cFieldDescriptor = klass;
-}
-
-/*
- * call-seq:
- *    EnumDescriptor.new(c_only_cookie, pool, ptr) => EnumDescriptor
- *
- * Creates a descriptor wrapper object.  May only be called from C.
- */
-VALUE FieldDescriptor_initialize(VALUE _self, VALUE cookie,
-                                 VALUE descriptor_pool, VALUE ptr) {
-  DEFINE_SELF(FieldDescriptor, 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->fielddef = (const upb_fielddef*)NUM2ULL(ptr);
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.name => name
- *
- * Returns the name of this field.
- */
-VALUE FieldDescriptor_name(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  return rb_str_maybe_null(upb_fielddef_name(self->fielddef));
-}
-
-upb_fieldtype_t ruby_to_fieldtype(VALUE type) {
-  if (TYPE(type) != T_SYMBOL) {
-    rb_raise(rb_eArgError, "Expected symbol for field type.");
-  }
-
-#define CONVERT(upb, ruby)                                           \
-  if (SYM2ID(type) == rb_intern( # ruby )) {                         \
-    return UPB_TYPE_ ## upb;                                         \
-  }
-
-  CONVERT(FLOAT, float);
-  CONVERT(DOUBLE, double);
-  CONVERT(BOOL, bool);
-  CONVERT(STRING, string);
-  CONVERT(BYTES, bytes);
-  CONVERT(MESSAGE, message);
-  CONVERT(ENUM, enum);
-  CONVERT(INT32, int32);
-  CONVERT(INT64, int64);
-  CONVERT(UINT32, uint32);
-  CONVERT(UINT64, uint64);
-
-#undef CONVERT
-
-  rb_raise(rb_eArgError, "Unknown field type.");
-  return 0;
-}
-
-VALUE fieldtype_to_ruby(upb_fieldtype_t type) {
-  switch (type) {
-#define CONVERT(upb, ruby)                                           \
-    case UPB_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
-    CONVERT(FLOAT, float);
-    CONVERT(DOUBLE, double);
-    CONVERT(BOOL, bool);
-    CONVERT(STRING, string);
-    CONVERT(BYTES, bytes);
-    CONVERT(MESSAGE, message);
-    CONVERT(ENUM, enum);
-    CONVERT(INT32, int32);
-    CONVERT(INT64, int64);
-    CONVERT(UINT32, uint32);
-    CONVERT(UINT64, uint64);
-#undef CONVERT
-  }
-  return Qnil;
-}
-
-upb_descriptortype_t ruby_to_descriptortype(VALUE type) {
-  if (TYPE(type) != T_SYMBOL) {
-    rb_raise(rb_eArgError, "Expected symbol for field type.");
-  }
-
-#define CONVERT(upb, ruby)                                           \
-  if (SYM2ID(type) == rb_intern( # ruby )) {                         \
-    return UPB_DESCRIPTOR_TYPE_ ## upb;                              \
-  }
-
-  CONVERT(FLOAT, float);
-  CONVERT(DOUBLE, double);
-  CONVERT(BOOL, bool);
-  CONVERT(STRING, string);
-  CONVERT(BYTES, bytes);
-  CONVERT(MESSAGE, message);
-  CONVERT(GROUP, group);
-  CONVERT(ENUM, enum);
-  CONVERT(INT32, int32);
-  CONVERT(INT64, int64);
-  CONVERT(UINT32, uint32);
-  CONVERT(UINT64, uint64);
-  CONVERT(SINT32, sint32);
-  CONVERT(SINT64, sint64);
-  CONVERT(FIXED32, fixed32);
-  CONVERT(FIXED64, fixed64);
-  CONVERT(SFIXED32, sfixed32);
-  CONVERT(SFIXED64, sfixed64);
-
-#undef CONVERT
-
-  rb_raise(rb_eArgError, "Unknown field type.");
-  return 0;
-}
-
-VALUE descriptortype_to_ruby(upb_descriptortype_t type) {
-  switch (type) {
-#define CONVERT(upb, ruby)                                           \
-    case UPB_DESCRIPTOR_TYPE_ ## upb : return ID2SYM(rb_intern( # ruby ));
-    CONVERT(FLOAT, float);
-    CONVERT(DOUBLE, double);
-    CONVERT(BOOL, bool);
-    CONVERT(STRING, string);
-    CONVERT(BYTES, bytes);
-    CONVERT(MESSAGE, message);
-    CONVERT(GROUP, group);
-    CONVERT(ENUM, enum);
-    CONVERT(INT32, int32);
-    CONVERT(INT64, int64);
-    CONVERT(UINT32, uint32);
-    CONVERT(UINT64, uint64);
-    CONVERT(SINT32, sint32);
-    CONVERT(SINT64, sint64);
-    CONVERT(FIXED32, fixed32);
-    CONVERT(FIXED64, fixed64);
-    CONVERT(SFIXED32, sfixed32);
-    CONVERT(SFIXED64, sfixed64);
-#undef CONVERT
-  }
-  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:
- *     FieldDescriptor.type => type
- *
- * Returns this field's type, as a Ruby symbol, or nil if not yet set.
- *
- * Valid field types are:
- *     :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
- *     :bytes, :message.
- */
-VALUE FieldDescriptor_type(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  return descriptortype_to_ruby(upb_fielddef_descriptortype(self->fielddef));
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.default => default
- *
- * Returns this field's default, as a Ruby object, or nil if not yet set.
- */
-VALUE FieldDescriptor_default(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  return layout_get_default(self->fielddef);
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.label => label
- *
- * Returns this field's label (i.e., plurality), as a Ruby symbol.
- *
- * Valid field labels are:
- *     :optional, :repeated
- */
-VALUE FieldDescriptor_label(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  switch (upb_fielddef_label(self->fielddef)) {
-#define CONVERT(upb, ruby)                                           \
-    case UPB_LABEL_ ## upb : return ID2SYM(rb_intern( # ruby ));
-
-    CONVERT(OPTIONAL, optional);
-    CONVERT(REQUIRED, required);
-    CONVERT(REPEATED, repeated);
-
-#undef CONVERT
-  }
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.number => number
- *
- * Returns the tag number for this field.
- */
-VALUE FieldDescriptor_number(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  return INT2NUM(upb_fielddef_number(self->fielddef));
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.submsg_name => submsg_name
- *
- * Returns the name of the message or enum type corresponding to this field, if
- * it is a message or enum field (respectively), or nil otherwise. This type
- * name will be resolved within the context of the pool to which the containing
- * message type is added.
- */
-VALUE FieldDescriptor_submsg_name(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  switch (upb_fielddef_type(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;
-  }
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.subtype => message_or_enum_descriptor
- *
- * Returns the message or enum descriptor corresponding to this field's type if
- * it is a message or enum field, respectively, or nil otherwise. Cannot be
- * called *until* the containing message type is added to a pool (and thus
- * resolved).
- */
-VALUE FieldDescriptor_subtype(VALUE _self) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  switch (upb_fielddef_type(self->fielddef)) {
-    case UPB_TYPE_ENUM:
-      return get_enumdef_obj(self->descriptor_pool,
-                             upb_fielddef_enumsubdef(self->fielddef));
-    case UPB_TYPE_MESSAGE:
-      return get_msgdef_obj(self->descriptor_pool,
-                            upb_fielddef_msgsubdef(self->fielddef));
-    default:
-      return Qnil;
-  }
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.get(message) => value
- *
- * Returns the value set for this field on the given message. Raises an
- * exception if message is of the wrong type.
- */
-VALUE FieldDescriptor_get(VALUE _self, VALUE msg_rb) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  MessageHeader* msg;
-  TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
-  if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
-    rb_raise(cTypeError, "get method called on wrong message type");
-  }
-  return layout_get(msg->descriptor->layout, Message_data(msg), self->fielddef);
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.has?(message) => boolean
- *
- * Returns whether the value is set on the given message. Raises an
- * exception when calling for fields that do not have presence.
- */
-VALUE FieldDescriptor_has(VALUE _self, VALUE msg_rb) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  MessageHeader* msg;
-  TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
-  if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
-    rb_raise(cTypeError, "has method called on wrong message type");
-  } else if (!upb_fielddef_haspresence(self->fielddef)) {
-    rb_raise(rb_eArgError, "does not track presence");
-  }
-
-  return layout_has(msg->descriptor->layout, Message_data(msg), self->fielddef);
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.clear(message)
- *
- * Clears the field from the message if it's set.
- */
-VALUE FieldDescriptor_clear(VALUE _self, VALUE msg_rb) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  MessageHeader* msg;
-  TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
-  if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
-    rb_raise(cTypeError, "has method called on wrong message type");
-  }
-
-  layout_clear(msg->descriptor->layout, Message_data(msg), self->fielddef);
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     FieldDescriptor.set(message, value)
- *
- * Sets the value corresponding to this field to the given value on the given
- * message. Raises an exception if message is of the wrong type. Performs the
- * ordinary type-checks for field setting.
- */
-VALUE FieldDescriptor_set(VALUE _self, VALUE msg_rb, VALUE value) {
-  DEFINE_SELF(FieldDescriptor, self, _self);
-  MessageHeader* msg;
-  TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
-  if (msg->descriptor->msgdef != upb_fielddef_containingtype(self->fielddef)) {
-    rb_raise(cTypeError, "set method called on wrong message type");
-  }
-  layout_set(msg->descriptor->layout, Message_data(msg), self->fielddef, value);
-  return Qnil;
-}
-
-// -----------------------------------------------------------------------------
-// OneofDescriptor.
-// -----------------------------------------------------------------------------
-
-DEFINE_CLASS(OneofDescriptor, "Google::Protobuf::OneofDescriptor");
-
-void OneofDescriptor_mark(void* _self) {
-  OneofDescriptor* self = _self;
-  rb_gc_mark(self->descriptor_pool);
-}
-
-void OneofDescriptor_free(void* _self) {
-  xfree(_self);
-}
-
-/*
- * call-seq:
- *     OneofDescriptor.new => oneof_descriptor
- *
- * Creates a new, empty, oneof descriptor. The oneof may only be modified prior
- * to being added to a message descriptor which is subsequently added to a pool.
- */
-VALUE OneofDescriptor_alloc(VALUE klass) {
-  OneofDescriptor* self = ALLOC(OneofDescriptor);
-  VALUE ret = TypedData_Wrap_Struct(klass, &_OneofDescriptor_type, self);
-  self->oneofdef = NULL;
-  self->descriptor_pool = Qnil;
-  return ret;
-}
-
-void OneofDescriptor_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "OneofDescriptor", rb_cObject);
-  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, "each", OneofDescriptor_each, 0);
-  rb_include_module(klass, rb_mEnumerable);
-  rb_gc_register_address(&cOneofDescriptor);
-  cOneofDescriptor = klass;
-}
-
-/*
- * call-seq:
- *    OneofDescriptor.new(c_only_cookie, pool, ptr) => OneofDescriptor
- *
- * Creates a descriptor wrapper object.  May only be called from C.
- */
-VALUE OneofDescriptor_initialize(VALUE _self, VALUE cookie,
-                                 VALUE descriptor_pool, VALUE ptr) {
-  DEFINE_SELF(OneofDescriptor, 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->oneofdef = (const upb_oneofdef*)NUM2ULL(ptr);
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     OneofDescriptor.name => name
- *
- * Returns the name of this oneof.
- */
-VALUE OneofDescriptor_name(VALUE _self) {
-  DEFINE_SELF(OneofDescriptor, self, _self);
-  return rb_str_maybe_null(upb_oneofdef_name(self->oneofdef));
-}
-
-/*
- * call-seq:
- *     OneofDescriptor.each(&block) => nil
- *
- * Iterates through fields in this oneof, yielding to the block on each one.
- */
-VALUE OneofDescriptor_each(VALUE _self) {
-  DEFINE_SELF(OneofDescriptor, self, _self);
-  upb_oneof_iter it;
-  for (upb_oneof_begin(&it, self->oneofdef);
-       !upb_oneof_done(&it);
-       upb_oneof_next(&it)) {
-    const upb_fielddef* f = upb_oneof_iter_field(&it);
-    VALUE obj = get_fielddef_obj(self->descriptor_pool, f);
-    rb_yield(obj);
-  }
-  return Qnil;
-}
-
-// -----------------------------------------------------------------------------
-// EnumDescriptor.
-// -----------------------------------------------------------------------------
-
-DEFINE_CLASS(EnumDescriptor, "Google::Protobuf::EnumDescriptor");
-
-void EnumDescriptor_mark(void* _self) {
-  EnumDescriptor* self = _self;
-  rb_gc_mark(self->module);
-  rb_gc_mark(self->descriptor_pool);
-}
-
-void EnumDescriptor_free(void* _self) {
-  xfree(_self);
-}
-
-VALUE EnumDescriptor_alloc(VALUE klass) {
-  EnumDescriptor* self = ALLOC(EnumDescriptor);
-  VALUE ret = TypedData_Wrap_Struct(klass, &_EnumDescriptor_type, self);
-  self->enumdef = NULL;
-  self->module = Qnil;
-  self->descriptor_pool = Qnil;
-  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) {
-  VALUE klass = rb_define_class_under(
-      module, "EnumDescriptor", rb_cObject);
-  rb_define_alloc_func(klass, EnumDescriptor_alloc);
-  rb_define_method(klass, "initialize", EnumDescriptor_initialize, 3);
-  rb_define_method(klass, "name", EnumDescriptor_name, 0);
-  rb_define_method(klass, "lookup_name", EnumDescriptor_lookup_name, 1);
-  rb_define_method(klass, "lookup_value", EnumDescriptor_lookup_value, 1);
-  rb_define_method(klass, "each", EnumDescriptor_each, 0);
-  rb_define_method(klass, "enummodule", EnumDescriptor_enummodule, 0);
-  rb_define_method(klass, "file_descriptor", EnumDescriptor_file_descriptor, 0);
-  rb_include_module(klass, rb_mEnumerable);
-  rb_gc_register_address(&cEnumDescriptor);
-  cEnumDescriptor = klass;
-}
-
-/*
- * call-seq:
- *    EnumDescriptor.file_descriptor
- *
- * Returns the FileDescriptor object this enum belongs to.
- */
-VALUE EnumDescriptor_file_descriptor(VALUE _self) {
-  DEFINE_SELF(EnumDescriptor, self, _self);
-  return get_filedef_obj(self->descriptor_pool,
-                         upb_enumdef_file(self->enumdef));
-}
-
-/*
- * call-seq:
- *     EnumDescriptor.name => name
- *
- * Returns the name of this enum type.
- */
-VALUE EnumDescriptor_name(VALUE _self) {
-  DEFINE_SELF(EnumDescriptor, self, _self);
-  return rb_str_maybe_null(upb_enumdef_fullname(self->enumdef));
-}
-
-/*
- * call-seq:
- *     EnumDescriptor.lookup_name(name) => value
- *
- * Returns the numeric value corresponding to the given key name (as a Ruby
- * symbol), or nil if none.
- */
-VALUE EnumDescriptor_lookup_name(VALUE _self, VALUE name) {
-  DEFINE_SELF(EnumDescriptor, self, _self);
-  const char* name_str= rb_id2name(SYM2ID(name));
-  int32_t val = 0;
-  if (upb_enumdef_ntoiz(self->enumdef, name_str, &val)) {
-    return INT2NUM(val);
-  } else {
-    return Qnil;
-  }
-}
-
-/*
- * call-seq:
- *     EnumDescriptor.lookup_value(name) => value
- *
- * Returns the key name (as a Ruby symbol) corresponding to the integer value,
- * or nil if none.
- */
-VALUE EnumDescriptor_lookup_value(VALUE _self, VALUE number) {
-  DEFINE_SELF(EnumDescriptor, self, _self);
-  int32_t val = NUM2INT(number);
-  const char* name = upb_enumdef_iton(self->enumdef, val);
-  if (name != NULL) {
-    return ID2SYM(rb_intern(name));
-  } else {
-    return Qnil;
-  }
-}
-
-/*
- * call-seq:
- *     EnumDescriptor.each(&block)
- *
- * Iterates over key => value mappings in this enum's definition, yielding to
- * the block with (key, value) arguments for each one.
- */
-VALUE EnumDescriptor_each(VALUE _self) {
-  DEFINE_SELF(EnumDescriptor, self, _self);
-
-  upb_enum_iter it;
-  for (upb_enum_begin(&it, self->enumdef);
-       !upb_enum_done(&it);
-       upb_enum_next(&it)) {
-    VALUE key = ID2SYM(rb_intern(upb_enum_iter_name(&it)));
-    VALUE number = INT2NUM(upb_enum_iter_number(&it));
-    rb_yield_values(2, key, number);
-  }
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     EnumDescriptor.enummodule => module
- *
- * Returns the Ruby module corresponding to this enum type.
- */
-VALUE EnumDescriptor_enummodule(VALUE _self) {
-  DEFINE_SELF(EnumDescriptor, self, _self);
-  if (self->module == Qnil) {
-    self->module = build_module_from_enumdesc(_self);
-  }
-  return self->module;
+static void FileBuilderContext_register(VALUE module) {
+  VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
+  rb_define_alloc_func(klass, FileBuilderContext_alloc);
+  rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
+  rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
+  rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
+  rb_gc_register_address(&cFileBuilderContext);
+  cFileBuilderContext = klass;
 }
 
 // -----------------------------------------------------------------------------
 // MessageBuilderContext.
 // -----------------------------------------------------------------------------
 
-DEFINE_CLASS(MessageBuilderContext,
-    "Google::Protobuf::Internal::MessageBuilderContext");
+typedef struct {
+  google_protobuf_DescriptorProto* msg_proto;
+  VALUE file_builder;
+} MessageBuilderContext;
 
-void MessageBuilderContext_mark(void* _self) {
+static VALUE cMessageBuilderContext = Qnil;
+
+static void MessageBuilderContext_mark(void* _self) {
   MessageBuilderContext* self = _self;
   rb_gc_mark(self->file_builder);
 }
 
-void MessageBuilderContext_free(void* _self) {
-  MessageBuilderContext* self = _self;
-  xfree(self);
-}
+static const rb_data_type_t MessageBuilderContext_type = {
+  "Google::Protobuf::Internal::MessageBuilderContext",
+  {MessageBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
 
-VALUE MessageBuilderContext_alloc(VALUE klass) {
-  MessageBuilderContext* self = ALLOC(MessageBuilderContext);
-  VALUE ret = TypedData_Wrap_Struct(
-      klass, &_MessageBuilderContext_type, self);
-  self->file_builder = Qnil;
+static MessageBuilderContext* ruby_to_MessageBuilderContext(VALUE val) {
+  MessageBuilderContext* ret;
+  TypedData_Get_Struct(val, MessageBuilderContext, &MessageBuilderContext_type,
+                       ret);
   return ret;
 }
 
-void MessageBuilderContext_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "MessageBuilderContext", rb_cObject);
-  rb_define_alloc_func(klass, MessageBuilderContext_alloc);
-  rb_define_method(klass, "initialize",
-                   MessageBuilderContext_initialize, 2);
-  rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
-  rb_define_method(klass, "proto3_optional", MessageBuilderContext_proto3_optional, -1);
-  rb_define_method(klass, "required", MessageBuilderContext_required, -1);
-  rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
-  rb_define_method(klass, "map", MessageBuilderContext_map, -1);
-  rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
-  rb_gc_register_address(&cMessageBuilderContext);
-  cMessageBuilderContext = klass;
+static VALUE MessageBuilderContext_alloc(VALUE klass) {
+  MessageBuilderContext* self = ALLOC(MessageBuilderContext);
+  VALUE ret = TypedData_Wrap_Struct(klass, &MessageBuilderContext_type, self);
+  self->file_builder = Qnil;
+  return ret;
 }
 
 /*
@@ -1451,10 +1685,9 @@
  * builder context. This class is intended to serve as a DSL context to be used
  * with #instance_eval.
  */
-VALUE MessageBuilderContext_initialize(VALUE _self,
-                                       VALUE _file_builder,
-                                       VALUE name) {
-  DEFINE_SELF(MessageBuilderContext, self, _self);
+static VALUE MessageBuilderContext_initialize(VALUE _self, VALUE _file_builder,
+                                              VALUE name) {
+  MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
   FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
   google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
 
@@ -1472,7 +1705,7 @@
                              VALUE type, VALUE number, VALUE type_class,
                              VALUE options, int oneof_index,
                              bool proto3_optional) {
-  DEFINE_SELF(MessageBuilderContext, self, msgbuilder_rb);
+  MessageBuilderContext* self = ruby_to_MessageBuilderContext(msgbuilder_rb);
   FileBuilderContext* file_context =
       ruby_to_FileBuilderContext(self->file_builder);
   google_protobuf_FieldDescriptorProto* field_proto;
@@ -1527,9 +1760,16 @@
   }
 }
 
+#if RUBY_API_VERSION_CODE >= 20700
+static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
+                           const VALUE* argv, VALUE blockarg) {
+  (void)blockarg;
+#else
 static VALUE make_mapentry(VALUE _message_builder, VALUE types, int argc,
                            VALUE* argv) {
-  DEFINE_SELF(MessageBuilderContext, message_builder, _message_builder);
+#endif
+  MessageBuilderContext* message_builder =
+      ruby_to_MessageBuilderContext(_message_builder);
   VALUE type_class = rb_ary_entry(types, 2);
   FileBuilderContext* file_context =
       ruby_to_FileBuilderContext(message_builder->file_builder);
@@ -1596,8 +1836,8 @@
  * FieldDescriptor#type=) and the type_class must be a string, if present (as
  * accepted by FieldDescriptor#submsg_name=).
  */
-VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
-                                            VALUE _self) {
+static VALUE MessageBuilderContext_proto3_optional(int argc, VALUE* argv,
+                                                   VALUE _self) {
   VALUE name, type, number;
   VALUE type_class, options = Qnil;
 
@@ -1630,7 +1870,8 @@
  * completeness. Any attempt to add a message type with required fields to a
  * pool will currently result in an error.
  */
-VALUE MessageBuilderContext_required(int argc, VALUE* argv, VALUE _self) {
+static VALUE MessageBuilderContext_required(int argc, VALUE* argv,
+                                            VALUE _self) {
   VALUE name, type, number;
   VALUE type_class, options = Qnil;
 
@@ -1658,7 +1899,8 @@
  * symbol (as accepted by FieldDescriptor#type=) and the type_class must be a
  * string, if present (as accepted by FieldDescriptor#submsg_name=).
  */
-VALUE MessageBuilderContext_repeated(int argc, VALUE* argv, VALUE _self) {
+static VALUE MessageBuilderContext_repeated(int argc, VALUE* argv,
+                                            VALUE _self) {
   VALUE name, type, number, type_class;
 
   if (argc < 3) {
@@ -1687,8 +1929,8 @@
  * type_class must be a string, if present (as accepted by
  * FieldDescriptor#submsg_name=).
  */
-VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
-  DEFINE_SELF(MessageBuilderContext, self, _self);
+static VALUE MessageBuilderContext_map(int argc, VALUE* argv, VALUE _self) {
+  MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
   VALUE name, key_type, value_type, number, type_class;
   VALUE mapentry_desc_name;
   FileBuilderContext* file_builder;
@@ -1717,14 +1959,6 @@
 
   file_builder = ruby_to_FileBuilderContext(self->file_builder);
 
-  // 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,
-             "Cannot add a native map field using proto2 syntax.");
-  }
-
   // Create a new message descriptor for the map entry message, and create a
   // repeated submessage field here with that type.
   msg_name = google_protobuf_DescriptorProto_name(self->msg_proto);
@@ -1768,8 +2002,8 @@
  *
  * This is the recommended, idiomatic way to build oneof definitions.
  */
-VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
-  DEFINE_SELF(MessageBuilderContext, self, _self);
+static VALUE MessageBuilderContext_oneof(VALUE _self, VALUE name) {
+  MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
   size_t oneof_count;
   FileBuilderContext* file_context =
       ruby_to_FileBuilderContext(self->file_builder);
@@ -1795,8 +2029,8 @@
   return Qnil;
 }
 
-void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
-  DEFINE_SELF(MessageBuilderContext, self, _self);
+static void MessageBuilderContext_add_synthetic_oneofs(VALUE _self) {
+  MessageBuilderContext* self = ruby_to_MessageBuilderContext(_self);
   FileBuilderContext* file_context =
       ruby_to_FileBuilderContext(self->file_builder);
   size_t field_count, oneof_count;
@@ -1845,40 +2079,57 @@
   }
 }
 
+static void MessageBuilderContext_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "MessageBuilderContext", rb_cObject);
+  rb_define_alloc_func(klass, MessageBuilderContext_alloc);
+  rb_define_method(klass, "initialize",
+                   MessageBuilderContext_initialize, 2);
+  rb_define_method(klass, "optional", MessageBuilderContext_optional, -1);
+  rb_define_method(klass, "proto3_optional", MessageBuilderContext_proto3_optional, -1);
+  rb_define_method(klass, "required", MessageBuilderContext_required, -1);
+  rb_define_method(klass, "repeated", MessageBuilderContext_repeated, -1);
+  rb_define_method(klass, "map", MessageBuilderContext_map, -1);
+  rb_define_method(klass, "oneof", MessageBuilderContext_oneof, 1);
+  rb_gc_register_address(&cMessageBuilderContext);
+  cMessageBuilderContext = klass;
+}
+
 // -----------------------------------------------------------------------------
 // OneofBuilderContext.
 // -----------------------------------------------------------------------------
 
-DEFINE_CLASS(OneofBuilderContext,
-    "Google::Protobuf::Internal::OneofBuilderContext");
+typedef struct {
+  int oneof_index;
+  VALUE message_builder;
+} OneofBuilderContext;
+
+static VALUE cOneofBuilderContext = Qnil;
 
 void OneofBuilderContext_mark(void* _self) {
   OneofBuilderContext* self = _self;
   rb_gc_mark(self->message_builder);
 }
 
-void OneofBuilderContext_free(void* _self) {
-  xfree(_self);
-}
+static const rb_data_type_t OneofBuilderContext_type = {
+  "Google::Protobuf::Internal::OneofBuilderContext",
+  {OneofBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
 
-VALUE OneofBuilderContext_alloc(VALUE klass) {
-  OneofBuilderContext* self = ALLOC(OneofBuilderContext);
-  VALUE ret = TypedData_Wrap_Struct(
-      klass, &_OneofBuilderContext_type, self);
-  self->oneof_index = 0;
-  self->message_builder = Qnil;
+static OneofBuilderContext* ruby_to_OneofBuilderContext(VALUE val) {
+  OneofBuilderContext* ret;
+  TypedData_Get_Struct(val, OneofBuilderContext, &OneofBuilderContext_type,
+                       ret);
   return ret;
 }
 
-void OneofBuilderContext_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "OneofBuilderContext", rb_cObject);
-  rb_define_alloc_func(klass, OneofBuilderContext_alloc);
-  rb_define_method(klass, "initialize",
-                   OneofBuilderContext_initialize, 2);
-  rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
-  rb_gc_register_address(&cOneofBuilderContext);
-  cOneofBuilderContext = klass;
+static VALUE OneofBuilderContext_alloc(VALUE klass) {
+  OneofBuilderContext* self = ALLOC(OneofBuilderContext);
+  VALUE ret = TypedData_Wrap_Struct(klass, &OneofBuilderContext_type, self);
+  self->oneof_index = 0;
+  self->message_builder = Qnil;
+  return ret;
 }
 
 /*
@@ -1889,10 +2140,9 @@
  * builder context. This class is intended to serve as a DSL context to be used
  * with #instance_eval.
  */
-VALUE OneofBuilderContext_initialize(VALUE _self,
-                                     VALUE oneof_index,
-                                     VALUE message_builder) {
-  DEFINE_SELF(OneofBuilderContext, self, _self);
+static VALUE OneofBuilderContext_initialize(VALUE _self, VALUE oneof_index,
+                                            VALUE message_builder) {
+  OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
   self->oneof_index = NUM2INT(oneof_index);
   self->message_builder = message_builder;
   return Qnil;
@@ -1908,8 +2158,8 @@
  * (as accepted by FieldDescriptor#type=) and the type_class must be a string,
  * if present (as accepted by FieldDescriptor#submsg_name=).
  */
-VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
-  DEFINE_SELF(OneofBuilderContext, self, _self);
+static VALUE OneofBuilderContext_optional(int argc, VALUE* argv, VALUE _self) {
+  OneofBuilderContext* self = ruby_to_OneofBuilderContext(_self);
   VALUE name, type, number;
   VALUE type_class, options = Qnil;
 
@@ -1921,39 +2171,51 @@
   return Qnil;
 }
 
+static void OneofBuilderContext_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "OneofBuilderContext", rb_cObject);
+  rb_define_alloc_func(klass, OneofBuilderContext_alloc);
+  rb_define_method(klass, "initialize",
+                   OneofBuilderContext_initialize, 2);
+  rb_define_method(klass, "optional", OneofBuilderContext_optional, -1);
+  rb_gc_register_address(&cOneofBuilderContext);
+  cOneofBuilderContext = klass;
+}
+
 // -----------------------------------------------------------------------------
 // EnumBuilderContext.
 // -----------------------------------------------------------------------------
 
-DEFINE_CLASS(EnumBuilderContext,
-    "Google::Protobuf::Internal::EnumBuilderContext");
+typedef struct {
+  google_protobuf_EnumDescriptorProto* enum_proto;
+  VALUE file_builder;
+} EnumBuilderContext;
+
+static VALUE cEnumBuilderContext = Qnil;
 
 void EnumBuilderContext_mark(void* _self) {
   EnumBuilderContext* self = _self;
   rb_gc_mark(self->file_builder);
 }
 
-void EnumBuilderContext_free(void* _self) {
-  xfree(_self);
-}
+static const rb_data_type_t EnumBuilderContext_type = {
+  "Google::Protobuf::Internal::EnumBuilderContext",
+  {EnumBuilderContext_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
 
-VALUE EnumBuilderContext_alloc(VALUE klass) {
-  EnumBuilderContext* self = ALLOC(EnumBuilderContext);
-  VALUE ret = TypedData_Wrap_Struct(
-      klass, &_EnumBuilderContext_type, self);
-  self->enum_proto = NULL;
-  self->file_builder = Qnil;
+static EnumBuilderContext* ruby_to_EnumBuilderContext(VALUE val) {
+  EnumBuilderContext* ret;
+  TypedData_Get_Struct(val, EnumBuilderContext, &EnumBuilderContext_type, ret);
   return ret;
 }
 
-void EnumBuilderContext_register(VALUE module) {
-  VALUE klass = rb_define_class_under(
-      module, "EnumBuilderContext", rb_cObject);
-  rb_define_alloc_func(klass, EnumBuilderContext_alloc);
-  rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
-  rb_define_method(klass, "value", EnumBuilderContext_value, 2);
-  rb_gc_register_address(&cEnumBuilderContext);
-  cEnumBuilderContext = klass;
+static VALUE EnumBuilderContext_alloc(VALUE klass) {
+  EnumBuilderContext* self = ALLOC(EnumBuilderContext);
+  VALUE ret = TypedData_Wrap_Struct(klass, &EnumBuilderContext_type, self);
+  self->enum_proto = NULL;
+  self->file_builder = Qnil;
+  return ret;
 }
 
 /*
@@ -1963,9 +2225,9 @@
  * 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.
  */
-VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
-                                    VALUE name) {
-  DEFINE_SELF(EnumBuilderContext, self, _self);
+static VALUE EnumBuilderContext_initialize(VALUE _self, VALUE _file_builder,
+                                           VALUE name) {
+  EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
   FileBuilderContext* file_builder = ruby_to_FileBuilderContext(_file_builder);
   google_protobuf_FileDescriptorProto* file_proto = file_builder->file_proto;
 
@@ -1986,8 +2248,8 @@
  * Adds the given name => number mapping to the enum type. Name must be a Ruby
  * symbol.
  */
-VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
-  DEFINE_SELF(EnumBuilderContext, self, _self);
+static VALUE EnumBuilderContext_value(VALUE _self, VALUE name, VALUE number) {
+  EnumBuilderContext* self = ruby_to_EnumBuilderContext(_self);
   FileBuilderContext* file_builder =
       ruby_to_FileBuilderContext(self->file_builder);
   google_protobuf_EnumValueDescriptorProto* enum_value;
@@ -2003,194 +2265,51 @@
   return Qnil;
 }
 
-
-// -----------------------------------------------------------------------------
-// FileBuilderContext.
-// -----------------------------------------------------------------------------
-
-DEFINE_CLASS(FileBuilderContext,
-             "Google::Protobuf::Internal::FileBuilderContext");
-
-void FileBuilderContext_mark(void* _self) {
-  FileBuilderContext* self = _self;
-  rb_gc_mark(self->descriptor_pool);
-}
-
-void FileBuilderContext_free(void* _self) {
-  FileBuilderContext* self = _self;
-  upb_arena_free(self->arena);
-  xfree(self);
-}
-
-upb_strview FileBuilderContext_strdup2(VALUE _self, const char *str) {
-  DEFINE_SELF(FileBuilderContext, self, _self);
-  upb_strview ret;
-  char *data;
-
-  ret.size = strlen(str);
-  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) {
-  return FileBuilderContext_strdup2(_self, get_str(rb_str));
-}
-
-upb_strview FileBuilderContext_strdup_sym(VALUE _self, VALUE rb_sym) {
-  Check_Type(rb_sym, T_SYMBOL);
-  return FileBuilderContext_strdup(_self, rb_id2str(SYM2ID(rb_sym)));
-}
-
-VALUE FileBuilderContext_alloc(VALUE klass) {
-  FileBuilderContext* self = ALLOC(FileBuilderContext);
-  VALUE ret = TypedData_Wrap_Struct(klass, &_FileBuilderContext_type, self);
-  self->arena = upb_arena_new();
-  self->file_proto = google_protobuf_FileDescriptorProto_new(self->arena);
-  self->descriptor_pool = Qnil;
-  return ret;
-}
-
-void FileBuilderContext_register(VALUE module) {
-  VALUE klass = rb_define_class_under(module, "FileBuilderContext", rb_cObject);
-  rb_define_alloc_func(klass, FileBuilderContext_alloc);
-  rb_define_method(klass, "initialize", FileBuilderContext_initialize, 3);
-  rb_define_method(klass, "add_message", FileBuilderContext_add_message, 1);
-  rb_define_method(klass, "add_enum", FileBuilderContext_add_enum, 1);
-  rb_gc_register_address(&cFileBuilderContext);
-  cFileBuilderContext = klass;
-}
-
-/*
- * call-seq:
- *     FileBuilderContext.new(descriptor_pool) => context
- *
- * 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
- * with #instance_eval.
- */
-VALUE FileBuilderContext_initialize(VALUE _self, VALUE descriptor_pool,
-                                    VALUE name, VALUE options) {
-  DEFINE_SELF(FileBuilderContext, self, _self);
-  self->descriptor_pool = descriptor_pool;
-
-  google_protobuf_FileDescriptorProto_set_name(
-      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) {
-    VALUE syntax;
-
-    Check_Type(options, T_HASH);
-    syntax = rb_hash_lookup2(options, ID2SYM(rb_intern("syntax")), Qnil);
-
-    if (syntax != Qnil) {
-      VALUE syntax_str;
-
-      Check_Type(syntax, T_SYMBOL);
-      syntax_str = rb_id2str(SYM2ID(syntax));
-      google_protobuf_FileDescriptorProto_set_syntax(
-          self->file_proto, FileBuilderContext_strdup(_self, syntax_str));
-    }
-  }
-
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     FileBuilderContext.add_message(name, &block)
- *
- * Creates a new, empty descriptor with the given name, and invokes the block in
- * the context of a MessageBuilderContext on that descriptor. The block can then
- * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
- * methods to define the message fields.
- *
- * This is the recommended, idiomatic way to build message definitions.
- */
-VALUE FileBuilderContext_add_message(VALUE _self, VALUE name) {
-  VALUE args[2] = { _self, name };
-  VALUE ctx = rb_class_new_instance(2, args, cMessageBuilderContext);
-  VALUE block = rb_block_proc();
-  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
-  MessageBuilderContext_add_synthetic_oneofs(ctx);
-  return Qnil;
-}
-
-/*
- * call-seq:
- *     FileBuilderContext.add_enum(name, &block)
- *
- * Creates a new, empty enum descriptor with the given name, and invokes the
- * block in the context of an EnumBuilderContext on that descriptor. The block
- * can then call EnumBuilderContext#add_value to define the enum values.
- *
- * This is the recommended, idiomatic way to build enum definitions.
- */
-VALUE FileBuilderContext_add_enum(VALUE _self, VALUE name) {
-  VALUE args[2] = { _self, name };
-  VALUE ctx = rb_class_new_instance(2, args, cEnumBuilderContext);
-  VALUE block = rb_block_proc();
-  rb_funcall_with_block(ctx, rb_intern("instance_eval"), 0, NULL, block);
-  return Qnil;
-}
-
-void FileBuilderContext_build(VALUE _self) {
-  DEFINE_SELF(FileBuilderContext, self, _self);
-  DescriptorPool* pool = ruby_to_DescriptorPool(self->descriptor_pool);
-  upb_status status;
-
-  rewrite_enum_defaults(pool->symtab, self->file_proto);
-  rewrite_names(_self, self->file_proto);
-
-  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));
-  }
+static void EnumBuilderContext_register(VALUE module) {
+  VALUE klass = rb_define_class_under(
+      module, "EnumBuilderContext", rb_cObject);
+  rb_define_alloc_func(klass, EnumBuilderContext_alloc);
+  rb_define_method(klass, "initialize", EnumBuilderContext_initialize, 2);
+  rb_define_method(klass, "value", EnumBuilderContext_value, 2);
+  rb_gc_register_address(&cEnumBuilderContext);
+  cEnumBuilderContext = klass;
 }
 
 // -----------------------------------------------------------------------------
 // Builder.
 // -----------------------------------------------------------------------------
 
-DEFINE_CLASS(Builder, "Google::Protobuf::Internal::Builder");
+typedef struct {
+  VALUE descriptor_pool;
+  VALUE default_file_builder;
+} Builder;
 
-void Builder_mark(void* _self) {
+static VALUE cBuilder = Qnil;
+
+static void Builder_mark(void* _self) {
   Builder* self = _self;
   rb_gc_mark(self->descriptor_pool);
   rb_gc_mark(self->default_file_builder);
 }
 
-void Builder_free(void* _self) {
-  xfree(_self);
-}
+static const rb_data_type_t Builder_type = {
+  "Google::Protobuf::Internal::Builder",
+  {Builder_mark, RUBY_DEFAULT_FREE, NULL},
+  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
+};
 
-VALUE Builder_alloc(VALUE klass) {
-  Builder* self = ALLOC(Builder);
-  VALUE ret = TypedData_Wrap_Struct(
-      klass, &_Builder_type, self);
-  self->descriptor_pool = Qnil;
-  self->default_file_builder = Qnil;
+static Builder* ruby_to_Builder(VALUE val) {
+  Builder* ret;
+  TypedData_Get_Struct(val, Builder, &Builder_type, ret);
   return ret;
 }
 
-void Builder_register(VALUE module) {
-  VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
-  rb_define_alloc_func(klass, Builder_alloc); 
-  rb_define_method(klass, "initialize", Builder_initialize, 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_enum", Builder_add_enum, 1);
-  rb_gc_register_address(&cBuilder);
-  cBuilder = klass;
+static VALUE Builder_alloc(VALUE klass) {
+  Builder* self = ALLOC(Builder);
+  VALUE ret = TypedData_Wrap_Struct(klass, &Builder_type, self);
+  self->descriptor_pool = Qnil;
+  self->default_file_builder = Qnil;
+  return ret;
 }
 
 /*
@@ -2201,8 +2320,8 @@
  * 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) {
-  DEFINE_SELF(Builder, self, _self);
+static VALUE Builder_initialize(VALUE _self, VALUE pool) {
+  Builder* self = ruby_to_Builder(_self);
   self->descriptor_pool = pool;
   self->default_file_builder = Qnil;  // Created lazily if needed.
   return Qnil;
@@ -2219,8 +2338,8 @@
  *
  * This is the recommended, idiomatic way to build file descriptors.
  */
-VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
-  DEFINE_SELF(Builder, self, _self);
+static VALUE Builder_add_file(int argc, VALUE* argv, VALUE _self) {
+  Builder* self = ruby_to_Builder(_self);
   VALUE name, options;
   VALUE ctx;
   VALUE block;
@@ -2240,7 +2359,7 @@
 }
 
 static VALUE Builder_get_default_file(VALUE _self) {
-  DEFINE_SELF(Builder, self, _self);
+  Builder* self = ruby_to_Builder(_self);
 
   /* Lazily create only if legacy builder-level methods are called. */
   if (self->default_file_builder == Qnil) {
@@ -2264,7 +2383,7 @@
  * files generated by protoc which don't add messages within "add_file" block.
  * Descriptors created this way get assigned to a default empty FileDescriptor.
  */
-VALUE Builder_add_message(VALUE _self, VALUE name) {
+static VALUE Builder_add_message(VALUE _self, VALUE name) {
   VALUE file_builder = Builder_get_default_file(_self);
   rb_funcall_with_block(file_builder, rb_intern("add_message"), 1, &name,
                         rb_block_proc());
@@ -2283,7 +2402,7 @@
  * Enum descriptors created this way get assigned to a default empty
  * FileDescriptor.
  */
-VALUE Builder_add_enum(VALUE _self, VALUE name) {
+static VALUE Builder_add_enum(VALUE _self, VALUE name) {
   VALUE file_builder = Builder_get_default_file(_self);
   rb_funcall_with_block(file_builder, rb_intern("add_enum"), 1, &name,
                         rb_block_proc());
@@ -2292,8 +2411,8 @@
 
 /* This method is hidden from Ruby, and only called directly from
  * DescriptorPool_build(). */
-VALUE Builder_build(VALUE _self) {
-  DEFINE_SELF(Builder, self, _self);
+static VALUE Builder_build(VALUE _self) {
+  Builder* self = ruby_to_Builder(_self);
 
   if (self->default_file_builder != Qnil) {
     FileBuilderContext_build(self->default_file_builder);
@@ -2303,8 +2422,19 @@
   return Qnil;
 }
 
+static void Builder_register(VALUE module) {
+  VALUE klass = rb_define_class_under(module, "Builder", rb_cObject);
+  rb_define_alloc_func(klass, Builder_alloc); 
+  rb_define_method(klass, "initialize", Builder_initialize, 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_enum", Builder_add_enum, 1);
+  rb_gc_register_address(&cBuilder);
+  cBuilder = klass;
+}
+
 static VALUE get_def_obj(VALUE _descriptor_pool, const void* ptr, VALUE klass) {
-  DEFINE_SELF(DescriptorPool, descriptor_pool, _descriptor_pool);
+  DescriptorPool* descriptor_pool = ruby_to_DescriptorPool(_descriptor_pool);
   VALUE key = ULL2NUM((intptr_t)ptr);
   VALUE def;
 
@@ -2319,48 +2449,111 @@
     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);
-
-    // For message defs, we now eagerly get/create descriptors for all
-    // submessages.  We will need these anyway to parse or serialize this
-    // message type.  But more importantly, we must do this now so that
-    // add_handlers_for_message() (which calls get_msgdef_obj()) does *not*
-    // need to create a Ruby object or insert into a Ruby Hash.  We need to
-    // avoid triggering GC, which can switch Ruby threads and re-enter our
-    // C extension from a different thread.  This wreaks havoc on our state
-    // if we were in the middle of building handlers.
-    if (klass == cDescriptor) {
-      const upb_msgdef *m = ptr;
-      upb_msg_field_iter it;
-      for (upb_msg_field_begin(&it, m);
-           !upb_msg_field_done(&it);
-           upb_msg_field_next(&it)) {
-        const upb_fielddef* f = upb_msg_iter_field(&it);
-        if (upb_fielddef_issubmsg(f)) {
-          get_msgdef_obj(_descriptor_pool, upb_fielddef_msgsubdef(f));
-        }
-      }
-    }
   }
 
   return def;
 }
 
-VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
+static VALUE get_msgdef_obj(VALUE descriptor_pool, const upb_msgdef* def) {
   return get_def_obj(descriptor_pool, def, cDescriptor);
 }
 
-VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
+static VALUE get_enumdef_obj(VALUE descriptor_pool, const upb_enumdef* def) {
   return get_def_obj(descriptor_pool, def, cEnumDescriptor);
 }
 
-VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
+static VALUE get_fielddef_obj(VALUE descriptor_pool, const upb_fielddef* def) {
   return get_def_obj(descriptor_pool, def, cFieldDescriptor);
 }
 
-VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
+static VALUE get_filedef_obj(VALUE descriptor_pool, const upb_filedef* def) {
   return get_def_obj(descriptor_pool, def, cFileDescriptor);
 }
 
-VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
+static VALUE get_oneofdef_obj(VALUE descriptor_pool, const upb_oneofdef* def) {
   return get_def_obj(descriptor_pool, def, cOneofDescriptor);
 }
+
+// -----------------------------------------------------------------------------
+// Shared functions
+// -----------------------------------------------------------------------------
+
+// Functions exposed to other modules in defs.h.
+
+VALUE Descriptor_DefToClass(const upb_msgdef *m) {
+  const upb_symtab *symtab = upb_filedef_symtab(upb_msgdef_file(m));
+  VALUE pool = ObjectCache_Get(symtab);
+  PBRUBY_ASSERT(pool != Qnil);
+  VALUE desc_rb = get_msgdef_obj(pool, m);
+  const Descriptor* desc = ruby_to_Descriptor(desc_rb);
+  return desc->klass;
+}
+
+const upb_msgdef *Descriptor_GetMsgDef(VALUE desc_rb) {
+  const Descriptor* desc = ruby_to_Descriptor(desc_rb);
+  return desc->msgdef;
+}
+
+VALUE TypeInfo_InitArg(int argc, VALUE *argv, int skip_arg) {
+  if (argc > skip_arg) {
+    if (argc > 1 + skip_arg) {
+      rb_raise(rb_eArgError, "Expected a maximum of %d arguments.", skip_arg + 1);
+    }
+    return argv[skip_arg];
+  } else {
+    return Qnil;
+  }
+}
+
+TypeInfo TypeInfo_FromClass(int argc, VALUE* argv, int skip_arg,
+                            VALUE* type_class, VALUE* init_arg) {
+  TypeInfo ret = {ruby_to_fieldtype(argv[skip_arg])};
+
+  if (ret.type == UPB_TYPE_MESSAGE || ret.type == UPB_TYPE_ENUM) {
+    *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 2);
+
+    if (argc < 2 + skip_arg) {
+      rb_raise(rb_eArgError, "Expected at least %d arguments for message/enum.",
+               2 + skip_arg);
+    }
+
+    VALUE klass = argv[1 + skip_arg];
+    VALUE desc = MessageOrEnum_GetDescriptor(klass);
+    *type_class = klass;
+
+    if (desc == Qnil) {
+      rb_raise(rb_eArgError,
+               "Type class has no descriptor. Please pass a "
+               "class or enum as returned by the DescriptorPool.");
+    }
+
+    if (ret.type == UPB_TYPE_MESSAGE) {
+      ret.def.msgdef = ruby_to_Descriptor(desc)->msgdef;
+      Message_CheckClass(klass);
+    } else {
+      PBRUBY_ASSERT(ret.type == UPB_TYPE_ENUM);
+      ret.def.enumdef = ruby_to_EnumDescriptor(desc)->enumdef;
+    }
+  } else {
+    *init_arg = TypeInfo_InitArg(argc, argv, skip_arg + 1);
+  }
+
+  return ret;
+}
+
+void Defs_register(VALUE module) {
+  DescriptorPool_register(module);
+  Descriptor_register(module);
+  FileDescriptor_register(module);
+  FieldDescriptor_register(module);
+  OneofDescriptor_register(module);
+  EnumDescriptor_register(module);
+  FileBuilderContext_register(module);
+  MessageBuilderContext_register(module);
+  OneofBuilderContext_register(module);
+  EnumBuilderContext_register(module);
+  Builder_register(module);
+
+  rb_gc_register_address(&c_only_cookie);
+  c_only_cookie = rb_class_new_instance(0, NULL, rb_cObject);
+}