forked from mirrors/gecko-dev
		
	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:
		
							parent
							
								
									368001bc1d
								
							
						
					
					
						commit
						1aeb55dd59
					
				
					 4 changed files with 50 additions and 31 deletions
				
			
		|  | @ -187,11 +187,10 @@ nsresult ModuleLoader::CompileOrFinishModuleScript( | |||
|     nsresult rv = aRequest->GetScriptSource(aCx, &maybeSource); | ||||
|     NS_ENSURE_SUCCESS(rv, rv); | ||||
| 
 | ||||
|     stencil = maybeSource.constructed<SourceText<char16_t>>() | ||||
|                   ? JS::CompileModuleScriptToStencil( | ||||
|                         aCx, aOptions, maybeSource.ref<SourceText<char16_t>>()) | ||||
|                   : JS::CompileModuleScriptToStencil( | ||||
|                         aCx, aOptions, maybeSource.ref<SourceText<Utf8Unit>>()); | ||||
|     auto compile = [&](auto& source) { | ||||
|       return JS::CompileModuleScriptToStencil(aCx, aOptions, source); | ||||
|     }; | ||||
|     stencil = maybeSource.mapNonEmpty(compile); | ||||
|   } else { | ||||
|     MOZ_ASSERT(aRequest->IsBytecode()); | ||||
|     JS::DecodeOptions decodeOptions(aOptions); | ||||
|  |  | |||
|  | @ -1589,17 +1589,18 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, | |||
|     nsresult rv = aRequest->GetScriptSource(cx, &maybeSource); | ||||
|     NS_ENSURE_SUCCESS(rv, rv); | ||||
| 
 | ||||
|     aRequest->GetScriptLoadContext()->mOffThreadToken = | ||||
|         maybeSource.constructed<SourceText<char16_t>>() | ||||
|             ? JS::CompileModuleToStencilOffThread( | ||||
|                   cx, options, maybeSource.ref<SourceText<char16_t>>(), | ||||
|                   OffThreadScriptLoaderCallback, static_cast<void*>(runnable)) | ||||
|             : JS::CompileModuleToStencilOffThread( | ||||
|                   cx, options, maybeSource.ref<SourceText<Utf8Unit>>(), | ||||
|                   OffThreadScriptLoaderCallback, static_cast<void*>(runnable)); | ||||
|     if (!aRequest->GetScriptLoadContext()->mOffThreadToken) { | ||||
|     auto compile = [&](auto& source) { | ||||
|       return JS::CompileModuleToStencilOffThread( | ||||
|           cx, options, source, OffThreadScriptLoaderCallback, runnable.get()); | ||||
|     }; | ||||
| 
 | ||||
|     MOZ_ASSERT(!maybeSource.empty()); | ||||
|     JS::OffThreadToken* token = maybeSource.mapNonEmpty(compile); | ||||
|     if (!token) { | ||||
|       return NS_ERROR_OUT_OF_MEMORY; | ||||
|     } | ||||
| 
 | ||||
|     aRequest->GetScriptLoadContext()->mOffThreadToken = token; | ||||
|   } else { | ||||
|     MOZ_ASSERT(aRequest->IsTextSource()); | ||||
| 
 | ||||
|  | @ -1638,17 +1639,18 @@ nsresult ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, | |||
|       } | ||||
|     } | ||||
| 
 | ||||
|     aRequest->GetScriptLoadContext()->mOffThreadToken = | ||||
|         maybeSource.constructed<SourceText<char16_t>>() | ||||
|             ? JS::CompileToStencilOffThread( | ||||
|                   cx, options, maybeSource.ref<SourceText<char16_t>>(), | ||||
|                   OffThreadScriptLoaderCallback, static_cast<void*>(runnable)) | ||||
|             : JS::CompileToStencilOffThread( | ||||
|                   cx, options, maybeSource.ref<SourceText<Utf8Unit>>(), | ||||
|                   OffThreadScriptLoaderCallback, static_cast<void*>(runnable)); | ||||
|     if (!aRequest->GetScriptLoadContext()->mOffThreadToken) { | ||||
|     auto compile = [&](auto& source) { | ||||
|       return JS::CompileToStencilOffThread( | ||||
|           cx, options, source, OffThreadScriptLoaderCallback, runnable.get()); | ||||
|     }; | ||||
| 
 | ||||
|     MOZ_ASSERT(!maybeSource.empty()); | ||||
|     JS::OffThreadToken* token = maybeSource.mapNonEmpty(compile); | ||||
|     if (!token) { | ||||
|       return NS_ERROR_OUT_OF_MEMORY; | ||||
|     } | ||||
| 
 | ||||
|     aRequest->GetScriptLoadContext()->mOffThreadToken = token; | ||||
|   } | ||||
|   signalOOM.release(); | ||||
| 
 | ||||
|  | @ -2170,10 +2172,11 @@ nsresult ScriptLoader::CompileOrDecodeClassicScript( | |||
|                                 MarkerInnerWindowIdFromJSContext(aCx), | ||||
|                                 profilerLabelString); | ||||
| 
 | ||||
|       auto compile = [&](auto& source) { return aExec.Compile(source); }; | ||||
| 
 | ||||
|       MOZ_ASSERT(!maybeSource.empty()); | ||||
|       TimeStamp startTime = TimeStamp::Now(); | ||||
|       rv = maybeSource.constructed<SourceText<char16_t>>() | ||||
|                ? aExec.Compile(maybeSource.ref<SourceText<char16_t>>()) | ||||
|                : aExec.Compile(maybeSource.ref<SourceText<Utf8Unit>>()); | ||||
|       rv = maybeSource.mapNonEmpty(compile); | ||||
|       mMainThreadParseTime += TimeStamp::Now() - startTime; | ||||
|     } | ||||
|   } | ||||
|  |  | |||
|  | @ -1281,11 +1281,11 @@ static JSString* ToUpperCase(JSContext* cx, JSLinearString* str) { | |||
|     } | ||||
|   } | ||||
| 
 | ||||
|   return newChars.constructed<Latin1Buffer>() | ||||
|              ? newChars.ref<Latin1Buffer>().toStringDontDeflate(cx, | ||||
|                                                                 resultLength) | ||||
|              : newChars.ref<TwoByteBuffer>().toStringDontDeflate(cx, | ||||
|                                                                  resultLength); | ||||
|   auto toString = [&](auto& chars) { | ||||
|     return chars.toStringDontDeflate(cx, resultLength); | ||||
|   }; | ||||
| 
 | ||||
|   return newChars.mapNonEmpty(toString); | ||||
| } | ||||
| 
 | ||||
| JSString* js::StringToUpperCase(JSContext* cx, HandleString string) { | ||||
|  |  | |||
|  | @ -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: | ||||
|   MaybeOneOf(const MaybeOneOf& aOther) = delete; | ||||
|   const MaybeOneOf& operator=(const MaybeOneOf& aOther) = delete; | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Jon Coppeard
						Jon Coppeard