32 Companion zmachine.js (11/28)
removeObj(o) {
const p = this.parent(o);
if (!p) return;
const sib = this.sibling(o);
if (this.child(p) === o) this.setChild(p, sib);
else {
let c = this.child(p);
while (c && this.sibling(c) !== o) c = this.sibling(c);
if (c) this.setSibling(c, sib);
}
this.setParent(o, 0);
this.setSibling(o, 0);
}
insertObj(o, d) {
this.removeObj(o);
this.setSibling(o, this.child(d));
this.setChild(d, o);
this.setParent(o, d);
}
// Property entries: returns [{num, addr (data), len}] in table order.
propAt(a) {
const b = this.mem[a];
if (!b) return null;
if (this.version <= 3) return { num: b & 31, len: (b >> 5) + 1, addr: a + 1 };
if (b & 0x80) {
const len = this.mem[a + 1] & 0x3f;
return { num: b & 0x3f, len: len || 64, addr: a + 2 };
}
return { num: b & 0x3f, len: b & 0x40 ? 2 : 1, addr: a + 1 };
}
firstProp(o) { const t = this.propTable(o); return t + 1 + 2 * this.mem[t]; }
findProp(o, n) {
for (let a = this.firstProp(o), p; (p = this.propAt(a)); a = p.addr + p.len) {
if (p.num === n) return p;
if (p.num < n) return null; // properties are stored in descending order
}
return null;
}
propLen(addr) {
if (addr === 0) return 0;
const b = this.mem[addr - 1];
if (this.version <= 3) return (b >> 5) + 1;
if (b & 0x80) return (b & 0x3f) || 64;
return b & 0x40 ? 2 : 1;
}