| 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 "CachedBytecode.h" |
| 32 | #include "CodeSpecializationKind.h" |
| 33 | #include "SourceOrigin.h" |
| 34 | #include <wtf/RefCounted.h> |
| 35 | #include <wtf/URL.h> |
| 36 | #include <wtf/text/TextPosition.h> |
| 37 | #include <wtf/text/WTFString.h> |
| 38 | |
| 39 | namespace JSC { |
| 40 | |
| 41 | class SourceCode; |
| 42 | class UnlinkedFunctionExecutable; |
| 43 | class UnlinkedFunctionCodeBlock; |
| 44 | |
| 45 | enum class SourceProviderSourceType : uint8_t { |
| 46 | Program, |
| 47 | Module, |
| 48 | WebAssembly, |
| 49 | }; |
| 50 | |
| 51 | using BytecodeCacheGenerator = Function<Ref<CachedBytecode>()>; |
| 52 | |
| 53 | class SourceProvider : public RefCounted<SourceProvider> { |
| 54 | public: |
| 55 | static const intptr_t nullID = 1; |
| 56 | |
| 57 | JS_EXPORT_PRIVATE SourceProvider(const SourceOrigin&, URL&&, const TextPosition& startPosition, SourceProviderSourceType); |
| 58 | |
| 59 | JS_EXPORT_PRIVATE virtual ~SourceProvider(); |
| 60 | |
| 61 | virtual unsigned hash() const = 0; |
| 62 | virtual StringView source() const = 0; |
| 63 | virtual RefPtr<CachedBytecode> cachedBytecode() const { return nullptr; } |
| 64 | virtual void cacheBytecode(const BytecodeCacheGenerator&) const { } |
| 65 | virtual void updateCache(const UnlinkedFunctionExecutable*, const SourceCode&, CodeSpecializationKind, const UnlinkedFunctionCodeBlock*) const { } |
| 66 | virtual void commitCachedBytecode() const { } |
| 67 | |
| 68 | StringView getRange(int start, int end) const |
| 69 | { |
| 70 | return source().substring(start, end - start); |
| 71 | } |
| 72 | |
| 73 | const SourceOrigin& sourceOrigin() const { return m_sourceOrigin; } |
| 74 | const URL& url() const { return m_url; } |
| 75 | const String& sourceURLDirective() const { return m_sourceURLDirective; } |
| 76 | const String& sourceMappingURLDirective() const { return m_sourceMappingURLDirective; } |
| 77 | |
| 78 | TextPosition startPosition() const { return m_startPosition; } |
| 79 | SourceProviderSourceType sourceType() const { return m_sourceType; } |
| 80 | |
| 81 | intptr_t asID() |
| 82 | { |
| 83 | if (!m_id) |
| 84 | getID(); |
| 85 | return m_id; |
| 86 | } |
| 87 | |
| 88 | void setSourceURLDirective(const String& sourceURLDirective) { m_sourceURLDirective = sourceURLDirective; } |
| 89 | void setSourceMappingURLDirective(const String& sourceMappingURLDirective) { m_sourceMappingURLDirective = sourceMappingURLDirective; } |
| 90 | |
| 91 | private: |
| 92 | JS_EXPORT_PRIVATE void getID(); |
| 93 | |
| 94 | SourceProviderSourceType m_sourceType; |
| 95 | URL m_url; |
| 96 | SourceOrigin m_sourceOrigin; |
| 97 | String m_sourceURLDirective; |
| 98 | String m_sourceMappingURLDirective; |
| 99 | TextPosition m_startPosition; |
| 100 | uintptr_t m_id { 0 }; |
| 101 | }; |
| 102 | |
| 103 | class StringSourceProvider : public SourceProvider { |
| 104 | public: |
| 105 | static Ref<StringSourceProvider> create(const String& source, const SourceOrigin& sourceOrigin, URL&& url, const TextPosition& startPosition = TextPosition(), SourceProviderSourceType sourceType = SourceProviderSourceType::Program) |
| 106 | { |
| 107 | return adoptRef(*new StringSourceProvider(source, sourceOrigin, WTFMove(url), startPosition, sourceType)); |
| 108 | } |
| 109 | |
| 110 | unsigned hash() const override |
| 111 | { |
| 112 | return m_source.get().hash(); |
| 113 | } |
| 114 | |
| 115 | StringView source() const override |
| 116 | { |
| 117 | return m_source.get(); |
| 118 | } |
| 119 | |
| 120 | protected: |
| 121 | StringSourceProvider(const String& source, const SourceOrigin& sourceOrigin, URL&& url, const TextPosition& startPosition, SourceProviderSourceType sourceType) |
| 122 | : SourceProvider(sourceOrigin, WTFMove(url), startPosition, sourceType) |
| 123 | , m_source(source.isNull() ? *StringImpl::empty() : *source.impl()) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | private: |
| 128 | Ref<StringImpl> m_source; |
| 129 | }; |
| 130 | |
| 131 | #if ENABLE(WEBASSEMBLY) |
| 132 | class WebAssemblySourceProvider : public SourceProvider { |
| 133 | public: |
| 134 | static Ref<WebAssemblySourceProvider> create(Vector<uint8_t>&& data, const SourceOrigin& sourceOrigin, URL&& url) |
| 135 | { |
| 136 | return adoptRef(*new WebAssemblySourceProvider(WTFMove(data), sourceOrigin, WTFMove(url))); |
| 137 | } |
| 138 | |
| 139 | unsigned hash() const override |
| 140 | { |
| 141 | return m_source.impl()->hash(); |
| 142 | } |
| 143 | |
| 144 | StringView source() const override |
| 145 | { |
| 146 | return m_source; |
| 147 | } |
| 148 | |
| 149 | const Vector<uint8_t>& data() const |
| 150 | { |
| 151 | return m_data; |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | WebAssemblySourceProvider(Vector<uint8_t>&& data, const SourceOrigin& sourceOrigin, URL&& url) |
| 156 | : SourceProvider(sourceOrigin, WTFMove(url), TextPosition(), SourceProviderSourceType::WebAssembly) |
| 157 | , m_source("[WebAssembly source]" ) |
| 158 | , m_data(WTFMove(data)) |
| 159 | { |
| 160 | } |
| 161 | |
| 162 | String m_source; |
| 163 | Vector<uint8_t> m_data; |
| 164 | }; |
| 165 | #endif |
| 166 | |
| 167 | } // namespace JSC |
| 168 | |