mirah 0.0.12-java → 0.1.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.
- 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
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Copyright (c) 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
|
+
# these are here to make sure the examples still run.
|
|
17
|
+
#
|
|
18
|
+
class ExampleTest < Test::Unit::TestCase
|
|
19
|
+
def compile_ex name
|
|
20
|
+
filename = File.dirname(__FILE__) + "/../../examples/#{name}.mirah"
|
|
21
|
+
compile(open(filename).read).first
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def example_test name, output
|
|
25
|
+
cls = compile_ex name
|
|
26
|
+
assert_output(output) { cls.main nil }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
{
|
|
30
|
+
'simple_class' => "constructor\nHello, \nMirah\n",
|
|
31
|
+
'macros/square' => "2.0\n8.0\n",
|
|
32
|
+
'macros/square_int' => "2.0\n8.0\n",
|
|
33
|
+
'macros/string_each_char' => "l\na\na\nt\n \nd\ne\n \nl\ne\ne\ne\nu\nw\n \nn\ni\ne\nt\n \ni\nn\n \nz\ni\nj\nn\n \nh\ne\nm\np\ni\ne\n \ns\nt\na\na\nn\n"
|
|
34
|
+
}.each do |example,output|
|
|
35
|
+
define_method "test_#{example}" do
|
|
36
|
+
example_test example, output
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if Float(ENV_JAVA['java.specification.version']) >= 1.7
|
|
41
|
+
def test_dynamic
|
|
42
|
+
example_test 'dynamic', "I got a java.util.Collections$UnmodifiableRandomAccessList of size 3\nI got a SizeThing of size 12\n"
|
|
43
|
+
end
|
|
44
|
+
else
|
|
45
|
+
def test_dynamic
|
|
46
|
+
ex = assert_raise Mirah::MirahError do
|
|
47
|
+
example_test 'dynamic', nil
|
|
48
|
+
end
|
|
49
|
+
assert_equal "Target JVM version: 1.6 doesn't support invoke dynamic", ex.message
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
data/test/jvm/factory_test.rb
CHANGED
|
@@ -14,9 +14,15 @@
|
|
|
14
14
|
# limitations under the License.
|
|
15
15
|
|
|
16
16
|
class FactoryTest < Test::Unit::TestCase
|
|
17
|
-
def test_bootclass_loader_can_find_things_on_bootclasspath
|
|
17
|
+
def test_bootclass_loader_can_find_things_on_bootclasspath
|
|
18
18
|
factory = Mirah::JVM::Types::TypeFactory.new
|
|
19
19
|
factory.bootclasspath = File.expand_path("#{__FILE__}/../../fixtures/") +"/"
|
|
20
20
|
assert factory.bootstrap_loader.get_resource 'org/foo/A.class'
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
def test_nil_classpath_is_ok
|
|
24
|
+
factory = Mirah::JVM::Types::TypeFactory.new
|
|
25
|
+
factory.classpath = nil
|
|
26
|
+
end
|
|
27
|
+
|
|
22
28
|
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Copyright (c) 2012 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 'test_helper'
|
|
17
|
+
require 'jvm/bytecode_test_helper'
|
|
18
|
+
class TestGenerics < Test::Unit::TestCase
|
|
19
|
+
|
|
20
|
+
def parse_and_type code, name=tmp_script_name
|
|
21
|
+
parse_and_resolve_types name, code
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_generics_calls_collections
|
|
25
|
+
cls, = compile(<<-EOF)
|
|
26
|
+
import java.util.ArrayList
|
|
27
|
+
|
|
28
|
+
foo = ArrayList.new()
|
|
29
|
+
foo.add("first string")
|
|
30
|
+
foo.add("second string")
|
|
31
|
+
System.out.println(foo.get(1).substring(2))
|
|
32
|
+
EOF
|
|
33
|
+
|
|
34
|
+
assert_output("cond string\n") do
|
|
35
|
+
cls.main(nil)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_generics_generic_payload
|
|
40
|
+
cls, = compile(<<-EOF)
|
|
41
|
+
import java.util.ArrayList
|
|
42
|
+
|
|
43
|
+
foo = ArrayList.new()
|
|
44
|
+
foo.add("first string")
|
|
45
|
+
foo.add("second string")
|
|
46
|
+
bar = ArrayList.new()
|
|
47
|
+
bar.add(foo)
|
|
48
|
+
System.out.println(bar.get(0).get(1).substring(2))
|
|
49
|
+
EOF
|
|
50
|
+
|
|
51
|
+
assert_output("cond string\n") do
|
|
52
|
+
cls.main(nil)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Copyright (c) 2012 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 HashTest < Test::Unit::TestCase
|
|
17
|
+
def test_hashes
|
|
18
|
+
cls, = compile(<<-EOF)
|
|
19
|
+
def foo1
|
|
20
|
+
{a:"A", b:"B"}
|
|
21
|
+
end
|
|
22
|
+
def foo2
|
|
23
|
+
return {a:"A", b:"B"}
|
|
24
|
+
end
|
|
25
|
+
EOF
|
|
26
|
+
|
|
27
|
+
map = cls.foo1
|
|
28
|
+
assert_equal("A", map["a"])
|
|
29
|
+
assert_equal("B", map["b"])
|
|
30
|
+
map = cls.foo2
|
|
31
|
+
assert_equal("A", map["a"])
|
|
32
|
+
assert_equal("B", map["b"])
|
|
33
|
+
|
|
34
|
+
cls, = compile(<<-'EOF')
|
|
35
|
+
def set(b:Object)
|
|
36
|
+
map = { }
|
|
37
|
+
map["key"] = b
|
|
38
|
+
map["key"]
|
|
39
|
+
end
|
|
40
|
+
EOF
|
|
41
|
+
|
|
42
|
+
assert_equal("foo", cls.set("foo"))
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_hash_with_value_from_static_method
|
|
46
|
+
cls, = compile(<<-EOF)
|
|
47
|
+
def foo1
|
|
48
|
+
{a: a, b:"B"}
|
|
49
|
+
end
|
|
50
|
+
def a
|
|
51
|
+
return "A"
|
|
52
|
+
end
|
|
53
|
+
EOF
|
|
54
|
+
assert_equal("A", cls.foo1["a"])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_hash_with_value_from_instance_method
|
|
58
|
+
cls, = compile(<<-EOF)
|
|
59
|
+
class HashTesty
|
|
60
|
+
def foo1
|
|
61
|
+
{a: a, b:"B"}
|
|
62
|
+
end
|
|
63
|
+
def a
|
|
64
|
+
return "A"
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
EOF
|
|
68
|
+
assert_equal("A", cls.new.foo1["a"])
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def test_scoped_self_through_method_call
|
|
72
|
+
cls, = compile(<<-EOF)
|
|
73
|
+
class ScopedSelfThroughMethodCall
|
|
74
|
+
def emptyMap
|
|
75
|
+
{}
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def foo
|
|
79
|
+
emptyMap["a"] = "A"
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
EOF
|
|
83
|
+
|
|
84
|
+
# just make sure it can execute
|
|
85
|
+
m = cls.new.foo
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def test_self_call_preserves_scope
|
|
89
|
+
cls, = compile(<<-EOF)
|
|
90
|
+
class SelfCallPreservesScope
|
|
91
|
+
def key
|
|
92
|
+
"key"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def foo
|
|
96
|
+
map = {}
|
|
97
|
+
map[key] = "value"
|
|
98
|
+
map
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
EOF
|
|
102
|
+
|
|
103
|
+
map = cls.new.foo
|
|
104
|
+
assert_equal("value", map["key"])
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Copyright (c) 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 ImportTest < Test::Unit::TestCase
|
|
17
|
+
|
|
18
|
+
def test_quote_import
|
|
19
|
+
cls, = compile("import 'java.util.ArrayList'; def foo; ArrayList.new; end; foo")
|
|
20
|
+
assert_equal java.util.ArrayList.java_class, cls.foo.java_class
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_no_quote_import
|
|
24
|
+
cls, = compile("import java.util.ArrayList; def foo; ArrayList.new; end; foo")
|
|
25
|
+
assert_equal java.util.ArrayList.java_class, cls.foo.java_class
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_alias_import_as_syntax
|
|
29
|
+
cls, = compile("import java.util.ArrayList as AL; def foo; AL.new; end; foo")
|
|
30
|
+
assert_equal java.util.ArrayList.java_class, cls.foo.java_class
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def test_alias_import_comma_syntax
|
|
34
|
+
cls, = compile("import 'AL', 'java.util.ArrayList'; def foo; AL.new; end; foo")
|
|
35
|
+
assert_equal java.util.ArrayList.java_class, cls.foo.java_class
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test_imported_decl
|
|
39
|
+
cls, = compile("import 'java.util.ArrayList'; def foo(a:ArrayList); a.size; end")
|
|
40
|
+
assert_equal 0, cls.foo(java.util.ArrayList.new)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_import_package_star
|
|
44
|
+
cls, = compile(<<-EOF)
|
|
45
|
+
import java.util.*
|
|
46
|
+
def foo
|
|
47
|
+
ArrayList.new
|
|
48
|
+
end
|
|
49
|
+
EOF
|
|
50
|
+
assert_equal java.util.ArrayList.java_class, cls.foo.java_class
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_static_import
|
|
54
|
+
cls, = compile(<<-EOF)
|
|
55
|
+
import static java.util.Arrays.*
|
|
56
|
+
def list(x:Object[])
|
|
57
|
+
asList(x)
|
|
58
|
+
end
|
|
59
|
+
EOF
|
|
60
|
+
|
|
61
|
+
o = ["1", "2", "3"].to_java(:object)
|
|
62
|
+
list = cls.list(o)
|
|
63
|
+
assert_kind_of(Java::JavaUtil::List, list)
|
|
64
|
+
assert_equal(["1", "2", "3"], list.to_a)
|
|
65
|
+
|
|
66
|
+
cls, = compile(<<-EOF)
|
|
67
|
+
import java.util.Arrays
|
|
68
|
+
class StaticImports
|
|
69
|
+
import static Arrays.*
|
|
70
|
+
def list(x:Object[])
|
|
71
|
+
asList(x)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
EOF
|
|
75
|
+
|
|
76
|
+
list = cls.new.list(o)
|
|
77
|
+
assert_kind_of(Java::JavaUtil::List, list)
|
|
78
|
+
assert_equal(["1", "2", "3"], list.to_a)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
class InterfaceTest < Test::Unit::TestCase
|
|
17
|
+
def test_interface
|
|
18
|
+
cls, = compile(<<-EOF)
|
|
19
|
+
import 'java.util.concurrent.Callable'
|
|
20
|
+
def foo(a:Callable)
|
|
21
|
+
a.call
|
|
22
|
+
end
|
|
23
|
+
EOF
|
|
24
|
+
result = cls.foo {0}
|
|
25
|
+
assert_equal 0, result
|
|
26
|
+
m = cls.java_class.java_method 'foo', java.util.concurrent.Callable
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_interface_declaration
|
|
31
|
+
interface = compile('interface A; end').first
|
|
32
|
+
assert(interface.java_class.interface?)
|
|
33
|
+
assert_equal('A', interface.java_class.name)
|
|
34
|
+
|
|
35
|
+
a, b = compile('interface A; end; interface B < A; end')
|
|
36
|
+
assert_include(a, b.ancestors)
|
|
37
|
+
assert_equal('A', a.java_class.name)
|
|
38
|
+
assert_equal('B', b.java_class.name)
|
|
39
|
+
|
|
40
|
+
a, b, c = compile(<<-EOF)
|
|
41
|
+
interface A
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
interface B
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
interface C < A, B
|
|
48
|
+
end
|
|
49
|
+
EOF
|
|
50
|
+
|
|
51
|
+
assert_include(a, c.ancestors)
|
|
52
|
+
assert_include(b, c.ancestors)
|
|
53
|
+
assert_equal('A', a.java_class.name)
|
|
54
|
+
assert_equal('B', b.java_class.name)
|
|
55
|
+
assert_equal('C', c.java_class.name)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_interface_override_return_type
|
|
59
|
+
assert_raise_kind_of Mirah::MirahError do
|
|
60
|
+
compile(<<-EOF)
|
|
61
|
+
interface AnInterface
|
|
62
|
+
def a:int; end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class AImpl implements AnInterface
|
|
66
|
+
def a
|
|
67
|
+
"foo"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
EOF
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
data/test/jvm/java_typer_test.rb
CHANGED
|
@@ -18,96 +18,118 @@ require 'mirah'
|
|
|
18
18
|
|
|
19
19
|
class JavaTyperTest < Test::Unit::TestCase
|
|
20
20
|
include Mirah
|
|
21
|
+
include Mirah::Util::ProcessErrors
|
|
22
|
+
|
|
23
|
+
java_import 'org.mirah.typer.simple.SimpleScoper'
|
|
24
|
+
java_import 'org.mirah.typer.BaseTypeFuture'
|
|
25
|
+
java_import 'org.mirah.typer.CallFuture'
|
|
26
|
+
java_import 'org.mirah.typer.TypeFuture'
|
|
21
27
|
|
|
22
28
|
def setup
|
|
23
|
-
|
|
24
|
-
@
|
|
25
|
-
|
|
29
|
+
@types = Mirah::JVM::Types::TypeFactory.new
|
|
30
|
+
@scopes = SimpleScoper.new {|scoper, node| Mirah::AST::StaticScope.new(node, scoper)}
|
|
31
|
+
@typer = Mirah::Typer::Typer.new(@types, @scopes, nil)
|
|
32
|
+
@mirah = Mirah::Transform::Transformer.new(Mirah::Util::CompilationState.new, @typer)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def inferred_type(node)
|
|
36
|
+
type = @typer.infer(node, false).resolve
|
|
37
|
+
if type.name == ':error'
|
|
38
|
+
catch(:exit) do
|
|
39
|
+
process_errors([type])
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
type
|
|
43
|
+
end
|
|
26
44
|
|
|
27
|
-
|
|
45
|
+
def method_type(target, name, args)
|
|
46
|
+
target = @types.cache_and_wrap(target)
|
|
47
|
+
args = args.map {|arg| @types.cache_and_wrap(arg) }
|
|
48
|
+
call = CallFuture.new(@types, nil, target, name, args, nil, nil)
|
|
49
|
+
@types.getMethodType(call).return_type
|
|
28
50
|
end
|
|
29
51
|
|
|
30
|
-
def
|
|
31
|
-
AST.
|
|
52
|
+
def parse(text)
|
|
53
|
+
AST.parse(text, '-', false, @mirah)
|
|
32
54
|
end
|
|
33
55
|
|
|
34
56
|
def test_simple_overtyped_meta_method
|
|
35
|
-
string_meta =
|
|
36
|
-
string =
|
|
57
|
+
string_meta = @types.type(nil, 'java.lang.String', false, true)
|
|
58
|
+
string = @types.type(nil, 'java.lang.String')
|
|
37
59
|
|
|
38
60
|
# integral types
|
|
39
61
|
['boolean', 'char', 'double', 'float', 'int', 'long'].each do |type_name|
|
|
40
|
-
type =
|
|
41
|
-
return_type =
|
|
42
|
-
assert_equal(string, return_type, "valueOf(#{type}) should return #{string}")
|
|
62
|
+
type = @types.type(nil, type_name)
|
|
63
|
+
return_type = method_type(string_meta, 'valueOf', [type])
|
|
64
|
+
assert_equal(string, return_type.resolve, "valueOf(#{type}) should return #{string}")
|
|
43
65
|
end
|
|
44
66
|
|
|
45
67
|
# char[]
|
|
46
|
-
type =
|
|
47
|
-
return_type =
|
|
48
|
-
assert_equal(string, return_type)
|
|
68
|
+
type = @types.type(nil, 'char', true)
|
|
69
|
+
return_type = method_type(string_meta, 'valueOf', [type])
|
|
70
|
+
assert_equal(string, return_type.resolve)
|
|
49
71
|
|
|
50
72
|
# Object
|
|
51
|
-
type =
|
|
52
|
-
return_type =
|
|
53
|
-
assert_equal(string, return_type)
|
|
73
|
+
type = @types.type(nil, 'java.lang.Object')
|
|
74
|
+
return_type = method_type(string_meta, 'valueOf', [type])
|
|
75
|
+
assert_equal(string, return_type.resolve)
|
|
54
76
|
end
|
|
55
77
|
|
|
56
78
|
def test_non_overtyped_method
|
|
57
|
-
string =
|
|
79
|
+
string = @types.type(nil, 'java.lang.String')
|
|
58
80
|
|
|
59
|
-
int =
|
|
60
|
-
return_type =
|
|
61
|
-
assert_equal(int, return_type)
|
|
81
|
+
int = @types.type(nil, 'int')
|
|
82
|
+
return_type = method_type(string, 'length', [])
|
|
83
|
+
assert_equal(int, return_type.resolve)
|
|
62
84
|
|
|
63
|
-
byte_array =
|
|
64
|
-
return_type =
|
|
65
|
-
assert_equal(byte_array, return_type)
|
|
85
|
+
byte_array = @types.type(nil, 'byte', true)
|
|
86
|
+
return_type = method_type(string, 'getBytes', [])
|
|
87
|
+
assert_equal(byte_array, return_type.resolve)
|
|
66
88
|
end
|
|
67
89
|
|
|
68
90
|
def test_simple_overtyped_method
|
|
69
|
-
string_meta =
|
|
70
|
-
string =
|
|
91
|
+
string_meta = @types.type(nil, 'java.lang.String', false, true)
|
|
92
|
+
string = @types.type(nil, 'java.lang.String')
|
|
71
93
|
|
|
72
|
-
return_type =
|
|
73
|
-
assert_equal(string, return_type)
|
|
94
|
+
return_type = method_type(string_meta, 'valueOf', [string])
|
|
95
|
+
assert_equal(string, return_type.resolve)
|
|
74
96
|
end
|
|
75
97
|
|
|
76
98
|
def test_primitive_conversion_method
|
|
77
|
-
string =
|
|
78
|
-
byte =
|
|
79
|
-
char =
|
|
80
|
-
long =
|
|
99
|
+
string = @types.type(nil, 'java.lang.String')
|
|
100
|
+
byte = @types.type(nil, 'byte')
|
|
101
|
+
char = @types.type(nil, 'char')
|
|
102
|
+
long = @types.type(nil, 'long')
|
|
81
103
|
|
|
82
|
-
return_type =
|
|
83
|
-
|
|
104
|
+
return_type = method_type(string, 'charAt', [byte])
|
|
105
|
+
assert_kind_of(TypeFuture, return_type)
|
|
106
|
+
assert_equal(char, return_type.resolve)
|
|
84
107
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
end
|
|
108
|
+
return_type = method_type(string, 'charAt', [long]).resolve
|
|
109
|
+
assert(return_type.isError)
|
|
88
110
|
end
|
|
89
111
|
|
|
90
112
|
include Mirah::JVM::MethodLookup
|
|
91
113
|
|
|
92
114
|
def test_is_more_specific
|
|
93
|
-
object = java.lang.Object
|
|
94
|
-
charseq = java.lang.CharSequence
|
|
95
|
-
string = java.lang.String
|
|
115
|
+
object = @types.type(nil, 'java.lang.Object')
|
|
116
|
+
charseq = @types.type(nil, 'java.lang.CharSequence')
|
|
117
|
+
string = @types.type(nil, 'java.lang.String')
|
|
96
118
|
|
|
97
|
-
assert
|
|
98
|
-
assert
|
|
99
|
-
assert
|
|
119
|
+
assert object.is_more_specific?([string], [object])
|
|
120
|
+
assert object.is_more_specific?([charseq], [object])
|
|
121
|
+
assert object.is_more_specific?([string], [charseq])
|
|
100
122
|
end
|
|
101
123
|
|
|
102
124
|
def test_primitive_convertible
|
|
103
|
-
boolean =
|
|
104
|
-
byte =
|
|
105
|
-
short =
|
|
106
|
-
char =
|
|
107
|
-
int =
|
|
108
|
-
long =
|
|
109
|
-
float =
|
|
110
|
-
double =
|
|
125
|
+
boolean = @types.type(nil, 'boolean')
|
|
126
|
+
byte = @types.type(nil, 'byte')
|
|
127
|
+
short = @types.type(nil, 'short')
|
|
128
|
+
char = @types.type(nil, 'char')
|
|
129
|
+
int = @types.type(nil, 'int')
|
|
130
|
+
long = @types.type(nil, 'long')
|
|
131
|
+
float = @types.type(nil, 'float')
|
|
132
|
+
double = @types.type(nil, 'double')
|
|
111
133
|
|
|
112
134
|
assert !primitive_convertible?(boolean, byte)
|
|
113
135
|
assert !primitive_convertible?(boolean, short)
|
|
@@ -183,29 +205,33 @@ class JavaTyperTest < Test::Unit::TestCase
|
|
|
183
205
|
end
|
|
184
206
|
|
|
185
207
|
def test_primitive_array
|
|
186
|
-
ary =
|
|
187
|
-
int =
|
|
208
|
+
ary = @types.type(nil, 'byte', true)
|
|
209
|
+
int = @types.type(nil, 'int')
|
|
210
|
+
|
|
211
|
+
# TODO fix intrinsics
|
|
212
|
+
assert_equal(@types.type(nil, 'byte'), method_type(ary, "[]", [int]).resolve)
|
|
213
|
+
end
|
|
188
214
|
|
|
189
|
-
java_typer = Typer::JavaTyper.new
|
|
190
215
|
|
|
191
|
-
|
|
216
|
+
def test_primitive_not_convertible_to_array_with_same_component_type
|
|
217
|
+
ary = @types.type(nil, 'byte', true)
|
|
218
|
+
byte = @types.type(nil, 'byte')
|
|
219
|
+
|
|
220
|
+
assert !primitive_convertible?(byte, ary)
|
|
192
221
|
end
|
|
193
222
|
|
|
194
223
|
def test_int
|
|
195
|
-
ast =
|
|
196
|
-
assert_equal(
|
|
224
|
+
ast = parse("#{1 << 16}")
|
|
225
|
+
assert_equal(@types.type(nil, 'int'), inferred_type(ast))
|
|
197
226
|
end
|
|
198
227
|
|
|
199
228
|
def test_long
|
|
200
|
-
ast =
|
|
201
|
-
assert_equal(
|
|
229
|
+
ast = parse("#{1 << 33}")
|
|
230
|
+
assert_equal(@types.type(nil, 'long'), inferred_type(ast))
|
|
202
231
|
end
|
|
203
|
-
|
|
204
|
-
def
|
|
205
|
-
ast =
|
|
206
|
-
assert_equal
|
|
207
|
-
|
|
208
|
-
ast = AST.parse("a = Object.new; a = dynamic('foo')")
|
|
209
|
-
assert_equal 'java.lang.Object', ast.infer(@typer, true).name
|
|
232
|
+
|
|
233
|
+
def test_static_method
|
|
234
|
+
ast = parse("class Foo;def self.bar; 1; end; end; Foo.bar")
|
|
235
|
+
assert_equal(@types.type(nil, 'int'), inferred_type(ast))
|
|
210
236
|
end
|
|
211
237
|
end
|