Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / js / loader / LoadedScript.h
blob5759800c27a22dcbc09337ad7f1a50359c26564a
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/MemoryReporting.h"
16 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
17 #include "mozilla/Variant.h"
18 #include "mozilla/Vector.h"
20 #include "nsCOMPtr.h"
21 #include "nsCycleCollectionParticipant.h"
22 #include "nsIMemoryReporter.h"
24 #include "jsapi.h"
25 #include "ScriptKind.h"
26 #include "ScriptFetchOptions.h"
28 class nsIURI;
30 namespace JS::loader {
32 class ScriptLoadRequest;
34 using Utf8Unit = mozilla::Utf8Unit;
36 void HostAddRefTopLevelScript(const JS::Value& aPrivate);
37 void HostReleaseTopLevelScript(const JS::Value& aPrivate);
39 class ClassicScript;
40 class ModuleScript;
41 class EventScript;
42 class LoadContextBase;
44 // A LoadedScript is a place where the Script is stored once it is loaded. It is
45 // not unique to a load, and can be shared across loads as long as it is
46 // properly ref-counted by each load instance.
48 // When the load is not performed, the URI represents the resource to be loaded,
49 // and it is replaced by the absolute resource location once loaded.
51 // As the LoadedScript can be shared, using the SharedSubResourceCache, it is
52 // exposed to the memory reporter such that sharing might be accounted for
53 // properly.
54 class LoadedScript : public nsIMemoryReporter {
55 ScriptKind mKind;
56 const mozilla::dom::ReferrerPolicy mReferrerPolicy;
57 RefPtr<ScriptFetchOptions> mFetchOptions;
58 nsCOMPtr<nsIURI> mURI;
59 nsCOMPtr<nsIURI> mBaseURL;
61 protected:
62 LoadedScript(ScriptKind aKind, mozilla::dom::ReferrerPolicy aReferrerPolicy,
63 ScriptFetchOptions* aFetchOptions, nsIURI* aURI);
65 virtual ~LoadedScript();
67 public:
68 // When the memory should be reported, register it using RegisterMemoryReport,
69 // and make sure to call SizeOfIncludingThis in the enclosing container.
71 // Each reported script would be listed under
72 // `explicit/js/script/loaded-script/<kind>`.
73 void RegisterMemoryReport();
74 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
76 public:
77 NS_DECL_CYCLE_COLLECTING_ISUPPORTS;
78 NS_DECL_NSIMEMORYREPORTER;
79 NS_DECL_CYCLE_COLLECTION_CLASS(LoadedScript)
81 bool IsClassicScript() const { return mKind == ScriptKind::eClassic; }
82 bool IsModuleScript() const { return mKind == ScriptKind::eModule; }
83 bool IsEventScript() const { return mKind == ScriptKind::eEvent; }
85 inline ClassicScript* AsClassicScript();
86 inline ModuleScript* AsModuleScript();
87 inline EventScript* AsEventScript();
89 // Used to propagate Fetch Options to child modules
90 ScriptFetchOptions* GetFetchOptions() const { return mFetchOptions; }
92 mozilla::dom::ReferrerPolicy ReferrerPolicy() const {
93 return mReferrerPolicy;
96 nsIURI* GetURI() const { return mURI; }
97 void SetBaseURL(nsIURI* aBaseURL) {
98 MOZ_ASSERT(!mBaseURL);
99 mBaseURL = aBaseURL;
101 nsIURI* BaseURL() const { return mBaseURL; }
103 void AssociateWithScript(JSScript* aScript);
105 public:
106 // ===========================================================================
107 // Encoding of the content provided by the network, or refined by the JS
108 // engine.
109 template <typename... Ts>
110 using Variant = mozilla::Variant<Ts...>;
112 template <typename... Ts>
113 using VariantType = mozilla::VariantType<Ts...>;
115 // Type of data provided by the nsChannel.
116 enum class DataType : uint8_t { eUnknown, eTextSource, eBytecode };
118 // Use a vector backed by the JS allocator for script text so that contents
119 // can be transferred in constant time to the JS engine, not copied in linear
120 // time.
121 template <typename Unit>
122 using ScriptTextBuffer = mozilla::Vector<Unit, 0, js::MallocAllocPolicy>;
124 using MaybeSourceText =
125 mozilla::MaybeOneOf<JS::SourceText<char16_t>, JS::SourceText<Utf8Unit>>;
127 bool IsUnknownDataType() const { return mDataType == DataType::eUnknown; }
128 bool IsTextSource() const { return mDataType == DataType::eTextSource; }
129 bool IsSource() const { return IsTextSource(); }
130 bool IsBytecode() const { return mDataType == DataType::eBytecode; }
132 void SetUnknownDataType() {
133 mDataType = DataType::eUnknown;
134 mScriptData.reset();
137 void SetTextSource(LoadContextBase* maybeLoadContext) {
138 MOZ_ASSERT(IsUnknownDataType());
139 mDataType = DataType::eTextSource;
140 mScriptData.emplace(VariantType<ScriptTextBuffer<Utf8Unit>>());
143 void SetBytecode() {
144 MOZ_ASSERT(IsUnknownDataType());
145 mDataType = DataType::eBytecode;
148 bool IsUTF16Text() const {
149 return mScriptData->is<ScriptTextBuffer<char16_t>>();
151 bool IsUTF8Text() const {
152 return mScriptData->is<ScriptTextBuffer<Utf8Unit>>();
155 template <typename Unit>
156 const ScriptTextBuffer<Unit>& ScriptText() const {
157 MOZ_ASSERT(IsTextSource());
158 return mScriptData->as<ScriptTextBuffer<Unit>>();
160 template <typename Unit>
161 ScriptTextBuffer<Unit>& ScriptText() {
162 MOZ_ASSERT(IsTextSource());
163 return mScriptData->as<ScriptTextBuffer<Unit>>();
166 size_t ScriptTextLength() const {
167 MOZ_ASSERT(IsTextSource());
168 return IsUTF16Text() ? ScriptText<char16_t>().length()
169 : ScriptText<Utf8Unit>().length();
172 // Get source text. On success |aMaybeSource| will contain either UTF-8 or
173 // UTF-16 source; on failure it will remain in its initial state.
174 nsresult GetScriptSource(JSContext* aCx, MaybeSourceText* aMaybeSource,
175 LoadContextBase* aMaybeLoadContext);
177 void ClearScriptSource() {
178 if (IsTextSource()) {
179 ClearScriptText();
183 void ClearScriptText() {
184 MOZ_ASSERT(IsTextSource());
185 return IsUTF16Text() ? ScriptText<char16_t>().clearAndFree()
186 : ScriptText<Utf8Unit>().clearAndFree();
189 size_t ReceivedScriptTextLength() const { return mReceivedScriptTextLength; }
191 void SetReceivedScriptTextLength(size_t aLength) {
192 mReceivedScriptTextLength = aLength;
195 JS::TranscodeBuffer& SRIAndBytecode() {
196 // Note: SRIAndBytecode might be called even if the IsSource() returns true,
197 // as we want to be able to save the bytecode content when we are loading
198 // from source.
199 MOZ_ASSERT(IsBytecode() || IsSource());
200 return mScriptBytecode;
202 JS::TranscodeRange Bytecode() const {
203 MOZ_ASSERT(IsBytecode());
204 const auto& bytecode = mScriptBytecode;
205 auto offset = mBytecodeOffset;
206 return JS::TranscodeRange(bytecode.begin() + offset,
207 bytecode.length() - offset);
210 size_t GetSRILength() const {
211 MOZ_ASSERT(IsBytecode() || IsSource());
212 return mBytecodeOffset;
214 void SetSRILength(size_t sriLength) {
215 MOZ_ASSERT(IsBytecode() || IsSource());
216 mBytecodeOffset = JS::AlignTranscodingBytecodeOffset(sriLength);
219 void DropBytecode() {
220 MOZ_ASSERT(IsBytecode() || IsSource());
221 mScriptBytecode.clearAndFree();
224 // Determine whether the mScriptData or mScriptBytecode is used.
225 DataType mDataType;
227 // Holds script source data for non-inline scripts.
228 mozilla::Maybe<
229 Variant<ScriptTextBuffer<char16_t>, ScriptTextBuffer<Utf8Unit>>>
230 mScriptData;
232 // The length of script source text, set when reading completes. This is used
233 // since mScriptData is cleared when the source is passed to the JS engine.
234 size_t mReceivedScriptTextLength;
236 // Holds the SRI serialized hash and the script bytecode for non-inline
237 // scripts. The data is laid out according to ScriptBytecodeDataLayout
238 // or, if compression is enabled, ScriptBytecodeCompressedDataLayout.
239 JS::TranscodeBuffer mScriptBytecode;
240 uint32_t mBytecodeOffset; // Offset of the bytecode in mScriptBytecode
243 // Provide accessors for any classes `Derived` which is providing the
244 // `getLoadedScript` function as interface. The accessors are meant to be
245 // inherited by the `Derived` class.
246 template <typename Derived>
247 class LoadedScriptDelegate {
248 private:
249 // Use a static_cast<Derived> instead of declaring virtual functions. This is
250 // meant to avoid relying on virtual table, and improve inlining for non-final
251 // classes.
252 const LoadedScript* GetLoadedScript() const {
253 return static_cast<const Derived*>(this)->getLoadedScript();
255 LoadedScript* GetLoadedScript() {
256 return static_cast<Derived*>(this)->getLoadedScript();
259 public:
260 template <typename Unit>
261 using ScriptTextBuffer = LoadedScript::ScriptTextBuffer<Unit>;
262 using MaybeSourceText = LoadedScript::MaybeSourceText;
264 bool IsModuleScript() const { return GetLoadedScript()->IsModuleScript(); }
265 bool IsEventScript() const { return GetLoadedScript()->IsEventScript(); }
267 bool IsUnknownDataType() const {
268 return GetLoadedScript()->IsUnknownDataType();
270 bool IsTextSource() const { return GetLoadedScript()->IsTextSource(); }
271 bool IsSource() const { return GetLoadedScript()->IsSource(); }
272 bool IsBytecode() const { return GetLoadedScript()->IsBytecode(); }
274 void SetUnknownDataType() { GetLoadedScript()->SetUnknownDataType(); }
276 void SetTextSource(LoadContextBase* maybeLoadContext) {
277 GetLoadedScript()->SetTextSource(maybeLoadContext);
280 void SetBytecode() { GetLoadedScript()->SetBytecode(); }
282 bool IsUTF16Text() const { return GetLoadedScript()->IsUTF16Text(); }
283 bool IsUTF8Text() const { return GetLoadedScript()->IsUTF8Text(); }
285 template <typename Unit>
286 const ScriptTextBuffer<Unit>& ScriptText() const {
287 const LoadedScript* loader = GetLoadedScript();
288 return loader->ScriptText<Unit>();
290 template <typename Unit>
291 ScriptTextBuffer<Unit>& ScriptText() {
292 LoadedScript* loader = GetLoadedScript();
293 return loader->ScriptText<Unit>();
296 size_t ScriptTextLength() const {
297 return GetLoadedScript()->ScriptTextLength();
300 size_t ReceivedScriptTextLength() const {
301 return GetLoadedScript()->ReceivedScriptTextLength();
304 void SetReceivedScriptTextLength(size_t aLength) {
305 GetLoadedScript()->SetReceivedScriptTextLength(aLength);
308 // Get source text. On success |aMaybeSource| will contain either UTF-8 or
309 // UTF-16 source; on failure it will remain in its initial state.
310 nsresult GetScriptSource(JSContext* aCx, MaybeSourceText* aMaybeSource,
311 LoadContextBase* aLoadContext) {
312 return GetLoadedScript()->GetScriptSource(aCx, aMaybeSource, aLoadContext);
315 void ClearScriptSource() { GetLoadedScript()->ClearScriptSource(); }
317 void ClearScriptText() { GetLoadedScript()->ClearScriptText(); }
319 JS::TranscodeBuffer& SRIAndBytecode() {
320 return GetLoadedScript()->SRIAndBytecode();
322 JS::TranscodeRange Bytecode() const { return GetLoadedScript()->Bytecode(); }
324 size_t GetSRILength() const { return GetLoadedScript()->GetSRILength(); }
325 void SetSRILength(size_t sriLength) {
326 GetLoadedScript()->SetSRILength(sriLength);
329 void DropBytecode() { GetLoadedScript()->DropBytecode(); }
332 class ClassicScript final : public LoadedScript {
333 ~ClassicScript() = default;
335 private:
336 // Scripts can be created only by ScriptLoadRequest::NoCacheEntryFound.
337 ClassicScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
338 ScriptFetchOptions* aFetchOptions, nsIURI* aURI);
340 friend class ScriptLoadRequest;
343 class EventScript final : public LoadedScript {
344 ~EventScript() = default;
346 public:
347 EventScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
348 ScriptFetchOptions* aFetchOptions, nsIURI* aURI);
351 // A single module script. May be used to satisfy multiple load requests.
353 class ModuleScript final : public LoadedScript {
354 JS::Heap<JSObject*> mModuleRecord;
355 JS::Heap<JS::Value> mParseError;
356 JS::Heap<JS::Value> mErrorToRethrow;
357 bool mForPreload;
358 bool mHadImportMap;
359 bool mDebuggerDataInitialized;
361 ~ModuleScript();
363 public:
364 NS_DECL_ISUPPORTS_INHERITED
365 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ModuleScript,
366 LoadedScript)
368 private:
369 // Scripts can be created only by ScriptLoadRequest::NoCacheEntryFound.
370 ModuleScript(mozilla::dom::ReferrerPolicy aReferrerPolicy,
371 ScriptFetchOptions* aFetchOptions, nsIURI* aURI);
373 friend class ScriptLoadRequest;
375 public:
376 void SetModuleRecord(JS::Handle<JSObject*> aModuleRecord);
377 void SetParseError(const JS::Value& aError);
378 void SetErrorToRethrow(const JS::Value& aError);
379 void SetForPreload(bool aValue);
380 void SetHadImportMap(bool aValue);
381 void SetDebuggerDataInitialized();
383 JSObject* ModuleRecord() const { return mModuleRecord; }
385 JS::Value ParseError() const { return mParseError; }
386 JS::Value ErrorToRethrow() const { return mErrorToRethrow; }
387 bool HasParseError() const { return !mParseError.isUndefined(); }
388 bool HasErrorToRethrow() const { return !mErrorToRethrow.isUndefined(); }
389 bool ForPreload() const { return mForPreload; }
390 bool HadImportMap() const { return mHadImportMap; }
391 bool DebuggerDataInitialized() const { return mDebuggerDataInitialized; }
393 void Shutdown();
395 void UnlinkModuleRecord();
397 friend void CheckModuleScriptPrivate(LoadedScript*, const JS::Value&);
400 ClassicScript* LoadedScript::AsClassicScript() {
401 MOZ_ASSERT(!IsModuleScript());
402 return static_cast<ClassicScript*>(this);
405 ModuleScript* LoadedScript::AsModuleScript() {
406 MOZ_ASSERT(IsModuleScript());
407 return static_cast<ModuleScript*>(this);
410 } // namespace JS::loader
412 #endif // js_loader_LoadedScript_h