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:
|
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/. */
|
2012-01-21 07:49:33 +04:00
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#ifndef js_RootingAPI_h
|
|
|
|
#define js_RootingAPI_h
|
2012-01-21 07:49:33 +04:00
|
|
|
|
2013-11-23 00:05:04 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-08-01 01:43:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2012-09-10 21:31:58 +04:00
|
|
|
#include "mozilla/GuardObjects.h"
|
2013-11-05 01:35:08 +04:00
|
|
|
#include "mozilla/LinkedList.h"
|
2015-07-20 18:20:35 +03:00
|
|
|
#include "mozilla/Move.h"
|
2012-12-27 21:20:22 +04:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2012-06-20 02:01:58 +04:00
|
|
|
|
2016-08-25 00:21:42 +03:00
|
|
|
#include <type_traits>
|
|
|
|
|
2012-09-10 21:31:58 +04:00
|
|
|
#include "jspubtd.h"
|
2012-04-13 05:15:26 +04:00
|
|
|
|
2015-09-29 23:39:33 +03:00
|
|
|
#include "js/GCAnnotations.h"
|
2016-01-26 23:53:35 +03:00
|
|
|
#include "js/GCPolicyAPI.h"
|
2014-05-01 20:26:12 +04:00
|
|
|
#include "js/HeapAPI.h"
|
2018-01-05 16:35:00 +03:00
|
|
|
#include "js/ProfilingStack.h"
|
2018-05-17 19:02:40 +03:00
|
|
|
#include "js/Realm.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2016-08-25 11:28:47 +03:00
|
|
|
#include "js/UniquePtr.h"
|
2013-08-14 02:34:12 +04:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2012-01-21 07:49:33 +04:00
|
|
|
/*
|
2018-06-26 19:02:59 +03:00
|
|
|
* [SMDOC] Stack Rooting
|
|
|
|
*
|
2012-01-21 07:49:33 +04:00
|
|
|
* Moving GC Stack Rooting
|
|
|
|
*
|
|
|
|
* A moving GC may change the physical location of GC allocated things, even
|
|
|
|
* when they are rooted, updating all pointers to the thing to refer to its new
|
|
|
|
* location. The GC must therefore know about all live pointers to a thing,
|
|
|
|
* not just one of them, in order to behave correctly.
|
|
|
|
*
|
2013-04-27 23:27:53 +04:00
|
|
|
* The |Rooted| and |Handle| classes below are used to root stack locations
|
2012-11-15 04:06:27 +04:00
|
|
|
* whose value may be held live across a call that can trigger GC. For a
|
|
|
|
* code fragment such as:
|
2012-01-21 07:49:33 +04:00
|
|
|
*
|
2015-03-29 01:22:11 +03:00
|
|
|
* JSObject* obj = NewObject(cx);
|
2012-11-15 04:06:27 +04:00
|
|
|
* DoSomething(cx);
|
2012-01-21 07:49:33 +04:00
|
|
|
* ... = obj->lastProperty();
|
|
|
|
*
|
2012-11-15 04:06:27 +04:00
|
|
|
* If |DoSomething()| can trigger a GC, the stack location of |obj| must be
|
|
|
|
* rooted to ensure that the GC does not move the JSObject referred to by
|
|
|
|
* |obj| without updating |obj|'s location itself. This rooting must happen
|
|
|
|
* regardless of whether there are other roots which ensure that the object
|
|
|
|
* itself will not be collected.
|
2012-01-21 07:49:33 +04:00
|
|
|
*
|
2012-11-15 04:06:27 +04:00
|
|
|
* If |DoSomething()| cannot trigger a GC, and the same holds for all other
|
|
|
|
* calls made between |obj|'s definitions and its last uses, then no rooting
|
2013-03-05 22:32:17 +04:00
|
|
|
* is required.
|
2012-01-21 07:49:33 +04:00
|
|
|
*
|
2012-11-15 04:06:27 +04:00
|
|
|
* SpiderMonkey can trigger a GC at almost any time and in ways that are not
|
|
|
|
* always clear. For example, the following innocuous-looking actions can
|
|
|
|
* cause a GC: allocation of any new GC thing; JSObject::hasProperty;
|
|
|
|
* JS_ReportError and friends; and ToNumber, among many others. The following
|
|
|
|
* dangerous-looking actions cannot trigger a GC: js_malloc, cx->malloc_,
|
|
|
|
* rt->malloc_, and friends and JS_ReportOutOfMemory.
|
|
|
|
*
|
2013-04-12 12:31:43 +04:00
|
|
|
* The following family of three classes will exactly root a stack location.
|
2012-11-15 04:06:27 +04:00
|
|
|
* Incorrect usage of these classes will result in a compile error in almost
|
|
|
|
* all cases. Therefore, it is very hard to be incorrectly rooted if you use
|
|
|
|
* these classes exclusively. These classes are all templated on the type T of
|
|
|
|
* the value being rooted.
|
2012-01-21 07:49:33 +04:00
|
|
|
*
|
2012-05-25 03:05:18 +04:00
|
|
|
* - Rooted<T> declares a variable of type T, whose value is always rooted.
|
|
|
|
* Rooted<T> may be automatically coerced to a Handle<T>, below. Rooted<T>
|
|
|
|
* should be used whenever a local variable's value may be held live across a
|
2013-04-12 12:31:43 +04:00
|
|
|
* call which can trigger a GC.
|
2012-01-21 07:49:33 +04:00
|
|
|
*
|
2012-05-25 03:05:18 +04:00
|
|
|
* - Handle<T> is a const reference to a Rooted<T>. Functions which take GC
|
2012-05-24 19:52:21 +04:00
|
|
|
* things or values as arguments and need to root those arguments should
|
|
|
|
* generally use handles for those arguments and avoid any explicit rooting.
|
|
|
|
* This has two benefits. First, when several such functions call each other
|
|
|
|
* then redundant rooting of multiple copies of the GC thing can be avoided.
|
|
|
|
* Second, if the caller does not pass a rooted value a compile error will be
|
|
|
|
* generated, which is quicker and easier to fix than when relying on a
|
|
|
|
* separate rooting analysis.
|
2012-11-15 04:06:27 +04:00
|
|
|
*
|
|
|
|
* - MutableHandle<T> is a non-const reference to Rooted<T>. It is used in the
|
2015-03-29 01:22:11 +03:00
|
|
|
* same way as Handle<T> and includes a |set(const T& v)| method to allow
|
2012-11-15 04:06:27 +04:00
|
|
|
* updating the value of the referenced Rooted<T>. A MutableHandle<T> can be
|
2015-05-30 18:06:35 +03:00
|
|
|
* created with an implicit cast from a Rooted<T>*.
|
2012-11-15 04:06:27 +04:00
|
|
|
*
|
2013-04-27 23:27:53 +04:00
|
|
|
* In some cases the small performance overhead of exact rooting (measured to
|
|
|
|
* be a few nanoseconds on desktop) is too much. In these cases, try the
|
|
|
|
* following:
|
2012-11-15 04:06:27 +04:00
|
|
|
*
|
|
|
|
* - Move all Rooted<T> above inner loops: this allows you to re-use the root
|
|
|
|
* on each iteration of the loop.
|
|
|
|
*
|
|
|
|
* - Pass Handle<T> through your hot call stack to avoid re-rooting costs at
|
|
|
|
* every invocation.
|
|
|
|
*
|
2012-12-04 23:10:53 +04:00
|
|
|
* The following diagram explains the list of supported, implicit type
|
|
|
|
* conversions between classes of this family:
|
|
|
|
*
|
2013-04-12 12:31:43 +04:00
|
|
|
* Rooted<T> ----> Handle<T>
|
|
|
|
* | ^
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* +---> MutableHandle<T>
|
|
|
|
* (via &)
|
2012-12-04 23:10:53 +04:00
|
|
|
*
|
2013-04-27 23:27:53 +04:00
|
|
|
* All of these types have an implicit conversion to raw pointers.
|
2012-01-21 07:49:33 +04:00
|
|
|
*/
|
|
|
|
|
2012-09-25 05:08:22 +04:00
|
|
|
namespace js {
|
|
|
|
|
2012-04-12 20:23:51 +04:00
|
|
|
template <typename T>
|
2015-12-28 21:11:40 +03:00
|
|
|
struct BarrierMethods {};
|
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename Element, typename Wrapper>
|
|
|
|
class WrappedPtrOperations {};
|
2012-11-15 04:06:27 +04:00
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename Element, typename Wrapper>
|
|
|
|
class MutableWrappedPtrOperations
|
|
|
|
: public WrappedPtrOperations<Element, Wrapper> {};
|
2012-09-25 05:08:22 +04:00
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename T, typename Wrapper>
|
|
|
|
class RootedBase : public MutableWrappedPtrOperations<T, Wrapper> {};
|
2012-09-25 05:08:22 +04:00
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename T, typename Wrapper>
|
|
|
|
class HandleBase : public WrappedPtrOperations<T, Wrapper> {};
|
|
|
|
|
|
|
|
template <typename T, typename Wrapper>
|
|
|
|
class MutableHandleBase : public MutableWrappedPtrOperations<T, Wrapper> {};
|
|
|
|
|
|
|
|
template <typename T, typename Wrapper>
|
|
|
|
class HeapBase : public MutableWrappedPtrOperations<T, Wrapper> {};
|
2013-05-27 15:51:25 +04:00
|
|
|
|
2016-08-25 00:21:42 +03:00
|
|
|
// Cannot use FOR_EACH_HEAP_ABLE_GC_POINTER_TYPE, as this would import too many
|
|
|
|
// macros into scope
|
|
|
|
template <typename T>
|
|
|
|
struct IsHeapConstructibleType {
|
|
|
|
static constexpr bool value = false;
|
|
|
|
};
|
|
|
|
#define DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE(T) \
|
|
|
|
template <> \
|
|
|
|
struct IsHeapConstructibleType<T> { \
|
|
|
|
static constexpr bool value = true; \
|
|
|
|
};
|
|
|
|
FOR_EACH_PUBLIC_GC_POINTER_TYPE(DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE)
|
|
|
|
FOR_EACH_PUBLIC_TAGGED_GC_POINTER_TYPE(DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE)
|
|
|
|
#undef DECLARE_IS_HEAP_CONSTRUCTIBLE_TYPE
|
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename T, typename Wrapper>
|
|
|
|
class PersistentRootedBase : public MutableWrappedPtrOperations<T, Wrapper> {};
|
2015-01-23 13:23:56 +03:00
|
|
|
|
2017-09-21 13:42:18 +03:00
|
|
|
template <typename T>
|
|
|
|
class FakeRooted;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class FakeMutableHandle;
|
|
|
|
|
2013-05-27 15:51:25 +04:00
|
|
|
namespace gc {
|
|
|
|
struct Cell;
|
2014-01-23 13:53:42 +04:00
|
|
|
template <typename T>
|
2014-01-22 15:28:06 +04:00
|
|
|
struct PersistentRootedMarker;
|
2013-05-27 15:51:25 +04:00
|
|
|
} /* namespace gc */
|
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
// Important: Return a reference so passing a Rooted<T>, etc. to
|
|
|
|
// something that takes a |const T&| is not a GC hazard.
|
2016-10-07 14:58:37 +03:00
|
|
|
#define DECLARE_POINTER_CONSTREF_OPS(T) \
|
|
|
|
operator const T&() const { return get(); } \
|
2015-03-29 01:22:11 +03:00
|
|
|
const T& operator->() const { return get(); }
|
2015-02-05 02:27:00 +03:00
|
|
|
|
|
|
|
// Assignment operators on a base class are hidden by the implicitly defined
|
|
|
|
// operator= on the derived class. Thus, define the operator= directly on the
|
|
|
|
// class as we would need to manually pass it through anyway.
|
|
|
|
#define DECLARE_POINTER_ASSIGN_OPS(Wrapper, T) \
|
2015-03-29 01:22:11 +03:00
|
|
|
Wrapper<T>& operator=(const T& p) { \
|
2015-02-05 02:27:00 +03:00
|
|
|
set(p); \
|
|
|
|
return *this; \
|
|
|
|
} \
|
2017-06-12 12:43:46 +03:00
|
|
|
Wrapper<T>& operator=(T&& p) { \
|
2018-05-30 22:15:35 +03:00
|
|
|
set(std::move(p)); \
|
2017-06-12 12:43:46 +03:00
|
|
|
return *this; \
|
|
|
|
} \
|
2015-03-29 01:22:11 +03:00
|
|
|
Wrapper<T>& operator=(const Wrapper<T>& other) { \
|
2015-02-05 02:27:00 +03:00
|
|
|
set(other.get()); \
|
|
|
|
return *this; \
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-07 14:58:37 +03:00
|
|
|
#define DELETE_ASSIGNMENT_OPS(Wrapper, T) \
|
2015-03-29 01:22:11 +03:00
|
|
|
template <typename S> \
|
|
|
|
Wrapper<T>& operator=(S) = delete; \
|
|
|
|
Wrapper<T>& operator=(const Wrapper<T>&) = delete;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
#define DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr) \
|
2015-03-29 01:22:11 +03:00
|
|
|
const T* address() const { return &(ptr); } \
|
|
|
|
const T& get() const { return (ptr); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
#define DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr) \
|
2015-03-29 01:22:11 +03:00
|
|
|
T* address() { return &(ptr); } \
|
|
|
|
T& get() { return (ptr); }
|
2015-02-05 02:27:00 +03:00
|
|
|
|
2012-09-25 05:08:22 +04:00
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2013-03-06 20:41:38 +04:00
|
|
|
template <typename T>
|
|
|
|
class Rooted;
|
2013-11-05 01:35:08 +04:00
|
|
|
template <typename T>
|
|
|
|
class PersistentRooted;
|
2013-03-06 20:41:38 +04:00
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
JS_FRIEND_API void HeapObjectWriteBarriers(JSObject** objp, JSObject* prev,
|
|
|
|
JSObject* next);
|
|
|
|
JS_FRIEND_API void HeapStringWriteBarriers(JSString** objp, JSString* prev,
|
|
|
|
JSString* next);
|
|
|
|
JS_FRIEND_API void HeapScriptWriteBarriers(JSScript** objp, JSScript* prev,
|
|
|
|
JSScript* next);
|
2014-08-01 01:43:45 +04:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
/**
|
|
|
|
* Create a safely-initialized |T|, suitable for use as a default value in
|
|
|
|
* situations requiring a safe but arbitrary |T| value.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
inline T SafelyInitialized() {
|
|
|
|
// This function wants to presume that |T()| -- which value-initializes a
|
|
|
|
// |T| per C++11 [expr.type.conv]p2 -- will produce a safely-initialized,
|
|
|
|
// safely-usable T that it can return.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
#if defined(XP_WIN) || defined(XP_MACOSX) || \
|
|
|
|
(defined(XP_UNIX) && !defined(__clang__))
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
// That presumption holds for pointers, where value initialization produces
|
|
|
|
// a null pointer.
|
|
|
|
constexpr bool IsPointer = std::is_pointer<T>::value;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
// For classes and unions we *assume* that if |T|'s default constructor is
|
|
|
|
// non-trivial it'll initialize correctly. (This is unideal, but C++
|
|
|
|
// doesn't offer a type trait indicating whether a class's constructor is
|
|
|
|
// user-defined, which better approximates our desired semantics.)
|
|
|
|
constexpr bool IsNonTriviallyDefaultConstructibleClassOrUnion =
|
|
|
|
(std::is_class<T>::value || std::is_union<T>::value) &&
|
|
|
|
!std::is_trivially_default_constructible<T>::value;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
static_assert(IsPointer || IsNonTriviallyDefaultConstructibleClassOrUnion,
|
|
|
|
"T() must evaluate to a safely-initialized T");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
2014-08-01 01:43:45 +04:00
|
|
|
#ifdef JS_DEBUG
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2014-08-01 01:43:45 +04:00
|
|
|
* For generational GC, assert that an object is in the tenured generation as
|
|
|
|
* opposed to being in the nursery.
|
|
|
|
*/
|
|
|
|
extern JS_FRIEND_API void AssertGCThingMustBeTenured(JSObject* obj);
|
2017-07-29 02:46:38 +03:00
|
|
|
extern JS_FRIEND_API void AssertGCThingIsNotNurseryAllocable(
|
|
|
|
js::gc::Cell* cell);
|
2014-08-01 01:43:45 +04:00
|
|
|
#else
|
2015-03-29 01:22:11 +03:00
|
|
|
inline void AssertGCThingMustBeTenured(JSObject* obj) {}
|
2017-07-29 02:46:38 +03:00
|
|
|
inline void AssertGCThingIsNotNurseryAllocable(js::gc::Cell* cell) {}
|
2014-08-01 01:43:45 +04:00
|
|
|
#endif
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2013-11-01 14:20:50 +04:00
|
|
|
* The Heap<T> class is a heap-stored reference to a JS GC thing. All members of
|
|
|
|
* heap classes that refer to GC things should use Heap<T> (or possibly
|
|
|
|
* TenuredHeap<T>, described below).
|
2013-05-27 15:51:25 +04:00
|
|
|
*
|
2013-11-01 14:20:50 +04:00
|
|
|
* Heap<T> is an abstraction that hides some of the complexity required to
|
|
|
|
* maintain GC invariants for the contained reference. It uses operator
|
|
|
|
* overloading to provide a normal pointer interface, but notifies the GC every
|
|
|
|
* time the value it contains is updated. This is necessary for generational GC,
|
|
|
|
* which keeps track of all pointers into the nursery.
|
2013-06-21 17:12:45 +04:00
|
|
|
*
|
2013-11-01 14:20:50 +04:00
|
|
|
* Heap<T> instances must be traced when their containing object is traced to
|
|
|
|
* keep the pointed-to GC thing alive.
|
2013-06-21 17:12:45 +04:00
|
|
|
*
|
2013-11-01 14:20:50 +04:00
|
|
|
* Heap<T> objects should only be used on the heap. GC references stored on the
|
|
|
|
* C/C++ stack must use Rooted/Handle/MutableHandle instead.
|
|
|
|
*
|
2016-08-25 00:21:42 +03:00
|
|
|
* Type T must be a public GC pointer type.
|
2013-05-27 15:51:25 +04:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-10-09 12:03:20 +03:00
|
|
|
class MOZ_NON_MEMMOVABLE Heap : public js::HeapBase<T, Heap<T>> {
|
2016-08-25 00:21:42 +03:00
|
|
|
// Please note: this can actually also be used by nsXBLMaybeCompiled<T>, for
|
|
|
|
// legacy reasons.
|
|
|
|
static_assert(js::IsHeapConstructibleType<T>::value,
|
|
|
|
"Type T must be a public GC pointer type");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-05-27 15:51:25 +04:00
|
|
|
public:
|
2016-10-29 01:11:56 +03:00
|
|
|
using ElementType = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
Heap() : ptr(SafelyInitialized<T>()) {
|
|
|
|
// No barriers are required for initialization to the default value.
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(T) == sizeof(Heap<T>),
|
|
|
|
"Heap<T> must be binary compatible with T.");
|
2013-06-14 02:14:44 +04:00
|
|
|
}
|
2016-09-11 12:15:22 +03:00
|
|
|
explicit Heap(const T& p) { init(p); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-02-07 22:03:21 +04:00
|
|
|
/*
|
|
|
|
* For Heap, move semantics are equivalent to copy semantics. In C++, a
|
|
|
|
* copy constructor taking const-ref is the way to get a single function
|
|
|
|
* that will be used for both lvalue and rvalue copies, so we can simply
|
|
|
|
* omit the rvalue variant.
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
explicit Heap(const Heap<T>& p) { init(p.ptr); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
~Heap() { writeBarriers(ptr, SafelyInitialized<T>()); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(Heap, T);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
const T* address() const { return &ptr; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
void exposeToActiveJS() const { js::BarrierMethods<T>::exposeToJS(ptr); }
|
2016-10-18 19:58:19 +03:00
|
|
|
const T& get() const {
|
|
|
|
exposeToActiveJS();
|
2016-02-07 20:08:55 +03:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
const T& unbarrieredGet() const { return ptr; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
T* unsafeGet() { return &ptr; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-27 19:26:22 +03:00
|
|
|
void unbarrieredSet(const T& newPtr) { ptr = newPtr; }
|
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
explicit operator bool() const {
|
|
|
|
return bool(js::BarrierMethods<T>::asGCThingOrNull(ptr));
|
|
|
|
}
|
|
|
|
explicit operator bool() {
|
|
|
|
return bool(js::BarrierMethods<T>::asGCThingOrNull(ptr));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-05-27 15:51:25 +04:00
|
|
|
private:
|
2016-09-11 12:15:22 +03:00
|
|
|
void init(const T& newPtr) {
|
2013-06-18 14:00:37 +04:00
|
|
|
ptr = newPtr;
|
2019-03-27 19:26:09 +03:00
|
|
|
writeBarriers(SafelyInitialized<T>(), ptr);
|
2013-06-18 14:00:37 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-09-11 12:15:22 +03:00
|
|
|
void set(const T& newPtr) {
|
2015-06-18 20:23:49 +03:00
|
|
|
T tmp = ptr;
|
|
|
|
ptr = newPtr;
|
2019-03-27 19:26:09 +03:00
|
|
|
writeBarriers(tmp, ptr);
|
2013-05-27 15:51:25 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
void writeBarriers(const T& prev, const T& next) {
|
|
|
|
js::BarrierMethods<T>::writeBarriers(&ptr, prev, next);
|
2013-05-27 15:51:25 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-05-27 15:51:25 +04:00
|
|
|
T ptr;
|
|
|
|
};
|
|
|
|
|
2016-02-07 20:08:55 +03:00
|
|
|
static MOZ_ALWAYS_INLINE bool ObjectIsTenured(JSObject* obj) {
|
|
|
|
return !js::gc::IsInsideNursery(reinterpret_cast<js::gc::Cell*>(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE bool ObjectIsTenured(const Heap<JSObject*>& obj) {
|
|
|
|
return ObjectIsTenured(obj.unbarrieredGet());
|
|
|
|
}
|
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE bool ObjectIsMarkedGray(JSObject* obj) {
|
2017-01-06 14:23:21 +03:00
|
|
|
auto cell = reinterpret_cast<js::gc::Cell*>(obj);
|
|
|
|
return js::gc::detail::CellIsMarkedGrayIfKnown(cell);
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static MOZ_ALWAYS_INLINE bool ObjectIsMarkedGray(
|
|
|
|
const JS::Heap<JSObject*>& obj) {
|
|
|
|
return ObjectIsMarkedGray(obj.unbarrieredGet());
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:28:10 +03:00
|
|
|
// The following *IsNotGray functions take account of the eventual
|
|
|
|
// gray marking state at the end of any ongoing incremental GC by
|
|
|
|
// delaying the checks if necessary.
|
|
|
|
|
2017-03-02 13:22:47 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
|
2018-12-07 00:28:14 +03:00
|
|
|
inline void AssertCellIsNotGray(const js::gc::Cell* maybeCell) {
|
2018-12-07 00:28:10 +03:00
|
|
|
if (maybeCell) {
|
|
|
|
js::gc::detail::AssertCellIsNotGray(maybeCell);
|
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:28:10 +03:00
|
|
|
inline void AssertObjectIsNotGray(JSObject* maybeObj) {
|
|
|
|
AssertCellIsNotGray(reinterpret_cast<js::gc::Cell*>(maybeObj));
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:28:10 +03:00
|
|
|
inline void AssertObjectIsNotGray(const JS::Heap<JSObject*>& obj) {
|
|
|
|
AssertObjectIsNotGray(obj.unbarrieredGet());
|
2017-03-02 13:22:47 +03:00
|
|
|
}
|
2018-12-07 00:28:10 +03:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
inline void AssertCellIsNotGray(js::gc::Cell* maybeCell) {}
|
|
|
|
inline void AssertObjectIsNotGray(JSObject* maybeObj) {}
|
|
|
|
inline void AssertObjectIsNotGray(const JS::Heap<JSObject*>& obj) {}
|
|
|
|
|
2017-03-02 13:22:47 +03:00
|
|
|
#endif
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2013-07-23 13:58:26 +04:00
|
|
|
* The TenuredHeap<T> class is similar to the Heap<T> class above in that it
|
|
|
|
* encapsulates the GC concerns of an on-heap reference to a JS object. However,
|
|
|
|
* it has two important differences:
|
|
|
|
*
|
|
|
|
* 1) Pointers which are statically known to only reference "tenured" objects
|
|
|
|
* can avoid the extra overhead of SpiderMonkey's write barriers.
|
|
|
|
*
|
|
|
|
* 2) Objects in the "tenured" heap have stronger alignment restrictions than
|
|
|
|
* those in the "nursery", so it is possible to store flags in the lower
|
|
|
|
* bits of pointers known to be tenured. TenuredHeap wraps a normal tagged
|
|
|
|
* pointer with a nice API for accessing the flag bits and adds various
|
|
|
|
* assertions to ensure that it is not mis-used.
|
|
|
|
*
|
|
|
|
* GC things are said to be "tenured" when they are located in the long-lived
|
|
|
|
* heap: e.g. they have gained tenure as an object by surviving past at least
|
|
|
|
* one GC. For performance, SpiderMonkey allocates some things which are known
|
|
|
|
* to normally be long lived directly into the tenured generation; for example,
|
|
|
|
* global objects. Additionally, SpiderMonkey does not visit individual objects
|
|
|
|
* when deleting non-tenured objects, so object with finalizers are also always
|
|
|
|
* tenured; for instance, this includes most DOM objects.
|
|
|
|
*
|
|
|
|
* The considerations to keep in mind when using a TenuredHeap<T> vs a normal
|
|
|
|
* Heap<T> are:
|
|
|
|
*
|
|
|
|
* - It is invalid for a TenuredHeap<T> to refer to a non-tenured thing.
|
|
|
|
* - It is however valid for a Heap<T> to refer to a tenured thing.
|
|
|
|
* - It is not possible to store flag bits in a Heap<T>.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-10 13:12:14 +03:00
|
|
|
class TenuredHeap : public js::HeapBase<T, TenuredHeap<T>> {
|
2013-07-23 13:58:26 +04:00
|
|
|
public:
|
2016-10-29 01:11:56 +03:00
|
|
|
using ElementType = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
TenuredHeap() : bits(0) {
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(sizeof(T) == sizeof(TenuredHeap<T>),
|
|
|
|
"TenuredHeap<T> must be binary compatible with T.");
|
2013-07-23 13:58:26 +04:00
|
|
|
}
|
|
|
|
explicit TenuredHeap(T p) : bits(0) { setPtr(p); }
|
2015-03-29 01:22:11 +03:00
|
|
|
explicit TenuredHeap(const TenuredHeap<T>& p) : bits(0) {
|
|
|
|
setPtr(p.getPtr());
|
|
|
|
}
|
2019-03-27 19:26:09 +03:00
|
|
|
~TenuredHeap() { pre(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
void setPtr(T newPtr) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT((reinterpret_cast<uintptr_t>(newPtr) & flagsMask) == 0);
|
2017-09-26 12:23:14 +03:00
|
|
|
MOZ_ASSERT(js::gc::IsCellPointerValidOrNull(newPtr));
|
2018-09-06 13:11:07 +03:00
|
|
|
if (newPtr) {
|
2013-07-23 13:58:26 +04:00
|
|
|
AssertGCThingMustBeTenured(newPtr);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2019-03-27 19:26:09 +03:00
|
|
|
pre();
|
|
|
|
unbarrieredSetPtr(newPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unbarrieredSetPtr(T newPtr) {
|
2013-07-23 13:58:26 +04:00
|
|
|
bits = (bits & flagsMask) | reinterpret_cast<uintptr_t>(newPtr);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
void setFlags(uintptr_t flagsToSet) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT((flagsToSet & ~flagsMask) == 0);
|
2013-07-23 13:58:26 +04:00
|
|
|
bits |= flagsToSet;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
void unsetFlags(uintptr_t flagsToUnset) {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT((flagsToUnset & ~flagsMask) == 0);
|
2013-07-23 13:58:26 +04:00
|
|
|
bits &= ~flagsToUnset;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
bool hasFlag(uintptr_t flag) const {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT((flag & ~flagsMask) == 0);
|
2013-07-23 13:58:26 +04:00
|
|
|
return (bits & flag) != 0;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-07 14:58:37 +03:00
|
|
|
T unbarrieredGetPtr() const { return reinterpret_cast<T>(bits & ~flagsMask); }
|
2013-07-23 13:58:26 +04:00
|
|
|
uintptr_t getFlags() const { return bits & flagsMask; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-18 19:58:19 +03:00
|
|
|
void exposeToActiveJS() const {
|
|
|
|
js::BarrierMethods<T>::exposeToJS(unbarrieredGetPtr());
|
|
|
|
}
|
2016-10-07 14:58:37 +03:00
|
|
|
T getPtr() const {
|
2016-10-18 19:58:19 +03:00
|
|
|
exposeToActiveJS();
|
|
|
|
return unbarrieredGetPtr();
|
2016-10-07 14:58:37 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
operator T() const { return getPtr(); }
|
|
|
|
T operator->() const { return getPtr(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-10-07 14:58:37 +03:00
|
|
|
explicit operator bool() const {
|
|
|
|
return bool(js::BarrierMethods<T>::asGCThingOrNull(unbarrieredGetPtr()));
|
|
|
|
}
|
|
|
|
explicit operator bool() {
|
|
|
|
return bool(js::BarrierMethods<T>::asGCThingOrNull(unbarrieredGetPtr()));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
TenuredHeap<T>& operator=(T p) {
|
2013-07-23 13:58:26 +04:00
|
|
|
setPtr(p);
|
|
|
|
return *this;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
TenuredHeap<T>& operator=(const TenuredHeap<T>& other) {
|
2013-11-18 21:21:44 +04:00
|
|
|
bits = other.bits;
|
|
|
|
return *this;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
private:
|
|
|
|
enum {
|
|
|
|
maskBits = 3,
|
|
|
|
flagsMask = (1 << maskBits) - 1,
|
|
|
|
};
|
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
void pre() {
|
|
|
|
if (T prev = unbarrieredGetPtr()) {
|
|
|
|
JS::IncrementalPreWriteBarrier(JS::GCCellPtr(prev));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 13:58:26 +04:00
|
|
|
uintptr_t bits;
|
|
|
|
};
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2012-04-28 03:40:10 +04:00
|
|
|
* Reference to a T that has been rooted elsewhere. This is most useful
|
|
|
|
* as a parameter type, which guarantees that the T lvalue is properly
|
|
|
|
* rooted. See "Move GC Stack Rooting" above.
|
2012-07-12 14:01:59 +04:00
|
|
|
*
|
|
|
|
* If you want to add additional methods to Handle for a specific
|
|
|
|
* specialization, define a HandleBase<T> specialization containing them.
|
2012-04-12 20:23:51 +04:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-10 13:12:14 +03:00
|
|
|
class MOZ_NONHEAP_CLASS Handle : public js::HandleBase<T, Handle<T>> {
|
2013-10-11 02:22:35 +04:00
|
|
|
friend class JS::MutableHandle<T>;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-04-12 20:23:51 +04:00
|
|
|
public:
|
2016-10-29 01:11:56 +03:00
|
|
|
using ElementType = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-06-20 02:01:58 +04:00
|
|
|
/* Creates a handle from a handle of a type convertible to T. */
|
|
|
|
template <typename S>
|
2015-07-28 04:15:34 +03:00
|
|
|
MOZ_IMPLICIT Handle(
|
|
|
|
Handle<S> handle,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy = 0) {
|
2015-03-29 01:22:11 +03:00
|
|
|
static_assert(sizeof(Handle<T>) == sizeof(T*),
|
2013-07-18 21:59:53 +04:00
|
|
|
"Handle must be binary compatible with T*.");
|
2015-03-29 01:22:11 +03:00
|
|
|
ptr = reinterpret_cast<const T*>(handle.address());
|
2012-04-12 20:23:51 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-14 00:07:34 +03:00
|
|
|
MOZ_IMPLICIT Handle(decltype(nullptr)) {
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(mozilla::IsPointer<T>::value,
|
2015-05-14 00:07:34 +03:00
|
|
|
"nullptr_t overload not valid for non-pointer types");
|
2017-02-09 02:18:08 +03:00
|
|
|
static void* const ConstNullValue = nullptr;
|
|
|
|
ptr = reinterpret_cast<const T*>(&ConstNullValue);
|
2012-07-08 14:00:11 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-26 05:46:24 +04:00
|
|
|
MOZ_IMPLICIT Handle(MutableHandle<T> handle) { ptr = handle.address(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-04-14 05:06:40 +04:00
|
|
|
/*
|
2013-07-23 13:58:28 +04:00
|
|
|
* Take care when calling this method!
|
2012-04-28 03:40:10 +04:00
|
|
|
*
|
2013-07-23 13:58:28 +04:00
|
|
|
* This creates a Handle from the raw location of a T.
|
|
|
|
*
|
|
|
|
* It should be called only if the following conditions hold:
|
|
|
|
*
|
|
|
|
* 1) the location of the T is guaranteed to be marked (for some reason
|
|
|
|
* other than being a Rooted), e.g., if it is guaranteed to be reachable
|
|
|
|
* from an implicit root.
|
|
|
|
*
|
|
|
|
* 2) the contents of the location are immutable, or at least cannot change
|
|
|
|
* for the lifetime of the handle, as its users may not expect its value
|
|
|
|
* to change underneath them.
|
2012-04-14 05:06:40 +04:00
|
|
|
*/
|
2016-07-09 00:39:53 +03:00
|
|
|
static constexpr Handle fromMarkedLocation(const T* p) {
|
2013-11-23 00:05:04 +04:00
|
|
|
return Handle(p, DeliberatelyChoosingThisOverload,
|
|
|
|
ImUsingThisOnlyInFromFromMarkedLocation);
|
2012-04-14 05:06:40 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-04-28 03:40:10 +04:00
|
|
|
/*
|
|
|
|
* Construct a handle from an explicitly rooted location. This is the
|
2012-05-24 19:52:21 +04:00
|
|
|
* normal way to create a handle, and normally happens implicitly.
|
2012-04-28 03:40:10 +04:00
|
|
|
*/
|
2012-06-20 02:01:58 +04:00
|
|
|
template <typename S>
|
2015-07-28 04:15:34 +03:00
|
|
|
inline MOZ_IMPLICIT Handle(
|
|
|
|
const Rooted<S>& root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy = 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-05 01:35:08 +04:00
|
|
|
template <typename S>
|
2015-07-28 04:15:34 +03:00
|
|
|
inline MOZ_IMPLICIT Handle(
|
|
|
|
const PersistentRooted<S>& root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy = 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-07-30 15:19:09 +04:00
|
|
|
/* Construct a read only handle from a mutable handle. */
|
|
|
|
template <typename S>
|
2015-07-28 04:15:34 +03:00
|
|
|
inline MOZ_IMPLICIT Handle(
|
|
|
|
MutableHandle<S>& root,
|
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy = 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-04-12 20:23:51 +04:00
|
|
|
private:
|
2018-04-12 22:15:26 +03:00
|
|
|
Handle() {}
|
2015-02-05 02:27:00 +03:00
|
|
|
DELETE_ASSIGNMENT_OPS(Handle, T);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-23 00:05:04 +04:00
|
|
|
enum Disambiguator { DeliberatelyChoosingThisOverload = 42 };
|
|
|
|
enum CallerIdentity { ImUsingThisOnlyInFromFromMarkedLocation = 17 };
|
2016-07-09 00:39:53 +03:00
|
|
|
constexpr Handle(const T* p, Disambiguator, CallerIdentity) : ptr(p) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
const T* ptr;
|
2012-04-13 09:07:43 +04:00
|
|
|
};
|
2012-04-12 20:23:51 +04:00
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2012-07-04 22:12:16 +04:00
|
|
|
* Similar to a handle, but the underlying storage can be changed. This is
|
|
|
|
* useful for outparams.
|
2012-07-12 14:01:59 +04:00
|
|
|
*
|
|
|
|
* If you want to add additional methods to MutableHandle for a specific
|
|
|
|
* specialization, define a MutableHandleBase<T> specialization containing
|
|
|
|
* them.
|
2012-07-04 22:12:16 +04:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-10 13:12:14 +03:00
|
|
|
class MOZ_STACK_CLASS MutableHandle
|
|
|
|
: public js::MutableHandleBase<T, MutableHandle<T>> {
|
2012-07-04 22:12:16 +04:00
|
|
|
public:
|
2016-10-29 01:11:56 +03:00
|
|
|
using ElementType = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
inline MOZ_IMPLICIT MutableHandle(Rooted<T>* root);
|
|
|
|
inline MOZ_IMPLICIT MutableHandle(PersistentRooted<T>* root);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-03 03:27:41 +04:00
|
|
|
private:
|
2015-01-14 20:09:11 +03:00
|
|
|
// Disallow nullptr for overloading purposes.
|
|
|
|
MutableHandle(decltype(nullptr)) = delete;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-03 03:27:41 +04:00
|
|
|
public:
|
2016-09-11 12:15:22 +03:00
|
|
|
void set(const T& v) {
|
2012-07-11 05:17:29 +04:00
|
|
|
*ptr = v;
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(GCPolicy<T>::isValid(*ptr));
|
2012-07-11 05:17:29 +04:00
|
|
|
}
|
2017-06-12 12:43:46 +03:00
|
|
|
void set(T&& v) {
|
2018-05-30 22:15:35 +03:00
|
|
|
*ptr = std::move(v);
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(GCPolicy<T>::isValid(*ptr));
|
2017-06-12 12:43:46 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-07-19 01:31:24 +04:00
|
|
|
/*
|
|
|
|
* This may be called only if the location of the T is guaranteed
|
|
|
|
* to be marked (for some reason other than being a Rooted),
|
|
|
|
* e.g., if it is guaranteed to be reachable from an implicit root.
|
|
|
|
*
|
|
|
|
* Create a MutableHandle from a raw location of a T.
|
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
static MutableHandle fromMarkedLocation(T* p) {
|
2012-07-19 01:31:24 +04:00
|
|
|
MutableHandle h;
|
|
|
|
h.ptr = p;
|
|
|
|
return h;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(*ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(*ptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-07-04 22:12:16 +04:00
|
|
|
private:
|
2018-04-12 22:15:26 +03:00
|
|
|
MutableHandle() {}
|
2015-02-05 02:27:00 +03:00
|
|
|
DELETE_ASSIGNMENT_OPS(MutableHandle, T);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
T* ptr;
|
2012-07-04 22:12:16 +04:00
|
|
|
};
|
|
|
|
|
2012-12-04 23:10:53 +04:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// Default implementations for barrier methods on GC thing pointers.
|
2012-01-21 07:49:33 +04:00
|
|
|
template <typename T>
|
2019-03-27 19:26:09 +03:00
|
|
|
struct PtrBarrierMethodsBase {
|
2015-03-29 01:22:11 +03:00
|
|
|
static T* initial() { return nullptr; }
|
2016-02-07 20:08:55 +03:00
|
|
|
static gc::Cell* asGCThingOrNull(T* v) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (!v) {
|
2016-02-07 20:08:55 +03:00
|
|
|
return nullptr;
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
MOZ_ASSERT(uintptr_t(v) > 32);
|
|
|
|
return reinterpret_cast<gc::Cell*>(v);
|
|
|
|
}
|
|
|
|
static void exposeToJS(T* t) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (t) {
|
2016-02-07 20:08:55 +03:00
|
|
|
js::gc::ExposeGCThingToActiveJS(JS::GCCellPtr(t));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
2014-04-12 01:29:14 +04:00
|
|
|
};
|
|
|
|
|
2019-03-27 19:26:09 +03:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct BarrierMethods<T*> : public detail::PtrBarrierMethodsBase<T> {
|
|
|
|
static void writeBarriers(T** vp, T* prev, T* next) {
|
|
|
|
if (prev) {
|
|
|
|
JS::IncrementalPreWriteBarrier(JS::GCCellPtr(prev));
|
|
|
|
}
|
|
|
|
if (next) {
|
|
|
|
JS::AssertGCThingIsNotNurseryAllocable(
|
|
|
|
reinterpret_cast<js::gc::Cell*>(next));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2014-08-01 01:43:45 +04:00
|
|
|
}
|
2019-03-27 19:26:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct BarrierMethods<JSObject*>
|
|
|
|
: public detail::PtrBarrierMethodsBase<JSObject> {
|
|
|
|
static void writeBarriers(JSObject** vp, JSObject* prev, JSObject* next) {
|
|
|
|
JS::HeapObjectWriteBarriers(vp, prev, next);
|
2013-05-27 15:51:25 +04:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
static void exposeToJS(JSObject* obj) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (obj) {
|
2016-02-07 20:08:55 +03:00
|
|
|
JS::ExposeObjectToActiveJS(obj);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
2012-01-21 07:49:33 +04:00
|
|
|
};
|
|
|
|
|
2014-06-23 23:36:36 +04:00
|
|
|
template <>
|
2019-03-27 19:26:09 +03:00
|
|
|
struct BarrierMethods<JSFunction*>
|
|
|
|
: public detail::PtrBarrierMethodsBase<JSFunction> {
|
|
|
|
static void writeBarriers(JSFunction** vp, JSFunction* prev,
|
|
|
|
JSFunction* next) {
|
|
|
|
JS::HeapObjectWriteBarriers(reinterpret_cast<JSObject**>(vp),
|
|
|
|
reinterpret_cast<JSObject*>(prev),
|
|
|
|
reinterpret_cast<JSObject*>(next));
|
2014-06-23 23:36:36 +04:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
static void exposeToJS(JSFunction* fun) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (fun) {
|
2016-02-07 20:08:55 +03:00
|
|
|
JS::ExposeObjectToActiveJS(reinterpret_cast<JSObject*>(fun));
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
}
|
2014-06-23 23:36:36 +04:00
|
|
|
};
|
|
|
|
|
2017-07-29 02:46:38 +03:00
|
|
|
template <>
|
2019-03-27 19:26:09 +03:00
|
|
|
struct BarrierMethods<JSString*>
|
|
|
|
: public detail::PtrBarrierMethodsBase<JSString> {
|
|
|
|
static void writeBarriers(JSString** vp, JSString* prev, JSString* next) {
|
|
|
|
JS::HeapStringWriteBarriers(vp, prev, next);
|
2017-07-29 02:46:38 +03:00
|
|
|
}
|
2019-03-27 19:26:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct BarrierMethods<JSScript*>
|
|
|
|
: public detail::PtrBarrierMethodsBase<JSScript> {
|
|
|
|
static void writeBarriers(JSScript** vp, JSScript* prev, JSScript* next) {
|
|
|
|
JS::HeapScriptWriteBarriers(vp, prev, next);
|
2018-05-29 00:47:33 +03:00
|
|
|
}
|
2017-07-29 02:46:38 +03:00
|
|
|
};
|
|
|
|
|
2015-11-17 22:31:52 +03:00
|
|
|
// Provide hash codes for Cell kinds that may be relocated and, thus, not have
|
|
|
|
// a stable address to use as the base for a hash code. Instead of the address,
|
|
|
|
// this hasher uses Cell::getUniqueId to provide exact matches and as a base
|
|
|
|
// for generating hash codes.
|
|
|
|
//
|
|
|
|
// Note: this hasher, like PointerHasher can "hash" a nullptr. While a nullptr
|
|
|
|
// would not likely be a useful key, there are some cases where being able to
|
|
|
|
// hash a nullptr is useful, either on purpose or because of bugs:
|
|
|
|
// (1) existence checks where the key may happen to be null and (2) some
|
|
|
|
// aggregate Lookup kinds embed a JSObject* that is frequently null and do not
|
|
|
|
// null test before dispatching to the hasher.
|
|
|
|
template <typename T>
|
2018-11-19 20:02:47 +03:00
|
|
|
struct JS_PUBLIC_API MovableCellHasher {
|
2015-11-17 22:31:52 +03:00
|
|
|
using Key = T;
|
|
|
|
using Lookup = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-13 12:20:00 +03:00
|
|
|
static bool hasHash(const Lookup& l);
|
|
|
|
static bool ensureHash(const Lookup& l);
|
2015-11-17 22:31:52 +03:00
|
|
|
static HashNumber hash(const Lookup& l);
|
|
|
|
static bool match(const Key& k, const Lookup& l);
|
|
|
|
static void rekey(Key& k, const Key& newKey) { k = newKey; }
|
|
|
|
};
|
|
|
|
|
2015-11-17 22:32:53 +03:00
|
|
|
template <typename T>
|
2018-11-19 20:02:47 +03:00
|
|
|
struct JS_PUBLIC_API MovableCellHasher<JS::Heap<T>> {
|
2015-11-17 22:32:53 +03:00
|
|
|
using Key = JS::Heap<T>;
|
|
|
|
using Lookup = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-13 12:20:00 +03:00
|
|
|
static bool hasHash(const Lookup& l) {
|
|
|
|
return MovableCellHasher<T>::hasHash(l);
|
|
|
|
}
|
|
|
|
static bool ensureHash(const Lookup& l) {
|
|
|
|
return MovableCellHasher<T>::ensureHash(l);
|
|
|
|
}
|
2015-11-17 22:32:53 +03:00
|
|
|
static HashNumber hash(const Lookup& l) {
|
|
|
|
return MovableCellHasher<T>::hash(l);
|
|
|
|
}
|
2016-02-07 20:08:55 +03:00
|
|
|
static bool match(const Key& k, const Lookup& l) {
|
|
|
|
return MovableCellHasher<T>::match(k.unbarrieredGet(), l);
|
|
|
|
}
|
2015-11-17 22:32:53 +03:00
|
|
|
static void rekey(Key& k, const Key& newKey) { k.unsafeSet(newKey); }
|
|
|
|
};
|
|
|
|
|
2018-07-26 13:15:49 +03:00
|
|
|
} // namespace js
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-07-13 12:20:00 +03:00
|
|
|
template <typename T>
|
2018-07-26 13:15:49 +03:00
|
|
|
struct FallibleHashMethods<js::MovableCellHasher<T>> {
|
2016-07-13 12:20:00 +03:00
|
|
|
template <typename Lookup>
|
|
|
|
static bool hasHash(Lookup&& l) {
|
2018-07-26 13:15:49 +03:00
|
|
|
return js::MovableCellHasher<T>::hasHash(std::forward<Lookup>(l));
|
2016-07-13 12:20:00 +03:00
|
|
|
}
|
|
|
|
template <typename Lookup>
|
|
|
|
static bool ensureHash(Lookup&& l) {
|
2018-07-26 13:15:49 +03:00
|
|
|
return js::MovableCellHasher<T>::ensureHash(std::forward<Lookup>(l));
|
2016-07-13 12:20:00 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-26 13:15:49 +03:00
|
|
|
} // namespace mozilla
|
2013-03-06 20:41:38 +04:00
|
|
|
|
2015-07-09 18:45:40 +03:00
|
|
|
namespace js {
|
|
|
|
|
2016-06-02 22:37:09 +03:00
|
|
|
// The alignment must be set because the Rooted and PersistentRooted ptr fields
|
|
|
|
// may be accessed through reinterpret_cast<Rooted<ConcreteTraceable>*>, and
|
|
|
|
// the compiler may choose a different alignment for the ptr field when it
|
|
|
|
// knows the actual type stored in DispatchWrapper<T>.
|
|
|
|
//
|
|
|
|
// It would make more sense to align only those specific fields of type
|
|
|
|
// DispatchWrapper, rather than DispatchWrapper itself, but that causes MSVC to
|
|
|
|
// fail when Rooted is used in an IsConvertible test.
|
2015-07-09 18:45:40 +03:00
|
|
|
template <typename T>
|
2016-07-25 11:14:04 +03:00
|
|
|
class alignas(8) DispatchWrapper {
|
2016-01-30 03:56:11 +03:00
|
|
|
static_assert(JS::MapTypeToRootKind<T>::kind == JS::RootKind::Traceable,
|
2015-07-31 18:27:12 +03:00
|
|
|
"DispatchWrapper is intended only for usage with a Traceable");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-01-30 03:56:11 +03:00
|
|
|
using TraceFn = void (*)(JSTracer*, T*, const char*);
|
2015-07-09 18:45:40 +03:00
|
|
|
TraceFn tracer;
|
2017-05-09 13:38:32 +03:00
|
|
|
alignas(gc::CellAlignBytes) T storage;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-09 18:45:40 +03:00
|
|
|
public:
|
2015-08-08 00:47:00 +03:00
|
|
|
template <typename U>
|
|
|
|
MOZ_IMPLICIT DispatchWrapper(U&& initial)
|
2016-04-26 19:18:48 +03:00
|
|
|
: tracer(&JS::GCPolicy<T>::trace), storage(std::forward<U>(initial)) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-08-08 00:47:00 +03:00
|
|
|
// Mimic a pointer type, so that we can drop into Rooted.
|
2015-07-09 18:45:40 +03:00
|
|
|
T* operator&() { return &storage; }
|
|
|
|
const T* operator&() const { return &storage; }
|
|
|
|
operator T&() { return storage; }
|
|
|
|
operator const T&() const { return storage; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-09 18:45:40 +03:00
|
|
|
// Trace the contained storage (of unknown type) using the trace function
|
|
|
|
// we set aside when we did know the type.
|
2016-09-24 01:59:55 +03:00
|
|
|
static void TraceWrapped(JSTracer* trc, T* thingp, const char* name) {
|
2015-07-09 18:45:40 +03:00
|
|
|
auto wrapper = reinterpret_cast<DispatchWrapper*>(
|
|
|
|
uintptr_t(thingp) - offsetof(DispatchWrapper, storage));
|
2016-01-30 03:56:11 +03:00
|
|
|
wrapper->tracer(trc, &wrapper->storage, name);
|
2015-07-09 18:45:40 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_PUBLIC_API AutoGCRooter;
|
2017-11-16 15:21:07 +03:00
|
|
|
|
|
|
|
// Our instantiations of Rooted<void*> and PersistentRooted<void*> require an
|
|
|
|
// instantiation of MapTypeToRootKind.
|
|
|
|
template <>
|
|
|
|
struct MapTypeToRootKind<void*> {
|
|
|
|
static const RootKind kind = RootKind::Traceable;
|
|
|
|
};
|
|
|
|
|
|
|
|
using RootedListHeads =
|
|
|
|
mozilla::EnumeratedArray<RootKind, RootKind::Limit, Rooted<void*>*>;
|
|
|
|
|
|
|
|
// Superclass of JSContext which can be used for rooting data in use by the
|
|
|
|
// current thread but that does not provide all the functions of a JSContext.
|
|
|
|
class RootingContext {
|
|
|
|
// Stack GC roots for Rooted GC heap pointers.
|
|
|
|
RootedListHeads stackRoots_;
|
|
|
|
template <typename T>
|
|
|
|
friend class JS::Rooted;
|
|
|
|
|
|
|
|
// Stack GC roots for AutoFooRooter classes.
|
|
|
|
JS::AutoGCRooter* autoGCRooters_;
|
|
|
|
friend class JS::AutoGCRooter;
|
|
|
|
|
2018-01-05 16:35:00 +03:00
|
|
|
// Gecko profiling metadata.
|
|
|
|
// This isn't really rooting related. It's only here because we want
|
Bug 1499507 - Fold the 'profiler is active' check into the 'JSContext has a non-null PseudoStack' check. r=sfink
This eliminates a few instructions from every profiler label and saves code size.
We have around 9000 WebIDL constructors + methods + getters + setters which all
have an inlined instance of this code.
This change reduces the binary size on Linux x64 by around 160KB.
Here's a diff of the impact on the code generated for Attr_Binding::get_specified
in the Mac build:
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rcx, %r14
movq %rdx, %r15
- movq __ZN7mozilla8profiler6detail12RacyFeatures18sActiveAndFeaturesE@GOT, %rax ; __ZN7mozilla8profiler6detail12RacyFeatures18sActiveAndFeaturesE@GOT
- movl (%rax), %eax
- testl %eax, %eax
- js loc_xxxxx
-
- movq $0x0, -40(%rbp)
- jmp loc_xxxxx
-
- movq 0x78(%rdi), %rbx
+ movq 0x80(%rdi), %rbx
movq %rbx, -40(%rbp)
testq %rbx, %rbx
je loc_xxxxx
movl 0x10(%rbx), %r12d
cmpl %r12d, (%rbx)
jbe loc_xxxxx
Differential Revision: https://phabricator.services.mozilla.com/D9192
--HG--
extra : moz-landing-system : lando
2018-11-06 07:29:35 +03:00
|
|
|
// GetContextProfilingStackIfEnabled to be inlineable into non-JS code, and
|
|
|
|
// we didn't want to add another superclass of JSContext just for this.
|
2018-01-05 16:35:00 +03:00
|
|
|
js::GeckoProfilerThread geckoProfiler_;
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
public:
|
|
|
|
RootingContext();
|
|
|
|
|
|
|
|
void traceStackRoots(JSTracer* trc);
|
|
|
|
void checkNoGCRooters();
|
|
|
|
|
2018-01-05 16:35:00 +03:00
|
|
|
js::GeckoProfilerThread& geckoProfiler() { return geckoProfiler_; }
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
protected:
|
|
|
|
// The remaining members in this class should only be accessed through
|
|
|
|
// JSContext pointers. They are unrelated to rooting and are in place so
|
|
|
|
// that inlined API functions can directly access the data.
|
|
|
|
|
2018-05-17 19:02:40 +03:00
|
|
|
/* The current realm. */
|
|
|
|
JS::Realm* realm_;
|
2017-11-16 15:21:07 +03:00
|
|
|
|
|
|
|
/* The current zone. */
|
|
|
|
JS::Zone* zone_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/* Limit pointer for checking native stack consumption. */
|
|
|
|
uintptr_t nativeStackLimit[StackKindCount];
|
|
|
|
|
|
|
|
static const RootingContext* get(const JSContext* cx) {
|
|
|
|
return reinterpret_cast<const RootingContext*>(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RootingContext* get(JSContext* cx) {
|
|
|
|
return reinterpret_cast<RootingContext*>(cx);
|
|
|
|
}
|
|
|
|
|
2018-05-31 12:28:48 +03:00
|
|
|
friend JS::Realm* js::GetContextRealm(const JSContext* cx);
|
2017-11-16 15:21:07 +03:00
|
|
|
friend JS::Zone* js::GetContextZone(const JSContext* cx);
|
|
|
|
};
|
|
|
|
|
2018-11-19 20:02:47 +03:00
|
|
|
class JS_PUBLIC_API AutoGCRooter {
|
2018-05-16 15:23:44 +03:00
|
|
|
protected:
|
|
|
|
enum class Tag : uint8_t {
|
|
|
|
Array, /* js::AutoArrayRooter */
|
|
|
|
ValueArray, /* js::AutoValueArray */
|
|
|
|
Parser, /* js::frontend::Parser */
|
|
|
|
#if defined(JS_BUILD_BINAST)
|
2019-03-12 12:16:00 +03:00
|
|
|
BinASTParser, /* js::frontend::BinASTParser */
|
2018-05-16 15:23:44 +03:00
|
|
|
#endif // defined(JS_BUILD_BINAST)
|
|
|
|
WrapperVector, /* js::AutoWrapperVector */
|
|
|
|
Wrapper, /* js::AutoWrapperRooter */
|
|
|
|
Custom /* js::CustomAutoRooter */
|
|
|
|
};
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
public:
|
2018-05-16 15:23:44 +03:00
|
|
|
AutoGCRooter(JSContext* cx, Tag tag)
|
2017-11-16 15:21:07 +03:00
|
|
|
: AutoGCRooter(JS::RootingContext::get(cx), tag) {}
|
2018-05-16 15:23:44 +03:00
|
|
|
AutoGCRooter(JS::RootingContext* cx, Tag tag)
|
|
|
|
: down(cx->autoGCRooters_), stackTop(&cx->autoGCRooters_), tag_(tag) {
|
2017-11-16 15:21:07 +03:00
|
|
|
MOZ_ASSERT(this != *stackTop);
|
|
|
|
*stackTop = this;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
~AutoGCRooter() {
|
|
|
|
MOZ_ASSERT(this == *stackTop);
|
|
|
|
*stackTop = down;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
/* Implemented in gc/RootMarking.cpp. */
|
|
|
|
inline void trace(JSTracer* trc);
|
2018-03-27 19:00:27 +03:00
|
|
|
static void traceAll(JSContext* cx, JSTracer* trc);
|
|
|
|
static void traceAllWrappers(JSContext* cx, JSTracer* trc);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-16 15:23:44 +03:00
|
|
|
private:
|
|
|
|
AutoGCRooter* const down;
|
|
|
|
AutoGCRooter** const stackTop;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
/*
|
2018-05-16 15:23:44 +03:00
|
|
|
* Discriminates actual subclass of this being used. The meaning is
|
|
|
|
* indicated by the corresponding value in the Tag enum.
|
2017-11-16 15:21:07 +03:00
|
|
|
*/
|
2018-05-16 15:23:44 +03:00
|
|
|
Tag tag_;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
/* No copy or assignment semantics. */
|
|
|
|
AutoGCRooter(AutoGCRooter& ida) = delete;
|
|
|
|
void operator=(AutoGCRooter& ida) = delete;
|
2018-07-25 02:52:43 +03:00
|
|
|
} JS_HAZ_ROOTED_BASE;
|
2017-11-16 15:21:07 +03:00
|
|
|
|
2017-04-13 23:22:20 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For pointer types, the TraceKind for tracing is based on the list it is
|
|
|
|
* in (selected via MapTypeToRootKind), so no additional storage is
|
|
|
|
* required here. Non-pointer types, however, share the same list, so the
|
|
|
|
* function to call for tracing is stored adjacent to the struct. Since C++
|
|
|
|
* cannot templatize on storage class, this is implemented via the wrapper
|
|
|
|
* class DispatchWrapper.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
using MaybeWrapped =
|
|
|
|
typename mozilla::Conditional<MapTypeToRootKind<T>::kind ==
|
|
|
|
JS::RootKind::Traceable,
|
|
|
|
js::DispatchWrapper<T>, T>::Type;
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2012-05-24 19:52:21 +04:00
|
|
|
* Local variable of type T whose value is always rooted. This is typically
|
|
|
|
* used for local variables, or for non-rooted values being passed to a
|
|
|
|
* function that requires a handle, e.g. Foo(Root<T>(cx, x)).
|
2012-07-12 14:01:59 +04:00
|
|
|
*
|
2012-07-13 03:42:26 +04:00
|
|
|
* If you want to add additional methods to Rooted for a specific
|
|
|
|
* specialization, define a RootedBase<T> specialization containing them.
|
2012-01-21 07:49:33 +04:00
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-10 13:12:14 +03:00
|
|
|
class MOZ_RAII Rooted : public js::RootedBase<T, Rooted<T>> {
|
2017-02-02 22:12:43 +03:00
|
|
|
inline void registerWithRootLists(RootedListHeads& roots) {
|
2016-04-25 22:32:36 +03:00
|
|
|
this->stack = &roots[JS::MapTypeToRootKind<T>::kind];
|
2013-07-10 19:29:52 +04:00
|
|
|
this->prev = *stack;
|
|
|
|
*stack = reinterpret_cast<Rooted<void*>*>(this);
|
2013-02-07 23:34:43 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
inline RootedListHeads& rootLists(RootingContext* cx) {
|
|
|
|
return cx->stackRoots_;
|
2016-04-25 22:32:36 +03:00
|
|
|
}
|
2017-02-02 22:12:43 +03:00
|
|
|
inline RootedListHeads& rootLists(JSContext* cx) {
|
|
|
|
return rootLists(RootingContext::get(cx));
|
2016-02-12 00:04:10 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-21 22:57:00 +03:00
|
|
|
public:
|
2016-10-29 01:11:56 +03:00
|
|
|
using ElementType = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-21 22:57:00 +03:00
|
|
|
template <typename RootingContext>
|
2015-09-15 22:12:26 +03:00
|
|
|
explicit Rooted(const RootingContext& cx) : ptr(SafelyInitialized<T>()) {
|
2016-02-12 00:04:10 +03:00
|
|
|
registerWithRootLists(rootLists(cx));
|
2013-06-13 01:17:54 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-21 22:57:00 +03:00
|
|
|
template <typename RootingContext, typename S>
|
2015-09-15 22:12:26 +03:00
|
|
|
Rooted(const RootingContext& cx, S&& initial)
|
2018-06-01 19:30:30 +03:00
|
|
|
: ptr(std::forward<S>(initial)) {
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(GCPolicy<T>::isValid(ptr));
|
2016-02-12 00:04:10 +03:00
|
|
|
registerWithRootLists(rootLists(cx));
|
2013-02-07 23:34:43 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-09-23 23:28:08 +04:00
|
|
|
~Rooted() {
|
2014-02-18 10:24:15 +04:00
|
|
|
MOZ_ASSERT(*stack == reinterpret_cast<Rooted<void*>*>(this));
|
2012-01-21 07:49:33 +04:00
|
|
|
*stack = prev;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
Rooted<T>* previous() { return reinterpret_cast<Rooted<T>*>(prev); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-05-25 06:38:09 +04:00
|
|
|
/*
|
2015-02-05 02:27:00 +03:00
|
|
|
* This method is public for Rooted so that Codegen.py can use a Rooted
|
|
|
|
* interchangeably with a MutableHandleValue.
|
2013-05-25 06:38:09 +04:00
|
|
|
*/
|
2016-09-11 12:15:22 +03:00
|
|
|
void set(const T& value) {
|
2013-06-08 06:45:45 +04:00
|
|
|
ptr = value;
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(GCPolicy<T>::isValid(ptr));
|
2013-06-08 06:45:45 +04:00
|
|
|
}
|
2017-06-12 12:43:46 +03:00
|
|
|
void set(T&& value) {
|
2018-05-30 22:15:35 +03:00
|
|
|
ptr = std::move(value);
|
2017-09-19 14:31:31 +03:00
|
|
|
MOZ_ASSERT(GCPolicy<T>::isValid(ptr));
|
2017-06-12 12:43:46 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(Rooted, T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr);
|
|
|
|
DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS(ptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2012-01-21 07:49:33 +04:00
|
|
|
private:
|
2014-06-05 18:38:00 +04:00
|
|
|
/*
|
|
|
|
* These need to be templated on void* to avoid aliasing issues between, for
|
|
|
|
* example, Rooted<JSObject> and Rooted<JSFunction>, which use the same
|
|
|
|
* stack head pointer for different classes.
|
|
|
|
*/
|
2015-04-02 12:22:21 +03:00
|
|
|
Rooted<void*>** stack;
|
|
|
|
Rooted<void*>* prev;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-13 23:22:20 +03:00
|
|
|
detail::MaybeWrapped<T> ptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
Rooted(const Rooted&) = delete;
|
2015-09-29 23:39:33 +03:00
|
|
|
} JS_HAZ_ROOTED;
|
2012-01-21 07:49:33 +04:00
|
|
|
|
2013-03-06 20:41:38 +04:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
/*
|
|
|
|
* Inlinable accessors for JSContext.
|
|
|
|
*
|
|
|
|
* - These must not be available on the more restricted superclasses of
|
|
|
|
* JSContext, so we can't simply define them on RootingContext.
|
|
|
|
*
|
|
|
|
* - They're perfectly ordinary JSContext functionality, so ought to be
|
|
|
|
* usable without resorting to jsfriendapi.h, and when JSContext is an
|
|
|
|
* incomplete type.
|
|
|
|
*/
|
2018-05-31 12:28:48 +03:00
|
|
|
inline JS::Realm* GetContextRealm(const JSContext* cx) {
|
|
|
|
return JS::RootingContext::get(cx)->realm_;
|
|
|
|
}
|
|
|
|
|
2017-11-16 15:21:07 +03:00
|
|
|
inline JS::Compartment* GetContextCompartment(const JSContext* cx) {
|
2018-09-06 13:11:07 +03:00
|
|
|
if (JS::Realm* realm = GetContextRealm(cx)) {
|
2018-06-06 15:30:52 +03:00
|
|
|
return GetCompartmentForRealm(realm);
|
2018-09-06 13:11:07 +03:00
|
|
|
}
|
2018-06-06 15:30:52 +03:00
|
|
|
return nullptr;
|
2017-11-16 15:21:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline JS::Zone* GetContextZone(const JSContext* cx) {
|
|
|
|
return JS::RootingContext::get(cx)->zone_;
|
|
|
|
}
|
|
|
|
|
Bug 1499507 - Fold the 'profiler is active' check into the 'JSContext has a non-null PseudoStack' check. r=sfink
This eliminates a few instructions from every profiler label and saves code size.
We have around 9000 WebIDL constructors + methods + getters + setters which all
have an inlined instance of this code.
This change reduces the binary size on Linux x64 by around 160KB.
Here's a diff of the impact on the code generated for Attr_Binding::get_specified
in the Mac build:
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rcx, %r14
movq %rdx, %r15
- movq __ZN7mozilla8profiler6detail12RacyFeatures18sActiveAndFeaturesE@GOT, %rax ; __ZN7mozilla8profiler6detail12RacyFeatures18sActiveAndFeaturesE@GOT
- movl (%rax), %eax
- testl %eax, %eax
- js loc_xxxxx
-
- movq $0x0, -40(%rbp)
- jmp loc_xxxxx
-
- movq 0x78(%rdi), %rbx
+ movq 0x80(%rdi), %rbx
movq %rbx, -40(%rbp)
testq %rbx, %rbx
je loc_xxxxx
movl 0x10(%rbx), %r12d
cmpl %r12d, (%rbx)
jbe loc_xxxxx
Differential Revision: https://phabricator.services.mozilla.com/D9192
--HG--
extra : moz-landing-system : lando
2018-11-06 07:29:35 +03:00
|
|
|
inline ProfilingStack* GetContextProfilingStackIfEnabled(JSContext* cx) {
|
|
|
|
return JS::RootingContext::get(cx)
|
|
|
|
->geckoProfiler()
|
|
|
|
.getProfilingStackIfEnabled();
|
2018-01-05 16:35:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2014-02-13 21:17:43 +04:00
|
|
|
* Augment the generic Rooted<T> interface when T = JSObject* with
|
|
|
|
* class-querying and downcasting operations.
|
|
|
|
*
|
|
|
|
* Given a Rooted<JSObject*> obj, one can view
|
|
|
|
* Handle<StringObject*> h = obj.as<StringObject*>();
|
|
|
|
* as an optimization of
|
|
|
|
* Rooted<StringObject*> rooted(cx, &obj->as<StringObject*>());
|
|
|
|
* Handle<StringObject*> h = rooted;
|
|
|
|
*/
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename Container>
|
|
|
|
class RootedBase<JSObject*, Container>
|
|
|
|
: public MutableWrappedPtrOperations<JSObject*, Container> {
|
2014-02-13 21:17:43 +04:00
|
|
|
public:
|
|
|
|
template <class U>
|
|
|
|
JS::Handle<U*> as() const;
|
|
|
|
};
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2014-10-03 06:32:39 +04:00
|
|
|
* Augment the generic Handle<T> interface when T = JSObject* with
|
|
|
|
* downcasting operations.
|
|
|
|
*
|
|
|
|
* Given a Handle<JSObject*> obj, one can view
|
|
|
|
* Handle<StringObject*> h = obj.as<StringObject*>();
|
|
|
|
* as an optimization of
|
|
|
|
* Rooted<StringObject*> rooted(cx, &obj->as<StringObject*>());
|
|
|
|
* Handle<StringObject*> h = rooted;
|
|
|
|
*/
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename Container>
|
|
|
|
class HandleBase<JSObject*, Container>
|
|
|
|
: public WrappedPtrOperations<JSObject*, Container> {
|
2014-10-03 06:32:39 +04:00
|
|
|
public:
|
|
|
|
template <class U>
|
|
|
|
JS::Handle<U*> as() const;
|
|
|
|
};
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2013-01-19 07:35:08 +04:00
|
|
|
* Types for a variable that either should or shouldn't be rooted, depending on
|
2014-02-02 00:04:03 +04:00
|
|
|
* the template parameter allowGC. Used for implementing functions that can
|
2013-01-19 07:35:08 +04:00
|
|
|
* operate on either rooted or unrooted data.
|
2013-01-24 02:22:10 +04:00
|
|
|
*
|
|
|
|
* The toHandle() and toMutableHandle() functions are for calling functions
|
2013-01-25 07:18:34 +04:00
|
|
|
* which require handle types and are only called in the CanGC case. These
|
2013-01-24 02:22:10 +04:00
|
|
|
* allow the calling code to type check.
|
2013-01-19 07:35:08 +04:00
|
|
|
*/
|
|
|
|
enum AllowGC { NoGC = 0, CanGC = 1 };
|
|
|
|
template <typename T, AllowGC allowGC>
|
|
|
|
class MaybeRooted {};
|
2014-10-03 06:32:39 +04:00
|
|
|
|
2013-01-25 07:18:34 +04:00
|
|
|
template <typename T>
|
|
|
|
class MaybeRooted<T, CanGC> {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2013-03-19 14:35:41 +04:00
|
|
|
typedef JS::Handle<T> HandleType;
|
|
|
|
typedef JS::Rooted<T> RootType;
|
|
|
|
typedef JS::MutableHandle<T> MutableHandleType;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-03-19 14:35:41 +04:00
|
|
|
static inline JS::Handle<T> toHandle(HandleType v) { return v; }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-03-19 14:35:41 +04:00
|
|
|
static inline JS::MutableHandle<T> toMutableHandle(MutableHandleType v) {
|
2013-01-24 02:22:10 +04:00
|
|
|
return v;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2014-10-03 06:32:39 +04:00
|
|
|
template <typename T2>
|
|
|
|
static inline JS::Handle<T2*> downcastHandle(HandleType v) {
|
|
|
|
return v.template as<T2>();
|
|
|
|
}
|
2013-01-19 07:35:08 +04:00
|
|
|
};
|
|
|
|
|
2012-09-25 05:08:22 +04:00
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2012-11-15 04:06:27 +04:00
|
|
|
template <typename T>
|
|
|
|
template <typename S>
|
2015-03-29 01:22:11 +03:00
|
|
|
inline Handle<T>::Handle(
|
|
|
|
const Rooted<S>& root,
|
2012-09-25 05:08:22 +04:00
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy) {
|
2015-03-29 01:22:11 +03:00
|
|
|
ptr = reinterpret_cast<const T*>(root.address());
|
2012-09-25 05:08:22 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:35:08 +04:00
|
|
|
template <typename T>
|
|
|
|
template <typename S>
|
2015-03-29 01:22:11 +03:00
|
|
|
inline Handle<T>::Handle(
|
|
|
|
const PersistentRooted<S>& root,
|
2013-11-05 01:35:08 +04:00
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy) {
|
2015-03-29 01:22:11 +03:00
|
|
|
ptr = reinterpret_cast<const T*>(root.address());
|
2013-11-05 01:35:08 +04:00
|
|
|
}
|
|
|
|
|
2012-11-15 04:06:27 +04:00
|
|
|
template <typename T>
|
|
|
|
template <typename S>
|
2015-03-29 01:22:11 +03:00
|
|
|
inline Handle<T>::Handle(
|
|
|
|
MutableHandle<S>& root,
|
2012-09-25 05:08:22 +04:00
|
|
|
typename mozilla::EnableIf<mozilla::IsConvertible<S, T>::value, int>::Type
|
|
|
|
dummy) {
|
2015-03-29 01:22:11 +03:00
|
|
|
ptr = reinterpret_cast<const T*>(root.address());
|
2012-09-25 05:08:22 +04:00
|
|
|
}
|
|
|
|
|
2013-01-05 05:32:01 +04:00
|
|
|
template <typename T>
|
2015-03-29 01:22:11 +03:00
|
|
|
inline MutableHandle<T>::MutableHandle(Rooted<T>* root) {
|
|
|
|
static_assert(sizeof(MutableHandle<T>) == sizeof(T*),
|
2013-07-18 21:59:53 +04:00
|
|
|
"MutableHandle must be binary compatible with T*.");
|
2012-09-25 05:08:22 +04:00
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
2013-11-05 01:35:08 +04:00
|
|
|
template <typename T>
|
2015-03-29 01:22:11 +03:00
|
|
|
inline MutableHandle<T>::MutableHandle(PersistentRooted<T>* root) {
|
|
|
|
static_assert(sizeof(MutableHandle<T>) == sizeof(T*),
|
2013-11-05 01:35:08 +04:00
|
|
|
"MutableHandle must be binary compatible with T*.");
|
|
|
|
ptr = root->address();
|
|
|
|
}
|
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
JS_PUBLIC_API void AddPersistentRoot(RootingContext* cx, RootKind kind,
|
|
|
|
PersistentRooted<void*>* root);
|
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
JS_PUBLIC_API void AddPersistentRoot(JSRuntime* rt, RootKind kind,
|
|
|
|
PersistentRooted<void*>* root);
|
|
|
|
|
2015-10-17 20:27:16 +03:00
|
|
|
/**
|
2013-11-05 01:35:08 +04:00
|
|
|
* A copyable, assignable global GC root type with arbitrary lifetime, an
|
|
|
|
* infallible constructor, and automatic unrooting on destruction.
|
|
|
|
*
|
|
|
|
* These roots can be used in heap-allocated data structures, so they are not
|
|
|
|
* associated with any particular JSContext or stack. They are registered with
|
|
|
|
* the JSRuntime itself, without locking, so they require a full JSContext to be
|
2016-02-10 20:48:28 +03:00
|
|
|
* initialized, not one of its more restricted superclasses. Initialization may
|
2015-01-23 13:23:56 +03:00
|
|
|
* take place on construction, or in two phases if the no-argument constructor
|
|
|
|
* is called followed by init().
|
2013-11-05 01:35:08 +04:00
|
|
|
*
|
|
|
|
* Note that you must not use an PersistentRooted in an object owned by a JS
|
|
|
|
* object:
|
|
|
|
*
|
|
|
|
* Whenever one object whose lifetime is decided by the GC refers to another
|
|
|
|
* such object, that edge must be traced only if the owning JS object is traced.
|
|
|
|
* This applies not only to JS objects (which obviously are managed by the GC)
|
|
|
|
* but also to C++ objects owned by JS objects.
|
|
|
|
*
|
|
|
|
* If you put a PersistentRooted in such a C++ object, that is almost certainly
|
|
|
|
* a leak. When a GC begins, the referent of the PersistentRooted is treated as
|
|
|
|
* live, unconditionally (because a PersistentRooted is a *root*), even if the
|
|
|
|
* JS object that owns it is unreachable. If there is any path from that
|
|
|
|
* referent back to the JS object, then the C++ object containing the
|
|
|
|
* PersistentRooted will not be destructed, and the whole blob of objects will
|
|
|
|
* not be freed, even if there are no references to them from the outside.
|
|
|
|
*
|
|
|
|
* In the context of Firefox, this is a severe restriction: almost everything in
|
|
|
|
* Firefox is owned by some JS object or another, so using PersistentRooted in
|
|
|
|
* such objects would introduce leaks. For these kinds of edges, Heap<T> or
|
|
|
|
* TenuredHeap<T> would be better types. It's up to the implementor of the type
|
|
|
|
* containing Heap<T> or TenuredHeap<T> members to make sure their referents get
|
|
|
|
* marked when the object itself is marked.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2017-01-10 13:12:14 +03:00
|
|
|
class PersistentRooted
|
|
|
|
: public js::RootedBase<T, PersistentRooted<T>>,
|
2015-01-23 13:23:56 +03:00
|
|
|
private mozilla::LinkedListElement<PersistentRooted<T>> {
|
2016-01-28 00:03:52 +03:00
|
|
|
using ListBase = mozilla::LinkedListElement<PersistentRooted<T>>;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-22 15:28:06 +04:00
|
|
|
friend class mozilla::LinkedList<PersistentRooted>;
|
|
|
|
friend class mozilla::LinkedListElement<PersistentRooted>;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
void registerWithRootLists(RootingContext* cx) {
|
2015-01-23 13:23:56 +03:00
|
|
|
MOZ_ASSERT(!initialized());
|
2016-01-27 20:54:53 +03:00
|
|
|
JS::RootKind kind = JS::MapTypeToRootKind<T>::kind;
|
2017-02-02 22:12:43 +03:00
|
|
|
AddPersistentRoot(cx, kind,
|
|
|
|
reinterpret_cast<JS::PersistentRooted<void*>*>(this));
|
2016-02-12 00:04:10 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
void registerWithRootLists(JSRuntime* rt) {
|
|
|
|
MOZ_ASSERT(!initialized());
|
|
|
|
JS::RootKind kind = JS::MapTypeToRootKind<T>::kind;
|
|
|
|
AddPersistentRoot(rt, kind,
|
|
|
|
reinterpret_cast<JS::PersistentRooted<void*>*>(this));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-11-05 01:35:08 +04:00
|
|
|
public:
|
2016-10-29 01:11:56 +03:00
|
|
|
using ElementType = T;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
PersistentRooted() : ptr(SafelyInitialized<T>()) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
explicit PersistentRooted(RootingContext* cx) : ptr(SafelyInitialized<T>()) {
|
|
|
|
registerWithRootLists(cx);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
explicit PersistentRooted(JSContext* cx) : ptr(SafelyInitialized<T>()) {
|
|
|
|
registerWithRootLists(RootingContext::get(cx));
|
2013-11-05 01:35:08 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
template <typename U>
|
|
|
|
PersistentRooted(RootingContext* cx, U&& initial)
|
2018-06-01 19:30:30 +03:00
|
|
|
: ptr(std::forward<U>(initial)) {
|
2017-02-02 22:12:43 +03:00
|
|
|
registerWithRootLists(cx);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
template <typename U>
|
|
|
|
PersistentRooted(JSContext* cx, U&& initial) : ptr(std::forward<U>(initial)) {
|
|
|
|
registerWithRootLists(RootingContext::get(cx));
|
2013-11-05 01:35:08 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
explicit PersistentRooted(JSRuntime* rt) : ptr(SafelyInitialized<T>()) {
|
|
|
|
registerWithRootLists(rt);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-24 05:30:06 +03:00
|
|
|
template <typename U>
|
|
|
|
PersistentRooted(JSRuntime* rt, U&& initial) : ptr(std::forward<U>(initial)) {
|
|
|
|
registerWithRootLists(rt);
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-03-29 01:22:11 +03:00
|
|
|
PersistentRooted(const PersistentRooted& rhs)
|
2015-01-23 13:23:56 +03:00
|
|
|
: mozilla::LinkedListElement<PersistentRooted<T>>(), ptr(rhs.ptr) {
|
2013-11-05 01:35:08 +04:00
|
|
|
/*
|
|
|
|
* Copy construction takes advantage of the fact that the original
|
|
|
|
* is already inserted, and simply adds itself to whatever list the
|
|
|
|
* original was on - no JSRuntime pointer needed.
|
2014-06-23 23:59:19 +04:00
|
|
|
*
|
|
|
|
* This requires mutating rhs's links, but those should be 'mutable'
|
|
|
|
* anyway. C++ doesn't let us declare mutable base classes.
|
2013-11-05 01:35:08 +04:00
|
|
|
*/
|
2015-03-29 01:22:11 +03:00
|
|
|
const_cast<PersistentRooted&>(rhs).setNext(this);
|
2013-11-05 01:35:08 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-23 13:23:56 +03:00
|
|
|
bool initialized() { return ListBase::isInList(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-05-25 12:01:25 +03:00
|
|
|
void init(JSContext* cx) { init(cx, SafelyInitialized<T>()); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-02-02 22:12:43 +03:00
|
|
|
template <typename U>
|
|
|
|
void init(JSContext* cx, U&& initial) {
|
2018-06-01 19:30:30 +03:00
|
|
|
ptr = std::forward<U>(initial);
|
2017-02-02 22:12:43 +03:00
|
|
|
registerWithRootLists(RootingContext::get(cx));
|
2015-01-23 13:23:56 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-23 13:23:56 +03:00
|
|
|
void reset() {
|
|
|
|
if (initialized()) {
|
2018-05-25 12:01:25 +03:00
|
|
|
set(SafelyInitialized<T>());
|
2015-01-23 13:23:56 +03:00
|
|
|
ListBase::remove();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-23 13:23:56 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
DECLARE_POINTER_CONSTREF_OPS(T);
|
|
|
|
DECLARE_POINTER_ASSIGN_OPS(PersistentRooted, T);
|
|
|
|
DECLARE_NONPOINTER_ACCESSOR_METHODS(ptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-07-22 18:31:49 +03:00
|
|
|
// These are the same as DECLARE_NONPOINTER_MUTABLE_ACCESSOR_METHODS, except
|
|
|
|
// they check that |this| is initialized in case the caller later stores
|
|
|
|
// something in |ptr|.
|
|
|
|
T* address() {
|
|
|
|
MOZ_ASSERT(initialized());
|
|
|
|
return &ptr;
|
|
|
|
}
|
|
|
|
T& get() {
|
|
|
|
MOZ_ASSERT(initialized());
|
|
|
|
return ptr;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-02-05 02:27:00 +03:00
|
|
|
private:
|
2016-02-10 23:28:18 +03:00
|
|
|
template <typename U>
|
|
|
|
void set(U&& value) {
|
2015-01-23 13:23:56 +03:00
|
|
|
MOZ_ASSERT(initialized());
|
2018-06-01 19:30:30 +03:00
|
|
|
ptr = std::forward<U>(value);
|
2013-11-05 01:35:08 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-04-13 23:22:20 +03:00
|
|
|
detail::MaybeWrapped<T> ptr;
|
2015-09-29 23:39:33 +03:00
|
|
|
} JS_HAZ_ROOTED;
|
2013-11-05 01:35:08 +04:00
|
|
|
|
2012-09-25 05:08:22 +04:00
|
|
|
} /* namespace JS */
|
|
|
|
|
|
|
|
namespace js {
|
2016-08-25 11:28:47 +03:00
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename T, typename D, typename Container>
|
|
|
|
class WrappedPtrOperations<UniquePtr<T, D>, Container> {
|
|
|
|
const UniquePtr<T, D>& uniquePtr() const {
|
|
|
|
return static_cast<const Container*>(this)->get();
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-25 11:28:47 +03:00
|
|
|
public:
|
|
|
|
explicit operator bool() const { return !!uniquePtr(); }
|
2017-06-12 12:43:46 +03:00
|
|
|
T* get() const { return uniquePtr().get(); }
|
|
|
|
T* operator->() const { return get(); }
|
|
|
|
T& operator*() const { return *uniquePtr(); }
|
2016-08-25 11:28:47 +03:00
|
|
|
};
|
|
|
|
|
2017-01-10 13:12:14 +03:00
|
|
|
template <typename T, typename D, typename Container>
|
|
|
|
class MutableWrappedPtrOperations<UniquePtr<T, D>, Container>
|
|
|
|
: public WrappedPtrOperations<UniquePtr<T, D>, Container> {
|
|
|
|
UniquePtr<T, D>& uniquePtr() { return static_cast<Container*>(this)->get(); }
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-25 11:28:47 +03:00
|
|
|
public:
|
|
|
|
MOZ_MUST_USE typename UniquePtr<T, D>::Pointer release() {
|
|
|
|
return uniquePtr().release();
|
|
|
|
}
|
2017-06-12 12:43:46 +03:00
|
|
|
void reset(T* ptr = T()) { uniquePtr().reset(ptr); }
|
2016-08-25 11:28:47 +03:00
|
|
|
};
|
|
|
|
|
2014-08-01 01:43:45 +04:00
|
|
|
namespace gc {
|
|
|
|
|
|
|
|
template <typename T, typename TraceCallbacks>
|
2015-03-29 01:22:11 +03:00
|
|
|
void CallTraceCallbackOnNonHeap(T* v, const TraceCallbacks& aCallbacks,
|
|
|
|
const char* aName, void* aClosure) {
|
2014-08-01 01:43:45 +04:00
|
|
|
static_assert(sizeof(T) == sizeof(JS::Heap<T>),
|
|
|
|
"T and Heap<T> must be compatible.");
|
|
|
|
MOZ_ASSERT(v);
|
2015-12-28 21:11:40 +03:00
|
|
|
mozilla::DebugOnly<Cell*> cell = BarrierMethods<T>::asGCThingOrNull(*v);
|
2014-08-01 01:43:45 +04:00
|
|
|
MOZ_ASSERT(cell);
|
|
|
|
MOZ_ASSERT(!IsInsideNursery(cell));
|
2015-03-29 01:22:11 +03:00
|
|
|
JS::Heap<T>* asHeapT = reinterpret_cast<JS::Heap<T>*>(v);
|
2014-08-01 01:43:45 +04:00
|
|
|
aCallbacks.Trace(asHeapT, aName, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace gc */
|
|
|
|
} /* namespace js */
|
2012-04-12 20:23:51 +04:00
|
|
|
|
2015-08-18 22:25:01 +03:00
|
|
|
// mozilla::Swap uses a stack temporary, which prevents classes like Heap<T>
|
|
|
|
// from being declared MOZ_HEAP_CLASS.
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void Swap(JS::Heap<T>& aX, JS::Heap<T>& aY) {
|
|
|
|
T tmp = aX;
|
|
|
|
aX = aY;
|
|
|
|
aY = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void Swap(JS::TenuredHeap<T>& aX, JS::TenuredHeap<T>& aY) {
|
|
|
|
T tmp = aX;
|
|
|
|
aX = aY;
|
|
|
|
aY = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
2016-10-29 01:11:56 +03:00
|
|
|
namespace js {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// DefineComparisonOps is a trait which selects which wrapper classes to define
|
|
|
|
// operator== and operator!= for. It supplies a getter function to extract the
|
|
|
|
// value to compare. This is used to avoid triggering the automatic read
|
|
|
|
// barriers where appropriate.
|
|
|
|
//
|
|
|
|
// If DefineComparisonOps is not specialized for a particular wrapper you may
|
|
|
|
// get errors such as 'invalid operands to binary expression' or 'no match for
|
|
|
|
// operator==' when trying to compare against instances of the wrapper.
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps : mozilla::FalseType {};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<JS::Heap<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const JS::Heap<T>& v) { return v.unbarrieredGet(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<JS::TenuredHeap<T>> : mozilla::TrueType {
|
|
|
|
static const T get(const JS::TenuredHeap<T>& v) {
|
|
|
|
return v.unbarrieredGetPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<JS::Rooted<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const JS::Rooted<T>& v) { return v.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<JS::Handle<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const JS::Handle<T>& v) { return v.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<JS::MutableHandle<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const JS::MutableHandle<T>& v) { return v.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<JS::PersistentRooted<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const JS::PersistentRooted<T>& v) { return v.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<js::FakeRooted<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const js::FakeRooted<T>& v) { return v.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct DefineComparisonOps<js::FakeMutableHandle<T>> : mozilla::TrueType {
|
|
|
|
static const T& get(const js::FakeMutableHandle<T>& v) { return v.get(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace detail */
|
|
|
|
} /* namespace js */
|
|
|
|
|
|
|
|
// Overload operator== and operator!= for all types with the DefineComparisonOps
|
|
|
|
// trait using the supplied getter.
|
|
|
|
//
|
|
|
|
// There are four cases:
|
|
|
|
|
|
|
|
// Case 1: comparison between two wrapper objects.
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
typename mozilla::EnableIf<js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
js::detail::DefineComparisonOps<U>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(const T& a, const U& b) {
|
|
|
|
return js::detail::DefineComparisonOps<T>::get(a) ==
|
|
|
|
js::detail::DefineComparisonOps<U>::get(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
typename mozilla::EnableIf<js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
js::detail::DefineComparisonOps<U>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(const T& a, const U& b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: comparison between a wrapper object and its unwrapped element type.
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<js::detail::DefineComparisonOps<T>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(const T& a, const typename T::ElementType& b) {
|
|
|
|
return js::detail::DefineComparisonOps<T>::get(a) == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<js::detail::DefineComparisonOps<T>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(const T& a, const typename T::ElementType& b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<js::detail::DefineComparisonOps<T>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(const typename T::ElementType& a, const T& b) {
|
|
|
|
return a == js::detail::DefineComparisonOps<T>::get(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<js::detail::DefineComparisonOps<T>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(const typename T::ElementType& a, const T& b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 3: For pointer wrappers, comparison between the wrapper and a const
|
|
|
|
// element pointer.
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(
|
|
|
|
const typename mozilla::RemovePointer<typename T::ElementType>::Type* a,
|
|
|
|
const T& b) {
|
|
|
|
return a == js::detail::DefineComparisonOps<T>::get(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(
|
|
|
|
const typename mozilla::RemovePointer<typename T::ElementType>::Type* a,
|
|
|
|
const T& b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(
|
|
|
|
const T& a,
|
|
|
|
const typename mozilla::RemovePointer<typename T::ElementType>::Type* b) {
|
|
|
|
return js::detail::DefineComparisonOps<T>::get(a) == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(
|
|
|
|
const T& a,
|
|
|
|
const typename mozilla::RemovePointer<typename T::ElementType>::Type* b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 4: For pointer wrappers, comparison between the wrapper and nullptr.
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(std::nullptr_t a, const T& b) {
|
|
|
|
return a == js::detail::DefineComparisonOps<T>::get(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(std::nullptr_t a, const T& b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator==(const T& a, std::nullptr_t b) {
|
|
|
|
return js::detail::DefineComparisonOps<T>::get(a) == b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename mozilla::EnableIf<
|
|
|
|
js::detail::DefineComparisonOps<T>::value &&
|
|
|
|
mozilla::IsPointer<typename T::ElementType>::value,
|
|
|
|
bool>::Type
|
|
|
|
operator!=(const T& a, std::nullptr_t b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2013-06-20 04:59:09 +04:00
|
|
|
#endif /* js_RootingAPI_h */
|