• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Protocol Buffers - Google's data interchange format
3  * Copyright 2014 Google Inc.  All rights reserved.
4  * https://developers.google.com/protocol-buffers/
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  *     * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.google.protobuf.jruby;
34 
35 import org.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;
40 
41 @JRubyClass(name = "Builder")
42 public class RubyBuilder extends RubyObject {
createRubyBuilder(Ruby runtime)43     public static void createRubyBuilder(Ruby runtime) {
44         RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
45         RubyClass cBuilder = protobuf.defineClassUnder("Builder", runtime.getObject(), new ObjectAllocator() {
46             @Override
47             public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
48                 return new RubyBuilder(runtime, klazz);
49             }
50         });
51         cBuilder.defineAnnotatedMethods(RubyBuilder.class);
52     }
53 
RubyBuilder(Ruby runtime, RubyClass metaClass)54     public RubyBuilder(Ruby runtime, RubyClass metaClass) {
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");
60     }
61 
62     /*
63      * call-seq:
64      *     Builder.new => builder
65      *
66      * Creates a new Builder. A Builder can accumulate a set of new message and enum
67      * descriptors and atomically register them into a pool in a way that allows for
68      * (co)recursive type references.
69      */
70     @JRubyMethod
initialize(ThreadContext context)71     public IRubyObject initialize(ThreadContext context) {
72         Ruby runtime = context.runtime;
73         this.pendingList = runtime.newArray();
74         return this;
75     }
76 
77     /*
78      * call-seq:
79      *     Builder.add_message(name, &block)
80      *
81      * Creates a new, empty descriptor with the given name, and invokes the block in
82      * the context of a MessageBuilderContext on that descriptor. The block can then
83      * call, e.g., MessageBuilderContext#optional and MessageBuilderContext#repeated
84      * methods to define the message fields.
85      *
86      * This is the recommended, idiomatic way to build message definitions.
87      */
88     @JRubyMethod(name = "add_message")
addMessage(ThreadContext context, IRubyObject name, Block block)89     public IRubyObject addMessage(ThreadContext context, IRubyObject name, Block block) {
90         RubyDescriptor msgdef = (RubyDescriptor) cDescriptor.newInstance(context, Block.NULL_BLOCK);
91         IRubyObject ctx = cMessageBuilderContext.newInstance(context, msgdef, this, Block.NULL_BLOCK);
92         msgdef.setName(context, name);
93         if (block.isGiven()) {
94             if (block.arity() == Arity.ONE_ARGUMENT) {
95                 block.yield(context, ctx);
96             } else {
97                 Binding binding = block.getBinding();
98                 binding.setSelf(ctx);
99                 block.yieldSpecific(context);
100             }
101         }
102         this.pendingList.add(msgdef);
103         return context.runtime.getNil();
104     }
105 
106     /*
107      * call-seq:
108      *     Builder.add_enum(name, &block)
109      *
110      * Creates a new, empty enum descriptor with the given name, and invokes the block in
111      * the context of an EnumBuilderContext on that descriptor. The block can then
112      * call EnumBuilderContext#add_value to define the enum values.
113      *
114      * This is the recommended, idiomatic way to build enum definitions.
115      */
116     @JRubyMethod(name = "add_enum")
addEnum(ThreadContext context, IRubyObject name, Block block)117     public IRubyObject addEnum(ThreadContext context, IRubyObject name, Block block) {
118         RubyEnumDescriptor enumDef = (RubyEnumDescriptor) cEnumDescriptor.newInstance(context, Block.NULL_BLOCK);
119         IRubyObject ctx = cEnumBuilderContext.newInstance(context, enumDef, Block.NULL_BLOCK);
120         enumDef.setName(context, name);
121 
122         if (block.isGiven()) {
123             if (block.arity() == Arity.ONE_ARGUMENT) {
124                 block.yield(context, ctx);
125             } else {
126                 Binding binding = block.getBinding();
127                 binding.setSelf(ctx);
128                 block.yieldSpecific(context);
129             }
130         }
131 
132         this.pendingList.add(enumDef);
133         return context.runtime.getNil();
134     }
135 
136     /*
137      * call-seq:
138      *     Builder.finalize_to_pool(pool)
139      *
140      * Adds all accumulated message and enum descriptors created in this builder
141      * context to the given pool. The operation occurs atomically, and all
142      * descriptors can refer to each other (including in cycles). This is the only
143      * way to build (co)recursive message definitions.
144      *
145      * This method is usually called automatically by DescriptorPool#build after it
146      * invokes the given user block in the context of the builder. The user should
147      * not normally need to call this manually because a Builder is not normally
148      * created manually.
149      */
150     @JRubyMethod(name = "finalize_to_pool")
finalizeToPool(ThreadContext context, IRubyObject rbPool)151     public IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool) {
152         RubyDescriptorPool pool = (RubyDescriptorPool) rbPool;
153         for (int i = 0; i < this.pendingList.size(); i++) {
154             IRubyObject defRb = this.pendingList.entry(i);
155             if (defRb instanceof RubyDescriptor) {
156                 pool.addToSymtab(context, (RubyDescriptor) defRb);
157             } else {
158                 pool.addToSymtab(context, (RubyEnumDescriptor) defRb);
159             }
160         }
161         this.pendingList = context.runtime.newArray();
162         return context.runtime.getNil();
163     }
164 
165     protected RubyArray pendingList;
166     private RubyClass cDescriptor, cEnumDescriptor, cMessageBuilderContext, cEnumBuilderContext;
167 }
168