From e8b181b9357159a4ac4806af6fd2bfc3b40779e0 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Mon, 14 Oct 2019 16:43:44 +0100 Subject: [PATCH 1/2] add clone.globalopts containing _clone. If set, this is called with each item. If it returns undefined, then the normal _clone continues. If it returns something, this is used as the item being cloned. --- clone.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/clone.js b/clone.js index 572ad87..4c60239 100644 --- a/clone.js +++ b/clone.js @@ -28,6 +28,10 @@ try { nativePromise = function() {}; } +var globalopts = { + _clone: null +}; + /** * Clones (copies) an Object using deep copying. * @@ -74,6 +78,13 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) { // cloning null always returns null if (parent === null) return null; + + if (globalopts._clone){ + var result = globalopts._clone(parent, depth); + if (result !== undefined) { + return result; + } + } if (depth === 0) return parent; @@ -160,9 +171,9 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) { var objProperty = Object.getOwnPropertyDescriptor(parent, i); if (objProperty.set === 'undefined') { // no setter defined. Skip cloning this property - continue; - } - child[i] = _clone(parent[i], depth - 1); + continue; + } + child[i] = _clone(parent[i], depth - 1); } catch(e){ if (e instanceof TypeError) { // when in strict mode, TypeError will be thrown if child[i] property only has a getter @@ -206,10 +217,12 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) { return child; } - + return _clone(parent, depth); } +clone.globalopts = globalopts; + /** * Simple flat clone using prototype, accepts only objects, usefull for property * override on FLAT configuration object (no nested props). From 440e6e2de96d19f01a867ac0ad9758a9d78729fd Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Mon, 14 Oct 2019 17:50:39 +0100 Subject: [PATCH 2/2] Add protection against V8 crash by detecting [object Pipe]. --- clone.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/clone.js b/clone.js index 4c60239..b0ec358 100644 --- a/clone.js +++ b/clone.js @@ -29,9 +29,14 @@ try { } var globalopts = { - _clone: null + _clone: null, + + crash_types: { + '[object Pipe]': true, + } }; + /** * Clones (copies) an Object using deep copying. * @@ -88,6 +93,11 @@ function clone(parent, circular, depth, prototype, includeNonEnumerable) { if (depth === 0) return parent; + + if (clone.__isCrash(parent)) { + // console.log("avoid crash on "+__objToStr(parent)); + return parent; + } var child; var proto; @@ -261,6 +271,12 @@ function __isRegExp(o) { } clone.__isRegExp = __isRegExp; +function __isCrash(o) { + return typeof o === 'object' && globalopts.crash_types[__objToStr(o)]; +} +clone.__isCrash = __isCrash; + + function __getRegExpFlags(re) { var flags = ''; if (re.global) flags += 'g';