2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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 02:22:17 -07:00
|
|
|
#ifndef nsJSEnvironment_h
|
|
|
|
#define nsJSEnvironment_h
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIScriptContext.h"
|
2012-03-24 01:18:21 -07:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2012-01-25 10:59:55 -08:00
|
|
|
#include "nsIXPConnect.h"
|
2012-03-11 00:53:05 -08:00
|
|
|
#include "nsIArray.h"
|
2012-06-14 19:31:55 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-08 16:30:10 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-08-28 21:30:06 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2014-09-08 16:30:10 -07:00
|
|
|
#include "xpcpublic.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-30 10:41:22 -07:00
|
|
|
class nsICycleCollectorListener;
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsIXPConnectJSObjectHolder;
|
2011-10-03 12:11:31 -07:00
|
|
|
class nsScriptNameSpaceManager;
|
2013-09-04 14:06:54 -07:00
|
|
|
class nsCycleCollectionNoteRootCallback;
|
2013-04-30 10:41:22 -07:00
|
|
|
|
2014-02-11 02:59:15 -08:00
|
|
|
namespace JS {
|
|
|
|
class AutoValueVector;
|
|
|
|
}
|
|
|
|
|
2011-04-28 15:48:52 -07:00
|
|
|
namespace mozilla {
|
2011-04-28 15:48:51 -07:00
|
|
|
template <class> class Maybe;
|
2013-11-20 14:35:16 -08:00
|
|
|
struct CycleCollectorResults;
|
2010-03-29 22:51:52 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-12 16:25:03 -07:00
|
|
|
// The amount of time we wait between a request to GC (due to leaving
|
|
|
|
// a page) and doing the actual GC.
|
|
|
|
#define NS_GC_DELAY 4000 // ms
|
|
|
|
|
2014-11-12 11:33:50 -08:00
|
|
|
#define NS_MAJOR_FORGET_SKIPPABLE_CALLS 5
|
|
|
|
|
2013-08-14 14:00:03 -07:00
|
|
|
class nsJSContext : public nsIScriptContext
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-08-19 16:24:29 -07:00
|
|
|
nsJSContext(bool aGCOnDestruction, nsIScriptGlobalObject* aGlobalObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2009-05-07 11:19:36 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsJSContext,
|
|
|
|
nsIScriptContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual nsIScriptGlobalObject *GetGlobalObject() MOZ_OVERRIDE;
|
2012-12-09 09:23:19 -08:00
|
|
|
inline nsIScriptGlobalObject *GetGlobalObjectRef() { return mGlobalObjectRef; }
|
2012-04-12 11:21:12 -07:00
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual JSContext* GetNativeContext() MOZ_OVERRIDE;
|
|
|
|
virtual nsresult InitContext() MOZ_OVERRIDE;
|
|
|
|
virtual bool IsContextInitialized() MOZ_OVERRIDE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual nsresult SetProperty(JS::Handle<JSObject*> aTarget, const char* aPropName, nsISupports* aVal) MOZ_OVERRIDE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual bool GetProcessingScriptTag() MOZ_OVERRIDE;
|
|
|
|
virtual void SetProcessingScriptTag(bool aResult) MOZ_OVERRIDE;
|
2013-01-15 03:46:35 -08:00
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual nsresult InitClasses(JS::Handle<JSObject*> aGlobalObj) MOZ_OVERRIDE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-06-05 09:15:48 -07:00
|
|
|
virtual void WillInitializeContext() MOZ_OVERRIDE;
|
|
|
|
virtual void DidInitializeContext() MOZ_OVERRIDE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-04 14:06:55 -07:00
|
|
|
virtual void SetWindowProxy(JS::Handle<JSObject*> aWindowProxy) MOZ_OVERRIDE;
|
|
|
|
virtual JSObject* GetWindowProxy() MOZ_OVERRIDE;
|
|
|
|
virtual JSObject* GetWindowProxyPreserveColor() MOZ_OVERRIDE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static void LoadStart();
|
2007-10-21 09:46:54 -07:00
|
|
|
static void LoadEnd();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-30 14:16:32 -07:00
|
|
|
enum IsShrinking {
|
|
|
|
ShrinkingGC,
|
|
|
|
NonShrinkingGC
|
|
|
|
};
|
|
|
|
|
|
|
|
enum IsIncremental {
|
|
|
|
IncrementalGC,
|
|
|
|
NonIncrementalGC
|
|
|
|
};
|
|
|
|
|
2013-08-19 16:24:29 -07:00
|
|
|
// Setup all the statics etc - safe to call multiple times after Startup().
|
|
|
|
void EnsureStatics();
|
|
|
|
|
2013-01-27 12:35:12 -08:00
|
|
|
static void GarbageCollectNow(JS::gcreason::Reason reason,
|
2012-06-30 14:16:32 -07:00
|
|
|
IsIncremental aIncremental = NonIncrementalGC,
|
2012-07-31 18:39:39 -07:00
|
|
|
IsShrinking aShrinking = NonShrinkingGC,
|
|
|
|
int64_t aSliceMillis = 0);
|
2011-12-27 03:59:29 -08:00
|
|
|
static void ShrinkGCBuffersNow();
|
2013-12-22 06:58:19 -08:00
|
|
|
|
2012-01-30 12:06:18 -08:00
|
|
|
// If aExtraForgetSkippableCalls is -1, forgetSkippable won't be
|
|
|
|
// called even if the previous collection was GC.
|
2012-07-30 07:20:58 -07:00
|
|
|
static void CycleCollectNow(nsICycleCollectorListener *aListener = nullptr,
|
2013-11-20 14:35:17 -08:00
|
|
|
int32_t aExtraForgetSkippableCalls = 0);
|
2013-12-22 06:58:19 -08:00
|
|
|
|
2014-03-04 16:21:48 -08:00
|
|
|
// Run a cycle collector slice, using a heuristic to decide how long to run it.
|
|
|
|
static void RunCycleCollectorSlice();
|
2013-12-22 06:58:19 -08:00
|
|
|
|
2014-05-13 11:58:39 -07:00
|
|
|
// Run a cycle collector slice, using the given work budget.
|
|
|
|
static void RunCycleCollectorWorkSlice(int64_t aWorkBudget);
|
|
|
|
|
2013-11-20 14:35:16 -08:00
|
|
|
static void BeginCycleCollectionCallback();
|
|
|
|
static void EndCycleCollectionCallback(mozilla::CycleCollectorResults &aResults);
|
2011-02-16 15:47:12 -08:00
|
|
|
|
2014-06-14 16:43:45 -07:00
|
|
|
// Return the longest CC slice time since ClearMaxCCSliceTime() was last called.
|
|
|
|
static uint32_t GetMaxCCSliceTimeSinceClear();
|
|
|
|
static void ClearMaxCCSliceTime();
|
|
|
|
|
2014-04-25 13:21:04 -07:00
|
|
|
static void RunNextCollectorTimer();
|
|
|
|
|
2013-01-27 12:35:12 -08:00
|
|
|
static void PokeGC(JS::gcreason::Reason aReason, int aDelay = 0);
|
2011-02-16 15:47:12 -08:00
|
|
|
static void KillGCTimer();
|
|
|
|
|
2011-12-27 03:59:29 -08:00
|
|
|
static void PokeShrinkGCBuffers();
|
|
|
|
static void KillShrinkGCBuffersTimer();
|
|
|
|
|
2011-02-16 15:47:12 -08:00
|
|
|
static void MaybePokeCC();
|
|
|
|
static void KillCCTimer();
|
2013-12-21 06:35:08 -08:00
|
|
|
static void KillICCTimer();
|
2012-05-09 11:53:23 -07:00
|
|
|
static void KillFullGCTimer();
|
2012-06-10 21:27:59 -07:00
|
|
|
static void KillInterSliceGCTimer();
|
2011-02-16 15:47:12 -08:00
|
|
|
|
2013-03-07 10:53:19 -08:00
|
|
|
// Calling LikelyShortLivingObjectCreated() makes a GC more likely.
|
|
|
|
static void LikelyShortLivingObjectCreated();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint32_t CleanupsSinceLastGC();
|
2012-01-30 12:06:18 -08:00
|
|
|
|
2012-01-26 07:03:21 -08:00
|
|
|
nsIScriptGlobalObject* GetCachedGlobalObject()
|
|
|
|
{
|
|
|
|
// Verify that we have a global so that this
|
|
|
|
// does always return a null when GetGlobalObject() is null.
|
2013-09-04 14:06:57 -07:00
|
|
|
JSObject* global = GetWindowProxy();
|
2012-07-30 07:20:58 -07:00
|
|
|
return global ? mGlobalObjectRef.get() : nullptr;
|
2012-01-26 07:03:21 -08:00
|
|
|
}
|
2014-11-27 03:47:51 -08:00
|
|
|
|
|
|
|
static void NotifyDidPaint();
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
2014-06-23 12:56:07 -07:00
|
|
|
virtual ~nsJSContext();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Helper to convert xpcom datatypes to jsvals.
|
2010-01-25 08:48:07 -08:00
|
|
|
nsresult ConvertSupportsTojsvals(nsISupports *aArgs,
|
2013-05-02 02:12:46 -07:00
|
|
|
JS::Handle<JSObject*> aScope,
|
2014-02-11 02:59:15 -08:00
|
|
|
JS::AutoValueVector &aArgsOut);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-04-11 15:52:10 -07:00
|
|
|
nsresult AddSupportsPrimitiveTojsvals(nsISupports *aArg, JS::Value *aArgv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2009-05-07 11:19:36 -07:00
|
|
|
void DestroyJSContext();
|
|
|
|
|
|
|
|
nsrefcnt GetCCRefcnt();
|
2007-11-01 15:51:57 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSContext *mContext;
|
2013-09-04 14:06:56 -07:00
|
|
|
JS::Heap<JSObject*> mWindowProxy;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsInitialized;
|
|
|
|
bool mGCOnDestruction;
|
2013-01-15 03:46:35 -08:00
|
|
|
bool mProcessingScriptTag;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-06-11 17:52:47 -07:00
|
|
|
PRTime mModalStateTime;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mModalStateDepth;
|
2009-06-11 17:52:47 -07:00
|
|
|
|
2010-09-03 14:15:50 -07: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-28 13:28:54 -07:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> mGlobalObjectRef;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-10 15:10:01 -08:00
|
|
|
static void JSOptionChangedCallback(const char *pref, void *data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
static bool DOMOperationCallback(JSContext *cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsIJSRuntimeService;
|
2013-08-28 21:30:06 -07:00
|
|
|
class nsIPrincipal;
|
|
|
|
class nsPIDOMWindow;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-19 16:24:29 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-08-19 16:24:29 -07:00
|
|
|
|
2013-08-19 16:24:29 -07:00
|
|
|
void StartupJSEnvironment();
|
|
|
|
void ShutdownJSEnvironment();
|
|
|
|
|
|
|
|
// Get the NameSpaceManager, creating if necessary
|
|
|
|
nsScriptNameSpaceManager* GetNameSpaceManager();
|
|
|
|
|
2013-08-28 21:30:06 -07:00
|
|
|
// Runnable that's used to do async error reporting
|
|
|
|
class AsyncErrorReporter : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// aWindow may be null if this error report is not associated with a window
|
2014-09-08 16:30:10 -07:00
|
|
|
AsyncErrorReporter(JSRuntime* aRuntime, xpc::ErrorReport* aReport)
|
|
|
|
: mReport(aReport)
|
|
|
|
{}
|
2013-08-28 21:30:06 -07:00
|
|
|
|
2015-01-15 13:13:53 -08:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2013-08-28 21:30:06 -07:00
|
|
|
{
|
2014-09-08 16:30:11 -07:00
|
|
|
mReport->LogToConsole();
|
2013-08-28 21:30:06 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-09-08 16:30:10 -07:00
|
|
|
nsRefPtr<xpc::ErrorReport> mReport;
|
2013-08-28 21:30:06 -07:00
|
|
|
};
|
|
|
|
|
2013-08-19 16:24:29 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2007-03-22 10:30:00 -07: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 \
|
2012-03-11 00:53:05 -08:00
|
|
|
{ 0xb6acdac8, 0xf5c6, 0x432c, \
|
|
|
|
{ 0xa8, 0x6e, 0x33, 0xee, 0xb1, 0xb0, 0xcd, 0xdc } }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-11 00:53:05 -08:00
|
|
|
class nsIJSArgArray : public nsIArray
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IJSARGARRAY_IID)
|
|
|
|
// Bug 312003 describes why this must be "void **", but after calling argv
|
2013-04-11 15:52:10 -07:00
|
|
|
// may be cast to JS::Value* and the args found at:
|
|
|
|
// ((JS::Value*)argv)[0], ..., ((JS::Value*)argv)[argc - 1]
|
2012-08-22 08:56:38 -07:00
|
|
|
virtual nsresult GetArgs(uint32_t *argc, void **argv) = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(nsIJSArgArray, NS_IJSARGARRAY_IID)
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
JSObject* NS_DOMReadStructuredClone(JSContext* cx,
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
JSStructuredCloneReader* reader, uint32_t tag,
|
|
|
|
uint32_t data, void* closure);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-08-08 15:53:04 -07:00
|
|
|
bool NS_DOMWriteStructuredClone(JSContext* cx,
|
|
|
|
JSStructuredCloneWriter* writer,
|
|
|
|
JS::Handle<JSObject*> obj, void *closure);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
Bug 708735 - Use <stdint.h> types in JSAPI and throughout SpiderMonkey. Continue to provide the {u,}int{8,16,32,64} and JS{Uint,Int}{8,16,32,64} integer types through a single header, however, for a simpler backout strategy -- and also to ease the transition for embedders. r=timeless on switching the jsd API to use the <stdint.h> types, r=luke, r=dmandelin
2011-12-08 19:54:10 -08:00
|
|
|
void NS_DOMStructuredCloneError(JSContext* cx, uint32_t errorid);
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2011-09-18 02:22:17 -07:00
|
|
|
#endif /* nsJSEnvironment_h */
|