Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating to jni-rs 0.21 (fix #1) #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion catch_panic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ jni = "0"
catch_panic_macros = { version = "1.0.0", path = "../catch_panic_macros" }

[dev-dependencies]
jni = { version = "0", features = ["invocation"] }
jni = { version = "0.21.1", features = ["invocation"] }
catch_panic = { path = ".", features = ["internal-doctests"] }
trybuild = "1.0.63"
2 changes: 1 addition & 1 deletion catch_panic/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
/// `#[catch_panic]`'s default panic handler. This
/// will rethrow all caught panics as java `RuntimeException`s
/// with the message passed to `panic!`.
pub fn default_handler(env: JNIEnv, err: Box<dyn Any + Send + 'static>) {
pub fn default_handler(mut env: JNIEnv, err: Box<dyn Any + Send + 'static>) {
let msg = match err.downcast_ref::<&'static str>() {
Some(s) => *s,
None => match err.downcast_ref::<String>() {
Expand Down
19 changes: 12 additions & 7 deletions catch_panic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
//! #
//! #[no_mangle]
//! #[catch_panic(default = "std::ptr::null_mut()")]
//! pub extern "C" fn Java_com_example_Example_gimmeAnObject(env: JNIEnv) -> jobject {
//! env.alloc_object("java/lang/Object").unwrap().into_inner()
//! pub extern "C" fn Java_com_example_Example_gimmeAnObject(mut env: JNIEnv) -> jobject {
//! env.alloc_object("java/lang/Object").unwrap().into_raw()
//! }
//!
//! # catch_panic::test::check_callback(|env| Java_com_example_Example_gimmeAnObject(env), false);
Expand All @@ -59,7 +59,7 @@
//! # use jni::JNIEnv;
//! # use catch_panic::catch_panic;
//! #
//! pub fn enterprise_certified_handler(env: JNIEnv, err: Box<dyn Any + Send + 'static>) {
//! pub fn enterprise_certified_handler(mut env: JNIEnv, err: Box<dyn Any + Send + 'static>) {
//! let msg = match err.downcast_ref::<&'static str>() {
//! Some(s) => *s,
//! None => match err.downcast_ref::<String>() {
Expand Down Expand Up @@ -88,7 +88,7 @@
//! #
//! # env.define_class(
//! # "com/example/ExampleEnterpriseException",
//! # object_class_loader.try_into().unwrap(),
//! # &object_class_loader.try_into().unwrap(),
//! # &src,
//! # )
//! # .unwrap();
Expand Down Expand Up @@ -120,12 +120,17 @@ pub mod test {

pub fn check_callback_with_setup<S, C, R>(setup: S, callback: C, should_throw: bool)
where
S: FnOnce(JNIEnv),
S: FnOnce(&mut JNIEnv),
C: Fn(JNIEnv) -> R,
{
let env = util::attach_current_thread();
setup(*env);
callback(*env);
unsafe {
// Safety: cloned env will be dropped before guard drop, and do not
// create local reference crossing local reference frame.
let mut env = env.unsafe_clone();
setup(&mut env);
callback(env);
}
assert_eq!(
env.exception_check().expect("Couldn't check if there was an exception"),
should_throw,
Expand Down
2 changes: 1 addition & 1 deletion catch_panic/testlib/jni-rs
Submodule jni-rs updated 67 files
+94 −3 CHANGELOG.md
+1 −1 CONTRIBUTING.md
+12 −2 Cargo.toml
+208 −54 benches/api_calls.rs
+0 −108 build.rs
+326 −0 docs/0.21-MIGRATION.md
+1 −1 example/mylib/Cargo.toml
+43 −36 example/mylib/src/lib.rs
+9 −9 src/lib.rs
+24 −20 src/wrapper/descriptors/class_desc.rs
+122 −5 src/wrapper/descriptors/desc.rs
+29 −19 src/wrapper/descriptors/exception_desc.rs
+10 −6 src/wrapper/descriptors/field_desc.rs
+16 −10 src/wrapper/descriptors/method_desc.rs
+41 −1 src/wrapper/errors.rs
+16 −19 src/wrapper/executor.rs
+266 −44 src/wrapper/java_vm/init_args.rs
+117 −0 src/wrapper/java_vm/init_args/char_encoding_generic.rs
+443 −0 src/wrapper/java_vm/init_args/char_encoding_windows.rs
+241 −58 src/wrapper/java_vm/vm.rs
+1,499 −663 src/wrapper/jnienv.rs
+4 −1 src/wrapper/macros.rs
+0 −137 src/wrapper/objects/auto_array.rs
+264 −0 src/wrapper/objects/auto_elements.rs
+168 −0 src/wrapper/objects/auto_elements_critical.rs
+106 −30 src/wrapper/objects/auto_local.rs
+0 −90 src/wrapper/objects/auto_primitive_array.rs
+33 −23 src/wrapper/objects/global_ref.rs
+42 −13 src/wrapper/objects/jbytebuffer.rs
+53 −13 src/wrapper/objects/jclass.rs
+47 −18 src/wrapper/objects/jfieldid.rs
+183 −86 src/wrapper/objects/jlist.rs
+195 −144 src/wrapper/objects/jmap.rs
+47 −18 src/wrapper/objects/jmethodid.rs
+71 −21 src/wrapper/objects/jobject.rs
+81 −0 src/wrapper/objects/jobject_array.rs
+144 −0 src/wrapper/objects/jprimitive_array.rs
+48 −19 src/wrapper/objects/jstaticfieldid.rs
+48 −20 src/wrapper/objects/jstaticmethodid.rs
+46 −13 src/wrapper/objects/jstring.rs
+50 −11 src/wrapper/objects/jthrowable.rs
+169 −105 src/wrapper/objects/jvalue.rs
+15 −5 src/wrapper/objects/mod.rs
+5 −2 src/wrapper/objects/release_mode.rs
+176 −0 src/wrapper/objects/weak_ref.rs
+60 −13 src/wrapper/signature.rs
+3 −3 src/wrapper/strings/ffi_str.rs
+160 −26 src/wrapper/strings/java_str.rs
+138 −2 tests/executor.rs
+2 −2 tests/executor_nested_attach.rs
+43 −0 tests/invocation_character_encoding.rs
+11 −11 tests/java_integers.rs
+14 −12 tests/jmap.rs
+471 −208 tests/jni_api.rs
+10 −10 tests/jni_global_refs.rs
+195 −0 tests/jni_weak_refs.rs
+2 −2 tests/threads_attach_guard.rs
+2 −2 tests/threads_detach.rs
+2 −2 tests/threads_detach_daemon.rs
+7 −3 tests/threads_explicit_detach.rs
+7 −3 tests/threads_explicit_detach_daemon.rs
+7 −3 tests/threads_explicit_detach_permanent.rs
+9 −9 tests/threads_nested_attach_daemon.rs
+9 −9 tests/threads_nested_attach_guard.rs
+9 −9 tests/threads_nested_attach_permanently.rs
+4 −3 tests/util/example_proxy.rs
+3 −3 tests/util/mod.rs
2 changes: 1 addition & 1 deletion catch_panic/ui_tests/fail/no_jni_env.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error: #[catch_panic] requires that this function has at least one argument of t
--> ui_tests/fail/no_jni_env.rs:4:1
|
4 | fn no_jni_env() {
| ^^^^^^^^^^^^^^^
| ^^
2 changes: 1 addition & 1 deletion catch_panic/ui_tests/fail/on_struct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ error: #[catch_panic] can only be applied to functions
--> ui_tests/fail/on_struct.rs:4:1
|
4 | pub struct PanicDonationBox;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^
33 changes: 19 additions & 14 deletions catch_panic/ui_tests/fail/wrong_handler_type.stderr
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
error[E0631]: type mismatch in function arguments
--> ui_tests/fail/wrong_handler_type.rs:9:25
|
4 | pub fn trust_me(_env: JNIEnv, err: String) {
| ------------------------------------------ found signature of `for<'r> fn(JNIEnv<'r>, String) -> _`
--> ui_tests/fail/wrong_handler_type.rs:9:25
|
4 | pub fn trust_me(_env: JNIEnv, err: String) {
| ------------------------------------------ found signature defined here
...
9 | #[catch_panic(handler = "trust_me")]
| ------------------------^^^^^^^^^^--
| | |
| | expected signature of `for<'r> fn(JNIEnv<'r>, Box<(dyn Any + Send + 'static)>) -> _`
| required by a bound introduced by this call
|
9 | #[catch_panic(handler = "trust_me")]
| ------------------------^^^^^^^^^^--
| | |
| | expected due to this
| required by a bound introduced by this call
|
= note: expected function signature `for<'a> fn(JNIEnv<'a>, Box<(dyn Any + Send + 'static)>) -> _`
found function signature `for<'a> fn(JNIEnv<'a>, String) -> _`
note: required by a bound in `__catch_panic`
--> src/handler.rs
|
| H: FnOnce(JNIEnv, Box<dyn Any + Send + 'static>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `__catch_panic`
--> src/handler.rs
|
| pub fn __catch_panic<F, R, H>(env: JNIEnv, default: R, handler: H, f: F) -> R
| ------------- required by a bound in this function
...
| H: FnOnce(JNIEnv, Box<dyn Any + Send + 'static>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `__catch_panic`
8 changes: 7 additions & 1 deletion catch_panic_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ pub fn catch_panic(attr: TokenStream, item: TokenStream) -> TokenStream {
TokenStream::from(quote! {
#(#attrs)*
#vis #sig {
::catch_panic::handler::__catch_panic(#first_arg_name, #default_value, #handler, move || {
// Safety: one JNIEnv goes to #handler, the other one goes to
// #block closure. __catch_panic() itself do not use JNIEnv so
// any local reference created with JNIEnv must be within #hander
// and/or #block, and these references are guaranteed to be
// discarded when #handler or #block exit.
let __handler_env = unsafe { #first_arg_name.unsafe_clone() };
::catch_panic::handler::__catch_panic(__handler_env, #default_value, #handler, move || {
#block
})
}
Expand Down