1/*
2 * Copyright (C) 2008, 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 *
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 "UnlinkedSourceCode.h"
32
33namespace JSC {
34
35 class SourceCode : public UnlinkedSourceCode {
36 friend class CachedSourceCode;
37
38 public:
39 SourceCode()
40 : UnlinkedSourceCode()
41 , m_firstLine(OrdinalNumber::beforeFirst())
42 , m_startColumn(OrdinalNumber::beforeFirst())
43 {
44 }
45
46 SourceCode(Ref<SourceProvider>&& provider)
47 : UnlinkedSourceCode(WTFMove(provider))
48 {
49 }
50
51 SourceCode(Ref<SourceProvider>&& provider, int firstLine, int startColumn)
52 : UnlinkedSourceCode(WTFMove(provider))
53 , m_firstLine(OrdinalNumber::fromOneBasedInt(std::max(firstLine, 1)))
54 , m_startColumn(OrdinalNumber::fromOneBasedInt(std::max(startColumn, 1)))
55 {
56 }
57
58 SourceCode(RefPtr<SourceProvider>&& provider, int startOffset, int endOffset, int firstLine, int startColumn)
59 : UnlinkedSourceCode(WTFMove(provider), startOffset, endOffset)
60 , m_firstLine(OrdinalNumber::fromOneBasedInt(std::max(firstLine, 1)))
61 , m_startColumn(OrdinalNumber::fromOneBasedInt(std::max(startColumn, 1)))
62 {
63 }
64
65 OrdinalNumber firstLine() const { return m_firstLine; }
66 OrdinalNumber startColumn() const { return m_startColumn; }
67
68 intptr_t providerID() const
69 {
70 if (!m_provider)
71 return SourceProvider::nullID;
72 return m_provider->asID();
73 }
74
75 SourceProvider* provider() const { return m_provider.get(); }
76
77 SourceCode subExpression(unsigned openBrace, unsigned closeBrace, int firstLine, int startColumn) const;
78
79 bool operator==(const SourceCode& other) const
80 {
81 return m_firstLine == other.m_firstLine
82 && m_startColumn == other.m_startColumn
83 && m_provider == other.m_provider
84 && m_startOffset == other.m_startOffset
85 && m_endOffset == other.m_endOffset;
86 }
87
88 bool operator!=(const SourceCode& other) const
89 {
90 return !(*this == other);
91 }
92
93 private:
94 OrdinalNumber m_firstLine;
95 OrdinalNumber m_startColumn;
96 };
97
98 inline SourceCode makeSource(const String& source, const SourceOrigin& sourceOrigin, URL&& url = URL(), const TextPosition& startPosition = TextPosition(), SourceProviderSourceType sourceType = SourceProviderSourceType::Program)
99 {
100 return SourceCode(StringSourceProvider::create(source, sourceOrigin, WTFMove(url), startPosition, sourceType), startPosition.m_line.oneBasedInt(), startPosition.m_column.oneBasedInt());
101 }
102
103 inline SourceCode SourceCode::subExpression(unsigned openBrace, unsigned closeBrace, int firstLine, int startColumn) const
104 {
105 startColumn += 1; // Convert to base 1.
106 return SourceCode(RefPtr<SourceProvider> { provider() }, openBrace, closeBrace + 1, firstLine, startColumn);
107 }
108
109} // namespace JSC
110