RubyBuilder.java
Go to the documentation of this file.
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 {
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 
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
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")
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")
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")
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);
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;
167 }
cBuilder
VALUE cBuilder
com.google.protobuf.jruby.RubyBuilder.createRubyBuilder
static void createRubyBuilder(Ruby runtime)
Definition: RubyBuilder.java:43
ctx
Definition: benchmark-async.c:30
com.google.protobuf.jruby.RubyBuilder
Definition: RubyBuilder.java:42
com.google.protobuf.jruby.RubyBuilder.RubyBuilder
RubyBuilder(Ruby runtime, RubyClass metaClass)
Definition: RubyBuilder.java:54
gen_gtest_pred_impl.Arity
def Arity(n)
Definition: bloaty/third_party/googletest/googletest/scripts/gen_gtest_pred_impl.py:163
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
setup.name
name
Definition: setup.py:542
block
Block * block
Definition: protobuf/src/google/protobuf/descriptor.cc:1041
cDescriptor
VALUE cDescriptor
Definition: protobuf/ruby/ext/google/protobuf_c/defs.c:230
com.google.protobuf.jruby.RubyBuilder.initialize
IRubyObject initialize(ThreadContext context)
Definition: RubyBuilder.java:71
com.google.protobuf.jruby.RubyBuilder.pendingList
RubyArray pendingList
Definition: RubyBuilder.java:165
cMessageBuilderContext
VALUE cMessageBuilderContext
com.google.protobuf.jruby.RubyEnumDescriptor
Definition: bloaty/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyEnumDescriptor.java:50
com.google.protobuf.jruby.RubyBuilder.cDescriptor
RubyClass cDescriptor
Definition: RubyBuilder.java:166
cEnumDescriptor
VALUE cEnumDescriptor
msgdef
const upb_msgdef * msgdef
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:808
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
com.google.protobuf.jruby.RubyDescriptor
Definition: bloaty/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java:50
com.google.protobuf.jruby.RubyEnumDescriptor.setName
IRubyObject setName(ThreadContext context, IRubyObject name)
Definition: bloaty/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyEnumDescriptor.java:100
com.google.protobuf.jruby.RubyDescriptorPool
Definition: bloaty/third_party/protobuf/ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptorPool.java:47
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
cEnumBuilderContext
VALUE cEnumBuilderContext


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:08