From 82b94fad5db022933c301c18d4d46c37dda9442b Mon Sep 17 00:00:00 2001 From: n1474335 Date: Mon, 29 Jul 2019 15:14:29 +0100 Subject: [PATCH] Fixed BigNumber type coercion issues when passed between workers --- src/core/Dish.mjs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index fbd5418a..9c878cf6 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -235,6 +235,10 @@ class Dish { case Dish.JSON: title = "application/json"; break; + case Dish.NUMBER: + case Dish.BIG_NUMBER: + title = this.value.toString(); + break; case Dish.ARRAY_BUFFER: case Dish.BYTE_ARRAY: title = this.detectDishType(); @@ -283,7 +287,21 @@ class Dish { case Dish.ARRAY_BUFFER: return this.value instanceof ArrayBuffer; case Dish.BIG_NUMBER: - return BigNumber.isBigNumber(this.value); + if (BigNumber.isBigNumber(this.value)) return true; + /* + If a BigNumber is passed between WebWorkers it is serialised as a JSON + object with a coefficient (c), exponent (e) and sign (s). We detect this + and reinitialise it as a BigNumber object. + */ + if (Object.keys(this.value).sort().equals(["c", "e", "s"])) { + const temp = new BigNumber(); + temp.c = this.value.c; + temp.e = this.value.e; + temp.s = this.value.s; + this.value = temp; + return true; + } + return false; case Dish.JSON: // All values can be serialised in some manner, so we return true in all cases return true;