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 "SourceOrigin.h"
32#include <wtf/RefCounted.h>
33#include <wtf/URL.h>
34#include <wtf/text/TextPosition.h>
35#include <wtf/text/WTFString.h>
36
37namespace JSC {
38
39 enum class SourceProviderSourceType : uint8_t {
40 Program,
41 Module,
42 WebAssembly,
43 };
44
45 class CachedBytecode {
46 WTF_MAKE_NONCOPYABLE(CachedBytecode);
47
48 public:
49 CachedBytecode()
50 : CachedBytecode(nullptr, 0)
51 {
52 }
53
54 CachedBytecode(const void* data, size_t size)
55 : m_owned(false)
56 , m_size(size)
57 , m_data(data)
58 {
59 }
60
61 CachedBytecode(MallocPtr<uint8_t>&& data, size_t size)
62 : m_owned(true)
63 , m_size(size)
64 , m_data(data.leakPtr())
65 {
66 }
67
68 CachedBytecode(CachedBytecode&& other)
69 {
70 m_owned = other.m_owned;
71 m_size = other.m_size;
72 m_data = other.m_data;
73 other.m_owned = false;
74 }
75
76 CachedBytecode& operator=(CachedBytecode&& other)
77 {
78 freeDataIfOwned();
79 m_owned = other.m_owned;
80 m_size = other.m_size;
81 m_data = other.m_data;
82 other.m_owned = false;
83 return *this;
84 }
85
86 const void* data() const { return m_data; }
87 size_t size() const { return m_size; }
88 bool owned() const { return m_owned; }
89
90 ~CachedBytecode()
91 {
92 freeDataIfOwned();
93 }
94
95 private:
96 void freeDataIfOwned()
97 {
98 if (m_data && m_owned)
99 fastFree(const_cast<void*>(m_data));
100 }
101
102 bool m_owned;
103 size_t m_size;
104 const void* m_data;
105 };
106
107 using BytecodeCacheGenerator = Function<CachedBytecode()>;
108
109 class SourceProvider : public RefCounted<SourceProvider> {
110 public:
111 static const intptr_t nullID = 1;
112
113 JS_EXPORT_PRIVATE SourceProvider(const SourceOrigin&, URL&&, const TextPosition& startPosition, SourceProviderSourceType);
114
115 JS_EXPORT_PRIVATE virtual ~SourceProvider();
116
117 virtual unsigned hash() const = 0;
118 virtual StringView source() const = 0;
119 virtual const CachedBytecode* cachedBytecode() const { return nullptr; }
120 virtual void cacheBytecode(const BytecodeCacheGenerator&) const { }
121
122 StringView getRange(int start, int end) const
123 {
124 return source().substring(start, end - start);
125 }
126
127 const SourceOrigin& sourceOrigin() const { return m_sourceOrigin; }
128 const URL& url() const { return m_url; }
129 const String& sourceURLDirective() const { return m_sourceURLDirective; }
130 const String& sourceMappingURLDirective() const { return m_sourceMappingURLDirective; }
131
132 TextPosition startPosition() const { return m_startPosition; }
133 SourceProviderSourceType sourceType() const { return m_sourceType; }
134
135 intptr_t asID()
136 {
137 if (!m_id)
138 getID();
139 return m_id;
140 }
141
142 void setSourceURLDirective(const String& sourceURLDirective) { m_sourceURLDirective = sourceURLDirective; }
143 void setSourceMappingURLDirective(const String& sourceMappingURLDirective) { m_sourceMappingURLDirective = sourceMappingURLDirective; }
144
145 private:
146 JS_EXPORT_PRIVATE void getID();
147
148 SourceProviderSourceType m_sourceType;
149 URL m_url;
150 SourceOrigin m_sourceOrigin;
151 String m_sourceURLDirective;
152 String m_sourceMappingURLDirective;
153 TextPosition m_startPosition;
154 uintptr_t m_id { 0 };
155 };
156
157 class StringSourceProvider : public SourceProvider {
158 public:
159 static Ref<StringSourceProvider> create(const String& source, const SourceOrigin& sourceOrigin, URL&& url, const TextPosition& startPosition = TextPosition(), SourceProviderSourceType sourceType = SourceProviderSourceType::Program)
160 {
161 return adoptRef(*new StringSourceProvider(source, sourceOrigin, WTFMove(url), startPosition, sourceType));
162 }
163
164 unsigned hash() const override
165 {
166 return m_source.get().hash();
167 }
168
169 StringView source() const override
170 {
171 return m_source.get();
172 }
173
174 protected:
175 StringSourceProvider(const String& source, const SourceOrigin& sourceOrigin, URL&& url, const TextPosition& startPosition, SourceProviderSourceType sourceType)
176 : SourceProvider(sourceOrigin, WTFMove(url), startPosition, sourceType)
177 , m_source(source.isNull() ? *StringImpl::empty() : *source.impl())
178 {
179 }
180
181 private:
182 Ref<StringImpl> m_source;
183 };
184
185#if ENABLE(WEBASSEMBLY)
186 class WebAssemblySourceProvider : public SourceProvider {
187 public:
188 static Ref<WebAssemblySourceProvider> create(Vector<uint8_t>&& data, const SourceOrigin& sourceOrigin, URL&& url)
189 {
190 return adoptRef(*new WebAssemblySourceProvider(WTFMove(data), sourceOrigin, WTFMove(url)));
191 }
192
193 unsigned hash() const override
194 {
195 return m_source.impl()->hash();
196 }
197
198 StringView source() const override
199 {
200 return m_source;
201 }
202
203 const Vector<uint8_t>& data() const
204 {
205 return m_data;
206 }
207
208 private:
209 WebAssemblySourceProvider(Vector<uint8_t>&& data, const SourceOrigin& sourceOrigin, URL&& url)
210 : SourceProvider(sourceOrigin, WTFMove(url), TextPosition(), SourceProviderSourceType::WebAssembly)
211 , m_source("[WebAssembly source]")
212 , m_data(WTFMove(data))
213 {
214 }
215
216 String m_source;
217 Vector<uint8_t> m_data;
218 };
219#endif
220
221} // namespace JSC
222