From 021b5dab22dee589a7e0cf3fd7c28589f8090e44 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 14 Apr 2009 01:40:14 +0000 Subject: [PATCH] I swear I had made one of these way back when. Here it is again. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5716 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../threerings/util/ImmutableProxyObject.as | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/as/com/threerings/util/ImmutableProxyObject.as diff --git a/src/as/com/threerings/util/ImmutableProxyObject.as b/src/as/com/threerings/util/ImmutableProxyObject.as new file mode 100644 index 000000000..0a549aec9 --- /dev/null +++ b/src/as/com/threerings/util/ImmutableProxyObject.as @@ -0,0 +1,116 @@ +// +// $Id$ + +package com.threerings.util { + +import flash.utils.Proxy; +import flash.utils.flash_proxy; + +use namespace flash_proxy; + +/** + * Acts like the passed-in Object, but prevents modifications. + */ +public class ImmutableProxyObject extends Proxy +{ + public function ImmutableProxyObject (source :Object, throwErrors :Boolean = true) + { + _source = source; + _throwErrors = throwErrors; + } + + public function hasOwnProperty (name :String) :Boolean + { + return _source.hasOwnProperty(name); + } + + public function isPrototypeOf (theClass :Object) :Boolean + { + return _source.isPrototypeOf(theClass); + } + + public function propertyIsEnumerable (name :String) :Boolean + { + return _source.propertyIsEnumerable(name); + } + + public function setPropertyIsEnumerable (name :String, isEnum :Boolean = true) :void + { + immutable(); + } + + public function toString () :String + { + return _source.toString(); + } + + // valueOf ? + + override flash_proxy function callProperty (name :*, ... rest) :* + { + Function(_source[name]).apply(null, rest); + } + + override flash_proxy function deleteProperty (name :*) :Boolean + { + immutable(); + return false; + } + + // omitted: getDescendants + + override flash_proxy function getProperty (key :*) :* + { + return _source[key]; + } + + override flash_proxy function hasProperty (key :*) :Boolean + { + return (key in _source); + } + + // omitted: isAttribute + + override flash_proxy function nextName (index :int) :String + { + return _itrKeys[index - 1]; + } + + override flash_proxy function nextNameIndex (index :int) :int + { + if (index == 0) { + _itrKeys = Util.keys(_source); + } + + if (index < _itrKeys.length) { + return index + 1; + } else { + _itrKeys = null; + return 0; + } + } + + override flash_proxy function nextValue (index :int) :* + { + return _source[_itrKeys[index - 1]]; + } + + override flash_proxy function setProperty (name :*, value :*) :void + { + immutable(); + } + + protected function immutable () :void + { + if (_throwErrors) { + throw new Error("You may not modify this object."); + } + } + + protected var _source :Object; + + protected var _throwErrors :Boolean; + + protected var _itrKeys :Array; +} +}