1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Romain Chanoir <romain.chanoir@courrier.uqam.ca>
4 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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 # Manipulates the Java Virtual Machine
20 # See: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html
22 c_compiler_option
("-I $(JAVA_HOME)/include/")
23 c_linker_option
("-L $(JNI_LIB_PATH) -ljvm")
30 # Utility to select options to create the VM using `create_jvm`
34 # var builder = new JavaVMBuilder
35 # builder.options.add "-Djava.class.path=."
36 # var jvm = builder.create_jvm
37 # var env = builder.jni_env
42 var version
: Int = "00010002".to_hex
43 var options
= new Array[String]
45 fun create_jvm
: nullable JavaVM
47 var args
= new JavaVMInitArgs
48 args
.version
= version
50 args
.n_options
= options
.length
52 var c_options
= new JavaVMOptionArray(options
.length
)
53 for o
in options
.length
.times
do
54 var option
= options
[o
]
55 var c_option
= c_options
[o
]
56 c_option
.string
= option
59 args
.options
= c_options
61 var jvm
= new JavaVM(args
, self)
66 if jvm
.address_is_null
then return null
71 extern class JavaVMInitArgs `{ JavaVMInitArgs* `}
72 new `{ return (JavaVMInitArgs*)malloc(sizeof(JavaVMInitArgs)); `}
74 # Set the defaut config for a VM
75 # Can be called after setting the version
76 fun set_default
`{ JNI_GetDefaultJavaVMInitArgs(recv); `}
78 fun version: Int `{ return recv->version; `}
79 fun version
=(v
: Int) `{ recv->version = v; `}
81 fun options: JavaVMOptionArray `{ return recv->options; `}
82 fun options
=(v
: JavaVMOptionArray) `{ recv->options = v; `}
84 fun n_options: Int `{ return recv->nOptions; `}
85 fun n_options
=(v
: Int) `{ recv->nOptions = v; `}
88 extern class JavaVMOption `{ JavaVMOption* `}
89 fun string
: String import NativeString.to_s
`{
90 return NativeString_to_s(recv->optionString);
92 fun string
=(v
: String) import String.to_cstring
`{
93 recv->optionString = String_to_cstring(v);
96 fun extra_info
: String import NativeString.to_s
`{
97 return NativeString_to_s(recv->extraInfo);
99 fun extra_info
=(v
: String) import String.to_cstring
`{
100 recv->extraInfo = String_to_cstring(v);
104 extern class JavaVMOptionArray `{ JavaVMOption* `}
105 new(size: Int) `{ return (JavaVMOption*)malloc(sizeof(JavaVMOption)*size); `}
107 fun [](i
: Int): JavaVMOption `{ return recv+i; `}
110 # Represents a jni JavaVM
111 extern class JavaVM `{JavaVM *`}
112 # Create the JVM, returns its handle and store the a pointer to JniEnv in `env_ref`
113 new(args
: JavaVMInitArgs, env_ref
: JniEnvRef) import jni_error
, JniEnvRef.jni_env
=, JniEnv.as nullable `{
118 res = JNI_CreateJavaVM(&jvm, (void**)&env, args);
121 JavaVM_jni_error(NULL, "Could not create Java VM", res);
125 JniEnvRef_jni_env__assign(env_ref, JniEnv_as_nullable_JniEnv(env));
130 fun jni_error
(msg
: NativeString, v
: Int)
132 print
"JNI Error: {msg} ({v})"
137 (*recv)->DestroyJavaVM(recv);
140 fun env
: JniEnv import jni_error
`{
142 int res = (*recv)->GetEnv(recv, (void **)&env, JNI_VERSION_1_6);
144 JavaVM_jni_error(NULL, "Could not get JNIEnv from Java VM", res);
150 fun attach_current_thread
: JniEnv `{
152 int res = (*recv)->AttachCurrentThread(recv, (void**)&env, NULL);
154 JavaVM_jni_error(NULL, "Could not attach current thread to Java VM", res);
160 # Represents a jni JNIEnv, which is a thread in a JavaVM
161 extern class JniEnv `{JNIEnv *`}
163 # Get a class object from its fully-qualified name or null if the class cannot be found
164 fun find_class(class_name : String): JClass import String.to_cstring `{
165 return (*recv
)->FindClass(recv
,String_to_cstring(class_name
));
168 # Return the method id for an instance of a class or interface
169 # The method is determined by its name and signature
170 # To obtain the method ID of a constructor, supply "<init>" as the method name and "void(V)" as the return type
171 fun get_method_id(clazz : JClass, name : String, signature : String): JMethodID import String.to_cstring `{
172 return (*recv
)->GetMethodID(recv
, clazz
, String_to_cstring(name
), String_to_cstring(signature
));
175 # Construct a new Java object from the `clazz
`, using the constructor ̀ method_id`
176 fun new_object
(clazz
: JClass, method_id
: JMethodID): JavaObject `{
177 return (*recv)->NewObject(recv, clazz, method_id);
180 # Return the JClass of `obj`
181 fun get_object_class
(obj
: JavaObject): JClass `{
182 return (*recv)->GetObjectClass(recv, obj);
185 # Registers native methods with the class specified by the `clazz` argument
186 fun register_natives
(clazz
: JClass, method
: JNINativeMethod, n_method
: Int): Int `{
187 return (*recv)->RegisterNatives(recv, clazz, method, n_method);
190 # Call a method on `obj` designed by `method_id` with an array `args` of arguments
191 fun call_void_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]) import convert_args_to_jni
`{
192 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
193 (*recv)->CallVoidMethodA(recv, obj, method_id, args_tab);
197 # Call a method on `obj` designed by `method_id` with an array `args` of argument returning a JavaObject
198 fun call_object_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): JavaObject import convert_args_to_jni
`{
199 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
200 (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
204 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Bool
205 fun call_boolean_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Bool import convert_args_to_jni
`{
206 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
207 return (*recv)->CallBooleanMethod(recv, obj, method_id, args_tab);
211 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Char
212 fun call_char_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Char import convert_args_to_jni
`{
213 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
214 return (*recv)->CallCharMethod(recv, obj, method_id, args_tab);
218 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning an Int
219 fun call_int_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Int import convert_args_to_jni
`{
220 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
221 return (*recv)->CallIntMethod(recv, obj, method_id, args_tab);
225 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Float
226 fun call_float_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Float import convert_args_to_jni
`{
227 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
228 return (*recv)->CallFloatMethod(recv, obj, method_id, args_tab);
232 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a NativeString
233 fun call_string_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): NativeString import convert_args_to_jni
`{
234 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
235 jobject jobj = (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
237 return (char*)(*recv)->GetStringUTFChars(recv, (jstring)jobj, NULL);
240 private fun convert_args_to_jni
(args
: nullable Array[nullable Object]): Pointer import Array[nullable Object].as not nullable, Array[nullable Object].[], Array[nullable Object].length
, nullable Object.as(Int), nullable Object.as(Char), nullable Object.as(Bool), nullable Object.as(Float), nullable Object.as(JavaObject), nullable Object.as(String), String.to_cstring
, String.length
`{
241 if(nullable_Array_of_nullable_Object_is_null(args)){
244 Array_of_nullable_Object nit_array = nullable_Array_of_nullable_Object_as_Array_of_nullable_Object(args);
245 int nit_array_length = Array_of_nullable_Object_length(nit_array);
247 jvalue *c_array = malloc(sizeof(jvalue)*(nit_array_length));
248 for (i = 0; i < nit_array_length; i ++) {
249 nullable_Object nullable_obj = Array_of_nullable_Object__index(nit_array, i);
250 if(nullable_Object_is_a_Int(nullable_obj)) {
251 int val = nullable_Object_as_Int(nullable_obj);
253 } else if (nullable_Object_is_a_Char(nullable_obj)){
254 char val = nullable_Object_as_Char(nullable_obj);
256 } else if (nullable_Object_is_a_Bool(nullable_obj)){
257 int val = nullable_Object_as_Bool(nullable_obj);
259 } else if(nullable_Object_is_a_Float(nullable_obj)){
260 float val = nullable_Object_as_Float(nullable_obj);
262 } else if(nullable_Object_is_a_JavaObject(nullable_obj)){
263 jobject val = nullable_Object_as_JavaObject(nullable_obj);
265 } else if(nullable_Object_is_a_String(nullable_obj)){
266 String val = nullable_Object_as_String(nullable_obj);
267 char* c = String_to_cstring(val);
268 jstring js = (*recv)->NewStringUTF(recv, c);
271 fprintf(stderr, "NOT YET SUPPORTED: nit objects are not supported\n");
278 # Returns the field ID for an instance field of a class. The field is specified by its name and signature
279 fun get_field_id
(clazz
: JClass, name
: String, sign
: String): JFieldID import String.to_cstring
`{
280 return (*recv)->GetFieldID(recv, clazz, String_to_cstring(name), String_to_cstring(sign));
283 # returns the value of an instance (nonstatic) field of an object. The field to access is specified by a field ID obtained by calling get_field_id()
284 fun get_object_field
(obj
: JavaObject, fieldID
: JFieldID): JavaObject `{
285 return (*recv)->GetObjectField(recv, obj, fieldID);
288 fun get_boolean_field
(obj
: JavaObject, fieldID
: JFieldID): Bool `{
289 return (*recv)->GetBooleanField(recv, obj, fieldID);
292 fun get_char_field
(obj
: JavaObject, fieldID
: JFieldID): Char `{
293 return (*recv)->GetCharField(recv, obj, fieldID);
296 fun get_int_field
(obj
: JavaObject, fieldID
: JFieldID): Int `{
297 return (*recv)->GetIntField(recv, obj, fieldID);
300 fun get_float_field
(obj
: JavaObject, fieldID
: JFieldID): Float `{
301 return (*recv)->GetFloatField(recv, obj, fieldID);
304 fun set_object_field
(obj
: JavaObject, fieldID
: JFieldID, value
: JavaObject) `{
305 (*recv)->SetObjectField(recv, obj, fieldID, value);
308 fun set_boolean_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Bool) `{
309 (*recv)->SetBooleanField(recv, obj, fieldID, value);
312 fun set_char_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Char) `{
313 (*recv)->SetCharField(recv, obj, fieldID, value);
316 fun set_int_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Int) `{
317 (*recv)->SetIntField(recv, obj, fieldID, value);
320 fun set_float_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Float) `{
321 (*recv)->SetFloatField(recv, obj, fieldID, value);
324 # Check for pending exception without creating a local reference to the exception object
325 fun exception_check
: Bool `{
326 return (*recv)->ExceptionCheck(recv);
329 # Construct an exception object from the specified class with the message specified by `message` and causes that exception to be thrown
330 fun throw_new
(clazz
: JClass, message
: String): Int import String.to_cstring
`{
331 return (*recv)->ThrowNew(recv, clazz, String_to_cstring(message));
334 # return the exception if there is one in the process of being thrown, or NULL if no exception is currently being thrown
335 fun exception_occurred
: JavaObject `{
336 return (*recv)->ExceptionOccurred(recv);
339 # prints an exception and backtrace to error channel
340 fun exception_describe
`{
341 return (*recv)->ExceptionDescribe(recv);
344 # clears any exception currently being thrown, has no effect if there is no exception
345 fun exception_clear
`{
346 return (*recv)->ExceptionClear(recv);
349 # Raise a fatal error
350 fun fatal_error
(msg
: String) import String.to_cstring
`{
351 (*recv)->FatalError(recv, String_to_cstring(msg));
354 # Transform a NIT String into a JavaObject
355 fun string_to_jobject
(string
: String): JavaObject `{
356 return (*recv)->NewStringUTF(recv, String_to_cstring(string));
360 # used to initialize a JavaVM
362 var jni_env
: nullable JniEnv = null
365 # Represents a jni jclass
366 extern class JClass `{jclass`}
369 # Represents a jni jmethodID
370 extern class JMethodID `{jmethodID`}
373 # Represens a jni jobject
374 extern class JavaObject `{jobject`}
377 # Represents a jni JNINNativeMethod
378 extern class JNINativeMethod `{ JNINativeMethod* `}
379 fun name
: String import NativeString.to_s
`{
380 return NativeString_to_s(recv->name);
383 fun name
=(name
: String) import String.to_cstring
`{
384 recv->name = String_to_cstring(name);
387 fun signature
: String import NativeString.to_s
`{
388 return NativeString_to_s(recv->signature);
391 fun signature
=(signature
: String) import String.to_cstring
`{
392 recv->signature = String_to_cstring(signature);
396 # Represents a jni jfieldID
397 extern class JFieldID `{jfieldID`}
400 # Reprents a jni jvalue
401 extern class JValue `{jvalue`}
403 fun set_boolean
(b
: Bool) `{
407 fun get_boolean
:Bool `{
411 fun set_char
(c
: Char)`{
415 fun get_char
: Char `{
419 fun set_int
(i
: Int) `{
427 fun set_float
(f
: Float) `{
431 fun get_float
: Float `{
435 fun set_jobject
(obj
: JavaObject) `{
439 fun get_jobject
: JavaObject `{
445 redef fun to_jvalue
(env
): JValue `{
453 redef fun to_jvalue
(env
): JValue `{
461 redef fun to_jvalue
(env
): JValue `{
468 redef class NativeString
469 redef fun to_jvalue
(env
)`{
471 value.l = (*env)->NewStringUTF(env, recv);
477 redef fun to_jvalue
(env
) do
478 return self.to_cstring
.to_jvalue
(env
)
483 fun to_jvalue
(env
: JniEnv): JValue is abstract