core.kotlin.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin
import java.util.*
/**
* Returns an empty array of the specified type [T].
*/
public inline fun emptyArray(): Array = arrayOfNulls(0) as Array
@library
public fun arrayOf(vararg elements: T): Array = noImpl
@library
public fun doubleArrayOf(vararg elements: Double): DoubleArray = noImpl
@library
public fun floatArrayOf(vararg elements: Float): FloatArray = noImpl
@library
public fun longArrayOf(vararg elements: Long): LongArray = noImpl
@library
public fun intArrayOf(vararg elements: Int): IntArray = noImpl
@library
public fun charArrayOf(vararg elements: Char): CharArray = noImpl
@library
public fun shortArrayOf(vararg elements: Short): ShortArray = noImpl
@library
public fun byteArrayOf(vararg elements: Byte): ByteArray = noImpl
@library
public fun booleanArrayOf(vararg elements: Boolean): BooleanArray = noImpl
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
*/
public fun lazy(initializer: () -> T): Lazy = UnsafeLazyImpl(initializer)
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
*
* The [mode] parameter is ignored. */
public fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer)
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
*
* The [lock] parameter is ignored.
*/
public fun lazy(lock: Any?, initializer: () -> T): Lazy = UnsafeLazyImpl(initializer)
internal fun arrayOfNulls(reference: Array, size: Int): Array {
return arrayOfNulls(size) as Array
}
internal fun arrayCopyResize(source: dynamic, newSize: Int, defaultValue: Any?): dynamic {
val result = source.slice(0, newSize)
var index: Int = source.length
if (newSize > index) {
result.length = newSize
while (index < newSize) result[index++] = defaultValue
}
return result
}
internal fun arrayPlusCollection(array: dynamic, collection: Collection): dynamic {
val result = array.slice()
result.length += collection.size
var index: Int = array.length
for (element in collection) result[index++] = element
return result
}
// no singleton map implementation in js, return map as is
internal inline fun Map.toSingletonMap(): Map = this
internal inline fun Array.copyToArrayOfAny(isVarargs: Boolean): Array =
if (isVarargs)
// no need to copy vararg array in JS
this
else
this.copyOf()