Repository: JetBrains/kotlin
File: core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt (Kotlin)

Code before:
    }

    private fun getAbsentArguments(): Array<Any?> = _absentArguments().clone()

    // See ArgumentGenerator#generate
    internal fun callDefaultMethod(args: Map<KParameter, Any?>, continuationArgument: Continuation<*>?): R {
        val parameters = parameters

        val arguments = getAbsentArguments().apply {
            if (isSuspend) {
                // continuationArgument is tail of arguments
                this[parameters.size] = continuationArgument
            }
        }
        val masks = IntArray((parameters.size + Integer.SIZE - 1) / Integer.SIZE)

        var valueParameterIndex = 0
        var anyOptional = false

        for (parameter in parameters) {
            when {
                args.containsKey(parameter) -> {
                    arguments[parameter.index] = args[parameter]
                }
                // Absent value is already set at _absentArguments
                parameter.isOptional -> {
                    val maskIndex = valueParameterIndex / Integer.SIZE
                    masks[maskIndex] = masks[maskIndex] or (1 shl (valueParameterIndex % Integer.SIZE))
                    anyOptional = true
                }
                parameter.isVararg -> {}
                else -> {
                    throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
                }
            }

            if (parameter.kind == KParameter.Kind.VALUE) {
                valueParameterIndex++
            }
        }

        if (!anyOptional) {
            return call(*arguments)
        }

        val caller = defaultCaller ?: throw KotlinReflectionInternalError("This callable does not support a default call: $descriptor")

        @Suppress("UNCHECKED_CAST")
        return reflectionCall {
            // +1 is argument for DefaultConstructorMarker or MethodHandle
            val mergedArgs = arrayOfNulls<Any?>(arguments.size + masks.size + 1)

Review comment (suggestion):
I assume that the code below is also making the `isSuspend` determination, but is there any reason why it is not using the properties defined in the `KCallable` interface?
https://github.com/JetBrains/kotlin/blob/a366bd5e682133289647895aef2463f345a975cf/core/reflection.jvm/src/kotlin/reflect/jvm/internal/KCallableImpl.kt#L221

I think that need to fix the above code or adapt this implementation to the above code.

Code after:

        for (i in 0 until maskSize) {
            arguments[parameterSize + i] = 0
        }

        arguments
    }

    private fun getAbsentArguments(): Array<Any?> = _absentArguments().clone()

    // See ArgumentGenerator#generate
    internal fun callDefaultMethod(args: Map<KParameter, Any?>, continuationArgument: Continuation<*>?): R {
        val parameters = parameters

        // Optimization for functions without value/receiver parameters.
        if (parameters.isEmpty()) {
            @Suppress("UNCHECKED_CAST")
            return reflectionCall {
                caller.call(if (isSuspend) arrayOf(continuationArgument) else emptyArray()) as R
            }
        }

        val parameterSize = parameters.size + (if (isSuspend) 1 else 0)

        val arguments = getAbsentArguments().apply {
            if (isSuspend) {
                this[parameters.size] = continuationArgument
            }
        }

        var valueParameterIndex = 0
        var anyOptional = false

        for (parameter in parameters) {
            when {
                args.containsKey(parameter) -> {
                    arguments[parameter.index] = args[parameter]
                }
                parameter.isOptional -> {
                    val maskIndex = parameterSize + (valueParameterIndex / Integer.SIZE)
                    arguments[maskIndex] = (arguments[maskIndex] as Int) or (1 shl (valueParameterIndex % Integer.SIZE))
                    anyOptional = true
                }
                parameter.isVararg -> {}
                else -> {
                    throw IllegalArgumentException("No argument provided for a required parameter: $parameter")
                }
            }

            if (parameter.kind == KParameter.Kind.VALUE) {
                valueParameterIndex++