33 package com.google.protobuf.jruby;
38 import org.jruby.anno.JRubyClass;
39 import org.jruby.anno.JRubyMethod;
40 import org.jruby.runtime.*;
41 import org.jruby.runtime.builtin.IRubyObject;
43 import java.util.HashMap;
46 @JRubyClass(
name =
"DescriptorPool")
49 RubyModule protobuf = runtime.getClassFromPath(
"Google::Protobuf");
50 RubyClass
cDescriptorPool = protobuf.defineClassUnder(
"DescriptorPool", runtime.getObject(),
new ObjectAllocator() {
52 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
67 this.
symtab =
new HashMap<IRubyObject, IRubyObject>();
68 this.
cBuilder = (RubyClass) context.runtime.getClassFromPath(
"Google::Protobuf::Builder");
69 this.builder = DescriptorProtos.FileDescriptorProto.newBuilder();
74 public IRubyObject
build(ThreadContext context, Block block) {
76 if (block.arity() ==
Arity.ONE_ARGUMENT) {
77 block.yield(context, ctx);
79 Binding binding = block.getBinding();
81 block.yieldSpecific(context);
84 buildFileDescriptor(context);
85 return context.runtime.getNil();
89 public IRubyObject
lookup(ThreadContext context, IRubyObject
name) {
92 return context.runtime.getNil();
106 @JRubyMethod(meta =
true,
name =
"generated_pool")
107 public static IRubyObject generatedPool(ThreadContext context, IRubyObject recv) {
108 return descriptorPool;
113 this.builder.addMessageType(def.
getBuilder());
122 Ruby runtime = context.runtime;
124 this.builder.setSyntax(
"proto3");
130 if (enumDescriptor.findValueByNumber(0) ==
null) {
131 throw runtime.newTypeError(
"Enum definition " + enumName
132 +
" does not contain a value for '0'");
135 .setDescriptor(enumDescriptor);
141 if (fieldDescriptor.isRequired()) {
142 throw runtime.newTypeError(
"Required fields are unsupported in proto3");
160 throw runtime.newRuntimeError(e.getMessage());
167 private Map<IRubyObject, IRubyObject>
symtab;
168 private DescriptorProtos.FileDescriptorProto.Builder
builder;