Replace explicit returnLabel call to check call inside try interpreter

This commit is contained in:
Ivan Kylchik
2020-04-15 13:49:31 +03:00
parent a14e8e4c28
commit 1e82975a7c
@@ -272,8 +272,8 @@ class IrInterpreter(irModule: IrModuleFragment) {
val defaultValues = expression.symbol.owner.valueParameters.map { it.defaultValue?.expression } val defaultValues = expression.symbol.owner.valueParameters.map { it.defaultValue?.expression }
return stack.newFrame(asSubFrame = true, initPool = pool) { return stack.newFrame(asSubFrame = true, initPool = pool) {
for ((i, valueArgument) in valueArguments.withIndex()) { // TODO zip for (i in valueArguments.indices) {
(valueArgument ?: defaultValues[i])!!.interpret().check { return@newFrame it } (valueArguments[i] ?: defaultValues[i])!!.interpret().check { return@newFrame it }
with(Variable(valueParametersDescriptors[i], stack.popReturnValue())) { with(Variable(valueParametersDescriptors[i], stack.popReturnValue())) {
stack.addVar(this) stack.addVar(this)
pool.add(this) pool.add(this)
@@ -636,18 +636,20 @@ class IrInterpreter(irModule: IrModuleFragment) {
} }
private suspend fun interpretTry(expression: IrTry): ExecutionResult { private suspend fun interpretTry(expression: IrTry): ExecutionResult {
var executionResult = expression.tryResult.interpret() try {
if (executionResult.returnLabel == ReturnLabel.EXCEPTION) { expression.tryResult.interpret().check(ReturnLabel.EXCEPTION) { return it } // if not exception -> return
val exception = stack.peekReturnValue() as ExceptionState val exception = stack.peekReturnValue() as ExceptionState
for (catchBlock in expression.catches) { for (catchBlock in expression.catches) {
if (exception.isSubtypeOf(catchBlock.catchParameter.type.classOrNull!!.owner)) { if (exception.isSubtypeOf(catchBlock.catchParameter.type.classOrNull!!.owner)) {
executionResult = catchBlock.interpret() catchBlock.interpret().check { return it }
break break
} }
} }
} finally {
expression.finallyExpression?.interpret()?.check { return it }
} }
// TODO check flow correctness; should I return finally result code if in catch there was an exception?
return expression.finallyExpression?.interpret() ?: executionResult return Next
} }
private suspend fun interpretCatch(expression: IrCatch): ExecutionResult { private suspend fun interpretCatch(expression: IrCatch): ExecutionResult {