Wednesday, January 27, 2016

Re: JsInterop and indexed types

Hi Alex, 
really thanks for excellent answers. 

I'm writing portable code. This is the reason why I can not use JS TypedArrays directly.
My code must work in GWT, RoboVM and Android.

I'm looking for writing code in Java once and run it with best possible efficiency on all platforms.

In RoboVM platform we have transparent marshalling of java arrays to native layer 
  int[] -> int *, char [] -> short *, e,t,c  String -> utf8 char *
In Android we use Get\Release PrimitiveArrayCritical for such needs to get underlying pointer in native. No copies. 

We don't have something similar on web. Very unfortunately. 
This is a true reason why I think this is urgent and why I'm asking of it.

As a result, we must implement all computations in two ways
  - in Java for native compilers (RoboVM + Android)
  - in JS for Web

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

This is a goal. And it is nice solve it.
 
Thanks
 -Kirill

    

On Wed, Jan 27, 2016 at 8:43 PM, Alexey Andreev <konsoletyper@gmail.com> wrote:

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 a topic in the Google Groups "GWT Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-web-toolkit/YOWINYAoCrI/unsubscribe.
To unsubscribe from this group and all its topics, 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.

--
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