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/ -I $(JAVA_HOME)/include/linux/"
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
77 # Unavailable on Android, where you cannot instanciate a new JVM.
80 JNI_GetDefaultJavaVMInitArgs(recv);
84 fun version
: Int `{ return recv->version; `}
85 fun version=(v: Int) `{ recv->version = v; `}
87 fun options
: JavaVMOptionArray `{ return recv->options; `}
88 fun options=(v: JavaVMOptionArray) `{ recv->options = v; `}
90 fun n_options
: Int `{ return recv->nOptions; `}
91 fun n_options=(v: Int) `{ recv->nOptions = v; `}
94 extern class JavaVMOption `{ JavaVMOption* `}
95 fun string: String import NativeString.to_s `{
96 return NativeString_to_s((char
*)recv-
>optionString
);
98 fun string=(v: String) import String.to_cstring `{
99 recv-
>optionString
= String_to_cstring(v
);
102 fun extra_info: String import NativeString.to_s `{
103 return NativeString_to_s((char
*)recv-
>extraInfo
);
105 fun extra_info=(v: String) import String.to_cstring `{
106 recv-
>extraInfo
= String_to_cstring(v
);
110 extern class JavaVMOptionArray `{ JavaVMOption* `}
111 new(size
: Int) `{ return (JavaVMOption*)malloc(sizeof(JavaVMOption)*size); `}
113 fun [](i: Int): JavaVMOption `{ return recv+i; `}
116 # Represents a jni JavaVM
117 extern class JavaVM `{JavaVM *`}
118 # Create the JVM, returns its handle and store the a pointer to JniEnv in `env_ref
`
120 # Unavailable on Android, where you cannot instanciate a new JVM.
121 new(args: JavaVMInitArgs, env_ref: JniEnvRef) import jni_error, JniEnvRef.jni_env=, JniEnv.as nullable `{
124 JavaVM_jni_error(NULL, "JVM creation not supported on Android", 0);
132 res
= JNI_CreateJavaVM(&jvm
, (void
**)&env
, args
);
135 JavaVM_jni_error(NULL, "Could not create Java VM", res
);
139 JniEnvRef_jni_env__assign(env_ref
, JniEnv_as_nullable_JniEnv(env
));
144 fun jni_error(msg: NativeString, v: Int)
146 print "JNI Error: {msg} ({v})"
151 (*recv
)->DestroyJavaVM(recv
);
154 fun env: JniEnv import jni_error `{
156 int res
= (*recv
)->GetEnv(recv
, (void
**)&env
, JNI_VERSION_1_6);
158 JavaVM_jni_error(NULL, "Could not get JNIEnv from Java VM", res
);
164 fun attach_current_thread: JniEnv `{
167 // the signature
is different
(better actually
) on
Android
168 int res
= (*recv
)->AttachCurrentThread(recv
, &env
, NULL);
170 int res
= (*recv
)->AttachCurrentThread(recv
, (void
**)&env
, NULL);
173 JavaVM_jni_error(NULL, "Could not attach current thread to Java VM", res
);
180 # Represents a jni JNIEnv, which is a thread in a JavaVM
181 extern class JniEnv `{JNIEnv *`}
183 # Get a class object from its fully-qualified name or null if the class cannot be found
184 fun find_class
(class_name
: String): JClass import String.to_cstring
`{
185 return (*recv)->FindClass(recv,String_to_cstring(class_name));
188 # Return the method id for an instance of a class or interface
189 # The method is determined by its name and signature
190 # To obtain the method ID of a constructor, supply "<init>" as the method name and "void(V)" as the return type
191 fun get_method_id
(clazz
: JClass, name
: String, signature
: String): JMethodID import String.to_cstring
`{
192 return (*recv)->GetMethodID(recv, clazz, String_to_cstring(name), String_to_cstring(signature));
195 # Construct a new Java object from the `clazz`, using the constructor ̀ method_id`
196 fun new_object
(clazz
: JClass, method_id
: JMethodID): JavaObject `{
197 return (*recv)->NewObject(recv, clazz, method_id);
200 # Return the JClass of `obj`
201 fun get_object_class
(obj
: JavaObject): JClass `{
202 return (*recv)->GetObjectClass(recv, obj);
205 # Registers native methods with the class specified by the `clazz` argument
206 fun register_natives
(clazz
: JClass, method
: JNINativeMethod, n_method
: Int): Int `{
207 return (*recv)->RegisterNatives(recv, clazz, method, n_method);
210 # Call a method on `obj` designed by `method_id` with an array `args` of arguments
211 fun call_void_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]) import convert_args_to_jni
`{
212 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
213 (*recv)->CallVoidMethodA(recv, obj, method_id, args_tab);
217 # Call a method on `obj` designed by `method_id` with an array `args` of argument returning a JavaObject
218 fun call_object_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): JavaObject import convert_args_to_jni
`{
219 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
220 jobject res = (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
225 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Bool
226 fun call_boolean_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Bool import convert_args_to_jni
`{
227 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
228 jboolean res = (*recv)->CallBooleanMethod(recv, obj, method_id, args_tab);
233 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Char
234 fun call_char_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Char import convert_args_to_jni
`{
235 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
236 jchar res = (*recv)->CallCharMethod(recv, obj, method_id, args_tab);
241 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning an Int
242 fun call_int_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Int import convert_args_to_jni
`{
243 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
244 jint res = (*recv)->CallIntMethod(recv, obj, method_id, args_tab);
249 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Float
250 fun call_float_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Float import convert_args_to_jni
`{
251 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
252 jfloat res = (*recv)->CallFloatMethod(recv, obj, method_id, args_tab);
257 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a NativeString
258 fun call_string_method
(obj
: JavaObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): NativeString import convert_args_to_jni
`{
259 jvalue * args_tab = JniEnv_convert_args_to_jni(recv, args);
260 jobject jobj = (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
262 return (char*)(*recv)->GetStringUTFChars(recv, (jstring)jobj, NULL);
265 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
`{
266 if(nullable_Array_of_nullable_Object_is_null(args)){
269 Array_of_nullable_Object nit_array = nullable_Array_of_nullable_Object_as_Array_of_nullable_Object(args);
270 int nit_array_length = Array_of_nullable_Object_length(nit_array);
272 jvalue *c_array = malloc(sizeof(jvalue)*(nit_array_length));
273 for (i = 0; i < nit_array_length; i ++) {
274 nullable_Object nullable_obj = Array_of_nullable_Object__index(nit_array, i);
275 if(nullable_Object_is_a_Int(nullable_obj)) {
276 int val = nullable_Object_as_Int(nullable_obj);
278 } else if (nullable_Object_is_a_Char(nullable_obj)){
279 char val = nullable_Object_as_Char(nullable_obj);
281 } else if (nullable_Object_is_a_Bool(nullable_obj)){
282 int val = nullable_Object_as_Bool(nullable_obj);
284 } else if(nullable_Object_is_a_Float(nullable_obj)){
285 float val = nullable_Object_as_Float(nullable_obj);
287 } else if(nullable_Object_is_a_JavaObject(nullable_obj)){
288 jobject val = nullable_Object_as_JavaObject(nullable_obj);
290 } else if(nullable_Object_is_a_String(nullable_obj)){
291 String val = nullable_Object_as_String(nullable_obj);
292 char* c = String_to_cstring(val);
293 jstring js = (*recv)->NewStringUTF(recv, c);
296 fprintf(stderr, "NOT YET SUPPORTED: nit objects are not supported\n");
303 # Returns the field ID for an instance field of a class. The field is specified by its name and signature
304 fun get_field_id
(clazz
: JClass, name
: String, sign
: String): JFieldID import String.to_cstring
`{
305 return (*recv)->GetFieldID(recv, clazz, String_to_cstring(name), String_to_cstring(sign));
308 # 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()
309 fun get_object_field
(obj
: JavaObject, fieldID
: JFieldID): JavaObject `{
310 return (*recv)->GetObjectField(recv, obj, fieldID);
313 fun get_boolean_field
(obj
: JavaObject, fieldID
: JFieldID): Bool `{
314 return (*recv)->GetBooleanField(recv, obj, fieldID);
317 fun get_char_field
(obj
: JavaObject, fieldID
: JFieldID): Char `{
318 return (*recv)->GetCharField(recv, obj, fieldID);
321 fun get_int_field
(obj
: JavaObject, fieldID
: JFieldID): Int `{
322 return (*recv)->GetIntField(recv, obj, fieldID);
325 fun get_float_field
(obj
: JavaObject, fieldID
: JFieldID): Float `{
326 return (*recv)->GetFloatField(recv, obj, fieldID);
329 fun set_object_field
(obj
: JavaObject, fieldID
: JFieldID, value
: JavaObject) `{
330 (*recv)->SetObjectField(recv, obj, fieldID, value);
333 fun set_boolean_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Bool) `{
334 (*recv)->SetBooleanField(recv, obj, fieldID, value);
337 fun set_char_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Char) `{
338 (*recv)->SetCharField(recv, obj, fieldID, value);
341 fun set_int_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Int) `{
342 (*recv)->SetIntField(recv, obj, fieldID, value);
345 fun set_float_field
(obj
: JavaObject, fieldID
: JFieldID, value
: Float) `{
346 (*recv)->SetFloatField(recv, obj, fieldID, value);
349 # Check for pending exception without creating a local reference to the exception object
350 fun exception_check
: Bool `{
351 return (*recv)->ExceptionCheck(recv);
354 # Construct an exception object from the specified class with the message specified by `message` and causes that exception to be thrown
355 fun throw_new
(clazz
: JClass, message
: String): Int import String.to_cstring
`{
356 return (*recv)->ThrowNew(recv, clazz, String_to_cstring(message));
359 # return the exception if there is one in the process of being thrown, or NULL if no exception is currently being thrown
360 fun exception_occurred
: JavaObject `{
361 return (*recv)->ExceptionOccurred(recv);
364 # prints an exception and backtrace to error channel
365 fun exception_describe
`{
366 return (*recv)->ExceptionDescribe(recv);
369 # clears any exception currently being thrown, has no effect if there is no exception
370 fun exception_clear
`{
371 return (*recv)->ExceptionClear(recv);
374 # Raise a fatal error
375 fun fatal_error
(msg
: String) import String.to_cstring
`{
376 (*recv)->FatalError(recv, String_to_cstring(msg));
379 # Transform a NIT String into a JavaObject
380 fun string_to_jobject
(string
: String): JavaObject `{
381 return (*recv)->NewStringUTF(recv, String_to_cstring(string));
384 # Pushes a local reference frame on the JNI stack
385 fun push_local_frame
(capacity
: Int): Bool `{
386 return (*recv)->PushLocalFrame(recv, capacity);
389 # Pops the current local reference frame on the JNI stack
391 # Similiar to `JavaObject::pop_from_local_frame` which returns a value.
392 fun pop_local_frame
`{
393 (*recv)->PopLocalFrame(recv, NULL);
397 # used to initialize a JavaVM
399 var jni_env
: nullable JniEnv = null
402 # Represents a jni jclass
403 extern class JClass `{jclass`}
406 # Represents a jni jmethodID
407 extern class JMethodID `{jmethodID`}
410 # Represens a jni jobject
411 extern class JavaObject in "Java" `{ java.lang.Object `}
414 # Represents a jni JNINNativeMethod
415 extern class JNINativeMethod `{ JNINativeMethod* `}
416 fun name
: String import NativeString.to_s
`{
417 return NativeString_to_s((void*)recv->name);
420 fun name
=(name
: String) import String.to_cstring
`{
421 recv->name = String_to_cstring(name);
424 fun signature
: String import NativeString.to_s
`{
425 return NativeString_to_s((void*)recv->signature);
428 fun signature
=(signature
: String) import String.to_cstring
`{
429 recv->signature = String_to_cstring(signature);
433 # Represents a jni jfieldID
434 extern class JFieldID `{jfieldID`}
437 # Reprents a jni jvalue
438 extern class JValue `{jvalue`}
440 fun set_boolean
(b
: Bool) `{
444 fun get_boolean
:Bool `{
448 fun set_char
(c
: Char)`{
452 fun get_char
: Char `{
456 fun set_int
(i
: Int) `{
464 fun set_float
(f
: Float) `{
468 fun get_float
: Float `{
472 fun set_jobject
(obj
: JavaObject) `{
476 fun get_jobject
: JavaObject `{
482 redef fun to_jvalue
(env
): JValue `{
490 redef fun to_jvalue
(env
): JValue `{
498 redef fun to_jvalue
(env
): JValue `{
505 redef class NativeString
506 redef fun to_jvalue
(env
)`{
508 value.l = (*env)->NewStringUTF(env, recv);
514 redef fun to_jvalue
(env
) do
515 return self.to_cstring
.to_jvalue
(env
)
520 fun to_jvalue
(env
: JniEnv): JValue is abstract