Back to index

lightning-sunbird  0.9+nobinonly
jsd_xpc.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Robert Ginda, <rginda@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef JSDSERVICE_H___
00041 #define JSDSERVICE_H___
00042 
00043 #include "jsdIDebuggerService.h"
00044 #include "jsdebug.h"
00045 #include "nsString.h"
00046 #include "nsCOMPtr.h"
00047 #include "nspr.h"
00048 
00049 // #if defined(DEBUG_rginda_l)
00050 // #   define DEBUG_verbose
00051 // #endif
00052 
00053 struct LiveEphemeral {
00054     /* link in a chain of live values list */
00055     PRCList                  links;
00056     jsdIEphemeral           *value;
00057     void                    *key;
00058 };
00059 
00060 struct PCMapEntry {
00061     PRUint32 pc, line;
00062 };
00063     
00064 /*******************************************************************************
00065  * reflected jsd data structures
00066  *******************************************************************************/
00067 
00068 class jsdObject : public jsdIObject
00069 {
00070   public:
00071     NS_DECL_ISUPPORTS
00072     NS_DECL_JSDIOBJECT
00073 
00074     /* you'll normally use use FromPtr() instead of directly constructing one */
00075     jsdObject (JSDContext *aCx, JSDObject *aObject) :
00076         mCx(aCx), mObject(aObject)
00077     {
00078     }
00079 
00080     static jsdIObject *FromPtr (JSDContext *aCx,
00081                                 JSDObject *aObject)
00082     {
00083         if (!aObject)
00084             return nsnull;
00085         
00086         jsdIObject *rv = new jsdObject (aCx, aObject);
00087         NS_IF_ADDREF(rv);
00088         return rv;
00089     }
00090 
00091   private:
00092     jsdObject(); /* no implementation */
00093     jsdObject(const jsdObject&); /* no implementation */
00094 
00095     JSDContext *mCx;
00096     JSDObject *mObject;
00097 };
00098 
00099 
00100 class jsdProperty : public jsdIProperty
00101 {
00102   public:
00103     NS_DECL_ISUPPORTS
00104     NS_DECL_JSDIPROPERTY
00105     NS_DECL_JSDIEPHEMERAL
00106     
00107     jsdProperty (JSDContext *aCx, JSDProperty *aProperty);
00108     virtual ~jsdProperty ();
00109     
00110     static jsdIProperty *FromPtr (JSDContext *aCx,
00111                                   JSDProperty *aProperty)
00112     {
00113         if (!aProperty)
00114             return nsnull;
00115         
00116         jsdIProperty *rv = new jsdProperty (aCx, aProperty);
00117         NS_IF_ADDREF(rv);
00118         return rv;
00119     }
00120 
00121     static void InvalidateAll();
00122 
00123   private:
00124     jsdProperty(); /* no implementation */
00125     jsdProperty(const jsdProperty&); /* no implementation */
00126 
00127     PRBool         mValid;
00128     LiveEphemeral  mLiveListEntry;
00129     JSDContext    *mCx;
00130     JSDProperty   *mProperty;
00131 };
00132 
00133 class jsdScript : public jsdIScript
00134 {
00135   public:
00136     NS_DECL_ISUPPORTS
00137     NS_DECL_JSDISCRIPT
00138     NS_DECL_JSDIEPHEMERAL
00139 
00140     /* you'll normally use use FromPtr() instead of directly constructing one */
00141     jsdScript (JSDContext *aCx, JSDScript *aScript);
00142     virtual ~jsdScript();
00143     
00144     static jsdIScript *FromPtr (JSDContext *aCx, JSDScript *aScript)
00145     {
00146         if (!aScript)
00147             return nsnull;
00148 
00149         void *data = JSD_GetScriptPrivate (aScript);
00150         jsdIScript *rv;
00151         
00152         if (data) {
00153             rv = NS_STATIC_CAST(jsdIScript *, data);
00154         } else {
00155             rv = new jsdScript (aCx, aScript);
00156             NS_IF_ADDREF(rv);  /* addref for the SetScriptPrivate, released in
00157                                 * Invalidate() */
00158             JSD_SetScriptPrivate (aScript, NS_STATIC_CAST(void *, rv));
00159         }
00160         
00161         NS_IF_ADDREF(rv); /* addref for return value */
00162         return rv;
00163     }
00164 
00165     static void InvalidateAll();
00166 
00167   private:
00168     static PRUint32 LastTag;
00169     
00170     jsdScript(); /* no implementation */
00171     jsdScript (const jsdScript&); /* no implementation */
00172     PCMapEntry* CreatePPLineMap();
00173     PRUint32    PPPcToLine(PRUint32 aPC);
00174     PRUint32    PPLineToPc(PRUint32 aLine);
00175     
00176     PRBool      mValid;
00177     PRUint32    mTag;
00178     JSDContext *mCx;
00179     JSDScript  *mScript;
00180     nsCString  *mFileName;
00181     nsCString  *mFunctionName;
00182     PRUint32    mBaseLineNumber, mLineExtent;
00183     PCMapEntry *mPPLineMap;
00184     PRUint32    mPCMapSize;
00185     jsuword     mFirstPC;
00186 };
00187 
00188 PRUint32 jsdScript::LastTag = 0;
00189 
00190 class jsdContext : public jsdIContext
00191 {
00192   public:
00193     NS_DECL_ISUPPORTS
00194     NS_DECL_JSDICONTEXT
00195     NS_DECL_JSDIEPHEMERAL
00196 
00197     jsdContext (JSDContext *aJSDCx, JSContext *aJSCx, nsISupports *aISCx);
00198     virtual ~jsdContext();
00199 
00200     static void InvalidateAll();
00201     static jsdIContext *FromPtr (JSDContext *aJSDCx, JSContext *aJSCx);
00202   private:
00203     static PRUint32 LastTag;
00204 
00205     jsdContext (); /* no implementation */
00206     jsdContext (const jsdContext&); /* no implementation */
00207 
00208     PRBool                 mValid;
00209     LiveEphemeral          mLiveListEntry;
00210     PRUint32               mTag;
00211     JSDContext            *mJSDCx;
00212     JSContext             *mJSCx;
00213     nsCOMPtr<nsISupports>  mISCx;
00214 };
00215 
00216 PRUint32 jsdContext::LastTag = 0;
00217 
00218 class jsdStackFrame : public jsdIStackFrame
00219 {
00220   public:
00221     NS_DECL_ISUPPORTS
00222     NS_DECL_JSDISTACKFRAME
00223     NS_DECL_JSDIEPHEMERAL
00224 
00225     /* you'll normally use use FromPtr() instead of directly constructing one */
00226     jsdStackFrame (JSDContext *aCx, JSDThreadState *aThreadState,
00227                    JSDStackFrameInfo *aStackFrameInfo);
00228     virtual ~jsdStackFrame();
00229 
00230     static void InvalidateAll();
00231     static jsdIStackFrame* FromPtr (JSDContext *aCx,
00232                                     JSDThreadState *aThreadState,
00233                                     JSDStackFrameInfo *aStackFrameInfo);
00234 
00235   private:
00236     jsdStackFrame(); /* no implementation */
00237     jsdStackFrame(const jsdStackFrame&); /* no implementation */
00238 
00239     PRBool             mValid;
00240     LiveEphemeral      mLiveListEntry;
00241     JSDContext        *mCx;
00242     JSDThreadState    *mThreadState;
00243     JSDStackFrameInfo *mStackFrameInfo;
00244 };
00245 
00246 class jsdValue : public jsdIValue
00247 {
00248   public:
00249     NS_DECL_ISUPPORTS
00250     NS_DECL_JSDIVALUE
00251     NS_DECL_JSDIEPHEMERAL
00252 
00253     /* you'll normally use use FromPtr() instead of directly constructing one */
00254     jsdValue (JSDContext *aCx, JSDValue *aValue);
00255     virtual ~jsdValue();
00256 
00257     static jsdIValue *FromPtr (JSDContext *aCx, JSDValue *aValue);    
00258     static void InvalidateAll();
00259     
00260   private:
00261     jsdValue(); /* no implementation */
00262     jsdValue (const jsdScript&); /* no implementation */
00263     
00264     PRBool         mValid;
00265     LiveEphemeral  mLiveListEntry;
00266     JSDContext    *mCx;
00267     JSDValue      *mValue;
00268 };
00269 
00270 /******************************************************************************
00271  * debugger service
00272  ******************************************************************************/
00273 
00274 class jsdService : public jsdIDebuggerService
00275 {
00276   public:
00277     NS_DECL_ISUPPORTS
00278     NS_DECL_JSDIDEBUGGERSERVICE
00279 
00280     jsdService() : mInitAtStartup(triUnknown), mOn(PR_FALSE), mPauseLevel(0),
00281                    mNestedLoopLevel(0), mCx(0), mRuntime(0), mErrorHook(0),
00282                    mBreakpointHook(0), mDebugHook(0), mDebuggerHook(0),
00283                    mInterruptHook(0), mScriptHook(0), mThrowHook(0),
00284                    mTopLevelHook(0), mFunctionHook(0)
00285     {
00286     }
00287 
00288     virtual ~jsdService();
00289     
00290     static jsdService *GetService ();
00291     
00292   private:
00293     enum Tristate {
00294         triUnknown = 0U,
00295         triYes = 1U,
00296         triNo = 2U
00297     };
00298         
00299     Tristate    mInitAtStartup;
00300     PRBool      mOn;
00301     PRUint32    mPauseLevel;
00302     PRUint32    mNestedLoopLevel;
00303     JSDContext *mCx;
00304     JSRuntime  *mRuntime;
00305 
00306     nsCOMPtr<jsdIErrorHook>     mErrorHook;
00307     nsCOMPtr<jsdIExecutionHook> mBreakpointHook;
00308     nsCOMPtr<jsdIExecutionHook> mDebugHook;
00309     nsCOMPtr<jsdIExecutionHook> mDebuggerHook;
00310     nsCOMPtr<jsdIExecutionHook> mInterruptHook;
00311     nsCOMPtr<jsdIScriptHook>    mScriptHook;
00312     nsCOMPtr<jsdIExecutionHook> mThrowHook;
00313     nsCOMPtr<jsdICallHook>      mTopLevelHook;
00314     nsCOMPtr<jsdICallHook>      mFunctionHook;
00315 
00316 };
00317 
00318 #endif /* JSDSERVICE_H___ */
00319 
00320 
00321 /* graveyard */
00322 
00323 #if 0
00324 
00325 class jsdContext : public jsdIContext
00326 {
00327   public:
00328     NS_DECL_ISUPPORTS
00329     NS_DECL_JSDICONTEXT
00330 
00331     /* you'll normally use use FromPtr() instead of directly constructing one */
00332     jsdContext (JSDContext *aCx) : mCx(aCx)
00333     {
00334         printf ("++++++ jsdContext\n");
00335     }
00336 
00337     static jsdIContext *FromPtr (JSDContext *aCx)
00338     {
00339         if (!aCx)
00340             return nsnull;
00341         
00342         void *data = JSD_GetContextPrivate (aCx);
00343         jsdIContext *rv;
00344         
00345         if (data) {
00346             rv = NS_STATIC_CAST(jsdIContext *, data);
00347         } else {
00348             rv = new jsdContext (aCx);
00349             NS_IF_ADDREF(rv);  // addref for the SetContextPrivate
00350             JSD_SetContextPrivate (aCx, NS_STATIC_CAST(void *, rv));
00351         }
00352         
00353         NS_IF_ADDREF(rv); // addref for the return value
00354         return rv;
00355     }
00356 
00357     virtual ~jsdContext() { printf ("------ ~jsdContext\n"); }
00358   private:            
00359     jsdContext(); /* no implementation */
00360     jsdContext(const jsdContext&); /* no implementation */
00361     
00362     JSDContext *mCx;
00363 };
00364 
00365 class jsdThreadState : public jsdIThreadState
00366 {
00367   public:
00368     NS_DECL_ISUPPORTS
00369     NS_DECL_JSDITHREADSTATE
00370 
00371     /* you'll normally use use FromPtr() instead of directly constructing one */
00372     jsdThreadState (JSDContext *aCx, JSDThreadState *aThreadState) :
00373         mCx(aCx), mThreadState(aThreadState)
00374     {
00375     }
00376 
00377     /* XXX These things are only valid for a short period of time, they reflect
00378      * state in the js engine that will go away after stepping past wherever
00379      * we were stopped at when this was created.  We could keep a list of every
00380      * instance of this we've created, and "invalidate" them before we let the
00381      * engine continue.  The next time we need a threadstate, we can search the
00382      * list to find an invalidated one, and just reuse it.
00383      */
00384     static jsdIThreadState *FromPtr (JSDContext *aCx,
00385                                      JSDThreadState *aThreadState)
00386     {
00387         if (!aThreadState)
00388             return nsnull;
00389         
00390         jsdIThreadState *rv = new jsdThreadState (aCx, aThreadState);
00391         NS_IF_ADDREF(rv);
00392         return rv;
00393     }
00394 
00395   private:
00396     jsdThreadState(); /* no implementation */
00397     jsdThreadState(const jsdThreadState&); /* no implementation */
00398 
00399     JSDContext     *mCx;
00400     JSDThreadState *mThreadState;
00401 };
00402 
00403 #endif