From 1ed5b289ca4437e27d7aaad23813cb7cdc661568 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Wed, 6 Mar 2019 14:56:34 +0200 Subject: [PATCH 1/5] add sincos polyfill --- std/assembly/math.ts | 25 ++++++++++++++----------- std/portable/index.d.ts | 5 +++++ std/portable/index.js | 9 +++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/std/assembly/math.ts b/std/assembly/math.ts index b6a88f6f33..f899f1b082 100644 --- a/std/assembly/math.ts +++ b/std/assembly/math.ts @@ -2329,18 +2329,21 @@ export namespace NativeMathf { } return tan_kernf(x, 0); } - if (ix <= 0x407b53d1) { /* |x| ~<= 5π/4 */ - if (ix <= 0x4016cbe3) { /* |x| ~<= 3π/4 */ - return tan_kernf((sign ? x + t1pio2 : x - t1pio2), 1); - } else { - return tan_kernf((sign ? x + t2pio2 : x - t2pio2), 0); + + if (ASC_SHRINK_LEVEL < 1) { + if (ix <= 0x407b53d1) { /* |x| ~<= 5π/4 */ + if (ix <= 0x4016cbe3) { /* |x| ~<= 3π/4 */ + return tan_kernf((sign ? x + t1pio2 : x - t1pio2), 1); + } else { + return tan_kernf((sign ? x + t2pio2 : x - t2pio2), 0); + } } - } - if (ix <= 0x40e231d5) { /* |x| ~<= 9π/4 */ - if (ix <= 0x40afeddf) { /* |x| ~<= 7π/4 */ - return tan_kernf((sign ? x + t3pio2 : x - t3pio2), 1); - } else { - return tan_kernf((sign ? x + t4pio2 : x - t4pio2), 0); + if (ix <= 0x40e231d5) { /* |x| ~<= 9π/4 */ + if (ix <= 0x40afeddf) { /* |x| ~<= 7π/4 */ + return tan_kernf((sign ? x + t3pio2 : x - t3pio2), 1); + } else { + return tan_kernf((sign ? x + t4pio2 : x - t4pio2), 0); + } } } diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index 26c7d0a1fd..184fbcb4ca 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -567,6 +567,10 @@ interface IMath { readonly PI: f64; readonly SQRT1_2: f64; readonly SQRT2: f64; + + sincos_sin: f64; + sincos_cos: f64; + abs(x: f64): f64; acos(x: f64): f64; acosh(x: f64): f64; @@ -598,6 +602,7 @@ interface IMath { sign(x: f64): f64; signbit(x: f64): bool; sin(x: f64): f64; + sincos(x: f64): f64; sinh(x: f64): f64; sqrt(x: f64): f64; tan(x: f64): f64; diff --git a/std/portable/index.js b/std/portable/index.js index 7e7e2725b0..305e30f67e 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -244,10 +244,19 @@ globalScope["fmodf"] = function fmodf(x, y) { }; globalScope["JSMath"] = Math; + +globalScope["JSMath"].sincos_sin = 0.0; +globalScope["JSMath"].sincos_cos = 0.0; + globalScope["JSMath"].signbit = function signbit(x) { F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x)); } +globalScope["JSMath"].sincos = function sincos(x) { + globalScope["JSMath"].sincos_sin = Math.sin(x); + globalScope["JSMath"].sincos_cos = Math.cos(x); +} + globalScope["memory"] = (() => { var HEAP = new Uint8Array(0); var HEAP_OFFSET = 0; From 561582299a4567217750a2699eb57c4209902e7b Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Wed, 6 Mar 2019 19:54:30 +0200 Subject: [PATCH 2/5] use defineProperties for JSMath --- std/portable/index.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index 305e30f67e..95f7477867 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -245,17 +245,21 @@ globalScope["fmodf"] = function fmodf(x, y) { globalScope["JSMath"] = Math; -globalScope["JSMath"].sincos_sin = 0.0; -globalScope["JSMath"].sincos_cos = 0.0; - -globalScope["JSMath"].signbit = function signbit(x) { - F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x)); -} - -globalScope["JSMath"].sincos = function sincos(x) { - globalScope["JSMath"].sincos_sin = Math.sin(x); - globalScope["JSMath"].sincos_cos = Math.cos(x); -} +Object.defineProperties(globalScope["JSMath"], { + sincos_sin: { value: 0.0, writable: true }, + sincos_cos: { value: 0.0, writable: true }, + signbit: { + value: function signbit(x) { + F64[0] = x; return Boolean((U64[1] >>> 31) & (x == x)); + } + }, + sincos: { + value: function sincos(x) { + this.sincos_sin = Math.sin(x); + this.sincos_cos = Math.cos(x); + } + } +}); globalScope["memory"] = (() => { var HEAP = new Uint8Array(0); From ec9c19ceba4905d5cf4bc1ffff6caee068cd8f1e Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Wed, 6 Mar 2019 20:37:47 +0200 Subject: [PATCH 3/5] add isNullable and isFunction portables --- std/portable/index.d.ts | 4 ++++ std/portable/index.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index 184fbcb4ca..086dee2235 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -100,8 +100,12 @@ declare function isFinite(value: T): bool; declare function isInteger(value: any): value is number; /** Tests if the specified value is a valid float. Can't distinguish a float from an integer. */ declare function isFloat(value: any): value is number; +/** Tests if the specified value is a nullable. */ +declare function isNullable(value: any): bool; /** Tests if the specified value is of a reference type. */ declare function isReference(value: any): value is object | string; +/** Tests if the specified value is a function */ +declare function isFunction(value: any): value is Function; /** Tests if the specified value can be used as a string. */ declare function isString(value: any): value is string | String; /** Tests if the specified value can be used as an array. */ diff --git a/std/portable/index.js b/std/portable/index.js index 95f7477867..d3bbef6309 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -206,10 +206,18 @@ globalScope["isFloat"] = function isFloat(arg) { return typeof arg === "number"; }; +globalScope["isNullable"] = function isNullable(arg) { + return arg == null; +} + globalScope["isReference"] = function isReference(arg) { return typeof arg === "object" || typeof arg === "string"; }; +globalScope["isFunction"] = function isFunction(arg) { + return typeof arg === "function" || arg instanceof Function; +} + globalScope["isString"] = function isString(arg) { return typeof arg === "string" || arg instanceof String; }; From 3d51de21ea61481a69de33dfad9aef3e47f931e7 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Wed, 6 Mar 2019 20:43:29 +0200 Subject: [PATCH 4/5] fix comments --- std/portable/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/portable/index.d.ts b/std/portable/index.d.ts index 086dee2235..1fbc7e41e0 100644 --- a/std/portable/index.d.ts +++ b/std/portable/index.d.ts @@ -100,17 +100,17 @@ declare function isFinite(value: T): bool; declare function isInteger(value: any): value is number; /** Tests if the specified value is a valid float. Can't distinguish a float from an integer. */ declare function isFloat(value: any): value is number; -/** Tests if the specified value is a nullable. */ +/** Tests if the specified value is of a nullable reference type. */ declare function isNullable(value: any): bool; /** Tests if the specified value is of a reference type. */ declare function isReference(value: any): value is object | string; -/** Tests if the specified value is a function */ +/** Tests if the specified value is of a function type */ declare function isFunction(value: any): value is Function; /** Tests if the specified value can be used as a string. */ declare function isString(value: any): value is string | String; /** Tests if the specified value can be used as an array. */ declare function isArray(value: any): value is Array; -/** Tests if the specified type *or* expression can be used as an array like object. Compiles to a constant. */ +/** Tests if the specified type *or* expression can be used as an array like object. */ declare function isArrayLike(value: any): value is ArrayLike; /** Tests if the specified expression resolves to a defined element. */ declare function isDefined(expression: any): bool; From 396637543860c3245b08a34b1f5a221e60692f52 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Wed, 6 Mar 2019 21:07:41 +0200 Subject: [PATCH 5/5] update --- std/portable/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/std/portable/index.js b/std/portable/index.js index d3bbef6309..00aa35e1af 100644 --- a/std/portable/index.js +++ b/std/portable/index.js @@ -207,7 +207,7 @@ globalScope["isFloat"] = function isFloat(arg) { }; globalScope["isNullable"] = function isNullable(arg) { - return arg == null; + return true; } globalScope["isReference"] = function isReference(arg) { @@ -215,7 +215,7 @@ globalScope["isReference"] = function isReference(arg) { }; globalScope["isFunction"] = function isFunction(arg) { - return typeof arg === "function" || arg instanceof Function; + return typeof arg === "function"; } globalScope["isString"] = function isString(arg) {