2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2011-09-18 13:22:17 +04:00
|
|
|
#ifndef nsJSEnvironment_h
|
|
|
|
#define nsJSEnvironment_h
|
1998-07-16 05:16:47 +04:00
|
|
|
|
|
|
|
#include "nsIScriptContext.h"
|
2012-03-24 12:18:21 +04:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
(13163, r=alecf, scc, waterson, others; names available on request)
- Fix most of bug 13163 (see TODO for rest). This entails adding a version-string argument to nsIScriptContext::EvaluateString and passing it around lots of places in content sinks.
- Fix leaks and confusion about mSecurityManager and mNameSpaceManager in nsJSEnvironment.cpp. These still need to move from nsJSContext to nsGlobalWindow or thereabouts, jband and vidur are looking at that.
- Added comments and expanded tabs in nsJSEnvironment.cpp, esp. to EvaluateString. Also changed various nsresult vars to be named rv. Also restored brace/style conformity to nsJSProtocolHandler.cpp.
- Factored CompileFunction from AddScriptEventListener to pave the way for brutal sharing of compiled JS event handlers via JS_CloneFunctionObject.
- Lots of nsCOMPtr uses added. I'm using one for mNameSpaceManager. Hold mSecurityManager as a service explicitly, on the other hand (awaiting scc's fix to allow comptrs for services), and release in nsJSContext's dtor (fixing a leak). These two managers should be moved to the window object -- TODO item below.
- Hold JSRuntimeService along with JSRuntime for live of nsJSEnvironment, fix for shaver.
- Fix window.setTimeout etc. so the filename and line number of the timeout expr is propagated. This meant factoring nsJSUtils.cpp code.
- Fix all content sinks to use the same, and up-to-date JavaScript version parsing (whether for script type or for old language attribute); also fix SplitMimeType clones to strip whitespace.
- With waterson, fix bug in brutal-sharing version of XUL content sink: script src= should not evaluate the inline content of its tag.
1999-10-31 03:43:30 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2004-02-12 01:22:05 +03:00
|
|
|
#include "prtime.h"
|
2007-03-08 14:17:16 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2012-03-11 12:53:05 +04:00
|
|
|
#include "nsIArray.h"
|
2012-06-15 06:31:55 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-06-10 02:38:50 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2014-09-09 03:30:10 +04:00
|
|
|
#include "xpcpublic.h"
|
2007-01-11 03:02:58 +03:00
|
|
|
|
2013-04-30 21:41:22 +04:00
|
|
|
class nsICycleCollectorListener;
|
2017-09-06 20:18:11 +03:00
|
|
|
class nsIDocShell;
|
2013-04-30 21:41:22 +04:00
|
|
|
|
2011-04-29 02:48:52 +04:00
|
|
|
namespace mozilla {
|
2019-09-12 12:56:52 +03:00
|
|
|
|
2011-04-29 02:48:51 +04:00
|
|
|
template <class>
|
|
|
|
class Maybe;
|
2013-11-21 02:35:16 +04:00
|
|
|
struct CycleCollectorResults;
|
1998-07-16 05:16:47 +04:00
|
|
|
|
2019-09-12 12:56:52 +03:00
|
|
|
static const uint32_t kMajorForgetSkippableCalls = 5;
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2014-11-12 22:33:50 +03:00
|
|
|
|
2013-08-15 01:00:03 +04:00
|
|
|
class nsJSContext : public nsIScriptContext {
|
1998-07-16 05:16:47 +04:00
|
|
|
public:
|
2013-08-20 03:24:29 +04:00
|
|
|
nsJSContext(bool aGCOnDestruction, nsIScriptGlobalObject* aGlobalObject);
|
1998-07-16 05:16:47 +04:00
|
|
|
|
2007-03-08 14:17:16 +03:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2009-05-07 22:19:36 +04:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsJSContext,
|
|
|
|
nsIScriptContext)
|
1998-07-16 05:16:47 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsIScriptGlobalObject* GetGlobalObject() override;
|
2012-12-09 21:23:19 +04:00
|
|
|
inline nsIScriptGlobalObject* GetGlobalObjectRef() {
|
|
|
|
return mGlobalObjectRef;
|
|
|
|
}
|
2012-04-12 22:21:12 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult SetProperty(JS::Handle<JSObject*> aTarget,
|
|
|
|
const char* aPropName,
|
|
|
|
nsISupports* aVal) override;
|
2006-06-13 07:07:47 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool GetProcessingScriptTag() override;
|
|
|
|
virtual void SetProcessingScriptTag(bool aResult) override;
|
2013-01-15 15:46:35 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual nsresult InitClasses(JS::Handle<JSObject*> aGlobalObj) override;
|
2005-07-31 00:57:07 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void SetWindowProxy(JS::Handle<JSObject*> aWindowProxy) override;
|
|
|
|
virtual JSObject* GetWindowProxy() override;
|
2013-09-05 01:06:55 +04:00
|
|
|
|
2012-07-01 01:16:32 +04:00
|
|
|
enum IsShrinking { ShrinkingGC, NonShrinkingGC };
|
|
|
|
|
|
|
|
enum IsIncremental { IncrementalGC, NonIncrementalGC };
|
|
|
|
|
2013-08-20 03:24:29 +04:00
|
|
|
// Setup all the statics etc - safe to call multiple times after Startup().
|
2018-05-08 18:07:33 +03:00
|
|
|
static void EnsureStatics();
|
2013-08-20 03:24:29 +04:00
|
|
|
|
2019-06-24 20:24:47 +03:00
|
|
|
static void SetLowMemoryState(bool aState);
|
|
|
|
|
2019-01-21 16:09:12 +03:00
|
|
|
static void GarbageCollectNow(JS::GCReason reason,
|
2012-07-01 01:16:32 +04:00
|
|
|
IsIncremental aIncremental = NonIncrementalGC,
|
2012-08-01 05:39:39 +04:00
|
|
|
IsShrinking aShrinking = NonShrinkingGC,
|
|
|
|
int64_t aSliceMillis = 0);
|
2013-12-22 18:58:19 +04:00
|
|
|
|
2017-07-03 22:53:54 +03:00
|
|
|
static void CycleCollectNow(nsICycleCollectorListener* aListener = nullptr);
|
2013-12-22 18:58:19 +04:00
|
|
|
|
2020-10-15 18:03:28 +03:00
|
|
|
// Finish up any in-progress incremental GC.
|
|
|
|
static void PrepareForCycleCollectionSlice(mozilla::TimeStamp aDeadline);
|
|
|
|
|
2014-03-05 04:21:48 +04:00
|
|
|
// Run a cycle collector slice, using a heuristic to decide how long to run
|
|
|
|
// it.
|
2017-06-10 02:38:50 +03:00
|
|
|
static void RunCycleCollectorSlice(mozilla::TimeStamp aDeadline);
|
2013-12-22 18:58:19 +04:00
|
|
|
|
2014-05-13 22:58:39 +04:00
|
|
|
// Run a cycle collector slice, using the given work budget.
|
|
|
|
static void RunCycleCollectorWorkSlice(int64_t aWorkBudget);
|
|
|
|
|
2013-11-21 02:35:16 +04:00
|
|
|
static void BeginCycleCollectionCallback();
|
|
|
|
static void EndCycleCollectionCallback(
|
|
|
|
mozilla::CycleCollectorResults& aResults);
|
2011-02-17 02:47:12 +03:00
|
|
|
|
2014-06-15 03:43:45 +04:00
|
|
|
// Return the longest CC slice time since ClearMaxCCSliceTime() was last
|
|
|
|
// called.
|
|
|
|
static uint32_t GetMaxCCSliceTimeSinceClear();
|
|
|
|
static void ClearMaxCCSliceTime();
|
|
|
|
|
2017-09-06 20:18:11 +03:00
|
|
|
// If there is some pending CC or GC timer/runner, this will run it.
|
|
|
|
static void RunNextCollectorTimer(
|
2019-01-21 16:09:12 +03:00
|
|
|
JS::GCReason aReason,
|
2017-09-06 20:18:11 +03:00
|
|
|
mozilla::TimeStamp aDeadline = mozilla::TimeStamp());
|
|
|
|
// If user has been idle and aDocShell is for an iframe being loaded in an
|
|
|
|
// already loaded top level docshell, this will run a CC or GC
|
|
|
|
// timer/runner if there is such pending.
|
|
|
|
static void MaybeRunNextCollectorSlice(nsIDocShell* aDocShell,
|
2019-01-21 16:09:12 +03:00
|
|
|
JS::GCReason aReason);
|
2014-04-26 00:21:04 +04:00
|
|
|
|
2017-01-23 20:22:26 +03:00
|
|
|
// The GC should probably run soon, in the zone of object aObj (if given).
|
2019-08-26 18:51:01 +03:00
|
|
|
static void PokeGC(JS::GCReason aReason, JSObject* aObj, uint32_t aDelay = 0);
|
2011-02-17 02:47:12 +03:00
|
|
|
static void KillGCTimer();
|
|
|
|
|
2015-02-24 12:40:02 +03:00
|
|
|
static void PokeShrinkingGC();
|
|
|
|
static void KillShrinkingGCTimer();
|
|
|
|
|
2011-02-17 02:47:12 +03:00
|
|
|
static void MaybePokeCC();
|
2020-12-04 19:03:07 +03:00
|
|
|
static void EnsureCCRunner(mozilla::TimeDuration aDelay,
|
|
|
|
mozilla::TimeDuration aBudget);
|
2017-06-10 02:38:50 +03:00
|
|
|
static void KillCCRunner();
|
2012-05-09 22:53:23 +04:00
|
|
|
static void KillFullGCTimer();
|
2017-06-10 02:38:50 +03:00
|
|
|
static void KillInterSliceGCRunner();
|
2011-02-17 02:47:12 +03:00
|
|
|
|
2013-03-07 22:53:19 +04:00
|
|
|
// Calling LikelyShortLivingObjectCreated() makes a GC more likely.
|
|
|
|
static void LikelyShortLivingObjectCreated();
|
|
|
|
|
2020-10-15 18:02:12 +03:00
|
|
|
static bool HasHadCleanupSinceLastGC();
|
2012-01-31 00:06:18 +04:00
|
|
|
|
2012-01-26 19:03:21 +04:00
|
|
|
nsIScriptGlobalObject* GetCachedGlobalObject() {
|
|
|
|
// Verify that we have a global so that this
|
|
|
|
// does always return a null when GetGlobalObject() is null.
|
2013-09-05 01:06:57 +04:00
|
|
|
JSObject* global = GetWindowProxy();
|
2012-07-30 18:20:58 +04:00
|
|
|
return global ? mGlobalObjectRef.get() : nullptr;
|
2012-01-26 19:03:21 +04:00
|
|
|
}
|
2014-11-27 14:47:51 +03:00
|
|
|
|
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
|
|
|
protected:
|
2014-06-23 23:56:07 +04:00
|
|
|
virtual ~nsJSContext();
|
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
// Helper to convert xpcom datatypes to jsvals.
|
2019-04-04 23:00:08 +03:00
|
|
|
nsresult ConvertSupportsTojsvals(JSContext* aCx, nsISupports* aArgs,
|
2013-05-02 13:12:46 +04:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2019-03-26 16:58:20 +03:00
|
|
|
JS::MutableHandleVector<JS::Value> aArgsOut);
|
2006-06-13 07:07:47 +04:00
|
|
|
|
2019-04-04 23:00:08 +03:00
|
|
|
nsresult AddSupportsPrimitiveTojsvals(JSContext* aCx, nsISupports* aArg,
|
|
|
|
JS::Value* aArgv);
|
2006-06-13 07:07:47 +04:00
|
|
|
|
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
|
|
|
private:
|
2016-06-02 17:34:40 +03:00
|
|
|
void Destroy();
|
2009-05-07 22:19:36 +04:00
|
|
|
|
2013-09-05 01:06:56 +04:00
|
|
|
JS::Heap<JSObject*> mWindowProxy;
|
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
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mGCOnDestruction;
|
2013-01-15 15:46:35 +04:00
|
|
|
bool mProcessingScriptTag;
|
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
|
|
|
|
2010-09-04 01:15:50 +04:00
|
|
|
// mGlobalObjectRef ensures that the outer window stays alive as long as the
|
|
|
|
// context does. It is eventually collected by the cycle collector.
|
2011-04-29 00:28:54 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> mGlobalObjectRef;
|
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
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool DOMOperationCallback(JSContext* cx);
|
1998-07-16 05:16:47 +04:00
|
|
|
};
|
|
|
|
|
2013-08-20 03:24:29 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-08-20 03:24:29 +04:00
|
|
|
|
2019-09-09 03:41:46 +03:00
|
|
|
class SerializedStackHolder;
|
|
|
|
|
2013-08-20 03:24:29 +04:00
|
|
|
void StartupJSEnvironment();
|
|
|
|
void ShutdownJSEnvironment();
|
|
|
|
|
2013-08-29 08:30:06 +04:00
|
|
|
// Runnable that's used to do async error reporting
|
2016-04-26 03:23:21 +03:00
|
|
|
class AsyncErrorReporter final : public mozilla::Runnable {
|
2013-08-29 08:30:06 +04:00
|
|
|
public:
|
2019-09-09 03:41:46 +03:00
|
|
|
explicit AsyncErrorReporter(xpc::ErrorReport* aReport);
|
|
|
|
// SerializeStack is suitable for main or worklet thread use.
|
|
|
|
// Stacks from worker threads are not supported.
|
|
|
|
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1578968
|
|
|
|
void SerializeStack(JSContext* aCx, JS::Handle<JSObject*> aStack);
|
2019-09-06 05:10:32 +03:00
|
|
|
|
2020-05-08 23:54:17 +03:00
|
|
|
// Set the exception value associated with this error report.
|
|
|
|
// Should only be called from the main thread.
|
|
|
|
void SetException(JSContext* aCx, JS::Handle<JS::Value> aException);
|
|
|
|
|
2019-09-09 03:37:09 +03:00
|
|
|
protected:
|
2019-09-09 03:41:46 +03:00
|
|
|
NS_IMETHOD Run() override;
|
|
|
|
|
2020-05-08 23:54:17 +03:00
|
|
|
// This is only used on main thread!
|
|
|
|
JS::PersistentRootedValue mException;
|
|
|
|
bool mHasException = false;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<xpc::ErrorReport> mReport;
|
2019-09-09 03:41:46 +03:00
|
|
|
// This may be used to marshal a stack from an arbitrary thread/runtime into
|
|
|
|
// the main thread/runtime where the console service runs.
|
|
|
|
UniquePtr<SerializedStackHolder> mStackHolder;
|
2013-08-29 08:30:06 +04:00
|
|
|
};
|
|
|
|
|
2013-08-20 03:24:29 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
1998-07-16 05:16:47 +04:00
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
// An interface for fast and native conversion to/from nsIArray. If an object
|
|
|
|
// supports this interface, JS can reach directly in for the argv, and avoid
|
|
|
|
// nsISupports conversion. If this interface is not supported, the object will
|
|
|
|
// be queried for nsIArray, and everything converted via xpcom objects.
|
|
|
|
#define NS_IJSARGARRAY_IID \
|
2018-11-30 13:46:48 +03:00
|
|
|
{ \
|
2012-03-11 12:53:05 +04:00
|
|
|
0xb6acdac8, 0xf5c6, 0x432c, { \
|
|
|
|
0xa8, 0x6e, 0x33, 0xee, 0xb1, 0xb0, 0xcd, 0xdc \
|
|
|
|
} \
|
|
|
|
}
|
2001-11-27 12:46:38 +03:00
|
|
|
|
2012-03-11 12:53:05 +04:00
|
|
|
class nsIJSArgArray : public nsIArray {
|
2006-06-13 07:07:47 +04:00
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IJSARGARRAY_IID)
|
|
|
|
// Bug 312003 describes why this must be "void **", but after calling argv
|
2013-04-12 02:52:10 +04:00
|
|
|
// may be cast to JS::Value* and the args found at:
|
|
|
|
// ((JS::Value*)argv)[0], ..., ((JS::Value*)argv)[argc - 1]
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual nsresult GetArgs(uint32_t* argc, void** argv) = 0;
|
1998-07-16 05:16:47 +04:00
|
|
|
};
|
|
|
|
|
2006-06-13 07:07:47 +04:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIJSArgArray, NS_IJSARGARRAY_IID)
|
|
|
|
|
2011-09-18 13:22:17 +04:00
|
|
|
#endif /* nsJSEnvironment_h */
|