Main Page   Compound List   File List   Compound Members   File Members  

TclXPCOMLoader.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * The contents of this file are subject to the Mozilla Public License Version
00004  * 1.1 (the "License"); you may not use this file except in compliance with
00005  * the License. You may obtain a copy of the License at
00006  * http://www.mozilla.org/MPL/
00007  *
00008  * Software distributed under the License is distributed on an "AS IS" basis,
00009  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00010  * for the specific language governing rights and limitations under the
00011  * License.
00012  *
00013  * The Original Code is TclXPCOM.
00014  * 
00015  * The Initial Developer of the Original Code is Mark Follett.
00016  * Portions created by Mark Follett are Copyright (C) 2001-2002
00017  * Mark Follett.  All Rights Reserved.
00018  * 
00019  * Contributor(s):
00020  *     Mark Follett <mef123@myrealbox.com> (Original Author)
00021  *
00022  * Alternatively, the contents of this file may be used under the terms of
00023  * either the GNU General Public License Version 2 or later (the "GPL"), or
00024  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00025  * in which case the provisions of the GPL or the LGPL are applicable instead
00026  * of those above. If you wish to allow use of your version of this file only
00027  * under the terms of either the GPL or the LGPL, and not to allow others to
00028  * use your version of this file under the terms of the MPL, indicate your
00029  * decision by deleting the provisions above and replace them with the notice
00030  * and other provisions required by the GPL or the LGPL. If you do not delete
00031  * the provisions above, a recipient may use your version of this file under
00032  * the terms of any one of the MPL, the GPL or the LGPL.
00033  */
00034 
00041 #ifndef MOZILLA_STRICT_API
00042 #define MOZILLA_STRICT_API
00043 #endif
00044 
00045 #include "tclxpcom.h"
00046 #include "nsIModule.h"
00047 #include "nsIComponentManager.h"
00048 #include "nsIFile.h"
00049 
00050 static nsIID nsimodule_iid = NS_IMODULE_IID;
00051 
00052 static int loaderFailed = 0;
00053 
00054 #define LOADER_SCRIPT "                                  \
00055   package require tclxpcom;                              \
00056   xpcom::module {package require tclxpcom_loader} {      \
00057     puts \"unloading\"; unload                           \
00058   }                                                      \
00059 "
00060 
00069 int
00070 GetModuleFromObj(
00071     Tcl_Interp *interp, 
00072     Tcl_Obj    *objPtr, 
00073     nsIModule **module  
00074 )
00075 {
00076     int rv;
00077     nsresult res;
00078     nsISupports *isupports;
00079     Tcl_ObjType *isupportstype;
00080 
00081     // no precondition for interp
00082     NS_PRECONDITION(objPtr, "null pointer");
00083     NS_PRECONDITION(module, "null pointer");
00084 
00085     isupportstype = Tcl_GetObjType(TCLXPCOM_ISUPPORTSTYPENAME);
00086 
00087     rv = Tcl_ConvertToType(interp, objPtr, isupportstype);
00088 
00089     if (rv == TCL_OK)
00090     {
00091         isupports = (nsISupports *)Tcl_GetHashValue((Tcl_HashEntry *)objPtr->internalRep.twoPtrValue.ptr2);
00092 
00093         res = isupports->QueryInterface(nsimodule_iid, (void **)module);
00094         if (NS_FAILED(res))
00095         {
00096             rv = TCL_ERROR;
00097         }
00098     }
00099 
00100     return rv;
00101 }
00102 
00103 
00111 int
00112 UnloadObjCmd(
00113     ClientData  clientData,  
00114     Tcl_Interp *interp,      
00115     int         objc,        
00116     Tcl_Obj    *CONST objv[] 
00117 )
00118 {
00119     Tcl_Interp *gInterp;
00120 
00121     NS_PRECONDITION(clientData, "null pointer");
00122     NS_PRECONDITION(interp, "null pointer");
00123     NS_PRECONDITION((objc >= 0), "objc must be positive");
00124     NS_PRECONDITION(objv, "null pointer");
00125 
00126     if (objc != 1)
00127     {
00128         Tcl_WrongNumArgs(interp, 1, objv, "");
00129         return TCL_ERROR;
00130     }
00131 
00132     gInterp = (Tcl_Interp *)clientData;
00133 
00134     Tcl_Preserve(gInterp);
00135     if (!Tcl_InterpDeleted(gInterp))
00136     {
00137         Tcl_DeleteInterp(gInterp);
00138     }
00139     Tcl_Release(gInterp);
00140 
00141     return TCL_OK;
00142 }
00143 
00144 
00150 extern "C" NS_EXPORT
00151 nsresult
00152 NSGetModule(
00153     nsIComponentManager *servMgr, 
00154     nsIFile             *location,
00155     nsIModule          **result   
00156 )
00157 {
00158     int rv;
00159     Tcl_Interp *interp;
00160 
00161     NS_PRECONDITION(servMgr, "null pointer");
00162     NS_PRECONDITION(location, "null pointer");
00163     NS_PRECONDITION(result, "null pointer");
00164 
00165     if (loaderFailed)
00166     {
00167         return NS_ERROR_FAILURE;
00168     }
00169 
00170     printf("Attempting to load the TclXPCOM Component Loader module...\n");
00171 
00172     // XXX this probably doesn't work on unix
00173     Tcl_FindExecutable("");
00174 
00175     interp = Tcl_CreateInterp();
00176     Tcl_Preserve(interp);
00177 
00178     rv = Tcl_Init(interp);
00179     if (rv == TCL_OK)
00180     {
00181         Tcl_CreateObjCommand(interp, "unload", UnloadObjCmd, interp, NULL);
00182         rv = Tcl_EvalObjEx(interp, Tcl_NewStringObj(LOADER_SCRIPT, -1), TCL_EVAL_GLOBAL | TCL_EVAL_DIRECT);
00183     }
00184 
00185     if (rv == TCL_OK)
00186     {
00187         rv = GetModuleFromObj(interp, Tcl_GetObjResult(interp), result);
00188     }
00189 
00190     if (rv != TCL_OK)
00191     {
00192         printf("Unable to load module:\n%s\n", Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
00193     }
00194 
00195     if (rv == TCL_OK)
00196     {
00197         // clear the module reference out of the interpreters result
00198         Tcl_ResetResult(interp);
00199         Tcl_Release(interp);
00200         return NS_OK;
00201     } else
00202     {
00203         if (!Tcl_InterpDeleted(interp))
00204         {
00205             Tcl_DeleteInterp(interp);
00206         }
00207         Tcl_Release(interp);
00208 
00209         loaderFailed = 1;
00210         return NS_ERROR_FAILURE;
00211     }
00212 }
00213 

Generated on Fri Jun 14 23:25:51 2002 for TclXPCOM by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002