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