Deepcopy patch

some things can't be copied... what to do
This commit is contained in:
hppeng 2022-07-14 16:04:01 -07:00
parent c03328c80d
commit 0626143f42

View file

@ -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;
}