cincheo / jsweet-eclipse-plugin

The official Eclipse plugin for the JSweet transpiler
http://www.jsweet.org
Apache License 2.0
20 stars 10 forks source link

System.out.print compiled wrong #20

Closed travelbasys closed 5 years ago

travelbasys commented 6 years ago

Hello. I am using the jsweet-quickstart project downloaded from GitHub.

I wrote another simple class:

package quickstart;

public class SimpleTest {

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("Test");

        System.out.print(sb.toString());
    }

}

After compiling I got the following SimpleTest.js:

/* Generated from Java with JSweet 2.0.0-rc1 - http://www.jsweet.org */
var quickstart;
(function (quickstart) {
    var SimpleTest = (function () {
        function SimpleTest() {
        }
        SimpleTest.main = function (args) {
            var sb = { str: "", toString: function () { return this.str; } };
            /* append */ (function (sb) { return sb.str = sb.str.concat("Test"); })(sb);
            java.lang.System.out.print(/* toString */ sb.str);
        };
        return SimpleTest;
    }());
    quickstart.SimpleTest = SimpleTest;
    SimpleTest["__class"] = "quickstart.SimpleTest";
})(quickstart || (quickstart = {}));
quickstart.SimpleTest.main(null);

I was quite impressed, how cleverly the StringBuilder.append method is emulated. But: System.out.print is just converted to java.lang.System.out.print. My Browser (Chrome) then complains in the developer console:

Uncaught ReferenceError: java is not defined at Function.SimpleTest.main (SimpleTest.js:10) at SimpleTest.js:17

Thanks for advice.

travelbasys commented 6 years ago

Oops... Forgot to mention: I am using Eclipse Neon and the JSweet and Maven plugins.

travelbasys commented 6 years ago

Got it! System.out.print causes the above problem. But System.out.println does not. Whatever the reasons.

PS: Sorry if this is in the wrong forum: jsweet might be the correct one.

renaudpawlak commented 6 years ago

Yes, that's indeed a JSweet issue :) As you can see in the default adapter, only println is supported.

This sort of partial support is kind of historic. In theory, all this should be done in the RemoveJavaDependenciesAdapter. Also it is quite easy to develop your own adapter to support it and extend JSweet for your own needs.

To make it right, I think we should move the println support to RemoveJavaDependenciesAdapter and add the print support there...

To avoid regressions, we can just add print support next to println support for now and move them in the next major release.

renaudpawlak commented 6 years ago

To be moved to JSweet.

lgrignon commented 5 years ago

Works on last JSweet Eclipse plugin version