PR_FALSE,
nsIXPConnect:: FLAG_SYSTEM_GLOBAL_OBJECT,
main(int argc, char **argv, char **envp){    JSRuntime *rt;    JSContext *cx;    JSObject *glob, *envobj;    int result;    nsresult rv;#if defined(XP_MAC)    initConsole("\pXPConnect Shell", "Welcome to the XPConnect Shell.\n", &argc, &argv);#endif    gErrFile = stderr;    gOutFile = stdout;    {        nsCOMPtr<nsIServiceManager> servMan;        rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);        if (NS_FAILED(rv)) {            printf("NS_InitXPCOM failed!\n");            return 1;        }        {            nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);            NS_ASSERTION(registrar, "Null nsIComponentRegistrar");            if (registrar)                registrar->AutoRegister(nsnull);        }        nsCOMPtr<nsIJSRuntimeService> rtsvc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1");        // get the JSRuntime from the runtime svc        if (!rtsvc) {            printf("failed to get nsJSRuntimeService!\n");            return 1;        }            if (NS_FAILED(rtsvc->GetRuntime(&rt)) || !rt) {            printf("failed to get JSRuntime from nsJSRuntimeService!\n");            return 1;        }        cx = JS_NewContext(rt, 8192);        if (!cx) {            printf("JS_NewContext failed!\n");            return 1;        }        JS_SetErrorReporter(cx, my_ErrorReporter);        nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());        if (!xpc) {            printf("failed to get nsXPConnect service!\n");            return 1;        }        // Since the caps security system might set a default security manager        // we will be sure that the secman on this context gives full trust.        // That way we can avoid getting principals from the caps security manager        // just to shut it up. Also, note that even though our secman will allow        // anything, we set the flags to '0' so it ought never get called anyway.        nsCOMPtr<nsIXPCSecurityManager> secman =            NS_STATIC_CAST(nsIXPCSecurityManager*, new FullTrustSecMan());        xpc->SetSecurityManagerForJSContext(cx, secman, 0);        //    xpc->SetCollectGarbageOnMainThreadOnly(PR_TRUE);        //    xpc->SetDeferReleasesUntilAfterGarbageCollection(PR_TRUE);#ifndef XPCONNECT_STANDALONE        // Fetch the system principal and store it away in a global, to use for        // script compilation in Load() and ProcessFile() (including interactive        // eval loop)        {            nsCOMPtr<nsIPrincipal> princ;            nsCOMPtr<nsIScriptSecurityManager> securityManager =                do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);            if (NS_SUCCEEDED(rv) && securityManager) {                rv = securityManager->GetSystemPrincipal(getter_AddRefs(princ));                if (NS_FAILED(rv)) {                    fprintf(gErrFile, "+++ Failed to obtain SystemPrincipal from ScriptSecurityManager service.\n");                } else {                    // fetch the JS principals and stick in a global                    rv = princ->GetJSPrincipals(cx, &gJSPrincipals);                    if (NS_FAILED(rv)) {                        fprintf(gErrFile, "+++ Failed to obtain JS principals from SystemPrincipal.\n");                    }                }            } else {                fprintf(gErrFile, "+++ Failed to get ScriptSecurityManager service, running without principals");            }        }#endif#ifdef TEST_TranslateThis        nsCOMPtr<nsIXPCFunctionThisTranslator>            translator(new nsXPCFunctionThisTranslator);        xpc->SetFunctionThisTranslator(NS_GET_IID(nsITestXPCFunctionCallback), translator, nsnull);#endif            nsCOMPtr<nsIJSContextStack> cxstack = do_GetService("@mozilla.org/js/xpc/ContextStack;1");        if (!cxstack) {            printf("failed to get the nsThreadJSContextStack service!\n");            return 1;        }        if(NS_FAILED(cxstack->Push(cx))) {            printf("failed to push the current JSContext on the nsThreadJSContextStack!\n");            return 1;        }        nsCOMPtr<nsIXPCScriptable> backstagePass;        nsresult rv = rtsvc->GetBackstagePass(getter_AddRefs(backstagePass));        if (NS_FAILED(rv)) {            fprintf(gErrFile, "+++ Failed to get backstage pass from rtsvc: %8x\n",                    rv);            return 1;        }        nsCOMPtr<nsIXPConnectJSObjectHolder> holder;        rv = xpc->InitClassesWithNewWrappedGlobal(cx, backstagePass,                                                  NS_GET_IID(nsISupports),                                                  PR_FALSE,                                                  getter_AddRefs(holder));        if (NS_FAILED(rv))            return 1;                rv = holder->GetJSObject(&glob);        if (NS_FAILED(rv)) {            NS_ASSERTION(glob == nsnull, "bad GetJSObject?");            return 1;        }        if (!JS_DefineFunctions(cx, glob, glob_functions))            return 1;        envobj = JS_DefineObject(cx, glob, "environment", &env_class, NULL, 0);        if (!envobj || !JS_SetPrivate(cx, envobj, envp))            return 1;        argc--;        argv++;        result = ProcessArgs(cx, glob, argv, argc);//#define TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN 1#ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN        // test of late call and release (see below)        nsCOMPtr<nsIJSContextStack> bogus;        xpc->WrapJS(cx, glob, NS_GET_IID(nsIJSContextStack),                    (void**) getter_AddRefs(bogus));#endif        JS_ClearScope(cx, glob);        JS_GC(cx);        JSContext *oldcx;        cxstack->Pop(&oldcx);        NS_ASSERTION(oldcx == cx, "JS thread context push/pop mismatch");        cxstack = nsnull;        JS_GC(cx);        JS_DestroyContext(cx);        xpc->SyncJSContexts();    } // this scopes the nsCOMPtrs    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM    rv = NS_ShutdownXPCOM( NULL );    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");#ifdef TEST_CALL_ON_WRAPPED_JS_AFTER_SHUTDOWN    // test of late call and release (see above)    JSContext* bogusCX;    bogus->Peek(&bogusCX);    bogus = nsnull;#endif    return result;}