[stdlib] Merge js-ir specific sources into common js sources
This commit is contained in:
committed by
Space Team
parent
f00d4022c4
commit
911fa3bbbb
@@ -1,5 +1,7 @@
|
||||
This directory contains shared sources of Kotlin/JS Standard Library for current and IR backends.
|
||||
## Kotlin Standard Library for JS
|
||||
|
||||
Note that `stdlib/js/src/generated` is not shared but used exclusively for current `js-v1` backend.
|
||||
This directory contains Kotlin/JS specific sources of Kotlin standard library
|
||||
that are used together common sources to produce the `kotlin-stdlib-js` artifact.
|
||||
|
||||
Kotlin/JS Standard Library module is moved to `libraries/stdlib/js-v1`.
|
||||
Additional sources are copied during the build from `/core/builtins/` except those builtins that
|
||||
have a more specific version for K/JS (see `builtins` subdirectory).
|
||||
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress(
|
||||
"NON_ABSTRACT_FUNCTION_WITH_NO_BODY",
|
||||
"MUST_BE_INITIALIZED_OR_BE_ABSTRACT",
|
||||
"EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE",
|
||||
"PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED",
|
||||
"WRONG_MODIFIER_TARGET",
|
||||
"UNUSED_PARAMETER"
|
||||
)
|
||||
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* An array of bytes. When targeting the JVM, instances of this class are represented as `byte[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
public class ByteArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Byte)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Byte
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Byte): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): ByteIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of chars. When targeting the JVM, instances of this class are represented as `char[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to null char (`\u0000').
|
||||
*/
|
||||
public class CharArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Char)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Char
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Char): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): CharIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of shorts. When targeting the JVM, instances of this class are represented as `short[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
public class ShortArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Short)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Short
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Short): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): ShortIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of ints. When targeting the JVM, instances of this class are represented as `int[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
public class IntArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Int)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Int
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Int): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): IntIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of longs. When targeting the JVM, instances of this class are represented as `long[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
public class LongArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Long)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Long
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Long): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): LongIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of floats. When targeting the JVM, instances of this class are represented as `float[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
public class FloatArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Float)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Float
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Float): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): FloatIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of doubles. When targeting the JVM, instances of this class are represented as `double[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
public class DoubleArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Double)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Double
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Double): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): DoubleIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of booleans. When targeting the JVM, instances of this class are represented as `boolean[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to `false`.
|
||||
*/
|
||||
public class BooleanArray(size: Int) {
|
||||
/**
|
||||
* Creates a new array of the specified [size], where each element is calculated by calling the specified
|
||||
* [init] function.
|
||||
*
|
||||
* The function [init] is called for each array element sequentially starting from the first one.
|
||||
* It should return the value for an array element given its index.
|
||||
*/
|
||||
public inline constructor(size: Int, init: (Int) -> Boolean)
|
||||
|
||||
/** Returns the array element at the given [index]. This method can be called using the index operator. */
|
||||
public operator fun get(index: Int): Boolean
|
||||
/** Sets the element at the given [index] to the given [value]. This method can be called using the index operator. */
|
||||
public operator fun set(index: Int, value: Boolean): Unit
|
||||
|
||||
/** Returns the number of elements in the array. */
|
||||
public val size: Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): BooleanIterator
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
// Generated by org.jetbrains.kotlin.generators.builtins.numbers.primitives.JsBooleanGenerator
|
||||
|
||||
@file:Suppress("NON_ABSTRACT_FUNCTION_WITH_NO_BODY", "UNUSED_PARAMETER")
|
||||
|
||||
package kotlin
|
||||
|
||||
/** Represents a value which is either `true` or `false`. */
|
||||
public class Boolean private constructor() : Comparable<Boolean> {
|
||||
@SinceKotlin("1.3")
|
||||
companion object {}
|
||||
|
||||
/** Returns the inverse of this boolean. */
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public operator fun not(): Boolean
|
||||
|
||||
/**
|
||||
* Performs a logical `and` operation between this Boolean and the [other] one. Unlike the `&&` operator,
|
||||
* this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated.
|
||||
*/
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public infix fun and(other: Boolean): Boolean
|
||||
|
||||
/**
|
||||
* Performs a logical `or` operation between this Boolean and the [other] one. Unlike the `||` operator,
|
||||
* this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated.
|
||||
*/
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public infix fun or(other: Boolean): Boolean
|
||||
|
||||
/** Performs a logical `xor` operation between this Boolean and the [other] one. */
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public infix fun xor(other: Boolean): Boolean
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun compareTo(other: Boolean): Int
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun toString(): String
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun equals(other: Any?): Boolean
|
||||
|
||||
public override fun hashCode(): Int
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
// Auto-generated file. DO NOT EDIT!
|
||||
// Generated by org.jetbrains.kotlin.generators.builtins.numbers.primitives.JsCharGenerator
|
||||
|
||||
package kotlin
|
||||
|
||||
// Char is a magic class.
|
||||
// Char is defined as a regular class, but we lower it as a value class.
|
||||
// See [org.jetbrains.kotlin.ir.backend.js.utils.JsInlineClassesUtils.isClassInlineLike] for explanation.
|
||||
|
||||
/** Represents a 16-bit Unicode character. */
|
||||
public class Char
|
||||
@kotlin.internal.LowPriorityInOverloadResolution
|
||||
internal constructor(private val value: Int) : Comparable<Char> {
|
||||
@SinceKotlin("1.5")
|
||||
@WasExperimental(ExperimentalStdlibApi::class)
|
||||
public constructor(code: UShort) : this(code.toInt())
|
||||
|
||||
/**
|
||||
* Compares this value with the specified value for order.
|
||||
*
|
||||
* Returns zero if this value is equal to the specified other value, a negative number if it's less than other,
|
||||
* or a positive number if it's greater than other.
|
||||
*/
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun compareTo(other: Char): Int =
|
||||
value - other.value
|
||||
|
||||
/** Adds the other Int value to this value resulting a Char. */
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public operator fun plus(other: Int): Char =
|
||||
(value + other).toChar()
|
||||
|
||||
/** Subtracts the other Char value from this value resulting an Int. */
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public operator fun minus(other: Char): Int =
|
||||
value - other.value
|
||||
|
||||
/** Subtracts the other Int value from this value resulting a Char. */
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public operator fun minus(other: Int): Char =
|
||||
(value - other).toChar()
|
||||
|
||||
/**
|
||||
* Returns this value incremented by one.
|
||||
*
|
||||
* @sample samples.misc.Builtins.inc
|
||||
*/
|
||||
public operator fun inc(): Char =
|
||||
(value + 1).toChar()
|
||||
|
||||
/**
|
||||
* Returns this value decremented by one.
|
||||
*
|
||||
* @sample samples.misc.Builtins.dec
|
||||
*/
|
||||
public operator fun dec(): Char =
|
||||
(value - 1).toChar()
|
||||
|
||||
/** Creates a range from this value to the specified [other] value. */
|
||||
public operator fun rangeTo(other: Char): CharRange =
|
||||
CharRange(this, other)
|
||||
|
||||
/**
|
||||
* Creates a range from this value up to but excluding the specified [other] value.
|
||||
*
|
||||
* If the [other] value is less than or equal to `this` value, then the returned range is empty.
|
||||
*/
|
||||
@SinceKotlin("1.9")
|
||||
@WasExperimental(ExperimentalStdlibApi::class)
|
||||
public operator fun rangeUntil(other: Char): CharRange =
|
||||
this until other
|
||||
|
||||
/** Returns the value of this character as a `Byte`. */
|
||||
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toByte()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.5")
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toByte(): Byte =
|
||||
value.toByte()
|
||||
|
||||
/** Returns the value of this character as a `Char`. */
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toChar(): Char =
|
||||
this
|
||||
|
||||
/** Returns the value of this character as a `Short`. */
|
||||
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toShort()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.5")
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toShort(): Short =
|
||||
value.toShort()
|
||||
|
||||
/** Returns the value of this character as a `Int`. */
|
||||
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.5")
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toInt(): Int =
|
||||
value
|
||||
|
||||
/** Returns the value of this character as a `Long`. */
|
||||
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toLong()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.5")
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toLong(): Long =
|
||||
value.toLong()
|
||||
|
||||
/** Returns the value of this character as a `Float`. */
|
||||
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toFloat()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.5")
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toFloat(): Float =
|
||||
value.toFloat()
|
||||
|
||||
/** Returns the value of this character as a `Double`. */
|
||||
@Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toDouble()"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.5")
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public fun toDouble(): Double =
|
||||
value.toDouble()
|
||||
|
||||
// TODO implicit usages of toString and valueOf must be covered in DCE
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
@Suppress("JS_NAME_PROHIBITED_FOR_OVERRIDE")
|
||||
@JsName("toString")
|
||||
public override fun toString(): String {
|
||||
return js("String").fromCharCode(value).unsafeCast<String>()
|
||||
}
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun equals(other: Any?): Boolean {
|
||||
if (other !is Char) return false
|
||||
return this.value == other.value
|
||||
}
|
||||
|
||||
public override fun hashCode(): Int =
|
||||
value
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* The minimum value of a character code unit.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public const val MIN_VALUE: Char = '\u0000'
|
||||
|
||||
/**
|
||||
* The maximum value of a character code unit.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public const val MAX_VALUE: Char = '\uFFFF'
|
||||
|
||||
/**
|
||||
* The minimum value of a Unicode high-surrogate code unit.
|
||||
*/
|
||||
public const val MIN_HIGH_SURROGATE: Char = '\uD800'
|
||||
|
||||
/**
|
||||
* The maximum value of a Unicode high-surrogate code unit.
|
||||
*/
|
||||
public const val MAX_HIGH_SURROGATE: Char = '\uDBFF'
|
||||
|
||||
/**
|
||||
* The minimum value of a Unicode low-surrogate code unit.
|
||||
*/
|
||||
public const val MIN_LOW_SURROGATE: Char = '\uDC00'
|
||||
|
||||
/**
|
||||
* The maximum value of a Unicode low-surrogate code unit.
|
||||
*/
|
||||
public const val MAX_LOW_SURROGATE: Char = '\uDFFF'
|
||||
|
||||
/**
|
||||
* The minimum value of a Unicode surrogate code unit.
|
||||
*/
|
||||
public const val MIN_SURROGATE: Char = MIN_HIGH_SURROGATE
|
||||
|
||||
/**
|
||||
* The maximum value of a Unicode surrogate code unit.
|
||||
*/
|
||||
public const val MAX_SURROGATE: Char = MAX_LOW_SURROGATE
|
||||
|
||||
/**
|
||||
* The number of bytes used to represent a Char in a binary form.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public const val SIZE_BYTES: Int = 2
|
||||
|
||||
/**
|
||||
* The number of bits used to represent a Char in a binary form.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public const val SIZE_BITS: Int = 16
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,435 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress(
|
||||
"NON_ABSTRACT_FUNCTION_WITH_NO_BODY",
|
||||
"MUST_BE_INITIALIZED_OR_BE_ABSTRACT",
|
||||
"EXTERNAL_TYPE_EXTENDS_NON_EXTERNAL_TYPE",
|
||||
"PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED",
|
||||
"WRONG_MODIFIER_TARGET"
|
||||
)
|
||||
/*
|
||||
* Copyright 2010-2015 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.collections
|
||||
|
||||
/**
|
||||
* Classes that inherit from this interface can be represented as a sequence of elements that can
|
||||
* be iterated over.
|
||||
* @param T the type of element being iterated over. The iterator is covariant in its element type.
|
||||
*/
|
||||
public interface Iterable<out T> {
|
||||
/**
|
||||
* Returns an iterator over the elements of this object.
|
||||
*/
|
||||
public operator fun iterator(): Iterator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Classes that inherit from this interface can be represented as a sequence of elements that can
|
||||
* be iterated over and that supports removing elements during iteration.
|
||||
* @param T the type of element being iterated over. The mutable iterator is invariant in its element type.
|
||||
*/
|
||||
public interface MutableIterable<out T> : Iterable<T> {
|
||||
/**
|
||||
* Returns an iterator over the elements of this sequence that supports removing elements during iteration.
|
||||
*/
|
||||
override fun iterator(): MutableIterator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic collection of elements. Methods in this interface support only read-only access to the collection;
|
||||
* read/write access is supported through the [MutableCollection] interface.
|
||||
* @param E the type of elements contained in the collection. The collection is covariant in its element type.
|
||||
*/
|
||||
public interface Collection<out E> : Iterable<E> {
|
||||
// Query Operations
|
||||
/**
|
||||
* Returns the size of the collection.
|
||||
*/
|
||||
public val size: Int
|
||||
|
||||
/**
|
||||
* Returns `true` if the collection is empty (contains no elements), `false` otherwise.
|
||||
*/
|
||||
public fun isEmpty(): Boolean
|
||||
|
||||
/**
|
||||
* Checks if the specified element is contained in this collection.
|
||||
*/
|
||||
public operator fun contains(element: @UnsafeVariance E): Boolean
|
||||
|
||||
override fun iterator(): Iterator<E>
|
||||
|
||||
// Bulk Operations
|
||||
/**
|
||||
* Checks if all elements in the specified collection are contained in this collection.
|
||||
*/
|
||||
public fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic collection of elements that supports adding and removing elements.
|
||||
*
|
||||
* @param E the type of elements contained in the collection. The mutable collection is invariant in its element type.
|
||||
*/
|
||||
public interface MutableCollection<E> : Collection<E>, MutableIterable<E> {
|
||||
// Query Operations
|
||||
override fun iterator(): MutableIterator<E>
|
||||
|
||||
// Modification Operations
|
||||
/**
|
||||
* Adds the specified element to the collection.
|
||||
*
|
||||
* @return `true` if the element has been added, `false` if the collection does not support duplicates
|
||||
* and the element is already contained in the collection.
|
||||
*/
|
||||
public fun add(element: E): Boolean
|
||||
|
||||
/**
|
||||
* Removes a single instance of the specified element from this
|
||||
* collection, if it is present.
|
||||
*
|
||||
* @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
|
||||
*/
|
||||
public fun remove(element: E): Boolean
|
||||
|
||||
// Bulk Modification Operations
|
||||
/**
|
||||
* Adds all of the elements of the specified collection to this collection.
|
||||
*
|
||||
* @return `true` if any of the specified elements was added to the collection, `false` if the collection was not modified.
|
||||
*/
|
||||
public fun addAll(elements: Collection<E>): Boolean
|
||||
|
||||
/**
|
||||
* Removes all of this collection's elements that are also contained in the specified collection.
|
||||
*
|
||||
* @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified.
|
||||
*/
|
||||
public fun removeAll(elements: Collection<E>): Boolean
|
||||
|
||||
/**
|
||||
* Retains only the elements in this collection that are contained in the specified collection.
|
||||
*
|
||||
* @return `true` if any element was removed from the collection, `false` if the collection was not modified.
|
||||
*/
|
||||
public fun retainAll(elements: Collection<E>): Boolean
|
||||
|
||||
/**
|
||||
* Removes all elements from this collection.
|
||||
*/
|
||||
public fun clear(): Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic ordered collection of elements. Methods in this interface support only read-only access to the list;
|
||||
* read/write access is supported through the [MutableList] interface.
|
||||
* @param E the type of elements contained in the list. The list is covariant in its element type.
|
||||
*/
|
||||
public interface List<out E> : Collection<E> {
|
||||
// Query Operations
|
||||
|
||||
override val size: Int
|
||||
override fun isEmpty(): Boolean
|
||||
override fun contains(element: @UnsafeVariance E): Boolean
|
||||
override fun iterator(): Iterator<E>
|
||||
|
||||
// Bulk Operations
|
||||
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
|
||||
|
||||
// Positional Access Operations
|
||||
/**
|
||||
* Returns the element at the specified index in the list.
|
||||
*/
|
||||
public operator fun get(index: Int): E
|
||||
|
||||
// Search Operations
|
||||
/**
|
||||
* Returns the index of the first occurrence of the specified element in the list, or -1 if the specified
|
||||
* element is not contained in the list.
|
||||
*/
|
||||
public fun indexOf(element: @UnsafeVariance E): Int
|
||||
|
||||
/**
|
||||
* Returns the index of the last occurrence of the specified element in the list, or -1 if the specified
|
||||
* element is not contained in the list.
|
||||
*/
|
||||
public fun lastIndexOf(element: @UnsafeVariance E): Int
|
||||
|
||||
// List Iterators
|
||||
/**
|
||||
* Returns a list iterator over the elements in this list (in proper sequence).
|
||||
*/
|
||||
public fun listIterator(): ListIterator<E>
|
||||
|
||||
/**
|
||||
* Returns a list iterator over the elements in this list (in proper sequence), starting at the specified [index].
|
||||
*/
|
||||
public fun listIterator(index: Int): ListIterator<E>
|
||||
|
||||
// View
|
||||
/**
|
||||
* Returns a view of the portion of this list between the specified [fromIndex] (inclusive) and [toIndex] (exclusive).
|
||||
* The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.
|
||||
*
|
||||
* Structural changes in the base list make the behavior of the view undefined.
|
||||
*/
|
||||
public fun subList(fromIndex: Int, toIndex: Int): List<E>
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic ordered collection of elements that supports adding and removing elements.
|
||||
* @param E the type of elements contained in the list. The mutable list is invariant in its element type.
|
||||
*/
|
||||
public interface MutableList<E> : List<E>, MutableCollection<E> {
|
||||
// Modification Operations
|
||||
/**
|
||||
* Adds the specified element to the end of this list.
|
||||
*
|
||||
* @return `true` because the list is always modified as the result of this operation.
|
||||
*/
|
||||
override fun add(element: E): Boolean
|
||||
|
||||
override fun remove(element: E): Boolean
|
||||
|
||||
// Bulk Modification Operations
|
||||
/**
|
||||
* Adds all of the elements of the specified collection to the end of this list.
|
||||
*
|
||||
* The elements are appended in the order they appear in the [elements] collection.
|
||||
*
|
||||
* @return `true` if the list was changed as the result of the operation.
|
||||
*/
|
||||
override fun addAll(elements: Collection<E>): Boolean
|
||||
|
||||
/**
|
||||
* Inserts all of the elements of the specified collection [elements] into this list at the specified [index].
|
||||
*
|
||||
* @return `true` if the list was changed as the result of the operation.
|
||||
*/
|
||||
public fun addAll(index: Int, elements: Collection<E>): Boolean
|
||||
|
||||
override fun removeAll(elements: Collection<E>): Boolean
|
||||
override fun retainAll(elements: Collection<E>): Boolean
|
||||
override fun clear(): Unit
|
||||
|
||||
// Positional Access Operations
|
||||
/**
|
||||
* Replaces the element at the specified position in this list with the specified element.
|
||||
*
|
||||
* @return the element previously at the specified position.
|
||||
*/
|
||||
public operator fun set(index: Int, element: E): E
|
||||
|
||||
/**
|
||||
* Inserts an element into the list at the specified [index].
|
||||
*/
|
||||
public fun add(index: Int, element: E): Unit
|
||||
|
||||
/**
|
||||
* Removes an element at the specified [index] from the list.
|
||||
*
|
||||
* @return the element that has been removed.
|
||||
*/
|
||||
public fun removeAt(index: Int): E
|
||||
|
||||
// List Iterators
|
||||
override fun listIterator(): MutableListIterator<E>
|
||||
|
||||
override fun listIterator(index: Int): MutableListIterator<E>
|
||||
|
||||
// View
|
||||
override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic unordered collection of elements that does not support duplicate elements.
|
||||
* Methods in this interface support only read-only access to the set;
|
||||
* read/write access is supported through the [MutableSet] interface.
|
||||
* @param E the type of elements contained in the set. The set is covariant in its element type.
|
||||
*/
|
||||
public interface Set<out E> : Collection<E> {
|
||||
// Query Operations
|
||||
|
||||
override val size: Int
|
||||
override fun isEmpty(): Boolean
|
||||
override fun contains(element: @UnsafeVariance E): Boolean
|
||||
override fun iterator(): Iterator<E>
|
||||
|
||||
// Bulk Operations
|
||||
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* A generic unordered collection of elements that does not support duplicate elements, and supports
|
||||
* adding and removing elements.
|
||||
* @param E the type of elements contained in the set. The mutable set is invariant in its element type.
|
||||
*/
|
||||
public interface MutableSet<E> : Set<E>, MutableCollection<E> {
|
||||
// Query Operations
|
||||
override fun iterator(): MutableIterator<E>
|
||||
|
||||
// Modification Operations
|
||||
|
||||
/**
|
||||
* Adds the specified element to the set.
|
||||
*
|
||||
* @return `true` if the element has been added, `false` if the element is already contained in the set.
|
||||
*/
|
||||
override fun add(element: E): Boolean
|
||||
|
||||
override fun remove(element: E): Boolean
|
||||
|
||||
// Bulk Modification Operations
|
||||
|
||||
override fun addAll(elements: Collection<E>): Boolean
|
||||
override fun removeAll(elements: Collection<E>): Boolean
|
||||
override fun retainAll(elements: Collection<E>): Boolean
|
||||
override fun clear(): Unit
|
||||
}
|
||||
|
||||
/**
|
||||
* A collection that holds pairs of objects (keys and values) and supports efficiently retrieving
|
||||
* the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
|
||||
* Methods in this interface support only read-only access to the map; read-write access is supported through
|
||||
* the [MutableMap] interface.
|
||||
* @param K the type of map keys. The map is invariant in its key type, as it
|
||||
* can accept key as a parameter (of [containsKey] for example) and return it in [keys] set.
|
||||
* @param V the type of map values. The map is covariant in its value type.
|
||||
*/
|
||||
public interface Map<K, out V> {
|
||||
// Query Operations
|
||||
/**
|
||||
* Returns the number of key/value pairs in the map.
|
||||
*/
|
||||
public val size: Int
|
||||
|
||||
/**
|
||||
* Returns `true` if the map is empty (contains no elements), `false` otherwise.
|
||||
*/
|
||||
public fun isEmpty(): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if the map contains the specified [key].
|
||||
*/
|
||||
public fun containsKey(key: K): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if the map maps one or more keys to the specified [value].
|
||||
*/
|
||||
public fun containsValue(value: @UnsafeVariance V): Boolean
|
||||
|
||||
/**
|
||||
* Returns the value corresponding to the given [key], or `null` if such a key is not present in the map.
|
||||
*/
|
||||
public operator fun get(key: K): V?
|
||||
|
||||
// Views
|
||||
/**
|
||||
* Returns a read-only [Set] of all keys in this map.
|
||||
*/
|
||||
public val keys: Set<K>
|
||||
|
||||
/**
|
||||
* Returns a read-only [Collection] of all values in this map. Note that this collection may contain duplicate values.
|
||||
*/
|
||||
public val values: Collection<V>
|
||||
|
||||
/**
|
||||
* Returns a read-only [Set] of all key/value pairs in this map.
|
||||
*/
|
||||
public val entries: Set<Map.Entry<K, V>>
|
||||
|
||||
/**
|
||||
* Represents a key/value pair held by a [Map].
|
||||
*/
|
||||
public interface Entry<out K, out V> {
|
||||
/**
|
||||
* Returns the key of this key/value pair.
|
||||
*/
|
||||
public val key: K
|
||||
|
||||
/**
|
||||
* Returns the value of this key/value pair.
|
||||
*/
|
||||
public val value: V
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A modifiable collection that holds pairs of objects (keys and values) and supports efficiently retrieving
|
||||
* the value corresponding to each key. Map keys are unique; the map holds only one value for each key.
|
||||
* @param K the type of map keys. The map is invariant in its key type.
|
||||
* @param V the type of map values. The mutable map is invariant in its value type.
|
||||
*/
|
||||
public interface MutableMap<K, V> : Map<K, V> {
|
||||
// Modification Operations
|
||||
/**
|
||||
* Associates the specified [value] with the specified [key] in the map.
|
||||
*
|
||||
* @return the previous value associated with the key, or `null` if the key was not present in the map.
|
||||
*/
|
||||
public fun put(key: K, value: V): V?
|
||||
|
||||
/**
|
||||
* Removes the specified key and its corresponding value from this map.
|
||||
*
|
||||
* @return the previous value associated with the key, or `null` if the key was not present in the map.
|
||||
*/
|
||||
public fun remove(key: K): V?
|
||||
|
||||
// Bulk Modification Operations
|
||||
/**
|
||||
* Updates this map with key/value pairs from the specified map [from].
|
||||
*/
|
||||
public fun putAll(from: Map<out K, V>): Unit
|
||||
|
||||
/**
|
||||
* Removes all elements from this map.
|
||||
*/
|
||||
public fun clear(): Unit
|
||||
|
||||
// Views
|
||||
/**
|
||||
* Returns a [MutableSet] of all keys in this map.
|
||||
*/
|
||||
override val keys: MutableSet<K>
|
||||
|
||||
/**
|
||||
* Returns a [MutableCollection] of all values in this map. Note that this collection may contain duplicate values.
|
||||
*/
|
||||
override val values: MutableCollection<V>
|
||||
|
||||
/**
|
||||
* Returns a [MutableSet] of all key/value pairs in this map.
|
||||
*/
|
||||
override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
|
||||
|
||||
/**
|
||||
* Represents a key/value pair held by a [MutableMap].
|
||||
*/
|
||||
public interface MutableEntry<K, V> : Map.Entry<K, V> {
|
||||
/**
|
||||
* Changes the value associated with the key of this entry.
|
||||
*
|
||||
* @return the previous value corresponding to the key.
|
||||
*/
|
||||
public fun setValue(newValue: V): V
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
import kotlin.js.*
|
||||
|
||||
abstract class Enum<E : Enum<E>>(@kotlin.internal.IntrinsicConstEvaluation val name: String, val ordinal: Int) : Comparable<E> {
|
||||
|
||||
final override fun compareTo(other: E) = ordinal.compareTo(other.ordinal)
|
||||
|
||||
final override fun equals(other: Any?) = this === other
|
||||
|
||||
final override fun hashCode(): Int = identityHashCode(this)
|
||||
|
||||
override fun toString() = name
|
||||
|
||||
companion object
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("NOTHING_TO_INLINE")
|
||||
|
||||
package kotlin
|
||||
|
||||
import kotlin.js.*
|
||||
|
||||
/**
|
||||
* Returns a string representation of the object. Can be called with a null receiver, in which case
|
||||
* it returns the string "null".
|
||||
*/
|
||||
public fun Any?.toString(): String = this?.toString() ?: "null"
|
||||
|
||||
|
||||
/**
|
||||
* Concatenates this string with the string representation of the given [other] object. If either the receiver
|
||||
* or the [other] object are null, they are represented as the string "null".
|
||||
*/
|
||||
public operator fun String?.plus(other: Any?): String =
|
||||
(this?.toString() ?: "null").plus(other?.toString() ?: "null")
|
||||
|
||||
/**
|
||||
* Returns an array of objects of the given type with the given [size], initialized with null values.
|
||||
*/
|
||||
public inline fun <T> arrayOfNulls(size: Int): Array<T?> = fillArrayVal<T?>(Array<T?>(size), null)
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified elements.
|
||||
*/
|
||||
public inline fun <T> arrayOf(vararg elements: T): Array<T> = elements.unsafeCast<Array<T>>()
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified [Double] numbers.
|
||||
*/
|
||||
public inline fun doubleArrayOf(vararg elements: Double): DoubleArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified [Float] numbers.
|
||||
*/
|
||||
public inline fun floatArrayOf(vararg elements: Float): FloatArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified [Long] numbers.
|
||||
*/
|
||||
public inline fun longArrayOf(vararg elements: Long): LongArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified [Int] numbers.
|
||||
*/
|
||||
public inline fun intArrayOf(vararg elements: Int): IntArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified characters.
|
||||
*/
|
||||
public inline fun charArrayOf(vararg elements: Char): CharArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified [Short] numbers.
|
||||
*/
|
||||
public inline fun shortArrayOf(vararg elements: Short): ShortArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified [Byte] numbers.
|
||||
*/
|
||||
public inline fun byteArrayOf(vararg elements: Byte): ByteArray = elements
|
||||
|
||||
/**
|
||||
* Returns an array containing the specified boolean values.
|
||||
*/
|
||||
public inline fun booleanArrayOf(vararg elements: Boolean): BooleanArray = elements
|
||||
|
||||
// Use non-inline calls to enumValuesIntrinsic and enumValueOfIntrinsic calls in order
|
||||
// for compiler to replace them with method calls of concrete enum classes after inlining.
|
||||
// TODO: Figure out better solution (Inline hacks? Dynamic calls to stable mangled names?)
|
||||
|
||||
/**
|
||||
* Returns an array containing enum T entries.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public inline fun <reified T : Enum<T>> enumValues(): Array<T> = enumValuesIntrinsic<T>()
|
||||
|
||||
/**
|
||||
* Returns an enum entry with specified name.
|
||||
*/
|
||||
@SinceKotlin("1.1")
|
||||
public inline fun <reified T : Enum<T>> enumValueOf(name: String): T = enumValueOfIntrinsic<T>(name)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("NON_ABSTRACT_FUNCTION_WITH_NO_BODY", "MUST_BE_INITIALIZED_OR_BE_ABSTRACT", "UNUSED_PARAMETER")
|
||||
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* The `String` class represents character strings. All string literals in Kotlin programs, such as `"abc"`, are
|
||||
* implemented as instances of this class.
|
||||
*/
|
||||
public class String : Comparable<String>, CharSequence {
|
||||
companion object {}
|
||||
|
||||
/**
|
||||
* Returns a string obtained by concatenating this string with the string representation of the given [other] object.
|
||||
*/
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public operator fun plus(other: Any?): String
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override val length: Int
|
||||
|
||||
/**
|
||||
* Returns the character of this string at the specified [index].
|
||||
*
|
||||
* If the [index] is out of bounds of this string, throws an [IndexOutOfBoundsException] except in Kotlin/JS
|
||||
* where the behavior is unspecified.
|
||||
*/
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun get(index: Int): Char
|
||||
|
||||
public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun compareTo(other: String): Int
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun equals(other: Any?): Boolean
|
||||
|
||||
public override fun hashCode(): Int
|
||||
|
||||
@kotlin.internal.IntrinsicConstEvaluation
|
||||
public override fun toString(): String
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
/**
|
||||
* The base class for all errors and exceptions. Only instances of this class can be thrown or caught.
|
||||
*
|
||||
* @param message the detail message string.
|
||||
* @param cause the cause of this throwable.
|
||||
*/
|
||||
@JsName("Error")
|
||||
public external open class Throwable {
|
||||
public open val message: String?
|
||||
public open val cause: Throwable?
|
||||
|
||||
public constructor(message: String?, cause: Throwable?)
|
||||
public constructor(message: String?)
|
||||
public constructor(cause: Throwable?)
|
||||
public constructor()
|
||||
|
||||
// TODO: add specialized version to runtime
|
||||
// public override fun equals(other: Any?): Boolean
|
||||
// public override fun hashCode(): Int
|
||||
public override fun toString(): String
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal typealias BitMask = IntArray
|
||||
|
||||
private fun bitMaskWith(activeBit: Int): BitMask {
|
||||
val numberIndex = activeBit shr 5
|
||||
val intArray = IntArray(numberIndex + 1)
|
||||
val positionInNumber = activeBit and 31
|
||||
val numberWithSettledBit = 1 shl positionInNumber
|
||||
intArray[numberIndex] = intArray[numberIndex] or numberWithSettledBit
|
||||
return intArray
|
||||
}
|
||||
|
||||
internal fun BitMask.isBitSet(possibleActiveBit: Int): Boolean {
|
||||
val numberIndex = possibleActiveBit shr 5
|
||||
if (numberIndex > size) return false
|
||||
val positionInNumber = possibleActiveBit and 31
|
||||
val numberWithSettledBit = 1 shl positionInNumber
|
||||
return get(numberIndex) and numberWithSettledBit != 0
|
||||
}
|
||||
|
||||
private fun compositeBitMask(capacity: Int, masks: Array<BitMask>): BitMask {
|
||||
return IntArray(capacity) { i ->
|
||||
var result = 0
|
||||
for (mask in masks) {
|
||||
if (i < mask.size) {
|
||||
result = result or mask[i]
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
internal fun implement(interfaces: Array<dynamic>): BitMask {
|
||||
var maxSize = 1
|
||||
val masks = js("[]")
|
||||
|
||||
for (i in interfaces) {
|
||||
var currentSize = maxSize
|
||||
val imask: BitMask? = i.prototype.`$imask$` ?: i.`$imask$`
|
||||
|
||||
if (imask != null) {
|
||||
masks.push(imask)
|
||||
currentSize = imask.size
|
||||
}
|
||||
|
||||
val iid: Int? = i.`$metadata$`.iid
|
||||
val iidImask: BitMask? = iid?.let { bitMaskWith(it) }
|
||||
|
||||
if (iidImask != null) {
|
||||
masks.push(iidImask)
|
||||
currentSize = JsMath.max(currentSize, iidImask.size)
|
||||
}
|
||||
|
||||
if (currentSize > maxSize) {
|
||||
maxSize = currentSize
|
||||
}
|
||||
}
|
||||
|
||||
return compositeBitMask(maxSize, masks)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal object DefaultConstructorMarker
|
||||
@@ -0,0 +1,7 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@JsName("Error")
|
||||
internal open external class JsError(message: String) : Throwable
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
import withType
|
||||
|
||||
@PublishedApi
|
||||
internal external fun <T> Array(size: Int): Array<T>
|
||||
|
||||
@PublishedApi
|
||||
internal fun <T> fillArrayVal(array: Array<T>, initValue: T): Array<T> {
|
||||
for (i in 0..array.size - 1) {
|
||||
array[i] = initValue
|
||||
}
|
||||
return array
|
||||
}
|
||||
|
||||
internal inline fun <T> arrayWithFun(size: Int, init: (Int) -> T) = fillArrayFun(Array<T>(size), init)
|
||||
|
||||
internal inline fun <T> fillArrayFun(array: dynamic, init: (Int) -> T): Array<T> {
|
||||
val result = array.unsafeCast<Array<T>>()
|
||||
var i = 0
|
||||
while (i != result.size) {
|
||||
result[i] = init(i)
|
||||
++i
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
internal fun booleanArray(size: Int): BooleanArray = withType("BooleanArray", fillArrayVal(Array<Boolean>(size), false)).unsafeCast<BooleanArray>()
|
||||
|
||||
internal fun booleanArrayOf(arr: Array<Boolean>): BooleanArray = withType("BooleanArray", arr.asDynamic().slice()).unsafeCast<BooleanArray>()
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun charArray(size: Int): CharArray = withType("CharArray", js("new Uint16Array(size)")).unsafeCast<CharArray>()
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun charArrayOf(arr: Array<Char>): CharArray = withType("CharArray", js("new Uint16Array(arr)")).unsafeCast<CharArray>()
|
||||
|
||||
internal fun longArray(size: Int): LongArray = withType("LongArray", fillArrayVal(Array<Long>(size), 0L)).unsafeCast<LongArray>()
|
||||
|
||||
internal fun longArrayOf(arr: Array<Long>): LongArray = withType("LongArray", arr.asDynamic().slice()).unsafeCast<LongArray>()
|
||||
|
||||
internal fun <T> arrayIterator(array: Array<T>) = object : Iterator<T> {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun next() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun booleanArrayIterator(array: BooleanArray) = object : BooleanIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextBoolean() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun byteArrayIterator(array: ByteArray) = object : ByteIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextByte() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun shortArrayIterator(array: ShortArray) = object : ShortIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextShort() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun charArrayIterator(array: CharArray) = object : CharIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextChar() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun intArrayIterator(array: IntArray) = object : IntIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextInt() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun floatArrayIterator(array: FloatArray) = object : FloatIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextFloat() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun doubleArrayIterator(array: DoubleArray) = object : DoubleIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextDouble() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
|
||||
internal fun longArrayIterator(array: LongArray) = object : LongIterator() {
|
||||
var index = 0
|
||||
override fun hasNext() = index != array.size
|
||||
override fun nextLong() = if (index != array.size) array[index++] else throw NoSuchElementException("$index")
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
// TODO use declarations from stdlib
|
||||
private external class ArrayBuffer(size: Int)
|
||||
private external class Float64Array(buffer: ArrayBuffer)
|
||||
private external class Float32Array(buffer: ArrayBuffer)
|
||||
private external class Int32Array(buffer: ArrayBuffer)
|
||||
|
||||
private val buf = ArrayBuffer(8)
|
||||
// TODO use one DataView instead of bunch of typed views.
|
||||
private val bufFloat64 = Float64Array(buf).unsafeCast<DoubleArray>()
|
||||
private val bufFloat32 = Float32Array(buf).unsafeCast<FloatArray>()
|
||||
private val bufInt32 = Int32Array(buf).unsafeCast<IntArray>()
|
||||
|
||||
private val lowIndex = run {
|
||||
bufFloat64[0] = -1.0 // bff00000_00000000
|
||||
if (bufInt32[0] != 0) 1 else 0
|
||||
}
|
||||
private val highIndex = 1 - lowIndex
|
||||
|
||||
internal fun doubleToRawBits(value: Double): Long {
|
||||
bufFloat64[0] = value
|
||||
return Long(bufInt32[lowIndex], bufInt32[highIndex])
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
internal fun doubleFromBits(value: Long): Double {
|
||||
bufInt32[lowIndex] = value.low
|
||||
bufInt32[highIndex] = value.high
|
||||
return bufFloat64[0]
|
||||
}
|
||||
|
||||
internal fun floatToRawBits(value: Float): Int {
|
||||
bufFloat32[0] = value
|
||||
return bufInt32[0]
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
internal fun floatFromBits(value: Int): Float {
|
||||
bufInt32[0] = value
|
||||
return bufFloat32[0]
|
||||
}
|
||||
|
||||
// returns zero value for number with positive sign bit and non-zero value for number with negative sign bit.
|
||||
internal fun doubleSignBit(value: Double): Int {
|
||||
bufFloat64[0] = value
|
||||
return bufInt32[highIndex] and Int.MIN_VALUE
|
||||
}
|
||||
|
||||
internal fun getNumberHashCode(obj: Double): Int {
|
||||
@Suppress("DEPRECATED_IDENTITY_EQUALS")
|
||||
if (jsBitwiseOr(obj, 0).unsafeCast<Double>() === obj) {
|
||||
return obj.toInt()
|
||||
}
|
||||
|
||||
bufFloat64[0] = obj
|
||||
return bufInt32[highIndex] * 31 + bufInt32[lowIndex]
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
import JsError
|
||||
|
||||
@JsName("Boolean")
|
||||
internal external fun nativeBoolean(obj: Any?): Boolean
|
||||
|
||||
internal fun booleanInExternalLog(name: String, obj: dynamic) {
|
||||
if (jsTypeOf(obj) != "boolean") {
|
||||
console.asDynamic().error("Boolean expected for '$name', but actual:", obj)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun booleanInExternalException(name: String, obj: dynamic) {
|
||||
if (jsTypeOf(obj) != "boolean") {
|
||||
throw JsError("Boolean expected for '$name', but actual: $obj")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal annotation class DoNotIntrinsify
|
||||
|
||||
@PublishedApi
|
||||
@DoNotIntrinsify
|
||||
internal fun charSequenceGet(a: CharSequence, index: Int): Char {
|
||||
return if (isString(a)) {
|
||||
Char(a.asDynamic().charCodeAt(index).unsafeCast<Int>())
|
||||
} else {
|
||||
a[index]
|
||||
}
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
@DoNotIntrinsify
|
||||
internal fun charSequenceLength(a: CharSequence): Int {
|
||||
return if (isString(a)) {
|
||||
a.asDynamic().length.unsafeCast<Int>()
|
||||
} else {
|
||||
a.length
|
||||
}
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
@DoNotIntrinsify
|
||||
internal fun charSequenceSubSequence(a: CharSequence, startIndex: Int, endIndex: Int): CharSequence {
|
||||
return if (isString(a)) {
|
||||
a.asDynamic().substring(startIndex, endIndex).unsafeCast<String>()
|
||||
} else {
|
||||
a.subSequence(startIndex, endIndex)
|
||||
}
|
||||
}
|
||||
|
||||
// Keeping this function as separate non-inline to intrincify `is` operator
|
||||
internal fun isString(a: CharSequence) = a is String
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js.*
|
||||
|
||||
internal fun arrayToString(array: Array<*>) = array.joinToString(", ", "[", "]") { toString(it) }
|
||||
|
||||
@OptIn(ExperimentalUnsignedTypes::class)
|
||||
internal fun <T> Array<out T>?.contentDeepHashCodeInternal(): Int {
|
||||
if (this == null) return 0
|
||||
var result = 1
|
||||
for (element in this) {
|
||||
val elementHash = when {
|
||||
element == null -> 0
|
||||
isArrayish(element) -> (element.unsafeCast<Array<*>>()).contentDeepHashCodeInternal()
|
||||
|
||||
element is UByteArray -> element.contentHashCode()
|
||||
element is UShortArray -> element.contentHashCode()
|
||||
element is UIntArray -> element.contentHashCode()
|
||||
element is ULongArray -> element.contentHashCode()
|
||||
|
||||
else -> element.hashCode()
|
||||
}
|
||||
|
||||
result = 31 * result + elementHash
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
internal fun <T> T.contentEqualsInternal(other: T): Boolean {
|
||||
val a = this.asDynamic()
|
||||
val b = other.asDynamic()
|
||||
|
||||
if (a === b) return true
|
||||
|
||||
if (a == null || b == null || !isArrayish(b) || a.length != b.length) return false
|
||||
|
||||
for (i in 0 until a.length) {
|
||||
if (!equals(a[i], b[i])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
internal fun <T> T.contentHashCodeInternal(): Int {
|
||||
val a = this.asDynamic()
|
||||
if (a == null) return 0
|
||||
|
||||
var result = 1
|
||||
|
||||
for (i in 0 until a.length) {
|
||||
result = result * 31 + hashCode(a[i])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
|
||||
// Adopted from misc.js
|
||||
|
||||
internal fun compareTo(a: dynamic, b: dynamic): Int = when (jsTypeOf(a)) {
|
||||
"number" -> when {
|
||||
jsTypeOf(b) == "number" ->
|
||||
doubleCompareTo(a, b)
|
||||
b is Long ->
|
||||
doubleCompareTo(a, b.toDouble())
|
||||
else ->
|
||||
primitiveCompareTo(a, b)
|
||||
}
|
||||
|
||||
"string", "boolean" -> primitiveCompareTo(a, b)
|
||||
|
||||
else -> compareToDoNotIntrinsicify(a, b)
|
||||
}
|
||||
|
||||
@DoNotIntrinsify
|
||||
private fun <T : Comparable<T>> compareToDoNotIntrinsicify(a: Comparable<T>, b: T) =
|
||||
a.compareTo(b)
|
||||
|
||||
internal fun primitiveCompareTo(a: dynamic, b: dynamic): Int =
|
||||
when {
|
||||
a < b -> -1
|
||||
a > b -> 1
|
||||
else -> 0
|
||||
}
|
||||
|
||||
internal fun doubleCompareTo(a: dynamic, b: dynamic): Int =
|
||||
when {
|
||||
a < b -> -1
|
||||
a > b -> 1
|
||||
|
||||
a === b -> {
|
||||
if (a !== 0)
|
||||
0
|
||||
else {
|
||||
val ia = 1.asDynamic() / a
|
||||
if (ia === 1.asDynamic() / b) {
|
||||
0
|
||||
} else if (ia < 0) {
|
||||
-1
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
a !== a ->
|
||||
if (b !== b) 0 else 1
|
||||
|
||||
else -> -1
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
/**
|
||||
* @param CT is return type of calling constructor (uses in DCE)
|
||||
*/
|
||||
internal fun <CT> construct(constructorType: dynamic, resultType: dynamic, vararg args: Any?): Any {
|
||||
return js("Reflect").construct(constructorType, args, resultType)
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal fun equals(obj1: dynamic, obj2: dynamic): Boolean {
|
||||
if (obj1 == null) {
|
||||
return obj2 == null
|
||||
}
|
||||
if (obj2 == null) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (jsTypeOf(obj1) == "object" && jsTypeOf(obj1.equals) == "function") {
|
||||
return (obj1.equals)(obj2)
|
||||
}
|
||||
|
||||
if (obj1 !== obj1) {
|
||||
return obj2 !== obj2
|
||||
}
|
||||
|
||||
if (jsTypeOf(obj1) == "number" && jsTypeOf(obj2) == "number") {
|
||||
return obj1 === obj2 && (obj1 !== 0 || 1.asDynamic() / obj1 === 1.asDynamic() / obj2)
|
||||
}
|
||||
return obj1 === obj2
|
||||
}
|
||||
|
||||
internal fun toString(o: dynamic): String = when {
|
||||
o == null -> "null"
|
||||
isArrayish(o) -> "[...]"
|
||||
jsTypeOf(o.toString) != "function" -> anyToString(o)
|
||||
else -> (o.toString)().unsafeCast<String>()
|
||||
}
|
||||
|
||||
internal fun anyToString(o: dynamic): String = js("Object").prototype.toString.call(o)
|
||||
|
||||
internal fun hashCode(obj: dynamic): Int {
|
||||
if (obj == null) return 0
|
||||
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
return when (val typeOf = jsTypeOf(obj)) {
|
||||
"object" -> if ("function" === jsTypeOf(obj.hashCode)) (obj.hashCode)() else getObjectHashCode(obj)
|
||||
"function" -> getObjectHashCode(obj)
|
||||
"number" -> getNumberHashCode(obj)
|
||||
"boolean" -> getBooleanHashCode(obj.unsafeCast<Boolean>())
|
||||
"string" -> getStringHashCode(js("String")(obj))
|
||||
"bigint" -> getBigIntHashCode(obj)
|
||||
"symbol" -> getSymbolHashCode(obj)
|
||||
else -> js("throw new Error('Unexpected typeof `' + typeOf + '`')")
|
||||
}
|
||||
}
|
||||
|
||||
internal fun getBooleanHashCode(value: Boolean): Int {
|
||||
return if (value) 1231 else 1237
|
||||
}
|
||||
|
||||
private fun getBigIntHashCode(value: dynamic): Int {
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val shiftNumber = js("BigInt(32)");
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val MASK = js("BigInt(0xffffffff)");
|
||||
|
||||
@Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE")
|
||||
var bigNumber = if (value < 0) -value else value
|
||||
var hashCode = 0
|
||||
val signum = if (value < 0) -1 else 1
|
||||
|
||||
while (bigNumber != 0) {
|
||||
val chunk = js("Number(bigNumber & MASK)").unsafeCast<Int>()
|
||||
hashCode = 31 * hashCode + chunk
|
||||
@Suppress("UNUSED_VALUE")
|
||||
bigNumber = js("bigNumber >> shiftNumber")
|
||||
}
|
||||
|
||||
return hashCode * signum
|
||||
}
|
||||
|
||||
@Suppress("MUST_BE_INITIALIZED")
|
||||
private var symbolWeakMap: dynamic
|
||||
|
||||
@Suppress("MUST_BE_INITIALIZED")
|
||||
private var symbolMap: dynamic
|
||||
|
||||
private fun getSymbolWeakMap(): dynamic {
|
||||
if (symbolWeakMap === VOID) {
|
||||
symbolWeakMap = js("new WeakMap()")
|
||||
}
|
||||
return symbolWeakMap
|
||||
}
|
||||
|
||||
private fun getSymbolMap(): dynamic {
|
||||
if (symbolMap === VOID) {
|
||||
symbolMap = js("new Map()")
|
||||
}
|
||||
return symbolMap
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
private fun symbolIsSharable(symbol: dynamic) = js("Symbol.keyFor(symbol)") != VOID
|
||||
|
||||
private fun getSymbolHashCode(value: dynamic): Int {
|
||||
val hashCodeMap = if (symbolIsSharable(value)) getSymbolMap() else getSymbolWeakMap()
|
||||
val cachedHashCode = hashCodeMap.get(value)
|
||||
|
||||
if (cachedHashCode !== VOID) return cachedHashCode
|
||||
|
||||
val hash = calculateRandomHash()
|
||||
hashCodeMap.set(value, hash)
|
||||
return hash
|
||||
}
|
||||
|
||||
private const val POW_2_32 = 4294967296.0
|
||||
private const val OBJECT_HASH_CODE_PROPERTY_NAME = "kotlinHashCodeValue$"
|
||||
|
||||
private fun calculateRandomHash(): Int {
|
||||
return jsBitwiseOr(js("Math").random() * POW_2_32, 0) // Make 32-bit singed integer.
|
||||
}
|
||||
|
||||
internal fun getObjectHashCode(obj: dynamic): Int {
|
||||
if (!jsIn(OBJECT_HASH_CODE_PROPERTY_NAME, obj)) {
|
||||
var hash = calculateRandomHash()
|
||||
var descriptor = js("new Object()")
|
||||
descriptor.value = hash
|
||||
descriptor.enumerable = false
|
||||
js("Object").defineProperty(obj, OBJECT_HASH_CODE_PROPERTY_NAME, descriptor)
|
||||
}
|
||||
return obj[OBJECT_HASH_CODE_PROPERTY_NAME].unsafeCast<Int>();
|
||||
}
|
||||
|
||||
internal fun getStringHashCode(str: String): Int {
|
||||
var hash = 0
|
||||
val length: Int = str.length // TODO: Implement WString.length
|
||||
for (i in 0..length-1) {
|
||||
val code: Int = str.asDynamic().charCodeAt(i)
|
||||
hash = hash * 31 + code
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
internal fun identityHashCode(obj: Any?): Int = getObjectHashCode(obj)
|
||||
|
||||
internal fun captureStack(instance: Throwable, constructorFunction: Any) {
|
||||
if (js("Error").captureStackTrace != null) {
|
||||
js("Error").captureStackTrace(instance, constructorFunction)
|
||||
} else {
|
||||
instance.asDynamic().stack = js("new Error()").stack
|
||||
}
|
||||
}
|
||||
|
||||
internal fun newThrowable(message: String?, cause: Throwable?): Throwable {
|
||||
val throwable = js("new Error()")
|
||||
throwable.message = if (isUndefined(message)) {
|
||||
if (isUndefined(cause)) message else cause?.toString() ?: VOID
|
||||
} else message ?: VOID
|
||||
throwable.cause = cause
|
||||
throwable.name = "Throwable"
|
||||
return throwable.unsafeCast<Throwable>()
|
||||
}
|
||||
|
||||
internal fun extendThrowable(this_: dynamic, message: String?, cause: Throwable?) {
|
||||
js("Error").call(this_)
|
||||
setPropertiesToThrowableInstance(this_, message, cause)
|
||||
}
|
||||
|
||||
internal fun setPropertiesToThrowableInstance(this_: dynamic, message: String?, cause: Throwable?) {
|
||||
val errorInfo = calculateErrorInfo(JsObject.getPrototypeOf(this_))
|
||||
if ((errorInfo and 0x1) == 0) {
|
||||
@Suppress("IfThenToElvis")
|
||||
this_.message = if (message == null) {
|
||||
@Suppress("SENSELESS_COMPARISON")
|
||||
if (message !== null) {
|
||||
// undefined
|
||||
cause?.toString() ?: VOID
|
||||
} else {
|
||||
// real null
|
||||
VOID
|
||||
}
|
||||
} else message
|
||||
}
|
||||
if ((errorInfo and 0x2) == 0) {
|
||||
this_.cause = cause
|
||||
}
|
||||
this_.name = JsObject.getPrototypeOf(this_).constructor.name
|
||||
}
|
||||
|
||||
@JsName("Object")
|
||||
internal external class JsObject {
|
||||
companion object {
|
||||
fun getPrototypeOf(obj: Any?): dynamic
|
||||
}
|
||||
}
|
||||
|
||||
// Note: once some error-compilation design happened consider to distinguish a special exception for error-code.
|
||||
internal fun errorCode(description: String): Nothing {
|
||||
throw IllegalStateException(description)
|
||||
}
|
||||
|
||||
@Suppress("SENSELESS_COMPARISON")
|
||||
internal fun isUndefined(value: dynamic): Boolean = value === VOID
|
||||
|
||||
internal fun <T, R> boxIntrinsic(@Suppress("UNUSED_PARAMETER") x: T): R = error("Should be lowered")
|
||||
internal fun <T, R> unboxIntrinsic(@Suppress("UNUSED_PARAMETER") x: T): R = error("Should be lowered")
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun protoOf(constructor: Any) =
|
||||
js("constructor.prototype")
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun <T> objectCreate(proto: T?) =
|
||||
js("Object.create(proto)")
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun createThis(ctor: Ctor, box: dynamic): dynamic {
|
||||
val self = js("Object.create(ctor.prototype)")
|
||||
boxApply(self, box)
|
||||
return self
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun boxApply(self: dynamic, box: dynamic) {
|
||||
if (box !== VOID) js("Object.assign(self, box)")
|
||||
}
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE", "REIFIED_TYPE_PARAMETER_NO_INLINE")
|
||||
internal fun <reified T : Any> createExternalThis(
|
||||
ctor: JsClass<T>,
|
||||
superExternalCtor: JsClass<T>,
|
||||
parameters: Array<Any?>,
|
||||
box: dynamic
|
||||
): T {
|
||||
val selfCtor = if (box === VOID) {
|
||||
ctor
|
||||
} else {
|
||||
val newCtor: dynamic = jsNewAnonymousClass(ctor)
|
||||
js("Object.assign(newCtor.prototype, box)")
|
||||
newCtor.constructor = ctor
|
||||
newCtor
|
||||
}
|
||||
return js("Reflect.construct(superExternalCtor, parameters, selfCtor)")
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
internal fun defineProp(obj: Any, name: String, getter: Any?, setter: Any?) =
|
||||
js("Object.defineProperty(obj, name, { configurable: true, get: getter, set: setter })")
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
import kotlin.coroutines.*
|
||||
|
||||
|
||||
@PublishedApi
|
||||
internal fun <T> getContinuation(): Continuation<T> { throw Exception("Implemented as intrinsic") }
|
||||
// Do we really need this intrinsic in JS?
|
||||
|
||||
@PublishedApi
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
internal suspend fun <T> returnIfSuspended(argument: Any?): T {
|
||||
return argument as T
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
internal fun <T> interceptContinuationIfNeeded(
|
||||
context: CoroutineContext,
|
||||
continuation: Continuation<T>
|
||||
) = context[ContinuationInterceptor]?.interceptContinuation(continuation) ?: continuation
|
||||
|
||||
|
||||
@SinceKotlin("1.2")
|
||||
@PublishedApi
|
||||
internal inline suspend fun getCoroutineContext(): CoroutineContext = getContinuation<Any?>().context
|
||||
|
||||
// TODO: remove `JS` suffix oncec `NameGenerator` is implemented
|
||||
@PublishedApi
|
||||
internal inline suspend fun <T> suspendCoroutineUninterceptedOrReturnJS(block: (Continuation<T>) -> Any?): T =
|
||||
returnIfSuspended<T>(block(getContinuation<T>()))
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
import JsError
|
||||
|
||||
internal fun unreachableDeclarationLog() {
|
||||
console.asDynamic().trace("Unreachable declaration")
|
||||
}
|
||||
|
||||
internal fun unreachableDeclarationException() {
|
||||
throw JsError("Unreachable declaration")
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
@JsPolyfill("""
|
||||
(function() {
|
||||
if (typeof globalThis === 'object') return;
|
||||
Object.defineProperty(Object.prototype, '__magic__', {
|
||||
get: function() {
|
||||
return this;
|
||||
},
|
||||
configurable: true
|
||||
});
|
||||
__magic__.globalThis = __magic__;
|
||||
delete Object.prototype.__magic__;
|
||||
}());
|
||||
""")
|
||||
internal external val globalThis: dynamic
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
@PublishedApi
|
||||
internal fun throwUninitializedPropertyAccessException(name: String): Nothing =
|
||||
throw UninitializedPropertyAccessException("lateinit property $name has not been initialized")
|
||||
|
||||
@PublishedApi
|
||||
internal fun throwKotlinNothingValueException(): Nothing =
|
||||
throw KotlinNothingValueException()
|
||||
|
||||
internal fun noWhenBranchMatchedException(): Nothing = throw NoWhenBranchMatchedException()
|
||||
|
||||
internal fun THROW_ISE(): Nothing {
|
||||
throw IllegalStateException()
|
||||
}
|
||||
|
||||
internal fun THROW_CCE(): Nothing {
|
||||
throw ClassCastException()
|
||||
}
|
||||
|
||||
internal fun THROW_NPE(): Nothing {
|
||||
throw NullPointerException()
|
||||
}
|
||||
|
||||
internal fun THROW_IAE(msg: String): Nothing {
|
||||
throw IllegalArgumentException(msg)
|
||||
}
|
||||
|
||||
internal fun <T:Any> ensureNotNull(v: T?): T = if (v == null) THROW_NPE() else v
|
||||
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("NON_MEMBER_FUNCTION_NO_BODY", "UNUSED_PARAMETER", "unused")
|
||||
|
||||
package kotlin.js
|
||||
|
||||
@RequiresOptIn(message = "Here be dragons! This is a compiler intrinsic, proceed with care!")
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
internal annotation class JsIntrinsic
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsEqeq(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsNotEq(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsUndefined(): Nothing?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsEqeqeq(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsNotEqeq(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsGt(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsGtEq(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsLt(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsLtEq(a: Any?, b: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsNot(a: Any?): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsUnaryPlus(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsUnaryMinus(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsPrefixInc(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsPostfixInc(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsPrefixDec(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsPostfixDec(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsPlus(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsMinus(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsMult(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsDiv(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsMod(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsPlusAssign(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsMinusAssign(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsMultAssign(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsDivAssign(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsModAssign(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsAnd(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsOr(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitAnd(a: Any?, b: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitOr(a: Any?, b: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitXor(a: Any?, b: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitNot(a: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitShiftR(a: Any?, b: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitShiftRU(a: Any?, b: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBitShiftL(a: Any?, b: Any?): Int
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsInstanceOfIntrinsic(a: Any?, b: Any?): Boolean
|
||||
|
||||
// @JsIntrinsic
|
||||
// To prevent people to insert @OptIn every time
|
||||
public external fun jsTypeOf(a: Any?): String
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsNewTarget(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun emptyObject(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun openInitializerBox(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsArrayLength(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsArrayGet(a: Any?, b: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsArraySet(a: Any?, b: Any?, c: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun arrayLiteral(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun int8Array(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun int16Array(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun int32Array(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun float32Array(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun float64Array(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun int8ArrayOf(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun int16ArrayOf(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun int32ArrayOf(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun float32ArrayOf(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun float64ArrayOf(a: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> sharedBoxCreate(v: T?): dynamic
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> sharedBoxRead(box: dynamic): T?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> sharedBoxWrite(box: dynamic, nv: T?)
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> DefaultType(): T
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsBind(receiver: Any?, target: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsCall(receiver: Any?, target: Any?, vararg args: Any?): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <A> slice(a: A): A
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> jsArrayLike2Array(arrayLike: Any?): Array<T>
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> jsSliceArrayLikeFromIndex(arrayLike: Any?, start: Int): Array<T>
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun <T> jsSliceArrayLikeFromIndexToIndex(arrayLike: Any?, start: Int, end: Int): Array<T>
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun unreachable(): Nothing
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsArguments(): Any?
|
||||
|
||||
@JsIntrinsic
|
||||
@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")
|
||||
internal fun <reified T : Any> jsNewAnonymousClass(superClass: JsClass<T>): JsClass<T>
|
||||
|
||||
@JsIntrinsic
|
||||
@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE") // TODO: mark `inline` and skip in inliner
|
||||
internal fun <reified T : Any> jsClassIntrinsic(): JsClass<T>
|
||||
|
||||
// Returns true if the specified property is in the specified object or its prototype chain.
|
||||
@JsIntrinsic
|
||||
internal fun jsInIntrinsic(lhs: Any?, rhs: Any): Boolean
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsDelete(e: Any?)
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsContextfulRef(context: dynamic, fn: dynamic): dynamic
|
||||
|
||||
@JsIntrinsic
|
||||
internal fun jsIsEs6(): Boolean
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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("[]")
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@Deprecated("Synchronization on Any? object is not supported.", ReplaceWith("lazy(initializer)"))
|
||||
@DeprecatedSinceKotlin(warningSince = "1.9")
|
||||
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
|
||||
val arr = dst.unsafeCast<Array<Any?>>()
|
||||
while (index < srcLen && index < dstLen) arr[index] = src[index++]
|
||||
return dst
|
||||
}
|
||||
|
||||
|
||||
internal fun arrayCopyResize(source: dynamic, newSize: Int, defaultValue: Any?): dynamic {
|
||||
val result = source.slice(0, newSize).unsafeCast<Array<Any?>>()
|
||||
copyArrayType(source, result)
|
||||
var index: Int = source.length
|
||||
if (newSize > index) {
|
||||
result.asDynamic().length = newSize
|
||||
while (index < newSize) result[index++] = defaultValue
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
internal fun <T> arrayPlusCollection(array: dynamic, collection: Collection<T>): dynamic {
|
||||
val result = array.slice().unsafeCast<Array<T>>()
|
||||
result.asDynamic().length = result.size + collection.size
|
||||
copyArrayType(array, result)
|
||||
var index: Int = array.length
|
||||
for (element in collection) result[index++] = element
|
||||
return result
|
||||
}
|
||||
|
||||
internal inline fun copyArrayType(from: dynamic, to: dynamic) {
|
||||
if (from.`$type$` !== undefined) {
|
||||
to.`$type$` = from.`$type$`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
@PublishedApi
|
||||
internal fun <T : Enum<T>> enumValuesIntrinsic(): Array<T> =
|
||||
throw IllegalStateException("Should be replaced by compiler")
|
||||
|
||||
@PublishedApi
|
||||
internal fun <T : Enum<T>> enumValueOfIntrinsic(@Suppress("UNUSED_PARAMETER") name: String): T =
|
||||
throw IllegalStateException("Should be replaced by compiler")
|
||||
|
||||
// These were necessary for legacy-property-access functionality in IR
|
||||
// But this functionality is not required anymore, so these methods are redundant
|
||||
// But they can't be removed because if old version of compiler will compile against new version of stdlib
|
||||
// (when these methods are removed) compiler will fail with Internal error
|
||||
@Deprecated("This is an intrinsic for removed functionality", level = DeprecationLevel.HIDDEN)
|
||||
internal fun safePropertyGet(self: dynamic, getterName: String, propName: String): dynamic {
|
||||
val getter = self[getterName]
|
||||
return if (getter != null) getter.call(self) else self[propName]
|
||||
}
|
||||
|
||||
@Deprecated("This is an intrinsic for removed functionality", level = DeprecationLevel.HIDDEN)
|
||||
internal fun safePropertySet(self: dynamic, setterName: String, propName: String, value: dynamic) {
|
||||
val setter = self[setterName]
|
||||
if (setter != null) setter.call(self, value) else self[propName] = value
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements annotated function in JavaScript.
|
||||
* [code] string must contain JS expression that evaluates to JS function with signature that matches annotated kotlin function
|
||||
*
|
||||
* For example, a function that adds two Doubles:
|
||||
*
|
||||
* @JsFun("(x, y) => x + y")
|
||||
* fun jsAdd(x: Double, y: Double): Double =
|
||||
* error("...")
|
||||
*
|
||||
* Code gets inserted as is without syntax verification.
|
||||
*/
|
||||
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
|
||||
internal annotation class JsFun(val code: String)
|
||||
|
||||
/**
|
||||
* The annotation is needed for annotating class declarations and type alias which are used inside exported declarations, but
|
||||
* doesn't contain @JsExport annotation
|
||||
* This information is used for generating special tagged types inside d.ts files, for more strict usage of implicitly exported entities
|
||||
*/
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
internal annotation class JsImplicitExport
|
||||
@@ -0,0 +1,395 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
// Copyright 2009 The Closure Library Authors. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
|
||||
package kotlin
|
||||
|
||||
internal fun Long.toNumber() = high * TWO_PWR_32_DBL_ + getLowBitsUnsigned()
|
||||
|
||||
internal fun Long.getLowBitsUnsigned() = if (low >= 0) low.toDouble() else TWO_PWR_32_DBL_ + low
|
||||
|
||||
internal fun hashCode(l: Long) = l.low xor l.high
|
||||
|
||||
internal fun Long.toStringImpl(radix: Int): String {
|
||||
if (radix < 2 || 36 < radix) {
|
||||
throw Exception("radix out of range: $radix")
|
||||
}
|
||||
|
||||
if (isZero()) {
|
||||
return "0"
|
||||
}
|
||||
|
||||
if (isNegative()) {
|
||||
if (equalsLong(MIN_VALUE)) {
|
||||
// We need to change the Long value before it can be negated, so we remove
|
||||
// the bottom-most digit in this base and then recurse to do the rest.
|
||||
val radixLong = fromInt(radix)
|
||||
val div = div(radixLong)
|
||||
val rem = div.multiply(radixLong).subtract(this).toInt()
|
||||
// Using rem.asDynamic() to break dependency on "kotlin.text" package
|
||||
return div.toStringImpl(radix) + rem.asDynamic().toString(radix).unsafeCast<String>()
|
||||
} else {
|
||||
return "-${negate().toStringImpl(radix)}"
|
||||
}
|
||||
}
|
||||
|
||||
// Do several digits each time through the loop, so as to
|
||||
// minimize the calls to the very expensive emulated div.
|
||||
val digitsPerTime = when {
|
||||
radix == 2 -> 31
|
||||
radix <= 10 -> 9
|
||||
radix <= 21 -> 7
|
||||
radix <= 35 -> 6
|
||||
else -> 5
|
||||
}
|
||||
val radixToPower = fromNumber(JsMath.pow(radix.toDouble(), digitsPerTime.toDouble()))
|
||||
|
||||
var rem = this
|
||||
var result = ""
|
||||
while (true) {
|
||||
val remDiv = rem.div(radixToPower)
|
||||
val intval = rem.subtract(remDiv.multiply(radixToPower)).toInt()
|
||||
var digits = intval.asDynamic().toString(radix).unsafeCast<String>()
|
||||
|
||||
rem = remDiv
|
||||
if (rem.isZero()) {
|
||||
return digits + result
|
||||
} else {
|
||||
while (digits.length < digitsPerTime) {
|
||||
digits = "0" + digits
|
||||
}
|
||||
result = digits + result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Long.negate() = unaryMinus()
|
||||
|
||||
internal fun Long.isZero() = high == 0 && low == 0
|
||||
|
||||
internal fun Long.isNegative() = high < 0
|
||||
|
||||
internal fun Long.isOdd() = low and 1 == 1
|
||||
|
||||
internal fun Long.equalsLong(other: Long) = high == other.high && low == other.low
|
||||
|
||||
internal fun Long.lessThan(other: Long) = compare(other) < 0
|
||||
|
||||
internal fun Long.lessThanOrEqual(other: Long) = compare(other) <= 0
|
||||
|
||||
internal fun Long.greaterThan(other: Long) = compare(other) > 0
|
||||
|
||||
internal fun Long.greaterThanOrEqual(other: Long) = compare(other) >= 0
|
||||
|
||||
internal fun Long.compare(other: Long): Int {
|
||||
if (equalsLong(other)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
val thisNeg = isNegative();
|
||||
val otherNeg = other.isNegative();
|
||||
|
||||
return when {
|
||||
thisNeg && !otherNeg -> -1
|
||||
!thisNeg && otherNeg -> 1
|
||||
// at this point, the signs are the same, so subtraction will not overflow
|
||||
subtract(other).isNegative() -> -1
|
||||
else -> 1
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Long.add(other: Long): Long {
|
||||
// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
|
||||
|
||||
val a48 = high ushr 16
|
||||
val a32 = high and 0xFFFF
|
||||
val a16 = low ushr 16
|
||||
val a00 = low and 0xFFFF
|
||||
|
||||
val b48 = other.high ushr 16
|
||||
val b32 = other.high and 0xFFFF
|
||||
val b16 = other.low ushr 16
|
||||
val b00 = other.low and 0xFFFF
|
||||
|
||||
var c48 = 0
|
||||
var c32 = 0
|
||||
var c16 = 0
|
||||
var c00 = 0
|
||||
c00 += a00 + b00
|
||||
c16 += c00 ushr 16
|
||||
c00 = c00 and 0xFFFF
|
||||
c16 += a16 + b16
|
||||
c32 += c16 ushr 16
|
||||
c16 = c16 and 0xFFFF
|
||||
c32 += a32 + b32
|
||||
c48 += c32 ushr 16
|
||||
c32 = c32 and 0xFFFF
|
||||
c48 += a48 + b48
|
||||
c48 = c48 and 0xFFFF
|
||||
return Long((c16 shl 16) or c00, (c48 shl 16) or c32)
|
||||
}
|
||||
|
||||
internal fun Long.subtract(other: Long) = add(other.unaryMinus())
|
||||
|
||||
internal fun Long.multiply(other: Long): Long {
|
||||
if (isZero()) {
|
||||
return ZERO
|
||||
} else if (other.isZero()) {
|
||||
return ZERO
|
||||
}
|
||||
|
||||
if (equalsLong(MIN_VALUE)) {
|
||||
return if (other.isOdd()) MIN_VALUE else ZERO
|
||||
} else if (other.equalsLong(MIN_VALUE)) {
|
||||
return if (isOdd()) MIN_VALUE else ZERO
|
||||
}
|
||||
|
||||
if (isNegative()) {
|
||||
return if (other.isNegative()) {
|
||||
negate().multiply(other.negate())
|
||||
} else {
|
||||
negate().multiply(other).negate()
|
||||
}
|
||||
} else if (other.isNegative()) {
|
||||
return multiply(other.negate()).negate()
|
||||
}
|
||||
|
||||
// If both longs are small, use float multiplication
|
||||
if (lessThan(TWO_PWR_24_) && other.lessThan(TWO_PWR_24_)) {
|
||||
return fromNumber(toNumber() * other.toNumber())
|
||||
}
|
||||
|
||||
// Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
|
||||
// We can skip products that would overflow.
|
||||
|
||||
val a48 = high ushr 16
|
||||
val a32 = high and 0xFFFF
|
||||
val a16 = low ushr 16
|
||||
val a00 = low and 0xFFFF
|
||||
|
||||
val b48 = other.high ushr 16
|
||||
val b32 = other.high and 0xFFFF
|
||||
val b16 = other.low ushr 16
|
||||
val b00 = other.low and 0xFFFF
|
||||
|
||||
var c48 = 0
|
||||
var c32 = 0
|
||||
var c16 = 0
|
||||
var c00 = 0
|
||||
c00 += a00 * b00
|
||||
c16 += c00 ushr 16
|
||||
c00 = c00 and 0xFFFF
|
||||
c16 += a16 * b00
|
||||
c32 += c16 ushr 16
|
||||
c16 = c16 and 0xFFFF
|
||||
c16 += a00 * b16
|
||||
c32 += c16 ushr 16
|
||||
c16 = c16 and 0xFFFF
|
||||
c32 += a32 * b00
|
||||
c48 += c32 ushr 16
|
||||
c32 = c32 and 0xFFFF
|
||||
c32 += a16 * b16
|
||||
c48 += c32 ushr 16
|
||||
c32 = c32 and 0xFFFF
|
||||
c32 += a00 * b32
|
||||
c48 += c32 ushr 16
|
||||
c32 = c32 and 0xFFFF
|
||||
c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48
|
||||
c48 = c48 and 0xFFFF
|
||||
return Long(c16 shl 16 or c00, c48 shl 16 or c32)
|
||||
}
|
||||
|
||||
internal fun Long.divide(other: Long): Long {
|
||||
if (other.isZero()) {
|
||||
throw Exception("division by zero")
|
||||
} else if (isZero()) {
|
||||
return ZERO
|
||||
}
|
||||
|
||||
if (equalsLong(MIN_VALUE)) {
|
||||
if (other.equalsLong(ONE) || other.equalsLong(NEG_ONE)) {
|
||||
return MIN_VALUE // recall that -MIN_VALUE == MIN_VALUE
|
||||
} else if (other.equalsLong(MIN_VALUE)) {
|
||||
return ONE
|
||||
} else {
|
||||
// At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
|
||||
val halfThis = shiftRight(1)
|
||||
val approx = halfThis.div(other).shiftLeft(1)
|
||||
if (approx.equalsLong(ZERO)) {
|
||||
return if (other.isNegative()) ONE else NEG_ONE
|
||||
} else {
|
||||
val rem = subtract(other.multiply(approx))
|
||||
return approx.add(rem.div(other))
|
||||
}
|
||||
}
|
||||
} else if (other.equalsLong(MIN_VALUE)) {
|
||||
return ZERO
|
||||
}
|
||||
|
||||
if (isNegative()) {
|
||||
return if (other.isNegative()) {
|
||||
negate().div(other.negate())
|
||||
} else {
|
||||
negate().div(other).negate()
|
||||
}
|
||||
} else if (other.isNegative()) {
|
||||
return div(other.negate()).negate()
|
||||
}
|
||||
|
||||
// Repeat the following until the remainder is less than other: find a
|
||||
// floating-point that approximates remainder / other *from below*, add this
|
||||
// into the result, and subtract it from the remainder. It is critical that
|
||||
// the approximate value is less than or equal to the real value so that the
|
||||
// remainder never becomes negative.
|
||||
var res = ZERO
|
||||
var rem = this
|
||||
while (rem.greaterThanOrEqual(other)) {
|
||||
// Approximate the result of division. This may be a little greater or
|
||||
// smaller than the actual value.
|
||||
val approxDouble = rem.toNumber() / other.toNumber()
|
||||
var approx2 = JsMath.max(1.0, JsMath.floor(approxDouble))
|
||||
|
||||
// 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 = JsMath.ceil(JsMath.log(approx2) / JsMath.LN2)
|
||||
val delta = if (log2 <= 48) 1.0 else JsMath.pow(2.0, log2 - 48)
|
||||
|
||||
// Decrease the approximation until it is smaller than the remainder. Note
|
||||
// that if it is too large, the product overflows and is negative.
|
||||
var approxRes = fromNumber(approx2)
|
||||
var approxRem = approxRes.multiply(other)
|
||||
while (approxRem.isNegative() || approxRem.greaterThan(rem)) {
|
||||
approx2 -= delta
|
||||
approxRes = fromNumber(approx2)
|
||||
approxRem = approxRes.multiply(other)
|
||||
}
|
||||
|
||||
// We know the answer can't be zero... and actually, zero would cause
|
||||
// infinite recursion since we would make no progress.
|
||||
if (approxRes.isZero()) {
|
||||
approxRes = ONE
|
||||
}
|
||||
|
||||
res = res.add(approxRes)
|
||||
rem = rem.subtract(approxRem)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
internal fun Long.modulo(other: Long) = subtract(div(other).multiply(other))
|
||||
|
||||
internal fun Long.shiftLeft(numBits: Int): Long {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val numBits = numBits and 63
|
||||
if (numBits == 0) {
|
||||
return this
|
||||
} else {
|
||||
if (numBits < 32) {
|
||||
return Long(low shl numBits, (high shl numBits) or (low ushr (32 - numBits)))
|
||||
} else {
|
||||
return Long(0, low shl (numBits - 32))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Long.shiftRight(numBits: Int): Long {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val numBits = numBits and 63
|
||||
if (numBits == 0) {
|
||||
return this
|
||||
} else {
|
||||
if (numBits < 32) {
|
||||
return Long((low ushr numBits) or (high shl (32 - numBits)), high shr numBits)
|
||||
} else {
|
||||
return Long(high shr (numBits - 32), if (high >= 0) 0 else -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Long.shiftRightUnsigned(numBits: Int): Long {
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val numBits = numBits and 63
|
||||
if (numBits == 0) {
|
||||
return this
|
||||
} else {
|
||||
if (numBits < 32) {
|
||||
return Long((low ushr numBits) or (high shl (32 - numBits)), high ushr numBits)
|
||||
} else return if (numBits == 32) {
|
||||
Long(high, 0)
|
||||
} else {
|
||||
Long(high ushr (numBits - 32), 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Long representing the given (32-bit) integer value.
|
||||
* @param {number} value The 32-bit integer in question.
|
||||
* @return {!Kotlin.Long} The corresponding Long value.
|
||||
*/
|
||||
// TODO: cache
|
||||
internal fun fromInt(value: Int) = Long(value, if (value < 0) -1 else 0)
|
||||
|
||||
/**
|
||||
* Converts this [Double] value to [Long].
|
||||
* The fractional part, if any, is rounded down towards zero.
|
||||
* Returns zero if this `Double` value is `NaN`, [Long.MIN_VALUE] if it's less than `Long.MIN_VALUE`,
|
||||
* [Long.MAX_VALUE] if it's bigger than `Long.MAX_VALUE`.
|
||||
*/
|
||||
internal fun fromNumber(value: Double): Long {
|
||||
if (value.isNaN()) {
|
||||
return ZERO;
|
||||
} else if (value <= -TWO_PWR_63_DBL_) {
|
||||
return MIN_VALUE;
|
||||
} else if (value + 1 >= TWO_PWR_63_DBL_) {
|
||||
return MAX_VALUE;
|
||||
} else if (value < 0) {
|
||||
return fromNumber(-value).negate();
|
||||
} else {
|
||||
val twoPwr32 = TWO_PWR_32_DBL_
|
||||
return Long(
|
||||
jsBitwiseOr(value.rem(twoPwr32), 0),
|
||||
jsBitwiseOr(value / twoPwr32, 0)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private const val TWO_PWR_16_DBL_ = (1 shl 16).toDouble()
|
||||
|
||||
private const val TWO_PWR_24_DBL_ = (1 shl 24).toDouble()
|
||||
|
||||
//private val TWO_PWR_32_DBL_ = TWO_PWR_16_DBL_ * TWO_PWR_16_DBL_
|
||||
private const val TWO_PWR_32_DBL_ = (1 shl 16).toDouble() * (1 shl 16).toDouble()
|
||||
|
||||
//private val TWO_PWR_64_DBL_ = TWO_PWR_32_DBL_ * TWO_PWR_32_DBL_
|
||||
private const val TWO_PWR_64_DBL_ = ((1 shl 16).toDouble() * (1 shl 16).toDouble()) * ((1 shl 16).toDouble() * (1 shl 16).toDouble())
|
||||
|
||||
//private val TWO_PWR_63_DBL_ = TWO_PWR_64_DBL_ / 2
|
||||
private const val TWO_PWR_63_DBL_ = (((1 shl 16).toDouble() * (1 shl 16).toDouble()) * ((1 shl 16).toDouble() * (1 shl 16).toDouble())) / 2
|
||||
|
||||
private val ZERO = fromInt(0)
|
||||
|
||||
private val ONE = fromInt(1)
|
||||
|
||||
private val NEG_ONE = fromInt(-1)
|
||||
|
||||
private val MAX_VALUE = Long(-1, -1 ushr 1)
|
||||
|
||||
private val MIN_VALUE = Long(0, 1 shl 31)
|
||||
|
||||
private val TWO_PWR_24_ = fromInt(1 shl 24)
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* 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:JsQualifier("Math")
|
||||
package kotlin.js
|
||||
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.imul === "undefined") {
|
||||
Math.imul = function imul(a, b) {
|
||||
return ((a & 0xffff0000) * (b & 0xffff) + (a & 0xffff) * (b | 0)) | 0;
|
||||
}
|
||||
}
|
||||
""")
|
||||
internal external fun imul(a_local: Int, b_local: Int): Int
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal fun setMetadataFor(
|
||||
ctor: Ctor,
|
||||
name: String?,
|
||||
metadataConstructor: (name: String?, defaultConstructor: dynamic, associatedObjectKey: Number?, associatedObjects: dynamic, suspendArity: Array<Int>?) -> Metadata,
|
||||
parent: Ctor?,
|
||||
interfaces: Array<dynamic>?,
|
||||
defaultConstructor: dynamic,
|
||||
associatedObjectKey: Number?,
|
||||
associatedObjects: dynamic,
|
||||
suspendArity: Array<Int>?
|
||||
) {
|
||||
if (parent != null) {
|
||||
js("""
|
||||
ctor.prototype = Object.create(parent.prototype)
|
||||
ctor.prototype.constructor = ctor;
|
||||
""")
|
||||
}
|
||||
|
||||
val metadata = metadataConstructor(name, defaultConstructor, associatedObjectKey, associatedObjects, suspendArity ?: js("[]"))
|
||||
ctor.`$metadata$` = metadata
|
||||
|
||||
if (interfaces != null) {
|
||||
val receiver = if (metadata.iid != null) ctor else ctor.prototype
|
||||
receiver.`$imask$` = implement(interfaces)
|
||||
}
|
||||
}
|
||||
|
||||
// There was a problem with per-module compilation (KT-55758) when the top-level state (iid) was reinitialized during stdlib module initialization
|
||||
// As a result we miss already incremented iid and had the same iids in two different modules
|
||||
// So, to keep the state consistent it was moved into the variable without initializer and function
|
||||
@Suppress("MUST_BE_INITIALIZED")
|
||||
private var iid: dynamic
|
||||
|
||||
private fun generateInterfaceId(): Int {
|
||||
if (iid === VOID) {
|
||||
iid = 0
|
||||
}
|
||||
iid = iid.unsafeCast<Int>() + 1
|
||||
return iid.unsafeCast<Int>()
|
||||
}
|
||||
|
||||
|
||||
internal fun interfaceMeta(
|
||||
name: String?,
|
||||
defaultConstructor: dynamic,
|
||||
associatedObjectKey: Number?,
|
||||
associatedObjects: dynamic,
|
||||
suspendArity: Array<Int>?
|
||||
): Metadata {
|
||||
return createMetadata("interface", name, defaultConstructor, associatedObjectKey, associatedObjects, suspendArity, generateInterfaceId())
|
||||
}
|
||||
|
||||
internal fun objectMeta(
|
||||
name: String?,
|
||||
defaultConstructor: dynamic,
|
||||
associatedObjectKey: Number?,
|
||||
associatedObjects: dynamic,
|
||||
suspendArity: Array<Int>?
|
||||
): Metadata {
|
||||
return createMetadata("object", name, defaultConstructor, associatedObjectKey, associatedObjects, suspendArity, null)
|
||||
}
|
||||
|
||||
internal fun classMeta(
|
||||
name: String?,
|
||||
defaultConstructor: dynamic,
|
||||
associatedObjectKey: Number?,
|
||||
associatedObjects: dynamic,
|
||||
suspendArity: Array<Int>?
|
||||
): Metadata {
|
||||
return createMetadata("class", name, defaultConstructor, associatedObjectKey, associatedObjects, suspendArity, null)
|
||||
}
|
||||
|
||||
// Seems like we need to disable this check if variables are used inside js annotation
|
||||
@Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE")
|
||||
private fun createMetadata(
|
||||
kind: String,
|
||||
name: String?,
|
||||
defaultConstructor: dynamic,
|
||||
associatedObjectKey: Number?,
|
||||
associatedObjects: dynamic,
|
||||
suspendArity: Array<Int>?,
|
||||
iid: Int?
|
||||
): Metadata {
|
||||
val undef = VOID
|
||||
return js("""({
|
||||
kind: kind,
|
||||
simpleName: name,
|
||||
associatedObjectKey: associatedObjectKey,
|
||||
associatedObjects: associatedObjects,
|
||||
suspendArity: suspendArity,
|
||||
${'$'}kClass$: undef,
|
||||
defaultConstructor: defaultConstructor,
|
||||
iid: iid
|
||||
})""")
|
||||
}
|
||||
|
||||
internal external interface Metadata {
|
||||
val kind: String
|
||||
// This field gives fast access to the prototype of metadata owner (Object.getPrototypeOf())
|
||||
// Can be pre-initialized or lazy initialized and then should be immutable
|
||||
val simpleName: String?
|
||||
val associatedObjectKey: Number?
|
||||
val associatedObjects: dynamic
|
||||
val suspendArity: Array<Int>?
|
||||
val iid: Int?
|
||||
|
||||
var `$kClass$`: dynamic
|
||||
val defaultConstructor: dynamic
|
||||
|
||||
var errorInfo: Int? // Bits set for overridden properties: "message" => 0x1, "cause" => 0x2
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
/** Concat regular Array's and TypedArray's into an Array.
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun <T> arrayConcat(vararg args: T): T {
|
||||
val len = args.size
|
||||
val typed = js("Array(len)").unsafeCast<Array<T>>()
|
||||
for (i in 0 .. (len - 1)) {
|
||||
val arr = args[i]
|
||||
if (arr !is Array<*>) {
|
||||
typed[i] = js("[]").slice.call(arr)
|
||||
} else {
|
||||
typed[i] = arr
|
||||
}
|
||||
}
|
||||
return js("[]").concat.apply(js("[]"), typed);
|
||||
}
|
||||
|
||||
/** Concat primitive arrays. Main use: prepare vararg arguments.
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun <T> primitiveArrayConcat(vararg args: T): T {
|
||||
var size_local = 0
|
||||
for (i in 0 .. (args.size - 1)) {
|
||||
size_local += args[i].unsafeCast<Array<Any?>>().size
|
||||
}
|
||||
val a = args[0]
|
||||
val result = js("new a.constructor(size_local)").unsafeCast<Array<Any?>>()
|
||||
if (a.asDynamic().`$type$` != null) {
|
||||
withType(a.asDynamic().`$type$`, result)
|
||||
}
|
||||
|
||||
size_local = 0
|
||||
for (i in 0 .. (args.size - 1)) {
|
||||
val arr = args[i].unsafeCast<Array<Any?>>()
|
||||
for (j in 0 .. (arr.size - 1)) {
|
||||
result[size_local++] = arr[j]
|
||||
}
|
||||
}
|
||||
return result.unsafeCast<T>()
|
||||
}
|
||||
|
||||
internal fun <T> taggedArrayCopy(array: dynamic): T {
|
||||
val res = array.slice()
|
||||
res.`$type$` = array.`$type$`
|
||||
return res.unsafeCast<T>()
|
||||
}
|
||||
|
||||
@PublishedApi
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
internal inline fun withType(type: String, array: dynamic): dynamic {
|
||||
array.`$type$` = type
|
||||
return array
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal fun numberToByte(a: dynamic): Byte = toByte(numberToInt(a))
|
||||
|
||||
internal fun numberToDouble(@Suppress("UNUSED_PARAMETER") a: dynamic): Double = js("+a").unsafeCast<Double>()
|
||||
|
||||
internal fun numberToInt(a: dynamic): Int = if (a is Long) a.toInt() else doubleToInt(a)
|
||||
|
||||
internal fun numberToShort(a: dynamic): Short = toShort(numberToInt(a))
|
||||
|
||||
// << and >> shifts are used to preserve sign of the number
|
||||
internal fun toByte(@Suppress("UNUSED_PARAMETER") a: dynamic): Byte = js("a << 24 >> 24").unsafeCast<Byte>()
|
||||
internal fun toShort(@Suppress("UNUSED_PARAMETER") a: dynamic): Short = js("a << 16 >> 16").unsafeCast<Short>()
|
||||
|
||||
internal fun numberToLong(a: dynamic): Long = if (a is Long) a else fromNumber(a)
|
||||
|
||||
internal fun toLong(a: dynamic): Long = fromInt(a)
|
||||
|
||||
internal fun doubleToInt(a: Double): Int = when {
|
||||
a > 2147483647 -> 2147483647
|
||||
a < -2147483648 -> -2147483648
|
||||
else -> jsBitwiseOr(a, 0)
|
||||
}
|
||||
|
||||
internal fun numberToChar(a: dynamic) = Char(numberToInt(a).toUShort())
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
// Creates IntRange for {Byte, Short, Int}.rangeTo(x: {Byte, Short, Int})
|
||||
internal fun numberRangeToNumber(start: dynamic, endInclusive: dynamic) =
|
||||
IntRange(start, endInclusive)
|
||||
|
||||
// Create LongRange for {Byte, Short, Int}.rangeTo(x: Long)
|
||||
// Long.rangeTo(x: *) should be implemented in Long class
|
||||
internal fun numberRangeToLong(start: dynamic, endInclusive: dynamic) =
|
||||
LongRange(numberToLong(start), endInclusive)
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
internal fun getPropertyCallableRef(
|
||||
name: String,
|
||||
paramCount: Int,
|
||||
superType: dynamic,
|
||||
getter: dynamic,
|
||||
setter: dynamic
|
||||
): KProperty<*> {
|
||||
getter.get = getter
|
||||
getter.set = setter
|
||||
getter.callableName = name
|
||||
return getPropertyRefClass(
|
||||
getter,
|
||||
getKPropMetadata(paramCount, setter),
|
||||
getInterfaceMaskFor(getter, superType)
|
||||
).unsafeCast<KProperty<*>>()
|
||||
}
|
||||
|
||||
internal fun getLocalDelegateReference(name: String, superType: dynamic, mutable: Boolean, lambda: dynamic): KProperty<*> {
|
||||
return getPropertyCallableRef(name, 0, superType, lambda, if (mutable) lambda else null)
|
||||
}
|
||||
|
||||
private fun getPropertyRefClass(obj: Ctor, metadata: Metadata, imask: BitMask): dynamic {
|
||||
obj.`$metadata$` = metadata
|
||||
obj.constructor = obj
|
||||
obj.`$imask$` = imask
|
||||
return obj;
|
||||
}
|
||||
|
||||
private fun getInterfaceMaskFor(obj: Ctor, superType: dynamic): BitMask =
|
||||
obj.`$imask$` ?: implement(arrayOf(superType))
|
||||
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
private fun getKPropMetadata(paramCount: Int, setter: Any?): dynamic {
|
||||
return propertyRefClassMetadataCache[paramCount][if (setter == null) 0 else 1]
|
||||
}
|
||||
|
||||
private fun metadataObject(): Metadata {
|
||||
return classMeta(VOID, VOID, VOID, VOID, VOID)
|
||||
}
|
||||
|
||||
private val propertyRefClassMetadataCache: Array<Array<dynamic>> = arrayOf<Array<dynamic>>(
|
||||
// immutable , mutable
|
||||
arrayOf<dynamic>(metadataObject(), metadataObject()), // 0
|
||||
arrayOf<dynamic>(metadataObject(), metadataObject()), // 1
|
||||
arrayOf<dynamic>(metadataObject(), metadataObject()) // 2
|
||||
)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
// Inlined intrinsics for backward compatibility with already implemented functions in stdlib for old JS backend
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun jsDeleteProperty(obj: dynamic, property: Any) = jsDelete(obj[property])
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun jsBitwiseOr(lhs: Any?, rhs: Any?): Int = jsBitOr(lhs, rhs)
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun jsBitwiseAnd(lhs: Any?, rhs: Any?): Int = jsBitAnd(lhs, rhs)
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun jsInstanceOf(obj: Any?, jsClass: Any?): Boolean = jsInstanceOfIntrinsic(obj, jsClass)
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
internal inline fun jsIn(lhs: Any?, rhs: Any): Boolean = jsInIntrinsic(lhs, rhs)
|
||||
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal external interface Ctor {
|
||||
var `$imask$`: BitMask?
|
||||
var `$metadata$`: Metadata
|
||||
var constructor: Ctor?
|
||||
val prototype: dynamic
|
||||
}
|
||||
|
||||
private fun hasProp(proto: dynamic, propName: String): Boolean = proto.hasOwnProperty(propName)
|
||||
|
||||
internal fun calculateErrorInfo(proto: dynamic): Int {
|
||||
val metadata: Metadata? = proto.constructor?.`$metadata$`
|
||||
|
||||
metadata?.errorInfo?.let { return it } // cached
|
||||
|
||||
var result = 0
|
||||
if (hasProp(proto, "message")) result = result or 0x1
|
||||
if (hasProp(proto, "cause")) result = result or 0x2
|
||||
|
||||
if (result != 0x3) { //
|
||||
val parentProto = getPrototypeOf(proto)
|
||||
if (parentProto != js("Error").prototype) {
|
||||
result = result or calculateErrorInfo(parentProto)
|
||||
}
|
||||
}
|
||||
|
||||
if (metadata != null) {
|
||||
metadata.errorInfo = result
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun getPrototypeOf(obj: dynamic) = JsObject.getPrototypeOf(obj)
|
||||
|
||||
private fun isInterfaceImpl(obj: dynamic, iface: Int): Boolean {
|
||||
val mask: BitMask = obj.`$imask$`.unsafeCast<BitMask?>() ?: return false
|
||||
return mask.isBitSet(iface)
|
||||
}
|
||||
|
||||
internal fun isInterface(obj: dynamic, iface: dynamic): Boolean {
|
||||
return isInterfaceImpl(obj, iface.`$metadata$`.iid)
|
||||
}
|
||||
|
||||
internal fun isSuspendFunction(obj: dynamic, arity: Int): Boolean {
|
||||
val objTypeOf = jsTypeOf(obj)
|
||||
|
||||
if (objTypeOf == "function") {
|
||||
@Suppress("DEPRECATED_IDENTITY_EQUALS")
|
||||
return obj.`$arity`.unsafeCast<Int>() === arity
|
||||
}
|
||||
|
||||
val suspendArity = obj?.constructor.unsafeCast<Ctor?>()?.`$metadata$`?.suspendArity ?: return false
|
||||
|
||||
@Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS")
|
||||
var result = false
|
||||
for (item in suspendArity) {
|
||||
if (arity == item) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun isJsArray(obj: Any): Boolean {
|
||||
return js("Array").isArray(obj).unsafeCast<Boolean>()
|
||||
}
|
||||
|
||||
internal fun isArray(obj: Any): Boolean {
|
||||
return isJsArray(obj) && !(obj.asDynamic().`$type$`)
|
||||
}
|
||||
|
||||
// TODO: Remove after the next bootstrap
|
||||
internal fun isObject(o: dynamic): Boolean = o != null
|
||||
|
||||
internal fun isArrayish(o: dynamic) = isJsArray(o) || arrayBufferIsView(o)
|
||||
|
||||
internal fun isChar(@Suppress("UNUSED_PARAMETER") c: Any): Boolean {
|
||||
error("isChar is not implemented")
|
||||
}
|
||||
|
||||
// TODO: Distinguish Boolean/Byte and Short/Char
|
||||
internal fun isBooleanArray(a: dynamic): Boolean = isJsArray(a) && a.`$type$` === "BooleanArray"
|
||||
internal fun isByteArray(a: dynamic): Boolean = jsInstanceOf(a, js("Int8Array"))
|
||||
internal fun isShortArray(a: dynamic): Boolean = jsInstanceOf(a, js("Int16Array"))
|
||||
internal fun isCharArray(a: dynamic): Boolean = jsInstanceOf(a, js("Uint16Array")) && a.`$type$` === "CharArray"
|
||||
internal fun isIntArray(a: dynamic): Boolean = jsInstanceOf(a, js("Int32Array"))
|
||||
internal fun isFloatArray(a: dynamic): Boolean = jsInstanceOf(a, js("Float32Array"))
|
||||
internal fun isDoubleArray(a: dynamic): Boolean = jsInstanceOf(a, js("Float64Array"))
|
||||
internal fun isLongArray(a: dynamic): Boolean = isJsArray(a) && a.`$type$` === "LongArray"
|
||||
|
||||
internal fun jsGetPrototypeOf(jsClass: dynamic) = js("Object").getPrototypeOf(jsClass)
|
||||
|
||||
internal fun jsIsType(obj: dynamic, jsClass: dynamic): Boolean {
|
||||
if (jsClass === js("Object")) {
|
||||
return obj != null
|
||||
}
|
||||
|
||||
val objType = jsTypeOf(obj)
|
||||
val jsClassType = jsTypeOf(jsClass)
|
||||
|
||||
if (obj == null || jsClass == null || (objType != "object" && objType != "function")) {
|
||||
return false
|
||||
}
|
||||
|
||||
// In WebKit (JavaScriptCore) for some interfaces from DOM typeof returns "object", nevertheless they can be used in RHS of instanceof
|
||||
val constructor = if (jsClassType == "object") jsGetPrototypeOf(jsClass) else jsClass
|
||||
val klassMetadata = constructor.`$metadata$`
|
||||
|
||||
if (klassMetadata?.kind === "interface") {
|
||||
val iid = klassMetadata.iid.unsafeCast<Int?>() ?: return false
|
||||
return isInterfaceImpl(obj, iid)
|
||||
}
|
||||
|
||||
return jsInstanceOf(obj, constructor)
|
||||
}
|
||||
|
||||
internal fun isNumber(a: dynamic) = jsTypeOf(a) == "number" || a is Long
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
internal fun isComparable(value: dynamic): Boolean {
|
||||
val type = jsTypeOf(value)
|
||||
|
||||
return type == "string" ||
|
||||
type == "boolean" ||
|
||||
isNumber(value) ||
|
||||
isInterface(value, jsClassIntrinsic<Comparable<*>>())
|
||||
}
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
internal fun isCharSequence(value: dynamic): Boolean =
|
||||
jsTypeOf(value) == "string" || isInterface(value, jsClassIntrinsic<CharSequence>())
|
||||
|
||||
|
||||
@OptIn(JsIntrinsic::class)
|
||||
internal fun isExternalObject(value: dynamic, ktExternalObject: dynamic) =
|
||||
jsEqeqeq(value, ktExternalObject) || (jsTypeOf(ktExternalObject) == "function" && jsInstanceOf(value, ktExternalObject))
|
||||
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal class IrLinkageError(message: String?) : Error(message)
|
||||
|
||||
internal fun throwLinkageError(message: String?): Nothing {
|
||||
throw IrLinkageError(message)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
internal val VOID: Nothing? = js("void 0")
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.coroutines
|
||||
|
||||
// Do not remove this file.
|
||||
// During klib deserialization the compiler uses this file as a place for declaring function type interfaces.
|
||||
// See FunctionTypeInterfacePackages.kt
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
// Do not remove this file.
|
||||
// During klib deserialization the compiler uses this file as a place for declaring function type interfaces.
|
||||
// See FunctionTypeInterfacePackages.kt
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.reflect
|
||||
|
||||
// Do not remove this file.
|
||||
// During klib deserialization the compiler uses this file as a place for declaring function type interfaces.
|
||||
// See FunctionTypeInterfacePackages.kt
|
||||
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.coroutines
|
||||
|
||||
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
@JsName("CoroutineImpl")
|
||||
internal abstract class CoroutineImpl(private val resultContinuation: Continuation<Any?>?) : Continuation<Any?> {
|
||||
protected var state = 0
|
||||
protected var exceptionState = 0
|
||||
protected var result: dynamic = null
|
||||
protected var exception: dynamic = null
|
||||
protected var finallyPath: Array<Int>? = null
|
||||
|
||||
private val _context: CoroutineContext? = resultContinuation?.context
|
||||
|
||||
public override val context: CoroutineContext get() = _context!!
|
||||
|
||||
private var intercepted_: Continuation<Any?>? = null
|
||||
|
||||
public fun intercepted(): Continuation<Any?> =
|
||||
intercepted_
|
||||
?: (context[ContinuationInterceptor]?.interceptContinuation(this) ?: this)
|
||||
.also { intercepted_ = it }
|
||||
|
||||
override fun resumeWith(result: Result<Any?>) {
|
||||
var current = this
|
||||
var currentResult: Any? = result.getOrNull()
|
||||
var currentException: Throwable? = result.exceptionOrNull()
|
||||
|
||||
// This loop unrolls recursion in current.resumeWith(param) to make saner and shorter stack traces on resume
|
||||
while (true) {
|
||||
with(current) {
|
||||
// Set result and exception fields in the current continuation
|
||||
if (currentException == null) {
|
||||
this.result = currentResult
|
||||
} else {
|
||||
state = exceptionState
|
||||
exception = currentException
|
||||
}
|
||||
|
||||
try {
|
||||
val outcome = doResume()
|
||||
if (outcome === COROUTINE_SUSPENDED) return
|
||||
currentResult = outcome
|
||||
currentException = null
|
||||
} catch (exception: dynamic) { // Catch all exceptions
|
||||
currentResult = null
|
||||
currentException = exception.unsafeCast<Throwable>()
|
||||
}
|
||||
|
||||
releaseIntercepted() // this state machine instance is terminating
|
||||
|
||||
val completion = resultContinuation!!
|
||||
|
||||
if (completion is CoroutineImpl) {
|
||||
// unrolling recursion via loop
|
||||
current = completion
|
||||
} else {
|
||||
// top-level completion reached -- invoke and return
|
||||
if (currentException != null) {
|
||||
completion.resumeWithException(currentException!!)
|
||||
} else {
|
||||
completion.resume(currentResult)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun releaseIntercepted() {
|
||||
val intercepted = intercepted_
|
||||
if (intercepted != null && intercepted !== this) {
|
||||
context[ContinuationInterceptor]!!.releaseInterceptedContinuation(intercepted)
|
||||
}
|
||||
this.intercepted_ = CompletedContinuation // just in case
|
||||
}
|
||||
|
||||
protected abstract fun doResume(): Any?
|
||||
|
||||
public open fun create(completion: Continuation<*>): Continuation<Unit> {
|
||||
throw UnsupportedOperationException("create(Continuation) has not been overridden")
|
||||
}
|
||||
|
||||
public open fun create(value: Any?, completion: Continuation<*>): Continuation<Unit> {
|
||||
throw UnsupportedOperationException("create(Any?;Continuation) has not been overridden")
|
||||
}
|
||||
}
|
||||
|
||||
internal object CompletedContinuation : Continuation<Any?> {
|
||||
override val context: CoroutineContext
|
||||
get() = error("This continuation is already complete")
|
||||
|
||||
override fun resumeWith(result: Result<Any?>) {
|
||||
error("This continuation is already complete")
|
||||
}
|
||||
|
||||
override fun toString(): String = "This continuation is already complete"
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "UNCHECKED_CAST")
|
||||
|
||||
package kotlin.coroutines.intrinsics
|
||||
|
||||
import kotlin.coroutines.Continuation
|
||||
import kotlin.coroutines.ContinuationInterceptor
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.CoroutineImpl
|
||||
import kotlin.internal.InlineOnly
|
||||
|
||||
/**
|
||||
* Invoke 'invoke' method of suspend super type
|
||||
* Because callable references translated with local classes,
|
||||
* necessary to call it in special way, not in synamic way
|
||||
*/
|
||||
@Suppress("UNUSED_PARAMETER", "unused")
|
||||
@PublishedApi
|
||||
internal fun <T> (suspend () -> T).invokeSuspendSuperType(
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
throw NotImplementedError("It is intrinsic method")
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke 'invoke' method of suspend super type with receiver
|
||||
* Because callable references translated with local classes,
|
||||
* necessary to call it in special way, not in synamic way
|
||||
*/
|
||||
@Suppress("UNUSED_PARAMETER", "unused")
|
||||
@PublishedApi
|
||||
internal fun <R, T> (suspend R.() -> T).invokeSuspendSuperTypeWithReceiver(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
throw NotImplementedError("It is intrinsic method")
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke 'invoke' method of suspend super type with receiver and param
|
||||
* Because callable references translated with local classes,
|
||||
* necessary to call it in special way, not in synamic way
|
||||
*/
|
||||
@Suppress("UNUSED_PARAMETER", "unused")
|
||||
@PublishedApi
|
||||
internal fun <R, P, T> (suspend R.(P) -> T).invokeSuspendSuperTypeWithReceiverAndParam(
|
||||
receiver: R,
|
||||
param: P,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
throw NotImplementedError("It is intrinsic method")
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts unintercepted coroutine without receiver and with result type [T] and executes it until its first suspension.
|
||||
* Returns the result of the coroutine or throws its exception if it does not suspend or [COROUTINE_SUSPENDED] if it suspends.
|
||||
* In the latter case, the [completion] continuation is invoked when coroutine completes with result or exception.
|
||||
*
|
||||
* The coroutine is started directly in the invoker's thread without going through the [ContinuationInterceptor] that might
|
||||
* be present in the completion's [CoroutineContext]. It is the invoker's responsibility to ensure that a proper invocation
|
||||
* context is established.
|
||||
*
|
||||
* This function is designed to be used from inside of [suspendCoroutineUninterceptedOrReturn] to resume the execution of a suspended
|
||||
* coroutine using a reference to the suspending function.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@InlineOnly
|
||||
public actual inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn(
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
val a = this.asDynamic()
|
||||
return if (jsTypeOf(a) == "function") a(completion)
|
||||
else this.invokeSuspendSuperType(completion)
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts unintercepted coroutine with receiver type [R] and result type [T] and executes it until its first suspension.
|
||||
* Returns the result of the coroutine or throws its exception if it does not suspend or [COROUTINE_SUSPENDED] if it suspends.
|
||||
* In the latter case, the [completion] continuation is invoked when coroutine completes with result or exception.
|
||||
*
|
||||
* The coroutine is started directly in the invoker's thread without going through the [ContinuationInterceptor] that might
|
||||
* be present in the completion's [CoroutineContext]. It is the invoker's responsibility to ensure that a proper invocation
|
||||
* context is established.
|
||||
*
|
||||
* This function is designed to be used from inside of [suspendCoroutineUninterceptedOrReturn] to resume the execution of a suspended
|
||||
* coroutine using a reference to the suspending function.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@InlineOnly
|
||||
public actual inline fun <R, T> (suspend R.() -> T).startCoroutineUninterceptedOrReturn(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
val a = this.asDynamic()
|
||||
return if (jsTypeOf(a) == "function") a(receiver, completion)
|
||||
else this.invokeSuspendSuperTypeWithReceiver(receiver, completion)
|
||||
}
|
||||
|
||||
@InlineOnly
|
||||
internal actual inline fun <R, P, T> (suspend R.(P) -> T).startCoroutineUninterceptedOrReturn(
|
||||
receiver: R,
|
||||
param: P,
|
||||
completion: Continuation<T>
|
||||
): Any? {
|
||||
val a = this.asDynamic()
|
||||
return if (jsTypeOf(a) == "function") a(receiver, param, completion)
|
||||
else this.invokeSuspendSuperTypeWithReceiverAndParam(receiver, param, completion)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates unintercepted coroutine without receiver and with result type [T].
|
||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
||||
*
|
||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
||||
* The [completion] continuation is invoked when coroutine completes with result or exception.
|
||||
*
|
||||
* This function returns unintercepted continuation.
|
||||
* Invocation of `resume(Unit)` starts coroutine directly in the invoker's thread without going through the
|
||||
* [ContinuationInterceptor] that might be present in the completion's [CoroutineContext].
|
||||
* It is the invoker's responsibility to ensure that a proper invocation context is established.
|
||||
* [Continuation.intercepted] can be used to acquire the intercepted continuation.
|
||||
*
|
||||
* Repeated invocation of any resume function on the resulting continuation corrupts the
|
||||
* state machine of the coroutine and may result in arbitrary behaviour or exception.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual fun <T> (suspend () -> T).createCoroutineUnintercepted(
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit> =
|
||||
createCoroutineFromSuspendFunction(completion) {
|
||||
val a = this.asDynamic()
|
||||
if (jsTypeOf(a) == "function") a(completion)
|
||||
else this.invokeSuspendSuperType(completion)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates unintercepted coroutine with receiver type [R] and result type [T].
|
||||
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
|
||||
*
|
||||
* To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance.
|
||||
* The [completion] continuation is invoked when coroutine completes with result or exception.
|
||||
*
|
||||
* This function returns unintercepted continuation.
|
||||
* Invocation of `resume(Unit)` starts coroutine directly in the invoker's thread without going through the
|
||||
* [ContinuationInterceptor] that might be present in the completion's [CoroutineContext].
|
||||
* It is the invoker's responsibility to ensure that a proper invocation context is established.
|
||||
* [Continuation.intercepted] can be used to acquire the intercepted continuation.
|
||||
*
|
||||
* Repeated invocation of any resume function on the resulting continuation corrupts the
|
||||
* state machine of the coroutine and may result in arbitrary behaviour or exception.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual fun <R, T> (suspend R.() -> T).createCoroutineUnintercepted(
|
||||
receiver: R,
|
||||
completion: Continuation<T>
|
||||
): Continuation<Unit> =
|
||||
createCoroutineFromSuspendFunction(completion) {
|
||||
val a = this.asDynamic()
|
||||
if (jsTypeOf(a) == "function") a(receiver, completion)
|
||||
else this.invokeSuspendSuperTypeWithReceiver(receiver, completion)
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercepts this continuation with [ContinuationInterceptor].
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public actual fun <T> Continuation<T>.intercepted(): Continuation<T> =
|
||||
(this as? CoroutineImpl)?.intercepted() ?: this
|
||||
|
||||
|
||||
private inline fun <T> createCoroutineFromSuspendFunction(
|
||||
completion: Continuation<T>,
|
||||
crossinline block: () -> Any?
|
||||
): Continuation<Unit> {
|
||||
return object : CoroutineImpl(completion as Continuation<Any?>) {
|
||||
override fun doResume(): Any? {
|
||||
if (exception != null) throw exception
|
||||
return block()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
public actual open class Error : Throwable {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class Exception : Throwable {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class RuntimeException : Exception {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class IllegalArgumentException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class IllegalStateException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class IndexOutOfBoundsException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
}
|
||||
|
||||
public actual open class ConcurrentModificationException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class UnsupportedOperationException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
|
||||
public actual open class NumberFormatException : IllegalArgumentException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
}
|
||||
|
||||
|
||||
public actual open class NullPointerException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
}
|
||||
|
||||
public actual open class ClassCastException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
}
|
||||
|
||||
public actual open class AssertionError : Error {
|
||||
public actual constructor() : super()
|
||||
public constructor(message: String?) : super(message)
|
||||
public actual constructor(message: Any?) : super(message?.toString(), message as? Throwable)
|
||||
@SinceKotlin("1.4")
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
}
|
||||
|
||||
public actual open class NoSuchElementException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
}
|
||||
|
||||
@SinceKotlin("1.3")
|
||||
public actual open class ArithmeticException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
}
|
||||
|
||||
public actual open class NoWhenBranchMatchedException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
public actual open class UninitializedPropertyAccessException : RuntimeException {
|
||||
public actual constructor() : super()
|
||||
public actual constructor(message: String?) : super(message)
|
||||
public actual constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public actual constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.math
|
||||
|
||||
/**
|
||||
* Returns this value with the sign bit same as of the [sign] value.
|
||||
*
|
||||
* If [sign] is `NaN` the sign of the result is undefined.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public actual fun Double.withSign(sign: Double): Double {
|
||||
val thisSignBit = doubleSignBit(this)
|
||||
val newSignBit = doubleSignBit(sign)
|
||||
return if (thisSignBit == newSignBit) this else -this
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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
|
||||
|
||||
/**
|
||||
* Returns a bit representation of the specified floating-point value as [Long]
|
||||
* according to the IEEE 754 floating-point "double format" bit layout.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public actual fun Double.toBits(): Long =
|
||||
doubleToRawBits(if (this.isNaN()) Double.NaN else this)
|
||||
|
||||
/**
|
||||
* Returns a bit representation of the specified floating-point value as [Long]
|
||||
* according to the IEEE 754 floating-point "double format" bit layout,
|
||||
* preserving `NaN` values exact layout.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public actual fun Double.toRawBits(): Long =
|
||||
doubleToRawBits(this)
|
||||
|
||||
/**
|
||||
* Returns the [Double] value corresponding to a given bit representation.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@kotlin.internal.InlineOnly
|
||||
public actual inline fun Double.Companion.fromBits(bits: Long): Double =
|
||||
doubleFromBits(bits)
|
||||
|
||||
/**
|
||||
* Returns a bit representation of the specified floating-point value as [Int]
|
||||
* according to the IEEE 754 floating-point "single format" bit layout.
|
||||
*
|
||||
* Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent,
|
||||
* so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public actual fun Float.toBits(): Int =
|
||||
floatToRawBits(if (this.isNaN()) Float.NaN else this)
|
||||
|
||||
/**
|
||||
* Returns a bit representation of the specified floating-point value as [Int]
|
||||
* according to the IEEE 754 floating-point "single format" bit layout,
|
||||
* preserving `NaN` values exact layout.
|
||||
*
|
||||
* Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent,
|
||||
* so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public actual fun Float.toRawBits(): Int =
|
||||
floatToRawBits(this)
|
||||
|
||||
/**
|
||||
* Returns the [Float] value corresponding to a given bit representation.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@kotlin.internal.InlineOnly
|
||||
public actual inline fun Float.Companion.fromBits(bits: Int): Float =
|
||||
floatFromBits(bits)
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.reflect
|
||||
|
||||
/**
|
||||
* Creates a new instance of the class, calling a constructor which either has no parameters or all parameters of which have
|
||||
* a default value. If there are no or many such constructors, an exception is thrown.
|
||||
*/
|
||||
@OptIn(JsIntrinsic::class)
|
||||
@SinceKotlin("1.9")
|
||||
@ExperimentalJsReflectionCreateInstance
|
||||
public fun <T : Any> KClass<T>.createInstance(): T {
|
||||
val jsClass = js.asDynamic()
|
||||
|
||||
if (jsClass === js("Object")) return js("{}")
|
||||
|
||||
val noArgsConstructor = jsClass.`$metadata$`.unsafeCast<Metadata?>()?.defaultConstructor
|
||||
?: throw IllegalArgumentException("Class \"$simpleName\" should have a single no-arg constructor")
|
||||
|
||||
return if (jsIsEs6() && noArgsConstructor !== jsClass) {
|
||||
js("noArgsConstructor.call(jsClass)")
|
||||
} else {
|
||||
js("new noArgsConstructor()")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.js
|
||||
|
||||
// DON'T USE! Use `K::class.js` instead.
|
||||
// The declaration kept only for backward compatibility with older compilers
|
||||
// TODO remove, but when?
|
||||
internal external fun <T : Any> jsClass(): JsClass<T>
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.js.internal.*
|
||||
|
||||
@PublishedApi
|
||||
internal fun <T : Annotation> KClass<*>.findAssociatedObject(annotationClass: KClass<T>): Any? {
|
||||
return if (this is KClassImpl<*> && annotationClass is KClassImpl<T>) {
|
||||
val key = annotationClass.jClass.asDynamic().`$metadata$`?.associatedObjectKey?.unsafeCast<Int>() ?: return null
|
||||
val map = jClass.asDynamic().`$metadata$`?.associatedObjects ?: return null
|
||||
val factory = map[key] ?: return null
|
||||
return factory()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.text
|
||||
|
||||
/**
|
||||
* Returns a string representation of this [Long] value in the specified [radix].
|
||||
*
|
||||
* @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
public actual fun Long.toString(radix: Int): String =
|
||||
this.toStringImpl(checkRadix(radix))
|
||||
Reference in New Issue
Block a user