1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Romain Chanoir <romain.chanoir@courrier.uqam.ca>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Manipulates the Java Virtual Machine
19 # See: http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/jniTOC.html
21 c_compiler_option
("-I $(JAVA_HOME)/include/")
22 c_linker_option
("-L $(JNI_LIB_PATH) -ljvm")
31 inline jvalue * convert_array_of_Object_to_c(nullable_Array_of_nullable_Object nullable_nit_array, JNIEnv* env){
32 if(nullable_Array_of_nullable_Object_is_null(nullable_nit_array)){
35 Array_of_nullable_Object nit_array = nullable_Array_of_nullable_Object_as_Array_of_nullable_Object(nullable_nit_array);
36 int nit_array_length = Array_of_nullable_Object_length(nit_array);
38 jvalue *c_array = malloc(sizeof(jvalue)*(nit_array_length));
39 for (i = 0; i < nit_array_length; i ++) {
40 nullable_Object nullable_obj = Array_of_nullable_Object__index(nit_array, i);
41 if(nullable_Object_is_a_Int(nullable_obj)) {
42 int val = nullable_Object_as_Int(nullable_obj);
44 }else if (nullable_Object_is_a_Char(nullable_obj)){
45 char val = nullable_Object_as_Char(nullable_obj);
47 }else if (nullable_Object_is_a_Bool(nullable_obj)){
48 int val = nullable_Object_as_Bool(nullable_obj);
50 }else if(nullable_Object_is_a_Float(nullable_obj)){
51 float val = nullable_Object_as_Float(nullable_obj);
53 }else if(nullable_Object_is_a_JObject(nullable_obj)){
54 jobject val = nullable_Object_as_JObject(nullable_obj);
56 }else if(nullable_Object_is_a_String(nullable_obj)){
57 String val = nullable_Object_as_String(nullable_obj);
58 char* c = String_to_cstring(val);
59 jstring js = (*env)->NewStringUTF(env, c);
62 fprintf(stderr, "NOT YET SUPPORTED: nit objects are not supported\n");
70 # Utility to select options to create the VM using `create_jvm`
74 # var builder = new JavaVMBuilder
75 # builder.options.add "-Djava.class.path=."
76 # var jvm = builder.create_jvm
77 # var env = builder.jni_env
82 var version
: Int = "00010002".to_hex
83 var options
= new Array[String]
85 fun create_jvm
: nullable JavaVM
87 var args
= new JavaVMInitArgs
88 args
.version
= version
90 args
.n_options
= options
.length
92 var c_options
= new JavaVMOptionArray(options
.length
)
93 for o
in options
.length
.times
do
94 var option
= options
[o
]
95 var c_option
= c_options
[o
]
96 c_option
.string
= option
99 args
.options
= c_options
101 var jvm
= new JavaVM(args
, self)
106 if jvm
.address_is_null
then return null
111 extern class JavaVMInitArgs `{ JavaVMInitArgs* `}
112 new `{ return (JavaVMInitArgs*)malloc(sizeof(JavaVMInitArgs)); `}
114 # Set the defaut config for a VM
115 # Can be called after setting the version
116 fun set_default
`{ JNI_GetDefaultJavaVMInitArgs(recv); `}
118 fun version: Int `{ return recv->version; `}
119 fun version
=(v
: Int) `{ recv->version = v; `}
121 fun options: JavaVMOptionArray `{ return recv->options; `}
122 fun options
=(v
: JavaVMOptionArray) `{ recv->options = v; `}
124 fun n_options: Int `{ return recv->nOptions; `}
125 fun n_options
=(v
: Int) `{ recv->nOptions = v; `}
128 extern class JavaVMOption `{ JavaVMOption* `}
129 fun string
: String import NativeString.to_s
`{
130 return NativeString_to_s(recv->optionString);
132 fun string
=(v
: String) import String.to_cstring
`{
133 recv->optionString = String_to_cstring(v);
136 fun extra_info
: String import NativeString.to_s
`{
137 return NativeString_to_s(recv->extraInfo);
139 fun extra_info
=(v
: String) import String.to_cstring
`{
140 recv->extraInfo = String_to_cstring(v);
144 extern class JavaVMOptionArray `{ JavaVMOption* `}
145 new(size: Int) `{ return (JavaVMOption*)malloc(sizeof(JavaVMOption)*size); `}
147 fun [](i
: Int): JavaVMOption `{ return recv+i; `}
150 # Represents a jni JavaVM
151 extern class JavaVM `{JavaVM *`}
152 # Create the JVM, returns its handle and store the a pointer to JniEnv in `env_ref`
153 new(args
: JavaVMInitArgs, env_ref
: JniEnvRef) import jni_error
, JniEnvRef.jni_env
=, JniEnv as nullable `{
158 res = JNI_CreateJavaVM(&jvm, (void**)&env, args);
161 JavaVM_jni_error(NULL, "Could not create Java VM", res);
165 JniEnvRef_jni_env__assign(env_ref, JniEnv_as_nullable_JniEnv(env));
170 fun jni_error
(msg
: NativeString, v
: Int)
172 print
"JNI Error: {msg} ({v})"
177 (*recv)->DestroyJavaVM(recv);
180 fun env
: JniEnv import jni_error
`{
182 int res = (*recv)->GetEnv(recv, (void **)&env, JNI_VERSION_1_6);
184 JavaVM_jni_error(NULL, "Could not get JNIEnv from Java VM", res);
190 fun attach_current_thread
: JniEnv `{
192 int res = (*recv)->AttachCurrentThread(recv, (void**)&env, NULL);
194 JavaVM_jni_error(NULL, "Could not attach current thread to Java VM", res);
200 # Represents a jni JNIEnv, which is a thread in a JavaVM
201 extern class JniEnv `{JNIEnv *`}
203 # Get a class object from its fully-qualified name or null if the class cannot be found
204 fun find_class(class_name : String): JClass import String.to_cstring `{
205 return (*recv
)->FindClass(recv
,String_to_cstring(class_name
));
208 # Return the method id for an instance of a class or interface
209 # The method is determined by its name and signature
210 # To obtain the method ID of a constructor, supply "<init>" as the method name and "void(V)" as the return type
211 fun get_method_id(clazz : JClass, name : String, signature : String): JMethodID import String.to_cstring `{
212 return (*recv
)->GetMethodID(recv
, clazz
, String_to_cstring(name
), String_to_cstring(signature
));
215 # Construct a new Java object from the `clazz
`, using the constructor ̀ method_id`
216 fun new_object
(clazz
: JClass, method_id
: JMethodID): JObject `{
217 return (*recv)->NewObject(recv, clazz, method_id);
220 # Return the JClass of `obj`
221 fun get_object_class
(obj
: JObject): JClass `{
222 return (*recv)->GetObjectClass(recv, obj);
225 # Registers native methods with the class specified by the `clazz` argument
226 fun register_natives
(clazz
: JClass, method
: JNINativeMethod, n_method
: Int): Int `{
227 return (*recv)->RegisterNatives(recv, clazz, method, n_method);
230 # Call a method on `obj` designed by `method_id` with an array `args` of arguments
231 fun call_void_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]) 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(JObject), nullable Object.as(String), String.to_cstring
`{
232 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
233 (*recv)->CallVoidMethodA(recv, obj, method_id, args_tab);
237 # Call a method on `obj` designed by `method_id` with an array `args` of argument returning a JObject
238 fun call_object_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): JObject 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(JObject), nullable Object.as(String), String.to_cstring
`{
239 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
240 (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
244 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Bool
245 fun call_boolean_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Bool 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(JObject), nullable Object.as(String), String.to_cstring
`{
246 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
247 return (*recv)->CallBooleanMethod(recv, obj, method_id, args_tab);
251 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Char
252 fun call_char_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Char 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(JObject), nullable Object.as(String), String.to_cstring
`{
253 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
254 return (*recv)->CallCharMethod(recv, obj, method_id, args_tab);
258 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning an Int
259 fun call_int_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Int 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(JObject), nullable Object.as(String), String.to_cstring
`{
260 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
261 return (*recv)->CallIntMethod(recv, obj, method_id, args_tab);
265 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Float
266 fun call_float_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): Float 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(JObject), nullable Object.as(String), String.to_cstring
`{
267 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
268 return (*recv)->CallFloatMethod(recv, obj, method_id, args_tab);
272 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a NativeString
273 fun call_string_method
(obj
: JObject, method_id
: JMethodID, args
: nullable Array[nullable Object]): NativeString 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(JObject), nullable Object.as(String), String.to_cstring
, String.length
`{
274 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
275 jobject jobj = (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
277 return (char*)(*recv)->GetStringUTFChars(recv, (jstring)jobj, NULL);
280 # Returns the field ID for an instance field of a class. The field is specified by its name and signature
281 fun get_field_id
(clazz
: JClass, name
: String, sign
: String): JFieldID import String.to_cstring
`{
282 return (*recv)->GetFieldID(recv, clazz, String_to_cstring(name), String_to_cstring(sign));
285 # 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()
286 fun get_object_field
(obj
: JObject, fieldID
: JFieldID): JObject `{
287 return (*recv)->GetObjectField(recv, obj, fieldID);
290 fun get_boolean_field
(obj
: JObject, fieldID
: JFieldID): Bool `{
291 return (*recv)->GetBooleanField(recv, obj, fieldID);
294 fun get_char_field
(obj
: JObject, fieldID
: JFieldID): Char `{
295 return (*recv)->GetCharField(recv, obj, fieldID);
298 fun get_int_field
(obj
: JObject, fieldID
: JFieldID): Int `{
299 return (*recv)->GetIntField(recv, obj, fieldID);
302 fun get_float_field
(obj
: JObject, fieldID
: JFieldID): Float `{
303 return (*recv)->GetFloatField(recv, obj, fieldID);
306 fun set_object_field
(obj
: JObject, fieldID
: JFieldID, value
: JObject) `{
307 (*recv)->SetObjectField(recv, obj, fieldID, value);
310 fun set_boolean_field
(obj
: JObject, fieldID
: JFieldID, value
: Bool) `{
311 (*recv)->SetBooleanField(recv, obj, fieldID, value);
314 fun set_char_field
(obj
: JObject, fieldID
: JFieldID, value
: Char) `{
315 (*recv)->SetCharField(recv, obj, fieldID, value);
318 fun set_int_field
(obj
: JObject, fieldID
: JFieldID, value
: Int) `{
319 (*recv)->SetIntField(recv, obj, fieldID, value);
322 fun set_float_field
(obj
: JObject, fieldID
: JFieldID, value
: Float) `{
323 (*recv)->SetFloatField(recv, obj, fieldID, value);
326 # Check for pending exception without creating a local reference to the exception object
327 fun exception_check
: Bool `{
328 return (*recv)->ExceptionCheck(recv);
331 # Construct an exception object from the specified class with the message specified by `message` and causes that exception to be thrown
332 fun throw_new
(clazz
: JClass, message
: String): Int import String.to_cstring
`{
333 return (*recv)->ThrowNew(recv, clazz, String_to_cstring(message));
336 # return the exception if there is one in the process of being thrown, or NULL if no exception is currently being thrown
337 fun exception_occurred
: JObject `{
338 return (*recv)->ExceptionOccurred(recv);
341 # prints an exception and backtrace to error channel
342 fun exception_describe
`{
343 return (*recv)->ExceptionDescribe(recv);
346 # clears any exception currently being thrown, has no effect if there is no exception
347 fun exception_clear
`{
348 return (*recv)->ExceptionClear(recv);
351 # Raise a fatal error
352 fun fatal_error
(msg
: String) import String.to_cstring
`{
353 (*recv)->FatalError(recv, String_to_cstring(msg));
356 # Transform a NIT String into a JObject
357 fun string_to_jobject
(string
: String): JObject `{
358 return (*recv)->NewStringUTF(recv, String_to_cstring(string));
362 # used to initialize a JavaVM
364 var jni_env
: nullable JniEnv = null
367 # Represents a jni jclass
368 extern class JClass `{jclass`}
371 # Represents a jni jmethodID
372 extern class JMethodID `{jmethodID`}
375 # Represens a jni jobject
376 extern class JObject `{jobject`}
379 # Represents a jni JNINNativeMethod
380 extern class JNINativeMethod `{ JNINativeMethod* `}
381 fun name
: String import NativeString.to_s
`{
382 return NativeString_to_s(recv->name);
385 fun name
=(name
: String) import String.to_cstring
`{
386 recv->name = String_to_cstring(name);
389 fun signature
: String import NativeString.to_s
`{
390 return NativeString_to_s(recv->signature);
393 fun signature
=(signature
: String) import String.to_cstring
`{
394 recv->signature = String_to_cstring(signature);
398 # Represents a jni jfieldID
399 extern class JFieldID `{jfieldID`}
402 # Reprents a jni jvalue
403 extern class JValue `{jvalue`}
405 fun set_boolean
(b
: Bool) `{
409 fun get_boolean
:Bool `{
413 fun set_char
(c
: Char)`{
417 fun get_char
: Char `{
421 fun set_int
(i
: Int) `{
429 fun set_float
(f
: Float) `{
433 fun get_float
: Float `{
437 fun set_jobject
(obj
: JObject) `{
441 fun get_jobject
: JObject `{
447 redef fun to_jvalue
(env
): JValue `{
455 redef fun to_jvalue
(env
): JValue `{
463 redef fun to_jvalue
(env
): JValue `{
470 redef class NativeString
471 redef fun to_jvalue
(env
)`{
473 value.l = (*env)->NewStringUTF(env, recv);
479 redef fun to_jvalue
(env
) do
480 return self.to_cstring
.to_jvalue
(env
)
485 fun to_jvalue
(env
: JniEnv): JValue is abstract