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

Mike Samuel mikesamuel at gmail.com
Thu Feb 7 16:45:31 EST 2008


On 07/02/2008, Mike Stay <metaweta at gmail.com> wrote:
>
> On Feb 7, 2008 9:15 AM, Mark Miller <erights at gmail.com> wrote:
> > On Feb 7, 2008 9:11 AM, Mike Stay <metaweta at gmail.com> 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 - metaweta at gmail.com
> 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 at googlegroups.com
> -~----------~----~----~----~------~----~------~--~---
>
>


More information about the e-lang mailing list