Update Kotlin with tests to 1.3.0-dev-391 (#2037)
This commit is contained in:
committed by
Nikolay Igotti
parent
81b7109856
commit
996ec4a6fd
+8
-5
@@ -748,11 +748,14 @@ class StubGenerator(
|
||||
|
||||
narrowedValue.toString()
|
||||
} else {
|
||||
val narrowedValue: Any = when (size) {
|
||||
1 -> value.toUByte()
|
||||
2 -> value.toUShort()
|
||||
4 -> value.toUInt()
|
||||
8 -> value.toULong()
|
||||
// Note: stub generator is built and run with different ABI versions,
|
||||
// so Kotlin unsigned types can't be used here currently.
|
||||
|
||||
val narrowedValue: String = when (size) {
|
||||
1 -> (value and 0xFF).toString()
|
||||
2 -> (value and 0xFFFF).toString()
|
||||
4 -> (value and 0xFFFFFFFF).toString()
|
||||
8 -> java.lang.Long.toUnsignedString(value)
|
||||
else -> return null
|
||||
}
|
||||
|
||||
|
||||
@@ -150,9 +150,6 @@ def commonSrc = file('build/stdlib')
|
||||
|
||||
task unzipStdlibSources(type: Copy) {
|
||||
from (zipTree(configurations.kotlin_common_stdlib_src.singleFile)) {
|
||||
exclude 'generated/_Sequences.kt'
|
||||
exclude 'kotlin/collections/Sequences.kt'
|
||||
exclude 'kotlin/collections/SlidingWindow.kt'
|
||||
include 'generated/**/*.kt'
|
||||
include 'kotlin/**/*.kt'
|
||||
}
|
||||
@@ -185,6 +182,7 @@ targetList.each { target ->
|
||||
'-produce', 'library', '-module-name', 'stdlib', '-XXLanguage:+AllowContractsForCustomFunctions',
|
||||
'-Xmulti-platform', '-Xuse-experimental=kotlin.Experimental',
|
||||
'-Xuse-experimental=kotlin.contracts.ExperimentalContracts',
|
||||
'-Xuse-experimental=kotlin.ExperimentalMultiplatform',
|
||||
commonSrc.absolutePath,
|
||||
project(':Interop:Runtime').file('src/main/kotlin'),
|
||||
project(':Interop:Runtime').file('src/native/kotlin'),
|
||||
|
||||
+7
-5
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.backend.common.DumpIrTreeWithDescriptorsVisitor
|
||||
import org.jetbrains.kotlin.backend.common.ReflectionTypes
|
||||
import org.jetbrains.kotlin.backend.common.validateIrModule
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.*
|
||||
import org.jetbrains.kotlin.backend.common.descriptors.DescriptorsFactory
|
||||
import org.jetbrains.kotlin.backend.konan.ir.KonanIr
|
||||
import org.jetbrains.kotlin.backend.konan.library.KonanLibraryWriter
|
||||
import org.jetbrains.kotlin.backend.konan.library.LinkData
|
||||
@@ -71,14 +70,17 @@ internal class SpecialDeclarationsFactory(val context: Context) {
|
||||
val outerClass = innerClass.parent as? IrClass
|
||||
?: throw AssertionError("No containing class for inner class ${innerClass.descriptor}")
|
||||
|
||||
val receiver = ReceiverParameterDescriptorImpl(innerClass.descriptor, ImplicitClassReceiver(innerClass.descriptor))
|
||||
val receiver = ReceiverParameterDescriptorImpl(
|
||||
innerClass.descriptor,
|
||||
ImplicitClassReceiver(innerClass.descriptor, null),
|
||||
Annotations.EMPTY
|
||||
)
|
||||
val descriptor = PropertyDescriptorImpl.create(
|
||||
innerClass.descriptor, Annotations.EMPTY, Modality.FINAL,
|
||||
Visibilities.PRIVATE, false, "this$0".synthesizedName, CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
SourceElement.NO_SOURCE, false, false, false, false, false, false
|
||||
).apply {
|
||||
val receiverType: KotlinType? = null
|
||||
this.setType(outerClass.descriptor.defaultType, emptyList(), receiver, receiverType)
|
||||
this.setType(outerClass.descriptor.defaultType, emptyList(), receiver, null)
|
||||
initialize(null, null)
|
||||
}
|
||||
|
||||
@@ -261,7 +263,7 @@ internal class SpecialDeclarationsFactory(val context: Context) {
|
||||
}
|
||||
|
||||
internal class Context(config: KonanConfig) : KonanBackendContext(config) {
|
||||
override val descriptorsFactory: DescriptorsFactory
|
||||
override val declarationFactory
|
||||
get() = TODO("not implemented")
|
||||
|
||||
override fun getClass(fqName: FqName): ClassDescriptor {
|
||||
|
||||
+6
-3
@@ -107,13 +107,16 @@ internal class EnumSpecialDeclarationsFactory(val context: Context) {
|
||||
|
||||
private fun createEnumValuesField(enumClassDescriptor: ClassDescriptor, implObjectDescriptor: ClassDescriptor): PropertyDescriptor {
|
||||
val valuesArrayType = context.builtIns.getArrayType(Variance.INVARIANT, enumClassDescriptor.defaultType)
|
||||
val receiver = ReceiverParameterDescriptorImpl(implObjectDescriptor, ImplicitClassReceiver(implObjectDescriptor))
|
||||
val receiver = ReceiverParameterDescriptorImpl(
|
||||
implObjectDescriptor,
|
||||
ImplicitClassReceiver(implObjectDescriptor, null),
|
||||
Annotations.EMPTY
|
||||
)
|
||||
return PropertyDescriptorImpl.create(implObjectDescriptor, Annotations.EMPTY, Modality.FINAL, Visibilities.PUBLIC,
|
||||
false, "VALUES".synthesizedName, CallableMemberDescriptor.Kind.SYNTHESIZED, SourceElement.NO_SOURCE,
|
||||
false, false, false, false, false, false).apply {
|
||||
|
||||
val receiverType: KotlinType? = null
|
||||
this.setType(valuesArrayType, emptyList(), receiver, receiverType)
|
||||
this.setType(valuesArrayType, emptyList(), receiver, null)
|
||||
this.initialize(null, null)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
package org.jetbrains.kotlin.backend.konan.descriptors
|
||||
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.descriptors.SharedVariablesManager
|
||||
import org.jetbrains.kotlin.backend.common.ir.SharedVariablesManager
|
||||
import org.jetbrains.kotlin.backend.konan.KonanBackendContext
|
||||
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
|
||||
+28
@@ -13,7 +13,10 @@ import org.jetbrains.kotlin.builtins.isFunctionType
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
|
||||
import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.resolve.DescriptorFactory
|
||||
import org.jetbrains.kotlin.resolve.OverridingUtil
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
@@ -174,3 +177,28 @@ val ClassDescriptor.enumEntries: List<ClassDescriptor>
|
||||
|
||||
internal val DeclarationDescriptor.isExpectMember: Boolean
|
||||
get() = this is MemberDescriptor && this.isExpect
|
||||
|
||||
internal fun KotlinType?.createExtensionReceiver(owner: CallableDescriptor): ReceiverParameterDescriptor? =
|
||||
DescriptorFactory.createExtensionReceiverParameterForCallable(
|
||||
owner,
|
||||
this,
|
||||
Annotations.EMPTY
|
||||
)
|
||||
|
||||
internal fun FunctionDescriptorImpl.initialize(
|
||||
extensionReceiverType: KotlinType?,
|
||||
dispatchReceiverParameter: ReceiverParameterDescriptor?,
|
||||
typeParameters: List<TypeParameterDescriptor>,
|
||||
unsubstitutedValueParameters: List<ValueParameterDescriptor>,
|
||||
unsubstitutedReturnType: KotlinType?,
|
||||
modality: Modality?,
|
||||
visibility: Visibility
|
||||
): FunctionDescriptorImpl = this.initialize(
|
||||
extensionReceiverType.createExtensionReceiver(this),
|
||||
dispatchReceiverParameter,
|
||||
typeParameters,
|
||||
unsubstitutedValueParameters,
|
||||
unsubstitutedReturnType,
|
||||
modality,
|
||||
visibility
|
||||
)
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.metadata.konan.KonanProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor
|
||||
|
||||
val DeserializedPropertyDescriptor.backingField: PropertyDescriptor?
|
||||
val DeserializedPropertyDescriptor.konanBackingField: PropertyDescriptor?
|
||||
get() =
|
||||
if (this.proto.getExtension(KonanProtoBuf.hasBackingField))
|
||||
this
|
||||
|
||||
+29
-2
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
// This is what Context collects about IR.
|
||||
@@ -395,13 +396,39 @@ internal class KonanSymbols(context: Context, val symbolTable: SymbolTable, val
|
||||
.filterNot { it.isExpect }.single().getter!!
|
||||
)
|
||||
|
||||
val successOrFailure = symbolTable.referenceClass(
|
||||
val kotlinResult = symbolTable.referenceClass(
|
||||
builtIns.builtInsModule.findClassAcrossModuleDependencies(
|
||||
ClassId.topLevel(FqName("kotlin.SuccessOrFailure")))!!
|
||||
ClassId.topLevel(FqName("kotlin.Result")))!!
|
||||
)
|
||||
|
||||
val kotlinResultGetOrThrow = symbolTable.referenceSimpleFunction(
|
||||
builtInsPackage("kotlin")
|
||||
.getContributedFunctions(Name.identifier("getOrThrow"), NoLookupLocation.FROM_BACKEND)
|
||||
.single {
|
||||
it.extensionReceiverParameter?.type?.constructor?.declarationDescriptor == kotlinResult.descriptor
|
||||
}
|
||||
)
|
||||
|
||||
val refClass = symbolTable.referenceClass(context.getInternalClass("Ref"))
|
||||
|
||||
val isInitializedPropertyDescriptor = builtInsPackage("kotlin")
|
||||
.getContributedVariables(Name.identifier("isInitialized"), NoLookupLocation.FROM_BACKEND).single {
|
||||
it.extensionReceiverParameter.let {
|
||||
it != null && TypeUtils.getClassDescriptor(it.type) == context.reflectionTypes.kProperty0
|
||||
} && !it.isExpect
|
||||
}
|
||||
|
||||
val isInitializedGetterDescriptor = isInitializedPropertyDescriptor.getter!!
|
||||
|
||||
val kFunctionImpl = symbolTable.referenceClass(context.reflectionTypes.kFunctionImpl)
|
||||
|
||||
val kProperty0Impl = symbolTable.referenceClass(context.reflectionTypes.kProperty0Impl)
|
||||
val kProperty1Impl = symbolTable.referenceClass(context.reflectionTypes.kProperty1Impl)
|
||||
val kProperty2Impl = symbolTable.referenceClass(context.reflectionTypes.kProperty2Impl)
|
||||
val kMutableProperty0Impl = symbolTable.referenceClass(context.reflectionTypes.kMutableProperty0Impl)
|
||||
val kMutableProperty1Impl = symbolTable.referenceClass(context.reflectionTypes.kMutableProperty1Impl)
|
||||
val kMutableProperty2Impl = symbolTable.referenceClass(context.reflectionTypes.kMutableProperty2Impl)
|
||||
|
||||
val kLocalDelegatedPropertyImpl = symbolTable.referenceClass(context.reflectionTypes.kLocalDelegatedPropertyImpl)
|
||||
val kLocalDelegatedMutablePropertyImpl = symbolTable.referenceClass(context.reflectionTypes.kLocalDelegatedMutablePropertyImpl)
|
||||
|
||||
|
||||
+2
-2
@@ -6,7 +6,7 @@
|
||||
package org.jetbrains.kotlin.backend.konan.irasdescriptors
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.atMostOne
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.backingField
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.konanBackingField
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.isInterface
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
@@ -85,7 +85,7 @@ val IrProperty.konanBackingField: IrField?
|
||||
assert(this.isReal)
|
||||
this.backingField?.let { return it }
|
||||
|
||||
(this.descriptor as? DeserializedPropertyDescriptor)?.backingField?.let { backingFieldDescriptor ->
|
||||
(this.descriptor as? DeserializedPropertyDescriptor)?.konanBackingField?.let { backingFieldDescriptor ->
|
||||
val result = IrFieldImpl(
|
||||
this.startOffset,
|
||||
this.endOffset,
|
||||
|
||||
+2
-2
@@ -1442,7 +1442,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
||||
|
||||
private fun evaluateGetField(value: IrGetField): LLVMValueRef {
|
||||
context.log{"evaluateGetField : ${ir2string(value)}"}
|
||||
if (value.descriptor.dispatchReceiverParameter != null) {
|
||||
if (!value.symbol.owner.isStatic) {
|
||||
val thisPtr = evaluateExpression(value.receiver!!)
|
||||
return functionGenerationContext.loadSlot(
|
||||
fieldPtrOfClass(thisPtr, value.symbol.owner), value.descriptor.isVar())
|
||||
@@ -1467,7 +1467,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
|
||||
private fun evaluateSetField(value: IrSetField): LLVMValueRef {
|
||||
context.log{"evaluateSetField : ${ir2string(value)}"}
|
||||
val valueToAssign = evaluateExpression(value.value)
|
||||
if (value.descriptor.dispatchReceiverParameter != null) {
|
||||
if (!value.symbol.owner.isStatic) {
|
||||
val thisPtr = evaluateExpression(value.receiver!!)
|
||||
assert(thisPtr.type == codegen.kObjHeaderPtr) {
|
||||
LLVMPrintTypeToString(thisPtr.type)?.toKString().toString()
|
||||
|
||||
+30
-16
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
@@ -66,22 +67,35 @@ internal class WorkersBridgesBuilding(val context: Context) : DeclarationContain
|
||||
val jobDescriptor = job.descriptor
|
||||
val arg = jobDescriptor.valueParameters[0]
|
||||
if (!::runtimeJobFunction.isInitialized) {
|
||||
val runtimeJobDescriptor = jobDescriptor.newCopyBuilder()
|
||||
.setReturnType(nullableAnyType)
|
||||
.setValueParameters(listOf(ValueParameterDescriptorImpl(
|
||||
containingDeclaration = jobDescriptor,
|
||||
original = null,
|
||||
index = 0,
|
||||
annotations = Annotations.EMPTY,
|
||||
name = arg.name,
|
||||
outType = nullableAnyType,
|
||||
declaresDefaultValue = arg.declaresDefaultValue(),
|
||||
isCrossinline = arg.isCrossinline,
|
||||
isNoinline = arg.isNoinline,
|
||||
varargElementType = arg.varargElementType,
|
||||
source = arg.source
|
||||
)))
|
||||
.build()!!
|
||||
val runtimeJobDescriptor = SimpleFunctionDescriptorImpl.create(
|
||||
jobDescriptor.containingDeclaration,
|
||||
jobDescriptor.annotations,
|
||||
jobDescriptor.name,
|
||||
jobDescriptor.kind,
|
||||
jobDescriptor.source
|
||||
).apply {
|
||||
initialize(
|
||||
null,
|
||||
null,
|
||||
emptyList(),
|
||||
listOf(ValueParameterDescriptorImpl(
|
||||
containingDeclaration = this,
|
||||
original = null,
|
||||
index = 0,
|
||||
annotations = Annotations.EMPTY,
|
||||
name = arg.name,
|
||||
outType = nullableAnyType,
|
||||
declaresDefaultValue = arg.declaresDefaultValue(),
|
||||
isCrossinline = arg.isCrossinline,
|
||||
isNoinline = arg.isNoinline,
|
||||
varargElementType = arg.varargElementType,
|
||||
source = arg.source
|
||||
)),
|
||||
nullableAnyType,
|
||||
jobDescriptor.modality,
|
||||
jobDescriptor.visibility
|
||||
)
|
||||
}
|
||||
|
||||
runtimeJobFunction = IrFunctionImpl(
|
||||
jobFunction.startOffset,
|
||||
|
||||
+3
-1
@@ -9,6 +9,8 @@ import org.jetbrains.kotlin.backend.common.*
|
||||
import org.jetbrains.kotlin.backend.common.IrElementVisitorVoidWithContext
|
||||
import org.jetbrains.kotlin.backend.common.lower.SimpleMemberScope
|
||||
import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.createExtensionReceiver
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.initialize
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.impl.*
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
@@ -366,7 +368,7 @@ internal class DeepCopyIrTreeWithDescriptors(val targetDescriptor: FunctionDescr
|
||||
/* outType = */ substituteType(oldDescriptor.type)!!,
|
||||
/* typeParameters = */ oldDescriptor.typeParameters,
|
||||
/* dispatchReceiverParameter = */ (containingDeclaration as ClassDescriptor).thisAsReceiverParameter,
|
||||
/* receiverType = */ substituteType(oldDescriptor.extensionReceiverParameter?.type))
|
||||
/* extensionReceiverParamter = */ substituteType(oldDescriptor.extensionReceiverParameter?.type).createExtensionReceiver(this))
|
||||
|
||||
initialize(
|
||||
/* getter = */ oldDescriptor.getter?.let { copyPropertyGetterDescriptor(it, this) },
|
||||
|
||||
+3
-5
@@ -12,12 +12,11 @@ import org.jetbrains.kotlin.backend.common.lower.irBlock
|
||||
import org.jetbrains.kotlin.backend.konan.InteropFqNames
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName
|
||||
import org.jetbrains.kotlin.backend.konan.KonanBackendContext
|
||||
import org.jetbrains.kotlin.backend.konan.getInlinedClass
|
||||
import org.jetbrains.kotlin.backend.konan.irasdescriptors.typeWithStarProjections
|
||||
import org.jetbrains.kotlin.backend.konan.isObjCClass
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||
@@ -314,13 +313,12 @@ internal class PropertyDelegationLowering(val context: KonanBackendContext) : Fi
|
||||
|
||||
private fun createKPropertiesFieldDescriptor(containingDeclaration: DeclarationDescriptor, fieldType: KotlinType): PropertyDescriptorImpl {
|
||||
return PropertyDescriptorImpl.create(containingDeclaration,
|
||||
AnnotationsImpl(listOf(AnnotationDescriptorImpl(context.ir.symbols.sharedImmutable.defaultType,
|
||||
Annotations.create(listOf(AnnotationDescriptorImpl(context.ir.symbols.sharedImmutable.defaultType,
|
||||
emptyMap(), SourceElement.NO_SOURCE))), Modality.FINAL, Visibilities.PRIVATE,
|
||||
false, "KPROPERTIES".synthesizedName, CallableMemberDescriptor.Kind.SYNTHESIZED, SourceElement.NO_SOURCE,
|
||||
false, false, false, false, false, false).apply {
|
||||
|
||||
val receiverType: KotlinType? = null
|
||||
this.setType(fieldType, emptyList(), null, receiverType)
|
||||
this.setType(fieldType, emptyList(), null, null)
|
||||
this.initialize(null, null)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -257,7 +257,7 @@ internal class EnumClassLowering(val context: Context) : ClassLoweringPass {
|
||||
val loweredArgument = loweredEnumConstructor.valueParameters[argument.loweredIndex()]
|
||||
val body = irConstructor.getDefault(loweredArgument)!!.deepCopyWithVariables()
|
||||
body.transformChildrenVoid(ParameterMapper(constructor))
|
||||
body.accept(SetDeclarationsParentVisitor, irConstructor)
|
||||
body.accept(SetDeclarationsParentVisitor, constructor)
|
||||
constructor.putDefault(constructorDescriptor.valueParameters[loweredArgument.index], body)
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.resolve.multiplatform.ExpectedActualResolver
|
||||
|
||||
@@ -56,6 +57,14 @@ internal class ExpectDeclarationsRemoving(val context: Context) : FileLoweringPa
|
||||
val index = declaration.index
|
||||
assert(function.valueParameters[index] == declaration)
|
||||
|
||||
if (function is IrConstructor &&
|
||||
ExpectedActualDeclarationChecker.isOptionalAnnotationClass(
|
||||
function.descriptor.constructedClass
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
function.findActualForExpected().valueParameters[index].defaultValue = defaultValue.also {
|
||||
it.expression = it.expression.remapExpectValueSymbols()
|
||||
}
|
||||
|
||||
+3
-1
@@ -65,7 +65,9 @@ internal class FunctionInlining(val context: Context): IrElementTransformerVoidW
|
||||
|
||||
val irCall = super.visitCall(expression) as IrCall
|
||||
val functionDescriptor = irCall.descriptor
|
||||
if (!functionDescriptor.needsInlining) return irCall // This call does not need inlining.
|
||||
if (!functionDescriptor.needsInlining || functionDescriptor == context.ir.symbols.isInitializedGetterDescriptor) {
|
||||
return irCall // This call does not need inlining.
|
||||
}
|
||||
|
||||
val functionDeclaration = getFunctionDeclaration(functionDescriptor) // Get declaration of the function to be inlined.
|
||||
if (functionDeclaration == null) { // We failed to get the declaration.
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ internal class InnerClassLowering(val context: Context) : ClassLoweringPass {
|
||||
}
|
||||
|
||||
private fun lowerConstructor(irConstructor: IrConstructor): IrConstructor {
|
||||
if (irConstructor.callsSuper()) {
|
||||
if (irConstructor.callsSuper(context.irBuiltIns)) {
|
||||
// Initializing constructor: initialize 'this.this$0' with '$outer'.
|
||||
val blockBody = irConstructor.body as? IrBlockBody
|
||||
?: throw AssertionError("Unexpected constructor body: ${irConstructor.body}")
|
||||
|
||||
+1
-2
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
@@ -425,7 +424,7 @@ internal class InteropLoweringPart1(val context: Context) : IrBuildingTransforme
|
||||
SourceElement.NO_SOURCE
|
||||
)
|
||||
|
||||
return AnnotationsImpl(listOf(annotation))
|
||||
return Annotations.create(listOf(annotation))
|
||||
}
|
||||
|
||||
private fun checkKotlinObjCClass(irClass: IrClass) {
|
||||
|
||||
+3
-16
@@ -5,14 +5,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.konan.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||
import org.jetbrains.kotlin.backend.common.lower.*
|
||||
import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.resolveFakeOverride
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.VariableDescriptor
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
@@ -27,25 +26,13 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
|
||||
internal class LateinitLowering(
|
||||
val context: CommonBackendContext,
|
||||
val context: Context,
|
||||
private val generateParameterNameInAssertion: Boolean = false
|
||||
) : FileLoweringPass {
|
||||
|
||||
private val KOTLIN_FQ_NAME = FqName("kotlin")
|
||||
private val kotlinPackageScope = context.ir.irModule.descriptor.getPackage(KOTLIN_FQ_NAME).memberScope
|
||||
private val isInitializedPropertyDescriptor = kotlinPackageScope
|
||||
.getContributedVariables(Name.identifier("isInitialized"), NoLookupLocation.FROM_BACKEND).single {
|
||||
it.extensionReceiverParameter.let {
|
||||
it != null && TypeUtils.getClassDescriptor(it.type) == context.reflectionTypes.kProperty0
|
||||
} && !it.isExpect
|
||||
}
|
||||
private val isInitializedGetterDescriptor = isInitializedPropertyDescriptor.getter!!
|
||||
private val isInitializedGetterDescriptor = context.ir.symbols.isInitializedGetterDescriptor
|
||||
|
||||
override fun lower(irFile: IrFile) {
|
||||
val lateinitPropertyToField = mutableMapOf<PropertyDescriptor, IrField>()
|
||||
|
||||
+10
-14
@@ -1200,26 +1200,22 @@ internal class SuspendFunctionsLowering(val context: Context): FileLoweringPass
|
||||
}
|
||||
}
|
||||
|
||||
private fun IrBuilderWithScope.irGetOrThrow(successOrFailure: IrExpression): IrExpression {
|
||||
// TODO: consider inlining getOrThrow function body here.
|
||||
val successOrFailureClass = symbols.successOrFailure.owner
|
||||
val getOrThrow = successOrFailureClass.simpleFunctions().single { it.name.asString() == "getOrThrow" }
|
||||
return irCall(getOrThrow).apply {
|
||||
dispatchReceiver = successOrFailure
|
||||
}
|
||||
}
|
||||
private fun IrBuilderWithScope.irGetOrThrow(result: IrExpression): IrExpression =
|
||||
irCall(symbols.kotlinResultGetOrThrow.owner).apply {
|
||||
extensionReceiver = result
|
||||
} // TODO: consider inlining getOrThrow function body here.
|
||||
|
||||
private fun IrBuilderWithScope.irExceptionOrNull(successOrFailure: IrExpression): IrExpression {
|
||||
val successOrFailureClass = symbols.successOrFailure.owner
|
||||
val exceptionOrNull = successOrFailureClass.simpleFunctions().single { it.name.asString() == "exceptionOrNull" }
|
||||
private fun IrBuilderWithScope.irExceptionOrNull(result: IrExpression): IrExpression {
|
||||
val resultClass = symbols.kotlinResult.owner
|
||||
val exceptionOrNull = resultClass.simpleFunctions().single { it.name.asString() == "exceptionOrNull" }
|
||||
return irCall(exceptionOrNull).apply {
|
||||
dispatchReceiver = successOrFailure
|
||||
dispatchReceiver = result
|
||||
}
|
||||
}
|
||||
|
||||
fun IrBlockBodyBuilder.irSuccess(value: IrExpression): IrCall {
|
||||
val createSuccessOrFailure = symbols.successOrFailure.owner.constructors.single { it.isPrimary }
|
||||
return irCall(createSuccessOrFailure).apply {
|
||||
val createResult = symbols.kotlinResult.owner.constructors.single { it.isPrimary }
|
||||
return irCall(createResult).apply {
|
||||
putValueArgument(0, value)
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.backend.konan.serialization
|
||||
import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.KonanIrDeserializationException
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.contributedMethods
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.createExtensionReceiver
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.findPackage
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.isFunctionInvoke
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -182,7 +183,8 @@ internal class IrDescriptorDeserializer(val context: Context,
|
||||
setOriginal(originalDescriptor)
|
||||
setReturnType(deserializeKotlinType(proto.type))
|
||||
if (proto.hasExtensionReceiverType()) {
|
||||
setExtensionReceiverType(deserializeKotlinType(proto.extensionReceiverType))
|
||||
val extensionReceiverType = deserializeKotlinType(proto.extensionReceiverType)
|
||||
setExtensionReceiverParameter(extensionReceiverType.createExtensionReceiver(originalDescriptor))
|
||||
}
|
||||
}.build()!!
|
||||
|
||||
@@ -197,9 +199,7 @@ internal class IrDescriptorDeserializer(val context: Context,
|
||||
val newDescriptor = originalDescriptor.newCopyBuilder().apply() {
|
||||
setOriginal(originalDescriptor)
|
||||
setReturnType(deserializeKotlinType(proto.type))
|
||||
if (proto.hasExtensionReceiverType()) {
|
||||
setExtensionReceiverType(deserializeKotlinType(proto.extensionReceiverType))
|
||||
}
|
||||
assert(!proto.hasExtensionReceiverType())
|
||||
}.build()!!
|
||||
|
||||
descriptorIndex.put(proto.index, newDescriptor)
|
||||
|
||||
+2
-4
@@ -171,10 +171,8 @@ class KonanDescriptorSerializer private constructor(
|
||||
val compileTimeConstant = descriptor.compileTimeInitializer
|
||||
val hasConstant = compileTimeConstant != null && compileTimeConstant !is NullValue
|
||||
|
||||
val hasAnnotations = descriptor.annotations.getAllAnnotations().isNotEmpty()
|
||||
|
||||
val propertyFlags = Flags.getAccessorFlags(
|
||||
hasAnnotations,
|
||||
hasAnnotations(descriptor),
|
||||
ProtoEnumFlags.visibility(normalizeVisibility(descriptor)),
|
||||
ProtoEnumFlags.modality(descriptor.modality),
|
||||
false, false, false
|
||||
@@ -206,7 +204,7 @@ class KonanDescriptorSerializer private constructor(
|
||||
}
|
||||
|
||||
val flags = Flags.getPropertyFlags(
|
||||
hasAnnotations,
|
||||
hasAnnotations(descriptor),
|
||||
ProtoEnumFlags.visibility(normalizeVisibility(descriptor)),
|
||||
ProtoEnumFlags.modality(descriptor.modality),
|
||||
ProtoEnumFlags.memberKind(descriptor.kind),
|
||||
|
||||
+1
-1
@@ -107,7 +107,7 @@ internal class LocalDeclarationSerializer(val context: Context, val rootFunction
|
||||
false, false, false, false, false,
|
||||
isDelegated)
|
||||
|
||||
property.setType(variable.type, listOf(), null, null as KotlinType?)
|
||||
property.setType(variable.type, listOf(), null, null)
|
||||
|
||||
// TODO: transform the getter and the setter too.
|
||||
property.initialize(null, null)
|
||||
|
||||
+4
-4
@@ -288,8 +288,8 @@ private class IrUnboundSymbolReplacer(
|
||||
expression.replaceTypeArguments()
|
||||
|
||||
val field = expression.field?.replaceOrSame(ReferenceSymbolTable::referenceField)
|
||||
val getter = expression.getter?.replace(ReferenceSymbolTable::referenceFunction) ?: expression.getter
|
||||
val setter = expression.setter?.replace(ReferenceSymbolTable::referenceFunction) ?: expression.setter
|
||||
val getter = expression.getter?.replace(ReferenceSymbolTable::referenceSimpleFunction) ?: expression.getter
|
||||
val setter = expression.setter?.replace(ReferenceSymbolTable::referenceSimpleFunction) ?: expression.setter
|
||||
|
||||
if (field == expression.field && getter == expression.getter && setter == expression.setter) {
|
||||
return super.visitPropertyReference(expression)
|
||||
@@ -311,8 +311,8 @@ private class IrUnboundSymbolReplacer(
|
||||
|
||||
override fun visitLocalDelegatedPropertyReference(expression: IrLocalDelegatedPropertyReference): IrExpression {
|
||||
val delegate = expression.delegate.replaceOrSame(ReferenceSymbolTable::referenceVariable)
|
||||
val getter = expression.getter.replace(ReferenceSymbolTable::referenceFunction) ?: expression.getter
|
||||
val setter = expression.setter?.replace(ReferenceSymbolTable::referenceFunction) ?: expression.setter
|
||||
val getter = expression.getter.replace(ReferenceSymbolTable::referenceSimpleFunction) ?: expression.getter
|
||||
val setter = expression.setter?.replace(ReferenceSymbolTable::referenceSimpleFunction) ?: expression.setter
|
||||
|
||||
if (delegate == expression.delegate && getter == expression.getter && setter == expression.setter) {
|
||||
return super.visitLocalDelegatedPropertyReference(expression)
|
||||
|
||||
+3
-7
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.ir.util
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.descriptors.substitute
|
||||
import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.KonanBackendContext
|
||||
import org.jetbrains.kotlin.backend.konan.KonanCompilationException
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName
|
||||
@@ -20,7 +19,6 @@ import org.jetbrains.kotlin.descriptors.ParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.annotations.Annotations
|
||||
import org.jetbrains.kotlin.descriptors.annotations.AnnotationsImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
|
||||
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
@@ -41,7 +39,6 @@ import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.OverridingStrategy
|
||||
import org.jetbrains.kotlin.resolve.OverridingUtil
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.isRestrictsSuspensionReceiver
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes
|
||||
@@ -56,7 +53,7 @@ internal fun IrFile.addTopLevelInitializer(expression: IrExpression, context: Ko
|
||||
val fieldDescriptor = PropertyDescriptorImpl.create(
|
||||
this.packageFragmentDescriptor,
|
||||
if (threadLocal)
|
||||
AnnotationsImpl(listOf(AnnotationDescriptorImpl(context.ir.symbols.threadLocal.defaultType,
|
||||
Annotations.create(listOf(AnnotationDescriptorImpl(context.ir.symbols.threadLocal.defaultType,
|
||||
emptyMap(), SourceElement.NO_SOURCE)))
|
||||
else
|
||||
Annotations.EMPTY,
|
||||
@@ -74,7 +71,7 @@ internal fun IrFile.addTopLevelInitializer(expression: IrExpression, context: Ko
|
||||
false
|
||||
)
|
||||
|
||||
fieldDescriptor.setType(expression.type.toKotlinType(), emptyList(), null, null as KotlinType?)
|
||||
fieldDescriptor.setType(expression.type.toKotlinType(), emptyList(), null, null)
|
||||
fieldDescriptor.initialize(null, null)
|
||||
|
||||
val irField = IrFieldImpl(
|
||||
@@ -686,8 +683,7 @@ fun createField(
|
||||
).apply {
|
||||
initialize(null, null)
|
||||
|
||||
val receiverType: KotlinType? = null
|
||||
setType(type.toKotlinType(), emptyList(), owner.thisAsReceiverParameter, receiverType)
|
||||
setType(type.toKotlinType(), emptyList(), owner.thisAsReceiverParameter, null)
|
||||
}
|
||||
|
||||
return IrFieldImpl(startOffset, endOffset, origin, descriptor, type)
|
||||
|
||||
@@ -43,7 +43,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
cli_bc project(path: ':backend.native', configuration: 'cli_bc')
|
||||
update_tests (group: 'org', name: 'Kotlin_13M2_CompilerAllPlugins', version: testKotlinVersion) {
|
||||
update_tests (group: 'org', name: 'Kotlin_dev_CompilerAllPlugins', version: testKotlinVersion) {
|
||||
artifact {
|
||||
name = 'internal/kotlin-test-data'
|
||||
type = 'zip'
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun suspendHere(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
fun builder(c: suspend () -> Unit) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1() {
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Unit = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun s1(): Unit = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
suspend fun suspendHere(): Int = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlin.coroutines.intrinsics.*
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
class Controller {
|
||||
|
||||
@@ -58,10 +58,10 @@ fun test(s: S, x1: Long, x2: B2, x3: UShort, x4: UInt, x5: Int, x6: Long) {
|
||||
|
||||
// Also check with some insignificant bits modified:
|
||||
|
||||
assign(s, x1 + 2, x2, (x3 + 8).toUShort(), x4 - 16, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
assign(s, x1 + 2, x2, (x3 + 8u).toUShort(), x4 - 16u, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
check(s, x1, x2, x3, x4, x5, x6)
|
||||
|
||||
assignReversed(s, x1 + 2, x2, (x3 + 8).toUShort(), x4 - 16, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
assignReversed(s, x1 + 2, x2, (x3 + 8u).toUShort(), x4 - 16u, x5 + 32, x6 + Long.MIN_VALUE)
|
||||
check(s, x1, x2, x3, x4, x5, x6)
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ fun main(args: Array<String>) {
|
||||
for (x1 in -1L..0L)
|
||||
for (x2 in B2.values())
|
||||
for (x3 in 0..7)
|
||||
for (x4 in uintArrayOf(0, 6, 15))
|
||||
for (x4 in uintArrayOf(0u, 6u, 15u))
|
||||
for (x5 in intArrayOf(-16, -2, -1, 0, 5, 15))
|
||||
for (x6 in longArrayOf(Long.MIN_VALUE/2, -1L shl 36, -325L, 0, 1L shl 48, Long.MAX_VALUE/2))
|
||||
test(s, x1, x2, x3.toUShort(), x4, x5, x6)
|
||||
|
||||
@@ -26,7 +26,7 @@ fun main(args: Array<String>) {
|
||||
with(serverAddr) {
|
||||
memset(this.ptr, 0, sockaddr_in.size.convert())
|
||||
sin_family = AF_INET.convert()
|
||||
sin_addr.s_addr = htons(0).convert()
|
||||
sin_addr.s_addr = htons(0u).convert()
|
||||
sin_port = htons(port)
|
||||
}
|
||||
|
||||
|
||||
@@ -154,15 +154,15 @@ abstract class KonanTest extends JavaExec {
|
||||
|
||||
def emptyContinuationBody =
|
||||
"""
|
||||
|override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
|override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
""".stripMargin()
|
||||
|
||||
def handleResultContinuationBody = """
|
||||
|override fun resumeWith(result: SuccessOrFailure<T>) { x(result.getOrThrow()) }
|
||||
|override fun resumeWith(result: Result<T>) { x(result.getOrThrow()) }
|
||||
""".stripMargin()
|
||||
|
||||
def handleExceptionContinuationBody = """
|
||||
|override fun resumeWith(result: SuccessOrFailure<Any?>) {
|
||||
|override fun resumeWith(result: Result<Any?>) {
|
||||
| val exception = result.exceptionOrNull() ?: return
|
||||
| x(exception)
|
||||
|}
|
||||
@@ -190,7 +190,7 @@ abstract class KonanTest extends JavaExec {
|
||||
|
|
||||
|abstract class ContinuationAdapter<in T> : Continuation<T> {
|
||||
| override val context: CoroutineContext = EmptyCoroutineContext
|
||||
| override fun resumeWith(result: SuccessOrFailure<T>) {
|
||||
| override fun resumeWith(result: Result<T>) {
|
||||
| if (result.isSuccess) {
|
||||
| resume(result.getOrThrow())
|
||||
| } else {
|
||||
|
||||
+3
-3
@@ -19,8 +19,8 @@ buildKotlinVersion=1.3-M2
|
||||
buildKotlinCompilerRepo=http://dl.bintray.com/kotlin/kotlin-eap
|
||||
remoteRoot=konan_tests
|
||||
testDataVersion=1226829:id
|
||||
kotlinCompilerRepo=https://teamcity.jetbrains.com/guestAuth/app/rest/builds/buildType:(id:Kotlin_13M2_CompilerAllPlugins),number:1.3-M2-release-211,tag:kotlin-native,pinned:true,branch:(default:any)/artifacts/content/maven
|
||||
kotlinVersion=1.3-M2-release-211
|
||||
testKotlinVersion=1.3-M2-release-214
|
||||
kotlinCompilerRepo=https://teamcity.jetbrains.com/guestAuth/app/rest/builds/buildType:(id:Kotlin_dev_CompilerAllPlugins),number:1.3.0-dev-391,tag:kotlin-native,pinned:true,branch:(default:any)/artifacts/content/maven
|
||||
kotlinVersion=1.3.0-dev-391
|
||||
testKotlinVersion=1.3.0-dev-391
|
||||
konanVersion=0.9
|
||||
org.gradle.jvmargs='-Dfile.encoding=UTF-8'
|
||||
|
||||
@@ -1,200 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package kotlin.collections
|
||||
|
||||
import kotlin.*
|
||||
import kotlin.sequences.buildIterator
|
||||
|
||||
internal fun checkWindowSizeStep(size: Int, step: Int) {
|
||||
require(size > 0 && step > 0) {
|
||||
if (size != step)
|
||||
"Both size $size and step $step must be greater than zero."
|
||||
else
|
||||
"size $size must be greater than zero."
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <T> Sequence<T>.windowedSequence(size: Int, step: Int, partialWindows: Boolean, reuseBuffer: Boolean): Sequence<List<T>> {
|
||||
checkWindowSizeStep(size, step)
|
||||
return Sequence { windowedIterator(iterator(), size, step, partialWindows, reuseBuffer) }
|
||||
}
|
||||
|
||||
internal fun <T> windowedIterator(iterator: Iterator<T>, size: Int, step: Int, partialWindows: Boolean, reuseBuffer: Boolean): Iterator<List<T>> {
|
||||
if (!iterator.hasNext()) return EmptyIterator
|
||||
return buildIterator<List<T>> {
|
||||
val gap = step - size
|
||||
if (gap >= 0) {
|
||||
var buffer = ArrayList<T>(size)
|
||||
var skip = 0
|
||||
for (e in iterator) {
|
||||
if (skip > 0) { skip -= 1; continue }
|
||||
buffer.add(e)
|
||||
if (buffer.size == size) {
|
||||
yield(buffer)
|
||||
if (reuseBuffer) buffer.clear() else buffer = ArrayList(size)
|
||||
skip = gap
|
||||
}
|
||||
}
|
||||
if (buffer.isNotEmpty()) {
|
||||
if (partialWindows || buffer.size == size) yield(buffer)
|
||||
}
|
||||
} else {
|
||||
val buffer = RingBuffer<T>(size)
|
||||
for (e in iterator) {
|
||||
buffer.add(e)
|
||||
if (buffer.isFull()) {
|
||||
yield(if (reuseBuffer) buffer else ArrayList(buffer))
|
||||
buffer.removeFirst(step)
|
||||
}
|
||||
}
|
||||
if (partialWindows) {
|
||||
while (buffer.size > step) {
|
||||
yield(if (reuseBuffer) buffer else ArrayList(buffer))
|
||||
buffer.removeFirst(step)
|
||||
}
|
||||
if (buffer.isNotEmpty()) yield(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class MovingSubList<out E>(private val list: List<E>) : AbstractList<E>(), RandomAccess {
|
||||
private var fromIndex: Int = 0
|
||||
private var _size: Int = 0
|
||||
|
||||
fun move(fromIndex: Int, toIndex: Int) {
|
||||
checkRangeIndexes(fromIndex, toIndex, list.size)
|
||||
this.fromIndex = fromIndex
|
||||
this._size = toIndex - fromIndex
|
||||
}
|
||||
|
||||
override fun get(index: Int): E {
|
||||
checkElementIndex(index, _size)
|
||||
|
||||
return list[fromIndex + index]
|
||||
}
|
||||
|
||||
override val size: Int get() = _size
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Provides ring buffer implementation.
|
||||
*
|
||||
* Buffer overflow is not allowed so [add] doesn't overwrite tail but raises an exception.
|
||||
*/
|
||||
private class RingBuffer<T>(val capacity: Int) : AbstractList<T>(), RandomAccess {
|
||||
init {
|
||||
require(capacity >= 0) { "ring buffer capacity should not be negative but it is $capacity" }
|
||||
}
|
||||
|
||||
private val buffer = arrayOfNulls<Any?>(capacity)
|
||||
private var startIndex: Int = 0
|
||||
|
||||
override var size: Int = 0
|
||||
private set
|
||||
|
||||
override fun get(index: Int): T {
|
||||
checkElementIndex(index, size)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return buffer[startIndex.forward(index)] as T
|
||||
}
|
||||
|
||||
fun isFull() = size == capacity
|
||||
|
||||
override fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
|
||||
private var count = size
|
||||
private var index = startIndex
|
||||
|
||||
override fun computeNext() {
|
||||
if (count == 0) {
|
||||
done()
|
||||
} else {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
setNext(buffer[index] as T)
|
||||
index = index.forward(1)
|
||||
count--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T> toArray(array: Array<T>): Array<T> {
|
||||
val result: Array<T?> =
|
||||
if (array.size < this.size) array.copyOf(this.size) else array as Array<T?>
|
||||
|
||||
val size = this.size
|
||||
|
||||
var widx = 0
|
||||
var idx = startIndex
|
||||
|
||||
while (widx < size && idx < capacity) {
|
||||
result[widx] = buffer[idx] as T
|
||||
widx++
|
||||
idx++
|
||||
}
|
||||
|
||||
idx = 0
|
||||
while (widx < size) {
|
||||
result[widx] = buffer[idx] as T
|
||||
widx++
|
||||
idx++
|
||||
}
|
||||
if (result.size > this.size) result[this.size] = null
|
||||
|
||||
return result as Array<T>
|
||||
}
|
||||
|
||||
override fun toArray(): Array<Any?> {
|
||||
return toArray(arrayOfNulls(size))
|
||||
}
|
||||
|
||||
/**
|
||||
* Add [element] to the buffer or fail with [IllegalStateException] if no free space available in the buffer
|
||||
*/
|
||||
fun add(element: T) {
|
||||
if (isFull()) {
|
||||
throw IllegalStateException("ring buffer is full")
|
||||
}
|
||||
|
||||
buffer[startIndex.forward(size)] = element
|
||||
size++
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes [n] first elements from the buffer or fails with [IllegalArgumentException] if not enough elements in the buffer to remove
|
||||
*/
|
||||
fun removeFirst(n: Int) {
|
||||
require(n >= 0) { "n shouldn't be negative but it is $n" }
|
||||
require(n <= size) { "n shouldn't be greater than the buffer size: n = $n, size = $size" }
|
||||
|
||||
if (n > 0) {
|
||||
val start = startIndex
|
||||
val end = start.forward(n)
|
||||
|
||||
if (start > end) {
|
||||
buffer.fill(null, start, capacity)
|
||||
buffer.fill(null, 0, end)
|
||||
} else {
|
||||
buffer.fill(null, start, end)
|
||||
}
|
||||
|
||||
startIndex = end
|
||||
size -= n
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun Int.forward(n: Int): Int = (this + n) % capacity
|
||||
|
||||
// TODO: replace with Array.fill from stdlib when available in common
|
||||
private fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
for (idx in fromIndex until toIndex) {
|
||||
this[idx] = element
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ internal abstract class BaseContinuationImpl(
|
||||
public val completion: Continuation<Any?>?
|
||||
) : Continuation<Any?>, Serializable {
|
||||
// This implementation is final. This fact is used to unroll resumeWith recursion.
|
||||
public final override fun resumeWith(result: SuccessOrFailure<Any?>) {
|
||||
public final override fun resumeWith(result: Result<Any?>) {
|
||||
// Invoke "resume" debug probe only once, even if previous frames are "resumed" in the loop below, too
|
||||
probeCoroutineResumed(this)
|
||||
// This loop unrolls recursion in current.resumeWith(param) to make saner and shorter stack traces on resume
|
||||
@@ -25,13 +25,13 @@ internal abstract class BaseContinuationImpl(
|
||||
while (true) {
|
||||
with(current) {
|
||||
val completion = completion!! // fail fast when trying to resume continuation without completion
|
||||
val outcome: SuccessOrFailure<Any?> =
|
||||
val outcome: Result<Any?> =
|
||||
try {
|
||||
val outcome = invokeSuspend(param)
|
||||
if (outcome === COROUTINE_SUSPENDED) return
|
||||
SuccessOrFailure.success(outcome)
|
||||
Result.success(outcome)
|
||||
} catch (exception: Throwable) {
|
||||
SuccessOrFailure.failure(exception)
|
||||
Result.failure(exception)
|
||||
}
|
||||
releaseIntercepted() // this state machine instance is terminating
|
||||
if (completion is BaseContinuationImpl) {
|
||||
@@ -47,7 +47,7 @@ internal abstract class BaseContinuationImpl(
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract fun invokeSuspend(result: SuccessOrFailure<Any?>): Any?
|
||||
protected abstract fun invokeSuspend(result: Result<Any?>): Any?
|
||||
|
||||
protected open fun releaseIntercepted() {
|
||||
// does nothing here, overridden in ContinuationImpl
|
||||
@@ -115,7 +115,7 @@ internal object CompletedContinuation : Continuation<Any?> {
|
||||
override val context: CoroutineContext
|
||||
get() = error("This continuation is already complete")
|
||||
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) {
|
||||
override fun resumeWith(result: Result<Any?>) {
|
||||
error("This continuation is already complete")
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ internal actual constructor(
|
||||
|
||||
private var result: Any? = initialResult
|
||||
|
||||
public actual override fun resumeWith(result: SuccessOrFailure<T>) {
|
||||
public actual override fun resumeWith(result: Result<T>) {
|
||||
val cur = this.result
|
||||
when {
|
||||
cur === UNDECIDED -> this.result = result.value
|
||||
@@ -45,7 +45,7 @@ internal actual constructor(
|
||||
}
|
||||
return when {
|
||||
result === RESUMED -> COROUTINE_SUSPENDED // already called continuation, indicate COROUTINE_SUSPENDED upstream
|
||||
result is SuccessOrFailure.Failure -> throw result.exception
|
||||
result is Result.Failure -> throw result.exception
|
||||
else -> result // either COROUTINE_SUSPENDED or data
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@ private inline fun <T> createCoroutineFromSuspendFunction(
|
||||
object : RestrictedContinuationImpl(completion as Continuation<Any?>) {
|
||||
private var label = 0
|
||||
|
||||
override fun invokeSuspend(result: SuccessOrFailure<Any?>): Any? =
|
||||
override fun invokeSuspend(result: Result<Any?>): Any? =
|
||||
when (label) {
|
||||
0 -> {
|
||||
label = 1
|
||||
@@ -166,7 +166,7 @@ private inline fun <T> createCoroutineFromSuspendFunction(
|
||||
object : ContinuationImpl(completion as Continuation<Any?>, context) {
|
||||
private var label = 0
|
||||
|
||||
override fun invokeSuspend(result: SuccessOrFailure<Any?>): Any? =
|
||||
override fun invokeSuspend(result: Result<Any?>): Any? =
|
||||
when (label) {
|
||||
0 -> {
|
||||
label = 1
|
||||
@@ -181,3 +181,11 @@ private inline fun <T> createCoroutineFromSuspendFunction(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This value is used as a return value of [suspendCoroutineOrReturn] `block` argument to state that
|
||||
* the execution was suspended and will not return any result immediately.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual val COROUTINE_SUSPENDED: Any
|
||||
get() = CoroutineSingletons.COROUTINE_SUSPENDED
|
||||
@@ -46,7 +46,7 @@ public annotation class Throws(vararg val exceptionClasses: KClass<out Throwable
|
||||
* object immutability.
|
||||
* PLEASE NOTE THAT THIS ANNOTATION MAY GO AWAY IN UPCOMING RELEASES.
|
||||
*/
|
||||
@Target(AnnotationTarget.FIELD, AnnotationTarget.CLASS)
|
||||
@Target(AnnotationTarget.PROPERTY, AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
public annotation class ThreadLocal
|
||||
|
||||
@@ -54,7 +54,7 @@ public annotation class ThreadLocal
|
||||
* Top level variable is immutable and so could be shared.
|
||||
* PLEASE NOTE THAT THIS ANNOTATION MAY GO AWAY IN UPCOMING RELEASES.
|
||||
*/
|
||||
@Target(AnnotationTarget.FIELD)
|
||||
@Target(AnnotationTarget.PROPERTY)
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
public annotation class SharedImmutable
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
@file:Suppress("RESERVED_MEMBER_INSIDE_INLINE_CLASS")
|
||||
|
||||
package kotlin.native.internal
|
||||
|
||||
@Intrinsic external fun getNativeNullPtr(): NativePtr
|
||||
|
||||
@@ -1,634 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:kotlin.jvm.JvmMultifileClass
|
||||
@file:kotlin.jvm.JvmName("SequencesKt")
|
||||
|
||||
package kotlin.sequences
|
||||
|
||||
import kotlin.*
|
||||
|
||||
/**
|
||||
* Given an [iterator] function constructs a [Sequence] that returns values through the [Iterator]
|
||||
* provided by that function.
|
||||
* The values are evaluated lazily, and the sequence is potentially infinite.
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.sequenceFromIterator
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Sequence(crossinline iterator: () -> Iterator<T>): Sequence<T> = object : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> = iterator()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a sequence that returns all elements from this iterator. The sequence is constrained to be iterated only once.
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.sequenceFromIterator
|
||||
*/
|
||||
public fun <T> Iterator<T>.asSequence(): Sequence<T> = Sequence { this }.constrainOnce()
|
||||
|
||||
/**
|
||||
* Creates a sequence that returns the specified values.
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.sequenceOfValues
|
||||
*/
|
||||
public fun <T> sequenceOf(vararg elements: T): Sequence<T> = if (elements.isEmpty()) emptySequence() else elements.asSequence()
|
||||
|
||||
/**
|
||||
* Returns an empty sequence.
|
||||
*/
|
||||
public fun <T> emptySequence(): Sequence<T> = EmptySequence
|
||||
|
||||
private object EmptySequence : Sequence<Nothing>, DropTakeSequence<Nothing> {
|
||||
override fun iterator(): Iterator<Nothing> = EmptyIterator
|
||||
override fun drop(n: Int) = EmptySequence
|
||||
override fun take(n: Int) = EmptySequence
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this sequence if it's not `null` and the empty sequence otherwise.
|
||||
* @sample samples.collections.Sequences.Usage.sequenceOrEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Sequence<T>?.orEmpty(): Sequence<T> = this ?: emptySequence()
|
||||
|
||||
|
||||
/**
|
||||
* Returns a sequence that iterates through the elements either of this sequence
|
||||
* or, if this sequence turns out to be empty, of the sequence returned by [defaultValue] function.
|
||||
*
|
||||
* @sample samples.collections.Sequences.Usage.sequenceIfEmpty
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public fun <T> Sequence<T>.ifEmpty(defaultValue: () -> Sequence<T>): Sequence<T> = buildSequence {
|
||||
val iterator = this@ifEmpty.iterator()
|
||||
if (iterator.hasNext()) {
|
||||
yieldAll(iterator)
|
||||
} else {
|
||||
yieldAll(defaultValue())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence of all elements from all sequences in this sequence.
|
||||
*
|
||||
* The operation is _intermediate_ and _stateless_.
|
||||
*/
|
||||
public fun <T> Sequence<Sequence<T>>.flatten(): Sequence<T> = flatten { it.iterator() }
|
||||
|
||||
/**
|
||||
* Returns a sequence of all elements from all iterables in this sequence.
|
||||
*
|
||||
* The operation is _intermediate_ and _stateless_.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("flattenSequenceOfIterable")
|
||||
public fun <T> Sequence<Iterable<T>>.flatten(): Sequence<T> = flatten { it.iterator() }
|
||||
|
||||
private fun <T, R> Sequence<T>.flatten(iterator: (T) -> Iterator<R>): Sequence<R> {
|
||||
if (this is TransformingSequence<*, *>) {
|
||||
return (this as TransformingSequence<*, T>).flatten(iterator)
|
||||
}
|
||||
return FlatteningSequence(this, { it }, iterator)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pair of lists, where
|
||||
* *first* list is built from the first values of each pair from this sequence,
|
||||
* *second* list is built from the second values of each pair from this sequence.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*/
|
||||
public fun <T, R> Sequence<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||
val listT = ArrayList<T>()
|
||||
val listR = ArrayList<R>()
|
||||
for (pair in this) {
|
||||
listT.add(pair.first)
|
||||
listR.add(pair.second)
|
||||
}
|
||||
return listT to listR
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that returns the values from the underlying [sequence] that either match or do not match
|
||||
* the specified [predicate].
|
||||
*
|
||||
* @param sendWhen If `true`, values for which the predicate returns `true` are returned. Otherwise,
|
||||
* values for which the predicate returns `false` are returned
|
||||
*/
|
||||
internal class FilteringSequence<T>(
|
||||
private val sequence: Sequence<T>,
|
||||
private val sendWhen: Boolean = true,
|
||||
private val predicate: (T) -> Boolean
|
||||
) : Sequence<T> {
|
||||
|
||||
override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
val iterator = sequence.iterator()
|
||||
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
|
||||
var nextItem: T? = null
|
||||
|
||||
private fun calcNext() {
|
||||
while (iterator.hasNext()) {
|
||||
val item = iterator.next()
|
||||
if (predicate(item) == sendWhen) {
|
||||
nextItem = item
|
||||
nextState = 1
|
||||
return
|
||||
}
|
||||
}
|
||||
nextState = 0
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (nextState == -1)
|
||||
calcNext()
|
||||
if (nextState == 0)
|
||||
throw NoSuchElementException()
|
||||
val result = nextItem
|
||||
nextItem = null
|
||||
nextState = -1
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return result as T
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (nextState == -1)
|
||||
calcNext()
|
||||
return nextState == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence which returns the results of applying the given [transformer] function to the values
|
||||
* in the underlying [sequence].
|
||||
*/
|
||||
|
||||
internal class TransformingSequence<T, R>
|
||||
constructor(private val sequence: Sequence<T>, private val transformer: (T) -> R) : Sequence<R> {
|
||||
override fun iterator(): Iterator<R> = object : Iterator<R> {
|
||||
val iterator = sequence.iterator()
|
||||
override fun next(): R {
|
||||
return transformer(iterator.next())
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iterator.hasNext()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun <E> flatten(iterator: (R) -> Iterator<E>): Sequence<E> {
|
||||
return FlatteningSequence<T, R, E>(sequence, transformer, iterator)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence which returns the results of applying the given [transformer] function to the values
|
||||
* in the underlying [sequence], where the transformer function takes the index of the value in the underlying
|
||||
* sequence along with the value itself.
|
||||
*/
|
||||
internal class TransformingIndexedSequence<T, R>
|
||||
constructor(private val sequence: Sequence<T>, private val transformer: (Int, T) -> R) : Sequence<R> {
|
||||
override fun iterator(): Iterator<R> = object : Iterator<R> {
|
||||
val iterator = sequence.iterator()
|
||||
var index = 0
|
||||
override fun next(): R {
|
||||
return transformer(checkIndexOverflow(index++), iterator.next())
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iterator.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence which combines values from the underlying [sequence] with their indices and returns them as
|
||||
* [IndexedValue] objects.
|
||||
*/
|
||||
internal class IndexingSequence<T>
|
||||
constructor(private val sequence: Sequence<T>) : Sequence<IndexedValue<T>> {
|
||||
override fun iterator(): Iterator<IndexedValue<T>> = object : Iterator<IndexedValue<T>> {
|
||||
val iterator = sequence.iterator()
|
||||
var index = 0
|
||||
override fun next(): IndexedValue<T> {
|
||||
return IndexedValue(checkIndexOverflow(index++), iterator.next())
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iterator.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence which takes the values from two parallel underlying sequences, passes them to the given
|
||||
* [transform] function and returns the values returned by that function. The sequence stops returning
|
||||
* values as soon as one of the underlying sequences stops returning values.
|
||||
*/
|
||||
internal class MergingSequence<T1, T2, V>
|
||||
constructor(
|
||||
private val sequence1: Sequence<T1>,
|
||||
private val sequence2: Sequence<T2>,
|
||||
private val transform: (T1, T2) -> V
|
||||
) : Sequence<V> {
|
||||
override fun iterator(): Iterator<V> = object : Iterator<V> {
|
||||
val iterator1 = sequence1.iterator()
|
||||
val iterator2 = sequence2.iterator()
|
||||
override fun next(): V {
|
||||
return transform(iterator1.next(), iterator2.next())
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iterator1.hasNext() && iterator2.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class FlatteningSequence<T, R, E>
|
||||
constructor(
|
||||
private val sequence: Sequence<T>,
|
||||
private val transformer: (T) -> R,
|
||||
private val iterator: (R) -> Iterator<E>
|
||||
) : Sequence<E> {
|
||||
override fun iterator(): Iterator<E> = object : Iterator<E> {
|
||||
val iterator = sequence.iterator()
|
||||
var itemIterator: Iterator<E>? = null
|
||||
|
||||
override fun next(): E {
|
||||
if (!ensureItemIterator())
|
||||
throw NoSuchElementException()
|
||||
return itemIterator!!.next()
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return ensureItemIterator()
|
||||
}
|
||||
|
||||
private fun ensureItemIterator(): Boolean {
|
||||
if (itemIterator?.hasNext() == false)
|
||||
itemIterator = null
|
||||
|
||||
while (itemIterator == null) {
|
||||
if (!iterator.hasNext()) {
|
||||
return false
|
||||
} else {
|
||||
val element = iterator.next()
|
||||
val nextItemIterator = iterator(transformer(element))
|
||||
if (nextItemIterator.hasNext()) {
|
||||
itemIterator = nextItemIterator
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that supports drop(n) and take(n) operations
|
||||
*/
|
||||
internal interface DropTakeSequence<T> : Sequence<T> {
|
||||
fun drop(n: Int): Sequence<T>
|
||||
fun take(n: Int): Sequence<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that skips [startIndex] values from the underlying [sequence]
|
||||
* and stops returning values right before [endIndex], i.e. stops at `endIndex - 1`
|
||||
*/
|
||||
internal class SubSequence<T>(
|
||||
private val sequence: Sequence<T>,
|
||||
private val startIndex: Int,
|
||||
private val endIndex: Int
|
||||
) : Sequence<T>, DropTakeSequence<T> {
|
||||
|
||||
init {
|
||||
require(startIndex >= 0) { "startIndex should be non-negative, but is $startIndex" }
|
||||
require(endIndex >= 0) { "endIndex should be non-negative, but is $endIndex" }
|
||||
require(endIndex >= startIndex) { "endIndex should be not less than startIndex, but was $endIndex < $startIndex" }
|
||||
}
|
||||
|
||||
private val count: Int get() = endIndex - startIndex
|
||||
|
||||
override fun drop(n: Int): Sequence<T> = if (n >= count) emptySequence() else SubSequence(sequence, startIndex + n, endIndex)
|
||||
override fun take(n: Int): Sequence<T> = if (n >= count) this else SubSequence(sequence, startIndex, startIndex + n)
|
||||
|
||||
override fun iterator() = object : Iterator<T> {
|
||||
|
||||
val iterator = sequence.iterator()
|
||||
var position = 0
|
||||
|
||||
// Shouldn't be called from constructor to avoid premature iteration
|
||||
private fun drop() {
|
||||
while (position < startIndex && iterator.hasNext()) {
|
||||
iterator.next()
|
||||
position++
|
||||
}
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
drop()
|
||||
return (position < endIndex) && iterator.hasNext()
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
drop()
|
||||
if (position >= endIndex)
|
||||
throw NoSuchElementException()
|
||||
position++
|
||||
return iterator.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that returns at most [count] values from the underlying [sequence], and stops returning values
|
||||
* as soon as that count is reached.
|
||||
*/
|
||||
internal class TakeSequence<T>(
|
||||
private val sequence: Sequence<T>,
|
||||
private val count: Int
|
||||
) : Sequence<T>, DropTakeSequence<T> {
|
||||
|
||||
init {
|
||||
require(count >= 0) { "count must be non-negative, but was $count." }
|
||||
}
|
||||
|
||||
override fun drop(n: Int): Sequence<T> = if (n >= count) emptySequence() else SubSequence(sequence, n, count)
|
||||
override fun take(n: Int): Sequence<T> = if (n >= count) this else TakeSequence(sequence, n)
|
||||
|
||||
override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
var left = count
|
||||
val iterator = sequence.iterator()
|
||||
|
||||
override fun next(): T {
|
||||
if (left == 0)
|
||||
throw NoSuchElementException()
|
||||
left--
|
||||
return iterator.next()
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return left > 0 && iterator.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that returns values from the underlying [sequence] while the [predicate] function returns
|
||||
* `true`, and stops returning values once the function returns `false` for the next element.
|
||||
*/
|
||||
internal class TakeWhileSequence<T>
|
||||
constructor(
|
||||
private val sequence: Sequence<T>,
|
||||
private val predicate: (T) -> Boolean
|
||||
) : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
val iterator = sequence.iterator()
|
||||
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
|
||||
var nextItem: T? = null
|
||||
|
||||
private fun calcNext() {
|
||||
if (iterator.hasNext()) {
|
||||
val item = iterator.next()
|
||||
if (predicate(item)) {
|
||||
nextState = 1
|
||||
nextItem = item
|
||||
return
|
||||
}
|
||||
}
|
||||
nextState = 0
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (nextState == -1)
|
||||
calcNext() // will change nextState
|
||||
if (nextState == 0)
|
||||
throw NoSuchElementException()
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val result = nextItem as T
|
||||
|
||||
// Clean next to avoid keeping reference on yielded instance
|
||||
nextItem = null
|
||||
nextState = -1
|
||||
return result
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (nextState == -1)
|
||||
calcNext() // will change nextState
|
||||
return nextState == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that skips the specified number of values from the underlying [sequence] and returns
|
||||
* all values after that.
|
||||
*/
|
||||
internal class DropSequence<T>(
|
||||
private val sequence: Sequence<T>,
|
||||
private val count: Int
|
||||
) : Sequence<T>, DropTakeSequence<T> {
|
||||
init {
|
||||
require(count >= 0) { "count must be non-negative, but was $count." }
|
||||
}
|
||||
|
||||
override fun drop(n: Int): Sequence<T> = (count + n).let { n1 -> if (n1 < 0) DropSequence(this, n) else DropSequence(sequence, n1) }
|
||||
override fun take(n: Int): Sequence<T> = (count + n).let { n1 -> if (n1 < 0) TakeSequence(this, n) else SubSequence(sequence, count, n1) }
|
||||
|
||||
override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
val iterator = sequence.iterator()
|
||||
var left = count
|
||||
|
||||
// Shouldn't be called from constructor to avoid premature iteration
|
||||
private fun drop() {
|
||||
while (left > 0 && iterator.hasNext()) {
|
||||
iterator.next()
|
||||
left--
|
||||
}
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
drop()
|
||||
return iterator.next()
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
drop()
|
||||
return iterator.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that skips the values from the underlying [sequence] while the given [predicate] returns `true` and returns
|
||||
* all values after that.
|
||||
*/
|
||||
internal class DropWhileSequence<T>
|
||||
constructor(
|
||||
private val sequence: Sequence<T>,
|
||||
private val predicate: (T) -> Boolean
|
||||
) : Sequence<T> {
|
||||
|
||||
override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
val iterator = sequence.iterator()
|
||||
var dropState: Int = -1 // -1 for not dropping, 1 for nextItem, 0 for normal iteration
|
||||
var nextItem: T? = null
|
||||
|
||||
private fun drop() {
|
||||
while (iterator.hasNext()) {
|
||||
val item = iterator.next()
|
||||
if (!predicate(item)) {
|
||||
nextItem = item
|
||||
dropState = 1
|
||||
return
|
||||
}
|
||||
}
|
||||
dropState = 0
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (dropState == -1)
|
||||
drop()
|
||||
|
||||
if (dropState == 1) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val result = nextItem as T
|
||||
nextItem = null
|
||||
dropState = 0
|
||||
return result
|
||||
}
|
||||
return iterator.next()
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (dropState == -1)
|
||||
drop()
|
||||
return dropState == 1 || iterator.hasNext()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class DistinctSequence<T, K>(private val source: Sequence<T>, private val keySelector: (T) -> K) : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> = DistinctIterator(source.iterator(), keySelector)
|
||||
}
|
||||
|
||||
private class DistinctIterator<T, K>(private val source: Iterator<T>, private val keySelector: (T) -> K) : AbstractIterator<T>() {
|
||||
private val observed = HashSet<K>()
|
||||
|
||||
override fun computeNext() {
|
||||
while (source.hasNext()) {
|
||||
val next = source.next()
|
||||
val key = keySelector(next)
|
||||
|
||||
if (observed.add(key)) {
|
||||
setNext(next)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class GeneratorSequence<T : Any>(private val getInitialValue: () -> T?, private val getNextValue: (T) -> T?) : Sequence<T> {
|
||||
override fun iterator(): Iterator<T> = object : Iterator<T> {
|
||||
var nextItem: T? = null
|
||||
var nextState: Int = -2 // -2 for initial unknown, -1 for next unknown, 0 for done, 1 for continue
|
||||
|
||||
private fun calcNext() {
|
||||
nextItem = if (nextState == -2) getInitialValue() else getNextValue(nextItem!!)
|
||||
nextState = if (nextItem == null) 0 else 1
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (nextState < 0)
|
||||
calcNext()
|
||||
|
||||
if (nextState == 0)
|
||||
throw NoSuchElementException()
|
||||
val result = nextItem as T
|
||||
// Do not clean nextItem (to avoid keeping reference on yielded instance) -- need to keep state for getNextValue
|
||||
nextState = -1
|
||||
return result
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (nextState < 0)
|
||||
calcNext()
|
||||
return nextState == 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a wrapper sequence that provides values of this sequence, but ensures it can be iterated only one time.
|
||||
*
|
||||
* The operation is _intermediate_ and _stateless_.
|
||||
*
|
||||
* [IllegalStateException] is thrown on iterating the returned sequence from the second time.
|
||||
*
|
||||
*/
|
||||
public fun <T> Sequence<T>.constrainOnce(): Sequence<T> {
|
||||
// as? does not work in js
|
||||
//return this as? ConstrainedOnceSequence<T> ?: ConstrainedOnceSequence(this)
|
||||
return if (this is ConstrainedOnceSequence<T>) this else ConstrainedOnceSequence(this)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a sequence which invokes the function to calculate the next value on each iteration until the function returns `null`.
|
||||
*
|
||||
* The returned sequence is constrained to be iterated only once.
|
||||
*
|
||||
* @see constrainOnce
|
||||
* @see buildSequence
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.generateSequence
|
||||
*/
|
||||
public fun <T : Any> generateSequence(nextFunction: () -> T?): Sequence<T> {
|
||||
return GeneratorSequence(nextFunction, { nextFunction() }).constrainOnce()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence defined by the starting value [seed] and the function [nextFunction],
|
||||
* which is invoked to calculate the next value based on the previous one on each iteration.
|
||||
*
|
||||
* The sequence produces values until it encounters first `null` value.
|
||||
* If [seed] is `null`, an empty sequence is produced.
|
||||
*
|
||||
* The sequence can be iterated multiple times, each time starting with [seed].
|
||||
*
|
||||
* @see buildSequence
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.generateSequenceWithSeed
|
||||
*/
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
public fun <T : Any> generateSequence(seed: T?, nextFunction: (T) -> T?): Sequence<T> =
|
||||
if (seed == null)
|
||||
EmptySequence
|
||||
else
|
||||
GeneratorSequence({ seed }, nextFunction)
|
||||
|
||||
/**
|
||||
* Returns a sequence defined by the function [seedFunction], which is invoked to produce the starting value,
|
||||
* and the [nextFunction], which is invoked to calculate the next value based on the previous one on each iteration.
|
||||
*
|
||||
* The sequence produces values until it encounters first `null` value.
|
||||
* If [seedFunction] returns `null`, an empty sequence is produced.
|
||||
*
|
||||
* The sequence can be iterated multiple times.
|
||||
*
|
||||
* @see buildSequence
|
||||
*
|
||||
* @sample samples.collections.Sequences.Building.generateSequenceWithLazySeed
|
||||
*/
|
||||
public fun <T : Any> generateSequence(seedFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> =
|
||||
GeneratorSequence(seedFunction, nextFunction)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -116,7 +116,7 @@ class Client(val clientFd: Int, val waitingList: MutableMap<Int, WaitingFor>) {
|
||||
|
||||
open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation<Any?> {
|
||||
companion object : EmptyContinuation()
|
||||
override fun resumeWith(result: SuccessOrFailure<Any?>) { result.getOrThrow() }
|
||||
override fun resumeWith(result: Result<Any?>) { result.getOrThrow() }
|
||||
}
|
||||
|
||||
fun acceptClientsAndRun(serverFd: Int, block: suspend Client.() -> Unit) {
|
||||
|
||||
Reference in New Issue
Block a user