|
1 | 1 | require 'bitescript'
|
| 2 | +require File.dirname(__FILE__) + '/signature' |
2 | 3 |
|
3 | 4 | RubyObject = org.jruby.RubyObject
|
4 | 5 | RubyBasicObject = org.jruby.RubyBasicObject
|
|
7 | 8 | IRubyObject = org.jruby.runtime.builtin.IRubyObject
|
8 | 9 | ThreadContext = org.jruby.runtime.ThreadContext
|
9 | 10 | LoadService = org.jruby.runtime.load.LoadService
|
| 11 | +JClass = java.lang.Class |
| 12 | +JavaUtil = org.jruby.javasupport.JavaUtil |
| 13 | +JObject = java.lang.Object |
10 | 14 |
|
11 | 15 | JAVA_CLASSNAME = ARGV[0]
|
12 | 16 | RUBY_CLASSNAME = ARGV[1]
|
|
20 | 24 | require RUBY_FILENAME
|
21 | 25 | RUBY_CLASS = eval(RUBY_CLASSNAME)
|
22 | 26 |
|
| 27 | +BiteScript.bytecode_version = BiteScript::JAVA1_5 |
| 28 | + |
| 29 | +def first_local(params, instance = true) |
| 30 | + i = instance ? 1 : 0 |
| 31 | + params.each do |param| |
| 32 | + if param == Java::long || param == Java::double |
| 33 | + i += 2 |
| 34 | + else |
| 35 | + i += 1 |
| 36 | + end |
| 37 | + end |
| 38 | + i |
| 39 | +end |
| 40 | + |
23 | 41 | file = BiteScript::FileBuilder.build("#{JAVA_CLASSNAME}.java.rb") do
|
24 | 42 | public_class JAVA_CLASSNAME, RubyObject do
|
25 | 43 | static_init do
|
|
41 | 59 |
|
42 | 60 | for method_name in RUBY_CLASS.public_instance_methods(false) do
|
43 | 61 | method = RUBY_CLASS.instance_method(method_name)
|
44 |
| - params = (method.arity < 0) ? [IRubyObject[]] : [IRubyObject] * method.arity |
45 |
| - public_method method_name, IRubyObject, *params do |
| 62 | + signature = RUBY_CLASS.signatures[method_name] |
| 63 | + |
| 64 | + if signature |
| 65 | + raise "signatures only supported for exact arities: #{RUBY_CLASS.to_s + '#' + method_name}" if method.arity < 0 |
| 66 | + params = signature.keys[0] |
| 67 | + retval = signature[params] |
| 68 | + use_ji = true |
| 69 | + else |
| 70 | + params = (method.arity < 0) ? [IRubyObject[]] : [IRubyObject] * method.arity |
| 71 | + retval = IRubyObject |
| 72 | + end |
| 73 | + |
| 74 | + public_method method_name, retval, *params do |
46 | 75 | aload 0
|
47 | 76 | dup
|
48 | 77 | invokeinterface IRubyObject, "getRuntime", [Ruby]
|
| 78 | + ruby_index = first_local(params) |
| 79 | + dup; astore ruby_index |
49 | 80 | invokevirtual Ruby, "getCurrentContext", [ThreadContext]
|
50 | 81 | ldc method_name
|
51 | 82 | # TODO: arity-specific call
|
52 |
| - if method.arity < 0 |
53 |
| - # restarg or optarg, just pass array through |
54 |
| - aload 1 |
55 |
| - else |
56 |
| - # all normal args, box them up |
| 83 | + if use_ji |
| 84 | + # We have a signature and need to use java integration logic |
57 | 85 | ldc method.arity
|
58 | 86 | anewarray IRubyObject
|
59 | 87 | i = 1;
|
| 88 | + index = 1 |
60 | 89 | 1.upto(method.arity) do |i|
|
61 | 90 | dup
|
62 | 91 | ldc i - 1
|
63 |
| - aload i |
| 92 | + aload ruby_index |
| 93 | + param_type = params[i - 1] |
| 94 | + if [boolean, byte, short, char, int].include? param_type |
| 95 | + iload index |
| 96 | + invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, int] |
| 97 | + elsif long == param_type |
| 98 | + lload index |
| 99 | + invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, long] |
| 100 | + index += 1 |
| 101 | + elsif float == param_type |
| 102 | + fload index |
| 103 | + invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, float] |
| 104 | + elsif double == param_type |
| 105 | + dload index |
| 106 | + invokestatic JavaUtil, "convertJavaToRuby", [IRubyObject, Ruby, double] |
| 107 | + index += 1 |
| 108 | + else |
| 109 | + aload i |
| 110 | + invokestatic JavaUtil, "convertJavaToUsableRubyObject", [IRubyObject, Ruby, object] |
| 111 | + end |
64 | 112 | aastore
|
| 113 | + index += 1 |
| 114 | + end |
| 115 | + else |
| 116 | + if method.arity < 0 |
| 117 | + # restarg or optarg, just pass array through |
| 118 | + aload 1 |
| 119 | + else |
| 120 | + # all normal args, box them up |
| 121 | + ldc method.arity |
| 122 | + anewarray IRubyObject |
| 123 | + i = 1; |
| 124 | + 1.upto(method.arity) do |i| |
| 125 | + dup |
| 126 | + ldc i - 1 |
| 127 | + aload i |
| 128 | + aastore |
| 129 | + end |
65 | 130 | end
|
66 | 131 | end
|
67 | 132 | invokevirtual RubyBasicObject, "callMethod", [IRubyObject, ThreadContext, string, IRubyObject[]]
|
68 |
| - areturn |
| 133 | + if use_ji |
| 134 | + if boolean == retval |
| 135 | + invokestatic JavaUtil, "convertRubyToJavaBoolean", [boolean, IRubyObject] |
| 136 | + ireturn |
| 137 | + elsif byte == retval |
| 138 | + invokestatic JavaUtil, "convertRubyToJavaByte", [byte, IRubyObject] |
| 139 | + ireturn |
| 140 | + elsif short == retval |
| 141 | + invokestatic JavaUtil, "convertRubyToJavaShort", [short, IRubyObject] |
| 142 | + ireturn |
| 143 | + elsif char == retval |
| 144 | + invokestatic JavaUtil, "convertRubyToJavaChar", [char, IRubyObject] |
| 145 | + ireturn |
| 146 | + elsif int == retval |
| 147 | + invokestatic JavaUtil, "convertRubyToJavaInt", [int, IRubyObject] |
| 148 | + ireturn |
| 149 | + elsif long == retval |
| 150 | + invokestatic JavaUtil, "convertRubyToJavaLong", [long, IRubyObject] |
| 151 | + lreturn |
| 152 | + elsif float == retval |
| 153 | + invokestatic JavaUtil, "convertRubyToJavaFloat", [float, IRubyObject] |
| 154 | + freturn |
| 155 | + elsif double == retval |
| 156 | + invokestatic JavaUtil, "convertRubyToJavaDouble", [double, IRubyObject] |
| 157 | + dreturn |
| 158 | + elsif retval == void |
| 159 | + pop |
| 160 | + returnvoid |
| 161 | + else |
| 162 | + ldc retval |
| 163 | + invokestatic JavaUtil, "convertRubyToJava", [JObject, IRubyObject, JClass] |
| 164 | + areturn |
| 165 | + end |
| 166 | + else |
| 167 | + areturn |
| 168 | + end |
69 | 169 | end
|
70 | 170 | end
|
71 | 171 | end
|
|
0 commit comments