Comments (4)
This crate doesn't have any wrapper for GetObjectRefType
because it uses Rust's type and lifetime system to keep track of JNI object reference types at compile time.
- An owned
JByteBuffer
,JClass
,JObject
,JObjectArray
,JPrimitiveArray
,JString
, orJThrowable
is always a JNI local reference. - An
AutoLocal
owns a JNI local reference (one of the types above) and deletes it when dropped. - A
GlobalRef
orWeakRef
is always a JNI global or weak reference, respectively. - There are no invalid references. The local reference types listed above all have a Rust lifetime parameter that proves that they're still valid.
GlobalRef
andWeakRef
are valid until they are dropped. AutoElements
,AutoElementsCritical
,JList
,JListIter
,JMap
andJMapIter
all borrow a JNI reference (not necessarily local).
Hence, the pattern you've been using in C++ shouldn't be necessary when using this crate.
I can think of one exception, though: if you receive a raw JNI object reference (that is, not one wrapped by one of the types in jni::objects
) from some other code and don't know whether it's local or global. Is that your situation?
from jni-rs.
Yes.
Also, there is still invalid references. An invalid reference can be created with
#[test]
fn get_local_ref_type() {
let env = attach_current_thread();
// let obj = env.new_string("test").unwrap();
let obj = JObject::null();
let auto_ref = AutoLocal::new(obj, &env);
// left: `InvalidRefType`
assert_eq!(env.get_object_ref_type(&auto_ref).unwrap(), RefType::LocalRefType);
}
from jni-rs.
It looks like jni-rs is currently missing null checks before calling delete_local_ref and delete_global_ref (the spec doesn't seem to clarify if it's OK to pass null to these, though I guess they are just noops as you'd want).
Null is a special case though, and I think it's probably good that you can make a null GlobalRef or null AutoLocal.
You shouldn't have to worry about whether references are valid or not unless dealing with raw sys types with unsafe code.
from jni-rs.
Oh okay, thanks.
from jni-rs.
Related Issues (20)
- pass rust function as implementation for Java object HOT 5
- JavaVM should be Clone HOT 1
- `get_static_field_unchecked` was not updated to use ReturnType in #344
- `{get,set}[_static]_field_unchecked` should be `unsafe`
- How to add elements into an `JObjectArray`? HOT 2
- `JNIStr` should implement `to_str()` and not `Deref` directly to a `CStr`
- How to pass JavaVM from c to rust HOT 2
- `JClass::from`, `JString::from`, `JThrowable::from`, etc. safety HOT 2
- Byte type confusion
- Feature request: JString from JChar slice via NewString
- How to return jobject as kotlin data class
- New release on crates.io
- Could not compile jni (build script) HOT 1
- JNIEnv::register_native_methods should be marked as unsafe
- JNIEnv::get_string should delete the local ref of string_class HOT 1
- Performance enhancement in get_string
- `new_object_unchecked` should accept`Desc<'local, JMethodID>`
- [Question]: How does one send a native rust function to java as a callback? HOT 2
- `JNIEnv::take_rust_field` seems meant for use with `java.lang.Object.finalize`, but that is deprecated
- Unsound usages of unsafe implementation from small size to large size HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jni-rs.