Draw2d compilation issues [WAS: seeking advice on where to initialize Canvas for classes that implement Drawable]

7 views
Skip to first unread message

Jake B

unread,
Jul 7, 2009, 1:24:54 AM7/7/09
to java2...@googlegroups.com
Hi Zhou,

I've spawned a new thread to talk about some of the Draw2d compilation
issues I've been facing. I'll spawn another thread later on to discuss
PaintListener issues.

Right now, I'm having the following issue trying to run
org.eclipse.draw2d.exmpales/org.eclipse.draw2d.examples.HelloWorld, in
core.z.js on line 67:

too much recursion
[Break on this error] var v=Math.round(value)&0xffffffff;

Here's a snippet of the code that appears to be causing this:

Clazz.makeConstructor(Integer,
function(value){
var v=Math.round(value)&0xffffffff;
this.valueOf=function(){
return v;
};
},"Number");

When I debug this with Firebug, the runtime behaviour is such that
"value" starts by being passed a value of 1, and increases by a factor
of 2, up to 32, and then appears not to be called anymore.
Instrumenting the code this way causes Firebug to avoid the above
error message, but also keeps the SWT Shell from opening, which
previously had been working.

I read what you said about debugging problems in Java2Script, but I
don't see how an issue manifesting itself in the j2s core would be
caused by issues in user-generated code, so I'd appreciate it if you
could help me analyze this and understand it. I have a few questions
to that effect:

* What is the intent of the above code block? It looks like it's
declaring a constructor for the Integer wrapper class, a part of j2s
JRE emulation, but I'm not clear on what the valueOf function is
supposed to be doing in this context.

* What is your take on the behaviour I have described?

I feel like issues like pose the greatest risk to my project, and so
I'd like to try to deal with them first before finishing issues
involving SWT emulation.

I'd greatly appreciate any guidance you can provide. Thanks,

Jake

On Wed, Jul 1, 2009 at 12:29 PM, Zhou Renjian<zhour...@gmail.com> wrote:
>
> On Wed, Jul 1, 2009 at 1:36 PM, Jake B <otak...@gmail.com> wrote:
>>
>> Hi all,
>>
>> I just wanted to report that I have made some progress. I'm not quite
>> sure what is the best way to deploy a Java2Script application (any
>> pointers on this would be appreciated), but here's a quick screenshot
>> showing some of my results:
>>
>> http://jacobbeard.net/research/Screenshot-Mozilla%20Firefox-j2sCanvas.png
>
> Congratulations!
>
>>
>>
>> The graphics there were compiled using Java2Script, use SWT GC API's,
>> and target HTML canvas. It's an incomplete implementation of GC, but a
>> full implementation is, I think, quite feasible. This is now fairly
>> low risk, and I'd estimate it to be about a week of work to finish
>> implementing GC on top of HTML canvas.
>>
>> Rather than pursue this, I'd like to now drill down on what appear to
>> me to be two higher-risk problems:
>> 1. Getting more sophisticated uses of GC to work, for example
>> org.eclipse.swt.examples.graphics.GraphicsExample from the
>> org.eclipse.swt.examples project.
>> 2. Getting more sophisticated libraries that target GC to work on top
>> of Java2Script, specifically Draw2d.
>
> Draw2D is complicated, and GEF would be much more complicated. No body has
> been there until someone reaches there step by step.
>
>>
>>
>> I have so far encountered challenges with both of these.
>>
>> For 1., it is still unclear what the best solution is for emulating
>> PaintListener, which is what many GC demos require. I feel we should
>> reopen the discussion regarding what is the best way to emulate this.
>
> It seems that PaintListener is a necessary for you.
>
> One of the key question to emulating PaintListener is what is the time to
> send an SWT.Paint event: When canvas is created, resized, destroyed? Sending
> too much SWT.Paint events will make the whole UI very slow.
>
> You can change
> protected boolean SetWindowPos(Object hWnd, Object hWndInsertAfter, int X,
> int Y, int cx, int cy, int uFlags);
> to send out an SWT.Paint event. You'd better using a delayed thread to send
> out event.
>
>>
>>
>> There were other technical challenges that I encountered for 1. and 2.
>> When attempting to run the compiled
>> org.eclipse.swt.examples.graphics.GraphicsExample the compiled version
>
> In my environment GraphicsExample is loading as expected, no browser
> freezing. And this might be a bug that we need to dig it out.
>
>>
>> of caused Firefox 3.0 to hang, it seems when it was trying to load
>> Tree.z.js. For 2., when attempting to run the compiled Draw2d Hello
>> World demo
>> (org.eclipse.draw2d.exmpales/org.eclipse.draw2d.examples.HelloWorld),
>> I got errors in core.z.js, about too much recursion. I don't really
>
> Too much recursion might be a defect of Java2Script. Try to avoid
> polymorphous constructors or methods like
>
> Hello(int x) {
>     this(0L);
> }
>
> Hello(long x) {
>     this.x = x;
> }
>
> Java2Script might not distinguish some data types, like all short, int,
> long, float, dobule are Number. and String, Object and arrays might confuse
> Java2Script.
>
> Try to find out methods that are cuasing problems. Comment out some methods
> or ignore all other methods leaving only one method by using @j2sIgnore and
> merge all those methods into one method using @j2sNative Javadoc. ...
>
> Hope this information helps.
>
>>
>> have a sense of how to debug issues like these. They seem to be
>> compiler issues, and I feel like I'm not really in a position to delve
>> deep into the inner workings of the j2s compiler. Hence my inclination
>> is to flag 1. and 2. as high-risk. I'd appreciate any insight you
>> might have as to how to track down what might be causing these
>> problems, as well as to whether it would be worthwhile to file bug
>> reports for them.
>>
>> Also, I wonder if it might be worthwhile to open a discussion
>> regarding the feasibility of using GWT compiler in place of the
>> Java2Script compiler, as was mentioned on the e4-dev list. This might
>> help solve some of the above issues. Or it might tremendously
>> complicate things :P
>
> I consider the two compilers may have a big gap to fill.
>
>>
>>
>> I have two final small questions:
>> * What is the easiest way to deploy Java2Script applications to a
>> normal web server (no servlet container)?
>
> You do not need a servlet container to deploy Java2Script applications. Just
> copy folder "j2slib" and all your *.js, *.css, *.html, image files to an
> HTTP server. You may need to modify your entry *.html a little bit,
> correcting the relative path according to your server path.
>
>>
>> * How can I tell Java2Script to import external JavaScript resources
>> like excanvas.js?
>
> What about creating a new EXCanvas.java and wrapping excanvas.js content
> using @j2sPrefix Javadoc like:
> /**
>  * @j2sPrefix
> /-*! ....comments
> -*-/
> var swfobject = function() {
> ...
> }
>  */
> public class FlashUtils {
>     public static boolean isFlashEnabled() {
>         return true;
>     }
> }
>
> We do NOT recommend to use internal Java2Script ClassLoader API for loading
> external *.js file.
>
>>
>>
>> Please let me know what you think. Thanks,
>>
>> Jake
>>
>> 2009/6/8 Jake B <otak...@gmail.com>:
>> > Got it. Thanks :)
>> >
>> > Jake
>> >
>> > On Mon, Jun 8, 2009 at 9:31 AM, Zhou Renjian<zhour...@gmail.com>
>> > wrote:
>> >> In JavaScript, we use @J2SNative annotation or @j2sNative javaDoc to do
>> >> so,
>> >> like the following:
>> >>
>> >> class A {
>> >>   private Object o;
>> >>   public void put(String k, String v) {
>> >>     if (k != null)
>> >>     /** @j2sNative
>> >>      *  this.o[k]=v;
>> >>      */ {}
>> >>   }
>> >>   public void get(String k) {
>> >>     /** @j2sNative
>> >>      *  return this.o[k];
>> >>      */ {}
>> >>   }
>> >> }
>> >>
>> >> or
>> >>
>> >> class A {
>> >>   private Object o;
>> >>   /**
>> >>    * @j2sNative
>> >>    * this.o[k]=v;
>> >>    */
>> >>   public void put(String k, String v) {
>> >>   }
>> >>   /**
>> >>    * @j2sNative
>> >>    * return this.o[k];
>> >>    */
>> >>   public native void get(String k);
>> >> }
>> >>
>> >> Regards,
>> >> Zhou Renjian
>> >>
>> >> --
>> >> http://webuzz.im/ Web IMs (G/A/M/Y)
>> >> http://j2s.sourceforge.net/ Java2Script
>> >>
>> >>
>> >> On Mon, Jun 8, 2009 at 9:23 PM, Jake B <otak...@gmail.com> wrote:
>> >>>
>> >>> Hi Zhou,
>> >>>
>> >>> > So modify your Hashtable member
>> >>> > into an JavaScript Object member and use its properties to store key
>> >>> > and
>> >>> > value. It will load less and run faster.
>> >>>
>> >>> I'm just not clear, is there a Java wrapper class around JavaScript
>> >>> object for Java2Script, like GWT's JavaScriptObject class? If you
>> >>> could give me a code snippet, that will help me better understand what
>> >>> you mean.
>> >>>
>> >>> Thanks,
>> >>>
>> >>> Jake
>> >>>
>> >>>
>> >>
>> >>
>> >> >>
>> >>
>> >
>>
>>
>
> Regards,
> Zhou Renjian
>
> --
> http://webuzz.im/ Web IMs (G/A/M/Y)
> http://j2s.sourceforge.net/ Java2Script
>
> >
>

Zhou Renjian

unread,
Jul 7, 2009, 2:05:21 AM7/7/09
to java2...@googlegroups.com
Hi Jake,

If you use latest GEF/Draw2D 3.5, I think you need to delete one of 2 methods #setAlpha in
/org.eclipse.draw2d/src/org/eclipse/draw2d/Shape.java
about line#370 and #385.It might trigger infinite method calls.

The step I take on your situations is to search all *.java with regular expression "Integer\s*(" to identify where Integer constructor is called. And later identify which line trigger the problem. ... It is a hard problem for me too.

For why #valueOf is there, here is the reason:
Integer extends Number, and Number have some methods like this:
Clazz.defineMethod (Number, "intValue",
    function () {
        return Math.round (this) & 0xffffffff;
    });
The "Math.round(this)" will be calling "Math.round(this.valueOf ())" in fact. "valueOf" is a special JavaScript prototype method. So the Integer constructor is defined with:
this.valueOf = function () { return v; };
It seems that such Integer emulation is not 100% correct, it needs more work to avoid potential problems. I will review it later.

Hope this message help.


Regards,
Zhou Renjian

--
http://webuzz.im/ Web IMs (G/A/M/Y)
http://j2s.sourceforge.net/ Java2Script


On Tue, Jul 7, 2009 at 1:24 PM, Jake B <otak...@gmail.com> wrote:
org.eclipse.draw2d.exmpales

Zhou Renjian

unread,
Jul 7, 2009, 2:13:48 AM7/7/09
to java2...@googlegroups.com
Both #setAntialias and #setAlpah need to be fixed.

    /**
     * @since 3.5
     */
    public void setAlpha(Integer value) {
        if(alpha != value) {
            alpha = value;
            repaint();
        }
    }
   
    /**
     * @since 3.5
     */
    public void setAlpha(int value) {
        setAlpha(new Integer(value));
    }
   
    /**
     * @see org.eclipse.swt.graphics.GC#setAntialias(int)
     * @param value
     * @since 3.5
     */
    public void setAntialias(Integer value) {
        if(antialias != value) {
            antialias = value;
            repaint();
        }
    }
   
    /**
     * @since 3.5
     */
    public void setAntialias(int value) {
        setAntialias(new Integer(value));
    }

Using regular expression like "\(.*Integer\s" may help you to identify other problems.



Regards,
Zhou Renjian

--
http://webuzz.im/ Web IMs (G/A/M/Y)
http://j2s.sourceforge.net/ Java2Script


Jake B

unread,
Jul 7, 2009, 2:26:53 AM7/7/09
to java2...@googlegroups.com
Zhou, thank you for the quick responses. I'll be sure to look into it tomorrow.

Jake
Reply all
Reply to author
Forward
0 new messages