Skip to content

Commit

Permalink
[JS IR] Drop last null arguments in calls of external functions
Browse files Browse the repository at this point in the history
^KT-40090 fixed
  • Loading branch information
ilgonmic committed Nov 23, 2020
1 parent 5c731c6 commit fe3030c
Showing 1 changed file with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,35 @@ fun translateCall(
}
}

fun translateCallArguments(expression: IrMemberAccessExpression<*>, context: JsGenerationContext, transformer: IrElementToJsExpressionTransformer): List<JsExpression> {
fun translateCallArguments(
expression: IrMemberAccessExpression<*>,
context: JsGenerationContext,
transformer: IrElementToJsExpressionTransformer,
): List<JsExpression> {
val size = expression.valueArgumentsCount

val arguments = (0 until size).mapTo(ArrayList(size)) { index ->
val argument = expression.getValueArgument(index)
val result = argument?.accept(transformer, context)
if (result == null) {
if (context.staticContext.backendContext.es6mode) return@mapTo JsPrefixOperation(JsUnaryOperator.VOID, JsIntLiteral(2))

assert(expression is IrFunctionAccessExpression && expression.symbol.owner.isExternalOrInheritedFromExternal())
JsPrefixOperation(JsUnaryOperator.VOID, JsIntLiteral(1))
} else
result
}
val validWithNullArgs = expression.validWithNullArgs()
val arguments = (0 until size)
.mapTo(ArrayList(size)) { index ->
val argument = expression.getValueArgument(index)
argument?.accept(transformer, context)
}
.onEach { result ->
if (result == null) {
assert(validWithNullArgs)
}
}
.dropLastWhile { it == null }
.map { it ?: JsPrefixOperation(JsUnaryOperator.VOID, JsIntLiteral(1)) }

return if (expression.symbol.isSuspend) {
arguments + context.continuation
} else arguments
}

private fun IrMemberAccessExpression<*>.validWithNullArgs() =
this is IrFunctionAccessExpression && symbol.owner.isExternalOrInheritedFromExternal()

fun JsStatement.asBlock() = this as? JsBlock ?: JsBlock(this)

fun defineProperty(receiver: JsExpression, name: String, value: () -> JsExpression): JsInvocation {
Expand Down

0 comments on commit fe3030c

Please sign in to comment.