Subj : Re: JS_NewObject To : netscape.public.mozilla.jseng From : "Jochen Stier" Date : Mon Mar 29 2004 01:51 pm Thanks, Really helpful stuff. I will go and do my homework first, before I bother you with any more of my elucid assumptions, LOL. BTW, you did not understand my question.... I said >With "internal class" I meant a C++ class for which I have created a >corresponding JavaScript class using JS_DefineClass. Now I would like to instantiate an object of the >"internal class" from within the C++ code !!!!!!!!!!===> and not the JavaScript <==== !!!!!!!! In the >constructor of the internal class I call JS_NewObject to obtain a JSObject*. I can now use that JSObject* as >one of the parameters to a JS_CallFunction and everything works fine. I can access the methods and >members of the internal class via JavaScript. What I am confused about is the GC ? At what point >does the GC delete the JSObject*, if at all ? I am NOT creating a new object from within JavaScript. I am creating it inside the C++ app and then pass it to a JavaScript as a parameter in the call JS_CallFunction (or whatever the proper name of that function may be). I am just wondering how I can let the GC know that a JSObjects exists and when it can remove them. But never mind, I will figure it out.... Thanks a lot for your help ! Cheers Jochen BTW, if not GC then what is reference counting good for ???? "Brendan Eich" wrote in message news:40688414.6070702@meer.net... > Jochen Stier wrote: > > Hi Brendan, > > > > > > With "internal class" I meant a C++ class for which I have created a > > corresponding JavaScript > > class using JS_DefineClass. > > > Do you mean JS_InitClass? > > > > Now I would like to instantiate an object of the "internal class" from > > within the C++ code and not the JavaScript. In the constructor of the > > internal class I call > > JS_NewObject to obtain a JSObject*. > > > Why do you call JS_NewObject instead of using the obj parameter, which > if your class constructor has been called via operator new (tested by > JS_IsConstructing(cx) from within the constructor code), the obj param > will refer to a new object of your class, created for you by the engine? > > Only if you want your constructor to create a new object when invoked as > a function (without operator new), as Array and Function do, should you > code if (!JS_IsConstructing(cx)) { obj = JS_NewObject(...); ... }. > > > > I can now use that JSObject* as one of the parameters to > > a JS_CallFunction and everything works fine. I can access the methods and > > members of the internal class via JavaScript. > > > Do you return the new object via *rval from your constructor function? > > > > What I am confused about is the GC ? At what point does the GC delete the > > JSObject*, if at all ? > > > The GC marks all live GC-things starting from its set of roots, some of > which are known by the code, some of which you add via JS_AddNamedRoot. > Any object, string, or double not reached during the mark phase will > be collected and finalized during the sweep phase. > > If the new object you create in your constructor is returned via *rval, > then the caller's reference will keep that object live until the caller > returns or finishes. But within your constructor, after you call > JS_NewObject, if you haven't protected the newborn, and if you call > JS_NewObject again, the newborn root in cx that protects the last new > object allocated using cx will be overwritten, and the first object may > be collected. > > Please read and understand > http://www.mozilla.org/js/spidermonkey/gctips.html. > > > > I can certainly delete an object of the "internal class" > > > You seem to be using delete and object here in a C++ sense. Don't > confuse those meanings with JS's delete and object -- which are quite > different. > > > > but there must be some call to > > the Javascript engine to decrement the reference count to the JS_Object. > > > Why do you assume a JSObject (no _) has a reference count? > > Garbage collection does not use a reference count; reference-counting is > not garbage collection. > > > > OpenInventor has > > something like somevar->ref() and somevar->unref(). Am I correct to assume > > that JS_AddRoot > > and JS_RemoveRoot are equivalent to the the ref() and unref() calls of > > OpenInventor ? > > > No. I really think you ought to study the docs > (http://www.mozilla.org/js/spidermonkey/) and code > (http://lxr.mozilla.org/mozilla/source/js/src/) more before assuming > facts not in evidence and then asking for elucidation in the newsgroup. > > > > What about > > the JS_DeleteProperty that was mentioned by Sterling in the previous answer > > ? > > > What about it? Please read any JS book, or the ECMA-262 spec > (http://www.mozilla.org/js/language/E262-3.pdf), and learn what the > delete operator does in JS. It is not a memory deallocator. > > /be .