00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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
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
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