1/*
2 * Copyright (C) 2008-2019 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include "SourceProvider.h"
32#include <wtf/RefPtr.h>
33
34namespace JSC {
35
36 class UnlinkedSourceCode {
37 template<typename SourceType>
38 friend class CachedUnlinkedSourceCodeShape;
39 friend class CachedSourceCodeWithoutProvider;
40
41 public:
42 UnlinkedSourceCode()
43 : m_provider(0)
44 , m_startOffset(0)
45 , m_endOffset(0)
46 {
47 }
48
49 UnlinkedSourceCode(WTF::HashTableDeletedValueType)
50 : m_provider(WTF::HashTableDeletedValue)
51 {
52 }
53
54 UnlinkedSourceCode(Ref<SourceProvider>&& provider)
55 : m_provider(WTFMove(provider))
56 , m_startOffset(0)
57 , m_endOffset(m_provider->source().length())
58 {
59 }
60
61 UnlinkedSourceCode(Ref<SourceProvider>&& provider, int startOffset, int endOffset)
62 : m_provider(WTFMove(provider))
63 , m_startOffset(startOffset)
64 , m_endOffset(endOffset)
65 {
66 }
67
68 UnlinkedSourceCode(RefPtr<SourceProvider>&& provider, int startOffset, int endOffset)
69 : m_provider(WTFMove(provider))
70 , m_startOffset(startOffset)
71 , m_endOffset(endOffset)
72 {
73 }
74
75 bool isHashTableDeletedValue() const { return m_provider.isHashTableDeletedValue(); }
76
77 SourceProvider& provider() const
78 {
79 return *m_provider;
80 }
81
82 unsigned hash() const
83 {
84 ASSERT(m_provider);
85 return m_provider->hash();
86 }
87
88 StringView view() const
89 {
90 if (!m_provider)
91 return StringView();
92 return m_provider->getRange(m_startOffset, m_endOffset);
93 }
94
95 CString toUTF8() const;
96
97 bool isNull() const { return !m_provider; }
98 int startOffset() const { return m_startOffset; }
99 int endOffset() const { return m_endOffset; }
100 int length() const { return m_endOffset - m_startOffset; }
101
102 protected:
103 // FIXME: Make it Ref<SourceProvidier>.
104 // https://bugs.webkit.org/show_bug.cgi?id=168325
105 RefPtr<SourceProvider> m_provider;
106 int m_startOffset;
107 int m_endOffset;
108 };
109
110} // namespace JSC
111