Re: [Caja] Re: a medium-size code review (metaweta/audit)

"Mike Samuel" <[email protected]>
Newsgroups gmane.comp.lang.e.general
Message-ID <[email protected]>
On 07/02/2008, Mike Stay <[email protected]> wrote:
>
> On Feb 7, 2008 9:15 AM, Mark Miller <[email protected]> wrote:
> > On Feb 7, 2008 9:11 AM, Mike Stay <[email protected]> wrote:
> > > [...]  If you'd like, I can change it so the value at a
> > > string is an array of trademarks, all of which coerce to the same
> > > string; auditing would push the trademark into that list.
> >
> > Yes, I'd prefer that, so that we can allow user-created trademarks. Thanks.
>
> OK, how's this:
>
>
>   /**
>    * Returns true if the object has a map of trademarks
>    * and the given trademark is in the map
>    */
>   function hasTrademark(trademark, obj) {
>     if (!hasOwnProp(obj, "trademarks___")) return false;
>     var list = obj.trademarks___[String(trademark)];
>     if (!list.length) return false;
>     for (var i=0; i < list.length; ++i) {
>       if (list[i]===trademark) return true;
>     }
>     return false;
>   }
>
>   /**
>    * Throws an exception if the object does not have any trademarks or
>    * the given trademark is not in the list of trademarks.
>    */
>   function guard(trademark, obj) {
>     enforce (hasTrademark(trademark, obj),
>       "This object does not have the [",
>       String(trademark),
>       "] trademark" );
>   }
>
>   /**
>    * Adds the given trademark to the given object's map of trademarks.
>    * If the object is still being constructed, delay the assignment.
>    * Creates the map if it doesn't already exist.
>    *
>    * TODO(metaweta): when erights checks in his first-class branch, add
>    * code to the make() method to set the underConstruction___ flag
>    * before the call to init(), then reset it and move the
>    * delayedTrademarks to the actual trademarks after the call to init().
>    */
>   function audit(trademark, obj) {
>     var map = obj.underConstruction___ ?
>         "delayedTrademarks___" : "trademarks___";
>     if (!obj[map]) { obj[map] = {}; }
>     var name = String(trademark);
>     if (!obj[map][name]) { obj[map][name] = []; }
>     obj[map][name].push(trademark);

If you're interested in skipping two lookups on second and subsequent calls,

var name = String(trademark);
var objMap = obj[map] || (obj[map] = {});
(objMap[name] || (objMap[name] = [])).push(trademark);

>   }
>
> --
> Mike Stay - [email protected]
> http://math.ucr.edu/~mike
> http://reperiendi.wordpress.com
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to http://groups.google.com/group/google-caja-discuss
> To unsubscribe, email google-caja-discuss-unsubscribe-/[email protected]
> -~----------~----~----~----~------~----~------~--~---
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.