1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 # Copyright 2014 Romain Chanoir <romain.chanoir@courrier.uqam.ca>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Manipulats the Java Virtual Machine
20 # See: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html
23 print
"Compilation des classes Java ..."
24 assert sys
.system
("javac test_jvm/Queue.java") == 0
25 assert sys
.system
("javac test_jvm/Test2.java") == 0
26 assert sys
.system
("javac test_jvm/TestJvm.java") == 0
28 print
"Initialisation de la JVM ..."
30 var builder
= new JavaVMBuilder
31 builder
.options
.add
"-Djava.class.path=."
32 var jvm
= builder
.create_jvm
33 var env
= builder
.jni_env
36 print
"---------------------Test 1----------------------"
38 var queue_c
= env
.find_class
("test_jvm/Queue")
39 if queue_c
.address_is_null
then print
("Error, Queue class not found")
41 # get the methods of the class
42 var f_init
= env
.get_method_id
(queue_c
, "<init>", "()V")
43 if f_init
.address_is_null
then print
("Error, fInit not found")
45 var f_push
= env
.get_method_id
(queue_c
, "push", "(Ljava/lang/String;)V")
46 if f_push
.address_is_null
then print
("Error, fPush not found")
48 var f_pop
= env
.get_method_id
(queue_c
, "pop", "()Ljava/lang/String;")
49 if f_pop
.address_is_null
then print
("Error, fPop not found")
51 var element1
= "premier"
52 var element2
= "deuxième"
53 var element3
= "troisième"
56 var queue
= env
.new_object
(queue_c
, f_init
)
57 if queue
.address_is_null
then print
("Error, object not initialized")
59 var arg_tab
= new Array[Object].with_items
(element1
)
61 # first call to push method
62 env
.call_void_method
(queue
, f_push
, arg_tab
)
63 arg_tab
= new Array[Object].with_items
(element2
)
64 env
.call_void_method
(queue
, f_push
, arg_tab
)
65 arg_tab
= new Array[Object].with_items
(element3
)
66 env
.call_void_method
(queue
, f_push
, arg_tab
)
68 var el
= env
.call_string_method
(queue
, f_pop
, null)
70 el
= env
.call_string_method
(queue
, f_pop
, null)
72 el
= env
.call_string_method
(queue
, f_pop
, null)
75 print
"--------------------Test 2---------------------"
77 var test_c
= env
.find_class
("test_jvm/TestJvm")
78 if test_c
.address_is_null
then print
("Error, TestJvm class not found")
80 f_init
= env
.get_method_id
(test_c
, "<init>", "()V")
81 if f_init
.address_is_null
then print
("Error, finit not found")
83 # Get the different methods ids
84 var m_bool
= env
.get_method_id
(test_c
, "isBool", "()Z")
85 if m_bool
.address_is_null
then print
("Error, mbool not found")
86 var m_char
= env
.get_method_id
(test_c
, "getC", "()C")
87 if m_char
.address_is_null
then print
("Error, mchar not found")
88 var m_i
= env
.get_method_id
(test_c
, "getI", "()I")
89 if m_i
.address_is_null
then print
("Error, mi not found")
90 var m_f
= env
.get_method_id
(test_c
, "getF", "()F")
91 if m_f
.address_is_null
then print
("Error, mf not found")
92 var m_test
= env
.get_method_id
(test_c
, "getTest", "()Ltest_jvm/Test2;")
93 if m_test
.address_is_null
then print
("Error, mtest not found")
96 var f_bool
=env
.get_field_id
(test_c
, "bool", "Z")
97 if f_bool
.address_is_null
then print
("Error, fbool not found")
98 var f_char
= env
.get_field_id
(test_c
, "c", "C")
99 if f_char
.address_is_null
then print
("Error, fchar not found")
100 var f_i
=env
.get_field_id
(test_c
, "i", "I")
101 if f_i
.address_is_null
then print
("Error, fi not found")
102 var f_f
= env
.get_field_id
(test_c
, "f", "F")
103 if f_f
.address_is_null
then print
("Error, ff not found")
104 var f_test
= env
.get_field_id
(test_c
, "test", "Ltest_jvm/TestJvm;")
107 var test
= env
.new_object
(test_c
, f_init
)
108 if test
.address_is_null
then print
("Error, object test not initialized")
110 # Retrieve field value with field ids
111 var v_bool
= env
.get_boolean_field
(test
, f_bool
)
112 if v_bool
== null then print
("Error, vbool not found")
113 var v_char
= env
.get_char_field
(test
, f_char
)
114 if v_char
== null then print
("Error, vchar not found")
115 var v_i
= env
.get_int_field
(test
, f_i
)
116 if v_i
== null then print
("Error, vi not found")
117 var v_f
= env
.get_float_field
(test
, f_f
)
118 if v_f
== null then print
("Error, vf not found")
119 var v_test1
= env
.get_object_field
(test
, f_test
)
120 if v_test1
== null then print
("Error, vtest1 not found")
122 # Set the new values for the fields
123 env
.set_boolean_field
(test
, f_bool
, true)
124 env
.set_char_field
(test
, f_char
, 'D')
125 env
.set_int_field
(test
, f_i
, 10)
126 env
.set_float_field
(test
, f_f
, 15.5)
127 env
.set_object_field
(test
, f_test
, test
)
129 # get the values using getter method
130 v_bool
= env
.call_boolean_method
(test
, m_bool
, null)
131 v_char
= env
.call_char_method
(test
, m_char
, null)
132 v_i
= env
.call_int_method
(test
, m_i
, null)
133 v_f
= env
.call_float_method
(test
, m_f
, null)
134 var v_test2
= env
.call_object_method
(test
, m_test
, null)
135 if v_test2
== null then print
("Error, vtest2 not found")
137 # assert the values of the fields