Creates a new Jvm. If a JavaVM is already created by the current process, it attempts to attach the current thread to it.
(jvm_options: &[String], lib_name_to_load: Option<String>)
| 130 | /// Creates a new Jvm. |
| 131 | /// If a JavaVM is already created by the current process, it attempts to attach the current thread to it. |
| 132 | fn create_jvm(jvm_options: &[String], lib_name_to_load: Option<String>) -> errors::Result<Jvm> { |
| 133 | debug("Creating a Jvm"); |
| 134 | let mut jvm: *mut JavaVM = ptr::null_mut(); |
| 135 | let mut jni_environment: *mut JNIEnv = ptr::null_mut(); |
| 136 | |
| 137 | // Create the Jvm atomically |
| 138 | let _g = cache::MUTEX.lock()?; |
| 139 | |
| 140 | let result = if let Some(env) = cache::get_thread_local_env_opt() { |
| 141 | debug("A JVM is already created for this thread. Retrieving it..."); |
| 142 | jni_environment = env; |
| 143 | |
| 144 | JNI_OK |
| 145 | } else { |
| 146 | let created_vm = Self::get_created_vm(); |
| 147 | |
| 148 | let res_int = if created_vm.is_some() { |
| 149 | debug("A JVM is already created by another thread. Retrieving it..."); |
| 150 | jni_environment = created_vm.unwrap(); |
| 151 | |
| 152 | JNI_OK |
| 153 | } else { |
| 154 | info("No JVMs exist. Creating a new one..."); |
| 155 | let mut cstrings_to_drop: Vec<*mut c_char> = Vec::with_capacity(jvm_options.len()); |
| 156 | let mut jvm_options_vec: Vec<JavaVMOption> = jvm_options |
| 157 | .iter() |
| 158 | .map(|opt| { |
| 159 | let cstr = utils::to_c_string(opt); |
| 160 | let jo = JavaVMOption { |
| 161 | optionString: cstr, |
| 162 | extraInfo: ptr::null_mut() as *mut c_void, |
| 163 | }; |
| 164 | cstrings_to_drop.push(cstr); |
| 165 | jo |
| 166 | }) |
| 167 | .collect(); |
| 168 | |
| 169 | let mut jvm_arguments = JavaVMInitArgs { |
| 170 | version: JNI_VERSION_1_6, |
| 171 | nOptions: jvm_options.len() as i32, |
| 172 | options: jvm_options_vec.as_mut_ptr(), |
| 173 | ignoreUnrecognized: JNI_TRUE, |
| 174 | }; |
| 175 | |
| 176 | let int_result = tweaks::create_java_vm( |
| 177 | &mut jvm, |
| 178 | (&mut jni_environment as *mut *mut JNIEnv) as *mut *mut c_void, |
| 179 | (&mut jvm_arguments as *mut JavaVMInitArgs) as *mut c_void, |
| 180 | ); |
| 181 | |
| 182 | cstrings_to_drop |
| 183 | .into_iter() |
| 184 | .for_each(|s| unsafe { utils::drop_c_string(s) }); |
| 185 | |
| 186 | int_result |
| 187 | }; |
| 188 | |
| 189 | res_int |
nothing calls this directly
no test coverage detected