eclipsesource / J2V8

Java Bindings for V8
2.54k stars 355 forks source link

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

Closed ancientloregames closed 7 years ago

ancientloregames commented 7 years ago

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 commented 7 years ago

You might consider looking at the features in PR #201.

Depending on the results of the PR, these features will be implemented in a separate github project or added to J2V8 in an upcoming release.

ancientloregames commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago
  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 commented 7 years ago
  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 commented 7 years ago

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!