Subj : Re: Creating Class Globally To : netscape.public.mozilla.jseng From : Brian Genisio Date : Tue Apr 20 2004 02:50 pm Brendan Eich wrote: > Brian Genisio wrote: > >> Hey all, >> >> Is there any way to create a Class for use in all contexts? >> >> I have a program running with many threads, and many contexts per >> thread. In addition, I have about 40 classes I need to initialize >> using JS_InitClass. I also have a context in the main thread that is >> alive as long as any thread is alive. > > > > A context is for running scripts, but it is not the same thing as a > global object. You need a global object as the scope chain for global > code, to hold the standard classes (Object, Date, etc.) plus your own. > How many global objects do you have? If you have one per context, then > you do need to use JS_InitClass for each. > > >> It seems like a waste to call InitClass for _every_ context. > > > > You mean "for every global object." > > But if it were a waste, you could avoid it and leave some global objects > without standard or custom class constructors, prototypes, etc. How > then would scripts work with those global objects? > > The approach of using JS_SealObject on a root context's standard > objects, before any other objects reference them, can work, but it is > not compatible with ECMA-262, or with JS as many scripts use JS. Once > sealed, the object graph can't be changed at all, so you can't add > String.prototype.sub to create a custom substring (or subscript, > whatever) method. > > >> Is there a way to use the classes created in the main context? I >> tried this, but when I try to construct an object with >> JS_ConstructObjectWithArguments, it returns NULL for the object. > > > > I have no idea what you did -- can you say more, or better, show code? > > >> Do I need to do anything to tell the context to use classes defined in >> a different context? > > > > Instead of worrying about pre-creating standard and/or custom classes, > look into using JS_ResolveStandardClass and the same technique for your > own custom classes. See js.c. > > /be Yes, I understand now. It is necessary for me to init the classes for each global object, which is associated with each concept. I reworked my code, and it works now. Thanks, Brian .