scala-inline 0.0.1-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.
Files changed (4) hide show
  1. data/README +16 -0
  2. data/examples/fastmath.rb +51 -0
  3. data/lib/scala_inline.rb +102 -0
  4. metadata +66 -0
data/README ADDED
@@ -0,0 +1,16 @@
1
+ Forked java-inline to add support for scala. See the examples dir for a simple example of using it.
2
+
3
+ Running example
4
+ -------------------
5
+
6
+ gem install RubyInline
7
+
8
+ and to run the example
9
+ cd examples
10
+ jruby -J-Dscala.home=<your scala home> fastmath.rb
11
+
12
+
13
+ TODO
14
+ -----
15
+ Create a gem install
16
+
@@ -0,0 +1,51 @@
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
+
36
+ def bench
37
+ require 'benchmark'
38
+ Benchmark.bm(30) do |bm|
39
+ end
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_scala") { 30000.times { math.factorial_scala(30) } } }
48
+ 5.times { bm.report("fib_ruby(35)") { math.fib_ruby(30) } }
49
+ 5.times { bm.report("fib_scala(35)") { math.fib_scala(30) } }
50
+ }
51
+
@@ -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,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scala-inline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: java
6
+ authors:
7
+ - Nilanjan Raychaudhuri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-20 00:00:00 -05: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: RubyInline plugin to inline scala code into Ruby code running on JRuby
26
+ email: nraychaudhuri@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README
35
+ - lib/scala_inline.rb
36
+ - examples/fastmath.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/nraychaudhuri/scala-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: scala-inline
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Scala language support for RubyInline
65
+ test_files: []
66
+