alicorn-systems / v8-adapter

Adapter for sharing Java classes and objects with a V8 runtime.
BSD 3-Clause Clear License
37 stars 18 forks source link

#11 Added ability of custom handling of return type from methods of injected objects (E.g. List/Map) #13

Closed AlexTrotsenko closed 6 years ago

AlexTrotsenko commented 6 years ago

by V8JavaAdapter.setJsTransformation(V8, T.class, new JavaToJsTransformation() {})

caer commented 6 years ago

@AlexTrotsenko I will review this shortly; apologies for two week delay...

AlexTrotsenko commented 6 years ago

Thank you, np with delays. Just to mention - this api only changes transformation of return values from the methods of injected Java classes, but not injection of Java object.

E.g. given:

class MapProvider {
      Map provideMap() {...}
}

Then following transformation will affect only return value of MapProvider.provideMap(), but not V8JavaAdapter.injectObject(v8, new Map())

V8JavaAdapter.setJsTransformation(v8, Map.class, javaObject -> V8ObjectUtils.toV8Array(v8, javaObject) )

I have implemented it as it's minimal functionality need for me.

If you think, that ability to "override" transformation of injected objects as well is also a good idea - then api could be changed to something like: V8JavaAdapter.injectClass(v8, Class, JavaToJsTransformation)

caer commented 6 years ago

On further inspection, this eerily resembles the interception API that we have already (see src/main/java/io/alicorn/v8/V8JavaClassInterceptor.java). Can you confirm this and then re-open this if the interceptor API does not meet your needs?

AlexTrotsenko commented 6 years ago

@crahda thanks for info, I will check V8JavaClassInterceptor !

AlexTrotsenko commented 5 years ago

@crahda I have tested suggested solution with V8JavaClassInterceptor.java.

Indeed, I was able to achieve the goal without adding new "JsTransformation" api. The only thing, that I need java object during construction of JS Object in .getConstructorScriptBody(), but not afterwards. Also onInject() call does not work for such case: I can't create JS array when Java List is injected. I have already created pull request with the changes - https://github.com/alicorn-systems/v8-adapter/pull/28.