java_inline 0.0.4-java

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,15 @@
1
+ java-inline is a plugin for RubyInline that adds JVM language support. See the examples dir for a simple example of using it.
2
+
3
+ java-inline adds support for Java
4
+
5
+ scala-inline adds support for Scala.
6
+
7
+ Running example
8
+ -------------------
9
+
10
+ gem install RubyInline
11
+
12
+ and to run the example
13
+ cd examples
14
+ jruby fastmath.rb
15
+ jruby -J-Dscala.home=<your scala home> scalamath.rb
@@ -0,0 +1,49 @@
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
+ end
40
+
41
+ math = FastMath.new
42
+
43
+ Benchmark.bmbm(30) {|bm|
44
+ 5.times { bm.report("factorial_ruby") { 30000.times { math.factorial_ruby(30) } } }
45
+ 5.times { bm.report("factorial_java") { 30000.times { math.factorial_java(30) } } }
46
+ 5.times { bm.report("fib_ruby(35)") { math.fib_ruby(35) } }
47
+ 5.times { bm.report("fib_java(35)") { math.fib_java(35) } }
48
+ }
49
+
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'inline'
3
+ require 'mirah_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 :Mirah do |builder|
22
+ builder.mirah "
23
+ def factorial_mirah(max:int)
24
+ i = max
25
+ result = 1
26
+ while i >= 2; result *= i-=1; end
27
+ result
28
+ end
29
+ "
30
+
31
+ builder.mirah "
32
+ def fib_mirah(n:int)
33
+ if n < 2
34
+ n
35
+ else
36
+ fib_mirah(n - 2) + fib_mirah(n - 1)
37
+ end
38
+ end
39
+ "
40
+ end
41
+ end
42
+
43
+ math = FastMath.new
44
+
45
+ Benchmark.bmbm(30) {|bm|
46
+ 5.times { bm.report("factorial_ruby") { 30000.times { math.factorial_ruby(30) } } }
47
+ 5.times { bm.report("factorial_mirah") { 30000.times { math.factorial_mirah(30) } } }
48
+ 5.times { bm.report("fib_ruby(35)") { math.fib_ruby(35) } }
49
+ 5.times { bm.report("fib_mirah(35)") { math.fib_mirah(35) } }
50
+ }
51
+
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'inline'
3
+ require '../lib/scala_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 :Scala do |builder|
22
+ builder.package "org.jruby.test"
23
+ builder.scala "
24
+ def factorial_scala(max:Int) = {
25
+ (1 /: 2.to(max)) { (accumulator, next) => accumulator * next }
26
+ }
27
+ "
28
+ builder.scala "
29
+ def fib_scala(n:Int):Int = {
30
+ if (n < 2) n
31
+ else fib_scala(n - 2) + fib_scala(n - 1)
32
+ }
33
+ "
34
+ end
35
+ end
36
+
37
+ math = FastMath.new
38
+
39
+ Benchmark.bmbm(30) {|bm|
40
+ 5.times { bm.report("factorial_ruby") { 30000.times { math.factorial_ruby(30) } } }
41
+ 5.times { bm.report("factorial_scala") { 30000.times { math.factorial_scala(30) } } }
42
+ 5.times { bm.report("fib_ruby(35)") { math.fib_ruby(35) } }
43
+ 5.times { bm.report("fib_scala(35)") { math.fib_scala(35) } }
44
+ }
45
+
@@ -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
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'inline'
3
+ require 'java'
4
+ require 'mirah'
5
+
6
+ # Add the inline cache dir to CLASSPATH
7
+ $CLASSPATH << Inline.directory
8
+
9
+ module Inline
10
+
11
+ # A Mirah builder for RubyInline. Provides the basic methods needed to
12
+ # allow assembling a set of Mirah methods that compile into a class and
13
+ # get bound to the same names in the containing module.
14
+ class Mirah
15
+ def initialize(mod)
16
+ @context = mod
17
+ @src = ""
18
+ @imports = []
19
+ @sigs = []
20
+ end
21
+
22
+ def load_cache
23
+ false
24
+ end
25
+
26
+ # Add an "import" line with the given class, as in
27
+ # builder.import "java.util.ArrayList". The imports will be composed
28
+ # into an appropriate block of code and added to the top of the source.
29
+ def import(cls)
30
+ if cls.respond_to? :java_class
31
+ @imports << cls.java_class.name
32
+ else
33
+ @imports << cls.to_s
34
+ end
35
+ end
36
+
37
+ # Add a Java method to the built Java source. This expects the method to
38
+ # be public and static, so it can be called as a function.
39
+ def duby(src)
40
+ @src << src << "\n"
41
+ signature = src.match(/def ([a-zA-Z0-9_]+)\((.*)\)/)
42
+ raise "Could not parse method signature" unless signature
43
+ @sigs << [nil, signature[1], signature[2]]
44
+ end
45
+
46
+ def build
47
+ @load_name = @name = "Mirah#{@src.hash.abs}"
48
+ filename = "#{@name}.mirah"
49
+
50
+ imports = "import " + @imports.join("\nimport ") if @imports.size > 0
51
+ full_src = "
52
+ #{imports}
53
+ class #{@name}
54
+ #{@src}
55
+ end
56
+ "
57
+
58
+ File.open(filename, "w") {|file| file.write(full_src)}
59
+ Dir.chdir(Inline.directory) do
60
+ ::Mirah.compile(filename)
61
+ end
62
+ end
63
+
64
+ def load
65
+ @context.module_eval "const_set :#{@name}, ::Java::#{@load_name}.new"
66
+ @sigs.each do |sig|
67
+ @context.module_eval "def #{sig[1]}(*args); #{@name}.#{sig[1]}(*args); end"
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,102 @@
1
+ require 'rubygems'
2
+ require 'inline'
3
+ require 'java'
4
+ include_class Java::java.lang.System
5
+
6
+ # Add the inline cache dir to CLASSPATH
7
+ $CLASSPATH << Inline.directory
8
+ $CLASSPATH << "#{System.getProperty('scala.home')}/lib/scala-compiler.jar"
9
+ $CLASSPATH << "#{System.getProperty('scala.home')}/lib/scala-library.jar"
10
+
11
+ module Inline
12
+
13
+ # A Java builder for RubyInline. Provides the basic methods needed to
14
+ # allow assembling a set of Java methods that compile into a class and
15
+ # get bound to the same names in the containing module.
16
+ class Scala
17
+ JFile = java.io.File
18
+ include_class Java::scala.tools.nsc.Main
19
+
20
+ def initialize(mod)
21
+ @context = mod
22
+ @src = ""
23
+ @imports = []
24
+ @sigs = []
25
+ end
26
+
27
+ def load_cache
28
+ false
29
+ end
30
+
31
+ # Set the package to use for the Java class being generated, as in
32
+ # builder.package "org.foo.bar"
33
+ def package(pkg)
34
+ @pkg = pkg
35
+ end
36
+
37
+ # Add an "import" line with the given class, as in
38
+ # builder.import "java.util.ArrayList". The imports will be composed
39
+ # into an appropriate block of code and added to the top of the source.
40
+ def import(cls)
41
+ if cls.respond_to? :java_class
42
+ @imports << cls.java_class.name
43
+ else
44
+ @imports << cls.to_s
45
+ end
46
+ end
47
+
48
+ # Add a Scala method to the built Scala source. This expects the method to
49
+ # be public and static, so it can be called as a function.
50
+ def scala(src)
51
+ @src << src << "\n"
52
+ signature = src.match(/def\W+([a-zA-Z0-9_]+)\((.*)\)/)
53
+ raise "Could not parse method signature" unless signature
54
+ @sigs << [signature[1], signature[2]]
55
+ end
56
+
57
+ def build
58
+ if @pkg
59
+ directory = "#{Inline.directory}/#{@pkg.gsub('.', '/')}"
60
+ unless File.directory? directory then
61
+ $stderr.puts "NOTE: creating #{directory} for RubyInline" if $DEBUG
62
+ FileUtils.mkdir_p directory
63
+ end
64
+
65
+ @name = "Scala#{@src.hash.abs}"
66
+ @load_name = "#{@pkg}.#{@name}"
67
+ filename = "#{directory}/#{@name}.scala"
68
+
69
+ imports = "import " + @imports.join("\nimport ") if @imports.size > 0
70
+ full_src = "
71
+ package #{@pkg}
72
+ #{imports}
73
+ object #{@name} {
74
+ #{@src}
75
+ }
76
+ "
77
+ else
78
+ @load_name = @name = "Java#{@src.hash.abs}"
79
+ filename = "#{Inline.directory}/#{@name}.scala"
80
+
81
+ imports = "import " + @imports.join("\nimport ") if @imports.size > 0
82
+ full_src = "
83
+ #{imports}
84
+ object #{@name} {
85
+ #{@src}
86
+ }
87
+ "
88
+ end
89
+
90
+ File.open(filename, "w") {|file| file.write(full_src)}
91
+ cmd_args = [filename, "-classpath", "#{System.getProperty('scala.home')}/lib/scala-library.jar", "-d", "#{Inline.directory}"]
92
+ Main.process(cmd_args.to_java(:string))
93
+ end
94
+
95
+ def load
96
+ @context.module_eval "const_set :#{@name}, ::Java::#{@load_name}"
97
+ @sigs.each do |sig|
98
+ @context.module_eval "def #{sig[0]}(*args); #{@name}.#{sig[0]}(*args); end"
99
+ end
100
+ end
101
+ end
102
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: java_inline
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.4
6
+ platform: java
7
+ authors:
8
+ - Charles Oliver Nutter
9
+ - Nilanjan Raychaudhuri
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-11-23 00:00:00 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: RubyInline
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: A set of plugins for RubyInline to allow embedding JVM languages into Ruby code running on JRuby
28
+ email:
29
+ - headius@headius.com
30
+ - nraychaudhuri@gmail.com
31
+ executables: []
32
+
33
+ extensions: []
34
+
35
+ extra_rdoc_files: []
36
+
37
+ files:
38
+ - README
39
+ - lib/java_inline.rb
40
+ - lib/mirah_inline.rb
41
+ - lib/scala_inline.rb
42
+ - examples/javamath.rb
43
+ - examples/mirahmath.rb
44
+ - examples/scalamath.rb
45
+ homepage: http://github.com/jruby/java_inline
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project: java_inline
68
+ rubygems_version: 1.8.9
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: JVM language support for RubyInline
72
+ test_files: []
73
+