33 package com.google.protobuf.jruby;
36 import org.jruby.anno.JRubyClass;
37 import org.jruby.anno.JRubyMethod;
38 import org.jruby.runtime.*;
39 import org.jruby.runtime.builtin.IRubyObject;
41 @JRubyClass(
name =
"Builder")
44 RubyModule protobuf = runtime.getClassFromPath(
"Google::Protobuf");
45 RubyClass
cBuilder = protobuf.defineClassUnder(
"Builder", runtime.getObject(),
new ObjectAllocator() {
47 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
55 super(runtime, metaClass);
56 this.
cDescriptor = (RubyClass) runtime.getClassFromPath(
"Google::Protobuf::Descriptor");
57 this.
cEnumDescriptor = (RubyClass) runtime.getClassFromPath(
"Google::Protobuf::EnumDescriptor");
58 this.
cMessageBuilderContext = (RubyClass) runtime.getClassFromPath(
"Google::Protobuf::MessageBuilderContext");
59 this.
cEnumBuilderContext = (RubyClass) runtime.getClassFromPath(
"Google::Protobuf::EnumBuilderContext");
72 Ruby runtime = context.runtime;
73 this.pendingList = runtime.newArray();
88 @JRubyMethod(
name =
"add_message")
89 public IRubyObject addMessage(ThreadContext context, IRubyObject
name, Block block) {
93 if (block.isGiven()) {
94 if (block.arity() ==
Arity.ONE_ARGUMENT) {
95 block.yield(context, ctx);
97 Binding binding = block.getBinding();
99 block.yieldSpecific(context);
102 this.pendingList.add(
msgdef);
103 return context.runtime.getNil();
116 @JRubyMethod(
name =
"add_enum")
117 public IRubyObject addEnum(ThreadContext context, IRubyObject
name, Block block) {
122 if (block.isGiven()) {
123 if (block.arity() ==
Arity.ONE_ARGUMENT) {
124 block.yield(context, ctx);
126 Binding binding = block.getBinding();
127 binding.setSelf(ctx);
128 block.yieldSpecific(context);
132 this.pendingList.add(enumDef);
133 return context.runtime.getNil();
150 @JRubyMethod(
name =
"finalize_to_pool")
151 public IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool) {
153 for (
int i = 0;
i < this.pendingList.size();
i++) {
154 IRubyObject defRb = this.pendingList.entry(
i);
161 this.pendingList = context.runtime.newArray();
162 return context.runtime.getNil();