Main Page   Compound List   File List   Compound Members   File Members  

TclXPCOMCmds.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 
00040 #include "TclXPCOMPrivate.h"
00041 
00042 
00051 int
00052 GetInterfaceInfoFromObj(
00053     Tcl_Interp        *interp,       
00054     Tcl_Obj           *objPtr,       
00055     nsIInterfaceInfo **interfaceinfo 
00056 )
00057 {
00058     int rv;
00059     nsresult res;
00060     nsIID *iid;
00061 
00062     NS_PRECONDITION(interp, "null pointer");
00063     NS_PRECONDITION(objPtr, "null pointer");
00064     NS_PRECONDITION(interfaceinfo, "null pointer");
00065 
00066     rv = TclXPCOM_GetNsIDFromObj(interp, objPtr, (nsID **)&iid);
00067 
00068     if (rv == TCL_OK)
00069     {
00070         res = manager->GetInfoForIID(iid, interfaceinfo);
00071     }
00072 
00073     if ((rv != TCL_OK) || (NS_FAILED(res)))
00074     {
00075         Tcl_ResetResult(interp);
00076         Tcl_AppendResult(interp, "unknown interface \"", Tcl_GetString(objPtr), "\"", NULL);
00077         return TCL_ERROR;
00078     }
00079 
00080     return TCL_OK;
00081 }
00082 
00083 
00090 int
00091 VerifyNsIDObj(
00092     Tcl_Interp *interp, 
00093     Tcl_Obj    *iidobj, 
00094     int         flags   
00095 )
00096 {
00097     int rv;
00098     nsIInterfaceInfo *info;
00099 
00100     NS_PRECONDITION(interp, "null pointer");
00101     NS_PRECONDITION(iidobj, "null pointer");
00102     NS_PRECONDITION(!(flags & ~(FORCE_HIDDEN | FORCE_NOTXPCOM | FORCE_NOTSCRIPTABLE)), "invalid flags");
00103 
00104     rv = GetInterfaceInfoFromObj(interp, iidobj, &info);
00105 
00106     if (rv == TCL_OK)
00107     {
00108         rv = TclXPCOM_CheckInterfaceIsReflectable(interp, info, flags);
00109         NS_RELEASE(info);
00110     }
00111 
00112     return rv;
00113 }
00114 
00115 
00122 int
00123 GetClassInfoFromInterfaceList(
00124     Tcl_Interp *interp,     
00125     Tcl_Obj    *interfaces, 
00126     int         flags,      
00127     ClassInfo **cinfo       
00128 )
00129 {
00130     int rv;
00131     int i;
00132     Tcl_Obj **iidobjlist;
00133     nsIID **iids;
00134     int iidcount;
00135     const nsIID *iid;
00136 
00137     NS_PRECONDITION(interp, "null pointer");
00138     //no precondition on interfaces
00139     NS_PRECONDITION(!(flags & ~(FORCE_HIDDEN | FORCE_NOTXPCOM | FORCE_NOTSCRIPTABLE)), "invalid flags");
00140     NS_PRECONDITION(cinfo, "null pointer");
00141  
00142     if (interfaces)
00143     {
00144         // verify interface list is valid
00145         rv = Tcl_ListObjGetElements(interp, interfaces, &iidcount, &iidobjlist);
00146         if (rv != TCL_OK)
00147         {
00148             return rv;
00149         }
00150     } else
00151     {
00152         iidcount = 0;
00153     }
00154     
00155     if (iidcount)
00156     {
00157         for (i = 0; i < iidcount; i++)
00158         {
00159             rv = VerifyNsIDObj(interp, iidobjlist[i], flags);
00160             if (rv != TCL_OK)
00161             {
00162                 return rv;
00163             }
00164         }
00165         
00166         iids = (nsIID **)memmgr->Alloc(sizeof(nsIID *) * iidcount);
00167         for (i = 0; i < iidcount; i++)
00168         {
00169             TclXPCOM_GetNsIDFromObj(interp, iidobjlist[i], &iids[i]);
00170         }
00171         
00172         *cinfo = TclXPCOM_GetClassInfo(iidcount, iids);
00173         memmgr->Free(iids);
00174     } else
00175     {
00176         iid = &nsisupports_iid;
00177         *cinfo = TclXPCOM_GetClassInfo(1, (nsIID **)&iid);
00178     }
00179 
00180     return TCL_OK;
00181 }
00182 
00183 
00194 int
00195 GetOverrideSwitches(
00196     ClientData     clientData,
00197     Tcl_Interp    *interp,    
00198     int            objc,      
00199     Tcl_Obj *CONST objv[],    
00200     int           *baseindex, 
00201     int           *flags      
00202 )
00203 {
00204     int i;
00205     int index;
00206     char *name;
00207 
00208     static char *switches[] = {"-hidden", "-notxpcom", "-noscript", "--", NULL};
00209     enum options {EVAL_HIDDEN, EVAL_NOTXPCOM, EVAL_NOTSCRIPTABLE, EVAL_LAST};
00210 
00211     // no precondition on clientData
00212     NS_PRECONDITION(interp, "null pointer");
00213     NS_PRECONDITION((objc >= 0), "objc must be positive");
00214     NS_PRECONDITION(objv, "null pointer");
00215     NS_PRECONDITION(baseindex, "null pointer");
00216     NS_PRECONDITION((*baseindex >= 0), "baseindex must be positive");
00217     NS_PRECONDITION(flags, "null pointer");
00218 
00219     *flags = 0;
00220     
00221     for (i = *baseindex; i < objc; i++)
00222     {
00223         name = Tcl_GetString(objv[i]);
00224         
00225         if (name[0] != '-')
00226         {
00227             break;
00228         }
00229         
00230         if (Tcl_GetIndexFromObj(interp, objv[i], switches, "switch", TCL_EXACT, &index) != TCL_OK)
00231         {
00232             return TCL_ERROR;
00233         }
00234         
00235         switch (index)
00236         {
00237             case EVAL_HIDDEN:
00238                 *flags |= FORCE_HIDDEN;
00239                 break;
00240             
00241             case EVAL_NOTXPCOM:
00242                 *flags |= FORCE_NOTXPCOM;
00243                 break;
00244 
00245             case EVAL_NOTSCRIPTABLE:
00246                 *flags |= FORCE_NOTSCRIPTABLE;
00247                 break;
00248             
00249             case EVAL_LAST:
00250                 i++;
00251                 goto endOfForLoop;
00252         }
00253     }
00254     
00255 endOfForLoop:
00256 
00257     *baseindex = i;
00258 
00259     return TCL_OK;
00260 }
00261 
00262 
00269 int
00270 TclXPCOM_WrapObjCmd(
00271     ClientData     clientData,
00272     Tcl_Interp    *interp,    
00273     int            objc,      
00274     Tcl_Obj *CONST objv[]     
00275 )
00276 {
00277     int rv;
00278     int i;
00279     int flags;
00280     Tcl_Obj *interfaces;
00281 
00282     int bindlistcount;
00283     Tcl_Obj **bindlist;
00284 
00285     TclXPCOMComponent *component;
00286     ClassInfo *cinfo;
00287 
00288     // no precondition on clientData
00289     NS_PRECONDITION(interp, "null pointer");
00290     NS_PRECONDITION((objc >= 0), "objc must be positive");
00291     NS_PRECONDITION(objv, "null pointer");
00292 
00293     i = 1;
00294 
00295     rv = GetOverrideSwitches(clientData, interp, objc, objv, &i, &flags);
00296     if (rv != TCL_OK)
00297     {
00298         return rv;
00299     }
00300 
00301     if (objc - i > 2)
00302     {
00303         Tcl_WrongNumArgs(interp, 2, objv, "?options? ?interfaces? ?bindings?");
00304         return TCL_ERROR;
00305     }
00306     
00307     if (objc - i > 0)
00308     {
00309         interfaces = objv[i];
00310     } else
00311     {
00312         interfaces = NULL;
00313     }
00314 
00315     rv = GetClassInfoFromInterfaceList(interp, interfaces, flags, &cinfo);
00316     if (rv != TCL_OK)
00317     {
00318         return rv;
00319     }
00320 
00321     if (objc - i > 1)
00322     {
00323         // verify binding list is valid and contains an even number of elements
00324         rv = Tcl_ListObjGetElements(interp, objv[i+1], &bindlistcount, &bindlist);
00325         if (rv != TCL_OK)
00326         {
00327             return rv;
00328         }
00329     } else
00330     {
00331         bindlistcount = 0;
00332     }
00333 
00334     if (bindlistcount & 1)
00335     {
00336         Tcl_ResetResult(interp);
00337         Tcl_AppendResult(interp, "binding list must have an even number of elements", NULL);
00338         return TCL_ERROR;
00339     }
00340 
00341     component = NewTclXPCOMComponent(interp, Tcl_GetCurrentThread(), cinfo);
00342 
00343     // process each binding
00344     for (i = 0; i < bindlistcount; i = i + 2)
00345     {
00346         rv = BindScript(interp, component, bindlist[i], bindlist[i+1], flags);
00347         if (rv != TCL_OK)
00348         {
00349             FreeTclXPCOMComponent(component);
00350             return rv;
00351         }
00352     }
00353 
00354     char str[128];
00355     sprintf(str, "Created   Component %p, Refcount: 0", component);
00356     NS_WARNING(str);
00357 
00358     Tcl_SetObjResult(interp, TclXPCOM_NewISupportsObj((nsISupports *)(nsXPTCStubBase *)component->stubs[0], nsisupports_info));
00359 
00360     return TCL_OK;
00361 }
00362 
00363 
00370 int
00371 TclXPCOM_InvokeObjCmd(
00372     ClientData     clientData,
00373     Tcl_Interp    *interp,    
00374     int            objc,      
00375     Tcl_Obj *CONST objv[]     
00376 )
00377 {
00378     int rv;
00379     int i, baseindex, flags, type;
00380     PRUint16 methodindex;
00381 
00382     nsISupports      *isupports, *ref;
00383     nsIInterfaceInfo *interfaceinfo, *queryinfo;
00384     InterfaceRef     *iref;
00385     MethodInfo       *minfo;
00386     Tcl_Obj          *args;
00387 
00388     // no precondition on clientData
00389     NS_PRECONDITION(interp, "null pointer");
00390     NS_PRECONDITION((objc >= 0), "objc must be positive");
00391     NS_PRECONDITION(objv, "null pointer");
00392 
00393     i = (int)clientData;
00394 
00395     rv = GetOverrideSwitches(clientData, interp, objc, objv, &i, &flags);
00396     if (rv != TCL_OK)
00397     {
00398         return rv;
00399     }
00400     
00401     if (objc - i < 2)
00402     {
00403         Tcl_WrongNumArgs(interp, 2, objv, "?options? interface command ?args?");
00404         return TCL_ERROR;
00405     }
00406 
00407     rv = TclXPCOM_GetISupportsFromObj(interp, objv[i], &isupports, &interfaceinfo);
00408     if (rv != TCL_OK)
00409     {
00410         return rv;
00411     }
00412 
00413     i++;
00414     baseindex = i;
00415 
00416     if (!strcmp(Tcl_GetString(objv[i]), "set"))
00417     {
00418         if (objc - i == 1)
00419         {
00420             Tcl_WrongNumArgs(interp, 1, objv, "attribute ?value?");
00421             return TCL_ERROR;
00422 
00423         } else if (objc - i == 2)
00424         {
00425             type = INTERFACEINFO_GETTER;
00426         } else
00427         {
00428             type = INTERFACEINFO_SETTER;
00429         }
00430 
00431         i++;
00432     } else
00433     {
00434         type = INTERFACEINFO_METHOD;
00435     }
00436 
00437     rv = GetMethodFromIRef(interp, isupports, interfaceinfo, objv[i], type, &queryinfo, &methodindex);
00438     if (rv != TCL_OK)
00439     {
00440         return rv;
00441     }
00442 
00443     rv = TclXPCOM_CheckInterfaceIsReflectable(interp, queryinfo, flags);
00444     if (rv != TCL_OK)
00445     {
00446         return rv;
00447     }
00448 
00449     rv = TclXPCOM_CheckMethodIsReflectable(interp, queryinfo, methodindex, flags);
00450     if (rv != TCL_OK)
00451     {
00452         return rv;
00453     }
00454 
00455     iref = GetInterfaceRef(isupports);
00456     NS_ASSERTION(iref, "isupports entry doesn't exist in irefhash");
00457 
00458     rv = QueryInterfaceIRef(interp, iref, queryinfo, &ref);
00459     if (rv != TCL_OK)
00460     {
00461         return rv;
00462     }
00463 
00464     i++;
00465 
00466     minfo = GetMethodInfo(queryinfo, methodindex);
00467     if ((objc - i) != minfo->tclparamcount)
00468     {
00469         args = TclXPCOM_ListMethodArguments(queryinfo, methodindex);
00470         Tcl_WrongNumArgs(interp, i - baseindex, &objv[baseindex], Tcl_GetString(args));
00471         Tcl_DecrRefCount(args);
00472         return TCL_ERROR;
00473     }
00474 
00475     rv = TclXPCOM_Invoke(interp, &objv[i], ref, queryinfo, methodindex);
00476 
00477     return rv;
00478 }
00479 
00480 
00487 int
00488 TclXPCOM_InfoObjCmd(
00489     ClientData     clientData,
00490     Tcl_Interp    *interp,    
00491     int            objc,      
00492     Tcl_Obj *CONST objv[]     
00493 )
00494 {
00495     int   rv;
00496     int   index;
00497     nsIInterfaceInfo   *interfaceinfo;
00498     ClassInfo          *cinfo;
00499     IdentifierInfo     *idinfo;
00500     
00501     static char *commands[] = {"constants", "attributes", "methods", 
00502                         "args", "type", "retval",
00503                         "servicemanager", NULL};
00504 
00505     enum command {XPCOM_CONSTANTS, XPCOM_ATTRIBUTES, XPCOM_METHODS,
00506                   XPCOM_ARGS, XPCOM_TYPE, XPCOM_RETVAL,
00507                   XPCOM_SERVICEMANAGER};
00508 
00509     // no precondition on clientData
00510     NS_PRECONDITION(interp, "null pointer");
00511     NS_PRECONDITION((objc >= 0), "objc must be positive");
00512     NS_PRECONDITION(objv, "null pointer");
00513 
00514     if (objc < 2)
00515     {
00516         Tcl_WrongNumArgs(interp, 2, objv, "option ?arg arg ...?");
00517         return TCL_ERROR;
00518     }
00519 
00520     rv = Tcl_GetIndexFromObj(interp, objv[1], commands, "option", 0, &index);
00521     if (rv != TCL_OK)
00522     {
00523         return rv;
00524     }
00525 
00526     switch (index)
00527     {
00528         case XPCOM_SERVICEMANAGER:
00529             if (objc != 2)
00530             {
00531                 Tcl_WrongNumArgs(interp, 2, objv, NULL);
00532                 return TCL_ERROR;
00533             }
00534 
00535             Tcl_SetObjResult(interp, TclXPCOM_NewISupportsObj(servmanager, nsiservicemanager_info));
00536             return TCL_OK;
00537             break;
00538 
00539         case XPCOM_TYPE:
00540             if (objc != 4)
00541             {
00542                 Tcl_WrongNumArgs(interp, 2, objv, "interfaces attribute");
00543                 return TCL_ERROR;
00544             }
00545 
00546             rv = GetClassInfoFromInterfaceList(interp, objv[2], FORCE_NOTSCRIPTABLE, &cinfo);
00547             if (rv != TCL_OK)
00548             {
00549                 return rv;
00550             }
00551 
00552             rv = TclXPCOM_GetIdentifierInfoFromClass(interp, cinfo, objv[3], INTERFACEINFO_GETTER, &interfaceinfo, &idinfo);
00553             if (rv != TCL_OK)
00554             {
00555                 return rv;
00556             }
00557             
00558             Tcl_SetObjResult(interp, TclXPCOM_GetAttributeType(interfaceinfo, idinfo));
00559             return TCL_OK;
00560             break;
00561 
00562         case XPCOM_RETVAL:
00563             if (objc != 4)
00564             {
00565                 Tcl_WrongNumArgs(interp, 2, objv, "interfaces method");
00566                 return TCL_ERROR;
00567             }
00568 
00569             rv = GetClassInfoFromInterfaceList(interp, objv[2], FORCE_NOTSCRIPTABLE, &cinfo);
00570             if (rv != TCL_OK)
00571             {
00572                 return rv;
00573             }
00574 
00575             rv = TclXPCOM_GetIdentifierInfoFromClass(interp, cinfo, objv[3], INTERFACEINFO_METHOD, &interfaceinfo, &idinfo);
00576             if (rv != TCL_OK)
00577             {
00578                 return rv;
00579             }
00580             
00581             Tcl_SetObjResult(interp, TclXPCOM_GetReturnValueType(interfaceinfo, idinfo));
00582             return TCL_OK;
00583             break;
00584 
00585         case XPCOM_ARGS:
00586             if (objc != 4)
00587             {
00588                 Tcl_WrongNumArgs(interp, 2, objv, "interfaces method");
00589                 return TCL_ERROR;
00590             }
00591 
00592             rv = GetClassInfoFromInterfaceList(interp, objv[2], FORCE_NOTSCRIPTABLE, &cinfo);
00593             if (rv != TCL_OK)
00594             {
00595                 return rv;
00596             }
00597 
00598             rv = TclXPCOM_GetIdentifierInfoFromClass(interp, cinfo, objv[3], INTERFACEINFO_METHOD, &interfaceinfo, &idinfo);
00599             if (rv != TCL_OK)
00600             {
00601                 return rv;
00602             }
00603             
00604             Tcl_SetObjResult(interp, TclXPCOM_ListMethodArguments(interfaceinfo, idinfo->indices.methodindex));
00605             return TCL_OK;
00606             break;
00607 
00608         case XPCOM_CONSTANTS:
00609             if (objc != 3)
00610             {
00611                 Tcl_WrongNumArgs(interp, 2, objv, "interface");
00612                 return TCL_ERROR;
00613             }
00614 
00615             rv = GetInterfaceInfoFromObj(interp, objv[2], &interfaceinfo);
00616             if (rv != TCL_OK)
00617             {
00618                 return rv;
00619             }
00620 
00621             Tcl_SetObjResult(interp, TclXPCOM_ListInterfaceConstants(interfaceinfo));
00622             NS_RELEASE(interfaceinfo);
00623             return TCL_OK;
00624             break;
00625 
00626         case XPCOM_ATTRIBUTES:
00627             if (objc != 3)
00628             {
00629                 Tcl_WrongNumArgs(interp, 2, objv, "interfaces");
00630                 return TCL_ERROR;
00631             }
00632 
00633             rv = GetClassInfoFromInterfaceList(interp, objv[2], FORCE_NOTSCRIPTABLE, &cinfo);
00634             if (rv != TCL_OK)
00635             {
00636                 return rv;
00637             }
00638 
00639             Tcl_SetObjResult(interp, TclXPCOM_ListClassAttributes(cinfo));
00640             return TCL_OK;
00641             break;
00642 
00643         case XPCOM_METHODS:
00644             if (objc != 3)
00645             {
00646                 Tcl_WrongNumArgs(interp, 2, objv, "interfaces");
00647                 return TCL_ERROR;
00648             }
00649 
00650             rv = GetClassInfoFromInterfaceList(interp, objv[2], FORCE_NOTSCRIPTABLE, &cinfo);
00651             if (rv != TCL_OK)
00652             {
00653                 return rv;
00654             }
00655 
00656             Tcl_SetObjResult(interp, TclXPCOM_ListClassMethods(cinfo));
00657             return TCL_OK;
00658             break;
00659 
00660         }
00661 
00662     return TCL_OK;
00663 }
00664 
00665 
00672 int
00673 TclXPCOM_ThisObjCmd(
00674     ClientData     clientData,
00675     Tcl_Interp    *interp,    
00676     int            objc,      
00677     Tcl_Obj *CONST objv[]     
00678 )
00679 {
00680     nsISupports      *isupports;
00681     nsIInterfaceInfo *interfaceinfo;
00682 
00683     if (objc != 1)
00684     {
00685         Tcl_WrongNumArgs(interp, 1, objv, NULL);
00686         return TCL_ERROR;
00687     }
00688     
00689     TclXPCOM_GetCurrentComponentContext(&interfaceinfo, &isupports);
00690     if (isupports)
00691     {
00692         Tcl_SetObjResult(interp, TclXPCOM_NewISupportsObj(isupports, interfaceinfo));
00693         return TCL_OK;
00694     } else
00695     {
00696         Tcl_ResetResult(interp);
00697         Tcl_AppendResult(interp, "not in a component context", NULL);
00698         return TCL_ERROR;
00699     }
00700 }
00701 
00702 
00709 int
00710 TclXPCOM_ConstantObjCmd(
00711     ClientData     clientData,
00712     Tcl_Interp    *interp,    
00713     int            objc,      
00714     Tcl_Obj *CONST objv[]     
00715 )
00716 {
00717     int rv;
00718     Tcl_Obj *result;
00719     nsIInterfaceInfo *interfaceinfo;
00720 
00721     if (objc != 3)
00722     {
00723         Tcl_WrongNumArgs(interp, 1, objv, "interface constant");
00724         return TCL_ERROR;
00725     }
00726     
00727     rv = GetInterfaceInfoFromObj(interp, objv[1], &interfaceinfo);
00728     if (rv != TCL_OK)
00729     {
00730         return rv;
00731     }
00732     
00733     rv = TclXPCOM_GetConstant(interp, interfaceinfo, objv[2], &result);
00734     
00735     if (rv == TCL_OK)
00736     {
00737         Tcl_SetObjResult(interp, result);
00738     }
00739     
00740     NS_RELEASE(interfaceinfo);
00741     return rv;
00742 }
00743 
00744 
00751 int
00752 TclXPCOM_NullObjCmd(
00753     ClientData     clientData,
00754     Tcl_Interp    *interp,    
00755     int            objc,      
00756     Tcl_Obj *CONST objv[]     
00757 )
00758 {
00759     if (objc != 1)
00760     {
00761         Tcl_WrongNumArgs(interp, 1, objv, NULL);
00762         return TCL_ERROR;
00763     }
00764     
00765     Tcl_SetObjResult(interp, TclXPCOM_NewPointerObj(NULL));
00766     return TCL_OK;
00767 }
00768 
00769 
00776 int
00777 TclXPCOM_IsNullObjCmd(
00778     ClientData     clientData,
00779     Tcl_Interp    *interp,    
00780     int            objc,      
00781     Tcl_Obj *CONST objv[]     
00782 )
00783 {
00784     if (objc != 2)
00785     {
00786         Tcl_WrongNumArgs(interp, 1, objv, "pointer");
00787         return TCL_ERROR;
00788     }
00789     
00790     Tcl_SetObjResult(interp, Tcl_NewBooleanObj(TclXPCOM_IsNULL(objv[1])));
00791     return TCL_OK;
00792 }
00793 
00794 
00801 int
00802 TclXPCOM_PointerObjCmd(
00803     ClientData     clientData,
00804     Tcl_Interp    *interp,    
00805     int            objc,      
00806     Tcl_Obj *CONST objv[]     
00807 )
00808 {
00809     void *pointerVal;
00810 
00811     if (objc != 2)
00812     {
00813         Tcl_WrongNumArgs(interp, 1, objv, "address");
00814         return TCL_ERROR;
00815     }
00816     
00817     if (sscanf(Tcl_GetString(objv[1]), "0x%p", &pointerVal) == 1)
00818     {
00819         Tcl_SetObjResult(interp, TclXPCOM_NewPointerObj(pointerVal));
00820         return TCL_OK;
00821     } else
00822     {
00823         Tcl_ResetResult(interp);
00824         Tcl_AppendResult(interp, "\"", Tcl_GetString(objv[1]), "\" is not a valid pointer address", NULL);
00825         return TCL_ERROR;
00826     }
00827 }
00828 
00829 
00836 int
00837 TclXPCOM_AddressObjCmd(
00838     ClientData     clientData,
00839     Tcl_Interp    *interp,    
00840     int            objc,      
00841     Tcl_Obj *CONST objv[]     
00842 )
00843 {
00844     int rv;
00845     void *pointerVal;
00846     char str[64];
00847 
00848     if (objc != 2)
00849     {
00850         Tcl_WrongNumArgs(interp, 1, objv, "pointer");
00851         return TCL_ERROR;
00852     }
00853     
00854     rv = TclXPCOM_GetPointerFromObj(interp, objv[1], &pointerVal);
00855     if (rv != TCL_OK)
00856     {
00857         return rv;
00858     }
00859     
00860     sprintf(str, "0x%p", pointerVal);
00861     Tcl_SetObjResult(interp, Tcl_NewStringObj(str, -1));
00862     return TCL_OK;
00863 }
00864 
00865 
00872 int
00873 TclXPCOM_FromStringObjCmd(
00874     ClientData     clientData,
00875     Tcl_Interp    *interp,    
00876     int            objc,      
00877     Tcl_Obj *CONST objv[]     
00878 )
00879 {
00880     int rv;
00881 
00882     if (objc != 2)
00883     {
00884         Tcl_WrongNumArgs(interp, 1, objv, "string");
00885         return TCL_ERROR;
00886     }
00887     
00888     rv = Tcl_ConvertToType(interp, objv[1], &ISupportsType);
00889     
00890     if (rv != TCL_OK)
00891     {
00892         return rv;
00893     }
00894     
00895     Tcl_SetObjResult(interp, objv[1]);
00896     return TCL_OK;
00897 }
00898 
00899 
00906 int
00907 TclXPCOM_SetReturnCodeObjCmd(
00908     ClientData     clientData,
00909     Tcl_Interp    *interp,    
00910     int            objc,      
00911     Tcl_Obj *CONST objv[]     
00912 )
00913 {
00914     int rv;
00915     long longVal;
00916 
00917     if (objc != 2)
00918     {
00919         Tcl_WrongNumArgs(interp, 1, objv, "code");
00920         return TCL_ERROR;
00921     }
00922     
00923     rv = Tcl_GetLongFromObj(interp, objv[1], &longVal);
00924     if (rv != TCL_OK)
00925     {
00926         return rv;
00927     }
00928     
00929     if (!TclXPCOM_SetReturnCode((nsresult)longVal))
00930     {
00931         Tcl_ResetResult(interp);
00932         Tcl_AppendResult(interp, "not in a component context", NULL);
00933         return TCL_ERROR;
00934     }
00935     
00936     return TCL_OK;
00937 }
00938 
00939 
00946 int
00947 TclXPCOM_IdObjCmd(
00948     ClientData     clientData,
00949     Tcl_Interp    *interp,    
00950     int            objc,      
00951     Tcl_Obj *CONST objv[]     
00952 )
00953 {
00954     int rv;
00955     nsIID *iid;
00956     char *str;
00957 
00958     if (objc != 2)
00959     {
00960         Tcl_WrongNumArgs(interp, 1, objv, "name");
00961         return TCL_ERROR;
00962     }
00963     
00964     rv = TclXPCOM_GetNsIDFromObj(interp, objv[1], &iid);
00965     if (rv != TCL_OK)
00966     {
00967         return rv;
00968     }
00969     
00970     str = ((nsID *)iid)->ToString();
00971     Tcl_SetObjResult(interp, Tcl_NewStringObj(str, -1));
00972     memmgr->Free(str);
00973     return TCL_OK;
00974 }
00975 
00976 
00977 #define NUM_COMMANDS  12
00978 
00979 static struct
00980 {
00981     char *command;
00982     Tcl_ObjCmdProc *cmdproc;
00983     ClientData clientdata;
00984 
00985 } cmdtable[NUM_COMMANDS] =
00986 {
00987     {"xpcom::invoke",        TclXPCOM_InvokeObjCmd,        (ClientData)1},
00988     {"xpcom::wrap",          TclXPCOM_WrapObjCmd,          (ClientData)NULL},
00989     {"xpcom::this",          TclXPCOM_ThisObjCmd,          (ClientData)NULL},
00990     {"xpcom::constant",      TclXPCOM_ConstantObjCmd,      (ClientData)NULL},
00991     {"xpcom::info",          TclXPCOM_InfoObjCmd,          (ClientData)NULL},
00992     {"xpcom::null",          TclXPCOM_NullObjCmd,          (ClientData)NULL},
00993     {"xpcom::isnull",        TclXPCOM_IsNullObjCmd,        (ClientData)NULL},
00994     {"xpcom::pointer",       TclXPCOM_PointerObjCmd,       (ClientData)NULL},
00995     {"xpcom::address",       TclXPCOM_AddressObjCmd,       (ClientData)NULL},
00996     {"xpcom::fromstring",    TclXPCOM_FromStringObjCmd,    (ClientData)NULL},
00997     {"xpcom::setReturnCode", TclXPCOM_SetReturnCodeObjCmd, (ClientData)NULL},
00998     {"xpcom::id",            TclXPCOM_IdObjCmd,            (ClientData)NULL}
00999 };
01000 
01001 
01009 void
01010 TclXPCOM_RegisterCommands(
01011     Tcl_Interp *interp 
01012 )
01013 {
01014     int i;
01015 
01016     for (i = 0; i < NUM_COMMANDS; i++)
01017     {
01018         Tcl_CreateObjCommand(interp, cmdtable[i].command, cmdtable[i].cmdproc, cmdtable[i].clientdata, NULL);
01019     }
01020 }

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