]>
Commit | Line | Data |
---|---|---|
81345200 A |
1 | /* |
2 | * Copyright (C) 2013 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 | #ifndef DFGLazyJSValue_h | |
27 | #define DFGLazyJSValue_h | |
28 | ||
29 | #if ENABLE(DFG_JIT) | |
30 | ||
31 | #include "JSCJSValue.h" | |
32 | #include <wtf/text/StringImpl.h> | |
33 | ||
34 | namespace JSC { namespace DFG { | |
35 | ||
36 | // Represents either a JSValue, or for JSValues that require allocation in the heap, | |
37 | // it tells you everything you'd need to know in order to allocate it. | |
38 | ||
39 | enum LazinessKind { | |
40 | KnownValue, | |
41 | SingleCharacterString, | |
42 | KnownStringImpl | |
43 | }; | |
44 | ||
45 | class LazyJSValue { | |
46 | public: | |
47 | LazyJSValue(JSValue value = JSValue()) | |
48 | : m_kind(KnownValue) | |
49 | { | |
50 | u.value = JSValue::encode(value); | |
51 | } | |
52 | ||
53 | static LazyJSValue singleCharacterString(UChar character) | |
54 | { | |
55 | LazyJSValue result; | |
56 | result.m_kind = SingleCharacterString; | |
57 | result.u.character = character; | |
58 | return result; | |
59 | } | |
60 | ||
61 | static LazyJSValue knownStringImpl(StringImpl* string) | |
62 | { | |
63 | LazyJSValue result; | |
64 | result.m_kind = KnownStringImpl; | |
65 | result.u.stringImpl = string; | |
66 | return result; | |
67 | } | |
68 | ||
69 | JSValue tryGetValue() const | |
70 | { | |
71 | if (m_kind == KnownValue) | |
72 | return value(); | |
73 | return JSValue(); | |
74 | } | |
75 | ||
76 | JSValue getValue(VM&) const; | |
77 | ||
78 | JSValue value() const | |
79 | { | |
80 | ASSERT(m_kind == KnownValue); | |
81 | return JSValue::decode(u.value); | |
82 | } | |
83 | ||
84 | UChar character() const | |
85 | { | |
86 | ASSERT(m_kind == SingleCharacterString); | |
87 | return u.character; | |
88 | } | |
89 | ||
90 | StringImpl* stringImpl() const | |
91 | { | |
92 | ASSERT(m_kind == KnownStringImpl); | |
93 | return u.stringImpl; | |
94 | } | |
95 | ||
96 | TriState strictEqual(const LazyJSValue& other) const; | |
97 | ||
98 | unsigned switchLookupValue() const | |
99 | { | |
100 | // NB. Not every kind of JSValue will be able to give you a switch lookup | |
101 | // value, and this method will assert, or do bad things, if you use it | |
102 | // for a kind of value that can't. | |
103 | switch (m_kind) { | |
104 | case KnownValue: | |
105 | return value().asInt32(); | |
106 | case SingleCharacterString: | |
107 | return character(); | |
108 | default: | |
109 | RELEASE_ASSERT_NOT_REACHED(); | |
110 | return 0; | |
111 | } | |
112 | } | |
113 | ||
114 | void dump(PrintStream&) const; | |
115 | void dumpInContext(PrintStream&, DumpContext*) const; | |
116 | ||
117 | private: | |
118 | union { | |
119 | EncodedJSValue value; | |
120 | UChar character; | |
121 | StringImpl* stringImpl; | |
122 | } u; | |
123 | LazinessKind m_kind; | |
124 | }; | |
125 | ||
126 | } } // namespace JSC::DFG | |
127 | ||
128 | #endif // ENABLE(DFG_JIT) | |
129 | ||
130 | #endif // DFGLazyJSValue_h | |
131 |