Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill out JRuby support #7923

Merged
merged 7 commits into from Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Makefile.am
Expand Up @@ -1115,13 +1115,15 @@ ruby_EXTRA_DIST= \
ruby/src/main/java/com/google/protobuf/jruby/RubyBuilder.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptor.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyDescriptorPool.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyEnum.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyEnumBuilderContext.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyEnumDescriptor.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyEnum.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyFieldDescriptor.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyFileBuilderContext.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyFileDescriptor.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyMessageBuilderContext.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyMessageBuilderContext.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyOneofBuilderContext.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyOneofDescriptor.java \
ruby/src/main/java/com/google/protobuf/jruby/RubyProtobuf.java \
Expand Down
3 changes: 3 additions & 0 deletions conformance/Makefile.am
Expand Up @@ -347,6 +347,9 @@ test_csharp: protoc_middleman conformance-test-runner conformance-csharp
test_ruby: protoc_middleman conformance-test-runner $(other_language_protoc_outputs)
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

test_jruby: protoc_middleman conformance-test-runner $(other_language_protoc_outputs)
RUBYLIB=../ruby/lib:. ./conformance-test-runner --enforce_recommended --failure_list failure_list_jruby.txt --text_format_failure_list text_format_failure_list_jruby.txt ./conformance_ruby.rb

test_php: protoc_middleman conformance-test-runner conformance-php $(other_language_protoc_outputs)
./conformance-test-runner --enforce_recommended --failure_list failure_list_php.txt --text_format_failure_list text_format_failure_list_php.txt ./conformance-php

Expand Down
810 changes: 810 additions & 0 deletions conformance/failure_list_jruby.txt

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions conformance/text_format_failure_list_jruby.txt
@@ -0,0 +1,8 @@
Recommended.Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.GroupUnknownFields_Print.TextFormatOutput
Recommended.Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.MessageUnknownFields_Print.TextFormatOutput
Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Print.TextFormatOutput
Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Print.TextFormatOutput
12 changes: 7 additions & 5 deletions ruby/pom.xml
Expand Up @@ -43,6 +43,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<finalName>${jar.finalName}</finalName>
<outputDirectory>${ruby.sources}</outputDirectory>
Expand All @@ -64,9 +65,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand All @@ -80,13 +82,13 @@
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby-complete</artifactId>
<version>1.7.13</version>
<version>9.2.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.0.0</version>
<artifactId>protobuf-java-util</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>
</project>
122 changes: 51 additions & 71 deletions ruby/src/main/java/com/google/protobuf/jruby/RubyBuilder.java
Expand Up @@ -41,8 +41,8 @@
@JRubyClass(name = "Builder")
public class RubyBuilder extends RubyObject {
public static void createRubyBuilder(Ruby runtime) {
RubyModule protobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cBuilder = protobuf.defineClassUnder("Builder", runtime.getObject(), new ObjectAllocator() {
RubyModule internal = runtime.getClassFromPath("Google::Protobuf::Internal");
RubyClass cBuilder = internal.defineClassUnder("Builder", runtime.getObject(), new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyBuilder(runtime, klazz);
Expand All @@ -53,10 +53,7 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {

public RubyBuilder(Ruby runtime, RubyClass metaClass) {
super(runtime, metaClass);
this.cDescriptor = (RubyClass) runtime.getClassFromPath("Google::Protobuf::Descriptor");
this.cEnumDescriptor = (RubyClass) runtime.getClassFromPath("Google::Protobuf::EnumDescriptor");
this.cMessageBuilderContext = (RubyClass) runtime.getClassFromPath("Google::Protobuf::MessageBuilderContext");
this.cEnumBuilderContext = (RubyClass) runtime.getClassFromPath("Google::Protobuf::EnumBuilderContext");
this.cFileBuilderContext = (RubyClass) runtime.getClassFromPath("Google::Protobuf::Internal::FileBuilderContext");
}

/*
Expand All @@ -68,100 +65,83 @@ public RubyBuilder(Ruby runtime, RubyClass metaClass) {
* (co)recursive type references.
*/
@JRubyMethod
public IRubyObject initialize(ThreadContext context) {
Ruby runtime = context.runtime;
this.pendingList = runtime.newArray();
public IRubyObject initialize(ThreadContext context, IRubyObject descriptorPool) {
this.descriptorPool = (RubyDescriptorPool) descriptorPool;
return this;
}

/*
* call-seq:
* Builder.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.
* Old and deprecated way to create a new descriptor.
* See FileBuilderContext.add_message for the recommended way.
*
* This is the recommended, idiomatic way to build message definitions.
* Exists for backwards compatibility to allow building descriptor pool for
* files generated by protoc which don't add messages within "add_file" block.
* Descriptors created this way get assigned to a default empty FileDescriptor.
*/
@JRubyMethod(name = "add_message")
public IRubyObject addMessage(ThreadContext context, IRubyObject name, Block block) {
RubyDescriptor msgdef = (RubyDescriptor) cDescriptor.newInstance(context, Block.NULL_BLOCK);
IRubyObject ctx = cMessageBuilderContext.newInstance(context, msgdef, this, Block.NULL_BLOCK);
msgdef.setName(context, name);
if (block.isGiven()) {
if (block.arity() == Arity.ONE_ARGUMENT) {
block.yield(context, ctx);
} else {
Binding binding = block.getBinding();
binding.setSelf(ctx);
block.yieldSpecific(context);
}
}
this.pendingList.add(msgdef);
return context.runtime.getNil();
ensureDefaultFileBuilder(context);
defaultFileBuilder.addMessage(context, name, block);
return context.nil;
}

/*
* call-seq:
* Builder.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.
* Old and deprecated way to create a new enum descriptor.
* See FileBuilderContext.add_enum for the recommended way.
*
* This is the recommended, idiomatic way to build enum definitions.
* Exists for backwards compatibility to allow building descriptor pool for
* files generated by protoc which don't add enums within "add_file" block.
* Enum descriptors created this way get assigned to a default empty
* FileDescriptor.
*/
@JRubyMethod(name = "add_enum")
public IRubyObject addEnum(ThreadContext context, IRubyObject name, Block block) {
RubyEnumDescriptor enumDef = (RubyEnumDescriptor) cEnumDescriptor.newInstance(context, Block.NULL_BLOCK);
IRubyObject ctx = cEnumBuilderContext.newInstance(context, enumDef, Block.NULL_BLOCK);
enumDef.setName(context, name);

if (block.isGiven()) {
if (block.arity() == Arity.ONE_ARGUMENT) {
block.yield(context, ctx);
} else {
Binding binding = block.getBinding();
binding.setSelf(ctx);
block.yieldSpecific(context);
}
}

this.pendingList.add(enumDef);
return context.runtime.getNil();
ensureDefaultFileBuilder(context);
defaultFileBuilder.addEnum(context, name, block);
return context.nil;
}

/*
* call-seq:
* Builder.finalize_to_pool(pool)
* Builder.add_file(name, options = nil, &block)
*
* Adds all accumulated message and enum descriptors created in this builder
* context to the given pool. The operation occurs atomically, and all
* descriptors can refer to each other (including in cycles). This is the only
* way to build (co)recursive message definitions.
* Creates a new, file descriptor with the given name and options and invokes
* the block in the context of a FileBuilderContext on that descriptor. The
* block can then call FileBuilderContext#add_message or
* FileBuilderContext#add_enum to define new messages or enums, respectively.
*
* This method is usually called automatically by DescriptorPool#build after it
* invokes the given user block in the context of the builder. The user should
* not normally need to call this manually because a Builder is not normally
* created manually.
* This is the recommended, idiomatic way to build file descriptors.
*/
@JRubyMethod(name = "finalize_to_pool")
public IRubyObject finalizeToPool(ThreadContext context, IRubyObject rbPool) {
RubyDescriptorPool pool = (RubyDescriptorPool) rbPool;
for (int i = 0; i < this.pendingList.size(); i++) {
IRubyObject defRb = this.pendingList.entry(i);
if (defRb instanceof RubyDescriptor) {
pool.addToSymtab(context, (RubyDescriptor) defRb);
} else {
pool.addToSymtab(context, (RubyEnumDescriptor) defRb);
}
@JRubyMethod(name = "add_file")
public IRubyObject addFile(ThreadContext context, IRubyObject name, IRubyObject options, Block block) {
RubyFileBuilderContext ctx = (RubyFileBuilderContext) cFileBuilderContext.newInstance(context, descriptorPool, name, options, Block.NULL_BLOCK);
ctx.instance_eval(context, block);
ctx.build(context);
return context.nil;
}

/*
* Used to trigger the build when using the deprecated syntax
*/
protected void build(ThreadContext context) {
if (defaultFileBuilder != null) {
defaultFileBuilder.build(context);
}
}

private void ensureDefaultFileBuilder(ThreadContext context) {
if (defaultFileBuilder == null) {
this.defaultFileBuilder = (RubyFileBuilderContext) cFileBuilderContext.newInstance(context, descriptorPool, context.runtime.newString("ruby_default_file.proto"), Block.NULL_BLOCK);
}
this.pendingList = context.runtime.newArray();
return context.runtime.getNil();
}

protected RubyArray pendingList;
private RubyClass cDescriptor, cEnumDescriptor, cMessageBuilderContext, cEnumBuilderContext;
private RubyClass cFileBuilderContext;
private RubyDescriptorPool descriptorPool;
private RubyFileBuilderContext defaultFileBuilder;
}