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

J2V8: creating new instance of binded object from JS #338

Closed
ancientloregames opened this issue Aug 21, 2017 · 6 comments
Closed

J2V8: creating new instance of binded object from JS #338

ancientloregames opened this issue Aug 21, 2017 · 6 comments

Comments

@ancientloregames
Copy link

Our teams is currently under consideration of witch Javascript Engine Bridge to use. We are choosing between J2V8 and LiquidCore.
Supposedly, I have to create several instances of some java class in javascript, how can one achieve this in J2V8 using standard instantiation annotation (new ClassName())? In liquidcore you can bind some class, that extends JSFuction, with super constructor:
JSFunction(JSContext ctx, final String methodName, final Class<?extends JSObject> instanceClass)
and register property with desired class name like this:
jsBaseContext.getJsContext().property("WebSocket", this);
and then on calling:
var x = new WebSocket();
java method methodName will fire, where we'll get new instanceClass as JSValue object as parameter, already binded and ready to use in javascript.

@caer
Copy link
Contributor

caer commented Aug 21, 2017 via email

@ancientloregames
Copy link
Author

@mizumi thanks a lot for such a quick answer! You've committed great addition to the project. After some testing, few questions have appeared, though.

  1. I'm using Facebook's Stetho for firing and debugging JS. On creating some amount of instances of java class and calling gc and V8JavaCache.removeGarbageCollectedJavaObjects();, those instances still exists in heap. Whats correct way to fully delete object?
  2. Is there a way to call JavaScript's gc from java?
  3. Does &release signature can be replaced with some implicit way of removing instances from list of class proxies? I'm creating cross platform app iOS/Android with all logic on JS, so the must be one set of scripts.

@caer
Copy link
Contributor

caer commented Aug 23, 2017

No problem; glad you like it!

  1. Are you referring to the V8 or Java heaps?
  2. That's a question for @irbull.
  3. Are you referring to the V8Object#release() method? And are you referring to the cache of proxies or the cache of instances in V8JavaCache?

@ancientloregames
Copy link
Author

  1. I've spotted those post-released instances in java heap dump via AS.
  2. I've found lowMemoryNotification method in V8 class. According to comment, it can call gc, but it's not clear, witch one. JS or Java? Can @irbull explain?
  3. What I've meant is that iOS has build-in way of firing JS without WebView, so, in order to keep common behavior across whole project, we need to unify all js scripts. Is there a way of making your project work as intended not calling $release method on class instances without altering code? Our app doesn't require access to objects created in js on native side. For now at least.

@caer
Copy link
Contributor

caer commented Aug 25, 2017

  1. It's possible there's a subtle bug I'm missing, but more than likely there is another reference being held to those objects which prevents it from being collected. You may consider invoked System.gc() immediately after V8JavaCache.removeGarbageCollectedJavaObjects() to see if that helps; I'm currently modifying this code so it's non-static and more robustly performs garbage collection.

  2. I have a suspicion it's referring to the V8 GC due to a distinct lack of Java GC invocations in that source code, but @irbull would know best.

  3. I've been reviewing the source code, and there is actually no reason for that invocation. I believe I may have included it with the purpose of preventing objects from being prematurely garbage collected, but there appear to be no issues in testing related to that. I will be removing it from my test source and updating this PR or the project that comes out of it.

@ancientloregames
Copy link
Author

Cool news! Hope, you'll share your improvements with the community. The essential new feature will be auto binding of the class properties to js and additional annotation of properties and methods to bind. I've invented this features in our project, but, you're definitely more competent of how to do this the right way. Thanks for your answers and time!

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