1/*
2 * Copyright (C) 2008, 2009, 2012, 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#ifndef SourceProvider_h
30#define SourceProvider_h
31
32#include <wtf/RefCounted.h>
33#include <wtf/text/TextPosition.h>
34#include <wtf/text/WTFString.h>
35
36namespace JSC {
37
38 class SourceProvider : public RefCounted<SourceProvider> {
39 public:
40 static const intptr_t nullID = 1;
41
42 JS_EXPORT_PRIVATE SourceProvider(const String& url, const TextPosition& startPosition);
43
44 JS_EXPORT_PRIVATE virtual ~SourceProvider();
45
46 virtual unsigned hash() const = 0;
47 virtual StringView source() const = 0;
48 StringView getRange(int start, int end) const
49 {
50 return source().substring(start, end - start);
51 }
52
53 const String& url() const { return m_url; }
54 const String& sourceURL() const { return m_sourceURLDirective; }
55 const String& sourceMappingURL() const { return m_sourceMappingURLDirective; }
56
57 TextPosition startPosition() const { return m_startPosition; }
58 intptr_t asID()
59 {
60 if (!m_id)
61 getID();
62 return m_id;
63 }
64
65 bool isValid() const { return m_validated; }
66 void setValid() { m_validated = true; }
67
68 private:
69 template <typename T> friend class Parser;
70
71 void setSourceURLDirective(const String& sourceURL) { m_sourceURLDirective = sourceURL; }
72 void setSourceMappingURLDirective(const String& sourceMappingURL) { m_sourceMappingURLDirective = sourceMappingURL; }
73
74 JS_EXPORT_PRIVATE void getID();
75
76 String m_url;
77 String m_sourceURLDirective;
78 String m_sourceMappingURLDirective;
79 TextPosition m_startPosition;
80 bool m_validated : 1;
81 uintptr_t m_id : sizeof(uintptr_t) * 8 - 1;
82 };
83
84 class StringSourceProvider : public SourceProvider {
85 public:
86 static Ref<StringSourceProvider> create(const String& source, const String& url, const TextPosition& startPosition = TextPosition::minimumPosition())
87 {
88 return adoptRef(*new StringSourceProvider(source, url, startPosition));
89 }
90
91 JS_EXPORT_PRIVATE ~StringSourceProvider() override;
92
93 unsigned hash() const override
94 {
95 return m_source.get().hash();
96 }
97
98 virtual StringView source() const override
99 {
100 return m_source.get();
101 }
102
103 private:
104 StringSourceProvider(const String& source, const String& url, const TextPosition& startPosition)
105 : SourceProvider(url, startPosition)
106 , m_source(source.isNull() ? *StringImpl::empty() : *source.impl())
107 {
108 }
109
110 Ref<StringImpl> m_source;
111 };
112
113#if ENABLE(WEBASSEMBLY)
114 class WebAssemblySourceProvider : public SourceProvider {
115 public:
116 static Ref<WebAssemblySourceProvider> create(const Vector<uint8_t>& data, const String& url)
117 {
118 return adoptRef(*new WebAssemblySourceProvider(data, url));
119 }
120
121 unsigned hash() const override
122 {
123 return m_source.impl()->hash();
124 }
125
126 virtual StringView source() const override
127 {
128 return m_source;
129 }
130
131 const Vector<uint8_t>& data() const
132 {
133 return m_data;
134 }
135
136 private:
137 WebAssemblySourceProvider(const Vector<uint8_t>& data, const String& url)
138 : SourceProvider(url, TextPosition::minimumPosition())
139 , m_source("[WebAssembly source]")
140 , m_data(data)
141 {
142 }
143
144 String m_source;
145 Vector<uint8_t> m_data;
146 };
147#endif
148
149} // namespace JSC
150
151#endif // SourceProvider_h
152