cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.45k stars 159 forks source link

Default field initializations can occur too late in overloaded constructors #730

Closed vorth closed 1 year ago

vorth commented 1 year ago

This is a regression, as it was working in 2.3.7.

Paste this Java code into the live sandbox. You can see the code for the zero-argument constructor is wrong, with the default initialization for number occurring last instead of first.

package org.jsweet;

import static def.dom.Globals.*;

public class HelloWorld {
    public static void main(String[] args) {
        Overloaded obj = new Overloaded(44);
        alert( "This should be 44: " + obj.getNumber() );

        obj = new Overloaded();
        alert( "This should be 23: " + obj.getNumber() );
    }
}

public class Overloaded {

    private int number = 0;

    public Overloaded( int number ) {
        super();
        this.number = number;
    }

    public Overloaded() {
        this(23);
    }

    public int getNumber() {
        return this.number;
    }
}
vorth commented 1 year ago

This is a critical problem. I believe it is causing a regression in j4ts java.util.TreeMap, which has overloaded constructors, when I build that with JSweet 3.1.0.