Subj : Different ways to embed SpiderMonkey To : netscape.public.mozilla.jseng From : block111 Date : Sun Sep 12 2004 05:05 pm Hi, I've been reading about SpiderMonkey and wanted to better understand different techniquest for interfacing it in c++ apps. consider a real-life example: server side scripting using javascript (mutlithreaded server, thread per client or using thread pool) aside of StandardClasses will have two properties always available to script: header (representing http header) and session (for storing data acros requests). Implementing header is quite easy - it adds http headers in the two ways: header["Location"] = "http://www.example.com/"; header.add("Location: http://www.example.com/"); and it has a method: header.show() returning string containing generated header; /* used for development for example*/ Implementing session is not as easy as header. Internally, all sessions on server could be stored in a global_sessions map, where string indicates SESSION_ID which is managed transparently from javascript, and all we get is that global property session would represent a JSOBJECT (which is global_sessions["SEESION_ID"]...) from our map. My troubles mostly came a this point: what should be this JSOBJECT stored in the map? it could be another map, but I would like to be able to store javascript functions as session data as well: (they could be stored as javascript source, but it's not what I mean) session.letmein = function(){ print('you are not welcome here, bye...'); } /* print(...) was also defined */ or session.letmein = function(){ -- ask for a password -- } then at any other request I would do session.letmein(); When a script finishes the session property is stored back into global_sessions map by its SESSION_ID. session property also could have a few methods - ex.: delete, empty, etc. This requirement to store native javascript objects confuses me alot - should I store JSOBJECT as (JSObject*)? And here goes rainfall of questions - if the server is mutlithreaded should there be a new JSContext allocated for every client being processed? (or in case with pool of threads there would be separate context for each thread). Will the session property (wich is a js object) be garbage collected after the script finishes? Would JS_AddRoot help in this case? Suppose thread A_t processed a request and created in context A_c a js object that is stored in the global_sessions map. Then thread B_t processes next request and it retrives pointer to that JSObject corresponding to the SESSION_ID which was created by A_t thread in context A_c. Will it be possible to access that object from another context (B_c) ??? What would you suggest as better design alternative for the two cases (header and session) considering better practices using SpiderMonkey c api? PS. I do not try to reinvent the wheel - i'm not trying to write a server. These are question to know how to implement different stuff using the engine. Thakns Pavel. .