MCPcopy
hub / github.com/KilledByAPixel/LittleJS / debugProtectConstant

Function debugProtectConstant

src/engineDebug.js:698–722  ·  view source on GitHub ↗
(obj)

Source from the content-addressed store, hash-verified

696
697// make color constants immutable with debug assertions
698function 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}

Callers 1

engineMath.jsFile · 0.70

Calls 1

ASSERTFunction · 0.70

Tested by

no test coverage detected