Class: Ruby2Java::JavaCompiler

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

Constant Summary collapse

RubyObject =
org.jruby.RubyObject
RubyBasicObject =
org.jruby.RubyBasicObject
Ruby =
org.jruby.Ruby
RubyClass =
org.jruby.RubyClass
IRubyObject =
org.jruby.runtime.builtin.IRubyObject
ThreadContext =
org.jruby.runtime.ThreadContext
LoadService =
org.jruby.runtime.load.LoadService
JClass =
java.lang.Class
JavaUtil =
org.jruby.javasupport.JavaUtil
JObject =
java.lang.Object

Instance Method Summary collapse

Constructor Details

#initialize(source_name) ⇒ JavaCompiler

Returns a new instance of JavaCompiler.



19
20
21
# File 'lib/compiler.rb', line 19

def initialize(source_name)
  @file_builder = BiteScript::FileBuilder.new(source_name)
end

Instance Method Details

#create_method(cb, method_name, method, signatures = nil, annotations = nil, options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/compiler.rb', line 102

def create_method(cb, method_name, method, signatures = nil, annotations = nil, options = {})
  java_signature = true

  unless signatures
    signatures = Signature.undefined_for_arity(method.arity)
    java_signature = false
  end

  for s in signatures do
    if options[:static]
      mb = cb.public_static_method method_name, s.retval, *(s.params)
    else
      mb = cb.public_method method_name, s.retval, *(s.params)
    end

    mb.start

    if annotations && annotations.size > 0
      # define annotations
      annotations.each do |anno_cls, anno_data|
        mb.annotate(anno_cls, true) do |anno|
          anno_data.each {|k,v| anno.send(k + "=", v)} if anno_data
        end
      end
    end

    # prepare receiver, context, and method name for callMethod later
    options[:static] ? mb.getstatic(mb.this, "__ruby_class__", RubyClass) : mb.aload(0)
    mb.dup
    mb.invokeinterface IRubyObject, "getRuntime", [Ruby]
    ruby_index = first_local(s.params, !options[:static])
    mb.dup; mb.astore ruby_index
    mb.invokevirtual Ruby, "getCurrentContext", [ThreadContext]
    mb.ldc method_name

    # TODO: arity-specific calling
    if java_signature
      java_args(mb, method, s.params, ruby_index, options)
    else
      ruby_args(mb, method, ruby_index, options)
    end

    # invoke the method dynamically
    # TODO: this could have a simple inline cache
    mb.invokevirtual RubyBasicObject, "callMethod", [IRubyObject, ThreadContext, mb.string, IRubyObject[]]

    # return the result
    if java_signature
      java_return(mb, s.retval)
    else
      ruby_return(mb, s.retval)
    end

    mb.stop
  end
end

#first_local(params, instance = true) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/compiler.rb', line 159

def first_local(params, instance = true)
  i = instance ? 1 : 0
  params.each do |param|
    if param == Java::long || param == Java::double
      i += 2
    else
      i += 1
    end
  end
  i
end

#java_args(mb, method, params, ruby_index, options = {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/compiler.rb', line 171

def java_args(mb, method, params, ruby_index, options = {})
  # We have a signature and need to use java integration logic
  mb.ldc params.size
  mb.anewarray IRubyObject
  base = options[:static] ? 0 : 1
  index = base
  0.upto(params.size - 1) do |i|
    mb.dup
    mb.ldc i 
    mb.aload ruby_index
    param_type = params[i - 1]
    if [mb.boolean, mb.byte, mb.short, mb.char, mb.int].include? param_type
      mb.iload index + base
      mb.invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, mb.int]
    elsif mb.long == param_type
      mb.lload index + base
      mb.invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, mb.long]
      index += 1
    elsif mb.float == param_type
      mb.fload index + base
      mb.invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, mb.float]
    elsif mb.double == param_type
      mb.dload index + base
      mb.invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, mb.double]
      index += 1
    else
      mb.aload index + base
      mb.invokestatic JavaUtil, "convertJavaToUsableRubyObject", [IRubyObject, Ruby, mb.object]
    end
    mb.aastore
    index += 1
  end
end

#java_return(mb, retval) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/compiler.rb', line 228

def java_return(mb, retval)
  if mb.boolean == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaBoolean", [mb.boolean, IRubyObject]
    mb.ireturn
  elsif mb.byte == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaByte", [mb.byte, IRubyObject]
    mb.ireturn
  elsif mb.short == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaShort", [mb.short, IRubyObject]
    mb.ireturn
  elsif mb.char == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaChar", [mb.char, IRubyObject]
    mb.ireturn
  elsif mb.int == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaInt", [mb.int, IRubyObject]
    mb.ireturn
  elsif mb.long == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaLong", [mb.long, IRubyObject]
    mb.lreturn
  elsif mb.float == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaFloat", [mb.float, IRubyObject]
    mb.freturn
  elsif mb.double == retval
    mb.invokestatic JavaUtil, "convertRubyToJavaDouble", [mb.double, IRubyObject]
    mb.dreturn
  elsif retval == mb.void || retval == nil
    mb.pop
    mb.returnvoid
  else
    mb.ldc retval
    mb.invokestatic JavaUtil, "convertRubyToJava", [JObject, IRubyObject, JClass]
    mb.areturn
  end
end

#process_class(ruby_class_path, ruby_class, java_name, *require_files) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/compiler.rb', line 32

def process_class(ruby_class_path, ruby_class, java_name, *require_files)
  @file_builder.package = ruby_class.package_name if ruby_class.package_name

  cb = @file_builder.public_class(java_name, RubyObject, *ruby_class.interfaces);

  # field to hold the RubyClass reference
  cb.private_static_field "__ruby_class__", RubyClass

  # If a require file is specified, load it in static initializer
  cb.static_init do
    invokestatic Ruby, "getGlobalRuntime", [Ruby]
    require_files.each do |require_file|
      dup
      ldc File.read(require_file)
      ldc require_file
      invokevirtual Ruby, "executeScript", [IRubyObject, cb.string, cb.string]
      pop
    end
    ldc ruby_class_path
    invokevirtual Ruby, "getClass", [RubyClass, cb.string]
    dup
    putstatic this, "__ruby_class__", RubyClass
    ldc this.name
    invokestatic JClass, "forName", [JClass, string]
    invokevirtual RubyClass, "setClassAllocator", [void, JClass]
    # load the script contents into the class file
    returnvoid
  end

  cb.public_constructor do
    aload 0
    invokestatic Ruby, "getGlobalRuntime", [Ruby]
    getstatic this, "__ruby_class__", RubyClass
    invokespecial RubyObject, "<init>", [cb.void, Ruby, RubyClass]
    returnvoid
  end

  for method_name in ruby_class.public_instance_methods(false) do
    method = ruby_class.instance_method(method_name)
    signatures = if ruby_class.respond_to? :signatures
      ruby_class.signatures[method_name]
    else
      nil
    end
    annotations = if ruby_class.respond_to? :annotations
      ruby_class.annotations[method_name]
    else
      nil
    end

    create_method(cb, method_name, method, signatures, annotations)
  end

  for method_name in ruby_class.public_methods(false) do
    method = ruby_class.method(method_name)
    signatures = if ruby_class.respond_to? :static_signatures
      ruby_class.static_signatures[method_name]
    else
      nil
    end
    annotations = if ruby_class.respond_to? :static_annotations
      ruby_class.static_annotations[method_name]
    else
      nil
    end

    create_method(cb, method_name, method, signatures, annotations, :static => true)
  end
end

#ruby_args(mb, method, ruby_index, options = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/compiler.rb', line 205

def ruby_args(mb, method, ruby_index, options = {})
  if method.arity < 0
    # restarg or optarg, just pass array through
    mb.aload options[:static] ? 0 : 1
  else
    # all normal args, box them up
    mb.ldc method.arity
    mb.anewarray IRubyObject
    i = ruby_index;
    base = options[:static] ? 0 : 1
    0.upto(method.arity - 1) do |i|
      mb.dup
      mb.ldc i
      mb.aload i + base
      mb.aastore
    end
  end
end

#ruby_return(mb, retval) ⇒ Object



224
225
226
# File 'lib/compiler.rb', line 224

def ruby_return(mb, retval)
  mb.areturn
end

#write_filesObject



23
24
25
26
27
28
29
30
# File 'lib/compiler.rb', line 23

def write_files
  @file_builder.generate do |name, builder|
    FileUtils.mkdir_p File.dirname(name)
    File.open(name, 'w') do |f|
      f.write(builder.generate)
    end
  end
end