]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/InferredValue.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / InferredValue.cpp
1 /*
2 * Copyright (C) 2015 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 #include "config.h"
27 #include "InferredValue.h"
28
29 #include "JSCInlines.h"
30
31 namespace JSC {
32
33 const ClassInfo InferredValue::s_info = { "InferredValue", 0, 0, CREATE_METHOD_TABLE(InferredValue) };
34
35 InferredValue* InferredValue::create(VM& vm)
36 {
37 InferredValue* result = new (NotNull, allocateCell<InferredValue>(vm.heap)) InferredValue(vm);
38 result->finishCreation(vm);
39 return result;
40 }
41
42 void InferredValue::destroy(JSCell* cell)
43 {
44 InferredValue* inferredValue = static_cast<InferredValue*>(cell);
45 inferredValue->InferredValue::~InferredValue();
46 }
47
48 Structure* InferredValue::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
49 {
50 return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
51 }
52
53 void InferredValue::visitChildren(JSCell* cell, SlotVisitor& visitor)
54 {
55 InferredValue* inferredValue = jsCast<InferredValue*>(cell);
56
57 if (inferredValue->m_set.hasBeenInvalidated()) {
58 inferredValue->m_cleanup = nullptr;
59 return;
60 }
61
62 if (!inferredValue->m_value)
63 return;
64 if (!inferredValue->m_value.get().isCell())
65 return;
66
67 if (!inferredValue->m_cleanup)
68 inferredValue->m_cleanup = std::make_unique<ValueCleanup>(inferredValue);
69 visitor.addUnconditionalFinalizer(inferredValue->m_cleanup.get());
70 }
71
72 InferredValue::InferredValue(VM& vm)
73 : Base(vm, vm.inferredValueStructure.get())
74 , m_set(ClearWatchpoint)
75 {
76 }
77
78 InferredValue::~InferredValue()
79 {
80 }
81
82 void InferredValue::notifyWriteSlow(VM& vm, JSValue value, const FireDetail& detail)
83 {
84 ASSERT(!!value);
85 switch (m_set.state()) {
86 case ClearWatchpoint:
87 m_value.set(vm, this, value);
88 m_set.startWatching();
89 return;
90
91 case IsWatched:
92 ASSERT(!!m_value);
93 if (m_value.get() == value)
94 return;
95 invalidate(detail);
96 return;
97
98 case IsInvalidated:
99 ASSERT_NOT_REACHED();
100 return;
101 }
102
103 ASSERT_NOT_REACHED();
104 }
105
106 void InferredValue::notifyWriteSlow(VM& vm, JSValue value, const char* reason)
107 {
108 notifyWriteSlow(vm, value, StringFireDetail(reason));
109 }
110
111 InferredValue::ValueCleanup::ValueCleanup(InferredValue* owner)
112 : m_owner(owner)
113 {
114 }
115
116 InferredValue::ValueCleanup::~ValueCleanup()
117 {
118 }
119
120 void InferredValue::ValueCleanup::finalizeUnconditionally()
121 {
122 ASSERT(m_owner->m_value);
123 ASSERT(m_owner->m_value.get().isCell());
124
125 if (Heap::isMarked(m_owner->m_value.get().asCell()))
126 return;
127
128 m_owner->invalidate(StringFireDetail("InferredValue clean-up during GC"));
129 }
130
131 } // namespace JSC
132