Wednesday, January 27, 2016

Re: JsInterop and indexed types


Sorry for by bad english. 
I meant a different thing.

Oh, now I see. Well, making Java array equal to JavaScript array is not an only solution for passing direct reference to an array. TeaVM could have a method to "unwrap" array, giving a native code reference to `data` field. There is one, but it is like "unsafe" in Oracle JDK: it is not documented and it's not guaranteed to remain its API in future versions. It should look like this:

public static Int8Array unwrapArray(byte[] array) {
    return Platform.getPlatformObject(array);
}
@JSBody(params = "wrapper", script = "return wrapper.data;")
private static native Int8Array doUnwrap(JSObject wrapper);

The magic is in undocumented Platform class, getPlatformObject particulary. It gets a Java object and returns it as JSObject. But do it on your own risk. Also, it's possible to corrupt application by assigning to the array something that does not fit in byte type (a floating point number or a string) or by changing the length of the array.

As for avoiding wrapper class. It's nearly impossible in general, since Java arrays are Java objects as well. Consider the following:

void foo(Object obj) {
    System.out.println(obj.toString());
}
void bar() {
    foo(new int[] { 1, 2, 3 });
}

How could TeaVM implement it without wrapper?

It is possible though to avoid negative effects of wrappers in certains cases, first, as I said, by eliminating unwrap operations when possible, and, second, performing some kind of escape analysis.

Note that the reverse operation (converting JavaScript array to Java array) is impossible in TeaVM by design. TeaVM performs global analysis of your code and for each variable (in terms of SSA) determines its possible types. It's impossible in general case to predict in compile time what actual type method like this may return:

Object wrapPlatformObject(JSObject obj);

I'm not sure how GWT actually works, but I suppose it does something similar, therefore it most likely you will fail with GWT as well. However, it's possible to hardcode additional methods to Platform class that convert JSArray to primitive arrays. You can open an issue if you really are going to use TeaVM for such purpose.

There are JSO wrappers in TeaVM and JSNI wrappers in GWT for TypedArrays. Why don't you use them directly to boost performance?

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment