1 | /* |
2 | * Copyright (C) 2015-2017 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
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. |
12 | * |
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. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "IsoSubspace.h" |
29 | #include "JSCast.h" |
30 | #include "VM.h" |
31 | #include "Watchpoint.h" |
32 | #include "WriteBarrier.h" |
33 | |
34 | namespace JSC { |
35 | |
36 | // Allocate one of these if you'd like to infer a constant value. Writes to the value should use |
37 | // notifyWrite(). So long as exactly one value had ever been written and invalidate() has never been |
38 | // called, and you register a watchpoint, you can rely on the inferredValue() being the one true |
39 | // value. |
40 | // |
41 | // Commonly used for inferring singletons - in that case each allocation does notifyWrite(). But you |
42 | // can use it for other things as well. |
43 | |
44 | class InferredValue final : public JSCell { |
45 | public: |
46 | typedef JSCell Base; |
47 | |
48 | template<typename CellType, SubspaceAccess mode> |
49 | static IsoSubspace* subspaceFor(VM& vm) |
50 | { |
51 | return vm.inferredValueSpace<mode>(); |
52 | } |
53 | |
54 | static InferredValue* create(VM&); |
55 | |
56 | static const bool needsDestruction = true; |
57 | static void destroy(JSCell*); |
58 | |
59 | static Structure* createStructure(VM&, JSGlobalObject*, JSValue prototype); |
60 | |
61 | static void visitChildren(JSCell*, SlotVisitor&); |
62 | |
63 | DECLARE_INFO; |
64 | |
65 | // For the purpose of deciding whether or not to watch this variable, you only need |
66 | // to inspect inferredValue(). If this returns something other than the empty |
67 | // value, then it means that at all future safepoints, this watchpoint set will be |
68 | // in one of these states: |
69 | // |
70 | // IsWatched: in this case, the variable's value must still be the |
71 | // inferredValue. |
72 | // |
73 | // IsInvalidated: in this case the variable's value may be anything but you'll |
74 | // either notice that it's invalidated and not install the watchpoint, or |
75 | // you will have been notified that the watchpoint was fired. |
76 | JSValue inferredValue() { return m_value.get(); } |
77 | |
78 | // Forwards some WatchpointSet methods. |
79 | WatchpointState state() const { return m_set.state(); } |
80 | bool isStillValid() const { return m_set.isStillValid(); } |
81 | bool hasBeenInvalidated() const { return m_set.hasBeenInvalidated(); } |
82 | void add(Watchpoint* watchpoint) { m_set.add(watchpoint); } |
83 | |
84 | void notifyWrite(VM& vm, JSValue value, const FireDetail& detail) |
85 | { |
86 | if (LIKELY(m_set.stateOnJSThread() == IsInvalidated)) |
87 | return; |
88 | notifyWriteSlow(vm, value, detail); |
89 | } |
90 | |
91 | void notifyWrite(VM& vm, JSValue value, const char* reason) |
92 | { |
93 | if (LIKELY(m_set.stateOnJSThread() == IsInvalidated)) |
94 | return; |
95 | notifyWriteSlow(vm, value, reason); |
96 | } |
97 | |
98 | void invalidate(VM& vm, const FireDetail& detail) |
99 | { |
100 | m_value.clear(); |
101 | m_set.invalidate(vm, detail); |
102 | } |
103 | |
104 | static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; |
105 | |
106 | void finalizeUnconditionally(VM&); |
107 | |
108 | private: |
109 | InferredValue(VM&); |
110 | ~InferredValue(); |
111 | |
112 | JS_EXPORT_PRIVATE void notifyWriteSlow(VM&, JSValue, const FireDetail&); |
113 | JS_EXPORT_PRIVATE void notifyWriteSlow(VM&, JSValue, const char* reason); |
114 | |
115 | InlineWatchpointSet m_set; |
116 | WriteBarrier<Unknown> m_value; |
117 | }; |
118 | |
119 | // FIXME: We could have an InlineInferredValue, which only allocates the InferredValue object when |
120 | // a notifyWrite() transitions us towards watching, and then clears the reference (allowing the object |
121 | // to die) when we get invalidated. |
122 | |
123 | } // namespace JSC |
124 | |