Bug 1766276 - Give MaybeOneOf a map method r=jandem

There are a few places where we call one of two overloads of a fuction based on
the contents of a MaybeOneOf. We can simplify this code by giving the class a
map method.

Differential Revision: https://phabricator.services.mozilla.com/D144595
This commit is contained in:
Jon Coppeard 2022-04-26 12:29:24 +00:00
parent 368001bc1d
commit 1aeb55dd59
4 changed files with 50 additions and 31 deletions

View file

@ -187,11 +187,10 @@ nsresult ModuleLoader::CompileOrFinishModuleScript(
nsresult rv = aRequest->GetScriptSource(aCx, &maybeSource); nsresult rv = aRequest->GetScriptSource(aCx, &maybeSource);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
stencil = maybeSource.constructed<SourceText<char16_t>>() auto compile = [&](auto& source) {
? JS::CompileModuleScriptToStencil( return JS::CompileModuleScriptToStencil(aCx, aOptions, source);
aCx, aOptions, maybeSource.ref<SourceText<char16_t>>()) };
: JS::CompileModuleScriptToStencil( stencil = maybeSource.mapNonEmpty(compile);
aCx, aOptions, maybeSource.ref<SourceText<Utf8Unit>>());
} else { } else {
MOZ_ASSERT(aRequest->IsBytecode()); MOZ_ASSERT(aRequest->IsBytecode());
JS::DecodeOptions decodeOptions(aOptions); JS::DecodeOptions decodeOptions(aOptions);

View file

@ -1589,17 +1589,18 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
nsresult rv = aRequest->GetScriptSource(cx, &maybeSource); nsresult rv = aRequest->GetScriptSource(cx, &maybeSource);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
aRequest->GetScriptLoadContext()->mOffThreadToken = auto compile = [&](auto& source) {
maybeSource.constructed<SourceText<char16_t>>() return JS::CompileModuleToStencilOffThread(
? JS::CompileModuleToStencilOffThread( cx, options, source, OffThreadScriptLoaderCallback, runnable.get());
cx, options, maybeSource.ref<SourceText<char16_t>>(), };
OffThreadScriptLoaderCallback, static_cast<void*>(runnable))
: JS::CompileModuleToStencilOffThread( MOZ_ASSERT(!maybeSource.empty());
cx, options, maybeSource.ref<SourceText<Utf8Unit>>(), JS::OffThreadToken* token = maybeSource.mapNonEmpty(compile);
OffThreadScriptLoaderCallback, static_cast<void*>(runnable)); if (!token) {
if (!aRequest->GetScriptLoadContext()->mOffThreadToken) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
aRequest->GetScriptLoadContext()->mOffThreadToken = token;
} else { } else {
MOZ_ASSERT(aRequest->IsTextSource()); MOZ_ASSERT(aRequest->IsTextSource());
@ -1638,17 +1639,18 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest,
} }
} }
aRequest->GetScriptLoadContext()->mOffThreadToken = auto compile = [&](auto& source) {
maybeSource.constructed<SourceText<char16_t>>() return JS::CompileToStencilOffThread(
? JS::CompileToStencilOffThread( cx, options, source, OffThreadScriptLoaderCallback, runnable.get());
cx, options, maybeSource.ref<SourceText<char16_t>>(), };
OffThreadScriptLoaderCallback, static_cast<void*>(runnable))
: JS::CompileToStencilOffThread( MOZ_ASSERT(!maybeSource.empty());
cx, options, maybeSource.ref<SourceText<Utf8Unit>>(), JS::OffThreadToken* token = maybeSource.mapNonEmpty(compile);
OffThreadScriptLoaderCallback, static_cast<void*>(runnable)); if (!token) {
if (!aRequest->GetScriptLoadContext()->mOffThreadToken) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
aRequest->GetScriptLoadContext()->mOffThreadToken = token;
} }
signalOOM.release(); signalOOM.release();
@ -2170,10 +2172,11 @@ nsresult ScriptLoader::CompileOrDecodeClassicScript(
MarkerInnerWindowIdFromJSContext(aCx), MarkerInnerWindowIdFromJSContext(aCx),
profilerLabelString); profilerLabelString);
auto compile = [&](auto& source) { return aExec.Compile(source); };
MOZ_ASSERT(!maybeSource.empty());
TimeStamp startTime = TimeStamp::Now(); TimeStamp startTime = TimeStamp::Now();
rv = maybeSource.constructed<SourceText<char16_t>>() rv = maybeSource.mapNonEmpty(compile);
? aExec.Compile(maybeSource.ref<SourceText<char16_t>>())
: aExec.Compile(maybeSource.ref<SourceText<Utf8Unit>>());
mMainThreadParseTime += TimeStamp::Now() - startTime; mMainThreadParseTime += TimeStamp::Now() - startTime;
} }
} }

View file

@ -1281,11 +1281,11 @@ static JSString* ToUpperCase(JSContext* cx, JSLinearString* str) {
} }
} }
return newChars.constructed<Latin1Buffer>() auto toString = [&](auto& chars) {
? newChars.ref<Latin1Buffer>().toStringDontDeflate(cx, return chars.toStringDontDeflate(cx, resultLength);
resultLength) };
: newChars.ref<TwoByteBuffer>().toStringDontDeflate(cx,
resultLength); return newChars.mapNonEmpty(toString);
} }
JSString* js::StringToUpperCase(JSContext* cx, HandleString string) { JSString* js::StringToUpperCase(JSContext* cx, HandleString string) {

View file

@ -131,6 +131,23 @@ class MOZ_NON_PARAM MaybeOneOf {
} }
} }
template <typename Func>
constexpr auto mapNonEmpty(Func&& aFunc) const {
MOZ_ASSERT(!empty());
if (state == SomeT1) {
return std::forward<Func>(aFunc)(as<T1>());
}
return std::forward<Func>(aFunc)(as<T2>());
}
template <typename Func>
constexpr auto mapNonEmpty(Func&& aFunc) {
MOZ_ASSERT(!empty());
if (state == SomeT1) {
return std::forward<Func>(aFunc)(as<T1>());
}
return std::forward<Func>(aFunc)(as<T2>());
}
private: private:
MaybeOneOf(const MaybeOneOf& aOther) = delete; MaybeOneOf(const MaybeOneOf& aOther) = delete;
const MaybeOneOf& operator=(const MaybeOneOf& aOther) = delete; const MaybeOneOf& operator=(const MaybeOneOf& aOther) = delete;