Deepcopy patch
some things can't be copied... what to do
This commit is contained in:
parent
c03328c80d
commit
0626143f42
1 changed files with 23 additions and 2 deletions
25
js/utils.js
25
js/utils.js
|
@ -750,13 +750,34 @@ function assert_error(func_binding, msg) {
|
|||
/**
|
||||
* Deep copy object/array of basic types.
|
||||
*/
|
||||
function deepcopy(obj) {
|
||||
function deepcopy(obj, refs=undefined) {
|
||||
if (refs === undefined) {
|
||||
refs = new Map();
|
||||
}
|
||||
if (typeof(obj) !== 'object' || obj === null) { // null or value type
|
||||
return obj;
|
||||
}
|
||||
let ret = Array.isArray(obj) ? [] : {};
|
||||
for (let key in obj) {
|
||||
ret[key] = deepcopy(obj[key]);
|
||||
let val;
|
||||
try {
|
||||
val = obj[key];
|
||||
} catch (exc) {
|
||||
console.trace();
|
||||
val = undefined;
|
||||
}
|
||||
if (typeof(obj) === 'object') {
|
||||
if (refs.has(val)) {
|
||||
ret[key] = refs.get(val);
|
||||
}
|
||||
else {
|
||||
refs.set(val, val);
|
||||
ret[key] = deepcopy(val, refs);
|
||||
}
|
||||
}
|
||||
else {
|
||||
ret[key] = val;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue