protobuf_java_helpers 0.1.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +25 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +14 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/ruby_protobuf/Int32ProtobufField.java +60 -0
- data/ext/ruby_protobuf/Int64ProtobufField.java +60 -0
- data/ext/ruby_protobuf/Sint32ProtobufField.java +60 -0
- data/ext/ruby_protobuf/Sint64ProtobufField.java +60 -0
- data/ext/ruby_protobuf/Uint32ProtobufField.java +60 -0
- data/ext/ruby_protobuf/Uint64ProtobufField.java +60 -0
- data/ext/ruby_protobuf/VarintProtobufField.java +60 -0
- data/ext/ruby_protobuf/Varinter.java +120 -0
- data/ext/ruby_protobuf/VarinterService.java +41 -0
- data/lib/jars/protobuf_java_helpers.jar +0 -0
- data/lib/protobuf_java_helpers.rb +10 -0
- data/lib/protobuf_java_helpers/version.rb +3 -0
- data/protobuf_java_helpers.gemspec +40 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de411bfffc972a22f24c695a0ba52f6c344dfdd4
|
4
|
+
data.tar.gz: a32e631dbd10a87e8d401daa85ad99d124c3bfcb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3c2f52ba0a206436059dceb2c5b63640496bfb1ae7cf8b426641930cba6776deef406b9c9cf627587ce5295a3d30654cf671778c3dbf4261a9f462dccface8f
|
7
|
+
data.tar.gz: f66bc71159bab7b9d3b9646bcb7ecfb9275456868af541fcaa0f39b8dec66eeab3f64fe9923de040b852228826fe28356f828a055096d248f96565dcd668ae89
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
protobuf_java_helpers (0.1.0-java)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.11.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rake-compiler (1.0.5)
|
12
|
+
rake
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
java
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
bundler (~> 1.16)
|
19
|
+
minitest (~> 5.0)
|
20
|
+
protobuf_java_helpers!
|
21
|
+
rake (~> 10.0)
|
22
|
+
rake-compiler
|
23
|
+
|
24
|
+
BUNDLED WITH
|
25
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Brandon Dewitt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# ProtobufJavaHelpers
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/protobuf_java_helpers`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'protobuf_java_helpers'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install protobuf_java_helpers
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/protobuf_java_helpers.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
require "rake/javaextensiontask"
|
4
|
+
|
5
|
+
::Rake::JavaExtensionTask.new("protobuf_java_helpers") do |ext|
|
6
|
+
jruby_home = ENV["MY_RUBY_HOME"] # this is available of rvm
|
7
|
+
jars = ["#{jruby_home}/lib/jruby.jar"] + FileList["lib/*.jar"]
|
8
|
+
ext.classpath = jars.map { |x| File.expand_path x }.join ":"
|
9
|
+
ext.ext_dir = "ext/"
|
10
|
+
ext.lib_dir = "lib/jars"
|
11
|
+
ext.name = "protobuf_java_helpers"
|
12
|
+
ext.source_version = "1.8"
|
13
|
+
ext.target_version = "1.8"
|
14
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "protobuf_java_helpers"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class Int32ProtobufField {
|
18
|
+
private static final long MIN = 0L;
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
20
|
+
private static String TYPE = "Int32";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class Int64ProtobufField {
|
18
|
+
private static final long MIN = ((long)java.lang.Math.pow(-2, 63));
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 63) - 1);
|
20
|
+
private static String TYPE = "Int32";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class Sint32ProtobufField {
|
18
|
+
private static final long MIN = ((long)java.lang.Math.pow(-2, 31));
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
20
|
+
private static String TYPE = "Sint32";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class Sint64ProtobufField {
|
18
|
+
private static final long MIN = ((long)java.lang.Math.pow(-2, 63));
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 63) - 1);
|
20
|
+
private static String TYPE = "Sint32";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class Uint32ProtobufField {
|
18
|
+
private static final long MIN = 0L;
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
20
|
+
private static String TYPE = "Uint32";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class Uint64ProtobufField {
|
18
|
+
private static final long MIN = 0L;
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 64) - 1);
|
20
|
+
private static String TYPE = "Uint32";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,60 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.lang.Math;
|
4
|
+
import org.jruby.Ruby;
|
5
|
+
import org.jruby.RubyBignum;
|
6
|
+
import org.jruby.RubyComplex;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyFloat;
|
9
|
+
import org.jruby.RubyInteger;
|
10
|
+
import org.jruby.RubyNumeric;
|
11
|
+
import org.jruby.RubyString;
|
12
|
+
import org.jruby.util.TypeConverter;
|
13
|
+
import org.jruby.anno.JRubyMethod;
|
14
|
+
import org.jruby.runtime.ThreadContext;
|
15
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
16
|
+
|
17
|
+
public class VarintProtobufField {
|
18
|
+
private static final long MIN = 0L;
|
19
|
+
private static final long MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
20
|
+
private static String TYPE = "Varint";
|
21
|
+
|
22
|
+
private static boolean internal_acceptable(IRubyObject recv) {
|
23
|
+
if (recv instanceof RubyInteger || recv instanceof RubyFixnum) {
|
24
|
+
long value = ((RubyFixnum) recv).getLongValue();
|
25
|
+
return value >= MIN && value <= MAX;
|
26
|
+
}
|
27
|
+
|
28
|
+
return false;
|
29
|
+
}
|
30
|
+
|
31
|
+
@JRubyMethod(name = "acceptable?")
|
32
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
33
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
34
|
+
|
35
|
+
if (internal_acceptable(recv)) {
|
36
|
+
return runtime.getTrue();
|
37
|
+
}
|
38
|
+
|
39
|
+
return runtime.getFalse();
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod(name = "coerce!")
|
43
|
+
public static IRubyObject coerce_bang( ThreadContext context, IRubyObject self, IRubyObject recv ) {
|
44
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
45
|
+
|
46
|
+
if (!internal_acceptable(recv)) {
|
47
|
+
throw runtime.newTypeError("can't convert " + recv.getMetaClass() + " into " + TYPE);
|
48
|
+
}
|
49
|
+
|
50
|
+
if (recv instanceof RubyInteger) {
|
51
|
+
return ((RubyInteger)recv).to_i();
|
52
|
+
}
|
53
|
+
|
54
|
+
if (recv instanceof RubyFixnum) {
|
55
|
+
return ((RubyFixnum)recv).to_i();
|
56
|
+
}
|
57
|
+
|
58
|
+
return org.jruby.util.TypeConverter.convertToInteger(context, recv, 10);
|
59
|
+
}
|
60
|
+
}
|
@@ -0,0 +1,120 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import java.io.IOException;
|
4
|
+
import java.io.InputStream;
|
5
|
+
import java.io.ByteArrayOutputStream;
|
6
|
+
import java.lang.Math;
|
7
|
+
import java.util.concurrent.ConcurrentMap;
|
8
|
+
import java.util.concurrent.ConcurrentHashMap;
|
9
|
+
import java.util.function.Function;
|
10
|
+
import org.jcodings.specific.ASCIIEncoding;
|
11
|
+
import org.jruby.Ruby;
|
12
|
+
import org.jruby.RubyBignum;
|
13
|
+
import org.jruby.RubyComplex;
|
14
|
+
import org.jruby.RubyFixnum;
|
15
|
+
import org.jruby.RubyFloat;
|
16
|
+
import org.jruby.RubyInteger;
|
17
|
+
import org.jruby.RubyNumeric;
|
18
|
+
import org.jruby.RubyString;
|
19
|
+
import org.jruby.RubyIO;
|
20
|
+
import org.jruby.ext.stringio.StringIO;
|
21
|
+
import org.jruby.util.ByteList;
|
22
|
+
import org.jruby.anno.JRubyMethod;
|
23
|
+
import org.jruby.runtime.ThreadContext;
|
24
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
25
|
+
|
26
|
+
public class Varinter {
|
27
|
+
private static final long INT32_MAX = ((long)java.lang.Math.pow(2, 31) - 1);
|
28
|
+
|
29
|
+
private static byte[] internal_to_varint(long value) {
|
30
|
+
java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream();
|
31
|
+
|
32
|
+
while (value > 0x7f) {
|
33
|
+
bytes.write((byte) (0x80 | (value & 0x7f)));
|
34
|
+
value >>= 7;
|
35
|
+
}
|
36
|
+
|
37
|
+
bytes.write((byte) value);
|
38
|
+
return bytes.toByteArray();
|
39
|
+
}
|
40
|
+
|
41
|
+
@JRubyMethod(name = "acceptable?")
|
42
|
+
public static IRubyObject acceptable_p( ThreadContext context, IRubyObject self ) {
|
43
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
44
|
+
|
45
|
+
if (self instanceof RubyInteger || self instanceof RubyFixnum) {
|
46
|
+
long value = ((RubyFixnum) self).getLongValue();
|
47
|
+
|
48
|
+
if (value >= 0 && value <= INT32_MAX) {
|
49
|
+
return runtime.getTrue();
|
50
|
+
} else {
|
51
|
+
return runtime.getFalse();
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
return runtime.getFalse();
|
56
|
+
}
|
57
|
+
|
58
|
+
@JRubyMethod
|
59
|
+
public static IRubyObject to_varint( ThreadContext context, IRubyObject self ) {
|
60
|
+
if (!(self instanceof RubyInteger || self instanceof RubyFixnum)) {
|
61
|
+
return context.nil;
|
62
|
+
}
|
63
|
+
|
64
|
+
long value = ((RubyFixnum) self).getLongValue();
|
65
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
66
|
+
RubyString bit_string = runtime.newString(new ByteList(internal_to_varint(value), true));
|
67
|
+
bit_string.force_encoding(context, runtime.getEncodingService().getEncoding(org.jcodings.specific.ASCIIEncoding.INSTANCE));
|
68
|
+
return bit_string;
|
69
|
+
}
|
70
|
+
|
71
|
+
@JRubyMethod
|
72
|
+
public static IRubyObject to_varint_64( ThreadContext context, IRubyObject self ) {
|
73
|
+
if (!(self instanceof RubyInteger || self instanceof RubyFixnum)) {
|
74
|
+
return context.nil;
|
75
|
+
}
|
76
|
+
|
77
|
+
long value = ((RubyFixnum) self).getLongValue();
|
78
|
+
org.jruby.Ruby runtime = context.getRuntime();
|
79
|
+
RubyString bit_string = runtime.newString(new ByteList(internal_to_varint(value & 0xffffffffffffffffL), true));
|
80
|
+
bit_string.force_encoding(context, runtime.getEncodingService().getEncoding(org.jcodings.specific.ASCIIEncoding.INSTANCE));
|
81
|
+
return bit_string;
|
82
|
+
}
|
83
|
+
|
84
|
+
@JRubyMethod
|
85
|
+
public static IRubyObject read_varint(ThreadContext context, IRubyObject self) throws IOException {
|
86
|
+
long value = 0L;
|
87
|
+
int index = 0;
|
88
|
+
long current_byte;
|
89
|
+
|
90
|
+
if (self instanceof StringIO) {
|
91
|
+
StringIO current_self = ((StringIO)self);
|
92
|
+
RubyFixnum fixnum;
|
93
|
+
|
94
|
+
do {
|
95
|
+
fixnum = ((RubyFixnum)(current_self.getbyte(context)));
|
96
|
+
current_byte = fixnum.getLongValue();
|
97
|
+
if (index == 0 && current_byte < 128) { return context.getRuntime().newFixnum(current_byte); }
|
98
|
+
value = (value | ((current_byte & 0x7f) << (7 * index)));
|
99
|
+
index++;
|
100
|
+
} while ((current_byte & 0x80) != 0);
|
101
|
+
|
102
|
+
return context.getRuntime().newFixnum(value);
|
103
|
+
}
|
104
|
+
|
105
|
+
if (self instanceof RubyIO) {
|
106
|
+
RubyIO current_self = ((RubyIO)self);
|
107
|
+
|
108
|
+
do {
|
109
|
+
current_byte = current_self.getByte(context);
|
110
|
+
if (index == 0 && current_byte < 128) { return context.getRuntime().newFixnum(current_byte); }
|
111
|
+
value = (value | ((current_byte & 0x7f) << (7 * index)));
|
112
|
+
index++;
|
113
|
+
} while ((current_byte & 0x80) != 0);
|
114
|
+
|
115
|
+
return context.getRuntime().newFixnum(value);
|
116
|
+
}
|
117
|
+
|
118
|
+
return context.nil;
|
119
|
+
}
|
120
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
package com.abrandoned.protobuf_java_helpers;
|
2
|
+
|
3
|
+
import org.jruby.Ruby;
|
4
|
+
import org.jruby.RubyModule;
|
5
|
+
import org.jruby.runtime.load.BasicLibraryService;
|
6
|
+
import com.abrandoned.protobuf_java_helpers.*;
|
7
|
+
|
8
|
+
import java.io.IOException;
|
9
|
+
|
10
|
+
public class VarinterService implements BasicLibraryService {
|
11
|
+
@Override
|
12
|
+
public boolean basicLoad(final Ruby runtime) throws IOException {
|
13
|
+
RubyModule protobuf_java_helpers = runtime.getModule("ProtobufJavaHelpers");
|
14
|
+
|
15
|
+
RubyModule varinter = protobuf_java_helpers.defineModuleUnder(Varinter.class.getSimpleName());
|
16
|
+
varinter.defineAnnotatedMethods(Varinter.class);
|
17
|
+
|
18
|
+
RubyModule uint64_protobuf = protobuf_java_helpers.defineModuleUnder(Uint64ProtobufField.class.getSimpleName());
|
19
|
+
uint64_protobuf.defineAnnotatedMethods(Uint64ProtobufField.class);
|
20
|
+
|
21
|
+
RubyModule uint32_protobuf = protobuf_java_helpers.defineModuleUnder(Uint32ProtobufField.class.getSimpleName());
|
22
|
+
uint32_protobuf.defineAnnotatedMethods(Uint32ProtobufField.class);
|
23
|
+
|
24
|
+
RubyModule sint64_protobuf = protobuf_java_helpers.defineModuleUnder(Sint64ProtobufField.class.getSimpleName());
|
25
|
+
sint64_protobuf.defineAnnotatedMethods(Sint64ProtobufField.class);
|
26
|
+
|
27
|
+
RubyModule sint32_protobuf = protobuf_java_helpers.defineModuleUnder(Sint32ProtobufField.class.getSimpleName());
|
28
|
+
sint32_protobuf.defineAnnotatedMethods(Sint32ProtobufField.class);
|
29
|
+
|
30
|
+
RubyModule int64_protobuf = protobuf_java_helpers.defineModuleUnder(Int64ProtobufField.class.getSimpleName());
|
31
|
+
int64_protobuf.defineAnnotatedMethods(Int64ProtobufField.class);
|
32
|
+
|
33
|
+
RubyModule int32_protobuf = protobuf_java_helpers.defineModuleUnder(Int32ProtobufField.class.getSimpleName());
|
34
|
+
int32_protobuf.defineAnnotatedMethods(Int32ProtobufField.class);
|
35
|
+
|
36
|
+
RubyModule varint_protobuf_field = protobuf_java_helpers.defineModuleUnder(VarintProtobufField.class.getSimpleName());
|
37
|
+
varint_protobuf_field.defineAnnotatedMethods(VarintProtobufField.class);
|
38
|
+
|
39
|
+
return true;
|
40
|
+
}
|
41
|
+
}
|
Binary file
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "java"
|
2
|
+
require "jruby"
|
3
|
+
require "protobuf_java_helpers/version"
|
4
|
+
|
5
|
+
module ProtobufJavaHelpers
|
6
|
+
# Your code goes here...
|
7
|
+
end
|
8
|
+
|
9
|
+
require "jars/protobuf_java_helpers.jar"
|
10
|
+
com.abrandoned.protobuf_java_helpers.VarinterService.new.basicLoad(::JRuby.runtime)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "protobuf_java_helpers/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "protobuf_java_helpers"
|
8
|
+
spec.version = ProtobufJavaHelpers::VERSION
|
9
|
+
spec.authors = ["Brandon Dewitt"]
|
10
|
+
spec.email = ["brandonsdewitt@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ Java helpers for ruby-protobuf/protobuf }
|
13
|
+
spec.description = %q{ Java helpers for ruby-protobuf/protobuf }
|
14
|
+
spec.homepage = "https://github.com/abrandoned/protobuf_java_helpers"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.platform = "java"
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
22
|
+
else
|
23
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
24
|
+
"public gem pushes."
|
25
|
+
end
|
26
|
+
|
27
|
+
# Specify which files should be added to the gem when it is released.
|
28
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
29
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
30
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
31
|
+
end
|
32
|
+
spec.bindir = "exe"
|
33
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
39
|
+
spec.add_development_dependency "rake-compiler"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: protobuf_java_helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Brandon Dewitt
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '1.16'
|
19
|
+
name: bundler
|
20
|
+
prerelease: false
|
21
|
+
type: :development
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '10.0'
|
33
|
+
name: rake
|
34
|
+
prerelease: false
|
35
|
+
type: :development
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '5.0'
|
47
|
+
name: minitest
|
48
|
+
prerelease: false
|
49
|
+
type: :development
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
name: rake-compiler
|
62
|
+
prerelease: false
|
63
|
+
type: :development
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: " Java helpers for ruby-protobuf/protobuf "
|
70
|
+
email:
|
71
|
+
- brandonsdewitt@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- ext/ruby_protobuf/Int32ProtobufField.java
|
86
|
+
- ext/ruby_protobuf/Int64ProtobufField.java
|
87
|
+
- ext/ruby_protobuf/Sint32ProtobufField.java
|
88
|
+
- ext/ruby_protobuf/Sint64ProtobufField.java
|
89
|
+
- ext/ruby_protobuf/Uint32ProtobufField.java
|
90
|
+
- ext/ruby_protobuf/Uint64ProtobufField.java
|
91
|
+
- ext/ruby_protobuf/VarintProtobufField.java
|
92
|
+
- ext/ruby_protobuf/Varinter.java
|
93
|
+
- ext/ruby_protobuf/VarinterService.java
|
94
|
+
- lib/jars/protobuf_java_helpers.jar
|
95
|
+
- lib/protobuf_java_helpers.rb
|
96
|
+
- lib/protobuf_java_helpers/version.rb
|
97
|
+
- protobuf_java_helpers.gemspec
|
98
|
+
homepage: https://github.com/abrandoned/protobuf_java_helpers
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata:
|
102
|
+
allowed_push_host: https://rubygems.org
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.6.14.1
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Java helpers for ruby-protobuf/protobuf
|
123
|
+
test_files: []
|