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 # Java Virtual Machine invocation API and others services from the JNI C API
20 # Users of this module and the Java FFI, on desktop computers, must define three environment variables:
21 # * `JAVA_HOME` points to the installation folder of the target Java VM.
22 # This folder should contain the JNI header file `include/jni.h`.
23 # e.g. `/usr/lib/jvm/default-java` on Debian Jessie.
24 # * `JNI_LIB_PATH` points to the folder with `libjvm.so`.
25 # e.g. `/usr/lib/jvm/default-java/jre/lib/amd64/server/` on Debian Jessie.
26 # * `LD_LIBRARY_PATH` has the path to the folder with `libjvm.so`.
27 # It's the same value as `JNI_LIB_PATH` but `LD_LIBRARY_PATH` is a colon separated list
28 # which may contain other paths.
30 # See: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html
32 cflags
"-I $(JAVA_HOME)/include/ -I $(JAVA_HOME)/include/linux/"
33 ldflags
"-L $(JNI_LIB_PATH) -ljvm"
40 # Utility to select options to create the VM using `create_jvm`
45 # var builder = new JavaVMBuilder
46 # builder.options.add "-Djava.class.path=."
47 # var jvm = builder.create_jvm
48 # var env = builder.jni_env
52 # Version code of the JVM requested by `create_jvm`
54 # Default at 0x00010002 for `JNI_VERSION_1_2`.
55 var version
= 0x00010002 is writable
57 # Additional option strings
58 var options
= new Array[String]
60 # Create a JVM instance, or return `null` on error
61 fun create_jvm
: nullable JavaVM
63 var args
= new JavaVMInitArgs
64 args
.version
= version
66 args
.n_options
= options
.length
68 var c_options
= new JavaVMOptionArray(options
.length
)
69 for o
in options
.length
.times
do
70 var option
= options
[o
]
71 var c_option
= c_options
[o
]
72 c_option
.string
= option
75 args
.options
= c_options
77 var jvm
= new JavaVM(args
)
82 if jvm
.address_is_null
then return null
87 private extern class JavaVMInitArgs `{ JavaVMInitArgs* `}
88 new `{ return (JavaVMInitArgs*)malloc(sizeof(JavaVMInitArgs)); `}
90 # Set the defaut config for a VM
91 # Can be called after setting the version
93 # Unavailable on Android, where you cannot instanciate a new JVM.
96 JNI_GetDefaultJavaVMInitArgs(self);
100 fun version
: Int `{ return self->version; `}
101 fun version=(v: Int) `{ self->version = v; `}
103 fun options
: JavaVMOptionArray `{ return self->options; `}
104 fun options=(v: JavaVMOptionArray) `{ self->options = v; `}
106 fun n_options
: Int `{ return self->nOptions; `}
107 fun n_options=(v: Int) `{ self->nOptions = v; `}
110 private extern class JavaVMOption `{ JavaVMOption* `}
111 fun string: String import CString.to_s `{
112 return CString_to_s((char
*)self-
>optionString
);
114 fun string=(v: String) import String.to_cstring `{
115 self-
>optionString
= String_to_cstring(v
);
118 fun extra_info: String import CString.to_s `{
119 return CString_to_s((char
*)self-
>extraInfo
);
121 fun extra_info=(v: String) import String.to_cstring `{
122 self-
>extraInfo
= String_to_cstring(v
);
126 private extern class JavaVMOptionArray `{ JavaVMOption* `}
127 new(size
: Int) `{ return (JavaVMOption*)malloc(sizeof(JavaVMOption)*size); `}
129 fun [](i: Int): JavaVMOption `{ return self+i; `}
132 # Represents a jni JavaVM
133 extern class JavaVM `{JavaVM *`}
136 # The corresponding `JniEnv` can be obtained by calling `env
`.
138 # Unavailable on some platforms, including Android where you cannot instanciate a new JVM.
139 private new(args: JavaVMInitArgs) import jni_error `{
142 JavaVM_jni_error(NULL, "JVM creation not supported on Android", 0);
150 res
= JNI_CreateJavaVM(&jvm
, (void
**)&env
, args
);
153 JavaVM_jni_error(NULL, "Could not create Java VM", res
);
160 private fun jni_error(msg: CString, v: Int)
162 print "JNI Error: {msg} ({v})"
166 # Unload the Java VM when the calling thread is the only remaining non-daemon attached user thread
168 (*self)->DestroyJavaVM(self);
171 # `JniEnv` attached to the calling thread
173 # A null pointer is returned if the calling thread is not attached to the JVM.
174 fun env: JniEnv import jni_error `{
176 int res
= (*self)->GetEnv(self, (void
**)&env
, JNI_VERSION_1_6);
177 if (res
== JNI_EDETACHED) {
178 JavaVM_jni_error(NULL, "Requesting JNIEnv from an unattached thread", res
);
181 else if (res
!= JNI_OK) {
182 JavaVM_jni_error(NULL, "Could not get JNIEnv from Java VM", res
);
188 # Attach the calling thread to the JVM and return its `JniEnv`
189 fun attach_current_thread: JniEnv import jni_error `{
192 // the signature
is different
(better actually
) on
Android
193 int res
= (*self)->AttachCurrentThread(self, &env
, NULL);
195 int res
= (*self)->AttachCurrentThread(self, (void
**)&env
, NULL);
198 JavaVM_jni_error(NULL, "Could not attach current thread to Java VM", res
);
204 # Detach the calling thread from this JVM
205 fun detach_current_thread import jni_error `{
206 int res
= (*self)->DetachCurrentThread(self);
208 JavaVM_jni_error(NULL, "Could not detach current thread to Java VM", res
);
213 # Represents a jni JNIEnv, which is a thread in a JavaVM
214 extern class JniEnv `{JNIEnv *`}
216 # Get a class object from its fully-qualified name or null if the class cannot be found
217 fun find_class
(class_name
: String): JClass import String.to_cstring
`{
218 return (*self)->FindClass(self,String_to_cstring(class_name));
221 # Return the method id for an instance of a class or interface
222 # The method is determined by its name and signature
223 # To obtain the method ID of a constructor, supply "<init>" as the method name and "void(V)" as the return type
224 fun get_method_id
(clazz
: JClass, name
: String, signature
: String): JMethodID import String.to_cstring
`{
225 return (*self)->GetMethodID(self, clazz, String_to_cstring(name), String_to_cstring(signature));
228 # Construct a new Java object from the `clazz`, using the constructor ̀ method_id`
229 fun new_object
(clazz
: JClass, method_id
: JMethodID): JavaObject `{
230 return (*self)->NewObject(self, clazz, method_id);
233 # Return the JClass of `obj`
234 fun get_object_class
(obj
: JavaObject): JClass `{
235 return (*self)->GetObjectClass(self, obj);
238 # Registers native methods with the class specified by the `clazz` argument
239 fun register_natives
(clazz
: JClass, method
: JNINativeMethod, n_method
: Int): Int `{
240 return (*self)->RegisterNatives(self, clazz, method, n_method);
243 # Call a method on `obj` designed by `method_id` with an array `args` of arguments
244 fun call_void_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]) import convert_args_to_jni
`{
245 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
246 (*self)->CallVoidMethodA(self, obj, method_id, args_tab);
250 # Call a method on `obj` designed by `method_id` with an array `args` of argument returning a JavaObject
251 fun call_object_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): JavaObject import convert_args_to_jni
`{
252 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
253 jobject res = (*self)->CallObjectMethod(self, obj, method_id, args_tab);
258 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Bool
259 fun call_boolean_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Bool import convert_args_to_jni
`{
260 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
261 jboolean res = (*self)->CallBooleanMethod(self, obj, method_id, args_tab);
266 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Char
267 fun call_char_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Char import convert_args_to_jni
`{
268 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
269 jchar res = (*self)->CallCharMethod(self, obj, method_id, args_tab);
274 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning an Int
275 fun call_int_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Int import convert_args_to_jni
`{
276 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
277 jint res = (*self)->CallIntMethod(self, obj, method_id, args_tab);
282 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Float
283 fun call_float_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Float import convert_args_to_jni
`{
284 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
285 jfloat res = (*self)->CallFloatMethod(self, obj, method_id, args_tab);
290 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a CString
291 fun call_string_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): CString import convert_args_to_jni
`{
292 jvalue * args_tab = JniEnv_convert_args_to_jni(self, args);
293 jobject jobj = (*self)->CallObjectMethod(self, obj, method_id, args_tab);
295 return (char*)(*self)->GetStringUTFChars(self, (jstring)jobj, NULL);
298 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
`{
299 if(nullable_Array_of_nullable_Object_is_null(args)){
302 Array_of_nullable_Object nit_array = nullable_Array_of_nullable_Object_as_Array_of_nullable_Object(args);
303 int nit_array_length = Array_of_nullable_Object_length(nit_array);
305 jvalue *c_array = malloc(sizeof(jvalue)*(nit_array_length));
306 for (i = 0; i < nit_array_length; i ++) {
307 nullable_Object nullable_obj = Array_of_nullable_Object__index(nit_array, i);
308 if(nullable_Object_is_a_Int(nullable_obj)) {
309 int val = nullable_Object_as_Int(nullable_obj);
311 } else if (nullable_Object_is_a_Char(nullable_obj)){
312 char val = nullable_Object_as_Char(nullable_obj);
314 } else if (nullable_Object_is_a_Bool(nullable_obj)){
315 int val = nullable_Object_as_Bool(nullable_obj);
317 } else if(nullable_Object_is_a_Float(nullable_obj)){
318 float val = nullable_Object_as_Float(nullable_obj);
320 } else if(nullable_Object_is_a_JavaObject(nullable_obj)){
321 jobject val = nullable_Object_as_JavaObject(nullable_obj);
323 } else if(nullable_Object_is_a_String(nullable_obj)){
324 String val = nullable_Object_as_String(nullable_obj);
325 char* c = String_to_cstring(val);
326 jstring js = (*self)->NewStringUTF(self, c);
329 fprintf(stderr, "NOT YET SUPPORTED: nit objects are not supported\n");
336 # Returns the field ID for an instance field of a class. The field is specified by its name and signature
337 fun get_field_id
(clazz
: JClass, name
: String, sign
: String): JFieldID import String.to_cstring
`{
338 return (*self)->GetFieldID(self, clazz, String_to_cstring(name), String_to_cstring(sign));
341 # 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()
342 fun get_object_field
(obj
: JavaObject, fieldID
: JFieldID): JavaObject `{
343 return (*self)->GetObjectField(self, obj, fieldID);
346 fun get_boolean_field
(obj
: JavaObject, fieldID
: JFieldID): Bool `{
347 return (*self)->GetBooleanField(self, obj, fieldID);
350 fun get_char_field
(obj
: JavaObject, fieldID
: JFieldID): Char `{
351 return (*self)->GetCharField(self, obj, fieldID);
354 fun get_int_field
(obj
: JavaObject, fieldID
: JFieldID): Int `{
355 return (*self)->GetIntField(self, obj, fieldID);
358 fun get_float_field
(obj
: JavaObject, fieldID
: JFieldID): Float `{
359 return (*self)->GetFloatField(self, obj, fieldID);
362 fun set_object_field
(obj
: JavaObject, fieldID
: JFieldID, value
: JavaObject) `{
363 (*self)->SetObjectField(self, obj, fieldID, value);
366 fun set_boolean_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Bool) `{
367 (*self)->SetBooleanField(self, obj, fieldID, value);
370 fun set_char_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Char) `{
371 (*self)->SetCharField(self, obj, fieldID, value);
374 fun set_int_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Int) `{
375 (*self)->SetIntField(self, obj, fieldID, value);
378 fun set_float_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Float) `{
379 (*self)->SetFloatField(self, obj, fieldID, value);
382 # Check for pending exception without creating a local reference to the exception object
383 fun exception_check
: Bool `{
384 return (*self)->ExceptionCheck(self);
387 # Construct an exception object from the specified class with the message specified by `message` and causes that exception to be thrown
388 fun throw_new
(clazz
: JClass, message
: String): Int import String.to_cstring
`{
389 return (*self)->ThrowNew(self, clazz, String_to_cstring(message));
392 # return the exception if there is one in the process of being thrown, or NULL if no exception is currently being thrown
393 fun exception_occurred
: JavaObject `{
394 return (*self)->ExceptionOccurred(self);
397 # prints an exception and backtrace to error channel
398 fun exception_describe
`{
399 return (*self)->ExceptionDescribe(self);
402 # clears any exception currently being thrown, has no effect if there is no exception
403 fun exception_clear
`{
404 return (*self)->ExceptionClear(self);
407 # Raise a fatal error
408 fun fatal_error
(msg
: String) import String.to_cstring
`{
409 (*self)->FatalError(self, String_to_cstring(msg));
412 # Transform a NIT String into a JavaObject
413 fun string_to_jobject
(string
: String): JavaObject `{
414 return (*self)->NewStringUTF(self, String_to_cstring(string));
417 # Pushes a local reference frame on the JNI stack
418 fun push_local_frame
(capacity
: Int): Bool `{
419 return (*self)->PushLocalFrame(self, capacity);
422 # Pops the current local reference frame on the JNI stack
424 # Similiar to `JavaObject::pop_from_local_frame` which returns a value.
425 fun pop_local_frame
`{
426 (*self)->PopLocalFrame(self, NULL);
430 # Represents a jni jclass
431 extern class JClass `{jclass`}
434 # Represents a jni jmethodID
435 extern class JMethodID `{jmethodID`}
438 # Represens a jni jobject
439 extern class JavaObject in "Java" `{ java.lang.Object `}
442 # Represents a jni JNINNativeMethod
443 extern class JNINativeMethod `{ JNINativeMethod* `}
444 fun name
: String import CString.to_s
`{
445 return CString_to_s((void*)self->name);
448 fun name
=(name
: String) import String.to_cstring
`{
449 self->name = String_to_cstring(name);
452 fun signature
: String import CString.to_s
`{
453 return CString_to_s((void*)self->signature);
456 fun signature
=(signature
: String) import String.to_cstring
`{
457 self->signature = String_to_cstring(signature);
461 # Represents a jni jfieldID
462 extern class JFieldID `{jfieldID`}
465 # Reprents a jni jvalue
466 extern class JValue `{jvalue`}
468 fun set_boolean
(b
: Bool) `{
472 fun get_boolean
:Bool `{
476 fun set_char
(c
: Char)`{
480 fun get_char
: Char `{
484 fun set_int
(i
: Int) `{
492 fun set_float
(f
: Float) `{
496 fun get_float
: Float `{
500 fun set_jobject
(obj
: JavaObject) `{
504 fun get_jobject
: JavaObject `{
510 redef fun to_jvalue
(env
): JValue `{
518 redef fun to_jvalue
(env
): JValue `{
526 redef fun to_jvalue
(env
): JValue `{
534 redef fun to_jvalue
(env
)`{
536 value.l = (*env)->NewStringUTF(env, self);
542 redef fun to_jvalue
(env
) do
543 return self.to_cstring
.to_jvalue
(env
)
548 fun to_jvalue
(env
: JniEnv): JValue is abstract