2011-12-18 14:09:16 +04:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2003-11-15 03:11:16 +03:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
1999-09-07 10:18:08 +04:00
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
1999-09-07 10:18:08 +04:00
|
|
|
* for the specific language governing rights and limitations under the
|
2003-11-15 03:11:16 +03:00
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
1999-09-07 10:18:08 +04:00
|
|
|
*
|
2003-11-15 03:11:16 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-05-03 02:44:23 +04:00
|
|
|
*
|
2000-08-16 08:01:02 +04:00
|
|
|
* Contributors:
|
|
|
|
* Mike Shaver <shaver@zeroknowledge.com>
|
|
|
|
* John Bandhauer <jband@netscape.com>
|
2001-03-21 04:35:39 +03:00
|
|
|
* IBM Corp.
|
2001-03-14 05:41:55 +03:00
|
|
|
* Robert Ginda <rginda@netscape.com>
|
2003-11-15 03:11:16 +03:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2011-12-16 23:42:07 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2000-05-15 22:07:36 +04:00
|
|
|
#include "prlog.h"
|
2011-11-10 04:27:08 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
2000-05-26 08:56:23 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2000-05-26 08:56:23 +04:00
|
|
|
#include "nsICategoryManager.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "nsIComponentManager.h"
|
2010-06-10 22:11:11 +04:00
|
|
|
#include "mozilla/Module.h"
|
2000-05-26 08:56:23 +04:00
|
|
|
#include "nsILocalFile.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "mozJSComponentLoader.h"
|
2011-07-10 07:21:16 +04:00
|
|
|
#include "mozJSLoaderUtils.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "nsIJSRuntimeService.h"
|
1999-11-10 03:34:26 +03:00
|
|
|
#include "nsIJSContextStack.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsCRT.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
2000-05-16 09:09:31 +04:00
|
|
|
#include "nsIObserverService.h"
|
2000-08-16 08:01:02 +04:00
|
|
|
#include "nsIXPCScriptable.h"
|
2003-08-02 23:33:52 +04:00
|
|
|
#include "nsString.h"
|
2000-05-26 08:56:23 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2005-10-20 20:39:24 +04:00
|
|
|
#include "nsIURI.h"
|
2007-06-11 01:13:18 +04:00
|
|
|
#include "nsIFileURL.h"
|
2010-06-15 23:38:46 +04:00
|
|
|
#include "nsIJARURI.h"
|
2002-01-08 04:31:25 +03:00
|
|
|
#include "nsNetUtil.h"
|
2011-07-06 20:34:26 +04:00
|
|
|
#include "nsDOMFile.h"
|
2005-10-20 20:39:24 +04:00
|
|
|
#include "jsxdrapi.h"
|
2007-05-16 03:27:40 +04:00
|
|
|
#include "jsprf.h"
|
2012-03-09 13:48:50 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2000-03-31 15:19:51 +04:00
|
|
|
// For reporting errors with the console service
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2010-08-12 23:37:52 +04:00
|
|
|
#include "nsIStorageStream.h"
|
|
|
|
#include "nsIStringStream.h"
|
2006-12-20 23:10:12 +03:00
|
|
|
#include "prmem.h"
|
2010-06-15 23:38:46 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
#include "nsILocalFileWin.h"
|
|
|
|
#endif
|
2010-11-16 04:39:28 +03:00
|
|
|
#include "xpcprivate.h"
|
2011-12-28 12:13:38 +04:00
|
|
|
#include "xpcpublic.h"
|
2011-02-25 14:53:36 +03:00
|
|
|
#include "nsIResProtocolHandler.h"
|
2000-03-31 15:19:51 +04:00
|
|
|
|
2010-08-12 23:37:52 +04:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
#include "mozilla/scache/StartupCacheUtils.h"
|
2011-01-07 22:04:19 +03:00
|
|
|
#include "mozilla/Omnijar.h"
|
2010-08-12 23:37:52 +04:00
|
|
|
|
2008-01-20 05:31:10 +03:00
|
|
|
#include "jsdbgapi.h"
|
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
|
|
|
|
2011-11-08 21:10:51 +04:00
|
|
|
using namespace mozilla;
|
2011-07-20 11:39:09 +04:00
|
|
|
using namespace mozilla::scache;
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
static const char kJSRuntimeServiceContractID[] = "@mozilla.org/js/xpc/RuntimeService;1";
|
|
|
|
static const char kXPConnectServiceContractID[] = "@mozilla.org/js/xpc/XPConnect;1";
|
|
|
|
static const char kObserverServiceContractID[] = "@mozilla.org/observer-service;1";
|
2011-06-16 08:54:47 +04:00
|
|
|
static const char kJSCachePrefix[] = "jsloader";
|
1999-11-10 03:34:26 +03:00
|
|
|
|
2005-10-04 03:34:58 +04:00
|
|
|
/* Some platforms don't have an implementation of PR_MemMap(). */
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 22:10:24 +03:00
|
|
|
#ifndef XP_OS2
|
2005-10-04 03:34:58 +04:00
|
|
|
#define HAVE_PR_MEMMAP
|
|
|
|
#endif
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
/**
|
|
|
|
* Buffer sizes for serialization and deserialization of scripts.
|
2008-04-02 10:41:23 +04:00
|
|
|
* FIXME: bug #411579 (tune this macro!) Last updated: Jan 2008
|
2005-10-20 20:39:24 +04:00
|
|
|
*/
|
|
|
|
#define XPC_SERIALIZATION_BUFFER_SIZE (64 * 1024)
|
2008-04-02 10:41:23 +04:00
|
|
|
#define XPC_DESERIALIZATION_BUFFER_SIZE (12 * 8192)
|
2005-10-20 20:39:24 +04:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
// NSPR_LOG_MODULES=JSComponentLoader:5
|
|
|
|
static PRLogModuleInfo *gJSCLLog;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LOG(args) PR_LOG(gJSCLLog, PR_LOG_DEBUG, args)
|
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
// Components.utils.import error messages
|
|
|
|
#define ERROR_SCOPE_OBJ "%s - Second argument must be an object."
|
|
|
|
#define ERROR_NOT_PRESENT "%s - EXPORTED_SYMBOLS is not present."
|
|
|
|
#define ERROR_NOT_AN_ARRAY "%s - EXPORTED_SYMBOLS is not an array."
|
|
|
|
#define ERROR_GETTING_ARRAY_LENGTH "%s - Error getting array length of EXPORTED_SYMBOLS."
|
|
|
|
#define ERROR_ARRAY_ELEMENT "%s - EXPORTED_SYMBOLS[%d] is not a string."
|
|
|
|
#define ERROR_GETTING_SYMBOL "%s - Could not get symbol '%s'."
|
|
|
|
#define ERROR_SETTING_SYMBOL "%s - Could not set symbol '%s' on target object."
|
|
|
|
|
2008-09-07 02:21:43 +04:00
|
|
|
void
|
2006-06-10 04:47:40 +04:00
|
|
|
mozJSLoaderErrorReporter(JSContext *cx, const char *message, JSErrorReport *rep)
|
2001-03-27 09:35:52 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
/* Use the console service to register the error. */
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
2005-10-20 20:39:24 +04:00
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
2001-03-27 09:35:52 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make an nsIScriptError, populate it with information from this
|
|
|
|
* error, then log it with the console service. The UI can then
|
2006-05-26 01:22:32 +04:00
|
|
|
* poll the service to update the Error console.
|
2001-03-27 09:35:52 +04:00
|
|
|
*/
|
2011-10-14 21:52:47 +04:00
|
|
|
nsCOMPtr<nsIScriptError> errorObject =
|
2005-10-20 20:39:24 +04:00
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2001-03-27 09:35:52 +04:00
|
|
|
if (consoleService && errorObject) {
|
|
|
|
/*
|
|
|
|
* Got an error object; prepare appropriate-width versions of
|
|
|
|
* various arguments to it.
|
|
|
|
*/
|
|
|
|
nsAutoString fileUni;
|
|
|
|
fileUni.AssignWithConversion(rep->filename);
|
|
|
|
|
|
|
|
PRUint32 column = rep->uctokenptr - rep->uclinebuf;
|
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
rv = errorObject->Init(reinterpret_cast<const PRUnichar*>
|
2011-10-14 21:52:49 +04:00
|
|
|
(rep->ucmessage),
|
2001-03-27 09:35:52 +04:00
|
|
|
fileUni.get(),
|
2007-07-08 11:08:04 +04:00
|
|
|
reinterpret_cast<const PRUnichar*>
|
2011-10-14 21:52:49 +04:00
|
|
|
(rep->uclinebuf),
|
2001-03-27 09:35:52 +04:00
|
|
|
rep->lineno, column, rep->flags,
|
|
|
|
"component javascript");
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = consoleService->LogMessage(errorObject);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// We're done! Skip return to fall thru to stderr
|
|
|
|
// printout, for the benefit of those invoking the
|
|
|
|
// browser with -console
|
|
|
|
// return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If any of the above fails for some reason, fall back to
|
|
|
|
* printing to stderr.
|
|
|
|
*/
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#ifdef DEBUG
|
2001-03-27 09:35:52 +04:00
|
|
|
fprintf(stderr, "JS Component Loader: %s %s:%d\n"
|
|
|
|
" %s\n",
|
|
|
|
JSREPORT_IS_WARNING(rep->flags) ? "WARNING" : "ERROR",
|
|
|
|
rep->filename, rep->lineno,
|
|
|
|
message ? message : "<no message>");
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#endif
|
2001-03-27 09:35:52 +04:00
|
|
|
}
|
|
|
|
|
2008-09-07 02:21:43 +04:00
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Dump(JSContext *cx, unsigned argc, jsval *vp)
|
1999-09-07 10:18:08 +04:00
|
|
|
{
|
|
|
|
JSString *str;
|
|
|
|
if (!argc)
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2010-03-24 00:08:18 +03:00
|
|
|
|
2010-08-16 23:35:04 +04:00
|
|
|
str = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
|
1999-09-07 10:18:08 +04:00
|
|
|
if (!str)
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2010-12-03 11:24:17 +03:00
|
|
|
size_t length;
|
|
|
|
const jschar *chars = JS_GetStringCharsAndLength(cx, str, &length);
|
|
|
|
if (!chars)
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2010-12-03 11:24:17 +03:00
|
|
|
|
2011-11-10 04:27:08 +04:00
|
|
|
NS_ConvertUTF16toUTF8 utf8str(reinterpret_cast<const PRUnichar*>(chars));
|
|
|
|
#ifdef ANDROID
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", utf8str.get());
|
|
|
|
#endif
|
|
|
|
fputs(utf8str.get(), stdout);
|
2011-10-19 18:01:24 +04:00
|
|
|
fflush(stdout);
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2008-09-07 02:21:43 +04:00
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Debug(JSContext *cx, unsigned argc, jsval *vp)
|
2000-05-26 08:56:23 +04:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-08-16 23:35:04 +04:00
|
|
|
return Dump(cx, argc, vp);
|
2000-05-26 08:56:23 +04:00
|
|
|
#else
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2000-05-26 08:56:23 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-09-07 02:21:43 +04:00
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Atob(JSContext *cx, unsigned argc, jsval *vp)
|
2006-12-20 23:10:12 +03:00
|
|
|
{
|
|
|
|
if (!argc)
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2006-12-20 23:10:12 +03:00
|
|
|
|
2011-12-28 12:13:38 +04:00
|
|
|
return xpc::Base64Decode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
|
2006-12-20 23:10:12 +03:00
|
|
|
}
|
|
|
|
|
2008-09-07 02:21:43 +04:00
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
Btoa(JSContext *cx, unsigned argc, jsval *vp)
|
2006-12-20 23:10:12 +03:00
|
|
|
{
|
|
|
|
if (!argc)
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2006-12-20 23:10:12 +03:00
|
|
|
|
2011-12-28 12:13:38 +04:00
|
|
|
return xpc::Base64Encode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
|
2006-12-20 23:10:12 +03:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:34:26 +04:00
|
|
|
static JSBool
|
2012-02-29 03:11:11 +04:00
|
|
|
File(JSContext *cx, unsigned argc, jsval *vp)
|
2011-07-06 20:34:26 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!argc) {
|
|
|
|
XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-07-06 20:34:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> native;
|
2011-07-12 06:41:29 +04:00
|
|
|
rv = nsDOMFileFile::NewFile(getter_AddRefs(native));
|
2011-07-06 20:34:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-07-06 20:34:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSNativeInitializer> initializer = do_QueryInterface(native);
|
|
|
|
NS_ASSERTION(initializer, "what?");
|
|
|
|
|
|
|
|
rv = initializer->Initialize(nsnull, cx, nsnull, argc, JS_ARGV(cx, vp));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-07-06 20:34:26 +04:00
|
|
|
}
|
|
|
|
|
2011-07-06 21:08:14 +04:00
|
|
|
nsXPConnect* xpc = nsXPConnect::GetXPConnect();
|
2011-07-06 20:34:26 +04:00
|
|
|
if (!xpc) {
|
|
|
|
XPCThrower::Throw(NS_ERROR_UNEXPECTED, cx);
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-07-06 20:34:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* glob = JS_GetGlobalForScopeChain(cx);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
|
|
|
jsval retval;
|
|
|
|
rv = xpc->WrapNativeToJSVal(cx, glob, native, nsnull,
|
|
|
|
&NS_GET_IID(nsISupports),
|
2011-10-17 18:59:28 +04:00
|
|
|
true, &retval, nsnull);
|
2011-07-06 20:34:26 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
XPCThrower::Throw(rv, cx);
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2011-07-06 20:34:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_SET_RVAL(cx, vp, retval);
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2011-07-06 20:34:26 +04:00
|
|
|
}
|
|
|
|
|
1999-09-07 10:18:08 +04:00
|
|
|
static JSFunctionSpec gGlobalFun[] = {
|
2010-08-16 23:35:04 +04:00
|
|
|
{"dump", Dump, 1,0},
|
|
|
|
{"debug", Debug, 1,0},
|
|
|
|
{"atob", Atob, 1,0},
|
|
|
|
{"btoa", Btoa, 1,0},
|
2011-07-06 20:34:26 +04:00
|
|
|
{"File", File, 1,JSFUN_CONSTRUCTOR},
|
2010-08-16 23:35:04 +04:00
|
|
|
{nsnull,nsnull,0,0}
|
1999-09-07 10:18:08 +04:00
|
|
|
};
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
class JSCLContextHelper
|
|
|
|
{
|
|
|
|
public:
|
2008-07-22 03:56:45 +04:00
|
|
|
JSCLContextHelper(mozJSComponentLoader* loader);
|
2011-05-13 19:56:26 +04:00
|
|
|
~JSCLContextHelper();
|
2009-02-18 07:11:09 +03:00
|
|
|
|
2011-05-13 19:56:26 +04:00
|
|
|
void reportErrorAfterPop(char *buf);
|
2005-10-20 20:39:24 +04:00
|
|
|
|
|
|
|
operator JSContext*() const {return mContext;}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSContext* mContext;
|
2008-07-22 03:56:45 +04:00
|
|
|
nsIThreadJSContextStack* mContextStack;
|
2011-05-13 19:56:26 +04:00
|
|
|
char* mBuf;
|
2010-03-12 09:50:10 +03:00
|
|
|
|
|
|
|
// prevent copying and assignment
|
2011-12-16 23:42:07 +04:00
|
|
|
JSCLContextHelper(const JSCLContextHelper &) MOZ_DELETE;
|
|
|
|
const JSCLContextHelper& operator=(const JSCLContextHelper &) MOZ_DELETE;
|
2005-10-20 20:39:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class JSCLAutoErrorReporterSetter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JSCLAutoErrorReporterSetter(JSContext* cx, JSErrorReporter reporter)
|
|
|
|
{mContext = cx; mOldReporter = JS_SetErrorReporter(cx, reporter);}
|
|
|
|
~JSCLAutoErrorReporterSetter()
|
2011-10-14 21:52:47 +04:00
|
|
|
{JS_SetErrorReporter(mContext, mOldReporter);}
|
2005-10-20 20:39:24 +04:00
|
|
|
private:
|
|
|
|
JSContext* mContext;
|
|
|
|
JSErrorReporter mOldReporter;
|
2011-12-16 23:42:07 +04:00
|
|
|
|
|
|
|
JSCLAutoErrorReporterSetter(const JSCLAutoErrorReporterSetter &) MOZ_DELETE;
|
|
|
|
const JSCLAutoErrorReporterSetter& operator=(const JSCLAutoErrorReporterSetter &) MOZ_DELETE;
|
2005-10-20 20:39:24 +04:00
|
|
|
};
|
|
|
|
|
2009-02-18 07:11:09 +03:00
|
|
|
static nsresult
|
2011-12-18 14:09:16 +04:00
|
|
|
ReportOnCaller(JSContext *callerContext,
|
2009-02-18 07:11:09 +03:00
|
|
|
const char *format, ...) {
|
2011-12-18 14:09:16 +04:00
|
|
|
if (!callerContext) {
|
2009-02-18 07:11:09 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2009-02-18 07:11:09 +03:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2011-05-13 19:56:26 +04:00
|
|
|
char *buf = JS_vsmprintf(format, ap);
|
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_ReportError(callerContext, buf);
|
|
|
|
JS_smprintf_free(buf);
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ReportOnCaller(JSCLContextHelper &helper,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
|
2011-05-13 19:56:26 +04:00
|
|
|
char *buf = JS_vsmprintf(format, ap);
|
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
|
|
|
|
2011-05-13 19:56:26 +04:00
|
|
|
helper.reportErrorAfterPop(buf);
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
|
|
|
|
1999-09-26 00:07:20 +04:00
|
|
|
mozJSComponentLoader::mozJSComponentLoader()
|
2002-06-15 01:33:05 +04:00
|
|
|
: mRuntime(nsnull),
|
2005-06-28 19:21:31 +04:00
|
|
|
mContext(nsnull),
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized(false)
|
1999-09-07 10:18:08 +04:00
|
|
|
{
|
2005-12-13 20:55:43 +03:00
|
|
|
NS_ASSERTION(!sSelf, "mozJSComponentLoader should be a singleton");
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gJSCLLog) {
|
|
|
|
gJSCLLog = PR_NewLogModule("JSComponentLoader");
|
|
|
|
}
|
|
|
|
#endif
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
sSelf = this;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
mozJSComponentLoader::~mozJSComponentLoader()
|
1999-09-07 10:18:08 +04:00
|
|
|
{
|
2005-12-13 20:55:43 +03:00
|
|
|
if (mInitialized) {
|
|
|
|
NS_ERROR("'xpcom-shutdown-loaders' was not fired before cleaning up mozJSComponentLoader");
|
|
|
|
UnloadModules();
|
1999-11-10 03:34:26 +03:00
|
|
|
}
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
sSelf = nsnull;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
mozJSComponentLoader*
|
|
|
|
mozJSComponentLoader::sSelf;
|
1999-09-08 00:30:25 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
NS_IMPL_ISUPPORTS3(mozJSComponentLoader,
|
2010-06-10 22:11:11 +04:00
|
|
|
mozilla::ModuleLoader,
|
2007-05-16 03:27:40 +04:00
|
|
|
xpcIJSModuleLoader,
|
|
|
|
nsIObserver)
|
2011-10-14 21:52:47 +04:00
|
|
|
|
1999-09-24 06:52:46 +04:00
|
|
|
nsresult
|
1999-09-24 05:37:44 +04:00
|
|
|
mozJSComponentLoader::ReallyInit()
|
|
|
|
{
|
2010-05-20 03:22:19 +04:00
|
|
|
NS_TIME_FUNCTION;
|
1999-09-24 05:37:44 +04:00
|
|
|
nsresult rv;
|
1999-11-10 03:34:26 +03:00
|
|
|
|
2010-08-12 23:37:52 +04:00
|
|
|
|
1999-11-10 03:34:26 +03:00
|
|
|
/*
|
|
|
|
* Get the JSRuntime from the runtime svc, if possible.
|
|
|
|
* We keep a reference around, because it's a Bad Thing if the runtime
|
|
|
|
* service gets shut down before we're done. Bad!
|
|
|
|
*/
|
2000-09-21 08:30:32 +04:00
|
|
|
|
2000-09-14 03:57:52 +04:00
|
|
|
mRuntimeService = do_GetService(kJSRuntimeServiceContractID, &rv);
|
1999-11-18 18:39:28 +03:00
|
|
|
if (NS_FAILED(rv) ||
|
|
|
|
NS_FAILED(rv = mRuntimeService->GetRuntime(&mRuntime)))
|
1999-09-08 00:30:25 +04:00
|
|
|
return rv;
|
1999-11-10 03:34:26 +03:00
|
|
|
|
2008-07-22 03:56:45 +04:00
|
|
|
mContextStack = do_GetService("@mozilla.org/js/xpc/ContextStack;1", &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2005-06-28 19:21:31 +04:00
|
|
|
// Create our compilation context.
|
|
|
|
mContext = JS_NewContext(mRuntime, 256);
|
|
|
|
if (!mContext)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-01-02 22:05:19 +04:00
|
|
|
uint32_t options = JS_GetOptions(mContext);
|
2005-06-28 19:21:31 +04:00
|
|
|
JS_SetOptions(mContext, options | JSOPTION_XML);
|
|
|
|
|
2008-03-25 17:08:48 +03:00
|
|
|
// Always use the latest js version
|
|
|
|
JS_SetVersion(mContext, JSVERSION_LATEST);
|
|
|
|
|
2011-10-14 21:52:47 +04:00
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secman =
|
2005-10-20 20:39:24 +04:00
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID);
|
2000-09-21 08:30:32 +04:00
|
|
|
if (!secman)
|
2000-08-16 08:01:02 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = secman->GetSystemPrincipal(getter_AddRefs(mSystemPrincipal));
|
|
|
|
if (NS_FAILED(rv) || !mSystemPrincipal)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
if (!mModules.Init(32))
|
1999-09-07 10:18:08 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-05-16 03:27:40 +04:00
|
|
|
if (!mImports.Init(32))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-06-20 10:29:49 +04:00
|
|
|
if (!mInProgressImports.Init(32))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obsSvc =
|
|
|
|
do_GetService(kObserverServiceContractID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = obsSvc->AddObserver(this, "xpcom-shutdown-loaders", false);
|
2005-12-13 20:55:43 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-01-15 01:03:21 +03:00
|
|
|
// Set up localized comparison and string conversion
|
|
|
|
xpc_LocalizeContext(mContext);
|
|
|
|
|
1999-11-10 03:34:26 +03:00
|
|
|
#ifdef DEBUG_shaver_off
|
2000-10-05 08:15:43 +04:00
|
|
|
fprintf(stderr, "mJCL: ReallyInit success!\n");
|
1999-09-07 10:18:08 +04:00
|
|
|
#endif
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = true;
|
1999-11-10 03:34:26 +03:00
|
|
|
|
1999-09-07 10:18:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
const mozilla::Module*
|
2011-11-08 21:10:51 +04:00
|
|
|
mozJSComponentLoader::LoadModule(FileLocation &aFile)
|
2010-06-15 23:38:46 +04:00
|
|
|
{
|
2011-11-08 21:10:51 +04:00
|
|
|
nsCOMPtr<nsILocalFile> file = aFile.GetBaseFile();
|
2010-06-15 23:38:46 +04:00
|
|
|
|
2011-11-08 21:10:51 +04:00
|
|
|
nsCString spec;
|
|
|
|
aFile.GetURIString(spec);
|
2010-06-15 23:38:46 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2011-11-08 21:10:51 +04:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), spec);
|
2010-06-21 19:41:42 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NULL;
|
2010-06-15 23:38:46 +04:00
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
#ifdef NS_FUNCTION_TIMER
|
|
|
|
NS_TIME_FUNCTION_FMT("%s (line %d) (file: %s)", MOZ_FUNCTION_NAME,
|
2011-11-08 21:10:51 +04:00
|
|
|
__LINE__, spec.get());
|
2010-05-20 03:22:19 +04:00
|
|
|
#endif
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
if (!mInitialized) {
|
|
|
|
rv = ReallyInit();
|
2005-12-03 18:27:17 +03:00
|
|
|
if (NS_FAILED(rv))
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
1999-09-27 06:22:25 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
ModuleEntry* mod;
|
2011-11-08 21:10:51 +04:00
|
|
|
if (mModules.Get(spec, &mod))
|
2010-06-21 19:41:42 +04:00
|
|
|
return mod;
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
nsAutoPtr<ModuleEntry> entry(new ModuleEntry);
|
|
|
|
if (!entry)
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2011-11-08 21:10:51 +04:00
|
|
|
rv = GlobalForLocation(file, uri, &entry->global,
|
2010-06-15 23:38:46 +04:00
|
|
|
&entry->location, nsnull);
|
2005-10-20 20:39:24 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
1999-09-07 10:18:08 +04:00
|
|
|
#ifdef DEBUG_shaver
|
1999-09-08 00:30:25 +04:00
|
|
|
fprintf(stderr, "GlobalForLocation failed!\n");
|
1999-09-07 10:18:08 +04:00
|
|
|
#endif
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc = do_GetService(kXPConnectServiceContractID,
|
|
|
|
&rv);
|
2005-12-13 20:55:43 +03:00
|
|
|
if (NS_FAILED(rv))
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2005-12-13 20:55:43 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIComponentManager> cm;
|
|
|
|
rv = NS_GetComponentManager(getter_AddRefs(cm));
|
|
|
|
if (NS_FAILED(rv))
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2000-08-24 11:50:27 +04:00
|
|
|
|
2008-07-22 03:56:45 +04:00
|
|
|
JSCLContextHelper cx(this);
|
2010-09-28 02:55:15 +04:00
|
|
|
JSAutoEnterCompartment ac;
|
2010-09-02 09:20:32 +04:00
|
|
|
if (!ac.enter(cx, entry->global))
|
|
|
|
return NULL;
|
1999-11-18 18:39:28 +03:00
|
|
|
|
2000-10-05 08:15:43 +04:00
|
|
|
JSObject* cm_jsobj;
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> cm_holder;
|
2011-10-14 21:52:47 +04:00
|
|
|
rv = xpc->WrapNative(cx, entry->global, cm,
|
2000-10-05 08:15:43 +04:00
|
|
|
NS_GET_IID(nsIComponentManager),
|
|
|
|
getter_AddRefs(cm_holder));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
#ifdef DEBUG_shaver
|
|
|
|
fprintf(stderr, "WrapNative(%p,%p,nsIComponentManager) failed: %x\n",
|
2001-03-27 09:35:52 +04:00
|
|
|
(void *)(JSContext*)cx, (void *)mCompMgr, rv);
|
2000-10-05 08:15:43 +04:00
|
|
|
#endif
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2000-10-05 08:15:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = cm_holder->GetJSObject(&cm_jsobj);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
#ifdef DEBUG_shaver
|
|
|
|
fprintf(stderr, "GetJSObject of ComponentManager failed\n");
|
|
|
|
#endif
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2005-12-13 20:55:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* file_jsobj;
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> file_holder;
|
2011-11-08 21:10:51 +04:00
|
|
|
rv = xpc->WrapNative(cx, entry->global, file,
|
2005-12-13 20:55:43 +03:00
|
|
|
NS_GET_IID(nsIFile),
|
|
|
|
getter_AddRefs(file_holder));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2005-12-13 20:55:43 +03:00
|
|
|
}
|
|
|
|
|
2006-11-22 21:01:37 +03:00
|
|
|
rv = file_holder->GetJSObject(&file_jsobj);
|
2005-12-13 20:55:43 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2000-10-05 08:15:43 +04:00
|
|
|
}
|
|
|
|
|
2006-06-10 04:47:40 +04:00
|
|
|
JSCLAutoErrorReporterSetter aers(cx, mozJSLoaderErrorReporter);
|
1999-11-18 18:39:28 +03:00
|
|
|
|
2010-06-12 00:13:26 +04:00
|
|
|
jsval NSGetFactory_val;
|
2004-10-12 07:37:20 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
if (!JS_GetProperty(cx, entry->global, "NSGetFactory", &NSGetFactory_val) ||
|
|
|
|
JSVAL_IS_VOID(NSGetFactory_val)) {
|
|
|
|
return NULL;
|
2004-10-12 07:37:20 +04:00
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
if (JS_TypeOfValue(cx, NSGetFactory_val) != JSTYPE_FUNCTION) {
|
2010-06-21 19:41:42 +04:00
|
|
|
nsCAutoString spec;
|
2011-11-08 21:10:51 +04:00
|
|
|
uri->GetSpec(spec);
|
2010-06-10 22:11:11 +04:00
|
|
|
JS_ReportError(cx, "%s has NSGetFactory property that is not a function",
|
2010-06-21 19:41:42 +04:00
|
|
|
spec.get());
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2004-10-12 07:37:20 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
JSObject *jsGetFactoryObj;
|
2010-06-12 00:13:26 +04:00
|
|
|
if (!JS_ValueToObject(cx, NSGetFactory_val, &jsGetFactoryObj) ||
|
2010-06-10 22:11:11 +04:00
|
|
|
!jsGetFactoryObj) {
|
1999-09-08 00:30:25 +04:00
|
|
|
/* XXX report error properly */
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
rv = xpc->WrapJS(cx, jsGetFactoryObj,
|
2010-06-12 00:13:26 +04:00
|
|
|
NS_GET_IID(xpcIJSGetFactory), getter_AddRefs(entry->getfactoryobj));
|
2005-10-20 20:39:24 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
1999-09-08 00:30:25 +04:00
|
|
|
/* XXX report error properly */
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#ifdef DEBUG
|
1999-09-07 10:18:08 +04:00
|
|
|
fprintf(stderr, "mJCL: couldn't get nsIModule from jsval\n");
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
#endif
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
// Cache this module for later
|
2011-11-08 21:10:51 +04:00
|
|
|
if (!mModules.Put(spec, entry))
|
2010-06-10 22:11:11 +04:00
|
|
|
return NULL;
|
2000-09-21 08:30:32 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
// The hash owns the ModuleEntry now, forget about it
|
2010-06-12 00:13:26 +04:00
|
|
|
return entry.forget();
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
// Some stack based classes for cleaning up on early return
|
|
|
|
#ifdef HAVE_PR_MEMMAP
|
|
|
|
class FileAutoCloser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FileAutoCloser(PRFileDesc *file) : mFile(file) {}
|
|
|
|
~FileAutoCloser() { PR_Close(mFile); }
|
|
|
|
private:
|
|
|
|
PRFileDesc *mFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileMapAutoCloser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit FileMapAutoCloser(PRFileMap *map) : mMap(map) {}
|
|
|
|
~FileMapAutoCloser() { PR_CloseFileMap(mMap); }
|
|
|
|
private:
|
|
|
|
PRFileMap *mMap;
|
|
|
|
};
|
2011-12-16 23:08:59 +04:00
|
|
|
#else
|
|
|
|
class ANSIFileAutoCloser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ANSIFileAutoCloser(FILE *file) : mFile(file) {}
|
|
|
|
~ANSIFileAutoCloser() { fclose(mFile); }
|
|
|
|
private:
|
|
|
|
FILE *mFile;
|
|
|
|
};
|
2005-10-20 20:39:24 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult
|
2010-06-15 23:38:46 +04:00
|
|
|
mozJSComponentLoader::GlobalForLocation(nsILocalFile *aComponentFile,
|
|
|
|
nsIURI *aURI,
|
2005-12-13 20:55:43 +03:00
|
|
|
JSObject **aGlobal,
|
2008-08-04 21:03:34 +04:00
|
|
|
char **aLocation,
|
|
|
|
jsval *exception)
|
1999-09-07 10:18:08 +04:00
|
|
|
{
|
2005-10-20 20:39:24 +04:00
|
|
|
nsresult rv;
|
2000-02-16 09:26:21 +03:00
|
|
|
|
2008-07-22 03:56:45 +04:00
|
|
|
JSCLContextHelper cx(this);
|
2010-09-28 02:55:15 +04:00
|
|
|
|
2011-07-02 01:11:31 +04:00
|
|
|
JS_AbortIfWrongThread(JS_GetRuntime(cx));
|
|
|
|
|
2010-09-02 09:20:32 +04:00
|
|
|
// preserve caller's compartment
|
2011-10-04 18:06:54 +04:00
|
|
|
js::AutoPreserveCompartment pc(cx);
|
2011-08-07 01:05:25 +04:00
|
|
|
|
2004-12-07 21:13:02 +03:00
|
|
|
nsCOMPtr<nsIXPCScriptable> backstagePass;
|
|
|
|
rv = mRuntimeService->GetBackstagePass(getter_AddRefs(backstagePass));
|
2005-10-20 20:39:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-12-07 21:13:02 +03:00
|
|
|
|
2006-06-10 04:47:40 +04:00
|
|
|
JSCLAutoErrorReporterSetter aers(cx, mozJSLoaderErrorReporter);
|
2003-03-23 10:22:18 +03:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
nsCOMPtr<nsIXPConnect> xpc =
|
|
|
|
do_GetService(kXPConnectServiceContractID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-09-21 08:30:32 +04:00
|
|
|
|
2005-06-28 19:21:31 +04:00
|
|
|
// Make sure InitClassesWithNewWrappedGlobal() installs the
|
|
|
|
// backstage pass as the global in our compilation context.
|
|
|
|
JS_SetGlobalObject(cx, nsnull);
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
|
2000-10-05 08:15:43 +04:00
|
|
|
rv = xpc->InitClassesWithNewWrappedGlobal(cx, backstagePass,
|
2010-07-20 00:36:49 +04:00
|
|
|
mSystemPrincipal,
|
2005-05-20 07:12:22 +04:00
|
|
|
nsIXPConnect::
|
2011-10-14 21:52:48 +04:00
|
|
|
FLAG_SYSTEM_GLOBAL_OBJECT,
|
2000-10-05 08:15:43 +04:00
|
|
|
getter_AddRefs(holder));
|
2005-10-20 20:39:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-03-23 10:22:18 +03:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
JSObject *global;
|
2000-10-05 08:15:43 +04:00
|
|
|
rv = holder->GetJSObject(&global);
|
2005-10-20 20:39:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
1999-09-08 00:30:25 +04:00
|
|
|
|
2010-09-28 02:55:15 +04:00
|
|
|
JSAutoEnterCompartment ac;
|
2010-08-27 18:49:31 +04:00
|
|
|
if (!ac.enter(cx, global))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-01-15 04:58:40 +03:00
|
|
|
if (!JS_DefineFunctions(cx, global, gGlobalFun) ||
|
|
|
|
!JS_DefineProfilingFunctions(cx, global)) {
|
2005-10-20 20:39:24 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2000-10-05 08:15:43 +04:00
|
|
|
}
|
2000-08-16 08:01:02 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
bool realFile = false;
|
|
|
|
// need to be extra careful checking for URIs pointing to files
|
|
|
|
// EnsureFile may not always get called, especially on resource URIs
|
|
|
|
// so we need to call GetFile to make sure this is a valid file
|
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
|
|
|
|
nsCOMPtr<nsIFile> testFile;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
fileURL->GetFile(getter_AddRefs(testFile));
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
if (testFile) {
|
|
|
|
realFile = true;
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> locationHolder;
|
|
|
|
rv = xpc->WrapNative(cx, global, aComponentFile,
|
|
|
|
NS_GET_IID(nsILocalFile),
|
|
|
|
getter_AddRefs(locationHolder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
JSObject *locationObj;
|
|
|
|
rv = locationHolder->GetJSObject(&locationObj);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!JS_DefineProperty(cx, global, "__LOCATION__",
|
|
|
|
OBJECT_TO_JSVAL(locationObj), nsnull, nsnull, 0))
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString nativePath;
|
2010-06-15 23:38:46 +04:00
|
|
|
rv = aURI->GetSpec(nativePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2011-04-14 02:10:13 +04:00
|
|
|
// Expose the URI from which the script was imported through a special
|
|
|
|
// variable that we insert into the JSM.
|
|
|
|
JSString *exposedUri = JS_NewStringCopyN(cx, nativePath.get(), nativePath.Length());
|
|
|
|
if (!JS_DefineProperty(cx, global, "__URI__",
|
|
|
|
STRING_TO_JSVAL(exposedUri), nsnull, nsnull, 0))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2011-09-02 10:46:00 +04:00
|
|
|
JSScript *script = nsnull;
|
2010-08-12 23:37:52 +04:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
// Before compiling the script, first check to see if we have it in
|
2010-08-12 23:37:52 +04:00
|
|
|
// the startupcache. Note: as a rule, startupcache errors are not fatal
|
2005-10-20 20:39:24 +04:00
|
|
|
// to loading the script, since we can always slow-load.
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool writeToCache = false;
|
2010-08-12 23:37:52 +04:00
|
|
|
StartupCache* cache = StartupCache::GetSingleton();
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2011-07-10 07:21:16 +04:00
|
|
|
nsCAutoString cachePath(kJSCachePrefix);
|
2011-07-20 11:39:09 +04:00
|
|
|
rv = PathifyURI(aURI, cachePath);
|
2011-07-10 07:21:16 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-08-12 23:37:52 +04:00
|
|
|
if (cache) {
|
2011-09-02 10:46:00 +04:00
|
|
|
rv = ReadCachedScript(cache, cachePath, cx, &script);
|
2005-10-20 20:39:24 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-08-12 23:37:52 +04:00
|
|
|
LOG(("Successfully loaded %s from startupcache\n", nativePath.get()));
|
2005-10-20 20:39:24 +04:00
|
|
|
} else {
|
2010-08-12 23:37:52 +04:00
|
|
|
// This is ok, it just means the script is not yet in the
|
|
|
|
// cache. Could mean that the cache was corrupted and got removed,
|
|
|
|
// but either way we're going to write this out.
|
2011-10-17 18:59:28 +04:00
|
|
|
writeToCache = true;
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
|
|
|
}
|
2005-03-24 21:24:09 +03:00
|
|
|
|
2011-09-02 10:46:00 +04:00
|
|
|
if (!script) {
|
2010-08-12 23:37:52 +04:00
|
|
|
// The script wasn't in the cache , so compile it now.
|
2005-10-20 20:39:24 +04:00
|
|
|
LOG(("Slow loading %s\n", nativePath.get()));
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2008-08-05 03:54:16 +04:00
|
|
|
// If |exception| is non-null, then our caller wants us to propagate
|
|
|
|
// any exceptions out to our caller. Ensure that the engine doesn't
|
2008-08-04 21:03:34 +04:00
|
|
|
// eagerly report the exception.
|
2012-01-02 22:05:19 +04:00
|
|
|
uint32_t oldopts = JS_GetOptions(cx);
|
2011-03-26 00:31:14 +03:00
|
|
|
JS_SetOptions(cx, oldopts | JSOPTION_NO_SCRIPT_RVAL |
|
2011-10-14 21:52:48 +04:00
|
|
|
(exception ? JSOPTION_DONT_REPORT_UNCAUGHT : 0));
|
2008-08-04 21:03:34 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
if (realFile) {
|
2005-10-04 03:34:58 +04:00
|
|
|
#ifdef HAVE_PR_MEMMAP
|
2010-06-15 23:38:46 +04:00
|
|
|
PRInt64 fileSize;
|
|
|
|
rv = aComponentFile->GetFileSize(&fileSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return rv;
|
|
|
|
}
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
PRInt64 maxSize;
|
|
|
|
LL_UI2L(maxSize, PR_UINT32_MAX);
|
|
|
|
if (LL_CMP(fileSize, >, maxSize)) {
|
|
|
|
NS_ERROR("file too large");
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
PRFileDesc *fileHandle;
|
|
|
|
rv = aComponentFile->OpenNSPRFileDesc(PR_RDONLY, 0, &fileHandle);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
// Make sure the file is closed, no matter how we return.
|
|
|
|
FileAutoCloser fileCloser(fileHandle);
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
PRFileMap *map = PR_CreateFileMap(fileHandle, fileSize,
|
|
|
|
PR_PROT_READONLY);
|
|
|
|
if (!map) {
|
|
|
|
NS_ERROR("Failed to create file map");
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
// Make sure the file map is closed, no matter how we return.
|
|
|
|
FileMapAutoCloser mapCloser(map);
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
PRUint32 fileSize32;
|
|
|
|
LL_L2UI(fileSize32, fileSize);
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
char *buf = static_cast<char*>(PR_MemMap(map, 0, fileSize32));
|
|
|
|
if (!buf) {
|
|
|
|
NS_WARNING("Failed to map file");
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
script = JS_CompileScriptForPrincipalsVersion(cx, global,
|
|
|
|
nsJSPrincipals::get(mSystemPrincipal),
|
|
|
|
buf, fileSize32, nativePath.get(), 1,
|
2011-10-14 21:52:48 +04:00
|
|
|
JSVERSION_LATEST);
|
2010-11-02 03:33:22 +03:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
PR_MemUnmap(buf, fileSize32);
|
2005-10-04 03:34:58 +04:00
|
|
|
|
|
|
|
#else /* HAVE_PR_MEMMAP */
|
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
/**
|
2011-12-16 23:08:59 +04:00
|
|
|
* No memmap implementation, so fall back to
|
|
|
|
* reading in the file
|
2010-06-15 23:38:46 +04:00
|
|
|
*/
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
FILE *fileHandle;
|
|
|
|
rv = aComponentFile->OpenANSIFileDesc("r", &fileHandle);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2011-12-16 23:08:59 +04:00
|
|
|
// Ensure file fclose
|
|
|
|
ANSIFileAutoCloser fileCloser(fileHandle);
|
|
|
|
|
|
|
|
PRInt64 len;
|
|
|
|
rv = aComponentFile->GetFileSize(&len);
|
|
|
|
if (NS_FAILED(rv) || len < 0) {
|
|
|
|
NS_WARNING("Failed to get file size");
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *buf = (char *) malloc(len * sizeof(char));
|
|
|
|
if (!buf) {
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t rlen = fread(buf, 1, len, fileHandle);
|
|
|
|
if (rlen != (PRUint64)len) {
|
|
|
|
free(buf);
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
NS_WARNING("Failed to read file");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-03-09 13:48:50 +04:00
|
|
|
script = JS_CompileScriptForPrincipalsVersion(cx, global,
|
|
|
|
nsJSPrincipals::get(mSystemPrincipal),
|
|
|
|
buf, rlen, nativePath.get(), 1,
|
2011-12-16 23:08:59 +04:00
|
|
|
JSVERSION_LATEST);
|
|
|
|
|
|
|
|
free(buf);
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2008-08-04 22:12:51 +04:00
|
|
|
#endif /* HAVE_PR_MEMMAP */
|
2010-06-15 23:38:46 +04:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> scriptChannel;
|
|
|
|
rv = ioService->NewChannelFromURI(aURI, getter_AddRefs(scriptChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> scriptStream;
|
|
|
|
rv = scriptChannel->Open(getter_AddRefs(scriptStream));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-08-04 21:03:34 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
PRUint32 len, bytesRead;
|
|
|
|
|
|
|
|
rv = scriptStream->Available(&len);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!len)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
/* malloc an internal buf the size of the file */
|
|
|
|
nsAutoArrayPtr<char> buf(new char[len + 1]);
|
|
|
|
if (!buf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* read the file in one swoop */
|
|
|
|
rv = scriptStream->Read(buf, len, &bytesRead);
|
|
|
|
if (bytesRead != len)
|
|
|
|
return NS_BASE_STREAM_OSERROR;
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2012-03-09 13:48:50 +04:00
|
|
|
script = JS_CompileScriptForPrincipalsVersion(cx, global,
|
|
|
|
nsJSPrincipals::get(mSystemPrincipal),
|
|
|
|
buf, bytesRead, nativePath.get(), 1,
|
2011-10-14 21:52:48 +04:00
|
|
|
JSVERSION_LATEST);
|
2010-06-15 23:38:46 +04:00
|
|
|
}
|
2008-08-04 21:03:34 +04:00
|
|
|
// Propagate the exception, if one exists. Also, don't leave the stale
|
|
|
|
// exception on this context.
|
2011-03-26 00:31:14 +03:00
|
|
|
JS_SetOptions(cx, oldopts);
|
2011-09-02 10:46:00 +04:00
|
|
|
if (!script && exception) {
|
2011-03-26 00:31:14 +03:00
|
|
|
JS_GetPendingException(cx, exception);
|
|
|
|
JS_ClearPendingException(cx);
|
2008-08-04 21:03:34 +04:00
|
|
|
}
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2011-09-02 10:46:00 +04:00
|
|
|
if (!script) {
|
1999-11-10 03:34:26 +03:00
|
|
|
#ifdef DEBUG_shaver_off
|
2005-10-20 20:39:24 +04:00
|
|
|
fprintf(stderr, "mJCL: script compilation of %s FAILED\n",
|
|
|
|
nativePath.get());
|
1999-09-08 00:30:25 +04:00
|
|
|
#endif
|
2005-10-20 20:39:24 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-09-08 00:30:25 +04:00
|
|
|
|
1999-11-10 03:34:26 +03:00
|
|
|
#ifdef DEBUG_shaver_off
|
2005-10-20 20:39:24 +04:00
|
|
|
fprintf(stderr, "mJCL: compiled JS component %s\n",
|
|
|
|
nativePath.get());
|
1999-09-08 00:30:25 +04:00
|
|
|
#endif
|
2005-10-04 03:34:58 +04:00
|
|
|
|
2010-08-12 23:37:52 +04:00
|
|
|
if (writeToCache) {
|
2011-10-14 21:52:47 +04:00
|
|
|
// We successfully compiled the script, so cache it.
|
2011-09-02 10:46:00 +04:00
|
|
|
rv = WriteCachedScript(cache, cachePath, cx, script);
|
2005-10-20 20:39:24 +04:00
|
|
|
|
|
|
|
// Don't treat failure to write as fatal, since we might be working
|
2010-08-12 23:37:52 +04:00
|
|
|
// with a read-only cache.
|
2005-10-20 20:39:24 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2010-08-12 23:37:52 +04:00
|
|
|
LOG(("Successfully wrote to cache\n"));
|
2005-10-20 20:39:24 +04:00
|
|
|
} else {
|
2010-08-12 23:37:52 +04:00
|
|
|
LOG(("Failed to write to cache\n"));
|
2003-03-23 10:22:18 +03:00
|
|
|
}
|
1999-09-08 00:30:25 +04:00
|
|
|
}
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2007-06-20 10:29:49 +04:00
|
|
|
// Assign aGlobal here so that it's available to recursive imports.
|
|
|
|
// See bug 384168.
|
|
|
|
*aGlobal = global;
|
|
|
|
|
2012-01-02 22:05:19 +04:00
|
|
|
uint32_t oldopts = JS_GetOptions(cx);
|
2011-12-28 20:55:11 +04:00
|
|
|
JS_SetOptions(cx, oldopts |
|
|
|
|
(exception ? JSOPTION_DONT_REPORT_UNCAUGHT : 0));
|
|
|
|
bool ok = JS_ExecuteScriptVersion(cx, global, script, NULL, JSVERSION_LATEST);
|
|
|
|
JS_SetOptions(cx, oldopts);
|
|
|
|
|
|
|
|
if (!ok) {
|
2005-10-20 20:39:24 +04:00
|
|
|
#ifdef DEBUG_shaver_off
|
|
|
|
fprintf(stderr, "mJCL: failed to execute %s\n", nativePath.get());
|
|
|
|
#endif
|
2011-12-28 20:55:11 +04:00
|
|
|
if (exception) {
|
|
|
|
JS_GetPendingException(cx, exception);
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
}
|
2007-06-20 10:29:49 +04:00
|
|
|
*aGlobal = nsnull;
|
2005-10-20 20:39:24 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2003-03-23 10:22:18 +03:00
|
|
|
}
|
1999-09-08 00:30:25 +04:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
/* Freed when we remove from the table. */
|
2010-06-15 23:38:46 +04:00
|
|
|
*aLocation = ToNewCString(nativePath);
|
2007-06-20 10:29:49 +04:00
|
|
|
if (!*aLocation) {
|
|
|
|
*aGlobal = nsnull;
|
2005-12-13 20:55:43 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-06-20 10:29:49 +04:00
|
|
|
}
|
2000-09-21 08:30:32 +04:00
|
|
|
|
2010-06-08 04:05:02 +04:00
|
|
|
JS_AddNamedObjectRoot(cx, aGlobal, *aLocation);
|
2005-10-20 20:39:24 +04:00
|
|
|
return NS_OK;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-12 00:13:26 +04:00
|
|
|
/* static */ PLDHashOperator
|
2011-11-08 21:08:49 +04:00
|
|
|
mozJSComponentLoader::ClearModules(const nsACString& key, ModuleEntry*& entry, void* cx)
|
2010-06-12 00:13:26 +04:00
|
|
|
{
|
|
|
|
entry->Clear();
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
void
|
|
|
|
mozJSComponentLoader::UnloadModules()
|
2005-12-03 18:27:17 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mInitialized = false;
|
2005-12-03 18:27:17 +03:00
|
|
|
|
2007-06-20 10:29:49 +04:00
|
|
|
mInProgressImports.Clear();
|
2007-05-16 03:27:40 +04:00
|
|
|
mImports.Clear();
|
2010-06-12 00:13:26 +04:00
|
|
|
|
|
|
|
mModules.Enumerate(ClearModules, NULL);
|
1999-11-10 03:34:26 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
// Destroying our context will force a GC.
|
|
|
|
JS_DestroyContext(mContext);
|
|
|
|
mContext = nsnull;
|
2003-03-23 10:22:18 +03:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
mRuntimeService = nsnull;
|
2008-07-22 03:56:45 +04:00
|
|
|
mContextStack = nsnull;
|
2001-03-27 09:35:52 +04:00
|
|
|
#ifdef DEBUG_shaver_off
|
1999-09-07 10:18:08 +04:00
|
|
|
fprintf(stderr, "mJCL: UnloadAll(%d)\n", aWhen);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
NS_IMETHODIMP
|
2011-12-18 14:09:16 +04:00
|
|
|
mozJSComponentLoader::Import(const nsACString& registryLocation,
|
|
|
|
const JS::Value& targetObj,
|
|
|
|
JSContext* cx,
|
|
|
|
PRUint8 optionalArgc,
|
|
|
|
JS::Value* retval)
|
2007-05-16 03:27:40 +04:00
|
|
|
{
|
2010-05-20 03:22:19 +04:00
|
|
|
NS_TIME_FUNCTION_FMT("%s (line %d) (file: %s)", MOZ_FUNCTION_NAME,
|
|
|
|
__LINE__, registryLocation.BeginReading());
|
|
|
|
|
2007-09-19 04:26:39 +04:00
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
JSObject *targetObject = nsnull;
|
|
|
|
|
2011-12-18 14:09:16 +04:00
|
|
|
if (optionalArgc) {
|
2007-05-16 03:27:40 +04:00
|
|
|
// The caller passed in the optional second argument. Get it.
|
2011-12-18 14:09:16 +04:00
|
|
|
if (!JSVAL_IS_OBJECT(targetObj)) {
|
|
|
|
return ReportOnCaller(cx, ERROR_SCOPE_OBJ,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(registryLocation).get());
|
|
|
|
}
|
2011-12-18 14:09:16 +04:00
|
|
|
targetObject = JSVAL_TO_OBJECT(targetObj);
|
2007-05-16 03:27:40 +04:00
|
|
|
} else {
|
|
|
|
// Our targetObject is the caller's global object. Find it by
|
|
|
|
// walking the calling object's parent chain.
|
2011-12-18 14:09:16 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIXPConnect> xpc =
|
|
|
|
do_GetService(kXPConnectServiceContractID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAXPCNativeCallContext *cc = nsnull;
|
|
|
|
rv = xpc->GetCurrentNativeCallContext(&cc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wn;
|
|
|
|
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
wn->GetJSObject(&targetObject);
|
|
|
|
if (!targetObject) {
|
|
|
|
NS_ERROR("null calling object");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-01-29 02:19:51 +03:00
|
|
|
|
|
|
|
targetObject = JS_GetGlobalForObject(cx, targetObject);
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2010-10-11 02:42:04 +04:00
|
|
|
JSAutoEnterCompartment ac;
|
2010-10-11 02:46:16 +04:00
|
|
|
if (targetObject && !ac.enter(cx, targetObject)) {
|
2010-10-11 02:42:04 +04:00
|
|
|
NS_ERROR("can't enter compartment");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
JSObject *globalObj = nsnull;
|
2011-12-18 14:09:16 +04:00
|
|
|
nsresult rv = ImportInto(registryLocation, targetObject, cx, &globalObj);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
2010-10-11 02:46:16 +04:00
|
|
|
if (globalObj && !JS_WrapObject(cx, &globalObj)) {
|
2010-10-11 02:42:04 +04:00
|
|
|
NS_ERROR("can't wrap return value");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-12-18 14:09:16 +04:00
|
|
|
*retval = OBJECT_TO_JSVAL(globalObj);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [noscript] JSObjectPtr importInto(in AUTF8String registryLocation,
|
|
|
|
in JSObjectPtr targetObj); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
mozJSComponentLoader::ImportInto(const nsACString & aLocation,
|
|
|
|
JSObject * targetObj,
|
2008-01-15 18:50:57 +03:00
|
|
|
nsAXPCNativeCallContext * cc,
|
2007-05-16 03:27:40 +04:00
|
|
|
JSObject * *_retval)
|
2011-12-18 14:09:16 +04:00
|
|
|
{
|
|
|
|
JSContext *callercx;
|
|
|
|
nsresult rv = cc->GetJSContext(&callercx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return ImportInto(aLocation, targetObj, callercx, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
mozJSComponentLoader::ImportInto(const nsACString & aLocation,
|
|
|
|
JSObject * targetObj,
|
|
|
|
JSContext * callercx,
|
|
|
|
JSObject * *_retval)
|
2007-05-16 03:27:40 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2007-12-01 09:45:04 +03:00
|
|
|
*_retval = nsnull;
|
2007-05-16 03:27:40 +04:00
|
|
|
|
|
|
|
if (!mInitialized) {
|
|
|
|
rv = ReallyInit();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2007-06-11 01:13:18 +04:00
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
2007-05-16 03:27:40 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-11 01:13:18 +04:00
|
|
|
|
2007-12-01 09:45:04 +03:00
|
|
|
// Get the URI.
|
2007-06-11 01:13:18 +04:00
|
|
|
nsCOMPtr<nsIURI> resURI;
|
|
|
|
rv = ioService->NewURI(aLocation, nsnull, nsnull, getter_AddRefs(resURI));
|
2010-06-15 23:38:46 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-11 01:13:18 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
// figure out the resolved URI
|
|
|
|
nsCOMPtr<nsIChannel> scriptChannel;
|
|
|
|
rv = ioService->NewChannelFromURI(resURI, getter_AddRefs(scriptChannel));
|
2007-12-01 09:45:04 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_INVALID_ARG);
|
2007-06-11 01:13:18 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
|
|
|
rv = scriptChannel->GetURI(getter_AddRefs(resolvedURI));
|
2007-05-16 03:27:40 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
// get the JAR if there is one
|
|
|
|
nsCOMPtr<nsIJARURI> jarURI;
|
|
|
|
jarURI = do_QueryInterface(resolvedURI, &rv);
|
|
|
|
nsCOMPtr<nsIFileURL> baseFileURL;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
2011-12-08 14:03:36 +04:00
|
|
|
while (jarURI) {
|
|
|
|
jarURI->GetJARFile(getter_AddRefs(baseURI));
|
|
|
|
jarURI = do_QueryInterface(baseURI, &rv);
|
|
|
|
}
|
2010-06-15 23:38:46 +04:00
|
|
|
baseFileURL = do_QueryInterface(baseURI, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
baseFileURL = do_QueryInterface(resolvedURI, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
nsCOMPtr<nsIFile> sourceFile;
|
|
|
|
rv = baseFileURL->GetFile(getter_AddRefs(sourceFile));
|
2007-05-16 03:27:40 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-15 23:38:46 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> sourceLocalFile;
|
|
|
|
sourceLocalFile = do_QueryInterface(sourceFile, &rv);
|
2007-05-16 03:27:40 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-08 21:08:49 +04:00
|
|
|
nsCAutoString key;
|
|
|
|
rv = resolvedURI->GetSpec(key);
|
2010-06-15 23:38:46 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
|
|
|
ModuleEntry* mod;
|
|
|
|
nsAutoPtr<ModuleEntry> newEntry;
|
2010-06-15 23:38:46 +04:00
|
|
|
if (!mImports.Get(key, &mod) && !mInProgressImports.Get(key, &mod)) {
|
2007-05-16 03:27:40 +04:00
|
|
|
newEntry = new ModuleEntry;
|
2010-06-15 23:38:46 +04:00
|
|
|
if (!newEntry || !mInProgressImports.Put(key, newEntry))
|
2007-05-16 03:27:40 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2012-02-24 01:50:01 +04:00
|
|
|
JS::Anchor<jsval> exception(JSVAL_VOID);
|
2010-06-15 23:38:46 +04:00
|
|
|
rv = GlobalForLocation(sourceLocalFile, resURI, &newEntry->global,
|
2012-02-24 01:50:01 +04:00
|
|
|
&newEntry->location, &exception.get());
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
mInProgressImports.Remove(key);
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*_retval = nsnull;
|
2008-08-04 21:03:34 +04:00
|
|
|
|
2012-02-24 01:50:01 +04:00
|
|
|
if (!JSVAL_IS_VOID(exception.get())) {
|
2008-08-04 21:03:34 +04:00
|
|
|
// An exception was thrown during compilation. Propagate it
|
|
|
|
// out to our caller so they can report it.
|
2012-02-24 01:50:01 +04:00
|
|
|
if (!JS_WrapValue(callercx, &exception.get()))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
JS_SetPendingException(callercx, exception.get());
|
2008-08-04 21:03:34 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Something failed, but we don't know what it is, guess.
|
2008-08-05 05:04:19 +04:00
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
mod = newEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mod->global, "Import table contains entry with no global");
|
|
|
|
*_retval = mod->global;
|
|
|
|
|
|
|
|
jsval symbols;
|
|
|
|
if (targetObj) {
|
2009-02-18 07:11:09 +03:00
|
|
|
JSCLContextHelper cxhelper(this);
|
2010-09-28 02:55:15 +04:00
|
|
|
|
|
|
|
JSAutoEnterCompartment ac;
|
2010-09-02 09:20:32 +04:00
|
|
|
if (!ac.enter(mContext, mod->global))
|
2011-01-21 07:19:19 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-09-19 04:26:39 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
if (!JS_GetProperty(mContext, mod->global,
|
|
|
|
"EXPORTED_SYMBOLS", &symbols)) {
|
2009-02-18 07:11:09 +03:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_NOT_PRESENT,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(aLocation).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *symbolsObj = nsnull;
|
|
|
|
if (!JSVAL_IS_OBJECT(symbols) ||
|
|
|
|
!(symbolsObj = JSVAL_TO_OBJECT(symbols)) ||
|
|
|
|
!JS_IsArrayObject(mContext, symbolsObj)) {
|
2009-02-18 07:11:09 +03:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_NOT_AN_ARRAY,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(aLocation).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over symbols array, installing symbols on targetObj:
|
|
|
|
|
2012-03-07 03:52:55 +04:00
|
|
|
uint32_t symbolCount = 0;
|
2007-05-16 03:27:40 +04:00
|
|
|
if (!JS_GetArrayLength(mContext, symbolsObj, &symbolCount)) {
|
2009-02-18 07:11:09 +03:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_GETTING_ARRAY_LENGTH,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(aLocation).get());
|
|
|
|
}
|
2007-06-18 19:36:16 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCAutoString logBuffer;
|
|
|
|
#endif
|
|
|
|
|
2012-03-07 03:52:55 +04:00
|
|
|
for (uint32_t i = 0; i < symbolCount; ++i) {
|
2007-05-16 03:27:40 +04:00
|
|
|
jsval val;
|
2010-10-28 19:15:53 +04:00
|
|
|
jsid symbolId;
|
2007-05-16 03:27:40 +04:00
|
|
|
|
|
|
|
if (!JS_GetElement(mContext, symbolsObj, i, &val) ||
|
2010-10-28 19:15:53 +04:00
|
|
|
!JSVAL_IS_STRING(val) ||
|
|
|
|
!JS_ValueToId(mContext, val, &symbolId)) {
|
2009-02-18 07:11:09 +03:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_ARRAY_ELEMENT,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(aLocation).get(), i);
|
|
|
|
}
|
|
|
|
|
2010-10-28 19:15:53 +04:00
|
|
|
if (!JS_GetPropertyById(mContext, mod->global, symbolId, &val)) {
|
2010-11-11 23:40:29 +03:00
|
|
|
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId));
|
|
|
|
if (!bytes)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-02-18 07:11:09 +03:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_GETTING_SYMBOL,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(aLocation).get(),
|
2010-11-11 23:40:29 +03:00
|
|
|
bytes.ptr());
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
|
|
|
|
2010-09-29 21:00:52 +04:00
|
|
|
JSAutoEnterCompartment target_ac;
|
|
|
|
|
|
|
|
if (!target_ac.enter(mContext, targetObj) ||
|
|
|
|
!JS_WrapValue(mContext, &val) ||
|
2010-10-28 19:15:53 +04:00
|
|
|
!JS_SetPropertyById(mContext, targetObj, symbolId, &val)) {
|
2010-11-11 23:40:29 +03:00
|
|
|
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId));
|
|
|
|
if (!bytes)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-02-18 07:11:09 +03:00
|
|
|
return ReportOnCaller(cxhelper, ERROR_SETTING_SYMBOL,
|
2007-05-16 03:27:40 +04:00
|
|
|
PromiseFlatCString(aLocation).get(),
|
2010-11-11 23:40:29 +03:00
|
|
|
bytes.ptr());
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (i == 0) {
|
2007-06-18 19:36:16 +04:00
|
|
|
logBuffer.AssignLiteral("Installing symbols [ ");
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
2010-11-11 23:40:29 +03:00
|
|
|
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId));
|
|
|
|
if (!!bytes)
|
|
|
|
logBuffer.Append(bytes.ptr());
|
2007-06-18 19:36:16 +04:00
|
|
|
logBuffer.AppendLiteral(" ");
|
2007-05-16 03:27:40 +04:00
|
|
|
if (i == symbolCount - 1) {
|
2011-08-16 02:56:46 +04:00
|
|
|
LOG(("%s] from %s\n", logBuffer.get(),
|
2011-10-14 21:52:48 +04:00
|
|
|
PromiseFlatCString(aLocation).get()));
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache this module for later
|
|
|
|
if (newEntry) {
|
2010-06-15 23:38:46 +04:00
|
|
|
if (!mImports.Put(key, newEntry))
|
2007-05-16 03:27:40 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
newEntry.forget();
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-15 22:08:43 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
mozJSComponentLoader::Unload(const nsACString & aLocation)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIOService> ioService = do_GetIOService(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Get the URI.
|
|
|
|
nsCOMPtr<nsIURI> resURI;
|
|
|
|
rv = ioService->NewURI(aLocation, nsnull, nsnull, getter_AddRefs(resURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// figure out the resolved URI
|
|
|
|
nsCOMPtr<nsIChannel> scriptChannel;
|
|
|
|
rv = ioService->NewChannelFromURI(resURI, getter_AddRefs(scriptChannel));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_INVALID_ARG);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> resolvedURI;
|
|
|
|
rv = scriptChannel->GetURI(getter_AddRefs(resolvedURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-08 21:08:49 +04:00
|
|
|
nsCAutoString key;
|
|
|
|
rv = resolvedURI->GetSpec(key);
|
2011-06-15 22:08:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ModuleEntry* mod;
|
|
|
|
if (mImports.Get(key, &mod)) {
|
|
|
|
mImports.Remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
mozJSComponentLoader::Observe(nsISupports *subject, const char *topic,
|
|
|
|
const PRUnichar *data)
|
|
|
|
{
|
2010-08-12 23:37:52 +04:00
|
|
|
if (!strcmp(topic, "xpcom-shutdown-loaders")) {
|
2005-12-13 20:55:43 +03:00
|
|
|
UnloadModules();
|
2011-10-14 21:52:48 +04:00
|
|
|
} else {
|
2005-12-13 20:55:43 +03:00
|
|
|
NS_ERROR("Unexpected observer topic.");
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-12 00:13:26 +04:00
|
|
|
/* static */ already_AddRefed<nsIFactory>
|
|
|
|
mozJSComponentLoader::ModuleEntry::GetFactory(const mozilla::Module& module,
|
|
|
|
const mozilla::Module::CIDEntry& entry)
|
|
|
|
{
|
|
|
|
const ModuleEntry& self = static_cast<const ModuleEntry&>(module);
|
|
|
|
NS_ASSERTION(self.getfactoryobj, "Handing out an uninitialized module?");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFactory> f;
|
|
|
|
nsresult rv = self.getfactoryobj->Get(*entry.cid, getter_AddRefs(f));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return f.forget();
|
|
|
|
}
|
|
|
|
|
1999-10-01 01:47:04 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2008-07-22 03:56:45 +04:00
|
|
|
JSCLContextHelper::JSCLContextHelper(mozJSComponentLoader *loader)
|
2011-07-19 01:54:48 +04:00
|
|
|
: mContext(loader->mContext),
|
2011-05-13 19:56:26 +04:00
|
|
|
mContextStack(loader->mContextStack),
|
|
|
|
mBuf(nsnull)
|
2000-09-21 08:30:32 +04:00
|
|
|
{
|
2008-07-22 03:56:45 +04:00
|
|
|
mContextStack->Push(mContext);
|
2011-07-19 01:54:48 +04:00
|
|
|
JS_BeginRequest(mContext);
|
2000-09-21 08:30:32 +04:00
|
|
|
}
|
|
|
|
|
2011-05-13 19:56:26 +04:00
|
|
|
JSCLContextHelper::~JSCLContextHelper()
|
2000-09-21 08:30:32 +04:00
|
|
|
{
|
2009-02-18 07:11:09 +03:00
|
|
|
if (mContextStack) {
|
2011-07-19 01:54:48 +04:00
|
|
|
JS_EndRequest(mContext);
|
2008-07-22 03:56:45 +04:00
|
|
|
|
2009-02-18 07:11:09 +03:00
|
|
|
mContextStack->Pop(nsnull);
|
2011-05-13 19:56:26 +04:00
|
|
|
|
|
|
|
JSContext* cx = nsnull;
|
2009-02-18 07:11:09 +03:00
|
|
|
mContextStack->Peek(&cx);
|
2011-05-13 19:56:26 +04:00
|
|
|
|
2009-02-18 07:11:09 +03:00
|
|
|
mContextStack = nsnull;
|
2011-05-13 19:56:26 +04:00
|
|
|
|
|
|
|
if (cx && mBuf) {
|
|
|
|
JS_ReportError(cx, mBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mBuf) {
|
|
|
|
JS_smprintf_free(mBuf);
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
2011-05-13 19:56:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
JSCLContextHelper::reportErrorAfterPop(char *buf)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mBuf, "Already called reportErrorAfterPop");
|
|
|
|
mBuf = buf;
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|