Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : /**
8 : * An implementation of Rooted for OwningNonNull<T>. This works by assuming
9 : * that T has a Trace() method defined on it which will trace whatever things
10 : * inside the T instance need tracing.
11 : *
12 : * This implementation has one serious drawback: operator= doesn't work right
13 : * because it's declared on Rooted directly and expects the type Rooted is
14 : * templated over.
15 : */
16 :
17 : #ifndef mozilla_RootedOwningNonNull_h__
18 : #define mozilla_RootedOwningNonNull_h__
19 :
20 : #include "mozilla/OwningNonNull.h"
21 : #include "js/GCPolicyAPI.h"
22 : #include "js/RootingAPI.h"
23 :
24 : namespace JS {
25 : template<typename T>
26 : struct GCPolicy<mozilla::OwningNonNull<T>>
27 : {
28 : typedef mozilla::OwningNonNull<T> SmartPtrType;
29 :
30 19 : static SmartPtrType initial()
31 : {
32 19 : return SmartPtrType();
33 : }
34 :
35 0 : static void trace(JSTracer* trc, SmartPtrType* tp,
36 : const char* name)
37 : {
38 : // We have to be very careful here. Normally, OwningNonNull can't be null.
39 : // But binding code can end up in a situation where it sets up a
40 : // Rooted<OwningNonNull> and then before it gets a chance to assign to it
41 : // (e.g. from the constructor of the thing being assigned) a GC happens. So
42 : // we can land here when *tp stores a null pointer because it's not
43 : // initialized.
44 : //
45 : // So we need to check for that before jumping.
46 0 : if ((*tp).isInitialized()) {
47 0 : (*tp)->Trace(trc);
48 : }
49 0 : }
50 : };
51 : } // namespace JS
52 :
53 : namespace js {
54 : template<typename T, typename Wrapper>
55 19 : struct WrappedPtrOperations<mozilla::OwningNonNull<T>, Wrapper>
56 : {
57 19 : operator T& () const
58 : {
59 19 : return static_cast<const Wrapper*>(this)->get();
60 : }
61 : };
62 : } // namespace js
63 :
64 : #endif /* mozilla_RootedOwningNonNull_h__ */
|