Hi

I'm using GWT 2.0.3

It seems to me that in hosted mode when I attempt to use (for example) a float[] as a parameter to a native call it silently fails.

The documentation only refers very obliquely to this:

Although Java arrays are not directly usable in JavaScript, there are some helper classes that efficiently achieve a similar effect: JsArray <http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/core/client/JsArray.html>, JsArrayBoolean <http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/core/client/JsArrayBoolean.html>, JsArrayInteger <http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/core/client/JsArrayInteger.html>, JsArrayNumber <http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/core/client/JsArrayNumber.html>, and JsArrayString <http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/core/client/JsArrayString.html>. These classes are wrappers around a native JavaScript array.

And, in fact, this appears to be true. If you want an array of floats, you must create them in a native method.

As an example, if you create a client function

public void myFunc(int x, int y, float[] z) {
   do something with z here.
}

"z" will always be 'undefined' in hosted mode (although, oddly, it seems that it can work in production mode.)

What does seem to work in both hosted and production mode is:

public static native JsArrayNumber getNativeArray () /*-{ return [ 0, 1, -5, -1, -1, -5, 1, -1, -5]; }-*/;

public void myFunc2(int x, int y,  JsArrayNumber z) {
// now if invoked something like myFunc2(5, 10, getNativeArray()) the array works as expected.
}

So, my questions are:

1. Is what I've just said correct?

2. If so, why doesn't the debug runtime system generate a warning when encountering a 'float []' in hosted mode? Ok it crashes with an obscure exception,
but that isn't very helpful.

If you need to initialize a float [] from a client side java program and then pass that array to a native method, you have to do something like:

float [] floats = myinstance.myMethod2CreateFloatArray();

public static native JsArrayNumber createNew() /*-{ return new Array(); }-*/;

   public static JsArrayNumber initArray(float [] f) {
       JsArrayNumber a = createNew();
       for (float fv :f)
           a.push(fv);
       return a;
};

This seems to work in both hosted and production mode. Any suggestions as to a better way to do this would be appreciated.

I think the documentation should be a little bit more explicit about this, I'd be happy to contribute.

Regards

Alan







--
You received this message because you are subscribed to the Google Groups "Google 
Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to