mirah 0.1.0.pre-java → 0.1.1-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.
Files changed (92) hide show
  1. data/History.txt +736 -0
  2. data/README.md +71 -0
  3. data/Rakefile +227 -73
  4. data/examples/Fib.class +0 -0
  5. data/examples/macros/{string-each-char.mirah → string_each_char.mirah} +2 -3
  6. data/examples/simple_class.mirah +3 -3
  7. data/examples/{dynamic.mirah → simple_class.mirah~} +7 -12
  8. data/javalib/mirah-bootstrap.jar +0 -0
  9. data/javalib/mirah-builtins.jar +0 -0
  10. data/javalib/mirah-compiler.jar +0 -0
  11. data/javalib/mirah-parser.jar +0 -0
  12. data/javalib/mirah-util.jar +0 -0
  13. data/lib/mirah.rb +8 -1
  14. data/lib/mirah/ast.rb +1 -1
  15. data/lib/mirah/ast/scope.rb +16 -0
  16. data/lib/mirah/commands/base.rb +1 -3
  17. data/lib/mirah/compiler.rb +17 -3
  18. data/lib/mirah/errors.rb +10 -10
  19. data/lib/mirah/generator.rb +21 -9
  20. data/lib/mirah/jvm/compiler.rb +17 -0
  21. data/lib/mirah/jvm/compiler/base.rb +24 -5
  22. data/lib/mirah/jvm/compiler/jvm_bytecode.rb +83 -20
  23. data/lib/mirah/jvm/method_lookup.rb +43 -22
  24. data/lib/mirah/jvm/types.rb +1 -2
  25. data/lib/mirah/jvm/types/array_type.rb +1 -6
  26. data/lib/mirah/jvm/types/ast_ext.rb +31 -0
  27. data/lib/mirah/jvm/types/basic_types.rb +1 -2
  28. data/lib/mirah/jvm/types/boolean.rb +11 -10
  29. data/lib/mirah/jvm/types/extensions.rb +14 -5
  30. data/lib/mirah/jvm/types/factory.rb +128 -43
  31. data/lib/mirah/jvm/types/floats.rb +8 -10
  32. data/lib/mirah/jvm/types/integers.rb +16 -9
  33. data/lib/mirah/jvm/types/intrinsics.rb +17 -69
  34. data/lib/mirah/jvm/types/meta_type.rb +5 -0
  35. data/lib/mirah/jvm/types/methods.rb +317 -151
  36. data/lib/mirah/jvm/types/methods.rb~ +973 -0
  37. data/lib/mirah/jvm/types/number.rb +29 -6
  38. data/lib/mirah/jvm/types/primitive_type.rb +35 -7
  39. data/lib/mirah/jvm/types/source_mirror.rb +11 -6
  40. data/lib/mirah/jvm/types/type.rb +52 -0
  41. data/lib/mirah/jvm/types/type_definition.rb +8 -2
  42. data/lib/mirah/transform/ast_ext.rb +9 -31
  43. data/lib/mirah/transform/transformer.rb +1 -1
  44. data/lib/mirah/typer.rb +2 -1
  45. data/lib/mirah/util/argument_processor.rb +10 -14
  46. data/lib/mirah/util/argument_processor.rb~ +146 -0
  47. data/lib/mirah/util/compilation_state.rb +15 -9
  48. data/lib/mirah/util/process_errors.rb +8 -2
  49. data/lib/mirah/version.rb +2 -2
  50. data/lib/mirah_task.rb +0 -7
  51. data/test/core/typer_test.rb +21 -13
  52. data/test/core/util/argument_processor_test.rb +19 -19
  53. data/test/core/util/class_loader_test.rb +19 -4
  54. data/test/core/util/compilation_state_test.rb +38 -0
  55. data/test/fixtures/org/foo/LowerCaseInnerClass$inner.class +0 -0
  56. data/test/fixtures/org/foo/LowerCaseInnerClass.class +0 -0
  57. data/test/fixtures/org/foo/LowerCaseInnerClass.java +7 -0
  58. data/test/jvm/blocks_test.rb +50 -29
  59. data/test/jvm/bytecode_test_helper.rb +71 -57
  60. data/test/jvm/cast_test.rb +162 -0
  61. data/test/jvm/constructors_test.rb +48 -0
  62. data/test/jvm/enumerable_test.rb +136 -7
  63. data/test/jvm/example_test.rb +39 -0
  64. data/test/jvm/factory_test.rb +6 -0
  65. data/test/jvm/generics_test.rb +0 -5
  66. data/test/jvm/import_test.rb +81 -0
  67. data/test/jvm/interface_test.rb +113 -0
  68. data/test/jvm/java_typer_test.rb +57 -11
  69. data/test/jvm/jvm_commands_test.rb +24 -0
  70. data/test/jvm/jvm_compiler_test.rb +186 -370
  71. data/test/jvm/macros_test.rb +67 -6
  72. data/test/jvm/main_method_test.rb +1 -1
  73. data/test/jvm/mirror_compilation_test_helper.rb +24 -0
  74. data/test/jvm/new_backend_test_helper.rb +25 -0
  75. data/test/jvm/rescue_test.rb +153 -18
  76. data/test/jvm/string_test.rb +41 -0
  77. data/test/jvm/varargs_test.rb +65 -0
  78. data/test/mirrors/base_type_test.rb +96 -0
  79. data/test/mirrors/bytecode_mirror_test.rb +86 -0
  80. data/test/mirrors/generics_test.rb +776 -0
  81. data/test/mirrors/member_test.rb +69 -0
  82. data/test/mirrors/method_lookup_test.rb +574 -0
  83. data/test/mirrors/mirrors_test.rb +562 -0
  84. data/test/mirrors/simple_async_mirror_loader_test.rb +110 -0
  85. data/test/mirrors/simple_mirror_loader_test.rb +104 -0
  86. data/test/test_helper.rb +2 -1
  87. metadata +244 -217
  88. data/README.txt +0 -59
  89. data/javalib/dynalink-0.2.jar +0 -0
  90. data/lib/mirah/jvm/typer.rb +0 -177
  91. data/lib/mirah/jvm/types/dynamic_type.rb +0 -45
  92. data/lib/mirah/jvm/types/unreachable_type.rb +0 -27
@@ -20,14 +20,14 @@ class MacrosTest < Test::Unit::TestCase
20
20
  mirah::lang::ast::Null.new
21
21
  end
22
22
 
23
- System.out.println(foo)
23
+ System.out.println(Object(foo))
24
24
  EOF
25
25
 
26
26
  assert_output("null\n") {cls.main(nil)}
27
27
  assert(!cls.respond_to?(:foo))
28
28
  end
29
29
 
30
- def test_import
30
+ def test_imported_constants_available_in_macros
31
31
  cls, = compile(<<-EOF)
32
32
  import java.util.LinkedList
33
33
  macro def foo
@@ -48,7 +48,7 @@ class MacrosTest < Test::Unit::TestCase
48
48
  mirah::lang::ast::Null.new
49
49
  end
50
50
 
51
- System.out.println(foo())
51
+ System.out.println(Object(foo()))
52
52
  EOF
53
53
 
54
54
  assert_output("null\n") {cls.main(nil)}
@@ -61,7 +61,7 @@ class MacrosTest < Test::Unit::TestCase
61
61
  quote { nil }
62
62
  end
63
63
 
64
- System.out.println(foo)
64
+ System.out.println(Object(foo))
65
65
  EOF
66
66
 
67
67
  assert_output("null\n") {cls.main(nil)}
@@ -83,7 +83,6 @@ class MacrosTest < Test::Unit::TestCase
83
83
  assert(!cls.respond_to?(:bar))
84
84
  end
85
85
 
86
-
87
86
  def test_instance_macro_call
88
87
  script, cls = compile(<<-EOF)
89
88
  class InstanceMacros
@@ -128,8 +127,54 @@ class MacrosTest < Test::Unit::TestCase
128
127
  assert_equal("foobar", script.function)
129
128
  end
130
129
 
131
- def test_unquote_method_definitions_with_main
130
+
131
+ def test_static_macro_call
132
+ script, cls = compile(<<-EOF)
133
+ class StaticMacros
134
+ def self.foobar
135
+ "foobar"
136
+ end
137
+
138
+ macro def self.macro_foobar
139
+ quote {`@call.target`.foobar}
140
+ end
141
+ end
142
+
143
+ def macro
144
+ StaticMacros.macro_foobar
145
+ end
146
+ EOF
147
+
148
+ assert_equal("foobar", script.macro)
149
+ end
150
+
151
+ def test_static_macro_vcall
132
152
  script, cls = compile(<<-EOF)
153
+ class StaticMacros2
154
+ def foobar
155
+ "foobar"
156
+ end
157
+
158
+ macro def self.macro_foobar
159
+ quote {`@call.target`.new.foobar}
160
+ end
161
+
162
+ def self.call_foobar
163
+ macro_foobar
164
+ end
165
+ end
166
+
167
+ def function
168
+ StaticMacros2.call_foobar
169
+ end
170
+ EOF
171
+
172
+ assert_equal("foobar", script.function)
173
+ end
174
+
175
+
176
+ def test_unquote_method_definitions_with_main
177
+ script, cls = compile(<<-'EOF')
133
178
  class UnquoteMacros
134
179
  macro def self.make_attr(name_node:Identifier, type:TypeName)
135
180
  name = name_node.identifier
@@ -275,4 +320,20 @@ class MacrosTest < Test::Unit::TestCase
275
320
  EOF
276
321
  assert_output("0\n3\n") {script.main(nil)}
277
322
  end
323
+
324
+ def test_separate_compilation
325
+ compile(<<-CODE)
326
+ class InlineOneSayer
327
+ macro def say_one
328
+ quote do
329
+ puts "one"
330
+ end
331
+ end
332
+ end
333
+ CODE
334
+ script, _ =compile(<<-CODE)
335
+ InlineOneSayer.new.say_one
336
+ CODE
337
+ assert_output("one\n") {script.main(nil)}
338
+ end
278
339
  end
@@ -6,7 +6,7 @@ class MainMethodTest < Test::Unit::TestCase
6
6
  System.out.println 'bar'
7
7
  EOC
8
8
 
9
- main_class, = compile code, 'with_main'
9
+ main_class, = compile code, :name => 'with_main'
10
10
 
11
11
  assert_output "bar\n" do
12
12
  main_class.main(nil)
@@ -0,0 +1,24 @@
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
+ require 'new_backend_test_helper'
16
+ require 'stringio'
17
+ require 'fileutils'
18
+
19
+ module JVMCompiler
20
+ def type_system
21
+ # TODO: classpath
22
+ Java::OrgMirahJvmMirrors::MirrorTypeSystem.new
23
+ end
24
+ end
@@ -0,0 +1,25 @@
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
+ require 'bytecode_test_helper'
16
+
17
+ module JVMCompiler
18
+ def compiler_type
19
+ Mirah::JVM::Compiler::Backend
20
+ end
21
+
22
+ def compiler_name
23
+ "new"
24
+ end
25
+ end
@@ -15,13 +15,13 @@
15
15
 
16
16
  class RescueTest < Test::Unit::TestCase
17
17
 
18
- def test_rescue
18
+ def test_rescue_with_no_raise_runs_begin_and_not_rescue
19
19
  cls, = compile(<<-EOF)
20
20
  def foo
21
21
  begin
22
- System.out.println "body"
22
+ puts "body"
23
23
  rescue
24
- System.out.println "rescue"
24
+ puts "rescue"
25
25
  end
26
26
  end
27
27
  EOF
@@ -30,14 +30,16 @@ class RescueTest < Test::Unit::TestCase
30
30
  cls.foo
31
31
  end
32
32
  assert_equal("body\n", output)
33
+ end
33
34
 
35
+ def test_rescue_with_raise_after_begin_runs_rescue
34
36
  cls, = compile(<<-EOF)
35
37
  def foo
36
38
  begin
37
- System.out.println "body"
39
+ puts "body"
38
40
  raise
39
41
  rescue
40
- System.out.println "rescue"
42
+ puts "rescue"
41
43
  end
42
44
  end
43
45
  EOF
@@ -46,20 +48,22 @@ class RescueTest < Test::Unit::TestCase
46
48
  cls.foo
47
49
  end
48
50
  assert_equal("body\nrescue\n", output)
51
+ end
49
52
 
53
+ def test_rescue_with_type_clause_and_untyped_clause
50
54
  cls, = compile(<<-EOF)
51
55
  def foo(a:int)
52
56
  begin
53
- System.out.println "body"
57
+ puts "body"
54
58
  if a == 0
55
59
  raise IllegalArgumentException
56
60
  else
57
61
  raise
58
62
  end
59
63
  rescue IllegalArgumentException
60
- System.out.println "IllegalArgumentException"
64
+ puts "IllegalArgumentException"
61
65
  rescue
62
- System.out.println "rescue"
66
+ puts "rescue"
63
67
  end
64
68
  end
65
69
  EOF
@@ -69,11 +73,14 @@ class RescueTest < Test::Unit::TestCase
69
73
  cls.foo(0)
70
74
  end
71
75
  assert_equal("body\nrescue\nbody\nIllegalArgumentException\n", output)
76
+ end
72
77
 
78
+
79
+ def test_rescue_with_multiple_types_or_throwable
73
80
  cls, = compile(<<-EOF)
74
81
  def foo(a:int)
75
82
  begin
76
- System.out.println "body"
83
+ puts "body"
77
84
  if a == 0
78
85
  raise IllegalArgumentException
79
86
  elsif a == 1
@@ -82,9 +89,9 @@ class RescueTest < Test::Unit::TestCase
82
89
  raise
83
90
  end
84
91
  rescue IllegalArgumentException, Exception
85
- System.out.println "multi"
92
+ puts "multi"
86
93
  rescue Throwable
87
- System.out.println "other"
94
+ puts "other"
88
95
  end
89
96
  end
90
97
  EOF
@@ -95,13 +102,15 @@ class RescueTest < Test::Unit::TestCase
95
102
  cls.foo(2)
96
103
  end
97
104
  assert_equal("body\nmulti\nbody\nother\nbody\nmulti\n", output)
105
+ end
98
106
 
107
+ def test_rescue_without_type_with_argument
99
108
  cls, = compile(<<-EOF)
100
109
  def foo
101
110
  begin
102
111
  raise "foo"
103
112
  rescue => ex
104
- System.out.println ex.getMessage
113
+ puts ex.getMessage
105
114
  end
106
115
  end
107
116
  EOF
@@ -110,8 +119,9 @@ class RescueTest < Test::Unit::TestCase
110
119
  cls.foo
111
120
  end
112
121
  assert_equal("foo\n", output)
122
+ end
113
123
 
114
-
124
+ def test_implicit_begin_on_method_with_rescue_and_else
115
125
  cls, = compile(<<-EOF)
116
126
  def foo(x:boolean)
117
127
  # throws Exception
@@ -126,11 +136,12 @@ class RescueTest < Test::Unit::TestCase
126
136
  EOF
127
137
 
128
138
  assert_equal "x", cls.foo(true)
129
- ex = assert_raise NativeException do
139
+ assert_raise_java java.lang.Exception, "!x" do
130
140
  cls.foo(false)
131
141
  end
132
- assert_equal "java.lang.Exception: !x", ex.message
142
+ end
133
143
 
144
+ def test_rescue_with_return_types_correctly
134
145
  cls, = compile(<<-EOF)
135
146
  def foo:long
136
147
  begin
@@ -148,15 +159,63 @@ class RescueTest < Test::Unit::TestCase
148
159
  assert_equal 1, cls.foo
149
160
  end
150
161
 
151
- def test_empty_rescues
162
+ def test_rescue_with_return_returns_value
163
+ cls, = compile(<<-EOF)
164
+ def foo:long
165
+ begin
166
+ raise "some error"
167
+ rescue Exception => e
168
+ return long(0)
169
+ end
170
+ end
171
+ EOF
172
+
173
+ assert_equal 0, cls.foo
174
+ end
175
+
176
+ def test_rescue_with_else_clause_returns_from_else_when_no_exception
152
177
  cls, = compile(<<-EOF)
178
+ def foo:long
179
+ begin
180
+ long(0)
181
+ rescue
182
+ long(1)
183
+ else
184
+ long(2)
185
+ end
186
+ end
187
+ EOF
188
+
189
+ assert_equal 2, cls.foo
190
+ end
191
+
192
+ def test_rescue_with_else_clause_returns_from_rescue_when_exception_raised
193
+ cls, = compile(<<-EOF)
194
+ def foo:long
195
+ begin
196
+ raise "oh, my"
197
+ rescue
198
+ long(1)
199
+ else
200
+ long(2)
201
+ end
202
+ end
203
+ EOF
204
+
205
+ assert_equal 1, cls.foo
206
+ end
207
+
208
+ def test_empty_rescue_body_compiles
209
+ compile(<<-EOF)
153
210
  begin
154
211
  rescue
155
212
  nil
156
213
  end
157
214
  EOF
215
+ end
158
216
 
159
- cls, = compile(<<-EOF)
217
+ def test_rescue_thats_not_an_expression_compiles
218
+ compile(<<-EOF)
160
219
  begin
161
220
  ""
162
221
  rescue
@@ -164,7 +223,9 @@ class RescueTest < Test::Unit::TestCase
164
223
  end
165
224
  nil
166
225
  EOF
226
+ end
167
227
 
228
+ def test_rescue_containing_while_with_ensure_runs_ensure
168
229
  cls, = compile(<<-EOF)
169
230
  def empty_with_ensure
170
231
  begin
@@ -174,10 +235,84 @@ class RescueTest < Test::Unit::TestCase
174
235
  end
175
236
  rescue
176
237
  ensure
177
- System.out.println 'ensuring'
238
+ puts 'ensuring'
178
239
  end
179
240
  ""
180
241
  end
181
242
  EOF
243
+ assert_output "ensuring\n" do
244
+ cls.empty_with_ensure
245
+ end
246
+ end
247
+
248
+ def test_empty_rescue_body_with_else_runs_else
249
+ cls, = compile(<<-EOF)
250
+ begin
251
+ rescue
252
+ nil
253
+ else
254
+ puts "else"
255
+ end
256
+ EOF
257
+ assert_output "else\n" do
258
+ cls.main nil
259
+ end
260
+ end
261
+
262
+ def test_ensure
263
+ cls, = compile(<<-EOF)
264
+ def foo
265
+ 1
266
+ ensure
267
+ puts "Hi"
268
+ end
269
+ EOF
270
+ output = capture_output do
271
+ assert_equal(1, cls.foo)
272
+ end
273
+ assert_equal "Hi\n", output
274
+
275
+ cls, = compile(<<-EOF)
276
+ def foo
277
+ return 1
278
+ ensure
279
+ puts "Hi"
280
+ end
281
+ EOF
282
+ output = capture_output do
283
+ assert_equal(1, cls.foo)
284
+ end
285
+ assert_equal "Hi\n", output
286
+
287
+ cls, = compile(<<-EOF)
288
+ def foo
289
+ begin
290
+ break
291
+ ensure
292
+ puts "Hi"
293
+ end while false
294
+ end
295
+ EOF
296
+ output = capture_output do
297
+ cls.foo
298
+ end
299
+ assert_equal "Hi\n", output
300
+ end
301
+
302
+ def test_loop_in_ensure
303
+ cls, = compile(<<-EOF)
304
+ begin
305
+ puts "a"
306
+ begin
307
+ puts "b"
308
+ break
309
+ end while false
310
+ puts "c"
311
+ ensure
312
+ puts "ensure"
313
+ end
314
+ EOF
315
+
316
+ assert_output("a\nb\nc\nensure\n") { cls.main(nil) }
182
317
  end
183
318
  end