Class: Inline::Mirah

Inherits:
Object
  • Object
show all
Defined in:
lib/mirah_inline.rb

Overview

A Mirah builder for RubyInline. Provides the basic methods needed to allow assembling a set of Mirah methods that compile into a class and get bound to the same names in the containing module.

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Mirah

Returns a new instance of Mirah.



15
16
17
18
19
20
# File 'lib/mirah_inline.rb', line 15

def initialize(mod)
  @context = mod
  @src = ""
  @imports = []
  @sigs = []
end

Instance Method Details

#buildObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mirah_inline.rb', line 46

def build
  @load_name = @name = "Mirah#{@src.hash.abs}"
  filename = "#{@name}.mirah"

  imports = "import " + @imports.join("\nimport ") if @imports.size > 0
  full_src = "
    #{imports}
    class #{@name}
    #{@src}
    end
  "

  File.open(filename, "w") {|file| file.write(full_src)}
  Dir.chdir(Inline.directory) do
    ::Mirah.compile(filename)
  end
end

#duby(src) ⇒ Object

Add a Java method to the built Java source. This expects the method to be public and static, so it can be called as a function.



39
40
41
42
43
44
# File 'lib/mirah_inline.rb', line 39

def duby(src)
  @src << src << "\n"
  signature = src.match(/def ([a-zA-Z0-9_]+)\((.*)\)/)
  raise "Could not parse method signature" unless signature
  @sigs << [nil, signature[1], signature[2]]
end

#import(cls) ⇒ Object

Add an “import” line with the given class, as in builder.import “java.util.ArrayList”. The imports will be composed into an appropriate block of code and added to the top of the source.



29
30
31
32
33
34
35
# File 'lib/mirah_inline.rb', line 29

def import(cls)
  if cls.respond_to? :java_class
    @imports << cls.java_class.name
  else
    @imports << cls.to_s
  end
end

#loadObject



64
65
66
67
68
69
# File 'lib/mirah_inline.rb', line 64

def load
  @context.module_eval "const_set :#{@name}, ::Java::#{@load_name}.new"
  @sigs.each do |sig|
    @context.module_eval "def #{sig[1]}(*args); #{@name}.#{sig[1]}(*args); end"
  end
end

#load_cacheObject



22
23
24
# File 'lib/mirah_inline.rb', line 22

def load_cache
  false
end