Pad: Fix copyPadWithoutHistory apool corruption bug

This commit is contained in:
Richard Hansen 2021-11-22 15:16:00 -05:00
parent ed78b56079
commit dab881139d
4 changed files with 96 additions and 5 deletions

View file

@ -91,6 +91,17 @@ class AttributePool {
this.nextNum = 0;
}
/**
* @returns {AttributePool} A deep copy of this attribute pool.
*/
clone() {
const c = new AttributePool();
for (const [n, a] of Object.entries(this.numToAttrib)) c.numToAttrib[n] = [a[0], a[1]];
Object.assign(c.attribToNum, this.attribToNum);
c.nextNum = this.nextNum;
return c;
}
/**
* Add an attribute to the attribute set, or query for an existing attribute identifier.
*
@ -164,7 +175,10 @@ class AttributePool {
/**
* @returns {Jsonable} An object that can be passed to `fromJsonable` to reconstruct this
* attribute pool. The returned object can be converted to JSON.
* attribute pool. The returned object can be converted to JSON. WARNING: The returned object
* has references to internal state (it is not a deep copy). Use the `clone()` method to copy
* a pool -- do NOT do `new AttributePool().fromJsonable(pool.toJsonable())` to copy because
* the resulting shared state will lead to pool corruption.
*/
toJsonable() {
return {
@ -177,7 +191,10 @@ class AttributePool {
* Replace the contents of this attribute pool with values from a previous call to `toJsonable`.
*
* @param {Jsonable} obj - Object returned by `toJsonable` containing the attributes and their
* identifiers.
* identifiers. WARNING: This function takes ownership of the object (it does not make a deep
* copy). Use the `clone()` method to copy a pool -- do NOT do
* `new AttributePool().fromJsonable(pool.toJsonable())` to copy because the resulting shared
* state will lead to pool corruption.
*/
fromJsonable(obj) {
this.numToAttrib = obj.numToAttrib;