Giter Club home page Giter Club logo

Comments (4)

argv-minus-one avatar argv-minus-one commented on June 24, 2024

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, or JThrowable 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 or WeakRef 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 and WeakRef are valid until they are dropped.
  • AutoElements, AutoElementsCritical, JList, JListIter, JMap and JMapIter 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.

Rabbit0w0 avatar Rabbit0w0 commented on June 24, 2024

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.

rib avatar rib commented on June 24, 2024

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.

Rabbit0w0 avatar Rabbit0w0 commented on June 24, 2024

Oh okay, thanks.

from jni-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.