From e59fbc109738b982d9d5a8440bc9c228851f9a2e Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 20 Jul 2017 08:35:39 +0300 Subject: [PATCH] Provide polyfills for missing ES6 math functions #KT-4900 Rename math.kt to js.math.kt --- .../src/core/{math.kt => js.math.kt} | 27 ++++++- js/js.libraries/src/js/polyfills.js | 73 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) rename js/js.libraries/src/core/{math.kt => js.math.kt} (68%) diff --git a/js/js.libraries/src/core/math.kt b/js/js.libraries/src/core/js.math.kt similarity index 68% rename from js/js.libraries/src/core/math.kt rename to js/js.libraries/src/core/js.math.kt index 0f5092d65c6..dd48abfa358 100644 --- a/js/js.libraries/src/core/math.kt +++ b/js/js.libraries/src/core/js.math.kt @@ -1,6 +1,6 @@ package kotlin.js -//TODO: declare using number +//TODO: deprecate /** * Exposes the JavaScript [Math object](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math) to Kotlin. */ @@ -28,6 +28,31 @@ public external object Math { public fun round(value: Number): Int public fun floor(value: Number): Int public fun ceil(value: Number): Int + + @PublishedApi + internal fun trunc(value: Number): Double + @PublishedApi + internal fun sign(value: Number): Double + + @PublishedApi + internal fun sinh(value: Double): Double + @PublishedApi + internal fun cosh(value: Double): Double + @PublishedApi + internal fun tanh(value: Double): Double + + @PublishedApi + internal fun hypot(x: Double, y: Double): Double + + @PublishedApi + internal fun expm1(value: Double): Double + + @PublishedApi + internal fun log10(value: Double): Double + @PublishedApi + internal fun log2(value: Double): Double + @PublishedApi + internal fun log1p(value: Double): Double } /** diff --git a/js/js.libraries/src/js/polyfills.js b/js/js.libraries/src/js/polyfills.js index 1b4a2490490..42025aea0f1 100644 --- a/js/js.libraries/src/js/polyfills.js +++ b/js/js.libraries/src/js/polyfills.js @@ -31,6 +31,79 @@ if (typeof String.prototype.endsWith === "undefined") { return lastIndex !== -1 && lastIndex === position; }; } +// ES6 Math polyfills +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; + }; +} +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); + }; +} +if (typeof Math.sinh === "undefined") { + Math.sinh = function(x) { + var y = Math.exp(x); + return (y - 1 / y) / 2; + }; +} +if (typeof Math.cosh === "undefined") { + Math.cosh = function(x) { + var y = Math.exp(x); + return (y + 1 / y) / 2; + }; +} +if (typeof Math.tanh === "undefined") { + Math.tanh = function(x){ + var a = Math.exp(+x), b = Math.exp(-x); + return a == Infinity ? 1 : b == Infinity ? -1 : (a - b) / (a + b); + }; +} +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); + }; +} +if (typeof Math.log10 === "undefined") { + Math.log10 = function(x) { + return Math.log(x) * Math.LOG10E; + }; +} +if (typeof Math.log2 === "undefined") { + Math.log2 = function(x) { + return Math.log(x) * Math.LOG2E; + }; +} +if (typeof Math.log1p === "undefined") { + Math.log1p = function(x) { + return Math.log(x + 1); + }; +} +if (typeof Math.expm1 === "undefined") { + Math.expm1 = function(x) { + return Math.exp(x) - 1; + }; +} // For HtmlUnit and PhantomJs if (typeof ArrayBuffer.isView === "undefined") { ArrayBuffer.isView = function(a) {