Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WeakMap and WeakSet errors #111

Open
terrymorse58 opened this issue Feb 6, 2020 · 1 comment
Open

WeakMap and WeakSet errors #111

terrymorse58 opened this issue Feb 6, 2020 · 1 comment

Comments

@terrymorse58
Copy link

The following clone test of an array containing a WeakMap produces an error:

// WeakMap test
const options = { includeNonEnumerable: true };

let wm = new WeakMap();
let obj = { foo: "I am foo" };
wm.set(obj, 42);
let src = [1, 2, wm, "bar"];

let dest = clone(src, options);
dest[1] = 3.1416;

console.log(src);
//  [ 1, 2, WeakMap { <items unknown> }, 'bar' ]
console.log(dest);
//  [ 1, 3.1416, WeakMap {}, 'bar' ]

let destMap = dest[2];
console.log(destMap instanceof WeakMap);
// true

destMap.has(obj);
//  TypeError: Method WeakMap.prototype.has called on incompatible receiver #<WeakMap>

An identical error occurs when running a similar test with WeakSet:

// TypeError: Method WeakSet.prototype.has called on incompatible receiver #<WeakSet>
@terrymorse58
Copy link
Author

Also, in the following test, clone failed to copy the ArrayCustom prototype.

class ArrayCustom extends Array {
  custom() { return true; }
}
let src = [1, 2, ArrayCustom.from(["I", "am", "foo"])];
let dest = clone(src);
dest[1] = 2.00001;

console.log(src);
// [ 1, 2, ArrayCustom [ 'I', 'am', 'foo' ] ]
console.log(dest);
// [ 1, 2.00001, [ 'I', 'am', 'foo' ] ]

let destCustom = dest[2];
if (!(destCustom instanceof ArrayCustom)) {
  throw "Error: failed to preserve ArrayCustom prototype";
}
// Error: failed to preserve ArrayCustom prototype

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant