[JS IR BE] Add more stdlib files to runtime

Keep old sources list for coroutine tests
This commit is contained in:
Svyatoslav Kuzmich
2018-08-28 19:51:26 +03:00
parent 97717afec4
commit c3b9a5108e
5 changed files with 192 additions and 35 deletions
@@ -41,7 +41,7 @@ public val COROUTINE_SUSPENDED: Any = Any()
@SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
@kotlin.internal.InlineOnly
public inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
completion: Continuation<T>
): Any? {
val self_0 = this
@@ -54,7 +54,7 @@ public inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
@SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
@kotlin.internal.InlineOnly
public inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
receiver: R,
completion: Continuation<T>
): Any? {
@@ -67,7 +67,7 @@ public inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn
}
@SinceKotlin("1.1")
public fun <R, T> (suspend R.() -> T).createCoroutineUnchecked(
public actual fun <R, T> (suspend R.() -> T).createCoroutineUnchecked(
receiver: R,
completion: Continuation<T>
): Continuation<Unit> {
@@ -81,7 +81,7 @@ public fun <R, T> (suspend R.() -> T).createCoroutineUnchecked(
}
@SinceKotlin("1.1")
public fun <T> (suspend () -> T).createCoroutineUnchecked(
public actual fun <T> (suspend () -> T).createCoroutineUnchecked(
completion: Continuation<T>
): Continuation<Unit> {
return if (this !is CoroutineImpl) {
+11 -29
View File
@@ -63,10 +63,6 @@ public fun <T> (suspend () -> T).createCoroutine(
completion: Continuation<T>
): Continuation<Unit> = SafeContinuation(createCoroutineUnchecked(completion), COROUTINE_SUSPENDED)
// TODO: remove this once implemented in stdlib
inline fun <T, R> T.let(f: (T) -> R) = f(this)
inline fun <R> run(f: () -> R) = f()
public interface CoroutineContext {
/**
* Returns the element with the given [key] from this context or `null`.
@@ -154,6 +150,7 @@ public interface ContinuationInterceptor : CoroutineContext.Element {
public fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T>
}
internal class CombinedContext(val left: CoroutineContext, val element: CoroutineContext.Element) : CoroutineContext {
override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E? {
var cur = this
@@ -217,38 +214,22 @@ public object EmptyCoroutineContext : CoroutineContext {
public override fun toString(): String = "EmptyCoroutineContext"
}
public interface Continuation<in T> {
/**
* Context of the coroutine that corresponds to this continuation.
*/
public val context: CoroutineContext
/**
* Resumes the execution of the corresponding coroutine passing [value] as the return value of the last suspension point.
*/
public fun resume(value: T)
/**
* Resumes the execution of the corresponding coroutine so that the [exception] is re-thrown right after the
* last suspension point.
*/
public fun resumeWithException(exception: Throwable)
}
public class SafeContinuation<in T>
public constructor(
@PublishedApi
internal actual class SafeContinuation<in T>
internal actual constructor(
private val delegate: Continuation<T>,
initialResult: Any?
) : Continuation<T> {
public constructor(delegate: Continuation<T>) : this(delegate, UNDECIDED)
@PublishedApi
internal actual constructor(delegate: Continuation<T>) : this(delegate, UNDECIDED)
public override val context: CoroutineContext
public actual override val context: CoroutineContext
get() = delegate.context
private var result: Any? = initialResult
override fun resume(value: T) {
actual override fun resume(value: T) {
when {
result === UNDECIDED -> {
result = value
@@ -263,7 +244,7 @@ public constructor(
}
}
override fun resumeWithException(exception: Throwable) {
actual override fun resumeWithException(exception: Throwable) {
when {
result === UNDECIDED -> {
result = Fail(exception)
@@ -278,7 +259,8 @@ public constructor(
}
}
public fun getResult(): Any? {
@PublishedApi
internal actual fun getResult(): Any? {
if (result === UNDECIDED) {
result = COROUTINE_SUSPENDED
}
@@ -0,0 +1,106 @@
/*
* 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 is a copy of stdlib/js/src/kotlin/kotlin.kt
// TODO: Compile arrayPlusCollection
// TODO: implement a copy of jsIsType for both JS backends
@file:Suppress("UNUSED_PARAMETER", "NOTHING_TO_INLINE")
package kotlin
/**
* Returns an empty array of the specified type [T].
*/
public inline fun <T> emptyArray(): Array<T> = js("[]")
@library
public fun <T> arrayOf(vararg elements: T): Array<T> = definedExternally
@library
public fun doubleArrayOf(vararg elements: Double): DoubleArray = definedExternally
@library
public fun floatArrayOf(vararg elements: Float): FloatArray = definedExternally
@library
public fun longArrayOf(vararg elements: Long): LongArray = definedExternally
@library
public fun intArrayOf(vararg elements: Int): IntArray = definedExternally
@library
public fun charArrayOf(vararg elements: Char): CharArray = definedExternally
@library
public fun shortArrayOf(vararg elements: Short): ShortArray = definedExternally
@library
public fun byteArrayOf(vararg elements: Byte): ByteArray = definedExternally
@library
public fun booleanArrayOf(vararg elements: Boolean): BooleanArray = definedExternally
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
*/
public actual fun <T> lazy(initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
*
* The [mode] parameter is ignored. */
public actual fun <T> lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
/**
* Creates a new instance of the [Lazy] that uses the specified initialization function [initializer].
*
* The [lock] parameter is ignored.
*/
public actual fun <T> lazy(lock: Any?, initializer: () -> T): Lazy<T> = UnsafeLazyImpl(initializer)
internal fun fillFrom(src: dynamic, dst: dynamic): dynamic {
val srcLen: Int = src.length
val dstLen: Int = dst.length
var index: Int = 0
while (index < srcLen && index < dstLen) dst[index] = src[index++]
return dst
}
internal fun arrayCopyResize(source: dynamic, newSize: Int, defaultValue: Any?): dynamic {
val result = source.slice(0, newSize)
copyArrayType(source, result)
var index: Int = source.length
if (newSize > index) {
result.length = newSize
while (index < newSize) result[index++] = defaultValue
}
return result
}
internal fun <T> arrayPlusCollection(array: dynamic, collection: Collection<T>): dynamic {
return TODO()
// val result = array.slice()
// result.length += collection.size
// copyArrayType(array, result)
// var index: Int = array.length
// for (element in collection) result[index++] = element
// return result
}
internal fun <T> fillFromCollection(dst: dynamic, startIndex: Int, collection: Collection<T>): dynamic {
var index = startIndex
for (element in collection) dst[index++] = element
return dst
}
internal inline fun copyArrayType(from: dynamic, to: dynamic) {
if (from.`$type$` !== undefined) {
to.`$type$` = from.`$type$`
}
}
+4 -1
View File
@@ -258,7 +258,10 @@ internal fun Long.divide(other: Long): Long {
// We will tweak the approximate result by changing it in the 48-th digit or
// the smallest non-fractional digit, whichever is larger.
val log2 = js("Math.ceil(Math.log(approx2) / Math.LN2)").unsafeCast<Int>()
val delta = if (log2 <= 48) 1.0 else js("Math.pow(2, log2 - 48)").unsafeCast<Double>()
// TODO: log2 is renamed but usage in js() functions is not
val log2_minus48 = log2 - 48
val delta = if (log2 <= 48) 1.0 else js("Math.pow(2, log2_minus48)").unsafeCast<Double>()
// Decrease the approximation until it is smaller than the remainder. Note
// that if it is too large, the product overflows and is negative.