mirah 0.1.4-java → 0.2.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/History.txt +531 -0
- data/README.md +23 -10
- data/Rakefile +239 -156
- data/TODO.md +71 -10
- data/bin/mirah +1 -1
- data/bin/mirahc +1 -1
- data/dist/mirahc.jar +0 -0
- data/examples/bintrees.mirah +2 -2
- data/examples/construction.mirah +2 -2
- data/examples/fields.mirah +1 -1
- data/examples/fractal.mirah +1 -1
- data/examples/fractal.rb +70 -0
- data/examples/interfaces.mirah +1 -1
- data/examples/java_thing.mirah +1 -1
- data/examples/macros/square.mirah +3 -3
- data/examples/macros/square_int.mirah +3 -3
- data/examples/macros/string_each_char.mirah +6 -6
- data/examples/rosettacode/100-doors.mirah +0 -2
- data/examples/rosettacode/count-occurrences-of-a-substring.mirah +3 -3
- data/examples/rosettacode/empty-string.mirah +1 -1
- data/examples/rosettacode/fizz-buzz.mirah +4 -4
- data/examples/rosettacode/is-string-numeric.mirah +7 -7
- data/examples/rosettacode/palindrome.mirah +2 -2
- data/examples/rosettacode/reverse-a-string.mirah +1 -1
- data/examples/rosettacode/rot-13.mirah +1 -1
- data/examples/{edb.mirah → rosettacode/simple_character_math.mirah} +13 -4
- data/examples/rosettacode/string-case.mirah +2 -2
- data/examples/rosettacode/string-length.mirah +1 -1
- data/examples/swing.mirah +9 -14
- data/extensions_and_macros.md +117 -0
- data/lib/mirah.rb +1 -1
- data/lib/mirah/errors.rb +3 -1
- data/lib/mirah/transform/ast_ext.rb +3 -2
- data/lib/mirah/util/process_errors.rb +1 -2
- data/lib/mirah/version.rb +1 -1
- data/test/A.class +0 -0
- data/test/core/util/jvm_version_test.rb +10 -0
- data/test/core/util/mirah_arguments_test.rb +51 -4
- data/test/fixtures/cp1251_test.mirah +7 -0
- data/test/fixtures/org/foo/AbstractExecutorJava8.java +30 -0
- data/test/fixtures/org/foo/ClassWithSelfReferencingTypeParameter.java +24 -0
- data/test/fixtures/org/foo/InnerInterfaceClass.java +12 -0
- data/test/fixtures/org/foo/IntAnno.class +0 -0
- data/test/fixtures/org/foo/TypeFixtureJava8.java +10 -0
- data/test/fixtures/utf8_test.mirah +7 -0
- data/test/jvm/access_levels_test.rb +31 -0
- data/test/jvm/annotations_test.rb +3 -6
- data/test/jvm/blocks_test.rb +303 -120
- data/test/jvm/cast_test.rb +123 -50
- data/test/jvm/closure_test.rb +242 -0
- data/test/jvm/constructors_test.rb +1 -3
- data/test/jvm/example_test.rb +6 -2
- data/test/jvm/extensions/array_extensions_test.rb +181 -0
- data/test/jvm/extensions/collection_extensions_test.rb +195 -0
- data/test/jvm/{enumerable_test.rb → extensions/enumerable_test.rb} +81 -13
- data/test/jvm/extensions/hash_extensions_test.rb +56 -0
- data/test/jvm/extensions/list_extensions_test.rb +143 -0
- data/test/jvm/extensions/lock_extensions_test.rb +43 -0
- data/test/jvm/{numeric_extensions_test.rb → extensions/numeric_extensions_test.rb} +0 -0
- data/test/jvm/extensions/numeric_operators_test.rb +86 -0
- data/test/jvm/extensions/object_extensions_test.rb +122 -0
- data/test/jvm/{string_builder_extensions_test.rb → extensions/string_builder_extensions_test.rb} +0 -0
- data/test/jvm/{string_extensions_test.rb → extensions/string_extensions_test.rb} +57 -4
- data/test/jvm/generics_test.rb +14 -6
- data/test/jvm/import_test.rb +38 -1
- data/test/jvm/interface_test.rb +17 -0
- data/test/jvm/jvm_commands_test.rb +9 -0
- data/test/jvm/jvm_compiler_test.rb +568 -43
- data/test/jvm/macros_test.rb +343 -19
- data/test/jvm/main_method_test.rb +1 -3
- data/test/jvm/new_backend_test_helper.rb +54 -7
- data/test/jvm/rescue_test.rb +20 -5
- data/test/jvm/static_fields_test.rb +52 -10
- data/test/jvm/{mirror_compilation_test_helper.rb → string_test.rb} +10 -9
- data/test/jvm/varargs_test.rb +6 -16
- data/test/mirrors/base_type_test.rb +20 -7
- data/test/mirrors/bytecode_mirror_test.rb +8 -3
- data/test/mirrors/generics_test.rb +89 -10
- data/test/mirrors/member_test.rb +1 -1
- data/test/mirrors/method_lookup_test.rb +10 -3
- data/test/mirrors/mirrors_test.rb +20 -20
- data/test/mirrors/simple_async_mirror_loader_test.rb +1 -1
- data/test/mirrors/simple_mirror_loader_test.rb +1 -1
- data/test/newMirahClass$Closure2.class +0 -0
- data/test/newMirahClass.class +0 -0
- data/test/test_helper.rb +8 -1
- metadata +31 -16
- data/bin/bundler +0 -16
- data/bin/rake +0 -16
- data/examples/ant/example-build.xml~ +0 -7
- data/examples/test.edb +0 -9
- data/lib/mirah/compiler.rb +0 -67
- data/lib/mirah/parser.rb +0 -224
- data/lib/mirah/util/delegate.rb +0 -65
- data/test/jvm/list_extensions_test.rb +0 -23
data/test/jvm/cast_test.rb
CHANGED
@@ -12,67 +12,68 @@
|
|
12
12
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
13
|
# See the License for the specific language governing permissions and
|
14
14
|
# limitations under the License.
|
15
|
+
require 'test_helper'
|
15
16
|
|
16
17
|
class CastTest < Test::Unit::TestCase
|
17
18
|
|
18
19
|
def test_cast
|
19
20
|
cls, = compile(<<-EOF)
|
20
|
-
def f2b;
|
21
|
-
def f2s;
|
22
|
-
def f2c;
|
23
|
-
def f2i;
|
24
|
-
def f2l;
|
25
|
-
def f2d;
|
26
|
-
|
27
|
-
def i2b; byte
|
28
|
-
def i2s; short
|
29
|
-
def i2c; char
|
30
|
-
def i2l; long
|
31
|
-
def i2f; float
|
32
|
-
def i2d; int
|
33
|
-
|
34
|
-
def b2s;
|
35
|
-
def b2c;
|
36
|
-
def b2i;
|
37
|
-
def b2l;
|
38
|
-
def b2f;
|
39
|
-
def b2d;
|
40
|
-
|
41
|
-
def s2b;
|
42
|
-
def s2c;
|
43
|
-
def s2i;
|
44
|
-
def s2l;
|
45
|
-
def s2f;
|
46
|
-
def s2d;
|
47
|
-
|
48
|
-
def c2b;
|
49
|
-
def c2s;
|
50
|
-
def c2i;
|
51
|
-
def c2l;
|
52
|
-
def c2f;
|
53
|
-
def c2d;
|
54
|
-
|
55
|
-
def l2b;
|
56
|
-
def l2c;
|
57
|
-
def l2i;
|
58
|
-
def l2l; long(long
|
59
|
-
def l2f;
|
60
|
-
def l2d;
|
61
|
-
|
62
|
-
def d2b;
|
63
|
-
def d2s;
|
64
|
-
def d2c;
|
65
|
-
def d2i;
|
66
|
-
def d2l;
|
67
|
-
def d2f;
|
21
|
+
def f2b; 1.0.as!(byte); end
|
22
|
+
def f2s; 1.0.as!(short); end
|
23
|
+
def f2c; 1.0.as!(char); end
|
24
|
+
def f2i; 1.0.as!(int); end
|
25
|
+
def f2l; 1.0.as!(long); end
|
26
|
+
def f2d; 1.0.as!(int); end
|
27
|
+
|
28
|
+
def i2b; 1.as!(byte); end
|
29
|
+
def i2s; 1.as!(short); end
|
30
|
+
def i2c; 1.as!(char); end
|
31
|
+
def i2l; 1.as!(long); end
|
32
|
+
def i2f; 1.as!(float); end
|
33
|
+
def i2d; 1.as!(int); end
|
34
|
+
|
35
|
+
def b2s; 1.as!(byte).as!(short); end
|
36
|
+
def b2c; 1.as!(byte).as!(char); end
|
37
|
+
def b2i; 1.as!(byte).as!(int); end
|
38
|
+
def b2l; 1.as!(byte).as!(long); end
|
39
|
+
def b2f; 1.as!(byte).as!(float); end
|
40
|
+
def b2d; 1.as!(byte).as!(double); end
|
41
|
+
|
42
|
+
def s2b; 1.as!(short).as!(byte); end
|
43
|
+
def s2c; 1.as!(short).as!(char); end
|
44
|
+
def s2i; 1.as!(short).as!(int); end
|
45
|
+
def s2l; 1.as!(short).as!(long); end
|
46
|
+
def s2f; 1.as!(short).as!(float); end
|
47
|
+
def s2d; 1.as!(short).as!(double); end
|
48
|
+
|
49
|
+
def c2b; 1.as!(char).as!(byte); end
|
50
|
+
def c2s; 1.as!(char).as!(short); end
|
51
|
+
def c2i; 1.as!(char).as!(int); end
|
52
|
+
def c2l; 1.as!(char).as!(long); end
|
53
|
+
def c2f; 1.as!(char).as!(float); end
|
54
|
+
def c2d; 1.as!(char).as!(double); end
|
55
|
+
|
56
|
+
def l2b; 1.as!(long).as!(byte); end
|
57
|
+
def l2c; 1.as!(long).as!(char); end
|
58
|
+
def l2i; 1.as!(long).as!(int); end
|
59
|
+
def l2l; 1.as!(long).as!(long); end
|
60
|
+
def l2f; 1.as!(long).as!(float); end
|
61
|
+
def l2d; 1.as!(long).as!(double); end
|
62
|
+
|
63
|
+
def d2b; 1.0.as!(byte); end
|
64
|
+
def d2s; 1.0.as!(short); end
|
65
|
+
def d2c; 1.0.as!(char); end
|
66
|
+
def d2i; 1.0.as!(int); end
|
67
|
+
def d2l; 1.0.as!(long); end
|
68
|
+
def d2f; 1.0.as!(float); end
|
68
69
|
|
69
70
|
def hard_i2f(a:int)
|
70
|
-
|
71
|
+
if a < 0
|
71
72
|
a *= -1
|
72
73
|
a * 2
|
73
74
|
else
|
74
75
|
a * 2
|
75
|
-
end
|
76
|
+
end.as! float
|
76
77
|
end
|
77
78
|
EOF
|
78
79
|
|
@@ -133,6 +134,22 @@ class CastTest < Test::Unit::TestCase
|
|
133
134
|
end
|
134
135
|
|
135
136
|
def test_array_cast
|
137
|
+
cls, = compile(<<-EOF)
|
138
|
+
def foo(a: Object)
|
139
|
+
bar(a.as!(String[]))
|
140
|
+
end
|
141
|
+
|
142
|
+
def bar(a: String[])
|
143
|
+
a[0]
|
144
|
+
end
|
145
|
+
EOF
|
146
|
+
|
147
|
+
assert_equal("foo", cls.foo(["foo", "bar"].to_java(:string)))
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
def test_warn_on_array_cast
|
152
|
+
# TODO
|
136
153
|
cls, = compile(<<-EOF)
|
137
154
|
def foo(a:Object)
|
138
155
|
bar(String[].cast(a))
|
@@ -146,6 +163,20 @@ class CastTest < Test::Unit::TestCase
|
|
146
163
|
assert_equal("foo", cls.foo(["foo", "bar"].to_java(:string)))
|
147
164
|
end
|
148
165
|
|
166
|
+
def test_cast_array_as_primitive
|
167
|
+
cls, = compile(<<-EOF)
|
168
|
+
def foo(a:Object)
|
169
|
+
bar(a.as!(int[]))
|
170
|
+
end
|
171
|
+
|
172
|
+
def bar(a:int[])
|
173
|
+
a[0]
|
174
|
+
end
|
175
|
+
EOF
|
176
|
+
|
177
|
+
assert_equal(2, cls.foo([2, 3].to_java(:int)))
|
178
|
+
end
|
179
|
+
|
149
180
|
def test_array_cast_primitive
|
150
181
|
cls, = compile(<<-EOF)
|
151
182
|
def foo(a:Object)
|
@@ -159,4 +190,46 @@ class CastTest < Test::Unit::TestCase
|
|
159
190
|
|
160
191
|
assert_equal(2, cls.foo([2, 3].to_java(:int)))
|
161
192
|
end
|
193
|
+
|
194
|
+
def test_explicit_block_argument_cast_in_array_iteration
|
195
|
+
cls, = compile(<<-EOF)
|
196
|
+
def foo():int
|
197
|
+
list = [1,2,3]
|
198
|
+
m = 0
|
199
|
+
list.each do |x: int|
|
200
|
+
m += x
|
201
|
+
end
|
202
|
+
return m
|
203
|
+
end
|
204
|
+
EOF
|
205
|
+
assert_equal 6, cls.foo
|
206
|
+
end
|
207
|
+
|
208
|
+
def test_explicit_call_cast_in_array_iteration
|
209
|
+
cls, = compile(<<-EOF)
|
210
|
+
def foo():int
|
211
|
+
list = [1,2,3]
|
212
|
+
m = 0
|
213
|
+
list.each do |x|
|
214
|
+
m = int(x) + m
|
215
|
+
end
|
216
|
+
return m
|
217
|
+
end
|
218
|
+
EOF
|
219
|
+
assert_equal 6, cls.foo
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_explicit_as_macro_cast_in_array_iteration
|
223
|
+
cls, = compile(<<-EOF)
|
224
|
+
def foo():int
|
225
|
+
list = [1,2,3]
|
226
|
+
m = 0
|
227
|
+
list.each do |x|
|
228
|
+
m = x.as!(int) + m
|
229
|
+
end
|
230
|
+
return m
|
231
|
+
end
|
232
|
+
EOF
|
233
|
+
assert_equal 6, cls.foo
|
234
|
+
end
|
162
235
|
end
|
@@ -0,0 +1,242 @@
|
|
1
|
+
# Copyright (c) 2010-2013 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
|
+
class ClosureTest < Test::Unit::TestCase
|
17
|
+
|
18
|
+
def test_interface_defined_after_used
|
19
|
+
cls, = compile(%q{
|
20
|
+
class FooTest1
|
21
|
+
def self.main(argv:String[]):void
|
22
|
+
FooB.new.action do
|
23
|
+
puts "executed foo1"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class FooB
|
29
|
+
def action(foo_param:Foo_Interface):void
|
30
|
+
foo_param.anymethod
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
interface Foo_Interface
|
35
|
+
def anymethod():void; end
|
36
|
+
end
|
37
|
+
|
38
|
+
class FooTest2
|
39
|
+
def self.main(argv:String[]):void
|
40
|
+
FooB.new.action do
|
41
|
+
puts "executed foo2"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
FooTest2.main(String[0])
|
46
|
+
FooTest1.main(String[0])
|
47
|
+
})
|
48
|
+
assert_run_output("executed foo2\nexecuted foo1\n", cls)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_super_is_not_synthesized_when_not_necessary
|
52
|
+
cls, = compile(%q{
|
53
|
+
class Bar
|
54
|
+
def initialize(val:int)
|
55
|
+
puts "bar"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Foo < Bar
|
60
|
+
attr_accessor baz:String
|
61
|
+
|
62
|
+
def initialize(val:int)
|
63
|
+
super
|
64
|
+
foo = 5
|
65
|
+
puts "foo"
|
66
|
+
perform do
|
67
|
+
puts foo
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def perform(runnable:Runnable)
|
72
|
+
runnable.run
|
73
|
+
end
|
74
|
+
end
|
75
|
+
Foo.new(3)
|
76
|
+
})
|
77
|
+
assert_run_output("bar\nfoo\n5\n", cls)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_lambda_and_other_closures_coexist
|
81
|
+
cls, = compile(%q[
|
82
|
+
class Foo
|
83
|
+
|
84
|
+
def anymethod()
|
85
|
+
foo = 5
|
86
|
+
perform do
|
87
|
+
puts "1: #{foo}"
|
88
|
+
end
|
89
|
+
a = lambda(Runnable) do
|
90
|
+
puts "2: #{foo}"
|
91
|
+
end
|
92
|
+
perform(a)
|
93
|
+
end
|
94
|
+
|
95
|
+
def perform(runnable:Runnable)
|
96
|
+
runnable.run
|
97
|
+
end
|
98
|
+
end
|
99
|
+
Foo.new.anymethod
|
100
|
+
])
|
101
|
+
assert_run_output("1: 5\n2: 5\n", cls)
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_doubly_nested_lambda
|
105
|
+
cls, = compile(%q[
|
106
|
+
class Foo
|
107
|
+
abstract def test(param:String); end
|
108
|
+
end
|
109
|
+
|
110
|
+
class Bar < Foo
|
111
|
+
def test(param)
|
112
|
+
y = 5
|
113
|
+
x = lambda(Runnable) do
|
114
|
+
puts "so: #{param}"
|
115
|
+
z = lambda(Runnable) do
|
116
|
+
puts "hey you #{y}"
|
117
|
+
end
|
118
|
+
z.run
|
119
|
+
end
|
120
|
+
x.run
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
Bar.new.test("bar")
|
125
|
+
])
|
126
|
+
assert_run_output("so: bar\nhey you 5\n", cls)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_direct_invoke_on_lambda
|
130
|
+
cls, = compile(%q[
|
131
|
+
lambda(Runnable) do
|
132
|
+
puts "abc"
|
133
|
+
end.run
|
134
|
+
])
|
135
|
+
assert_run_output("abc\n", cls)
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_lambda_contains_methods
|
139
|
+
cls, = compile(%q[
|
140
|
+
foo = 3
|
141
|
+
|
142
|
+
lambda(Runnable) do
|
143
|
+
def run
|
144
|
+
puts foo
|
145
|
+
end
|
146
|
+
end.run
|
147
|
+
])
|
148
|
+
assert_run_output("3\n", cls)
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_closure_over_array_parameter
|
152
|
+
cls, = compile(%q{
|
153
|
+
def bar(param:byte[])
|
154
|
+
runnable = lambda(Runnable) do
|
155
|
+
puts param[0]
|
156
|
+
end
|
157
|
+
|
158
|
+
runnable.run
|
159
|
+
end
|
160
|
+
|
161
|
+
bar(byte[1])
|
162
|
+
})
|
163
|
+
assert_run_output("0\n", cls)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_closure_with_typed_parameter
|
167
|
+
cls, = compile(%q{
|
168
|
+
class Bar
|
169
|
+
end
|
170
|
+
|
171
|
+
interface Foo
|
172
|
+
def action(bar:Bar); end
|
173
|
+
end
|
174
|
+
|
175
|
+
class FooCaller
|
176
|
+
|
177
|
+
def with_foo(foo:Foo):void
|
178
|
+
foo.action(Bar.new)
|
179
|
+
end
|
180
|
+
|
181
|
+
def call_with_foo
|
182
|
+
with_foo do |bar:Bar|
|
183
|
+
puts "Closure called."
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
FooCaller.new.call_with_foo
|
189
|
+
})
|
190
|
+
assert_run_output("Closure called.\n", cls)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_deep_nested_runnable_with_binding
|
194
|
+
cls, = compile(%q{
|
195
|
+
class TestBuilder
|
196
|
+
def self.create(b: Runnable):void
|
197
|
+
b.run()
|
198
|
+
end
|
199
|
+
|
200
|
+
def self.main(args:String[]):void
|
201
|
+
level_0 = "level_0"
|
202
|
+
b = create do
|
203
|
+
level_1="level_1"
|
204
|
+
TestBuilder.create do
|
205
|
+
level_2="level_2"
|
206
|
+
TestBuilder.create do
|
207
|
+
level_3="level_3"
|
208
|
+
puts "#{level_0} #{level_1} #{level_2} #{level_3}"
|
209
|
+
end
|
210
|
+
TestBuilder.create do
|
211
|
+
level_2="level_2_3"
|
212
|
+
puts "#{level_0} #{level_1} #{level_2}"
|
213
|
+
end
|
214
|
+
puts "#{level_0} #{level_1} #{level_2}"
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
TestBuilder.main(String[0])
|
221
|
+
})
|
222
|
+
assert_run_output("level_0 level_1 level_2 level_3\nlevel_0 level_1 level_2_3\nlevel_0 level_1 level_2_3\n", cls)
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_closure_with_assignment_in_rescue
|
226
|
+
cls, = compile(%q{
|
227
|
+
foo = nil
|
228
|
+
t = Thread.new do
|
229
|
+
begin
|
230
|
+
raise Exception
|
231
|
+
rescue => e
|
232
|
+
foo = Integer.new(3)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
t.start
|
236
|
+
t.join
|
237
|
+
puts foo
|
238
|
+
})
|
239
|
+
assert_run_output("3\n", cls)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|