Pass earlier scripts as array, removes 255-lines repl limitation

fixes #KT-10060
This commit is contained in:
Ilya Chernikov
2017-05-02 12:34:52 +02:00
parent 15ccd28e2e
commit 732367e671
5 changed files with 27 additions and 22 deletions
@@ -158,7 +158,7 @@ public class ScriptCodegen extends MemberCodegen<KtScript> {
iv.load(0, classType); iv.load(0, classType);
int valueParamStart = context.getEarlierScripts().size() + 1; int valueParamStart = context.getEarlierScripts().isEmpty() ? 1 : 2; // this + array of earlier scripts if not empty
List<ValueParameterDescriptor> valueParameters = scriptDescriptor.getUnsubstitutedPrimaryConstructor().getValueParameters(); List<ValueParameterDescriptor> valueParameters = scriptDescriptor.getUnsubstitutedPrimaryConstructor().getValueParameters();
for (ValueParameterDescriptor superclassParam: ctorDesc.getValueParameters()) { for (ValueParameterDescriptor superclassParam: ctorDesc.getValueParameters()) {
@@ -185,18 +185,20 @@ public class ScriptCodegen extends MemberCodegen<KtScript> {
FrameMap frameMap = new FrameMap(); FrameMap frameMap = new FrameMap();
frameMap.enterTemp(OBJECT_TYPE); frameMap.enterTemp(OBJECT_TYPE);
for (ScriptDescriptor importedScript : context.getEarlierScripts()) {
frameMap.enter(importedScript, OBJECT_TYPE);
}
int offset = 1; if (!context.getEarlierScripts().isEmpty()) {
int scriptsParamIndex = frameMap.enterTemp(AsmUtil.getArrayType(OBJECT_TYPE));
for (ScriptDescriptor earlierScript : context.getEarlierScripts()) { int earlierScriptIndex = 0;
Type earlierClassType = typeMapper.mapClass(earlierScript); for (ScriptDescriptor earlierScript : context.getEarlierScripts()) {
iv.load(0, classType); Type earlierClassType = typeMapper.mapClass(earlierScript);
iv.load(offset, earlierClassType); iv.load(0, classType);
offset += earlierClassType.getSize(); iv.load(scriptsParamIndex, earlierClassType);
iv.putfield(classType.getInternalName(), context.getScriptFieldName(earlierScript), earlierClassType.getDescriptor()); iv.aconst(earlierScriptIndex++);
iv.aload(OBJECT_TYPE);
iv.checkcast(earlierClassType);
iv.putfield(classType.getInternalName(), context.getScriptFieldName(earlierScript), earlierClassType.getDescriptor());
}
} }
ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, methodContext, state, this); ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, methodContext, state, this);
@@ -1477,8 +1477,8 @@ public class KotlinTypeMapper {
sw.writeParametersStart(); sw.writeParametersStart();
for (ScriptDescriptor importedScript : importedScripts) { if (importedScripts.size() > 0) {
writeParameter(sw, importedScript.getDefaultType(), /* callableDescriptor = */ null); writeParameter(sw, DescriptorUtilsKt.getModule(script).getBuiltIns().getArray().getDefaultType(), null);
} }
for (ValueParameterDescriptor valueParameter : script.getUnsubstitutedPrimaryConstructor().getValueParameters()) { for (ValueParameterDescriptor valueParameter : script.getUnsubstitutedPrimaryConstructor().getValueParameters()) {
@@ -74,10 +74,14 @@ open class GenericReplEvaluator(val baseClasspath: Iterable<File>,
val useScriptArgs = currentScriptArgs?.scriptArgs val useScriptArgs = currentScriptArgs?.scriptArgs
val useScriptArgsTypes = currentScriptArgs?.scriptArgsTypes?.map { it.java } val useScriptArgsTypes = currentScriptArgs?.scriptArgsTypes?.map { it.java }
val constructorParams: Array<Class<*>> = (historyActor.effectiveHistory.map { it.klass.java } + val hasHistory = historyActor.effectiveHistory.isNotEmpty()
(useScriptArgs?.mapIndexed { i, it -> useScriptArgsTypes?.getOrNull(i) ?: it?.javaClass ?: Any::class.java } ?: emptyList())
).toTypedArray() val constructorParams: Array<Class<*>> = (if (hasHistory) arrayOf<Class<*>>(Array<Any>::class.java) else emptyArray<Class<*>>()) +
val constructorArgs: Array<Any?> = (historyActor.effectiveHistory.map { it.instance } + useScriptArgs.orEmpty()).toTypedArray() (useScriptArgs?.mapIndexed { i, it -> useScriptArgsTypes?.getOrNull(i) ?: it?.javaClass ?: Any::class.java } ?: emptyList())
val constructorArgs: Array<out Any?> = if (hasHistory) arrayOf(historyActor.effectiveHistory.map { it.instance }.takeIf { it.isNotEmpty() }?.toTypedArray(),
*(useScriptArgs.orEmpty()))
else useScriptArgs.orEmpty()
// TODO: try/catch ? // TODO: try/catch ?
val scriptInstanceConstructor = scriptClass.getConstructor(*constructorParams) val scriptInstanceConstructor = scriptClass.getConstructor(*constructorParams)
@@ -51,7 +51,7 @@ class ReplInterpreter(
hasErrors = false hasErrors = false
} }
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation) { override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) {
val msg = messageRenderer.render(severity, message, location) val msg = messageRenderer.render(severity, message, location)
with (replConfiguration.writer) { with (replConfiguration.writer) {
when (severity) { when (severity) {
@@ -146,15 +146,14 @@ class GenericReplTest : TestCase() {
} }
} }
// #KT-10060, TODO: fix and uncomment/rename @Test
// @Test fun test256Evals() {
fun ignored_test256Evals() {
TestRepl().use { repl -> TestRepl().use { repl ->
val state = repl.createState() val state = repl.createState()
repl.compileAndEval(state, ReplCodeLine(0, 0, "val x0 = 0")) repl.compileAndEval(state, ReplCodeLine(0, 0, "val x0 = 0"))
val evals = 255 val evals = 256
for (i in 1..evals) { for (i in 1..evals) {
repl.compileAndEval(state, ReplCodeLine(i, 0, "val x$i = x${i-1} + 1")) repl.compileAndEval(state, ReplCodeLine(i, 0, "val x$i = x${i-1} + 1"))
} }