1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3
* ***** BEGIN LICENSE BLOCK *****
4
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
6
* The contents of this file are subject to the Mozilla Public License Version
7
* 1.1 (the "License"); you may not use this file except in compliance with
8
* the License. You may obtain a copy of the License at
9
* http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the
16
* The Original Code is Mozilla Communicator client code, released
19
* The Initial Developer of the Original Code is
20
* Netscape Communications Corporation.
21
* Portions created by the Initial Developer are Copyright (C) 1999
22
* the Initial Developer. All Rights Reserved.
25
* John Bandhauer <jband@netscape.com>
27
* Alternatively, the contents of this file may be used under the terms of
28
* either of the GNU General Public License Version 2 or later (the "GPL"),
29
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30
* in which case the provisions of the GPL or the LGPL are applicable instead
31
* of those above. If you wish to allow use of your version of this file only
32
* under the terms of either the GPL or the LGPL, and not to allow others to
33
* use your version of this file under the terms of the MPL, indicate your
34
* decision by deleting the provisions above and replace them with the notice
35
* and other provisions required by the GPL or the LGPL. If you do not delete
36
* the provisions above, a recipient may use your version of this file under
37
* the terms of any one of the MPL, the GPL or the LGPL.
39
* ***** END LICENSE BLOCK ***** */
41
/* Implements nsXPCToolsCompiler. */
43
#include "xpctools_private.h"
44
#include "nsDirectoryServiceDefs.h"
45
#include "nsDirectoryServiceUtils.h"
47
NS_IMPL_ISUPPORTS1(nsXPCToolsCompiler, nsIXPCToolsCompiler)
49
nsXPCToolsCompiler::nsXPCToolsCompiler()
53
nsXPCToolsCompiler::~nsXPCToolsCompiler()
57
/* readonly attribute nsILocalFile binDir; */
58
NS_IMETHODIMP nsXPCToolsCompiler::GetBinDir(nsILocalFile * *aBinDir)
62
nsCOMPtr<nsIFile> file;
63
nsresult rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, getter_AddRefs(file));
67
nsCOMPtr<nsILocalFile> lfile = do_QueryInterface(file);
68
NS_ADDREF(*aBinDir = lfile);
72
JS_STATIC_DLL_CALLBACK(void) ErrorReporter(JSContext *cx, const char *message,
73
JSErrorReport *report)
75
printf("compile error!\n");
78
static JSClass global_class = {
79
"nsXPCToolsCompiler::global", 0,
80
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
81
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub
84
/* void CompileFile (in nsILocalFile aFile, in PRBool strict); */
85
NS_IMETHODIMP nsXPCToolsCompiler::CompileFile(nsILocalFile *aFile, PRBool strict)
87
// use the xpccallcontext stuff to get the current JSContext
89
// get the xpconnect service
91
nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
93
return NS_ERROR_FAILURE;
95
// get the xpconnect native call context
96
nsCOMPtr<nsIXPCNativeCallContext> callContext;
97
xpc->GetCurrentNativeCallContext(getter_AddRefs(callContext));
99
return NS_ERROR_FAILURE;
101
// verify that we are being called from JS (i.e. the current call is
102
// to this object - though we don't verify that it is to this exact method)
103
nsCOMPtr<nsISupports> callee;
104
callContext->GetCallee(getter_AddRefs(callee));
105
if(!callee || callee.get() != (nsISupports*)this)
106
return NS_ERROR_FAILURE;
108
// Get JSContext of current call
110
rv = callContext->GetJSContext(&cx);
111
if(NS_FAILED(rv) || !cx)
112
return NS_ERROR_FAILURE;
115
if(NS_FAILED(aFile->OpenANSIFileDesc("r", &handle)))
116
return NS_ERROR_FAILURE;
118
JSObject* glob = JS_NewObject(cx, &global_class, NULL, NULL);
120
return NS_ERROR_FAILURE;
121
if (!JS_InitStandardClasses(cx, glob))
122
return NS_ERROR_FAILURE;
125
if(NS_FAILED(aFile->GetNativePath(path)))
126
return NS_ERROR_FAILURE;
128
uint32 oldoptions = JS_GetOptions(cx);
129
JS_SetOptions(cx, JSOPTION_WERROR | (strict ? JSOPTION_STRICT : 0));
130
JSErrorReporter older = JS_SetErrorReporter(cx, ErrorReporter);
131
JSExceptionState *es =JS_SaveExceptionState(cx);
133
if(!JS_CompileFileHandle(cx, glob, path.get(), handle))
136
JSErrorReport* report;
137
if(JS_GetPendingException(cx, &v) &&
138
nsnull != (report = JS_ErrorFromException(cx, v)))
141
const char* msg = "Error";
142
str = JS_ValueToString(cx, v);
144
msg = JS_GetStringBytes(str);
145
printf("%s [%s,%d]\n\n",
148
(int)report->lineno);
152
printf("no script and no error report!\n");
156
JS_RestoreExceptionState(cx, es);
157
JS_SetErrorReporter(cx, older);
158
JS_SetOptions(cx, oldoptions);