Skip to content

Commit 51d38ab

Browse files
committed
#112: impl Jvm::instance_into_raw_object and jvm::into_raw
1 parent bda0114 commit 51d38ab

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

rust/src/api/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,30 @@ impl Jvm {
12331233
}
12341234
}
12351235

1236+
/// Consumes an `Instance` and returns its jobject. The returned jobject is a JNI local reference.
1237+
pub fn instance_into_raw_object(&self, instance: Instance) -> errors::Result<jobject> {
1238+
debug(&format!("Getting the raw jobject from instance of class {}", instance.borrow().class_name()));
1239+
// Call the getObjectMethod. This returns a localref
1240+
let object_instance = unsafe {
1241+
(opt_to_res(cache::get_jni_call_object_method())?)(
1242+
self.jni_env,
1243+
instance.jinstance,
1244+
cache::get_get_object_method()?,
1245+
)};
1246+
1247+
Self::do_return(
1248+
self.jni_env,
1249+
object_instance,
1250+
)
1251+
}
1252+
1253+
/// Consumes the `Jvm` and returns its `JNIEnv`
1254+
pub fn into_raw(self) -> *mut JNIEnv {
1255+
debug(&format!("Getting the raw JNIEnv from the Jvm"));
1256+
1257+
self.jni_env
1258+
}
1259+
12361260
/// Returns the Rust representation of the provided instance, boxed
12371261
pub fn to_rust_boxed<T>(&self, instance: Instance) -> errors::Result<Box<T>>
12381262
where

rust/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ mod lib_unit_tests {
163163
use std::collections::HashMap;
164164
use std::convert::TryFrom;
165165
use std::path::MAIN_SEPARATOR;
166+
use std::ptr::null_mut;
166167
use std::thread::JoinHandle;
167168
use std::{thread, time};
168169

@@ -1302,4 +1303,22 @@ mod lib_unit_tests {
13021303
assert!(jvm.check_equals(&null_instance, InvocationArg::try_from(Null::Integer)?)?);
13031304
Ok(())
13041305
}
1306+
1307+
#[test]
1308+
fn instance_into_raw_object() -> errors::Result<()> {
1309+
let jvm = create_tests_jvm()?;
1310+
let test_instance = jvm.create_instance("org.astonbitecode.j4rs.tests.MyTest", InvocationArg::empty())?;
1311+
let null_instance = jvm.invoke(&test_instance, "getNullInteger", InvocationArg::empty())?;
1312+
let jobj = jvm.instance_into_raw_object(null_instance)?;
1313+
assert!(jobj == null_mut());
1314+
Ok(())
1315+
}
1316+
1317+
#[test]
1318+
fn jvm_into_raw_object() -> errors::Result<()> {
1319+
let jvm = create_tests_jvm()?;
1320+
let jobj = jvm.into_raw();
1321+
assert!(jobj != null_mut());
1322+
Ok(())
1323+
}
13051324
}

0 commit comments

Comments
 (0)