java-inline 0.0.2-java → 0.0.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/examples/dubymath.rb +51 -0
  2. data/lib/duby_inline.rb +71 -0
  3. metadata +30 -28
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'inline'
3
+ require 'duby_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 :Duby do |builder|
22
+ builder.duby "
23
+ def factorial_duby(max:int)
24
+ i = max
25
+ result = 1
26
+ while i >= 2; result *= i-=1; end
27
+ result
28
+ end
29
+ "
30
+
31
+ builder.duby "
32
+ def fib_duby(n:int)
33
+ if n < 2
34
+ n
35
+ else
36
+ fib_duby(n - 2) + fib_duby(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_duby") { 30000.times { math.factorial_duby(30) } } }
48
+ 5.times { bm.report("fib_ruby(35)") { math.fib_ruby(30) } }
49
+ 5.times { bm.report("fib_duby(35)") { math.fib_duby(30) } }
50
+ }
51
+
@@ -0,0 +1,71 @@
1
+ require 'rubygems'
2
+ require 'inline'
3
+ require 'java'
4
+ require 'duby'
5
+
6
+ # Add the inline cache dir to CLASSPATH
7
+ $CLASSPATH << Inline.directory
8
+
9
+ module Inline
10
+
11
+ # A Duby builder for RubyInline. Provides the basic methods needed to
12
+ # allow assembling a set of Duby methods that compile into a class and
13
+ # get bound to the same names in the containing module.
14
+ class Duby
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 = "Duby#{@src.hash.abs}"
48
+ filename = "#{@name}.duby"
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
+ ::Duby.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
metadata CHANGED
@@ -1,32 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: java-inline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: java
6
6
  authors:
7
- - Charles Oliver Nutter
8
- - Nilanjan Raychaudhuri
7
+ - Charles Oliver Nutter
8
+ - Nilanjan Raychaudhuri
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-11-23 00:00:00 -08:00
13
+ date: 2009-11-23 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
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:
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:
26
26
  description: A set of plugins for RubyInline to allow embedding JVM languages into Ruby code running on JRuby
27
27
  email:
28
- - headius@headius.com
29
- - nraychaudhuri@gmail.com
28
+ - headius@headius.com
29
+ - nraychaudhuri@gmail.com
30
30
  executables: []
31
31
 
32
32
  extensions: []
@@ -34,11 +34,13 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
 
36
36
  files:
37
- - README
38
- - lib/java_inline.rb
39
- - lib/scala_inline.rb
40
- - examples/javamath.rb
41
- - examples/scalamath.rb
37
+ - README
38
+ - lib/duby_inline.rb
39
+ - lib/java_inline.rb
40
+ - lib/scala_inline.rb
41
+ - examples/dubymath.rb
42
+ - examples/javamath.rb
43
+ - examples/scalamath.rb
42
44
  has_rdoc: true
43
45
  homepage: http://kenai.com/projects/java-inline
44
46
  licenses: []
@@ -47,18 +49,18 @@ post_install_message:
47
49
  rdoc_options: []
48
50
 
49
51
  require_paths:
50
- - lib
52
+ - lib
51
53
  required_ruby_version: !ruby/object:Gem::Requirement
52
54
  requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
56
58
  version:
57
59
  required_rubygems_version: !ruby/object:Gem::Requirement
58
60
  requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: "0"
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
62
64
  version:
63
65
  requirements: []
64
66