1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef js_loader_LoadedScript_h
8 #define js_loader_LoadedScript_h
10 #include "js/AllocPolicy.h"
11 #include "js/Transcoding.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/MaybeOneOf.h"
15 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
16 #include "mozilla/Variant.h"
17 #include "mozilla/Vector.h"
20 #include "nsCycleCollectionParticipant.h"
23 #include "ScriptKind.h"
24 #include "ScriptFetchOptions.h"
28 namespace JS::loader
{
30 class ScriptLoadRequest
;
32 using Utf8Unit
= mozilla::Utf8Unit
;
34 void HostAddRefTopLevelScript(const JS::Value
& aPrivate
);
35 void HostReleaseTopLevelScript(const JS::Value
& aPrivate
);
40 class LoadContextBase
;
42 class LoadedScript
: public nsISupports
{
44 const mozilla::dom::ReferrerPolicy mReferrerPolicy
;
45 RefPtr
<ScriptFetchOptions
> mFetchOptions
;
46 nsCOMPtr
<nsIURI
> mURI
;
47 nsCOMPtr
<nsIURI
> mBaseURL
;
50 LoadedScript(ScriptKind aKind
, mozilla::dom::ReferrerPolicy aReferrerPolicy
,
51 ScriptFetchOptions
* aFetchOptions
, nsIURI
* aURI
);
53 virtual ~LoadedScript();
56 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
57 NS_DECL_CYCLE_COLLECTION_CLASS(LoadedScript
)
59 bool IsClassicScript() const { return mKind
== ScriptKind::eClassic
; }
60 bool IsModuleScript() const { return mKind
== ScriptKind::eModule
; }
61 bool IsEventScript() const { return mKind
== ScriptKind::eEvent
; }
63 inline ClassicScript
* AsClassicScript();
64 inline ModuleScript
* AsModuleScript();
65 inline EventScript
* AsEventScript();
67 // Used to propagate Fetch Options to child modules
68 ScriptFetchOptions
* GetFetchOptions() const { return mFetchOptions
; }
70 mozilla::dom::ReferrerPolicy
ReferrerPolicy() const {
71 return mReferrerPolicy
;
74 nsIURI
* GetURI() const { return mURI
; }
75 void SetBaseURL(nsIURI
* aBaseURL
) {
76 MOZ_ASSERT(!mBaseURL
);
79 nsIURI
* BaseURL() const { return mBaseURL
; }
81 void AssociateWithScript(JSScript
* aScript
);
84 // ===========================================================================
85 // Encoding of the content provided by the network, or refined by the JS
87 template <typename
... Ts
>
88 using Variant
= mozilla::Variant
<Ts
...>;
90 template <typename
... Ts
>
91 using VariantType
= mozilla::VariantType
<Ts
...>;
93 // Type of data provided by the nsChannel.
94 enum class DataType
: uint8_t { eUnknown
, eTextSource
, eBytecode
};
96 // Use a vector backed by the JS allocator for script text so that contents
97 // can be transferred in constant time to the JS engine, not copied in linear
99 template <typename Unit
>
100 using ScriptTextBuffer
= mozilla::Vector
<Unit
, 0, js::MallocAllocPolicy
>;
102 using MaybeSourceText
=
103 mozilla::MaybeOneOf
<JS::SourceText
<char16_t
>, JS::SourceText
<Utf8Unit
>>;
105 bool IsUnknownDataType() const { return mDataType
== DataType::eUnknown
; }
106 bool IsTextSource() const { return mDataType
== DataType::eTextSource
; }
107 bool IsSource() const { return IsTextSource(); }
108 bool IsBytecode() const { return mDataType
== DataType::eBytecode
; }
110 void SetUnknownDataType() {
111 mDataType
= DataType::eUnknown
;
115 void SetTextSource(LoadContextBase
* maybeLoadContext
) {
116 MOZ_ASSERT(IsUnknownDataType());
117 mDataType
= DataType::eTextSource
;
118 mScriptData
.emplace(VariantType
<ScriptTextBuffer
<Utf8Unit
>>());
122 MOZ_ASSERT(IsUnknownDataType());
123 mDataType
= DataType::eBytecode
;
126 bool IsUTF16Text() const {
127 return mScriptData
->is
<ScriptTextBuffer
<char16_t
>>();
129 bool IsUTF8Text() const {
130 return mScriptData
->is
<ScriptTextBuffer
<Utf8Unit
>>();
133 template <typename Unit
>
134 const ScriptTextBuffer
<Unit
>& ScriptText() const {
135 MOZ_ASSERT(IsTextSource());
136 return mScriptData
->as
<ScriptTextBuffer
<Unit
>>();
138 template <typename Unit
>
139 ScriptTextBuffer
<Unit
>& ScriptText() {
140 MOZ_ASSERT(IsTextSource());
141 return mScriptData
->as
<ScriptTextBuffer
<Unit
>>();
144 size_t ScriptTextLength() const {
145 MOZ_ASSERT(IsTextSource());
146 return IsUTF16Text() ? ScriptText
<char16_t
>().length()
147 : ScriptText
<Utf8Unit
>().length();
150 // Get source text. On success |aMaybeSource| will contain either UTF-8 or
151 // UTF-16 source; on failure it will remain in its initial state.
152 nsresult
GetScriptSource(JSContext
* aCx
, MaybeSourceText
* aMaybeSource
,
153 LoadContextBase
* aMaybeLoadContext
);
155 void ClearScriptSource() {
156 if (IsTextSource()) {
161 void ClearScriptText() {
162 MOZ_ASSERT(IsTextSource());
163 return IsUTF16Text() ? ScriptText
<char16_t
>().clearAndFree()
164 : ScriptText
<Utf8Unit
>().clearAndFree();
167 size_t ReceivedScriptTextLength() const { return mReceivedScriptTextLength
; }
169 void SetReceivedScriptTextLength(size_t aLength
) {
170 mReceivedScriptTextLength
= aLength
;
173 JS::TranscodeBuffer
& SRIAndBytecode() {
174 // Note: SRIAndBytecode might be called even if the IsSource() returns true,
175 // as we want to be able to save the bytecode content when we are loading
177 MOZ_ASSERT(IsBytecode() || IsSource());
178 return mScriptBytecode
;
180 JS::TranscodeRange
Bytecode() const {
181 MOZ_ASSERT(IsBytecode());
182 const auto& bytecode
= mScriptBytecode
;
183 auto offset
= mBytecodeOffset
;
184 return JS::TranscodeRange(bytecode
.begin() + offset
,
185 bytecode
.length() - offset
);
188 size_t GetSRILength() const {
189 MOZ_ASSERT(IsBytecode() || IsSource());
190 return mBytecodeOffset
;
192 void SetSRILength(size_t sriLength
) {
193 MOZ_ASSERT(IsBytecode() || IsSource());
194 mBytecodeOffset
= JS::AlignTranscodingBytecodeOffset(sriLength
);
197 void DropBytecode() {
198 MOZ_ASSERT(IsBytecode() || IsSource());
199 mScriptBytecode
.clearAndFree();
202 // Determine whether the mScriptData or mScriptBytecode is used.
205 // Holds script source data for non-inline scripts.
207 Variant
<ScriptTextBuffer
<char16_t
>, ScriptTextBuffer
<Utf8Unit
>>>
210 // The length of script source text, set when reading completes. This is used
211 // since mScriptData is cleared when the source is passed to the JS engine.
212 size_t mReceivedScriptTextLength
;
214 // Holds the SRI serialized hash and the script bytecode for non-inline
215 // scripts. The data is laid out according to ScriptBytecodeDataLayout
216 // or, if compression is enabled, ScriptBytecodeCompressedDataLayout.
217 JS::TranscodeBuffer mScriptBytecode
;
218 uint32_t mBytecodeOffset
; // Offset of the bytecode in mScriptBytecode
221 // Provide accessors for any classes `Derived` which is providing the
222 // `getLoadedScript` function as interface. The accessors are meant to be
223 // inherited by the `Derived` class.
224 template <typename Derived
>
225 class LoadedScriptDelegate
{
227 // Use a static_cast<Derived> instead of declaring virtual functions. This is
228 // meant to avoid relying on virtual table, and improve inlining for non-final
230 const LoadedScript
* GetLoadedScript() const {
231 return static_cast<const Derived
*>(this)->getLoadedScript();
233 LoadedScript
* GetLoadedScript() {
234 return static_cast<Derived
*>(this)->getLoadedScript();
238 template <typename Unit
>
239 using ScriptTextBuffer
= LoadedScript::ScriptTextBuffer
<Unit
>;
240 using MaybeSourceText
= LoadedScript::MaybeSourceText
;
242 bool IsModuleScript() const { return GetLoadedScript()->IsModuleScript(); }
243 bool IsEventScript() const { return GetLoadedScript()->IsEventScript(); }
245 bool IsUnknownDataType() const {
246 return GetLoadedScript()->IsUnknownDataType();
248 bool IsTextSource() const { return GetLoadedScript()->IsTextSource(); }
249 bool IsSource() const { return GetLoadedScript()->IsSource(); }
250 bool IsBytecode() const { return GetLoadedScript()->IsBytecode(); }
252 void SetUnknownDataType() { GetLoadedScript()->SetUnknownDataType(); }
254 void SetTextSource(LoadContextBase
* maybeLoadContext
) {
255 GetLoadedScript()->SetTextSource(maybeLoadContext
);
258 void SetBytecode() { GetLoadedScript()->SetBytecode(); }
260 bool IsUTF16Text() const { return GetLoadedScript()->IsUTF16Text(); }
261 bool IsUTF8Text() const { return GetLoadedScript()->IsUTF8Text(); }
263 template <typename Unit
>
264 const ScriptTextBuffer
<Unit
>& ScriptText() const {
265 const LoadedScript
* loader
= GetLoadedScript();
266 return loader
->ScriptText
<Unit
>();
268 template <typename Unit
>
269 ScriptTextBuffer
<Unit
>& ScriptText() {
270 LoadedScript
* loader
= GetLoadedScript();
271 return loader
->ScriptText
<Unit
>();
274 size_t ScriptTextLength() const {
275 return GetLoadedScript()->ScriptTextLength();
278 size_t ReceivedScriptTextLength() const {
279 return GetLoadedScript()->ReceivedScriptTextLength();
282 void SetReceivedScriptTextLength(size_t aLength
) {
283 GetLoadedScript()->SetReceivedScriptTextLength(aLength
);
286 // Get source text. On success |aMaybeSource| will contain either UTF-8 or
287 // UTF-16 source; on failure it will remain in its initial state.
288 nsresult
GetScriptSource(JSContext
* aCx
, MaybeSourceText
* aMaybeSource
,
289 LoadContextBase
* aLoadContext
) {
290 return GetLoadedScript()->GetScriptSource(aCx
, aMaybeSource
, aLoadContext
);
293 void ClearScriptSource() { GetLoadedScript()->ClearScriptSource(); }
295 void ClearScriptText() { GetLoadedScript()->ClearScriptText(); }
297 JS::TranscodeBuffer
& SRIAndBytecode() {
298 return GetLoadedScript()->SRIAndBytecode();
300 JS::TranscodeRange
Bytecode() const { return GetLoadedScript()->Bytecode(); }
302 size_t GetSRILength() const { return GetLoadedScript()->GetSRILength(); }
303 void SetSRILength(size_t sriLength
) {
304 GetLoadedScript()->SetSRILength(sriLength
);
307 void DropBytecode() { GetLoadedScript()->DropBytecode(); }
310 class ClassicScript final
: public LoadedScript
{
311 ~ClassicScript() = default;
314 // Scripts can be created only by ScriptLoadRequest::NoCacheEntryFound.
315 ClassicScript(mozilla::dom::ReferrerPolicy aReferrerPolicy
,
316 ScriptFetchOptions
* aFetchOptions
, nsIURI
* aURI
);
318 friend class ScriptLoadRequest
;
321 class EventScript final
: public LoadedScript
{
322 ~EventScript() = default;
325 EventScript(mozilla::dom::ReferrerPolicy aReferrerPolicy
,
326 ScriptFetchOptions
* aFetchOptions
, nsIURI
* aURI
);
329 // A single module script. May be used to satisfy multiple load requests.
331 class ModuleScript final
: public LoadedScript
{
332 JS::Heap
<JSObject
*> mModuleRecord
;
333 JS::Heap
<JS::Value
> mParseError
;
334 JS::Heap
<JS::Value
> mErrorToRethrow
;
335 bool mDebuggerDataInitialized
;
340 NS_DECL_ISUPPORTS_INHERITED
341 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ModuleScript
,
345 // Scripts can be created only by ScriptLoadRequest::NoCacheEntryFound.
346 ModuleScript(mozilla::dom::ReferrerPolicy aReferrerPolicy
,
347 ScriptFetchOptions
* aFetchOptions
, nsIURI
* aURI
);
349 friend class ScriptLoadRequest
;
352 void SetModuleRecord(JS::Handle
<JSObject
*> aModuleRecord
);
353 void SetParseError(const JS::Value
& aError
);
354 void SetErrorToRethrow(const JS::Value
& aError
);
355 void SetDebuggerDataInitialized();
357 JSObject
* ModuleRecord() const { return mModuleRecord
; }
359 JS::Value
ParseError() const { return mParseError
; }
360 JS::Value
ErrorToRethrow() const { return mErrorToRethrow
; }
361 bool HasParseError() const { return !mParseError
.isUndefined(); }
362 bool HasErrorToRethrow() const { return !mErrorToRethrow
.isUndefined(); }
363 bool DebuggerDataInitialized() const { return mDebuggerDataInitialized
; }
367 void UnlinkModuleRecord();
369 friend void CheckModuleScriptPrivate(LoadedScript
*, const JS::Value
&);
372 ClassicScript
* LoadedScript::AsClassicScript() {
373 MOZ_ASSERT(!IsModuleScript());
374 return static_cast<ClassicScript
*>(this);
377 ModuleScript
* LoadedScript::AsModuleScript() {
378 MOZ_ASSERT(IsModuleScript());
379 return static_cast<ModuleScript
*>(this);
382 } // namespace JS::loader
384 #endif // js_loader_LoadedScript_h