Bug 1311726 - Move getScriptSource to ScriptLoadRequest; r=jonco
Differential Revision: https://phabricator.services.mozilla.com/D132950
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include "mozilla/Utf8.h" // mozilla::Utf8Unit
|
||||
|
||||
#include "js/OffThreadScriptCompilation.h"
|
||||
#include "js/SourceText.h"
|
||||
|
||||
#include "ModuleLoadRequest.h"
|
||||
#include "nsContentUtils.h"
|
||||
@@ -22,6 +23,8 @@
|
||||
#include "ScriptLoadRequest.h"
|
||||
#include "ScriptSettings.h"
|
||||
|
||||
using JS::SourceText;
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
@@ -272,6 +275,66 @@ void ScriptLoadRequest::SetIsLoadRequest(nsIScriptElement* aElement) {
|
||||
mFetchOptions->mIsPreload = false;
|
||||
}
|
||||
|
||||
nsresult ScriptLoadRequest::GetScriptSource(JSContext* aCx,
|
||||
MaybeSourceText* aMaybeSource) {
|
||||
// If there's no script text, we try to get it from the element
|
||||
if (mIsInline) {
|
||||
nsAutoString inlineData;
|
||||
GetScriptElement()->GetScriptText(inlineData);
|
||||
|
||||
size_t nbytes = inlineData.Length() * sizeof(char16_t);
|
||||
JS::UniqueTwoByteChars chars(
|
||||
static_cast<char16_t*>(JS_malloc(aCx, nbytes)));
|
||||
if (!chars) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
memcpy(chars.get(), inlineData.get(), nbytes);
|
||||
|
||||
SourceText<char16_t> srcBuf;
|
||||
if (!srcBuf.init(aCx, std::move(chars), inlineData.Length())) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
aMaybeSource->construct<SourceText<char16_t>>(std::move(srcBuf));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
size_t length = ScriptTextLength();
|
||||
if (IsUTF16Text()) {
|
||||
JS::UniqueTwoByteChars chars;
|
||||
chars.reset(ScriptText<char16_t>().extractOrCopyRawBuffer());
|
||||
if (!chars) {
|
||||
JS_ReportOutOfMemory(aCx);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
SourceText<char16_t> srcBuf;
|
||||
if (!srcBuf.init(aCx, std::move(chars), length)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
aMaybeSource->construct<SourceText<char16_t>>(std::move(srcBuf));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
MOZ_ASSERT(IsUTF8Text());
|
||||
UniquePtr<Utf8Unit[], JS::FreePolicy> chars;
|
||||
chars.reset(ScriptText<Utf8Unit>().extractOrCopyRawBuffer());
|
||||
if (!chars) {
|
||||
JS_ReportOutOfMemory(aCx);
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
SourceText<Utf8Unit> srcBuf;
|
||||
if (!srcBuf.init(aCx, std::move(chars), length)) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
aMaybeSource->construct<SourceText<Utf8Unit>>(std::move(srcBuf));
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////
|
||||
// ScriptLoadRequestList
|
||||
//////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user