Subj : Re: Object reference graph: is there a way? To : netscape.public.mozilla.jseng From : anton muhin Date : Fri Sep 03 2004 08:46 pm Brendan Eich wrote: > anton muhin wrote: > >> Dear gurus, >> >> Is there a way to find out all the object that directly (or >> indirectly) reference the given object with SpiderMonkey's api? >> >> Example: >> >> var o1 = new Object() >> var o2 = new Object() >> var o3 = new Object() >> var o4 = new Object() >> >> o4.foo = o2 >> o4.bar = o3 >> >> o2.foo = o1 >> o3.bar = o1 >> >> For o1 I want to know that it is referenced by o2 & o3, and, >> indirectly, by o3. I suppose that there could be a way as this issue >> seems to be related to garbage collecting. > > Yes, and to uneval/toSource -- try uneval(o1). I might be missing something or my wording was poor, but uneval seems to be wrong tool: js> var o1 = new Object() js> var o2 = new Object() js> o2.foo = o1 [object Object] js> uneval(o1) ({}) js> uneval(o2) ({foo:{}}) js> It seems that I cannot find out from o1 that it is referenced by o2. >> The problem I'm trying to solve is notification on objects modification. > > Why do you want to walk an object graph to notify about modifications? It's just an event system of VRML JavaScript: I should generate an event if a field of an object gets changed. By now we implement standard Observer pattern, but I was just curious if we duplicate the mechanism that is already present in SpiderMonkey. Thank you for your time and answers, with the best regards, anton. .