2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-12 11:00:09 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2011-12-16 23:42:07 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <cstdarg>
|
2007-05-16 03:27:40 +04:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2014-02-12 01:01:29 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
# include <android/log.h>
|
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
2000-05-26 08:56:23 +04:00
|
|
|
|
2013-03-17 07:36:37 +04:00
|
|
|
#include "jsapi.h"
|
2018-09-05 12:25:42 +03:00
|
|
|
#include "js/CharacterEncoding.h"
|
2018-08-25 06:51:49 +03:00
|
|
|
#include "js/CompilationAndEvaluation.h"
|
2018-02-21 19:30:19 +03:00
|
|
|
#include "js/Printf.h"
|
2019-01-04 00:37:01 +03:00
|
|
|
#include "js/PropertySpec.h"
|
2000-05-26 08:56:23 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2005-12-13 20:55:43 +03:00
|
|
|
#include "nsAutoPtr.h"
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
#include "nsExceptionHandler.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "nsIComponentManager.h"
|
2010-06-10 22:11:11 +04:00
|
|
|
#include "mozilla/Module.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "mozJSComponentLoader.h"
|
2011-07-10 07:21:16 +04:00
|
|
|
#include "mozJSLoaderUtils.h"
|
1999-09-07 10:18:08 +04:00
|
|
|
#include "nsIXPConnect.h"
|
2000-05-26 08:56:23 +04:00
|
|
|
#include "nsIScriptSecurityManager.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"
|
2012-03-09 13:48:50 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2014-07-10 19:36:33 +04:00
|
|
|
#include "nsJSUtils.h"
|
2010-11-16 04:39:28 +03:00
|
|
|
#include "xpcprivate.h"
|
2011-12-28 12:13:38 +04:00
|
|
|
#include "xpcpublic.h"
|
2012-07-23 18:47:18 +04:00
|
|
|
#include "nsContentUtils.h"
|
2017-10-30 04:17:34 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2015-03-06 19:34:08 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2017-06-20 23:11:06 +03:00
|
|
|
#include "GeckoProfiler.h"
|
2012-07-23 18:47:18 +04:00
|
|
|
#include "WrapperFactory.h"
|
2000-03-31 15:19:51 +04:00
|
|
|
|
2017-05-20 22:20:35 +03:00
|
|
|
#include "AutoMemMap.h"
|
|
|
|
#include "ScriptPreloader-inl.h"
|
|
|
|
|
2010-08-12 23:37:52 +04:00
|
|
|
#include "mozilla/scache/StartupCache.h"
|
|
|
|
#include "mozilla/scache/StartupCacheUtils.h"
|
2014-05-13 01:37:46 +04:00
|
|
|
#include "mozilla/MacroForEach.h"
|
2012-06-07 06:52:14 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2017-11-05 03:20:17 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2017-05-04 08:06:33 +03:00
|
|
|
#include "mozilla/ScriptPreloader.h"
|
2018-01-08 16:05:03 +03:00
|
|
|
#include "mozilla/dom/DOMPrefs.h"
|
2014-08-08 16:30:54 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2018-01-19 01:12:04 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2015-12-06 17:20:16 +03:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2010-08-12 23:37:52 +04:00
|
|
|
|
2011-11-08 21:10:51 +04:00
|
|
|
using namespace mozilla;
|
2011-07-20 11:39:09 +04:00
|
|
|
using namespace mozilla::scache;
|
2017-05-20 22:20:35 +03:00
|
|
|
using namespace mozilla::loader;
|
2012-07-23 18:47:18 +04:00
|
|
|
using namespace xpc;
|
2013-04-23 19:48:05 +04:00
|
|
|
using namespace JS;
|
2011-07-20 11:39:09 +04:00
|
|
|
|
2017-09-10 00:59:00 +03:00
|
|
|
#define JS_CACHE_PREFIX(aType) "jsloader/" aType
|
1999-11-10 03:34:26 +03:00
|
|
|
|
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
|
|
|
|
2016-05-26 02:31:13 +03:00
|
|
|
// MOZ_LOG=JSComponentLoader:5
|
2016-01-28 21:35:00 +03:00
|
|
|
static LazyLogModule gJSCLLog("JSComponentLoader");
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
#define LOG(args) MOZ_LOG(gJSCLLog, mozilla::LogLevel::Debug, args)
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
// Components.utils.import error messages
|
|
|
|
#define ERROR_SCOPE_OBJ "%s - Second argument must be an object."
|
2018-12-13 00:30:04 +03:00
|
|
|
#define ERROR_NO_TARGET_OBJECT "%s - Couldn't find target object for import."
|
2007-05-16 03:27:40 +04:00
|
|
|
#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."
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
static bool Dump(JSContext* cx, unsigned argc, Value* vp) {
|
2018-01-08 16:05:03 +03:00
|
|
|
if (!mozilla::dom::DOMPrefs::DumpEnabled()) {
|
2017-08-02 21:56:33 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-16 16:31:36 +04:00
|
|
|
CallArgs args = CallArgsFromVp(argc, vp);
|
|
|
|
|
2018-09-12 21:14:49 +03:00
|
|
|
if (args.length() == 0) {
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2010-03-24 00:08:18 +03:00
|
|
|
|
2014-07-10 19:36:33 +04:00
|
|
|
RootedString str(cx, JS::ToString(cx, args[0]));
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!str) {
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2018-09-05 12:25:42 +03:00
|
|
|
JS::UniqueChars utf8str = JS_EncodeStringToUTF8(cx, str);
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!utf8str) {
|
2011-11-26 14:05:59 +04:00
|
|
|
return false;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2010-12-03 11:24:17 +03:00
|
|
|
|
2014-02-12 01:01:29 +04:00
|
|
|
#ifdef ANDROID
|
2018-09-05 12:25:42 +03:00
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", utf8str.get());
|
2014-02-12 01:01:29 +04:00
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (IsDebuggerPresent()) {
|
2014-07-10 19:36:33 +04:00
|
|
|
nsAutoJSString wstr;
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!wstr.init(cx, str)) {
|
2014-07-10 19:36:33 +04:00
|
|
|
return false;
|
2014-02-12 01:01:29 +04:00
|
|
|
}
|
2014-07-10 19:36:33 +04:00
|
|
|
OutputDebugStringW(wstr.get());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-02-12 01:01:29 +04:00
|
|
|
#endif
|
2018-09-05 12:25:42 +03:00
|
|
|
fputs(utf8str.get(), stdout);
|
2014-02-12 01:01:29 +04:00
|
|
|
fflush(stdout);
|
2011-11-26 14:05:59 +04:00
|
|
|
return true;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2015-07-18 22:45:35 +03:00
|
|
|
static bool Debug(JSContext* cx, unsigned argc, Value* 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
|
|
|
|
}
|
|
|
|
|
2013-04-23 01:15:49 +04:00
|
|
|
static const JSFunctionSpec gGlobalFun[] = {
|
2017-08-25 18:04:12 +03:00
|
|
|
JS_FN("dump", Dump, 1, 0), JS_FN("debug", Debug, 1, 0),
|
|
|
|
JS_FN("atob", Atob, 1, 0), JS_FN("btoa", Btoa, 1, 0), JS_FS_END};
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2013-04-29 22:16:19 +04:00
|
|
|
class MOZ_STACK_CLASS JSCLContextHelper {
|
2005-10-20 20:39:24 +04:00
|
|
|
public:
|
2014-09-01 05:06:35 +04:00
|
|
|
explicit JSCLContextHelper(JSContext* aCx);
|
2011-05-13 19:56:26 +04:00
|
|
|
~JSCLContextHelper();
|
2009-02-18 07:11:09 +03:00
|
|
|
|
2017-03-04 01:10:11 +03:00
|
|
|
void reportErrorAfterPop(UniqueChars&& buf);
|
2005-10-20 20:39:24 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
JSContext* mContext;
|
2017-03-04 01:10:11 +03:00
|
|
|
UniqueChars mBuf;
|
2010-03-12 09:50:10 +03:00
|
|
|
|
|
|
|
// prevent copying and assignment
|
2015-03-29 01:22:11 +03:00
|
|
|
JSCLContextHelper(const JSCLContextHelper&) = delete;
|
|
|
|
const JSCLContextHelper& operator=(const JSCLContextHelper&) = delete;
|
2005-10-20 20:39:24 +04:00
|
|
|
};
|
|
|
|
|
2016-10-11 21:44:40 +03:00
|
|
|
static nsresult MOZ_FORMAT_PRINTF(2, 3)
|
2016-09-30 06:34:44 +03:00
|
|
|
ReportOnCallerUTF8(JSContext* callerContext, 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);
|
|
|
|
|
2017-03-04 01:10:11 +03:00
|
|
|
UniqueChars buf = JS_vsmprintf(format, ap);
|
2011-05-13 19:56:26 +04:00
|
|
|
if (!buf) {
|
2016-10-10 14:31:16 +03:00
|
|
|
va_end(ap);
|
2011-05-13 19:56:26 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-03-04 01:10:11 +03:00
|
|
|
JS_ReportErrorUTF8(callerContext, "%s", buf.get());
|
2011-05-13 19:56:26 +04:00
|
|
|
|
2016-10-10 14:31:16 +03:00
|
|
|
va_end(ap);
|
2011-05-13 19:56:26 +04:00
|
|
|
return NS_OK;
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
|
|
|
|
1999-09-26 00:07:20 +04:00
|
|
|
mozJSComponentLoader::mozJSComponentLoader()
|
2014-08-07 22:36:31 +04:00
|
|
|
: mModules(16),
|
2014-08-06 17:31:21 +04:00
|
|
|
mImports(16),
|
|
|
|
mInProgressImports(16),
|
2017-07-25 06:32:42 +03:00
|
|
|
mLocations(16),
|
2017-07-19 00:47:27 +03:00
|
|
|
mInitialized(false),
|
|
|
|
mShareLoaderGlobal(false),
|
|
|
|
mLoaderGlobal(dom::RootingCx()) {
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(!sSelf, "mozJSComponentLoader should be a singleton");
|
2017-10-23 23:53:58 +03:00
|
|
|
}
|
2005-12-13 20:55:43 +03:00
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
#define ENSURE_DEP(name) \
|
|
|
|
{ \
|
|
|
|
nsresult rv = Ensure##name(); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
}
|
|
|
|
#define ENSURE_DEPS(...) MOZ_FOR_EACH(ENSURE_DEP, (), (__VA_ARGS__));
|
|
|
|
#define BEGIN_ENSURE(self, ...) \
|
|
|
|
{ \
|
|
|
|
if (m##self) return NS_OK; \
|
|
|
|
ENSURE_DEPS(__VA_ARGS__); \
|
|
|
|
}
|
|
|
|
|
|
|
|
class MOZ_STACK_CLASS ComponentLoaderInfo {
|
|
|
|
public:
|
2014-09-01 05:06:35 +04:00
|
|
|
explicit ComponentLoaderInfo(const nsACString& aLocation)
|
|
|
|
: mLocation(aLocation) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
nsIIOService* IOService() {
|
|
|
|
MOZ_ASSERT(mIOService);
|
|
|
|
return mIOService;
|
|
|
|
}
|
|
|
|
nsresult EnsureIOService() {
|
2017-07-22 01:42:38 +03:00
|
|
|
if (mIOService) {
|
|
|
|
return NS_OK;
|
2014-05-13 01:37:46 +04:00
|
|
|
}
|
2017-07-25 06:32:42 +03:00
|
|
|
nsresult rv;
|
2005-10-20 20:39:24 +04:00
|
|
|
mIOService = do_GetIOService(&rv);
|
2014-05-13 01:37:46 +04:00
|
|
|
return rv;
|
2017-07-25 06:32:42 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
nsIURI* URI() {
|
|
|
|
MOZ_ASSERT(mURI);
|
|
|
|
return mURI;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-07-25 06:32:42 +03:00
|
|
|
nsresult EnsureURI() {
|
|
|
|
BEGIN_ENSURE(URI, IOService);
|
|
|
|
return mIOService->NewURI(mLocation, nullptr, nullptr,
|
2014-05-13 01:37:46 +04:00
|
|
|
getter_AddRefs(mURI));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
nsIChannel* ScriptChannel() {
|
|
|
|
MOZ_ASSERT(mScriptChannel);
|
|
|
|
return mScriptChannel;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-13 01:37:46 +04:00
|
|
|
nsresult EnsureScriptChannel() {
|
|
|
|
BEGIN_ENSURE(ScriptChannel, IOService, URI);
|
2015-02-17 21:09:23 +03:00
|
|
|
return NS_NewChannel(getter_AddRefs(mScriptChannel), mURI,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2016-03-08 01:19:50 +03:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2015-02-17 21:09:23 +03:00
|
|
|
nsIContentPolicy::TYPE_SCRIPT,
|
2018-01-24 19:17:31 +03:00
|
|
|
nullptr, // aPerformanceStorage
|
2015-02-17 21:09:23 +03:00
|
|
|
nullptr, // aLoadGroup
|
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_NORMAL, mIOService);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
nsIURI* ResolvedURI() {
|
|
|
|
MOZ_ASSERT(mResolvedURI);
|
|
|
|
return mResolvedURI;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-13 01:37:46 +04:00
|
|
|
nsresult EnsureResolvedURI() {
|
2017-07-22 01:42:38 +03:00
|
|
|
BEGIN_ENSURE(ResolvedURI, URI);
|
|
|
|
return ResolveURI(mURI, getter_AddRefs(mResolvedURI));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
const nsACString& Key() { return mLocation; }
|
2017-07-25 06:32:42 +03:00
|
|
|
nsresult EnsureKey() { return NS_OK; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-30 06:34:44 +03:00
|
|
|
MOZ_MUST_USE nsresult GetLocation(nsCString& aLocation) {
|
|
|
|
nsresult rv = EnsureURI();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return mURI->GetSpec(aLocation);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
private:
|
|
|
|
const nsACString& mLocation;
|
|
|
|
nsCOMPtr<nsIIOService> mIOService;
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIChannel> mScriptChannel;
|
|
|
|
nsCOMPtr<nsIURI> mResolvedURI;
|
|
|
|
};
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
template <typename... Args>
|
|
|
|
static nsresult ReportOnCallerUTF8(JSCLContextHelper& helper,
|
|
|
|
const char* format,
|
|
|
|
ComponentLoaderInfo& info, Args... args) {
|
|
|
|
nsCString location;
|
|
|
|
MOZ_TRY(info.GetLocation(location));
|
|
|
|
|
|
|
|
UniqueChars buf = JS_smprintf(format, location.get(), args...);
|
|
|
|
if (!buf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
helper.reportErrorAfterPop(std::move(buf));
|
2018-01-19 01:12:04 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
#undef BEGIN_ENSURE
|
|
|
|
#undef ENSURE_DEPS
|
|
|
|
#undef ENSURE_DEP
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
mozJSComponentLoader::~mozJSComponentLoader() {
|
|
|
|
if (mInitialized) {
|
|
|
|
NS_ERROR(
|
2019-02-09 02:09:20 +03:00
|
|
|
"UnloadModules() was not explicitly called before cleaning up "
|
2005-12-13 20:55:43 +03:00
|
|
|
"mozJSComponentLoader");
|
|
|
|
UnloadModules();
|
1999-11-10 03:34:26 +03:00
|
|
|
}
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
sSelf = nullptr;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 02:19:34 +03:00
|
|
|
StaticRefPtr<mozJSComponentLoader> mozJSComponentLoader::sSelf;
|
1999-09-08 00:30:25 +04:00
|
|
|
|
1999-09-24 05:37:44 +04:00
|
|
|
nsresult mozJSComponentLoader::ReallyInit() {
|
2017-08-26 05:36:44 +03:00
|
|
|
MOZ_ASSERT(!mInitialized);
|
|
|
|
|
2017-09-10 00:59:45 +03:00
|
|
|
const char* shareGlobal = PR_GetEnv("MOZ_LOADER_SHARE_GLOBAL");
|
|
|
|
if (shareGlobal && *shareGlobal) {
|
|
|
|
nsDependentCString val(shareGlobal);
|
|
|
|
mShareLoaderGlobal =
|
|
|
|
!(val.EqualsLiteral("0") || val.LowerCaseEqualsLiteral("no") ||
|
|
|
|
val.LowerCaseEqualsLiteral("false") ||
|
|
|
|
val.LowerCaseEqualsLiteral("off"));
|
|
|
|
} else {
|
|
|
|
mShareLoaderGlobal = Preferences::GetBool("jsloader.shareGlobal");
|
|
|
|
}
|
2017-07-19 00:47:27 +03:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-07 00:00:29 +03:00
|
|
|
// For terrible compatibility reasons, we need to consider both the global
|
2016-08-25 11:28:47 +03:00
|
|
|
// lexical environment and the global of modules when searching for exported
|
2015-10-07 00:00:29 +03:00
|
|
|
// symbols.
|
|
|
|
static JSObject* ResolveModuleObjectProperty(JSContext* aCx,
|
|
|
|
HandleObject aModObj,
|
|
|
|
const char* name) {
|
2016-08-25 11:28:47 +03:00
|
|
|
if (JS_HasExtensibleLexicalEnvironment(aModObj)) {
|
|
|
|
RootedObject lexical(aCx, JS_ExtensibleLexicalEnvironment(aModObj));
|
2015-10-07 00:00:29 +03:00
|
|
|
bool found;
|
|
|
|
if (!JS_HasOwnProperty(aCx, lexical, name, &found)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
return lexical;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return aModObj;
|
|
|
|
}
|
|
|
|
|
2017-11-05 03:20:17 +03:00
|
|
|
static mozilla::Result<nsCString, nsresult> ReadScript(
|
|
|
|
ComponentLoaderInfo& aInfo);
|
|
|
|
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
static nsresult AnnotateScriptContents(CrashReporter::Annotation aName,
|
|
|
|
const nsACString& aURI) {
|
2017-11-05 03:20:17 +03:00
|
|
|
ComponentLoaderInfo info(aURI);
|
|
|
|
|
|
|
|
nsCString str;
|
|
|
|
MOZ_TRY_VAR(str, ReadScript(info));
|
|
|
|
|
|
|
|
// The crash reporter won't accept any strings with embedded nuls. We
|
|
|
|
// shouldn't have any here, but if we do because of data corruption, we
|
|
|
|
// still want the annotation. So replace any embedded nuls before
|
|
|
|
// annotating.
|
|
|
|
str.ReplaceSubstring(NS_LITERAL_CSTRING("\0"), NS_LITERAL_CSTRING("\\0"));
|
|
|
|
|
|
|
|
CrashReporter::AnnotateCrashReport(aName, str);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult mozJSComponentLoader::AnnotateCrashReport() {
|
|
|
|
Unused << AnnotateScriptContents(
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
CrashReporter::Annotation::nsAsyncShutdownComponent,
|
2017-11-05 03:20:17 +03:00
|
|
|
NS_LITERAL_CSTRING("resource://gre/components/nsAsyncShutdown.js"));
|
|
|
|
|
|
|
|
Unused << AnnotateScriptContents(
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
CrashReporter::Annotation::AsyncShutdownModule,
|
2017-11-05 03:20:17 +03:00
|
|
|
NS_LITERAL_CSTRING("resource://gre/modules/AsyncShutdown.jsm"));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
const mozilla::Module* mozJSComponentLoader::LoadModule(FileLocation& aFile) {
|
2016-07-08 03:08:25 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
MOZ_ASSERT(false, "Don't use JS components off the main thread");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-06-15 23:38:46 +04:00
|
|
|
|
2013-10-11 00:35:53 +04:00
|
|
|
nsCOMPtr<nsIFile> file = aFile.GetBaseFile();
|
1999-09-27 06:22:25 +04:00
|
|
|
|
2017-10-30 04:17:34 +03:00
|
|
|
nsCString spec;
|
|
|
|
aFile.GetURIString(spec);
|
2014-05-13 01:37:46 +04:00
|
|
|
ComponentLoaderInfo info(spec);
|
2017-10-30 04:17:34 +03:00
|
|
|
nsresult rv = info.EnsureURI();
|
2013-10-11 00:35:53 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
1999-09-07 10:18:08 +04:00
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
if (!mInitialized) {
|
|
|
|
rv = ReallyInit();
|
2018-09-12 21:14:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-02-11 20:53:22 +03:00
|
|
|
AUTO_PROFILER_TEXT_MARKER_CAUSE("JS XPCOM", spec, JS,
|
|
|
|
profiler_get_backtrace());
|
2018-08-02 09:49:10 +03:00
|
|
|
AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("mozJSComponentLoader::LoadModule",
|
|
|
|
OTHER, spec);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
ModuleEntry* mod;
|
2018-09-12 21:14:49 +03:00
|
|
|
if (mModules.Get(spec, &mod)) {
|
2018-08-02 09:49:10 +03:00
|
|
|
return mod;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
dom::AutoJSAPI jsapi;
|
2014-08-07 22:28:16 +04:00
|
|
|
jsapi.Init();
|
2014-01-31 13:55:20 +04:00
|
|
|
JSContext* cx = jsapi.cx();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-31 13:55:20 +04:00
|
|
|
bool isCriticalModule =
|
|
|
|
StringEndsWith(spec, NS_LITERAL_CSTRING("/nsAsyncShutdown.js"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-07 00:00:29 +03:00
|
|
|
nsAutoPtr<ModuleEntry> entry(new ModuleEntry(RootingContext::get(cx)));
|
|
|
|
RootedValue exn(cx);
|
|
|
|
rv = ObjectForLocation(info, file, &entry->obj, &entry->thisObjectKey,
|
2013-04-23 19:48:05 +04:00
|
|
|
&entry->location, isCriticalModule, &exn);
|
2015-10-07 00:00:29 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Temporary debugging assertion for bug 1403348:
|
|
|
|
if (isCriticalModule && !exn.isUndefined()) {
|
2017-11-05 03:20:17 +03:00
|
|
|
AnnotateCrashReport();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-07 00:00:29 +03:00
|
|
|
JSAutoRealm ar(cx, xpc::PrivilegedJunkScope());
|
2017-10-30 04:17:34 +03:00
|
|
|
JS_WrapValue(cx, &exn);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-30 04:17:34 +03:00
|
|
|
nsAutoCString file;
|
|
|
|
uint32_t line;
|
|
|
|
uint32_t column;
|
|
|
|
nsAutoString msg;
|
2015-10-07 00:00:29 +03:00
|
|
|
nsContentUtils::ExtractErrorValues(cx, exn, file, &line, &column, msg);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-07 00:00:29 +03:00
|
|
|
NS_ConvertUTF16toUTF8 cMsg(msg);
|
2017-10-30 04:17:34 +03:00
|
|
|
MOZ_CRASH_UNSAFE_PRINTF(
|
2015-10-07 00:00:29 +03:00
|
|
|
"Failed to load module \"%s\": "
|
2017-10-30 04:17:34 +03:00
|
|
|
"[\"%s\" {file: \"%s\", line: %u}]",
|
2015-10-07 00:00:29 +03:00
|
|
|
spec.get(), cMsg.get(), file.get(), line);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
nsCOMPtr<nsIComponentManager> cm;
|
|
|
|
rv = NS_GetComponentManager(getter_AddRefs(cm));
|
2015-10-07 00:00:29 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
2004-10-12 07:37:20 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
JSAutoRealm ar(cx, entry->obj);
|
2014-01-31 13:55:20 +04:00
|
|
|
RootedObject entryObj(cx, entry->obj);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-10-07 00:00:29 +03:00
|
|
|
RootedObject NSGetFactoryHolder(
|
2010-06-10 22:11:11 +04:00
|
|
|
cx, ResolveModuleObjectProperty(cx, entryObj, "NSGetFactory"));
|
|
|
|
RootedValue NSGetFactory_val(cx);
|
|
|
|
if (!NSGetFactoryHolder ||
|
|
|
|
!JS_GetProperty(cx, NSGetFactoryHolder, "NSGetFactory",
|
|
|
|
&NSGetFactory_val) ||
|
2016-09-30 06:34:43 +03:00
|
|
|
NSGetFactory_val.isUndefined()) {
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-09-30 06:34:43 +03:00
|
|
|
if (JS_TypeOfValue(cx, NSGetFactory_val) != JSTYPE_FUNCTION) {
|
2018-11-30 13:46:48 +03:00
|
|
|
/*
|
2016-09-30 06:34:43 +03:00
|
|
|
* spec's encoding is ASCII unless it's zip file, otherwise it's
|
|
|
|
* random encoding. Latin1 variant is safe for random encoding.
|
|
|
|
*/
|
|
|
|
JS_ReportErrorLatin1(
|
|
|
|
cx, "%s has NSGetFactory property that is not a function", spec.get());
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
2004-10-12 07:37:20 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-04-23 19:48:05 +04:00
|
|
|
RootedObject jsGetFactoryObj(cx);
|
2013-09-20 13:22:59 +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 */
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-10 00:08:15 +03:00
|
|
|
rv = nsXPConnect::XPConnect()->WrapJS(cx, jsGetFactoryObj,
|
|
|
|
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
|
2013-10-11 00:35:53 +04:00
|
|
|
return nullptr;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2017-09-13 22:19:53 +03:00
|
|
|
#if defined(NIGHTLY_BUILD) || defined(DEBUG)
|
|
|
|
if (Preferences::GetBool("browser.startup.record", false)) {
|
|
|
|
entry->importStack = xpc_PrintJSStack(cx, false, false, false).get();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
// Cache this module for later
|
2012-05-18 21:30:49 +04:00
|
|
|
mModules.Put(spec, entry);
|
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
|
|
|
}
|
|
|
|
|
2012-11-13 23:13:27 +04:00
|
|
|
void mozJSComponentLoader::FindTargetObject(JSContext* aCx,
|
2013-10-14 15:42:16 +04:00
|
|
|
MutableHandleObject aTargetObject) {
|
2017-07-19 00:47:27 +03:00
|
|
|
aTargetObject.set(js::GetJSMEnvironmentOfScriptedCaller(aCx));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-09-10 21:23:32 +03:00
|
|
|
// The above could fail if the scripted caller is not a component/JSM (it
|
|
|
|
// could be a DOM scope, for instance).
|
|
|
|
//
|
|
|
|
// If the target object was not in the JSM shared global, return the global
|
|
|
|
// instead. This is needed when calling the subscript loader within a frame
|
|
|
|
// script, since it the FrameScript NSVO will have been found.
|
|
|
|
if (!aTargetObject ||
|
2018-07-06 19:16:24 +03:00
|
|
|
!IsLoaderGlobal(JS::GetNonCCWObjectGlobal(aTargetObject))) {
|
2018-12-13 00:30:04 +03:00
|
|
|
aTargetObject.set(JS::GetScriptedCallerGlobal(aCx));
|
|
|
|
|
|
|
|
// Return nullptr if the scripted caller is in a different compartment.
|
|
|
|
if (js::GetObjectCompartment(aTargetObject) !=
|
|
|
|
js::GetContextCompartment(aCx)) {
|
|
|
|
aTargetObject.set(nullptr);
|
|
|
|
}
|
2017-07-19 00:47:27 +03:00
|
|
|
}
|
2012-11-13 23:13:27 +04:00
|
|
|
}
|
|
|
|
|
2018-10-24 02:19:34 +03:00
|
|
|
void mozJSComponentLoader::InitStatics() {
|
|
|
|
MOZ_ASSERT(!sSelf);
|
|
|
|
sSelf = new mozJSComponentLoader();
|
|
|
|
}
|
|
|
|
|
2019-02-09 02:09:20 +03:00
|
|
|
void mozJSComponentLoader::Unload() {
|
|
|
|
if (sSelf) {
|
|
|
|
sSelf->UnloadModules();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 02:19:34 +03:00
|
|
|
void mozJSComponentLoader::Shutdown() {
|
|
|
|
MOZ_ASSERT(sSelf);
|
|
|
|
sSelf = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-31 07:19:57 +03:00
|
|
|
// This requires that the keys be strings and the values be pointers.
|
|
|
|
template <class Key, class Data, class UserData>
|
|
|
|
static size_t SizeOfTableExcludingThis(
|
|
|
|
const nsBaseHashtable<Key, Data, UserData>& aTable,
|
|
|
|
MallocSizeOf aMallocSizeOf) {
|
|
|
|
size_t n = aTable.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (auto iter = aTable.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
n += iter.Key().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
|
|
|
|
n += iter.Data()->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
return n;
|
2013-12-06 01:54:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t mozJSComponentLoader::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
|
2015-07-31 07:19:57 +03:00
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += SizeOfTableExcludingThis(mModules, aMallocSizeOf);
|
|
|
|
n += SizeOfTableExcludingThis(mImports, aMallocSizeOf);
|
2017-07-25 06:32:42 +03:00
|
|
|
n += mLocations.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2015-07-31 07:19:57 +03:00
|
|
|
n += SizeOfTableExcludingThis(mInProgressImports, aMallocSizeOf);
|
|
|
|
return n;
|
2013-12-06 01:54:30 +04:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:46:13 +03:00
|
|
|
void mozJSComponentLoader::CreateLoaderGlobal(JSContext* aCx,
|
2017-08-26 05:36:44 +03:00
|
|
|
const nsACString& aLocation,
|
2017-05-16 01:46:13 +03:00
|
|
|
MutableHandleObject aGlobal) {
|
2017-09-19 02:35:39 +03:00
|
|
|
RefPtr<BackstagePass> backstagePass;
|
|
|
|
nsresult rv = NS_NewBackstagePass(getter_AddRefs(backstagePass));
|
2017-05-16 01:46:13 +03:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2017-06-12 20:45:52 +03:00
|
|
|
RealmOptions options;
|
|
|
|
|
2017-05-16 01:46:13 +03:00
|
|
|
options.creationOptions().setNewCompartmentInSystemZone();
|
|
|
|
xpc::SetPrefableRealmOptions(options);
|
|
|
|
|
2018-03-14 06:21:22 +03:00
|
|
|
// Defer firing OnNewGlobalObject until after the __URI__ property has
|
|
|
|
// been defined so the JS debugger can tell what module the global is
|
2018-11-30 13:46:48 +03:00
|
|
|
// for
|
2017-09-19 02:35:39 +03:00
|
|
|
RootedObject global(aCx);
|
2018-03-14 06:21:22 +03:00
|
|
|
rv = xpc::InitClassesWithNewWrappedGlobal(
|
|
|
|
aCx, static_cast<nsIGlobalObject*>(backstagePass),
|
|
|
|
nsContentUtils::GetSystemPrincipal(), xpc::DONT_FIRE_ONNEWGLOBALHOOK,
|
2018-09-12 21:14:49 +03:00
|
|
|
options, &global);
|
2017-07-19 00:47:27 +03:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE_VOID(global);
|
|
|
|
|
|
|
|
backstagePass->SetGlobalObject(global);
|
|
|
|
|
|
|
|
JSAutoRealm ar(aCx, global);
|
|
|
|
if (!JS_DefineFunctions(aCx, global, gGlobalFun) ||
|
|
|
|
!JS_DefineProfilingFunctions(aCx, global)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the location information for the new global, so that tools like
|
|
|
|
// about:memory may use that information
|
|
|
|
xpc::SetLocationForGlobal(global, aLocation);
|
|
|
|
|
|
|
|
aGlobal.set(global);
|
|
|
|
}
|
|
|
|
|
2017-08-26 05:36:44 +03:00
|
|
|
bool mozJSComponentLoader::ReuseGlobal(nsIURI* aURI) {
|
|
|
|
if (!mShareLoaderGlobal) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-26 05:36:44 +03:00
|
|
|
nsCString spec;
|
|
|
|
NS_ENSURE_SUCCESS(aURI->GetSpec(spec), false);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-08-26 05:36:44 +03:00
|
|
|
// The loader calls Object.freeze on global properties, which
|
|
|
|
// causes problems if the global is shared with other code.
|
|
|
|
if (spec.EqualsASCII("resource://gre/modules/commonjs/toolkit/loader.js")) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-08-26 05:36:44 +03:00
|
|
|
// Various tests call addDebuggerToGlobal on the result of
|
|
|
|
// importing this JSM, which would be annoying to fix.
|
|
|
|
if (spec.EqualsASCII("resource://gre/modules/jsdebugger.jsm")) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-18 21:42:56 +03:00
|
|
|
// BrowserTestUtils.jsm calls Cu.permitCPOWsInScope(this) which sets a
|
|
|
|
// per-compartment flag to permit CPOWs. We don't want to set this flag for
|
|
|
|
// all other JSMs.
|
|
|
|
if (spec.EqualsASCII("resource://testing-common/BrowserTestUtils.jsm")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-26 05:36:44 +03:00
|
|
|
// Some SpecialPowers jsms call Cu.forcePermissiveCOWs(),
|
|
|
|
// which sets a per-compartment flag that disables certain
|
|
|
|
// security wrappers, so don't use the shared global for them
|
|
|
|
// to avoid breaking tests.
|
|
|
|
if (FindInReadable(NS_LITERAL_CSTRING("resource://specialpowers/"), spec)) {
|
|
|
|
return false;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
return true;
|
2017-08-26 05:36:44 +03:00
|
|
|
}
|
|
|
|
|
2014-08-07 22:16:47 +04:00
|
|
|
JSObject* mozJSComponentLoader::GetSharedGlobal(JSContext* aCx) {
|
2018-03-14 06:21:22 +03:00
|
|
|
if (!mLoaderGlobal) {
|
2017-05-16 01:46:13 +03:00
|
|
|
JS::RootedObject globalObj(aCx);
|
2018-03-14 06:21:22 +03:00
|
|
|
CreateLoaderGlobal(aCx, NS_LITERAL_CSTRING("shared JSM global"),
|
2017-05-16 01:46:13 +03:00
|
|
|
&globalObj);
|
2012-10-31 20:13:28 +04:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
// If we fail to create a module global this early, we're not going to
|
|
|
|
// get very far, so just bail out now.
|
|
|
|
MOZ_RELEASE_ASSERT(globalObj);
|
2017-08-26 05:36:44 +03:00
|
|
|
mLoaderGlobal = globalObj;
|
2017-07-19 00:47:27 +03:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
// AutoEntryScript required to invoke debugger hook, which is a
|
|
|
|
// Gecko-specific concept at present.
|
2017-08-26 05:36:44 +03:00
|
|
|
dom::AutoEntryScript aes(globalObj, "component loader report global");
|
2012-10-31 20:13:28 +04:00
|
|
|
JS_FireOnNewGlobalObject(aes.cx(), globalObj);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-10-31 20:13:28 +04:00
|
|
|
|
2010-06-15 23:38:46 +04:00
|
|
|
return mLoaderGlobal;
|
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
JSObject* mozJSComponentLoader::PrepareObjectForLocation(
|
2010-06-15 23:38:46 +04:00
|
|
|
JSContext* aCx, nsIFile* aComponentFile, nsIURI* aURI, bool* aReuseGlobal,
|
2012-10-31 20:13:28 +04:00
|
|
|
bool* aRealFile) {
|
|
|
|
nsAutoCString nativePath;
|
2017-06-12 20:45:52 +03:00
|
|
|
NS_ENSURE_SUCCESS(aURI->GetSpec(nativePath), nullptr);
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2015-07-01 21:17:17 +03:00
|
|
|
bool reuseGlobal = ReuseGlobal(aURI);
|
2015-07-04 04:29:00 +03:00
|
|
|
|
2017-05-10 00:08:15 +03:00
|
|
|
*aReuseGlobal = reuseGlobal;
|
2010-06-15 23:38:46 +04:00
|
|
|
|
2018-09-12 21:14:49 +03:00
|
|
|
bool createdNewGlobal = false;
|
|
|
|
RootedObject globalObj(aCx);
|
2017-08-26 05:36:44 +03:00
|
|
|
if (reuseGlobal) {
|
|
|
|
globalObj = GetSharedGlobal(aCx);
|
2018-09-12 21:14:49 +03:00
|
|
|
} else if (!globalObj) {
|
|
|
|
CreateLoaderGlobal(aCx, nativePath, &globalObj);
|
2015-03-06 19:34:08 +03:00
|
|
|
createdNewGlobal = true;
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
|
|
|
|
2011-04-14 02:10:13 +04:00
|
|
|
// |thisObj| is the object we set properties on for a particular .jsm.
|
2014-01-22 06:11:17 +04:00
|
|
|
RootedObject thisObj(aCx, globalObj);
|
|
|
|
NS_ENSURE_TRUE(thisObj, nullptr);
|
|
|
|
|
2018-09-12 21:14:49 +03:00
|
|
|
JSAutoRealm ar(aCx, thisObj);
|
2012-10-30 23:28:11 +04:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
if (reuseGlobal) {
|
2017-05-16 01:46:13 +03:00
|
|
|
thisObj = js::NewJSMEnvironment(aCx);
|
2017-05-18 23:18:53 +03:00
|
|
|
NS_ENSURE_TRUE(thisObj, nullptr);
|
2012-10-31 20:13:28 +04:00
|
|
|
}
|
|
|
|
|
2017-11-05 03:20:17 +03:00
|
|
|
*aRealFile = false;
|
|
|
|
|
|
|
|
// need to be extra careful checking for URIs pointing to files
|
2010-06-15 23:38:46 +04:00
|
|
|
// EnsureFile may not always get called, especially on resource URIs
|
2017-11-05 03:20:17 +03:00
|
|
|
// so we need to call GetFile to make sure this is a valid file
|
2017-05-16 01:46:13 +03:00
|
|
|
nsresult rv = NS_OK;
|
2017-11-05 03:20:17 +03:00
|
|
|
nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(aURI, &rv);
|
|
|
|
nsCOMPtr<nsIFile> testFile;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
fileURL->GetFile(getter_AddRefs(testFile));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-11-05 03:20:17 +03:00
|
|
|
|
|
|
|
if (testFile) {
|
|
|
|
*aRealFile = true;
|
|
|
|
|
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
RootedObject locationObj(aCx);
|
|
|
|
|
|
|
|
rv = nsXPConnect::XPConnect()->WrapNative(aCx, thisObj, aComponentFile,
|
2018-09-12 21:14:49 +03:00
|
|
|
NS_GET_IID(nsIFile),
|
|
|
|
locationObj.address());
|
2017-11-05 03:20:17 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2015-03-06 19:34:08 +03:00
|
|
|
NS_ENSURE_TRUE(locationObj, nullptr);
|
2017-11-05 03:20:17 +03:00
|
|
|
|
|
|
|
if (!JS_DefineProperty(aCx, thisObj, "__LOCATION__", locationObj, 0)) {
|
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2017-11-05 03:20:17 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
// Expose the URI from which the script was imported through a special
|
2014-08-08 16:30:54 +04:00
|
|
|
// variable that we insert into the JSM.
|
2014-01-22 06:11:17 +04:00
|
|
|
RootedString exposedUri(
|
2014-08-08 16:30:54 +04:00
|
|
|
aCx, JS_NewStringCopyN(aCx, nativePath.get(), nativePath.Length()));
|
|
|
|
NS_ENSURE_TRUE(exposedUri, nullptr);
|
2012-10-31 20:13:28 +04:00
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
if (!JS_DefineProperty(aCx, thisObj, "__URI__", exposedUri, 0)) {
|
2012-10-31 20:13:28 +04:00
|
|
|
return nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-10-31 20:13:28 +04:00
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
if (createdNewGlobal) {
|
2014-08-08 16:30:54 +04:00
|
|
|
// AutoEntryScript required to invoke debugger hook, which is a
|
|
|
|
// Gecko-specific concept at present.
|
2018-08-02 09:49:10 +03:00
|
|
|
dom::AutoEntryScript aes(globalObj, "component loader report global");
|
|
|
|
JS_FireOnNewGlobalObject(aes.cx(), globalObj);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2013-04-26 21:50:18 +04:00
|
|
|
return thisObj;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-10-31 20:13:28 +04:00
|
|
|
|
|
|
|
static mozilla::Result<nsCString, nsresult> ReadScript(
|
2014-05-13 01:37:46 +04:00
|
|
|
ComponentLoaderInfo& aInfo) {
|
|
|
|
MOZ_TRY(aInfo.EnsureScriptChannel());
|
2010-08-12 23:37:52 +04:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
nsCOMPtr<nsIInputStream> scriptStream;
|
2019-02-12 19:08:25 +03:00
|
|
|
MOZ_TRY(NS_MaybeOpenChannelUsingOpen(aInfo.ScriptChannel(),
|
|
|
|
getter_AddRefs(scriptStream)));
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2010-08-12 23:37:52 +04:00
|
|
|
uint64_t len64;
|
2017-11-05 03:20:17 +03:00
|
|
|
uint32_t bytesRead;
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2017-07-22 01:42:38 +03:00
|
|
|
MOZ_TRY(scriptStream->Available(&len64));
|
|
|
|
NS_ENSURE_TRUE(len64 < UINT32_MAX, Err(NS_ERROR_FILE_TOO_BIG));
|
|
|
|
NS_ENSURE_TRUE(len64, Err(NS_ERROR_FAILURE));
|
2017-11-05 03:20:17 +03:00
|
|
|
uint32_t len = (uint32_t)len64;
|
2017-07-22 01:42:38 +03:00
|
|
|
|
2017-09-10 00:59:00 +03:00
|
|
|
/* malloc an internal buf the size of the file */
|
|
|
|
nsCString str;
|
2017-07-22 01:42:38 +03:00
|
|
|
if (!str.SetLength(len, fallible)) {
|
2011-07-10 07:21:16 +04:00
|
|
|
return Err(NS_ERROR_OUT_OF_MEMORY);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-07-10 07:21:16 +04:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
/* read the file in one swoop */
|
|
|
|
MOZ_TRY(scriptStream->Read(str.BeginWriting(), len, &bytesRead));
|
|
|
|
if (bytesRead != len) {
|
2017-07-07 21:14:04 +03:00
|
|
|
return Err(NS_BASE_STREAM_OSERROR);
|
2017-05-01 07:53:49 +03:00
|
|
|
}
|
2012-10-31 20:13:28 +04:00
|
|
|
|
2017-05-01 07:53:49 +03:00
|
|
|
return std::move(str);
|
2018-07-24 18:44:35 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
nsresult mozJSComponentLoader::ObjectForLocation(
|
|
|
|
ComponentLoaderInfo& aInfo, nsIFile* aComponentFile,
|
|
|
|
MutableHandleObject aObject, MutableHandleScript aTableScript,
|
|
|
|
char** aLocation, bool aPropagateExceptions,
|
|
|
|
MutableHandleValue aException) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-08 16:30:54 +04:00
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
2017-07-19 00:47:27 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
bool realFile = false;
|
|
|
|
nsresult rv = aInfo.EnsureURI();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
bool reuseGlobal = false;
|
|
|
|
RootedObject obj(cx, PrepareObjectForLocation(cx, aComponentFile, aInfo.URI(),
|
2017-07-19 00:47:27 +03:00
|
|
|
&reuseGlobal, &realFile));
|
2017-05-16 00:33:21 +03:00
|
|
|
NS_ENSURE_TRUE(obj, NS_ERROR_FAILURE);
|
|
|
|
MOZ_ASSERT(JS_IsGlobalObject(obj) == !reuseGlobal);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-08-02 09:49:10 +03:00
|
|
|
JSAutoRealm ar(cx, obj);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
RootedScript script(cx);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
nsAutoCString nativePath;
|
2014-05-13 01:37:46 +04:00
|
|
|
rv = aInfo.URI()->GetSpec(nativePath);
|
2017-05-16 00:33:21 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
// Before compiling the script, first check to see if we have it in
|
|
|
|
// the startupcache. Note: as a rule, startupcache errors are not fatal
|
|
|
|
// to loading the script, since we can always slow-load.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
bool writeToCache = false;
|
2013-10-14 15:42:16 +04:00
|
|
|
StartupCache* cache = StartupCache::GetSingleton();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-10-14 15:42:16 +04:00
|
|
|
aInfo.EnsureResolvedURI();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
nsAutoCString cachePath(reuseGlobal ? JS_CACHE_PREFIX("non-syntactic")
|
2016-09-23 10:50:47 +03:00
|
|
|
: JS_CACHE_PREFIX("global"));
|
|
|
|
rv = PathifyURI(aInfo.ResolvedURI(), cachePath);
|
2017-07-19 21:57:18 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-19 21:57:18 +03:00
|
|
|
script = ScriptPreloader::GetSingleton().GetCachedScript(cx, cachePath);
|
2010-06-15 23:38:46 +04:00
|
|
|
if (!script && cache) {
|
2017-05-20 22:20:35 +03:00
|
|
|
ReadCachedScript(cache, cachePath, cx, &script);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:00:46 +03:00
|
|
|
if (script) {
|
2017-05-16 00:33:21 +03:00
|
|
|
LOG(("Successfully loaded %s from startupcache\n", nativePath.get()));
|
2017-05-01 07:53:49 +03:00
|
|
|
} else if (cache) {
|
2017-05-16 00:33:21 +03: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.
|
2017-05-01 07:53:49 +03:00
|
|
|
writeToCache = true;
|
2017-05-16 00:33:21 +03:00
|
|
|
// ReadCachedScript may have set a pending exception.
|
|
|
|
JS_ClearPendingException(cx);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:00:46 +03:00
|
|
|
if (!script) {
|
2017-05-16 00:33:21 +03:00
|
|
|
// The script wasn't in the cache , so compile it now.
|
|
|
|
LOG(("Slow loading %s\n", nativePath.get()));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-05-16 00:33:21 +03:00
|
|
|
// If we are debugging a replaying process and have diverged from the
|
2017-05-20 22:20:35 +03:00
|
|
|
// recording, trying to load and compile new code will cause the
|
|
|
|
// debugger operation to fail, so just abort now.
|
2018-09-12 21:14:49 +03:00
|
|
|
if (recordreplay::HasDivergedFromRecording()) {
|
2018-09-13 06:48:44 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 06:48:44 +03:00
|
|
|
// Use lazy source if we're using the startup cache. Non-lazy source +
|
|
|
|
// startup cache regresses installer size (due to source code stored in
|
|
|
|
// XDR encoded modules in omni.ja). Also, XDR decoding is relatively
|
|
|
|
// fast. When we're not using the startup cache, we want to use non-lazy
|
2017-05-16 00:33:21 +03:00
|
|
|
// source code so that we can use lazy parsing.
|
2018-09-13 06:48:44 +03:00
|
|
|
// See bug 1303754.
|
|
|
|
CompileOptions options(cx);
|
|
|
|
options.setNoScriptRval(true)
|
|
|
|
.maybeMakeStrictMode(true)
|
|
|
|
.setFileAndLine(nativePath.get(), 1)
|
|
|
|
.setSourceIsLazy(cache || ScriptPreloader::GetSingleton().Active());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-06-27 23:10:51 +03:00
|
|
|
if (realFile) {
|
2017-05-20 22:20:35 +03:00
|
|
|
AutoMemMap map;
|
2018-09-13 06:48:44 +03:00
|
|
|
MOZ_TRY(map.init(aComponentFile));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-13 06:48:44 +03:00
|
|
|
// Note: exceptions will get handled further down;
|
2018-01-19 01:12:04 +03:00
|
|
|
// don't early return for them here.
|
2018-09-13 06:48:44 +03:00
|
|
|
auto buf = map.get<char>();
|
2017-08-26 05:36:44 +03:00
|
|
|
if (reuseGlobal) {
|
2018-12-05 01:01:06 +03:00
|
|
|
CompileUtf8ForNonSyntacticScope(cx, options, buf.get(), map.size(),
|
|
|
|
&script);
|
2018-09-12 21:14:49 +03:00
|
|
|
} else {
|
2018-12-05 01:01:06 +03:00
|
|
|
CompileUtf8(cx, options, buf.get(), map.size(), &script);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2010-06-15 23:38:46 +04:00
|
|
|
} else {
|
2017-11-05 03:20:17 +03:00
|
|
|
nsCString str;
|
|
|
|
MOZ_TRY_VAR(str, ReadScript(aInfo));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-09-12 21:14:49 +03:00
|
|
|
if (reuseGlobal) {
|
2018-12-05 01:01:06 +03:00
|
|
|
CompileUtf8ForNonSyntacticScope(cx, options, str.get(), str.Length(),
|
|
|
|
&script);
|
2018-09-12 21:14:49 +03:00
|
|
|
} else {
|
2018-12-05 01:01:06 +03:00
|
|
|
CompileUtf8(cx, options, str.get(), str.Length(), &script);
|
2010-06-15 23:38:46 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03: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.
|
2017-05-16 01:00:46 +03:00
|
|
|
if (!script && aPropagateExceptions && jsapi.HasException()) {
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!jsapi.StealException(aException)) {
|
2016-07-14 06:18:11 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-08-04 21:03:34 +04:00
|
|
|
}
|
2005-10-20 20:39:24 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2017-05-16 01:00:46 +03:00
|
|
|
if (!script) {
|
2005-10-20 20:39:24 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-09-08 00:30:25 +04:00
|
|
|
|
2017-05-16 01:00:46 +03:00
|
|
|
ScriptPreloader::GetSingleton().NoteScript(nativePath, cachePath, script);
|
2017-05-04 08:06:33 +03: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.
|
2017-07-07 21:27:53 +03: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"));
|
1999-09-08 00:30:25 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-10-20 20:39:24 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
// Assign aObject here so that it's available to recursive imports.
|
2007-06-20 10:29:49 +04:00
|
|
|
// See bug 384168.
|
2014-04-16 12:47:53 +04:00
|
|
|
aObject.set(obj);
|
2013-12-20 00:49:52 +04:00
|
|
|
|
2017-05-16 01:00:46 +03:00
|
|
|
aTableScript.set(script);
|
2013-10-14 15:42:16 +04:00
|
|
|
|
2016-03-09 23:28:33 +03:00
|
|
|
{ // Scope for AutoEntryScript
|
|
|
|
|
2017-05-16 01:00:46 +03:00
|
|
|
// We're going to run script via JS_ExecuteScript, so we need an
|
|
|
|
// AutoEntryScript. This is Gecko-specific and not in any spec.
|
2016-03-09 23:28:33 +03:00
|
|
|
dom::AutoEntryScript aes(CurrentGlobalOrNull(cx),
|
2015-04-09 04:23:48 +03:00
|
|
|
"component loader load module");
|
2016-03-09 23:28:33 +03:00
|
|
|
JSContext* aescx = aes.cx();
|
2017-07-19 00:47:27 +03:00
|
|
|
|
|
|
|
bool executeOk = false;
|
|
|
|
if (JS_IsGlobalObject(obj)) {
|
|
|
|
JS::RootedValue rval(cx);
|
|
|
|
executeOk = JS::CloneAndExecuteScript(aescx, script, &rval);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2017-07-19 00:47:27 +03:00
|
|
|
executeOk = js::ExecuteInJSMEnvironment(aescx, script, obj);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
if (!executeOk) {
|
2016-03-09 23:28:33 +03:00
|
|
|
if (aPropagateExceptions && aes.HasException()) {
|
2016-07-14 06:18:11 +03:00
|
|
|
// Ignore return value because we're returning an error code
|
|
|
|
// anyway.
|
|
|
|
Unused << aes.StealException(aException);
|
2016-03-09 23:28:33 +03:00
|
|
|
}
|
|
|
|
aObject.set(nullptr);
|
|
|
|
aTableScript.set(nullptr);
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-03-23 10:22:18 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03: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) {
|
2014-04-16 12:47:53 +04:00
|
|
|
aObject.set(nullptr);
|
|
|
|
aTableScript.set(nullptr);
|
2005-12-13 20:55:43 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-06-20 10:29:49 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-10-20 20:39:24 +04:00
|
|
|
return NS_OK;
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2005-12-13 20:55:43 +03:00
|
|
|
void mozJSComponentLoader::UnloadModules() {
|
2017-07-19 00:47:27 +03:00
|
|
|
mInitialized = false;
|
2010-06-12 00:13:26 +04:00
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
if (mLoaderGlobal) {
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
RootedObject global(cx, mLoaderGlobal);
|
2018-08-02 09:49:10 +03:00
|
|
|
JSAutoRealm ar(cx, global);
|
2017-07-19 00:47:27 +03:00
|
|
|
MOZ_ASSERT(JS_HasExtensibleLexicalEnvironment(global));
|
|
|
|
JS_SetAllNonReservedSlotsToUndefined(
|
|
|
|
cx, JS_ExtensibleLexicalEnvironment(global));
|
|
|
|
JS_SetAllNonReservedSlotsToUndefined(cx, global);
|
|
|
|
mLoaderGlobal = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2007-06-20 10:29:49 +04:00
|
|
|
mInProgressImports.Clear();
|
|
|
|
mImports.Clear();
|
2017-07-25 06:32:42 +03:00
|
|
|
mLocations.Clear();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-11-23 05:52:40 +03:00
|
|
|
for (auto iter = mModules.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.Data()->Clear();
|
|
|
|
iter.Remove();
|
|
|
|
}
|
1999-09-07 10:18:08 +04:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
nsresult mozJSComponentLoader::ImportInto(const nsACString& registryLocation,
|
|
|
|
HandleValue targetValArg,
|
|
|
|
JSContext* cx, uint8_t optionalArgc,
|
|
|
|
MutableHandleValue retval) {
|
2012-10-26 03:10:53 +04:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-04-23 19:48:05 +04:00
|
|
|
RootedValue targetVal(cx, targetValArg);
|
|
|
|
RootedObject targetObject(cx, nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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.
|
2012-07-23 18:47:18 +04:00
|
|
|
if (targetVal.isObject()) {
|
|
|
|
// If we're passing in something like a content DOM window, chances
|
|
|
|
// are the caller expects the properties to end up on the object
|
|
|
|
// proper and not on the Xray holder. This is dubious, but can be used
|
|
|
|
// during testing. Given that dumb callers can already leak JSMs into
|
|
|
|
// content by passing a raw content JS object (where Xrays aren't
|
|
|
|
// possible), we aim for consistency here. Waive xray.
|
|
|
|
if (WrapperFactory::IsXrayWrapper(&targetVal.toObject()) &&
|
2013-10-26 20:19:05 +04:00
|
|
|
!WrapperFactory::WaiveXrayAndWrap(cx, &targetVal)) {
|
2012-07-23 18:47:18 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
targetObject = &targetVal.toObject();
|
|
|
|
} else if (!targetVal.isNull()) {
|
|
|
|
// If targetVal isNull(), we actually want to leave targetObject null.
|
|
|
|
// Not doing so breaks |make package|.
|
2016-09-30 06:34:44 +03:00
|
|
|
return ReportOnCallerUTF8(cx, ERROR_SCOPE_OBJ,
|
|
|
|
PromiseFlatCString(registryLocation).get());
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2017-05-17 00:27:50 +03:00
|
|
|
FindTargetObject(cx, &targetObject);
|
2018-12-13 00:30:04 +03:00
|
|
|
if (!targetObject) {
|
|
|
|
return ReportOnCallerUTF8(cx, ERROR_NO_TARGET_OBJECT,
|
|
|
|
PromiseFlatCString(registryLocation).get());
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-10-14 21:52:47 +04:00
|
|
|
|
2018-08-08 16:07:41 +03:00
|
|
|
js::AssertSameCompartment(cx, targetObject);
|
2010-10-11 02:42:04 +04:00
|
|
|
|
2013-04-23 19:48:05 +04:00
|
|
|
RootedObject global(cx);
|
|
|
|
nsresult rv = ImportInto(registryLocation, targetObject, cx, &global);
|
2010-10-11 02:42:04 +04:00
|
|
|
|
2013-04-23 19:48:05 +04:00
|
|
|
if (global) {
|
2013-10-16 04:02:23 +04:00
|
|
|
if (!JS_WrapObject(cx, &global)) {
|
2013-04-23 19:48:05 +04:00
|
|
|
NS_ERROR("can't wrap return value");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-05-16 03:27:40 +04:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
retval.setObject(*global);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-15 22:22:45 +04:00
|
|
|
return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-15 22:22:45 +04:00
|
|
|
|
|
|
|
nsresult mozJSComponentLoader::IsModuleLoaded(const nsACString& aLocation,
|
|
|
|
bool* retval) {
|
|
|
|
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (!mInitialized) {
|
|
|
|
rv = ReallyInit();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2014-05-15 22:22:45 +04:00
|
|
|
|
2017-09-13 22:19:53 +03:00
|
|
|
ComponentLoaderInfo info(aLocation);
|
|
|
|
rv = info.EnsureKey();
|
2014-05-15 22:22:45 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-15 22:22:45 +04:00
|
|
|
*retval = !!mImports.Get(info.Key());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-10-24 02:28:44 +03:00
|
|
|
void mozJSComponentLoader::GetLoadedModules(
|
|
|
|
nsTArray<nsCString>& aLoadedModules) {
|
|
|
|
aLoadedModules.SetCapacity(mImports.Count());
|
2017-06-01 00:00:43 +03:00
|
|
|
for (auto iter = mImports.Iter(); !iter.Done(); iter.Next()) {
|
2018-10-24 02:28:44 +03:00
|
|
|
aLoadedModules.AppendElement(iter.Data()->location);
|
2017-06-01 00:00:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 02:28:44 +03:00
|
|
|
void mozJSComponentLoader::GetLoadedComponents(
|
|
|
|
nsTArray<nsCString>& aLoadedComponents) {
|
|
|
|
aLoadedComponents.SetCapacity(mModules.Count());
|
2017-06-01 00:00:43 +03:00
|
|
|
for (auto iter = mModules.Iter(); !iter.Done(); iter.Next()) {
|
2018-10-24 02:28:44 +03:00
|
|
|
aLoadedComponents.AppendElement(iter.Data()->location);
|
2017-06-01 00:00:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 22:19:53 +03:00
|
|
|
nsresult mozJSComponentLoader::GetModuleImportStack(const nsACString& aLocation,
|
|
|
|
nsACString& retval) {
|
2018-01-05 00:17:38 +03:00
|
|
|
#ifdef STARTUP_RECORDER_ENABLED
|
2017-09-13 22:19:53 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
|
|
|
ComponentLoaderInfo info(aLocation);
|
|
|
|
nsresult rv = info.EnsureKey();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ModuleEntry* mod;
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!mImports.Get(info.Key(), &mod)) {
|
2017-09-13 22:19:53 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2017-09-13 22:19:53 +03:00
|
|
|
|
|
|
|
retval = mod->importStack;
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult mozJSComponentLoader::GetComponentLoadStack(
|
|
|
|
const nsACString& aLocation, nsACString& retval) {
|
2018-01-05 00:17:38 +03:00
|
|
|
#ifdef STARTUP_RECORDER_ENABLED
|
2017-09-13 22:19:53 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
|
|
|
|
MOZ_ASSERT(mInitialized);
|
|
|
|
|
|
|
|
ComponentLoaderInfo info(aLocation);
|
|
|
|
nsresult rv = info.EnsureURI();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
ModuleEntry* mod;
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!mModules.Get(info.Key(), &mod)) {
|
2017-09-13 22:19:53 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2017-09-13 22:19:53 +03:00
|
|
|
|
|
|
|
retval = mod->importStack;
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-07 00:00:29 +03:00
|
|
|
static JSObject* ResolveModuleObjectPropertyById(JSContext* aCx,
|
|
|
|
HandleObject aModObj,
|
|
|
|
HandleId id) {
|
2016-08-25 11:28:47 +03:00
|
|
|
if (JS_HasExtensibleLexicalEnvironment(aModObj)) {
|
|
|
|
RootedObject lexical(aCx, JS_ExtensibleLexicalEnvironment(aModObj));
|
2015-10-07 00:00:29 +03:00
|
|
|
bool found;
|
|
|
|
if (!JS_HasOwnPropertyById(aCx, lexical, id, &found)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
if (found) {
|
|
|
|
return lexical;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
return aModObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult mozJSComponentLoader::ImportInto(const nsACString& aLocation,
|
|
|
|
HandleObject targetObj, JSContext* cx,
|
|
|
|
MutableHandleObject vp) {
|
|
|
|
vp.set(nullptr);
|
|
|
|
|
|
|
|
JS::RootedObject exports(cx);
|
|
|
|
MOZ_TRY(Import(cx, aLocation, vp, &exports, !targetObj));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
if (targetObj) {
|
|
|
|
JS::Rooted<JS::IdVector> ids(cx, JS::IdVector(cx));
|
|
|
|
if (!JS_Enumerate(cx, exports, &ids)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::RootedValue value(cx);
|
|
|
|
JS::RootedId id(cx);
|
|
|
|
for (jsid idVal : ids) {
|
|
|
|
id = idVal;
|
|
|
|
if (!JS_GetPropertyById(cx, exports, id, &value) ||
|
|
|
|
!JS_SetPropertyById(cx, targetObj, id, value)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
nsresult mozJSComponentLoader::ExtractExports(
|
|
|
|
JSContext* aCx, ComponentLoaderInfo& aInfo, ModuleEntry* aMod,
|
|
|
|
JS::MutableHandleObject aExports) {
|
|
|
|
// cxhelper must be created before jsapi, so that jsapi is destroyed and
|
|
|
|
// pops any context it has pushed before we report to the caller context.
|
|
|
|
JSCLContextHelper cxhelper(aCx);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
// Even though we are calling JS_SetPropertyById on targetObj, we want
|
|
|
|
// to ensure that we never run script here, so we use an AutoJSAPI and
|
|
|
|
// not an AutoEntryScript.
|
|
|
|
dom::AutoJSAPI jsapi;
|
2017-07-19 00:47:27 +03:00
|
|
|
jsapi.Init();
|
2018-01-19 01:12:04 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
2018-08-02 09:49:10 +03:00
|
|
|
JSAutoRealm ar(cx, aMod->obj);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
RootedValue symbols(cx);
|
2018-11-30 13:46:48 +03:00
|
|
|
{
|
2018-01-19 01:12:04 +03:00
|
|
|
RootedObject obj(
|
|
|
|
cx, ResolveModuleObjectProperty(cx, aMod->obj, "EXPORTED_SYMBOLS"));
|
|
|
|
if (!obj || !JS_GetProperty(cx, obj, "EXPORTED_SYMBOLS", &symbols)) {
|
|
|
|
return ReportOnCallerUTF8(cxhelper, ERROR_NOT_PRESENT, aInfo);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
bool isArray;
|
|
|
|
if (!JS_IsArrayObject(cx, symbols, &isArray)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
if (!isArray) {
|
|
|
|
return ReportOnCallerUTF8(cxhelper, ERROR_NOT_AN_ARRAY, aInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
RootedObject symbolsObj(cx, &symbols.toObject());
|
|
|
|
|
|
|
|
// Iterate over symbols array, installing symbols on targetObj:
|
|
|
|
|
|
|
|
uint32_t symbolCount = 0;
|
|
|
|
if (!JS_GetArrayLength(cx, symbolsObj, &symbolCount)) {
|
|
|
|
return ReportOnCallerUTF8(cxhelper, ERROR_GETTING_ARRAY_LENGTH, aInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoCString logBuffer;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
aExports.set(JS_NewPlainObject(cx));
|
|
|
|
if (!aExports) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool missing = false;
|
|
|
|
|
|
|
|
RootedValue value(cx);
|
|
|
|
RootedId symbolId(cx);
|
|
|
|
RootedObject symbolHolder(cx);
|
|
|
|
for (uint32_t i = 0; i < symbolCount; ++i) {
|
|
|
|
if (!JS_GetElement(cx, symbolsObj, i, &value) || !value.isString() ||
|
|
|
|
!JS_ValueToId(cx, value, &symbolId)) {
|
|
|
|
return ReportOnCallerUTF8(cxhelper, ERROR_ARRAY_ELEMENT, aInfo, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
symbolHolder = ResolveModuleObjectPropertyById(cx, aMod->obj, symbolId);
|
|
|
|
if (!symbolHolder ||
|
|
|
|
!JS_GetPropertyById(cx, symbolHolder, symbolId, &value)) {
|
|
|
|
RootedString symbolStr(cx, JSID_TO_STRING(symbolId));
|
2018-09-05 12:25:42 +03:00
|
|
|
JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, symbolStr);
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!bytes) {
|
2018-01-19 01:12:04 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
return ReportOnCallerUTF8(cxhelper, ERROR_GETTING_SYMBOL, aInfo,
|
2018-09-05 12:25:42 +03:00
|
|
|
bytes.get());
|
2018-01-19 01:12:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.isUndefined()) {
|
|
|
|
missing = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetPropertyById(cx, aExports, symbolId, value)) {
|
|
|
|
RootedString symbolStr(cx, JSID_TO_STRING(symbolId));
|
2018-09-05 12:25:42 +03:00
|
|
|
JS::UniqueChars bytes = JS_EncodeStringToUTF8(cx, symbolStr);
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!bytes) {
|
2018-01-19 01:12:04 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
return ReportOnCallerUTF8(cxhelper, ERROR_GETTING_SYMBOL, aInfo,
|
2018-09-05 12:25:42 +03:00
|
|
|
bytes.get());
|
2018-01-19 01:12:04 +03:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (i == 0) {
|
|
|
|
logBuffer.AssignLiteral("Installing symbols [ ");
|
|
|
|
}
|
|
|
|
JS::UniqueChars bytes =
|
|
|
|
JS_EncodeStringToLatin1(cx, JSID_TO_STRING(symbolId));
|
|
|
|
if (!!bytes) {
|
|
|
|
logBuffer.Append(bytes.get());
|
|
|
|
}
|
|
|
|
logBuffer.Append(' ');
|
|
|
|
if (i == symbolCount - 1) {
|
|
|
|
nsCString location;
|
|
|
|
MOZ_TRY(aInfo.GetLocation(location));
|
|
|
|
LOG(("%s] from %s\n", logBuffer.get(), location.get()));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
// Don't cache the exports object if any of its exported symbols are
|
|
|
|
// missing. If the module hasn't finished loading yet, they may be
|
|
|
|
// defined the next time we try to import it.
|
2014-05-13 01:37:46 +04:00
|
|
|
if (!missing) {
|
2018-01-19 01:12:04 +03:00
|
|
|
aMod->exports = aExports;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult mozJSComponentLoader::Import(JSContext* aCx,
|
|
|
|
const nsACString& aLocation,
|
|
|
|
JS::MutableHandleObject aModuleGlobal,
|
|
|
|
JS::MutableHandleObject aModuleExports,
|
|
|
|
bool aIgnoreExports) {
|
2013-04-23 19:48:05 +04:00
|
|
|
nsresult rv;
|
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
|
|
|
|
2019-02-11 20:53:22 +03:00
|
|
|
AUTO_PROFILER_TEXT_MARKER_CAUSE("ChromeUtils.import", aLocation, JS,
|
|
|
|
profiler_get_backtrace());
|
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
ComponentLoaderInfo info(aLocation);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
rv = info.EnsureKey();
|
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;
|
2014-05-13 01:37:46 +04:00
|
|
|
if (!mImports.Get(info.Key(), &mod) &&
|
|
|
|
!mInProgressImports.Get(info.Key(), &mod)) {
|
2018-01-19 01:12:04 +03:00
|
|
|
newEntry = new ModuleEntry(RootingContext::get(aCx));
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!newEntry) {
|
2007-05-16 03:27:40 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2017-07-22 01:11:02 +03:00
|
|
|
|
2018-02-21 19:54:17 +03:00
|
|
|
// Note: This implies EnsureURI().
|
|
|
|
MOZ_TRY(info.EnsureResolvedURI());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-07-22 01:11:02 +03:00
|
|
|
// get the JAR if there is one
|
|
|
|
nsCOMPtr<nsIJARURI> jarURI;
|
|
|
|
jarURI = do_QueryInterface(info.ResolvedURI(), &rv);
|
|
|
|
nsCOMPtr<nsIFileURL> baseFileURL;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
|
|
|
while (jarURI) {
|
|
|
|
jarURI->GetJARFile(getter_AddRefs(baseURI));
|
|
|
|
jarURI = do_QueryInterface(baseURI, &rv);
|
|
|
|
}
|
|
|
|
baseFileURL = do_QueryInterface(baseURI, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
baseFileURL = do_QueryInterface(info.ResolvedURI(), &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> sourceFile;
|
|
|
|
rv = baseFileURL->GetFile(getter_AddRefs(sourceFile));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-07-25 06:32:42 +03:00
|
|
|
rv = info.ResolvedURI()->GetSpec(newEntry->resolvedURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCString* existingPath;
|
|
|
|
if (mLocations.Get(newEntry->resolvedURL, &existingPath) &&
|
|
|
|
*existingPath != info.Key()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLocations.Put(newEntry->resolvedURL, new nsCString(info.Key()));
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
RootedValue exception(aCx);
|
2018-02-21 19:54:17 +03:00
|
|
|
{
|
|
|
|
mInProgressImports.Put(info.Key(), newEntry);
|
|
|
|
auto cleanup =
|
|
|
|
MakeScopeExit([&]() { mInProgressImports.Remove(info.Key()); });
|
|
|
|
|
|
|
|
rv = ObjectForLocation(info, sourceFile, &newEntry->obj,
|
|
|
|
&newEntry->thisObjectKey, &newEntry->location,
|
|
|
|
true, &exception);
|
|
|
|
}
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-04-23 19:48:05 +04:00
|
|
|
if (!exception.isUndefined()) {
|
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.
|
2018-09-12 21:14:49 +03:00
|
|
|
if (!JS_WrapValue(aCx, &exception)) {
|
2012-02-24 01:50:01 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
JS_SetPendingException(aCx, exception);
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2008-08-04 21:03:34 +04:00
|
|
|
// 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;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2007-06-20 10:29:49 +04:00
|
|
|
|
2018-01-05 00:17:38 +03:00
|
|
|
#ifdef STARTUP_RECORDER_ENABLED
|
2017-09-13 22:19:53 +03:00
|
|
|
if (Preferences::GetBool("browser.startup.record", false)) {
|
2018-01-19 01:12:04 +03:00
|
|
|
newEntry->importStack = xpc_PrintJSStack(aCx, false, false, false).get();
|
2017-09-13 22:19:53 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-16 03:27:40 +04:00
|
|
|
mod = newEntry;
|
|
|
|
}
|
|
|
|
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(mod->obj, "Import table contains entry with no object");
|
2018-01-19 01:12:04 +03:00
|
|
|
aModuleGlobal.set(mod->obj);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
JS::RootedObject exports(aCx, mod->exports);
|
|
|
|
if (!exports && !aIgnoreExports) {
|
|
|
|
MOZ_TRY(ExtractExports(aCx, info, mod, &exports));
|
|
|
|
}
|
2007-05-16 03:27:40 +04:00
|
|
|
|
2018-01-19 01:12:04 +03:00
|
|
|
if (exports && !JS_WrapObject(aCx, &exports)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-05-16 03:27:40 +04:00
|
|
|
}
|
2018-01-19 01:12:04 +03:00
|
|
|
aModuleExports.set(exports);
|
2007-05-16 03:27:40 +04:00
|
|
|
|
|
|
|
// Cache this module for later
|
|
|
|
if (newEntry) {
|
2014-05-13 01:37:46 +04:00
|
|
|
mImports.Put(info.Key(), newEntry);
|
2007-05-16 03:27:40 +04:00
|
|
|
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
|
|
|
nsresult mozJSComponentLoader::Unload(const nsACString& aLocation) {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mInitialized) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-13 01:37:46 +04:00
|
|
|
ComponentLoaderInfo info(aLocation);
|
|
|
|
rv = info.EnsureKey();
|
2011-06-15 22:08:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
ModuleEntry* mod;
|
2014-05-13 01:37:46 +04:00
|
|
|
if (mImports.Get(info.Key(), &mod)) {
|
2017-07-25 06:32:42 +03:00
|
|
|
mLocations.Remove(mod->resolvedURL);
|
2014-05-13 01:37:46 +04:00
|
|
|
mImports.Remove(info.Key());
|
2011-06-15 22:08:43 +04:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:47:27 +03:00
|
|
|
// If this is the last module to be unloaded, we will leak mLoaderGlobal
|
|
|
|
// until UnloadModules is called. So be it.
|
|
|
|
|
2011-06-15 22:08:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:54:30 +04:00
|
|
|
size_t mozJSComponentLoader::ModuleEntry::SizeOfIncludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = aMallocSizeOf(this);
|
|
|
|
n += aMallocSizeOf(location);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:04 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<nsIFactory> mozJSComponentLoader::ModuleEntry::GetFactory(
|
2010-06-12 00:13:26 +04:00
|
|
|
const mozilla::Module& module, const mozilla::Module::CIDEntry& entry) {
|
|
|
|
const ModuleEntry& self = static_cast<const ModuleEntry&>(module);
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(self.getfactoryobj, "Handing out an uninitialized module?");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-06-12 00:13:26 +04:00
|
|
|
nsCOMPtr<nsIFactory> f;
|
|
|
|
nsresult rv = self.getfactoryobj->Get(*entry.cid, getter_AddRefs(f));
|
2018-09-12 21:14:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-06-12 11:00:09 +04:00
|
|
|
return nullptr;
|
2018-09-12 21:14:49 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-06-12 00:13:26 +04:00
|
|
|
return f.forget();
|
|
|
|
}
|
|
|
|
|
1999-10-01 01:47:04 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2013-04-29 22:16:19 +04:00
|
|
|
JSCLContextHelper::JSCLContextHelper(JSContext* aCx)
|
|
|
|
: mContext(aCx), mBuf(nullptr) {}
|
2000-09-21 08:30:32 +04:00
|
|
|
|
2011-05-13 19:56:26 +04:00
|
|
|
JSCLContextHelper::~JSCLContextHelper() {
|
|
|
|
if (mBuf) {
|
2017-03-04 01:10:11 +03:00
|
|
|
JS_ReportErrorUTF8(mContext, "%s", mBuf.get());
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|
2011-05-13 19:56:26 +04:00
|
|
|
}
|
|
|
|
|
2017-03-04 01:10:11 +03:00
|
|
|
void JSCLContextHelper::reportErrorAfterPop(UniqueChars&& buf) {
|
2013-08-14 11:00:34 +04:00
|
|
|
MOZ_ASSERT(!mBuf, "Already called reportErrorAfterPop");
|
2018-05-30 22:15:35 +03:00
|
|
|
mBuf = std::move(buf);
|
2009-02-18 07:11:09 +03:00
|
|
|
}
|