Back to index

lightning-sunbird  0.9+nobinonly
nsXPCToolsCompiler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; 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 Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1999
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   John Bandhauer <jband@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 /* Implements nsXPCToolsCompiler. */
00042 
00043 #include "xpctools_private.h"
00044 #include "nsDirectoryServiceDefs.h"
00045 
00046 NS_IMPL_ISUPPORTS1(nsXPCToolsCompiler, nsIXPCToolsCompiler)
00047 
00048 nsXPCToolsCompiler::nsXPCToolsCompiler()
00049 {
00050 }
00051 
00052 nsXPCToolsCompiler::~nsXPCToolsCompiler()
00053 {
00054 }
00055 
00056 /* readonly attribute nsILocalFile binDir; */
00057 NS_IMETHODIMP nsXPCToolsCompiler::GetBinDir(nsILocalFile * *aBinDir)
00058 {
00059     *aBinDir = nsnull;
00060     
00061     nsCOMPtr<nsIFile> file;
00062     nsresult rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, getter_AddRefs(file));
00063     if(NS_FAILED(rv))
00064         return rv;
00065 
00066     nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
00067     NS_ADDREF(*aBinDir = lfile);
00068     return NS_OK;
00069 }
00070 
00071 JS_STATIC_DLL_CALLBACK(void) ErrorReporter(JSContext *cx, const char *message,
00072                           JSErrorReport *report)
00073 {
00074     printf("compile error!\n");
00075 }
00076 
00077 static JSClass global_class = {
00078     "nsXPCToolsCompiler::global", 0,
00079     JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,
00080     JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   JS_FinalizeStub
00081 };
00082 
00083 /* void CompileFile (in nsILocalFile aFile, in PRBool strict); */
00084 NS_IMETHODIMP nsXPCToolsCompiler::CompileFile(nsILocalFile *aFile, PRBool strict)
00085 {
00086     // use the xpccallcontext stuff to get the current JSContext
00087     
00088     // get the xpconnect service
00089     nsresult rv;
00090     nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
00091     if(NS_FAILED(rv))
00092         return NS_ERROR_FAILURE;
00093 
00094     // get the xpconnect native call context
00095     nsCOMPtr<nsIXPCNativeCallContext> callContext;
00096     xpc->GetCurrentNativeCallContext(getter_AddRefs(callContext));
00097     if(!callContext)
00098         return NS_ERROR_FAILURE;
00099 
00100     // verify that we are being called from JS (i.e. the current call is
00101     // to this object - though we don't verify that it is to this exact method)
00102     nsCOMPtr<nsISupports> callee;
00103     callContext->GetCallee(getter_AddRefs(callee));
00104     if(!callee || callee.get() != (nsISupports*)this)
00105         return NS_ERROR_FAILURE;
00106 
00107     // Get JSContext of current call
00108     JSContext* cx;
00109     rv = callContext->GetJSContext(&cx);
00110     if(NS_FAILED(rv) || !cx)
00111         return NS_ERROR_FAILURE;
00112 
00113     FILE* handle;
00114     if(NS_FAILED(aFile->OpenANSIFileDesc("r", &handle)))
00115         return NS_ERROR_FAILURE;
00116 
00117     JSObject* glob = JS_NewObject(cx, &global_class, NULL, NULL);
00118     if (!glob)
00119         return NS_ERROR_FAILURE;
00120     if (!JS_InitStandardClasses(cx, glob))
00121         return NS_ERROR_FAILURE;
00122 
00123     nsCAutoString path;
00124     if(NS_FAILED(aFile->GetNativePath(path)))
00125         return NS_ERROR_FAILURE;
00126 
00127     uint32 oldoptions = JS_GetOptions(cx);
00128     JS_SetOptions(cx, JSOPTION_WERROR | (strict ? JSOPTION_STRICT : 0));
00129     JSErrorReporter older = JS_SetErrorReporter(cx, ErrorReporter);
00130     JSExceptionState *es =JS_SaveExceptionState(cx);
00131 
00132     if(!JS_CompileFileHandle(cx, glob, path.get(), handle))
00133     {
00134         jsval v;
00135         JSErrorReport* report;
00136         if(JS_GetPendingException(cx, &v) &&
00137            nsnull != (report = JS_ErrorFromException(cx, v)))
00138         {
00139             JSString* str;
00140             const char* msg = "Error";
00141             str = JS_ValueToString(cx, v);
00142             if(str)
00143                 msg = JS_GetStringBytes(str);
00144             printf("%s [%s,%d]\n\n",
00145                     msg,
00146                     report->filename, 
00147                     (int)report->lineno);            
00148         }
00149         else
00150         {
00151             printf("no script and no error report!\n");
00152         }
00153         
00154     }    
00155     JS_RestoreExceptionState(cx, es);
00156     JS_SetErrorReporter(cx, older);
00157     JS_SetOptions(cx, oldoptions);
00158         
00159     return NS_OK;
00160 }