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.ImportElementSupport;
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.UnaryOperatorElementSupport;
import org.jsweet.transpiler.util.AbstractTreePrinter;
@ -5563,9 +5564,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
}
print("]");
} else {
if (!getAdapter().substituteNewArrayWithVariableLength(new NewArrayElementSupport(newArray))) {
print("(s => { let a=[]; while(s-->0) a.push(" + Util.getTypeInitialValue(newArray.elemtype.type)
+ "); return a; })(").print(newArray.dims.head).print(")");
}
}
} else {
print("<any> (function(dims) { " + VAR_DECL_KEYWORD
+ " allocate = function(dims) { if (dims.length === 0) { return "

View File

@ -57,6 +57,7 @@ import org.jsweet.transpiler.model.ForeachLoopElement;
import org.jsweet.transpiler.model.IdentifierElement;
import org.jsweet.transpiler.model.ImportElement;
import org.jsweet.transpiler.model.MethodInvocationElement;
import org.jsweet.transpiler.model.NewArrayElement;
import org.jsweet.transpiler.model.NewClassElement;
import org.jsweet.transpiler.model.TypeCastElement;
import org.jsweet.transpiler.model.UnaryOperatorElement;
@ -901,6 +902,17 @@ public class PrinterAdapter {
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.
*