feat: add polyfills insertion for ES Next used features
This commit is contained in:
@@ -1464,7 +1464,7 @@ public actual fun CharArray.copyOfRange(fromIndex: Int, toIndex: Int): CharArray
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1480,7 +1480,7 @@ public actual fun <T> Array<T>.fill(element: T, fromIndex: Int = 0, toIndex: Int
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1496,7 +1496,7 @@ public actual fun ByteArray.fill(element: Byte, fromIndex: Int = 0, toIndex: Int
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1512,7 +1512,7 @@ public actual fun ShortArray.fill(element: Short, fromIndex: Int = 0, toIndex: I
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1528,7 +1528,7 @@ public actual fun IntArray.fill(element: Int, fromIndex: Int = 0, toIndex: Int =
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1544,7 +1544,7 @@ public actual fun LongArray.fill(element: Long, fromIndex: Int = 0, toIndex: Int
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1560,7 +1560,7 @@ public actual fun FloatArray.fill(element: Float, fromIndex: Int = 0, toIndex: I
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1576,7 +1576,7 @@ public actual fun DoubleArray.fill(element: Double, fromIndex: Int = 0, toIndex:
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1592,7 +1592,7 @@ public actual fun BooleanArray.fill(element: Boolean, fromIndex: Int = 0, toInde
|
||||
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
|
||||
public actual fun CharArray.fill(element: Char, fromIndex: Int = 0, toIndex: Int = size): Unit {
|
||||
AbstractList.checkRangeIndexes(fromIndex, toIndex, size)
|
||||
this.asDynamic().fill(element, fromIndex, toIndex);
|
||||
nativeFill(element, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2062,7 +2062,7 @@ public actual fun CharArray.sort(fromIndex: Int = 0, toIndex: Int = size): Unit
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2072,7 +2072,7 @@ public inline fun ByteArray.sort(noinline comparison: (a: Byte, b: Byte) -> Int)
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2082,7 +2082,7 @@ public inline fun ShortArray.sort(noinline comparison: (a: Short, b: Short) -> I
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2092,7 +2092,7 @@ public inline fun IntArray.sort(noinline comparison: (a: Int, b: Int) -> Int): U
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2102,7 +2102,7 @@ public inline fun LongArray.sort(noinline comparison: (a: Long, b: Long) -> Int)
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2112,7 +2112,7 @@ public inline fun FloatArray.sort(noinline comparison: (a: Float, b: Float) -> I
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2122,7 +2122,7 @@ public inline fun DoubleArray.sort(noinline comparison: (a: Double, b: Double) -
|
||||
@DeprecatedSinceKotlin(warningSince = "1.6")
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.sort(noinline comparison: (a: Char, b: Char) -> Int): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
nativeSort(comparison)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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:JsQualifier("ArrayBuffer")
|
||||
package kotlin.js
|
||||
|
||||
@JsName("isView")
|
||||
@JsPolyfill("""
|
||||
if (typeof ArrayBuffer.isView === "undefined") {
|
||||
ArrayBuffer.isView = function(a) {
|
||||
return a != null && a.__proto__ != null && a.__proto__.__proto__ === Int8Array.prototype.__proto__;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun arrayBufferIsView(value: Any?): Boolean
|
||||
@@ -7,6 +7,7 @@ package kotlin.collections
|
||||
|
||||
import kotlin.comparisons.naturalOrder
|
||||
import kotlin.random.Random
|
||||
import kotlin.js.arrayBufferIsView
|
||||
|
||||
/**
|
||||
* Returns the array if it's not `null`, or an empty array otherwise.
|
||||
@@ -193,7 +194,7 @@ internal fun <T> arrayCopy(source: Array<out T>, destination: Array<in T>, desti
|
||||
val rangeSize = endIndex - startIndex
|
||||
AbstractList.checkRangeIndexes(destinationOffset, destinationOffset + rangeSize, destination.size)
|
||||
|
||||
if (js("ArrayBuffer").isView(destination) && js("ArrayBuffer").isView(source)) {
|
||||
if (arrayBufferIsView(destination) && arrayBufferIsView(source)) {
|
||||
val subrange = source.asDynamic().subarray(startIndex, endIndex)
|
||||
destination.asDynamic().set(subrange, destinationOffset)
|
||||
} else {
|
||||
|
||||
@@ -7,4 +7,4 @@ package kotlin.js
|
||||
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
|
||||
internal annotation class JsNativeImplementation(val implementation: String)
|
||||
internal annotation class JsPolyfill(val implementation: String)
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
@PublishedApi
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@JsPolyfill("""
|
||||
if (typeof Array.prototype.fill === "undefined") {
|
||||
// Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#Polyfill
|
||||
Object.defineProperty(Array.prototype, 'fill', {
|
||||
value: function (value) {
|
||||
// Steps 1-2.
|
||||
if (this == null) {
|
||||
throw new TypeError('this is null or not defined');
|
||||
}
|
||||
|
||||
var O = Object(this);
|
||||
|
||||
// Steps 3-5.
|
||||
var len = O.length >>> 0;
|
||||
|
||||
// Steps 6-7.
|
||||
var start = arguments[1];
|
||||
var relativeStart = start >> 0;
|
||||
|
||||
// Step 8.
|
||||
var k = relativeStart < 0 ?
|
||||
Math.max(len + relativeStart, 0) :
|
||||
Math.min(relativeStart, len);
|
||||
|
||||
// Steps 9-10.
|
||||
var end = arguments[2];
|
||||
var relativeEnd = end === undefined ?
|
||||
len : end >> 0;
|
||||
|
||||
// Step 11.
|
||||
var finalValue = relativeEnd < 0 ?
|
||||
Math.max(len + relativeEnd, 0) :
|
||||
Math.min(relativeEnd, len);
|
||||
|
||||
// Step 12.
|
||||
while (k < finalValue) {
|
||||
O[k] = value;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Step 13.
|
||||
return O;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
[Int8Array, Int16Array, Uint16Array, Int32Array, Float32Array, Float64Array].forEach(function (TypedArray) {
|
||||
if (typeof TypedArray.prototype.fill === "undefined") {
|
||||
Object.defineProperty(TypedArray.prototype, 'fill', {
|
||||
value: Array.prototype.fill
|
||||
});
|
||||
}
|
||||
})
|
||||
""")
|
||||
internal inline fun Any.nativeFill(element: Any?, fromIndex: Int, toIndex: Int): Unit {
|
||||
asDynamic().fill(element, fromIndex, toIndex)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
@PublishedApi
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@JsPolyfill("""
|
||||
[Int8Array, Int16Array, Uint16Array, Int32Array, Float32Array, Float64Array].forEach(function (TypedArray) {
|
||||
if (typeof TypedArray.prototype.sort === "undefined") {
|
||||
Object.defineProperty(TypedArray.prototype, 'sort', {
|
||||
value: function(compareFunction) {
|
||||
compareFunction = compareFunction || function (a, b) {
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
if (a === b) {
|
||||
if (a !== 0) return 0;
|
||||
var ia = 1 / a;
|
||||
return ia === 1 / b ? 0 : (ia < 0 ? -1 : 1);
|
||||
}
|
||||
return a !== a ? (b !== b ? 0 : 1) : -1
|
||||
}
|
||||
return Array.prototype.sort.call(this, compareFunction || totalOrderComparator);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
""")
|
||||
internal inline fun Any.nativeSort(noinline comparison: (a: dynamic, b: dynamic) -> Int = js("undefined")): Unit {
|
||||
asDynamic().sort(comparison)
|
||||
}
|
||||
+16
-20
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@@ -33,24 +33,20 @@ internal external object JsMath {
|
||||
fun round(value: Number): Double
|
||||
fun floor(value: Number): Double
|
||||
fun ceil(value: Number): Double
|
||||
|
||||
fun trunc(value: Number): Double
|
||||
fun sign(value: Number): Double
|
||||
|
||||
fun sinh(value: Double): Double
|
||||
fun cosh(value: Double): Double
|
||||
fun tanh(value: Double): Double
|
||||
fun asinh(value: Double): Double
|
||||
fun acosh(value: Double): Double
|
||||
fun atanh(value: Double): Double
|
||||
|
||||
fun hypot(x: Double, y: Double): Double
|
||||
|
||||
fun expm1(value: Double): Double
|
||||
fun log10(value: Double): Double
|
||||
fun log2(value: Double): Double
|
||||
fun log1p(value: Double): Double
|
||||
|
||||
fun clz32(value: Int): Int
|
||||
}
|
||||
|
||||
internal const val defineTaylorNBound = """
|
||||
var epsilon = 2.220446049250313E-16;
|
||||
var taylor_2_bound = Math.sqrt(epsilon);
|
||||
var taylor_n_bound = Math.sqrt(taylor_2_bound);
|
||||
"""
|
||||
|
||||
internal const val defineUpperTaylor2Bound = """
|
||||
$defineTaylorNBound
|
||||
var upper_taylor_2_bound = 1/taylor_2_bound;
|
||||
"""
|
||||
|
||||
internal const val defineUpperTaylorNBound = """
|
||||
$defineUpperTaylor2Bound
|
||||
var upper_taylor_n_bound = 1/taylor_n_bound;
|
||||
"""
|
||||
@@ -0,0 +1,308 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
@file:JsQualifier("Math")
|
||||
package kotlin.js
|
||||
// ES6 Math polyfills
|
||||
// Inverse hyperbolic function implementations derived from boost special math functions,
|
||||
// Copyright Eric Ford & Hubert Holin 2001.
|
||||
|
||||
@PublishedApi
|
||||
@JsName("sign")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.sign === "undefined") {
|
||||
Math.sign = function(x) {
|
||||
x = +x; // convert to a number
|
||||
if (x === 0 || isNaN(x)) {
|
||||
return Number(x);
|
||||
}
|
||||
return x > 0 ? 1 : -1;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeSign(value: Number): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("trunc")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.trunc === "undefined") {
|
||||
Math.trunc = function(x) {
|
||||
if (isNaN(x)) {
|
||||
return NaN;
|
||||
}
|
||||
if (x > 0) {
|
||||
return Math.floor(x);
|
||||
}
|
||||
return Math.ceil(x);
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeTrunc(value: Number): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("sinh")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.sinh === "undefined") {
|
||||
$defineTaylorNBound
|
||||
Math.sinh = function(x) {
|
||||
if (Math.abs(x) < taylor_n_bound) {
|
||||
var result = x;
|
||||
if (Math.abs(x) > taylor_2_bound) {
|
||||
result += (x * x * x) / 6;
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
var y = Math.exp(x);
|
||||
var y1 = 1 / y;
|
||||
if (!isFinite(y)) return Math.exp(x - Math.LN2);
|
||||
if (!isFinite(y1)) return -Math.exp(-x - Math.LN2);
|
||||
return (y - y1) / 2;
|
||||
}
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeSinh(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("cosh")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.cosh === "undefined") {
|
||||
Math.cosh = function(x) {
|
||||
var y = Math.exp(x);
|
||||
var y1 = 1 / y;
|
||||
if (!isFinite(y) || !isFinite(y1)) return Math.exp(Math.abs(x) - Math.LN2);
|
||||
return (y + y1) / 2;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeCosh(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("tanh")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.tanh === "undefined") {
|
||||
$defineTaylorNBound
|
||||
Math.tanh = function(x){
|
||||
if (Math.abs(x) < taylor_n_bound) {
|
||||
var result = x;
|
||||
if (Math.abs(x) > taylor_2_bound) {
|
||||
result -= (x * x * x) / 3;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
var a = Math.exp(+x), b = Math.exp(-x);
|
||||
return a === Infinity ? 1 : b === Infinity ? -1 : (a - b) / (a + b);
|
||||
}
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeTanh(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("asinh")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.asinh === "undefined") {
|
||||
$defineUpperTaylorNBound
|
||||
var asinh = function(x) {
|
||||
if (x >= +taylor_n_bound)
|
||||
{
|
||||
if (x > upper_taylor_n_bound)
|
||||
{
|
||||
if (x > upper_taylor_2_bound)
|
||||
{
|
||||
// approximation by laurent series in 1/x at 0+ order from -1 to 0
|
||||
return Math.log(x) + Math.LN2;
|
||||
}
|
||||
else
|
||||
{
|
||||
// approximation by laurent series in 1/x at 0+ order from -1 to 1
|
||||
return Math.log(x * 2 + (1 / (x * 2)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Math.log(x + Math.sqrt(x * x + 1));
|
||||
}
|
||||
}
|
||||
else if (x <= -taylor_n_bound)
|
||||
{
|
||||
return -asinh(-x);
|
||||
}
|
||||
else
|
||||
{
|
||||
// approximation by taylor series in x at 0 up to order 2
|
||||
var result = x;
|
||||
if (Math.abs(x) >= taylor_2_bound)
|
||||
{
|
||||
var x3 = x * x * x;
|
||||
// approximation by taylor series in x at 0 up to order 4
|
||||
result -= x3 / 6;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
Math.asinh = asinh;
|
||||
}
|
||||
""")
|
||||
internal external fun nativeAsinh(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("acosh")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.acosh === "undefined") {
|
||||
$defineUpperTaylor2Bound
|
||||
Math.acosh = function(x) {
|
||||
if (x < 1)
|
||||
{
|
||||
return NaN;
|
||||
}
|
||||
else if (x - 1 >= taylor_n_bound)
|
||||
{
|
||||
if (x > upper_taylor_2_bound)
|
||||
{
|
||||
// approximation by laurent series in 1/x at 0+ order from -1 to 0
|
||||
return Math.log(x) + Math.LN2;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Math.log(x + Math.sqrt(x * x - 1));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var y = Math.sqrt(x - 1);
|
||||
// approximation by taylor series in y at 0 up to order 2
|
||||
var result = y;
|
||||
if (y >= taylor_2_bound)
|
||||
{
|
||||
var y3 = y * y * y;
|
||||
// approximation by taylor series in y at 0 up to order 4
|
||||
result -= y3 / 12;
|
||||
}
|
||||
|
||||
return Math.sqrt(2) * result;
|
||||
}
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeAcosh(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("atanh")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.atanh === "undefined") {
|
||||
$defineTaylorNBound
|
||||
Math.atanh = function(x) {
|
||||
if (Math.abs(x) < taylor_n_bound) {
|
||||
var result = x;
|
||||
if (Math.abs(x) > taylor_2_bound) {
|
||||
result += (x * x * x) / 3;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return Math.log((1 + x) / (1 - x)) / 2;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeAtanh(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("log1p")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.log1p === "undefined") {
|
||||
$defineTaylorNBound
|
||||
Math.log1p = function(x) {
|
||||
if (Math.abs(x) < taylor_n_bound) {
|
||||
var x2 = x * x;
|
||||
var x3 = x2 * x;
|
||||
var x4 = x3 * x;
|
||||
// approximation by taylor series in x at 0 up to order 4
|
||||
return (-x4 / 4 + x3 / 3 - x2 / 2 + x);
|
||||
}
|
||||
return Math.log(x + 1);
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeLog1p(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("expm1")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.expm1 === "undefined") {
|
||||
$defineTaylorNBound
|
||||
Math.expm1 = function(x) {
|
||||
if (Math.abs(x) < taylor_n_bound) {
|
||||
var x2 = x * x;
|
||||
var x3 = x2 * x;
|
||||
var x4 = x3 * x;
|
||||
// approximation by taylor series in x at 0 up to order 4
|
||||
return (x4 / 24 + x3 / 6 + x2 / 2 + x);
|
||||
}
|
||||
return Math.exp(x) - 1;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeExpm1(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("hypot")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.hypot === "undefined") {
|
||||
Math.hypot = function() {
|
||||
var y = 0;
|
||||
var length = arguments.length;
|
||||
|
||||
for (var i = 0; i < length; i++) {
|
||||
if (arguments[i] === Infinity || arguments[i] === -Infinity) {
|
||||
return Infinity;
|
||||
}
|
||||
y += arguments[i] * arguments[i];
|
||||
}
|
||||
return Math.sqrt(y);
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeHypot(x: Double, y: Double): Double
|
||||
|
||||
|
||||
@PublishedApi
|
||||
@JsName("log10")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.log10 === "undefined") {
|
||||
Math.log10 = function(x) {
|
||||
return Math.log(x) * Math.LOG10E;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeLog10(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("log2")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.log2 === "undefined") {
|
||||
Math.log2 = function(x) {
|
||||
return Math.log(x) * Math.LOG2E;
|
||||
};
|
||||
}
|
||||
""")
|
||||
internal external fun nativeLog2(value: Double): Double
|
||||
|
||||
@PublishedApi
|
||||
@JsName("clz32")
|
||||
@JsPolyfill("""
|
||||
if (typeof Math.clz32 === "undefined") {
|
||||
Math.clz32 = (function(log, LN2) {
|
||||
return function(x) {
|
||||
var asUint = x >>> 0;
|
||||
if (asUint === 0) {
|
||||
return 32;
|
||||
}
|
||||
return 31 - (log(asUint) / LN2 | 0) | 0; // the "| 0" acts like math.floor
|
||||
};
|
||||
})(Math.log, Math.LN2);
|
||||
}
|
||||
""")
|
||||
internal external fun nativeClz32(value: Int): Int
|
||||
@@ -101,7 +101,7 @@ public actual inline fun atan2(y: Double, x: Double): Double = nativeMath.atan2(
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun sinh(x: Double): Double = nativeMath.sinh(x)
|
||||
public actual inline fun sinh(x: Double): Double = nativeSinh(x)
|
||||
|
||||
/**
|
||||
* Computes the hyperbolic cosine of the value [x].
|
||||
@@ -112,7 +112,7 @@ public actual inline fun sinh(x: Double): Double = nativeMath.sinh(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun cosh(x: Double): Double = nativeMath.cosh(x)
|
||||
public actual inline fun cosh(x: Double): Double = nativeCosh(x)
|
||||
|
||||
/**
|
||||
* Computes the hyperbolic tangent of the value [x].
|
||||
@@ -124,7 +124,7 @@ public actual inline fun cosh(x: Double): Double = nativeMath.cosh(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun tanh(x: Double): Double = nativeMath.tanh(x)
|
||||
public actual inline fun tanh(x: Double): Double = nativeTanh(x)
|
||||
|
||||
/**
|
||||
* Computes the inverse hyperbolic sine of the value [x].
|
||||
@@ -138,7 +138,7 @@ public actual inline fun tanh(x: Double): Double = nativeMath.tanh(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun asinh(x: Double): Double = nativeMath.asinh(x)
|
||||
public actual inline fun asinh(x: Double): Double = nativeAsinh(x)
|
||||
|
||||
/**
|
||||
* Computes the inverse hyperbolic cosine of the value [x].
|
||||
@@ -152,7 +152,7 @@ public actual inline fun asinh(x: Double): Double = nativeMath.asinh(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun acosh(x: Double): Double = nativeMath.acosh(x)
|
||||
public actual inline fun acosh(x: Double): Double = nativeAcosh(x)
|
||||
|
||||
/**
|
||||
* Computes the inverse hyperbolic tangent of the value [x].
|
||||
@@ -167,7 +167,7 @@ public actual inline fun acosh(x: Double): Double = nativeMath.acosh(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun atanh(x: Double): Double = nativeMath.atanh(x)
|
||||
public actual inline fun atanh(x: Double): Double = nativeAtanh(x)
|
||||
|
||||
/**
|
||||
* Computes `sqrt(x^2 + y^2)` without intermediate overflow or underflow.
|
||||
@@ -178,7 +178,7 @@ public actual inline fun atanh(x: Double): Double = nativeMath.atanh(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun hypot(x: Double, y: Double): Double = nativeMath.hypot(x, y)
|
||||
public actual inline fun hypot(x: Double, y: Double): Double = nativeHypot(x, y)
|
||||
|
||||
/**
|
||||
* Computes the positive square root of the value [x].
|
||||
@@ -216,7 +216,7 @@ public actual inline fun exp(x: Double): Double = nativeMath.exp(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun expm1(x: Double): Double = nativeMath.expm1(x)
|
||||
public actual inline fun expm1(x: Double): Double = nativeExpm1(x)
|
||||
|
||||
/**
|
||||
* Computes the logarithm of the value [x] to the given [base].
|
||||
@@ -256,7 +256,7 @@ public actual inline fun ln(x: Double): Double = nativeMath.log(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun log10(x: Double): Double = nativeMath.log10(x)
|
||||
public actual inline fun log10(x: Double): Double = nativeLog10(x)
|
||||
|
||||
/**
|
||||
* Computes the binary logarithm (base 2) of the value [x].
|
||||
@@ -265,7 +265,7 @@ public actual inline fun log10(x: Double): Double = nativeMath.log10(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun log2(x: Double): Double = nativeMath.log2(x)
|
||||
public actual inline fun log2(x: Double): Double = nativeLog2(x)
|
||||
|
||||
/**
|
||||
* Computes `ln(x + 1)`.
|
||||
@@ -283,7 +283,7 @@ public actual inline fun log2(x: Double): Double = nativeMath.log2(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun ln1p(x: Double): Double = nativeMath.log1p(x)
|
||||
public actual inline fun ln1p(x: Double): Double = nativeLog1p(x)
|
||||
|
||||
/**
|
||||
* Rounds the given value [x] to an integer towards positive infinity.
|
||||
@@ -319,7 +319,7 @@ public actual inline fun floor(x: Double): Double = nativeMath.floor(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun truncate(x: Double): Double = nativeMath.trunc(x)
|
||||
public actual inline fun truncate(x: Double): Double = nativeTrunc(x)
|
||||
|
||||
/**
|
||||
* Rounds the given value [x] towards the closest integer with ties rounded towards even integer.
|
||||
@@ -359,7 +359,7 @@ public actual inline fun abs(x: Double): Double = nativeMath.abs(x)
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun sign(x: Double): Double = nativeMath.sign(x)
|
||||
public actual inline fun sign(x: Double): Double = nativeSign(x)
|
||||
|
||||
|
||||
/**
|
||||
@@ -429,7 +429,7 @@ public actual inline val Double.absoluteValue: Double get() = nativeMath.abs(thi
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline val Double.sign: Double get() = nativeMath.sign(this)
|
||||
public actual inline val Double.sign: Double get() = nativeSign(this)
|
||||
|
||||
/**
|
||||
* Returns this value with the sign bit same as of the [sign] value.
|
||||
@@ -626,7 +626,7 @@ public actual inline fun atan2(y: Float, x: Float): Float = nativeMath.atan2(y.t
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun sinh(x: Float): Float = nativeMath.sinh(x.toDouble()).toFloat()
|
||||
public actual inline fun sinh(x: Float): Float = nativeSinh(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the hyperbolic cosine of the value [x].
|
||||
@@ -637,7 +637,7 @@ public actual inline fun sinh(x: Float): Float = nativeMath.sinh(x.toDouble()).t
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun cosh(x: Float): Float = nativeMath.cosh(x.toDouble()).toFloat()
|
||||
public actual inline fun cosh(x: Float): Float = nativeCosh(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the hyperbolic tangent of the value [x].
|
||||
@@ -649,7 +649,7 @@ public actual inline fun cosh(x: Float): Float = nativeMath.cosh(x.toDouble()).t
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun tanh(x: Float): Float = nativeMath.tanh(x.toDouble()).toFloat()
|
||||
public actual inline fun tanh(x: Float): Float = nativeTanh(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the inverse hyperbolic sine of the value [x].
|
||||
@@ -663,7 +663,7 @@ public actual inline fun tanh(x: Float): Float = nativeMath.tanh(x.toDouble()).t
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun asinh(x: Float): Float = nativeMath.asinh(x.toDouble()).toFloat()
|
||||
public actual inline fun asinh(x: Float): Float = nativeAsinh(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the inverse hyperbolic cosine of the value [x].
|
||||
@@ -677,7 +677,7 @@ public actual inline fun asinh(x: Float): Float = nativeMath.asinh(x.toDouble())
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun acosh(x: Float): Float = nativeMath.acosh(x.toDouble()).toFloat()
|
||||
public actual inline fun acosh(x: Float): Float = nativeAcosh(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the inverse hyperbolic tangent of the value [x].
|
||||
@@ -692,7 +692,7 @@ public actual inline fun acosh(x: Float): Float = nativeMath.acosh(x.toDouble())
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun atanh(x: Float): Float = nativeMath.atanh(x.toDouble()).toFloat()
|
||||
public actual inline fun atanh(x: Float): Float = nativeAtanh(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes `sqrt(x^2 + y^2)` without intermediate overflow or underflow.
|
||||
@@ -703,7 +703,7 @@ public actual inline fun atanh(x: Float): Float = nativeMath.atanh(x.toDouble())
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun hypot(x: Float, y: Float): Float = nativeMath.hypot(x.toDouble(), y.toDouble()).toFloat()
|
||||
public actual inline fun hypot(x: Float, y: Float): Float = nativeHypot(x.toDouble(), y.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the positive square root of the value [x].
|
||||
@@ -741,7 +741,7 @@ public actual inline fun exp(x: Float): Float = nativeMath.exp(x.toDouble()).toF
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun expm1(x: Float): Float = nativeMath.expm1(x.toDouble()).toFloat()
|
||||
public actual inline fun expm1(x: Float): Float = nativeExpm1(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the logarithm of the value [x] to the given [base].
|
||||
@@ -779,7 +779,7 @@ public actual inline fun ln(x: Float): Float = nativeMath.log(x.toDouble()).toFl
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun log10(x: Float): Float = nativeMath.log10(x.toDouble()).toFloat()
|
||||
public actual inline fun log10(x: Float): Float = nativeLog10(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes the binary logarithm (base 2) of the value [x].
|
||||
@@ -788,7 +788,7 @@ public actual inline fun log10(x: Float): Float = nativeMath.log10(x.toDouble())
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun log2(x: Float): Float = nativeMath.log2(x.toDouble()).toFloat()
|
||||
public actual inline fun log2(x: Float): Float = nativeLog2(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Computes `ln(a + 1)`.
|
||||
@@ -806,7 +806,7 @@ public actual inline fun log2(x: Float): Float = nativeMath.log2(x.toDouble()).t
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun ln1p(x: Float): Float = nativeMath.log1p(x.toDouble()).toFloat()
|
||||
public actual inline fun ln1p(x: Float): Float = nativeLog1p(x.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Rounds the given value [x] to an integer towards positive infinity.
|
||||
@@ -878,7 +878,7 @@ public actual inline fun abs(x: Float): Float = nativeMath.abs(x.toDouble()).toF
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline fun sign(x: Float): Float = nativeMath.sign(x.toDouble()).toFloat()
|
||||
public actual inline fun sign(x: Float): Float = nativeSign(x.toDouble()).toFloat()
|
||||
|
||||
|
||||
|
||||
@@ -950,7 +950,7 @@ public actual inline val Float.absoluteValue: Float get() = nativeMath.abs(this.
|
||||
*/
|
||||
@SinceKotlin("1.2")
|
||||
@InlineOnly
|
||||
public actual inline val Float.sign: Float get() = nativeMath.sign(this.toDouble()).toFloat()
|
||||
public actual inline val Float.sign: Float get() = nativeSign(this.toDouble()).toFloat()
|
||||
|
||||
/**
|
||||
* Returns this value with the sign bit same as of the [sign] value.
|
||||
|
||||
@@ -60,7 +60,7 @@ public actual fun Int.countOneBits(): Int {
|
||||
@SinceKotlin("1.4")
|
||||
@WasExperimental(ExperimentalStdlibApi::class)
|
||||
@kotlin.internal.InlineOnly
|
||||
public actual inline fun Int.countLeadingZeroBits(): Int = JsMath.clz32(this)
|
||||
public actual inline fun Int.countLeadingZeroBits(): Int = nativeClz32(this)
|
||||
|
||||
/**
|
||||
* Counts the number of consecutive least significant bits that are zero in the binary representation of this [Int] number.
|
||||
|
||||
@@ -217,9 +217,34 @@ internal actual inline fun String.nativeIndexOf(str: String, fromIndex: Int): In
|
||||
internal actual inline fun String.nativeLastIndexOf(str: String, fromIndex: Int): Int = asDynamic().lastIndexOf(str, fromIndex)
|
||||
|
||||
@kotlin.internal.InlineOnly
|
||||
@kotlin.js.JsPolyfill("""
|
||||
if (typeof String.prototype.startsWith === "undefined") {
|
||||
Object.defineProperty(String.prototype, "startsWith", {
|
||||
value: function (searchString, position) {
|
||||
position = position || 0;
|
||||
return this.lastIndexOf(searchString, position) === position;
|
||||
}
|
||||
});
|
||||
}
|
||||
""")
|
||||
internal inline fun String.nativeStartsWith(s: String, position: Int): Boolean = asDynamic().startsWith(s, position)
|
||||
|
||||
@kotlin.internal.InlineOnly
|
||||
@kotlin.js.JsPolyfill("""
|
||||
if (typeof String.prototype.endsWith === "undefined") {
|
||||
Object.defineProperty(String.prototype, "endsWith", {
|
||||
value: function (searchString, position) {
|
||||
var subjectString = this.toString();
|
||||
if (position === undefined || position > subjectString.length) {
|
||||
position = subjectString.length;
|
||||
}
|
||||
position -= searchString.length;
|
||||
var lastIndex = subjectString.indexOf(searchString, position);
|
||||
return lastIndex !== -1 && lastIndex === position;
|
||||
}
|
||||
});
|
||||
}
|
||||
""")
|
||||
internal inline fun String.nativeEndsWith(s: String): Boolean = asDynamic().endsWith(s)
|
||||
|
||||
@kotlin.internal.InlineOnly
|
||||
|
||||
Reference in New Issue
Block a user