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

what's the right way to manage the java object? #362

Closed
jan4984 opened this issue Nov 6, 2017 · 3 comments
Closed

what's the right way to manage the java object? #362

jan4984 opened this issue Nov 6, 2017 · 3 comments

Comments

@jan4984
Copy link

jan4984 commented Nov 6, 2017

I have a js class implement in j2v8(kotlin).

    private val constructor = JavaCallback { receiver, parameters ->
        val name = parameters.getString(0)
        val shadow = MyJavaClass(name)
        //=========================================
        //TODO:attach the shadow java object to receiver
    }
    private val test= JavaVoidCallback { receiver, _ ->
        //=========================================
        //TODO::get java object back from receiver
        shadow.doSomething()
    }
   val c = V8Function(v8, constructor)
        val cp = c.getObject("prototype")
        cp.registerJavaMethod(test, "__test__")
        cp.release()
   v8.add("MyJavaClass", c)
   v8.executeVoideScript("new MyJavaClass().__test__()")

I can not find a way to make a shadow java object to the js object, including let Java GC not release my shadow when js object lives.

any suggestion how to attach NON-STATIC java method to js object?

@caer
Copy link
Contributor

caer commented Nov 6, 2017

PR #201 most likely addresses the problem you are trying to solve. I am in the process of making it into a stand-alone GitHub project so that users can use it on top of J2V8 without having to incorporate it into the main J2V8 repository; let me know if you have any questions about how it works!

@jan4984
Copy link
Author

jan4984 commented Nov 6, 2017

ok, I subscribed #201 , close this.

@jan4984 jan4984 closed this as completed Nov 6, 2017
@jan4984
Copy link
Author

jan4984 commented Nov 6, 2017

Just let any looking solution know, I currently use this code and works if you use them carefully

//static MyJavaClass curInstance=null;
receiver.registerVCB("__java_back__"){_,_->
        curInstance=shadow
}
//whenever you need java instance
receiver.executeVoidFunction("__java_back__")
//and now the receiver's java shadow object in curInstance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants