mirah 0.0.12-java → 0.1.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +372 -0
- data/README.txt +4 -5
- data/Rakefile +178 -55
- data/examples/appengine/Readme +3 -3
- data/examples/appengine/src/org/mirah/MirahApp.mirah +1 -1
- data/examples/appengine/src/org/mirah/list.dhtml +1 -1
- data/examples/bintrees.mirah +1 -1
- data/examples/edb.mirah +1 -1
- data/examples/fib.mirah +1 -1
- data/examples/interfaces.mirah +1 -1
- data/examples/macros/{string-each-char.mirah → string_each_char.mirah} +4 -5
- data/examples/maven/README.txt +1 -1
- data/examples/maven/src/main/mirah/hello_mirah.mirah +1 -1
- data/examples/plugins/appengine/Rakefile +1 -1
- data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/MetaModel.mirah +1 -1
- data/examples/plugins/appengine/src/com/google/appengine/ext/duby/db/Model.duby +1 -1
- data/examples/plugins/appengine/test/com/google/appengine/ext/duby/db/ModelTest.duby +1 -1
- data/examples/rosettacode/100-doors.mirah +6 -6
- data/examples/rosettacode/README.txt +3 -3
- data/examples/rosettacode/boolean-values.mirah +1 -1
- data/examples/rosettacode/comments.mirah +1 -1
- data/examples/rosettacode/count-occurrences-of-a-substring.mirah +1 -1
- data/examples/rosettacode/factorial.mirah +1 -1
- data/examples/rosettacode/fibonacci.mirah +1 -1
- data/examples/rosettacode/fizz-buzz.mirah +2 -2
- data/examples/rosettacode/flatten-a-list.mirah +4 -4
- data/examples/rosettacode/guess-the-number.mirah +2 -2
- data/examples/rosettacode/hamming-numbers.mirah +4 -4
- data/examples/rosettacode/is-string-numeric.mirah +22 -22
- data/examples/rosettacode/palindrome.mirah +2 -2
- data/examples/rosettacode/random-numbers.mirah +1 -1
- data/examples/rosettacode/repeat-a-string.mirah +1 -1
- data/examples/rosettacode/reverse-a-string.mirah +1 -1
- data/examples/rosettacode/rot-13.mirah +5 -5
- data/examples/rosettacode/secure-temporary-file.mirah +2 -2
- data/examples/rosettacode/sleep.mirah +1 -1
- data/examples/rosettacode/string-length.mirah +5 -5
- data/examples/swing.mirah +1 -1
- data/examples/test.edb +1 -1
- data/javalib/mirah-bootstrap.jar +0 -0
- data/javalib/mirah-builtins.jar +0 -0
- data/javalib/mirah-parser.jar +0 -0
- data/javalib/mirah-util.jar +0 -0
- data/lib/duby.rb +1 -1
- data/lib/mirah.rb +50 -28
- data/lib/mirah/ast.rb +15 -605
- data/lib/mirah/ast/scope.rb +98 -69
- data/lib/mirah/commands.rb +1 -1
- data/lib/mirah/commands/base.rb +7 -7
- data/lib/mirah/commands/compile.rb +3 -3
- data/lib/mirah/commands/parse.rb +7 -5
- data/lib/mirah/commands/run.rb +12 -19
- data/lib/mirah/compiler.rb +15 -23
- data/lib/mirah/errors.rb +16 -1
- data/lib/mirah/generator.rb +79 -39
- data/lib/mirah/jvm/compiler.rb +1 -19
- data/lib/mirah/jvm/compiler/base.rb +233 -90
- data/lib/mirah/jvm/compiler/jvm_bytecode.rb +675 -363
- data/lib/mirah/jvm/method_lookup.rb +134 -65
- data/lib/mirah/jvm/typer.rb +10 -5
- data/lib/mirah/jvm/types.rb +10 -2
- data/lib/mirah/jvm/types/array_type.rb +10 -12
- data/lib/mirah/{compiler/type.rb → jvm/types/ast_ext.rb} +12 -8
- data/lib/mirah/jvm/types/basic_types.rb +26 -33
- data/lib/mirah/jvm/types/bitescript_ext.rb +1 -1
- data/lib/mirah/jvm/types/block_type.rb +15 -0
- data/lib/mirah/jvm/types/boolean.rb +8 -4
- data/lib/mirah/jvm/types/dynamic_type.rb +12 -13
- data/lib/mirah/jvm/types/enumerable.rb +7 -7
- data/lib/mirah/jvm/types/extensions.rb +11 -6
- data/lib/mirah/jvm/types/factory.rb +624 -94
- data/lib/mirah/jvm/types/floats.rb +21 -15
- data/lib/mirah/jvm/types/generic_type.rb +72 -0
- data/lib/mirah/jvm/types/implicit_nil_type.rb +29 -0
- data/lib/mirah/jvm/types/integers.rb +26 -71
- data/lib/mirah/jvm/types/interface_definition.rb +3 -3
- data/lib/mirah/jvm/types/intrinsics.rb +203 -168
- data/lib/mirah/jvm/types/literals.rb +6 -6
- data/lib/mirah/jvm/types/meta_type.rb +13 -4
- data/lib/mirah/jvm/types/methods.rb +281 -93
- data/lib/mirah/jvm/types/null_type.rb +17 -5
- data/lib/mirah/jvm/types/number.rb +10 -7
- data/lib/mirah/jvm/types/primitive_type.rb +17 -6
- data/lib/mirah/jvm/types/source_mirror.rb +12 -7
- data/lib/mirah/jvm/types/type.rb +107 -23
- data/lib/mirah/jvm/types/type_definition.rb +25 -10
- data/lib/mirah/jvm/types/unreachable_type.rb +1 -1
- data/lib/mirah/jvm/types/void_type.rb +3 -3
- data/lib/mirah/parser.rb +154 -16
- data/lib/mirah/plugin/edb.rb +1 -1
- data/lib/mirah/transform.rb +1 -2
- data/lib/mirah/transform/ast_ext.rb +24 -43
- data/lib/mirah/transform/transformer.rb +29 -224
- data/lib/mirah/typer.rb +2 -16
- data/lib/mirah/util/argument_processor.rb +25 -10
- data/lib/mirah/util/class_loader.rb +1 -1
- data/lib/mirah/util/compilation_state.rb +16 -17
- data/lib/mirah/util/delegate.rb +2 -2
- data/lib/mirah/util/logging.rb +110 -0
- data/lib/mirah/util/process_errors.rb +69 -11
- data/lib/mirah/version.rb +1 -1
- data/test/core/commands_test.rb +6 -24
- data/test/core/env_test.rb +5 -5
- data/{lib/mirah/jvm/source_generator/typer.rb → test/core/generator_test.rb} +9 -9
- data/test/core/typer_test.rb +196 -158
- data/test/core/util/argument_processor_test.rb +10 -10
- data/test/core/util/class_loader_test.rb +6 -5
- data/test/fixtures/org/foo/LowerCaseInnerClass$inner.class +0 -0
- data/test/fixtures/org/foo/LowerCaseInnerClass.class +0 -0
- data/test/fixtures/org/foo/LowerCaseInnerClass.java +7 -0
- data/test/jvm/annotations_test.rb +5 -5
- data/test/jvm/blocks_test.rb +140 -88
- data/test/jvm/bytecode_test_helper.rb +112 -94
- data/test/jvm/cast_test.rb +162 -0
- data/test/jvm/constructors_test.rb +18 -8
- data/test/jvm/enumerable_test.rb +77 -44
- data/test/jvm/example_test.rb +53 -0
- data/test/jvm/factory_test.rb +7 -1
- data/test/jvm/generics_test.rb +57 -0
- data/test/jvm/hash_test.rb +106 -0
- data/test/jvm/import_test.rb +81 -0
- data/test/jvm/interface_test.rb +73 -0
- data/test/jvm/java_typer_test.rb +92 -66
- data/{lib/mirah/typer/base.rb → test/jvm/jvm_commands_test.rb} +6 -10
- data/test/jvm/jvm_compiler_test.rb +170 -604
- data/test/jvm/list_extensions_test.rb +23 -0
- data/test/jvm/macros_test.rb +197 -32
- data/test/jvm/main_method_test.rb +4 -4
- data/test/jvm/numeric_extensions_test.rb +13 -0
- data/test/jvm/rescue_test.rb +73 -16
- data/test/jvm/varargs_test.rb +65 -0
- data/test/test_helper.rb +1 -2
- metadata +234 -251
- data/examples/SortClosure$__xform_tmp_1.class +0 -0
- data/examples/SortClosure$__xform_tmp_2.class +0 -0
- data/examples/SortClosure.class +0 -0
- data/examples/macros/StringEachChar$Extension1.class +0 -0
- data/lib/mirah/ast/call.rb +0 -345
- data/lib/mirah/ast/class.rb +0 -359
- data/lib/mirah/ast/flow.rb +0 -381
- data/lib/mirah/ast/intrinsics.rb +0 -563
- data/lib/mirah/ast/literal.rb +0 -178
- data/lib/mirah/ast/local.rb +0 -112
- data/lib/mirah/ast/method.rb +0 -408
- data/lib/mirah/ast/structure.rb +0 -387
- data/lib/mirah/ast/type.rb +0 -146
- data/lib/mirah/commands/base.rb~ +0 -57
- data/lib/mirah/compiler/call.rb +0 -45
- data/lib/mirah/compiler/class.rb +0 -81
- data/lib/mirah/compiler/flow.rb +0 -109
- data/lib/mirah/compiler/literal.rb +0 -130
- data/lib/mirah/compiler/local.rb +0 -59
- data/lib/mirah/compiler/method.rb +0 -44
- data/lib/mirah/compiler/structure.rb +0 -65
- data/lib/mirah/jvm/compiler/java_source.rb +0 -787
- data/lib/mirah/jvm/method_lookup.rb~ +0 -247
- data/lib/mirah/jvm/source_generator/builder.rb +0 -468
- data/lib/mirah/jvm/source_generator/loops.rb +0 -131
- data/lib/mirah/jvm/source_generator/precompile.rb +0 -210
- data/lib/mirah/plugin/gwt.rb +0 -189
- data/lib/mirah/plugin/java.rb +0 -70
- data/lib/mirah/transform/error.rb +0 -13
- data/lib/mirah/transform/helper.rb +0 -765
- data/lib/mirah/typer/simple.rb +0 -384
- data/lib/mirah/version.rb~ +0 -18
- data/test/core/ast_test.rb +0 -382
- data/test/core/compilation_test.rb +0 -130
- data/test/core/macros_test.rb +0 -61
- data/test/jvm/javac_test_helper.rb +0 -89
- data/test/jvm/jvm_compiler_test.rb~ +0 -2181
- data/test/plugins/gwt_test.rb +0 -69
@@ -1,131 +0,0 @@
|
|
1
|
-
# Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
|
2
|
-
# All contributing project authors may be found in the NOTICE file.
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
# you may not use this file except in compliance with the License.
|
6
|
-
# You may obtain a copy of the License at
|
7
|
-
#
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
#
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
# See the License for the specific language governing permissions and
|
14
|
-
# limitations under the License.
|
15
|
-
|
16
|
-
module Mirah
|
17
|
-
module JVM
|
18
|
-
module Compiler
|
19
|
-
class JavaSource < Base
|
20
|
-
class SimpleWhileLoop
|
21
|
-
attr_reader :compiler, :loop
|
22
|
-
def initialize(loop, compiler)
|
23
|
-
@loop = loop
|
24
|
-
@compiler = compiler
|
25
|
-
end
|
26
|
-
|
27
|
-
def break
|
28
|
-
compiler.method.puts "break;"
|
29
|
-
end
|
30
|
-
|
31
|
-
def next
|
32
|
-
compiler.method.puts "continue;"
|
33
|
-
end
|
34
|
-
|
35
|
-
def redo
|
36
|
-
raise "#{self.class.name} doesn't support redo"
|
37
|
-
end
|
38
|
-
|
39
|
-
def compile(expression)
|
40
|
-
prepare
|
41
|
-
@loop.init.compile(compiler, false) if @loop.init?
|
42
|
-
@start.call
|
43
|
-
compiler.method.block do
|
44
|
-
@loop.pre.compile(compiler, false) if @loop.pre?
|
45
|
-
compile_body
|
46
|
-
@loop.post.compile(compiler, false) if @loop.post?
|
47
|
-
end
|
48
|
-
if @end_check
|
49
|
-
@end_check.call
|
50
|
-
compiler.method.puts ';'
|
51
|
-
end
|
52
|
-
if expression
|
53
|
-
compiler.method.puts "#{compiler.lvalue}null;"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def compile_body
|
58
|
-
loop.body.compile(compiler, false) if loop.body
|
59
|
-
end
|
60
|
-
|
61
|
-
def prepare
|
62
|
-
predicate = loop.condition.predicate.precompile(compiler)
|
63
|
-
negative = loop.negative ? '!' : ''
|
64
|
-
check = lambda do
|
65
|
-
compiler.method.print "while (#{negative}"
|
66
|
-
predicate.compile(compiler, true)
|
67
|
-
compiler.method.print ')'
|
68
|
-
end
|
69
|
-
if loop.check_first
|
70
|
-
@start = check
|
71
|
-
else
|
72
|
-
@start = lambda {compiler.method.print 'do'}
|
73
|
-
@end_check = check
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
module Redoable
|
79
|
-
def compile_with_redo(block)
|
80
|
-
@redo = compiler.method.tmp(JVMTypes::Boolean)
|
81
|
-
compiler.method.puts "#{@inner}:"
|
82
|
-
compiler.method.block "do" do
|
83
|
-
compiler.method.puts "#{@redo} = false;"
|
84
|
-
block.compile(compiler, false) if block
|
85
|
-
end
|
86
|
-
compiler.method.puts "while (#{@redo});"
|
87
|
-
end
|
88
|
-
|
89
|
-
def break
|
90
|
-
compiler.method.puts "break #{@outer};"
|
91
|
-
end
|
92
|
-
|
93
|
-
def next
|
94
|
-
compiler.method.puts "break #{@inner};"
|
95
|
-
end
|
96
|
-
|
97
|
-
def redo
|
98
|
-
compiler.method.puts "#{@redo} = true;"
|
99
|
-
compiler.method.puts "continue #{@inner};"
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
class ComplexWhileLoop < SimpleWhileLoop
|
104
|
-
include Redoable
|
105
|
-
def prepare
|
106
|
-
super
|
107
|
-
@outer = compiler.method.label
|
108
|
-
@inner = compiler.method.label
|
109
|
-
@complex_predicate = !loop.condition.predicate.expr?(compiler)
|
110
|
-
super_start = @start
|
111
|
-
@start = lambda do
|
112
|
-
compiler.method.puts "#{@outer}:"
|
113
|
-
super_start.call
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def compile_body
|
118
|
-
if @loop.redo?
|
119
|
-
compile_with_redo(@loop.body)
|
120
|
-
else
|
121
|
-
compiler.method.puts "#{@inner}:"
|
122
|
-
compiler.method.block do
|
123
|
-
loop.body.compile(compiler, false) if loop.body
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
@@ -1,210 +0,0 @@
|
|
1
|
-
# Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
|
2
|
-
# All contributing project authors may be found in the NOTICE file.
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
# you may not use this file except in compliance with the License.
|
6
|
-
# You may obtain a copy of the License at
|
7
|
-
#
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
#
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
# See the License for the specific language governing permissions and
|
14
|
-
# limitations under the License.
|
15
|
-
|
16
|
-
require 'mirah/ast'
|
17
|
-
|
18
|
-
module Mirah::AST
|
19
|
-
class TempValue
|
20
|
-
def initialize(node, compiler=nil, value=nil)
|
21
|
-
if compiler.nil?
|
22
|
-
@tempname = node
|
23
|
-
else
|
24
|
-
@tempname = compiler.temp(node, value)
|
25
|
-
@tempvalue = value || node
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def compile(compiler, expression)
|
30
|
-
if expression
|
31
|
-
compiler.method.print @tempname
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def reload(compiler)
|
36
|
-
compiler.assign(@tempname, @tempvalue)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
class Node
|
41
|
-
def expr?(compiler)
|
42
|
-
true
|
43
|
-
end
|
44
|
-
|
45
|
-
def precompile(compiler)
|
46
|
-
if expr?(compiler)
|
47
|
-
self
|
48
|
-
else
|
49
|
-
temp(compiler)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def temp(compiler, value=nil)
|
54
|
-
TempValue.new(self, compiler, value)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class Body
|
59
|
-
def expr?(compiler)
|
60
|
-
false
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
class If
|
65
|
-
def expr?(compiler)
|
66
|
-
return false unless condition.predicate.expr?(compiler)
|
67
|
-
return false unless body.nil? || body.expr?(compiler)
|
68
|
-
return false unless self.else.nil? || self.else.expr?(compiler)
|
69
|
-
true
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class Loop
|
74
|
-
def expr?(compiler)
|
75
|
-
false
|
76
|
-
end
|
77
|
-
|
78
|
-
def precompile(compiler)
|
79
|
-
compile(compiler, false)
|
80
|
-
temp(compiler, 'null')
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
class Call
|
85
|
-
def method(compiler=nil)
|
86
|
-
@method ||= begin
|
87
|
-
arg_types = parameters.map {|p| p.inferred_type}
|
88
|
-
target.inferred_type.get_method(name, arg_types)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def expr?(compiler)
|
93
|
-
target.expr?(compiler) &&
|
94
|
-
parameters.all? {|p| p.expr?(compiler)} &&
|
95
|
-
cast || (
|
96
|
-
!method.return_type.kind_of?(Mirah::AST::InlineCode) &&
|
97
|
-
!method.return_type.void?)
|
98
|
-
end
|
99
|
-
|
100
|
-
def precompile_target(compiler)
|
101
|
-
if method.return_type.void? && target.expr?(compiler)
|
102
|
-
TempValue.new(target, compiler)
|
103
|
-
else
|
104
|
-
target.precompile(compiler)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
class FunctionalCall
|
110
|
-
def method(compiler)
|
111
|
-
@method ||= begin
|
112
|
-
arg_types = parameters.map {|p| p.inferred_type}
|
113
|
-
@self_type.get_method(name, arg_types)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def expr?(compiler)
|
118
|
-
parameters.all? {|p| p.expr?(compiler)} &&
|
119
|
-
(cast? || !method(compiler).return_type.void?)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
# TODO merge with FunctionalCall logic (almost identical)
|
124
|
-
class Super
|
125
|
-
def method(compiler)
|
126
|
-
@method ||= begin
|
127
|
-
arg_types = parameters.map {|p| p.inferred_type}
|
128
|
-
compiler.self_type.superclass.get_method(name, arg_types)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def expr?(compiler)
|
133
|
-
parameters.all? {|p| p.expr?(compiler)} &&
|
134
|
-
!method(compiler).return_type.void?
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
class EmtpyArray
|
139
|
-
def expr?(compiler)
|
140
|
-
size.expr?(compiler)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
class LocalAssignment
|
145
|
-
def expr?(compiler)
|
146
|
-
compiler.method.local?(name) && value.expr?(compiler)
|
147
|
-
end
|
148
|
-
|
149
|
-
def precompile(compiler)
|
150
|
-
if expr?(compiler)
|
151
|
-
self
|
152
|
-
else
|
153
|
-
compile(compiler, false)
|
154
|
-
TempValue.new(name)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
class ToString
|
160
|
-
def expr?(compiler)
|
161
|
-
body.expr?(compiler)
|
162
|
-
end
|
163
|
-
|
164
|
-
def temp(compiler, value=nil)
|
165
|
-
TempValue.new(body, compiler, value)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
class StringConcat
|
170
|
-
def expr?(compiler)
|
171
|
-
children.all? {|x| x.expr?(compiler)}
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
class Return
|
176
|
-
def expr?(compiler)
|
177
|
-
false
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
class Raise
|
182
|
-
def expr?(compiler)
|
183
|
-
false
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
class Rescue
|
188
|
-
def expr?(compiler)
|
189
|
-
false
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
class Ensure
|
194
|
-
def expr?(compiler)
|
195
|
-
false
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
class FieldAssignment
|
200
|
-
def expr?(compiler)
|
201
|
-
false
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
class Colon2
|
206
|
-
def expr?(compiler)
|
207
|
-
true
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
data/lib/mirah/plugin/gwt.rb
DELETED
@@ -1,189 +0,0 @@
|
|
1
|
-
# Copyright (c) 2010 The Mirah project authors. All Rights Reserved.
|
2
|
-
# All contributing project authors may be found in the NOTICE file.
|
3
|
-
#
|
4
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
# you may not use this file except in compliance with the License.
|
6
|
-
# You may obtain a copy of the License at
|
7
|
-
#
|
8
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
#
|
10
|
-
# Unless required by applicable law or agreed to in writing, software
|
11
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
# See the License for the specific language governing permissions and
|
14
|
-
# limitations under the License.
|
15
|
-
|
16
|
-
require 'mirah/jvm/source_generator/builder'
|
17
|
-
|
18
|
-
module Mirah::JavaSource
|
19
|
-
class ClassBuilder
|
20
|
-
def build_jsni_method(name, visibility, static, exceptions, type, *args)
|
21
|
-
finish_declaration
|
22
|
-
type ||= Mirah::AST.type(nil, :void)
|
23
|
-
@methods << JsniMethodBuilder.new(self,
|
24
|
-
:name => name,
|
25
|
-
:visibility => visibility,
|
26
|
-
:static => static,
|
27
|
-
:return => type,
|
28
|
-
:args => args,
|
29
|
-
:exceptions => exceptions)
|
30
|
-
@methods[-1]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class JsniMethodBuilder < MethodBuilder
|
35
|
-
include Helper
|
36
|
-
|
37
|
-
attr_accessor :name, :type, :out
|
38
|
-
|
39
|
-
def initialize(cls, options)
|
40
|
-
super(cls, options)
|
41
|
-
end
|
42
|
-
|
43
|
-
# Based on superclass's method.
|
44
|
-
def start
|
45
|
-
print "public#{@static} native #{@typename} #{@name}("
|
46
|
-
@args.each_with_index do |(type, name), i|
|
47
|
-
print ', ' unless i == 0
|
48
|
-
print "#{type.to_source} #{name}"
|
49
|
-
end
|
50
|
-
print ')'
|
51
|
-
unless @exceptions.empty?
|
52
|
-
print ' throws '
|
53
|
-
@exceptions.each_with_index do |exception, i|
|
54
|
-
print ', ' unless i == 0
|
55
|
-
print exception.name
|
56
|
-
end
|
57
|
-
end
|
58
|
-
puts ' /*-{'
|
59
|
-
end
|
60
|
-
|
61
|
-
# Based on superclass's method.
|
62
|
-
def stop
|
63
|
-
puts '}-*/;'
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
module Mirah::JVM::Compiler
|
69
|
-
class Base
|
70
|
-
alias :original_create_method_builder :create_method_builder
|
71
|
-
# arg_types must be an Array
|
72
|
-
def create_method_builder(name, node, static, exceptions, return_type, arg_types)
|
73
|
-
unless node.class == Mirah::AST::JsniMethodDefinition
|
74
|
-
original_create_method_builder(name, node, static, exceptions, return_type, arg_types)
|
75
|
-
else
|
76
|
-
@class.build_jsni_method(name.to_s, node.visibility, static,
|
77
|
-
exceptions, return_type, *arg_types)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
class JavaSource < Base
|
83
|
-
def define_jsni_method(node)
|
84
|
-
base_define_method(node, false) do |method, arg_types|
|
85
|
-
with :method => method do
|
86
|
-
log "Starting new JSNI method #{node.name}"
|
87
|
-
@method.start
|
88
|
-
|
89
|
-
@method.puts node.body.literal.chomp
|
90
|
-
|
91
|
-
log "JSNI method #{node.name} complete!"
|
92
|
-
@method.stop
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
module Mirah::AST
|
100
|
-
class JsniMethodDefinition < MethodDefinition
|
101
|
-
def initialize(static, parent, line_number, name, annotations=[], &block)
|
102
|
-
super(parent, line_number, name, annotations, &block)
|
103
|
-
@static = static
|
104
|
-
end
|
105
|
-
|
106
|
-
def compile(compiler, expression)
|
107
|
-
compiler.define_jsni_method(self)
|
108
|
-
end
|
109
|
-
|
110
|
-
def infer(typer, expression)
|
111
|
-
@static ||= scope.static_scope.self_type.meta? unless scope.nil?
|
112
|
-
@defining_class ||= begin
|
113
|
-
static_scope.self_node = :self
|
114
|
-
static_scope.self_type = if static?
|
115
|
-
scope.static_scope.self_type.meta
|
116
|
-
else
|
117
|
-
scope.static_scope.self_type
|
118
|
-
end
|
119
|
-
end
|
120
|
-
resolve_if(typer) do
|
121
|
-
argument_types = typer.infer(arguments, true)
|
122
|
-
if argument_types.all?
|
123
|
-
typer.learn_method_type(defining_class, name, argument_types,
|
124
|
-
signature[:return], signature[:throws])
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
# JSNI can't be abstract.
|
130
|
-
def abstract?
|
131
|
-
false
|
132
|
-
end
|
133
|
-
|
134
|
-
def static?
|
135
|
-
@static
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
defmacro 'def_jsni' do |transformer, fcall, parent|
|
140
|
-
args = fcall.parameters
|
141
|
-
|
142
|
-
unless args.size == 3
|
143
|
-
raise "def_jsni must have 3 arguments."
|
144
|
-
end
|
145
|
-
|
146
|
-
call_node = args[1]
|
147
|
-
if Self === call_node.target
|
148
|
-
is_static = true
|
149
|
-
end
|
150
|
-
|
151
|
-
JsniMethodDefinition.new(is_static,
|
152
|
-
parent,
|
153
|
-
fcall.position,
|
154
|
-
call_node.name,
|
155
|
-
transformer.annotations) do |defn|
|
156
|
-
|
157
|
-
signature = {:return => Mirah::AST.type(nil, args[0].name)}
|
158
|
-
method = call_node.parameters[0]
|
159
|
-
|
160
|
-
unless method.nil?
|
161
|
-
hash_node = method.parameters[0]
|
162
|
-
|
163
|
-
args = Arguments.new(defn, defn.position) do |args_new|
|
164
|
-
arg_list = []
|
165
|
-
hash_node.child_nodes.each_slice(2) do |name, type|
|
166
|
-
position = name.position + type.position
|
167
|
-
name = name.literal
|
168
|
-
type = Mirah::AST.type(nil, type.name)
|
169
|
-
signature[name.intern] = type
|
170
|
-
arg_list.push(RequiredArgument.new(args_new, position, name))
|
171
|
-
end
|
172
|
-
[arg_list, nil, nil, nil]
|
173
|
-
end
|
174
|
-
else
|
175
|
-
args = Arguments.new(defn, defn.position) do |args_new|
|
176
|
-
[nil, nil, nil, nil]
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
body_node = fcall.parameters[-1]
|
181
|
-
|
182
|
-
[
|
183
|
-
signature,
|
184
|
-
args,
|
185
|
-
body_node
|
186
|
-
]
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|