2013-06-23 11:15:10 +04: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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
|
|
|
|
2015-07-09 22:32:04 +03:00
|
|
|
#include "js/Class.h"
|
2015-02-10 21:58:18 +03:00
|
|
|
#include "js/Proxy.h"
|
2017-04-28 00:10:15 +03:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2013-09-10 19:29:43 +04:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2013-06-23 11:15:10 +04:00
|
|
|
#include "nsCycleCollectionTraversalCallback.h"
|
2013-06-23 11:15:42 +04:00
|
|
|
#include "nsCycleCollector.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2015-07-09 22:32:04 +03:00
|
|
|
#ifdef DEBUG
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
bool nsWrapperCache::HasJSObjectMovedOp(JSObject* aWrapper) {
|
2015-07-09 22:32:04 +03:00
|
|
|
return js::HasObjectMovedOp(aWrapper);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-23 11:15:42 +04:00
|
|
|
void nsWrapperCache::HoldJSObjects(void* aScriptObjectHolder,
|
|
|
|
nsScriptObjectTracer* aTracer) {
|
2013-08-17 00:10:17 +04:00
|
|
|
cyclecollector::HoldJSObjectsImpl(aScriptObjectHolder, aTracer);
|
2015-12-31 16:21:49 +03:00
|
|
|
if (mWrapper && !JS::ObjectIsTenured(mWrapper)) {
|
2017-04-28 00:10:15 +03:00
|
|
|
CycleCollectedJSRuntime::Get()->NurseryWrapperPreserved(mWrapper);
|
2015-12-31 16:21:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsWrapperCache::SetWrapperJSObject(JSObject* aWrapper) {
|
|
|
|
mWrapper = aWrapper;
|
2018-04-05 18:22:41 +03:00
|
|
|
UnsetWrapperFlags(kWrapperFlagsMask);
|
2015-12-31 16:21:49 +03:00
|
|
|
|
|
|
|
if (aWrapper && !JS::ObjectIsTenured(aWrapper)) {
|
2017-04-28 00:10:15 +03:00
|
|
|
CycleCollectedJSRuntime::Get()->NurseryWrapperAdded(this);
|
2015-12-31 16:21:49 +03:00
|
|
|
}
|
2018-07-23 17:46:37 +03:00
|
|
|
|
|
|
|
if (mozilla::recordreplay::IsReplaying()) {
|
|
|
|
mozilla::recordreplay::SetWeakPointerJSRoot(this, aWrapper);
|
|
|
|
}
|
2013-06-23 11:15:42 +04:00
|
|
|
}
|
2013-06-23 11:15:10 +04:00
|
|
|
|
2013-08-02 11:04:01 +04:00
|
|
|
void nsWrapperCache::ReleaseWrapper(void* aScriptObjectHolder) {
|
|
|
|
if (PreservingWrapper()) {
|
|
|
|
SetPreservingWrapper(false);
|
2013-08-17 00:10:17 +04:00
|
|
|
cyclecollector::DropJSObjectsImpl(aScriptObjectHolder);
|
2013-08-02 11:04:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 11:15:10 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
|
|
|
class DebugWrapperTraversalCallback
|
|
|
|
: public nsCycleCollectionTraversalCallback {
|
|
|
|
public:
|
2014-12-05 20:38:34 +03:00
|
|
|
explicit DebugWrapperTraversalCallback(JSObject* aWrapper)
|
2016-09-24 01:42:13 +03:00
|
|
|
: mFound(false), mWrapper(JS::GCCellPtr(aWrapper)) {
|
2013-06-23 11:15:10 +04:00
|
|
|
mFlags = WANT_ALL_TRACES;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD_(void)
|
|
|
|
DescribeRefCountedNode(nsrefcnt aRefCount, const char* aObjName) override {}
|
|
|
|
NS_IMETHOD_(void)
|
|
|
|
DescribeGCedNode(bool aIsMarked, const char* aObjName,
|
2017-11-06 06:37:28 +03:00
|
|
|
uint64_t aCompartmentAddress) override {}
|
2013-06-23 11:15:10 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD_(void) NoteJSChild(const JS::GCCellPtr& aChild) override {
|
2013-06-23 11:15:10 +04:00
|
|
|
if (aChild == mWrapper) {
|
|
|
|
mFound = true;
|
|
|
|
}
|
|
|
|
}
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD_(void) NoteXPCOMChild(nsISupports* aChild) override {}
|
2013-06-23 11:15:10 +04:00
|
|
|
NS_IMETHOD_(void)
|
|
|
|
NoteNativeChild(void* aChild,
|
2017-11-06 06:37:28 +03:00
|
|
|
nsCycleCollectionParticipant* aHelper) override {}
|
2013-06-23 11:15:10 +04:00
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
NS_IMETHOD_(void) NoteNextEdgeName(const char* aName) override {}
|
2013-06-23 11:15:10 +04:00
|
|
|
|
|
|
|
bool mFound;
|
|
|
|
|
|
|
|
private:
|
2016-09-24 01:42:13 +03:00
|
|
|
JS::GCCellPtr mWrapper;
|
2013-06-23 11:15:10 +04:00
|
|
|
};
|
|
|
|
|
2014-12-05 20:38:34 +03:00
|
|
|
static void DebugWrapperTraceCallback(JS::GCCellPtr aPtr, const char* aName,
|
|
|
|
void* aClosure) {
|
2013-06-23 11:15:10 +04:00
|
|
|
DebugWrapperTraversalCallback* callback =
|
|
|
|
static_cast<DebugWrapperTraversalCallback*>(aClosure);
|
2015-07-04 01:06:23 +03:00
|
|
|
if (aPtr.is<JSObject>()) {
|
2016-09-24 01:42:13 +03:00
|
|
|
callback->NoteJSChild(aPtr);
|
2014-12-05 20:38:34 +03:00
|
|
|
}
|
2013-06-23 11:15:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsWrapperCache::CheckCCWrapperTraversal(void* aScriptObjectHolder,
|
|
|
|
nsScriptObjectTracer* aTracer) {
|
2018-07-23 17:46:37 +03:00
|
|
|
// Skip checking if we are recording or replaying, as calling
|
|
|
|
// GetWrapperPreserveColor() can cause the cache's wrapper to be cleared.
|
|
|
|
if (recordreplay::IsRecordingOrReplaying()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 14:46:31 +03:00
|
|
|
JSObject* wrapper = GetWrapperPreserveColor();
|
2013-06-23 11:15:10 +04:00
|
|
|
if (!wrapper) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugWrapperTraversalCallback callback(wrapper);
|
|
|
|
|
2014-12-05 20:38:34 +03:00
|
|
|
// The CC traversal machinery cannot trigger GC; however, the analysis cannot
|
|
|
|
// see through the COM layer, so we use a suppression to help it.
|
|
|
|
JS::AutoSuppressGCAnalysis suppress;
|
|
|
|
|
2017-01-03 22:46:49 +03:00
|
|
|
aTracer->TraverseNativeAndJS(aScriptObjectHolder, callback);
|
2013-06-23 11:15:10 +04:00
|
|
|
MOZ_ASSERT(callback.mFound,
|
|
|
|
"Cycle collection participant didn't traverse to preserved "
|
|
|
|
"wrapper! This will probably crash.");
|
|
|
|
|
|
|
|
callback.mFound = false;
|
|
|
|
aTracer->Trace(aScriptObjectHolder,
|
|
|
|
TraceCallbackFunc(DebugWrapperTraceCallback), &callback);
|
|
|
|
MOZ_ASSERT(callback.mFound,
|
|
|
|
"Cycle collection participant didn't trace preserved wrapper! "
|
|
|
|
"This will probably crash.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DEBUG
|