java-inline 0.0.1-java → 0.0.2-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.
data/README CHANGED
@@ -1 +1,15 @@
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.
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
@@ -36,13 +36,6 @@ class FastMath
36
36
  }
37
37
  "
38
38
  end
39
-
40
- def bench
41
- require 'benchmark'
42
-
43
- Benchmark.bm(30) do |bm|
44
- end
45
- end
46
39
  end
47
40
 
48
41
  math = FastMath.new
@@ -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(30) } }
43
+ 5.times { bm.report("fib_scala(35)") { math.fib_scala(30) } }
44
+ }
45
+
@@ -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 CHANGED
@@ -1,29 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: java-inline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: java
6
6
  authors:
7
- - Charles Oliver Nutter
7
+ - Charles Oliver Nutter
8
+ - Nilanjan Raychaudhuri
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2009-11-18 00:00:00 -08:00
13
+ date: 2009-11-23 00:00:00 -08:00
13
14
  default_executable:
14
15
  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:
16
+ - !ruby/object:Gem::Dependency
17
+ name: RubyInline
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
25
26
  description: A set of plugins for RubyInline to allow embedding JVM languages into Ruby code running on JRuby
26
- email: headius@headius.com
27
+ email:
28
+ - headius@headius.com
29
+ - nraychaudhuri@gmail.com
27
30
  executables: []
28
31
 
29
32
  extensions: []
@@ -31,9 +34,11 @@ extensions: []
31
34
  extra_rdoc_files: []
32
35
 
33
36
  files:
34
- - README
35
- - lib/java_inline.rb
36
- - examples/fastmath.rb
37
+ - README
38
+ - lib/java_inline.rb
39
+ - lib/scala_inline.rb
40
+ - examples/javamath.rb
41
+ - examples/scalamath.rb
37
42
  has_rdoc: true
38
43
  homepage: http://kenai.com/projects/java-inline
39
44
  licenses: []
@@ -42,18 +47,18 @@ post_install_message:
42
47
  rdoc_options: []
43
48
 
44
49
  require_paths:
45
- - lib
50
+ - lib
46
51
  required_ruby_version: !ruby/object:Gem::Requirement
47
52
  requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- version: "0"
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
51
56
  version:
52
57
  required_rubygems_version: !ruby/object:Gem::Requirement
53
58
  requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
57
62
  version:
58
63
  requirements: []
59
64