Subj : Re: Performance Interfacing to C++ To : netscape.public.mozilla.jseng From : Thorsten R Date : Sat Aug 28 2004 02:17 pm > Why are you using JS_DefineObject? Do you mean you are creating a JS > object to act as a peer or proxy for each C++ object and giving it a > name in the global scope? Hmm. Yes. This is exactly what I do. > Reading again, I take it from this that you are defining the objects on > the prototype itself. Have you considered using lazy techniques? See > JSClass.resolve, JSClass.getProperty, JSClass.enumerate -- the comments > on the function pointer typedefs in jspubtd.h are useful. I will take a look and - if required - open a new thread on this. > If your C++ object owns the C++ string, it can and should free it as you > say. But if you have handed off ownership to the GC, via > JS_AddExternalStringFinalizer, then you can't free it until the GC calls > the finalizer you added. You can't have it both ways. This appears to be a problem. In my C++ classes, the setters for string members (e.g. m_szStr) are always like this: void SetStr(const char *new_value) { if (m_szStr) free(m_szStr); m_szStr = strdup(new_value); } I always free the existing string (if present), before assigning a new one via strdup(). This method is also called from the code which interfaces the JS engine with my C++ class. Question: Do I really need to wait until my StringFinalizer callback is called? In the moment the internal C++ setter is called, it means that someone in JS code made an assignment to this property. This means the old C++ string (i.e. the char memory) is never used again within JS. The GC might at some point decide to free it and call my StringFinalizer callback, but I believe (hope) no function within JS is accessing the C++ string (i.e. the char memory) itself - for what reason should it do so? So, if my StringFinalizer callback is just an empty function which does nothing, it should be safe to free the string in the moment a new string is assigned to the C++ member, since the old string it is not used anymore. Or am I missing something? (I know the old JS string object keeps a pointer to the (no longer existing) old C++ string, but as long as no code in JS is accessing this pointer memory, it would be no problem to free it, instead of waiting until my StringFinalizer callback is called.) BTW, do I need to root a string object which is created with JS_NewExternalString()? Regards Thorsten .