add a method to substitute some new array expression

This commit is contained in:
Renaud Pawlak 2021-02-12 17:09:22 +01:00
parent 5da32198e0
commit dc444a7396
2 changed files with 17 additions and 2 deletions

View File

@ -81,6 +81,7 @@ import org.jsweet.transpiler.model.support.ExtendedElementSupport;
import org.jsweet.transpiler.model.support.ForeachLoopElementSupport; import org.jsweet.transpiler.model.support.ForeachLoopElementSupport;
import org.jsweet.transpiler.model.support.ImportElementSupport; import org.jsweet.transpiler.model.support.ImportElementSupport;
import org.jsweet.transpiler.model.support.MethodInvocationElementSupport; import org.jsweet.transpiler.model.support.MethodInvocationElementSupport;
import org.jsweet.transpiler.model.support.NewArrayElementSupport;
import org.jsweet.transpiler.model.support.NewClassElementSupport; import org.jsweet.transpiler.model.support.NewClassElementSupport;
import org.jsweet.transpiler.model.support.UnaryOperatorElementSupport; import org.jsweet.transpiler.model.support.UnaryOperatorElementSupport;
import org.jsweet.transpiler.util.AbstractTreePrinter; import org.jsweet.transpiler.util.AbstractTreePrinter;
@ -5563,8 +5564,10 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
} }
print("]"); print("]");
} else { } else {
print("(s => { let a=[]; while(s-->0) a.push(" + Util.getTypeInitialValue(newArray.elemtype.type) if (!getAdapter().substituteNewArrayWithVariableLength(new NewArrayElementSupport(newArray))) {
+ "); return a; })(").print(newArray.dims.head).print(")"); print("(s => { let a=[]; while(s-->0) a.push(" + Util.getTypeInitialValue(newArray.elemtype.type)
+ "); return a; })(").print(newArray.dims.head).print(")");
}
} }
} else { } else {
print("<any> (function(dims) { " + VAR_DECL_KEYWORD print("<any> (function(dims) { " + VAR_DECL_KEYWORD

View File

@ -57,6 +57,7 @@ import org.jsweet.transpiler.model.ForeachLoopElement;
import org.jsweet.transpiler.model.IdentifierElement; import org.jsweet.transpiler.model.IdentifierElement;
import org.jsweet.transpiler.model.ImportElement; import org.jsweet.transpiler.model.ImportElement;
import org.jsweet.transpiler.model.MethodInvocationElement; import org.jsweet.transpiler.model.MethodInvocationElement;
import org.jsweet.transpiler.model.NewArrayElement;
import org.jsweet.transpiler.model.NewClassElement; import org.jsweet.transpiler.model.NewClassElement;
import org.jsweet.transpiler.model.TypeCastElement; import org.jsweet.transpiler.model.TypeCastElement;
import org.jsweet.transpiler.model.UnaryOperatorElement; import org.jsweet.transpiler.model.UnaryOperatorElement;
@ -901,6 +902,17 @@ public class PrinterAdapter {
return parentAdapter == null ? false : parentAdapter.substituteMethodInvocation(invocation); return parentAdapter == null ? false : parentAdapter.substituteMethodInvocation(invocation);
} }
/**
* Substitutes <em>new array</em> expressions, which length are initialized with a variable.
*
* @param newArray
* the new array being printed
* @return true if substituted
*/
public boolean substituteNewArrayWithVariableLength(NewArrayElement newArray) {
return parentAdapter == null ? false : parentAdapter.substituteNewArrayWithVariableLength(newArray);
}
/** /**
* Substitutes a given type declaration. * Substitutes a given type declaration.
* *