alicorn-systems/v8-adapter

Accessing injected java map values as JS Object properties

Closed this issue · 2 comments

newk5 commented

Apologies for opening this issue as I've noticed this has been talked before (#11) but I'm a little confused. Is this possible to do somehow with a V8JavaClassInterceptor right now? Or is this a planned feature for the new restructure? If this is possible to achieve with the current version, could you provide an example of how to do it?

@newk5 I have tried to use interceptor api, but was not able to achieve the goal without passing original java object in interceptor call-back.
The change is in the PR #28.

Until it's review and merged by @crahda you can get using jitpack:

dependencies {
	        implementation 'com.github.AlexTrotsenko:v8-adapter:issues_11_customizing_object_injection-SNAPSHOT'
	}

Just in case - you have to set-up Jitpack 1st:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

You can check an example of using it is in the tests. But shortly it's following:

        public void shouldCustomizeJavaToJSTransformation() {
        final V8JavaClassInterceptor interceptor = new V8JavaClassInterceptor<Map>() {
            @Override
            public Object objectInjectorOverride(Map object) {
                return V8ObjectUtils.toV8Object(v8, object);
            }
             @Override public String getConstructorScriptBody() { return null; }
            @Override public void onInject(V8JavaClassInterceptorContext context, Map object) { }
            @Override public void onExtract(V8JavaClassInterceptorContext context, Map object) { }
        };
         final V8JavaClassInterceptor listInterceptor = new V8JavaClassInterceptor<List>() {
            @Override
            public Object objectInjectorOverride(List object) {
                return V8ObjectUtils.toV8Array(v8, object);
            }
             @Override public String getConstructorScriptBody() { return null; }
            @Override public void onInject(V8JavaClassInterceptorContext context, List object) { }
            @Override public void onExtract(V8JavaClassInterceptorContext context, List object) { }
        };
         V8JavaAdapter.injectClass(HashMap.class, interceptor, v8);
        V8JavaAdapter.injectClass(ArrayList.class, listInterceptor, v8);

Here instead of HashMap and ArrayList you can specify implementation needed for you or several if needed.

caer commented

These changes are now available in 1.59-SNAPSHOT, and will be available in 1.59 sometime in the next few days!