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
40 public:
41 UnlinkedSourceCode()
42 : m_provider(0)
43 , m_startOffset(0)
44 , m_endOffset(0)
45 {
46 }
47
48 UnlinkedSourceCode(WTF::HashTableDeletedValueType)
49 : m_provider(WTF::HashTableDeletedValue)
50 {
51 }
52
53 UnlinkedSourceCode(Ref<SourceProvider>&& provider)
54 : m_provider(WTFMove(provider))
55 , m_startOffset(0)
56 , m_endOffset(m_provider->source().length())
57 {
58 }
59
60 UnlinkedSourceCode(Ref<SourceProvider>&& provider, int startOffset, int endOffset)
61 : m_provider(WTFMove(provider))
62 , m_startOffset(startOffset)
63 , m_endOffset(endOffset)
64 {
65 }
66
67 UnlinkedSourceCode(RefPtr<SourceProvider>&& provider, int startOffset, int endOffset)
68 : m_provider(WTFMove(provider))
69 , m_startOffset(startOffset)
70 , m_endOffset(endOffset)
71 {
72 }
73
74 bool isHashTableDeletedValue() const { return m_provider.isHashTableDeletedValue(); }
75
76 const SourceProvider& provider() const
77 {
78 return *m_provider;
79 }
80
81 unsigned hash() const
82 {
83 ASSERT(m_provider);
84 return m_provider->hash();
85 }
86
87 StringView view() const
88 {
89 if (!m_provider)
90 return StringView();
91 return m_provider->getRange(m_startOffset, m_endOffset);
92 }
93
94 CString toUTF8() const;
95
96 bool isNull() const { return !m_provider; }
97 int startOffset() const { return m_startOffset; }
98 int endOffset() const { return m_endOffset; }
99 int length() const { return m_endOffset - m_startOffset; }
100
101 protected:
102 // FIXME: Make it Ref<SourceProvidier>.
103 // https://bugs.webkit.org/show_bug.cgi?id=168325
104 RefPtr<SourceProvider> m_provider;
105 int m_startOffset;
106 int m_endOffset;
107 };
108
109} // namespace JSC
110