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 # Represents a jni JavaVM
71 extern class JavaVM `{JavaVM *`}
72 new(env_ref: JniEnvRef) import jni_error, JniEnvRef.jni_env=, JniEnv as nullable `{
78 JavaVMOption options
[1];
79 options
[0].optionString
= "-Djava.class.path=.";
80 args
.version
= 0x00010002;
81 args
.options
= options
;
84 res
= JNI_CreateJavaVM(&jvm
, (void
**)&env
, &args
);
87 JavaVM_jni_error(NULL, "Could not create Java VM");
91 JniEnvRef_jni_env__assign(env_ref
, JniEnv_as_nullable_JniEnv(env
));
96 fun jni_error(msg: NativeString)
98 print "JNI Error: {msg}"
102 fun destroy_java_vm `{
103 (*recv
)->DestroyJavaVM(recv
);
107 # Represents a jni JNIEnv, which is a thread in a JavaVM
108 extern class JniEnv `{JNIEnv *`}
110 # Get a class object from its fully-qualified name or null if the class cannot be found
111 fun find_class
(class_name
: String): JClass import String.to_cstring
`{
112 return (*recv)->FindClass(recv,String_to_cstring(class_name));
115 # Return the method id for an instance of a class or interface
116 # The method is determined by its name and signature
117 # To obtain the method ID of a constructor, supply "<init>" as the method name and "void(V)" as the return type
118 fun get_method_id
(clazz
: JClass, name
: String, signature
: String): JMethodID import String.to_cstring
`{
119 return (*recv)->GetMethodID(recv, clazz, String_to_cstring(name), String_to_cstring(signature));
122 # Construct a new Java object from the `clazz`, using the constructor ̀ method_id`
123 fun new_object
(clazz
: JClass, method_id
: JMethodID): JObject `{
124 return (*recv)->NewObject(recv, clazz, method_id);
127 # Return the JClass of `obj`
128 fun get_object_class
(obj
: JObject): JClass `{
129 return (*recv)->GetObjectClass(recv, obj);
132 # Registers native methods with the class specified by the `clazz` argument
133 fun register_natives
(clazz
: JClass, method
: JNINativeMethod, n_method
: Int): Int `{
134 return (*recv)->RegisterNatives(recv, clazz, method, n_method);
137 # Call a method on `obj` designed by `method_id` with an array `args` of arguments
138 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
`{
139 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
140 (*recv)->CallVoidMethodA(recv, obj, method_id, args_tab);
144 # Call a method on `obj` designed by `method_id` with an array `args` of argument returning a JObject
145 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
`{
146 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
147 (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
151 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Bool
152 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
`{
153 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
154 return (*recv)->CallBooleanMethod(recv, obj, method_id, args_tab);
158 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Char
159 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
`{
160 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
161 return (*recv)->CallCharMethod(recv, obj, method_id, args_tab);
165 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning an Int
166 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
`{
167 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
168 return (*recv)->CallIntMethod(recv, obj, method_id, args_tab);
172 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a Float
173 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
`{
174 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
175 return (*recv)->CallFloatMethod(recv, obj, method_id, args_tab);
179 # Call a method on `obj` designed by `method_id` with an array `args` of arguments returning a NativeString
180 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
`{
181 jvalue * args_tab = convert_array_of_Object_to_c(args, recv);
182 jobject jobj = (*recv)->CallObjectMethod(recv, obj, method_id, args_tab);
184 return (char*)(*recv)->GetStringUTFChars(recv, (jstring)jobj, NULL);
187 # Returns the field ID for an instance field of a class. The field is specified by its name and signature
188 fun get_field_id
(clazz
: JClass, name
: String, sign
: String): JFieldID import String.to_cstring
`{
189 return (*recv)->GetFieldID(recv, clazz, String_to_cstring(name), String_to_cstring(sign));
192 # 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()
193 fun get_object_field
(obj
: JObject, fieldID
: JFieldID): JObject `{
194 return (*recv)->GetObjectField(recv, obj, fieldID);
197 fun get_boolean_field
(obj
: JObject, fieldID
: JFieldID): Bool `{
198 return (*recv)->GetBooleanField(recv, obj, fieldID);
201 fun get_char_field
(obj
: JObject, fieldID
: JFieldID): Char `{
202 return (*recv)->GetCharField(recv, obj, fieldID);
205 fun get_int_field
(obj
: JObject, fieldID
: JFieldID): Int `{
206 return (*recv)->GetIntField(recv, obj, fieldID);
209 fun get_float_field
(obj
: JObject, fieldID
: JFieldID): Float `{
210 return (*recv)->GetFloatField(recv, obj, fieldID);
213 fun set_object_field
(obj
: JObject, fieldID
: JFieldID, value
: JObject) `{
214 (*recv)->SetObjectField(recv, obj, fieldID, value);
217 fun set_boolean_field
(obj
: JObject, fieldID
: JFieldID, value
: Bool) `{
218 (*recv)->SetBooleanField(recv, obj, fieldID, value);
221 fun set_char_field
(obj
: JObject, fieldID
: JFieldID, value
: Char) `{
222 (*recv)->SetCharField(recv, obj, fieldID, value);
225 fun set_int_field
(obj
: JObject, fieldID
: JFieldID, value
: Int) `{
226 (*recv)->SetIntField(recv, obj, fieldID, value);
229 fun set_float_field
(obj
: JObject, fieldID
: JFieldID, value
: Float) `{
230 (*recv)->SetFloatField(recv, obj, fieldID, value);
233 # Check for pending exception without creating a local reference to the exception object
234 fun exception_check
: Bool `{
235 return (*recv)->ExceptionCheck(recv);
238 # Construct an exception object from the specified class with the message specified by `message` and causes that exception to be thrown
239 fun throw_new
(clazz
: JClass, message
: String): Int import String.to_cstring
`{
240 return (*recv)->ThrowNew(recv, clazz, String_to_cstring(message));
243 # return the exception if there is one in the process of being thrown, or NULL if no exception is currently being thrown
244 fun exception_occurred
: JObject `{
245 return (*recv)->ExceptionOccurred(recv);
248 # prints an exception and backtrace to error channel
249 fun exception_describe
`{
250 return (*recv)->ExceptionDescribe(recv);
253 # clears any exception currently being thrown, has no effect if there is no exception
254 fun exception_clear
`{
255 return (*recv)->ExceptionClear(recv);
258 # Raise a fatal error
259 fun fatal_error
(msg
: String) import String.to_cstring
`{
260 (*recv)->FatalError(recv, String_to_cstring(msg));
263 # Transform a NIT String into a JObject
264 fun string_to_jobject
(string
: String): JObject `{
265 return (*recv)->NewStringUTF(recv, String_to_cstring(string));
269 # used to initialize a JavaVM
271 var jni_env
: nullable JniEnv = null
274 # Represents a jni jclass
275 extern class JClass `{jclass`}
278 # Represents a jni jmethodID
279 extern class JMethodID `{jmethodID`}
282 # Represens a jni jobject
283 extern class JObject `{jobject`}
286 # Represents a jni JNINNativeMethod
287 extern class JNINativeMethod `{ JNINativeMethod* `}
288 fun name
: String import NativeString.to_s
`{
289 return NativeString_to_s(recv->name);
292 fun name
=(name
: String) import String.to_cstring
`{
293 recv->name = String_to_cstring(name);
296 fun signature
: String import NativeString.to_s
`{
297 return NativeString_to_s(recv->signature);
300 fun signature
=(signature
: String) import String.to_cstring
`{
301 recv->signature = String_to_cstring(signature);
305 # Represents a jni jfieldID
306 extern class JFieldID `{jfieldID`}
309 # Reprents a jni jvalue
310 extern class JValue `{jvalue`}
312 fun set_boolean
(b
: Bool) `{
316 fun get_boolean
:Bool `{
320 fun set_char
(c
: Char)`{
324 fun get_char
: Char `{
328 fun set_int
(i
: Int) `{
336 fun set_float
(f
: Float) `{
340 fun get_float
: Float `{
344 fun set_jobject
(obj
: JObject) `{
348 fun get_jobject
: JObject `{
354 redef fun to_jvalue
(env
): JValue `{
362 redef fun to_jvalue
(env
): JValue `{
370 redef fun to_jvalue
(env
): JValue `{
377 redef class NativeString
378 redef fun to_jvalue
(env
)`{
380 value.l = (*env)->NewStringUTF(env, recv);
386 redef fun to_jvalue
(env
) do
387 return self.to_cstring
.to_jvalue
(env
)
392 fun to_jvalue
(env
: JniEnv): JValue is abstract