java-inline 0.0.1-java
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -0
- data/examples/fastmath.rb +56 -0
- data/lib/java_inline.rb +107 -0
- metadata +66 -0
data/README
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
java-inline is a plugin for RubyInline that adds JVM language support. See the examples dir for a simple example of using it.
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'inline'
|
3
|
+
require 'java_inline'
|
4
|
+
require 'benchmark'
|
5
|
+
|
6
|
+
class FastMath
|
7
|
+
def factorial_ruby(n)
|
8
|
+
f = 1
|
9
|
+
n.downto(2) { |x| f *= x }
|
10
|
+
return f
|
11
|
+
end
|
12
|
+
|
13
|
+
def fib_ruby(n)
|
14
|
+
if n < 2
|
15
|
+
n
|
16
|
+
else
|
17
|
+
fib_ruby(n - 2) + fib_ruby(n - 1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
inline :Java do |builder|
|
22
|
+
builder.package "org.jruby.test"
|
23
|
+
builder.java "
|
24
|
+
public static long factorial_java(int max) {
|
25
|
+
int i=max, result=1;
|
26
|
+
while (i >= 2) { result *= i--; }
|
27
|
+
return result;
|
28
|
+
}
|
29
|
+
"
|
30
|
+
|
31
|
+
builder.java "
|
32
|
+
public static int fib_java(int n) {
|
33
|
+
if (n < 2) return n;
|
34
|
+
|
35
|
+
return fib_java(n - 2) + fib_java(n - 1);
|
36
|
+
}
|
37
|
+
"
|
38
|
+
end
|
39
|
+
|
40
|
+
def bench
|
41
|
+
require 'benchmark'
|
42
|
+
|
43
|
+
Benchmark.bm(30) do |bm|
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
math = FastMath.new
|
49
|
+
|
50
|
+
Benchmark.bmbm(30) {|bm|
|
51
|
+
5.times { bm.report("factorial_ruby") { 30000.times { math.factorial_ruby(30) } } }
|
52
|
+
5.times { bm.report("factorial_java") { 30000.times { math.factorial_java(30) } } }
|
53
|
+
5.times { bm.report("fib_ruby(35)") { math.fib_ruby(30) } }
|
54
|
+
5.times { bm.report("fib_java(35)") { math.fib_java(30) } }
|
55
|
+
}
|
56
|
+
|
data/lib/java_inline.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'inline'
|
3
|
+
require 'java'
|
4
|
+
|
5
|
+
# Add the inline cache dir to CLASSPATH
|
6
|
+
$CLASSPATH << Inline.directory
|
7
|
+
|
8
|
+
module Inline
|
9
|
+
|
10
|
+
# A Java builder for RubyInline. Provides the basic methods needed to
|
11
|
+
# allow assembling a set of Java methods that compile into a class and
|
12
|
+
# get bound to the same names in the containing module.
|
13
|
+
class Java
|
14
|
+
JFile = java.io.File
|
15
|
+
import javax.tools.ToolProvider
|
16
|
+
import javax.tools.SimpleJavaFileObject
|
17
|
+
import javax.tools.StandardLocation
|
18
|
+
|
19
|
+
def initialize(mod)
|
20
|
+
@context = mod
|
21
|
+
@src = ""
|
22
|
+
@imports = []
|
23
|
+
@sigs = []
|
24
|
+
end
|
25
|
+
|
26
|
+
def load_cache
|
27
|
+
false
|
28
|
+
end
|
29
|
+
|
30
|
+
# Set the package to use for the Java class being generated, as in
|
31
|
+
# builder.package "org.foo.bar"
|
32
|
+
def package(pkg)
|
33
|
+
@pkg = pkg
|
34
|
+
end
|
35
|
+
|
36
|
+
# Add an "import" line with the given class, as in
|
37
|
+
# builder.import "java.util.ArrayList". The imports will be composed
|
38
|
+
# into an appropriate block of code and added to the top of the source.
|
39
|
+
def import(cls)
|
40
|
+
if cls.respond_to? :java_class
|
41
|
+
@imports << cls.java_class.name
|
42
|
+
else
|
43
|
+
@imports << cls.to_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Add a Java method to the built Java source. This expects the method to
|
48
|
+
# be public and static, so it can be called as a function.
|
49
|
+
def java(src)
|
50
|
+
@src << src << "\n"
|
51
|
+
signature = src.match(/public static\W+(\w+)\W+([a-zA-Z0-9_]+)\((.*)\)/)
|
52
|
+
raise "Could not parse method signature" unless signature
|
53
|
+
@sigs << [signature[1], signature[2], signature[3]]
|
54
|
+
end
|
55
|
+
|
56
|
+
def build
|
57
|
+
compiler = ToolProvider.system_java_compiler
|
58
|
+
file_mgr = compiler.get_standard_file_manager(nil, nil, nil)
|
59
|
+
file_mgr.set_location(StandardLocation::CLASS_OUTPUT, [JFile.new(Inline.directory)])
|
60
|
+
|
61
|
+
if @pkg
|
62
|
+
directory = "#{Inline.directory}/#{@pkg.gsub('.', '/')}"
|
63
|
+
unless File.directory? directory then
|
64
|
+
$stderr.puts "NOTE: creating #{directory} for RubyInline" if $DEBUG
|
65
|
+
FileUtils.mkdir_p directory
|
66
|
+
end
|
67
|
+
|
68
|
+
@name = "Java#{@src.hash.abs}"
|
69
|
+
@load_name = "#{@pkg}.#{@name}"
|
70
|
+
filename = "#{directory}/#{@name}.java"
|
71
|
+
|
72
|
+
imports = "import " + @imports.join(";\nimport ") + ";" if @imports.size > 0
|
73
|
+
full_src = "
|
74
|
+
package #{@pkg};
|
75
|
+
#{imports}
|
76
|
+
public class #{@name} {
|
77
|
+
#{@src}
|
78
|
+
}
|
79
|
+
"
|
80
|
+
else
|
81
|
+
@load_name = @name = "Java#{@src.hash.abs}"
|
82
|
+
filename = "#{Inline.directory}/#{@name}.java"
|
83
|
+
|
84
|
+
imports = "import " + @imports.join(";\nimport ") + ";" if @imports.size > 0
|
85
|
+
full_src = "
|
86
|
+
#{imports}
|
87
|
+
public class #{@name} {
|
88
|
+
#{@src}
|
89
|
+
}
|
90
|
+
"
|
91
|
+
end
|
92
|
+
|
93
|
+
File.open(filename, "w") {|file| file.write(full_src)}
|
94
|
+
file_objs = file_mgr.get_java_file_objects_from_strings([filename])
|
95
|
+
|
96
|
+
compiler.get_task(nil, file_mgr, nil, nil, nil, file_objs).call
|
97
|
+
file_mgr.close
|
98
|
+
end
|
99
|
+
|
100
|
+
def load
|
101
|
+
@context.module_eval "const_set :#{@name}, ::Java::#{@load_name}"
|
102
|
+
@sigs.each do |sig|
|
103
|
+
@context.module_eval "def #{sig[1]}(*args); #{@name}.#{sig[1]}(*args); end"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: java-inline
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Charles Oliver Nutter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-18 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: RubyInline
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: A set of plugins for RubyInline to allow embedding JVM languages into Ruby code running on JRuby
|
26
|
+
email: headius@headius.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- README
|
35
|
+
- lib/java_inline.rb
|
36
|
+
- examples/fastmath.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://kenai.com/projects/java-inline
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: java-inline
|
61
|
+
rubygems_version: 1.3.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: JVM language support for RubyInline
|
65
|
+
test_files: []
|
66
|
+
|