| 696 | |
| 697 | // make color constants immutable with debug assertions |
| 698 | function debugProtectConstant(obj) |
| 699 | { |
| 700 | if (debug) |
| 701 | { |
| 702 | // get properties and store original values |
| 703 | const props = Object.keys(obj), values = {}; |
| 704 | props.forEach(prop => values[prop] = obj[prop]); |
| 705 | |
| 706 | // replace with getters/setters that assert |
| 707 | props.forEach(prop => |
| 708 | { |
| 709 | Object.defineProperty(obj, prop, { |
| 710 | get: ()=> values[prop], |
| 711 | set: (value)=> |
| 712 | { |
| 713 | ASSERT(false, `Cannot modify engine constant. Attempted to set constant (${obj}) property '${prop}' to '${value}'.`); |
| 714 | }, |
| 715 | enumerable: true |
| 716 | }); |
| 717 | }); |
| 718 | } |
| 719 | |
| 720 | // freeze the object to prevent adding new properties |
| 721 | return Object.freeze(obj); |
| 722 | } |