2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef InferredValue_h
27 #define InferredValue_h
30 #include "Watchpoint.h"
31 #include "WriteBarrier.h"
35 // Allocate one of these if you'd like to infer a constant value. Writes to the value should use
36 // notifyWrite(). So long as exactly one value had ever been written and invalidate() has never been
37 // called, and you register a watchpoint, you can rely on the inferredValue() being the one true
40 // Commonly used for inferring singletons - in that case each allocation does notifyWrite(). But you
41 // can use it for other things as well.
43 class InferredValue
: public JSCell
{
47 static InferredValue
* create(VM
&);
49 static const bool needsDestruction
= true;
50 static void destroy(JSCell
*);
52 static Structure
* createStructure(VM
&, JSGlobalObject
*, JSValue prototype
);
54 static void visitChildren(JSCell
*, SlotVisitor
&);
58 // For the purpose of deciding whether or not to watch this variable, you only need
59 // to inspect inferredValue(). If this returns something other than the empty
60 // value, then it means that at all future safepoints, this watchpoint set will be
61 // in one of these states:
63 // IsWatched: in this case, the variable's value must still be the
66 // IsInvalidated: in this case the variable's value may be anything but you'll
67 // either notice that it's invalidated and not install the watchpoint, or
68 // you will have been notified that the watchpoint was fired.
69 JSValue
inferredValue() { return m_value
.get(); }
71 // Forwards some WatchpointSet methods.
72 WatchpointState
state() const { return m_set
.state(); }
73 bool isStillValid() const { return m_set
.isStillValid(); }
74 bool hasBeenInvalidated() const { return m_set
.hasBeenInvalidated(); }
75 void add(Watchpoint
* watchpoint
) { m_set
.add(watchpoint
); }
77 void notifyWrite(VM
& vm
, JSValue value
, const FireDetail
& detail
)
79 if (LIKELY(m_set
.stateOnJSThread() == IsInvalidated
))
81 notifyWriteSlow(vm
, value
, detail
);
84 void notifyWrite(VM
& vm
, JSValue value
, const char* reason
)
86 if (LIKELY(m_set
.stateOnJSThread() == IsInvalidated
))
88 notifyWriteSlow(vm
, value
, reason
);
91 void invalidate(const FireDetail
& detail
)
94 m_set
.invalidate(detail
);
97 static const unsigned StructureFlags
= StructureIsImmortal
| Base::StructureFlags
;
99 // We could have used Weak<>. But we want arbitrary JSValues, not just cells. It's also somewhat
100 // convenient to have eager notification of death.
102 // Also note that this should be a private class, but it isn't because Windows.
103 class ValueCleanup
: public UnconditionalFinalizer
{
104 WTF_MAKE_FAST_ALLOCATED
;
107 ValueCleanup(InferredValue
*);
108 virtual ~ValueCleanup();
111 void finalizeUnconditionally() override
;
114 InferredValue
* m_owner
;
121 JS_EXPORT_PRIVATE
void notifyWriteSlow(VM
&, JSValue
, const FireDetail
&);
122 JS_EXPORT_PRIVATE
void notifyWriteSlow(VM
&, JSValue
, const char* reason
);
124 friend class ValueCleanup
;
126 InlineWatchpointSet m_set
;
127 WriteBarrier
<Unknown
> m_value
;
128 std::unique_ptr
<ValueCleanup
> m_cleanup
;
131 // FIXME: We could have an InlineInferredValue, which only allocates the InferredValue object when
132 // a notifyWrite() transitions us towards watching, and then clears the reference (allowing the object
133 // to die) when we get invalidated.
137 #endif // InferredValue_h