Bug 1544882 - Make BytecodeCompiler::assignSource<Unit>(...) directly defer to a new ScriptSource::assignSource<Unit>(...) function. r=arai
authorJeff Walden <jwalden@mit.edu>
Mon, 15 Apr 2019 16:55:51 -0700
changeset 471358 762b78b66248061140984b2a2876a329c6479389
parent 471357 14248be0eba939ce2317e887e205386ad3214774
child 471359 7e238465037a8a9f7fedb033e50c9d4e9cb9c5f6
push id112914
push userjwalden@mit.edu
push dateThu, 25 Apr 2019 23:25:27 +0000
treeherdermozilla-inbound@8be1bf5f1e97 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersarai
bugs1544882
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1544882 - Make BytecodeCompiler::assignSource<Unit>(...) directly defer to a new ScriptSource::assignSource<Unit>(...) function. r=arai Differential Revision: https://phabricator.services.mozilla.com/D27770
js/src/frontend/BytecodeCompilation.h
js/src/frontend/BytecodeCompiler.cpp
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
--- a/js/src/frontend/BytecodeCompilation.h
+++ b/js/src/frontend/BytecodeCompilation.h
@@ -95,17 +95,19 @@ class MOZ_STACK_CLASS BytecodeCompiler {
 
   MOZ_MUST_USE bool emplaceEmitter(mozilla::Maybe<BytecodeEmitter>& emitter,
                                    const EitherParser& parser,
                                    SharedContext* sharedContext);
 
   // This function lives here, not in SourceAwareCompiler, because it mostly
   // uses fields in *this* class.
   template <typename Unit>
-  MOZ_MUST_USE bool assignSource(JS::SourceText<Unit>& sourceBuffer);
+  MOZ_MUST_USE bool assignSource(JS::SourceText<Unit>& sourceBuffer) {
+    return scriptSource->assignSource(cx, options, sourceBuffer);
+  }
 
   bool canLazilyParse() const;
 
   MOZ_MUST_USE bool deoptimizeArgumentsInEnclosingScripts(
       JSContext* cx, JS::Handle<JSObject*> environment);
 };
 
 class MOZ_STACK_CLASS GlobalScriptInfo final : public BytecodeCompiler {
--- a/js/src/frontend/BytecodeCompiler.cpp
+++ b/js/src/frontend/BytecodeCompiler.cpp
@@ -395,29 +395,16 @@ bool BytecodeCompiler::createScriptSourc
   if (!sourceObject) {
     return false;
   }
 
   scriptSource = sourceObject->source();
   return true;
 }
 
-template <typename Unit>
-bool BytecodeCompiler::assignSource(SourceText<Unit>& sourceBuffer) {
-  if (!cx->realm()->behaviors().discardSource()) {
-    if (options.sourceIsLazy) {
-      scriptSource->setSourceRetrievable();
-    } else if (!scriptSource->setSourceCopy(cx, sourceBuffer)) {
-      return false;
-    }
-  }
-
-  return true;
-}
-
 bool BytecodeCompiler::canLazilyParse() const {
   return options.canLazilyParse &&
          !cx->realm()->behaviors().disableLazyParsing() &&
          !cx->realm()->behaviors().discardSource() && !options.sourceIsLazy &&
          !coverage::IsLCovEnabled() &&
          // Disabled during record/replay. The replay debugger requires
          // scripts to be constructed in a consistent order, which might not
          // happen with lazy parsing.
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -2226,18 +2226,30 @@ MOZ_MUST_USE bool ScriptSource::setCompr
     return false;
   }
 
   setCompressedSource<Unit>(std::move(*deduped), sourceLength);
   return true;
 }
 
 template <typename Unit>
-bool ScriptSource::setSourceCopy(JSContext* cx, SourceText<Unit>& srcBuf) {
-  MOZ_ASSERT(!hasSourceText());
+bool ScriptSource::assignSource(JSContext* cx,
+                                const ReadOnlyCompileOptions& options,
+                                SourceText<Unit>& srcBuf) {
+  MOZ_ASSERT(data.is<Missing>(),
+             "source assignment should only occur on fresh ScriptSources");
+
+  if (cx->realm()->behaviors().discardSource()) {
+    return true;
+  }
+
+  if (options.sourceIsLazy) {
+    sourceRetrievable_ = true;
+    return true;
+  }
 
   JSRuntime* runtime = cx->zone()->runtimeFromAnyThread();
   auto& cache = runtime->sharedImmutableStrings();
   auto deduped = cache.getOrCreate(srcBuf.get(), srcBuf.length(), [&srcBuf]() {
     using CharT = typename SourceTypeTraits<Unit>::CharT;
     return srcBuf.ownsUnits()
                ? UniquePtr<CharT[], JS::FreePolicy>(srcBuf.takeChars())
                : DuplicateString(srcBuf.get(), srcBuf.length());
@@ -2246,20 +2258,22 @@ bool ScriptSource::setSourceCopy(JSConte
     ReportOutOfMemory(cx);
     return false;
   }
 
   setSource<Unit>(std::move(*deduped));
   return true;
 }
 
-template bool ScriptSource::setSourceCopy(JSContext* cx,
-                                          SourceText<char16_t>& srcBuf);
-template bool ScriptSource::setSourceCopy(JSContext* cx,
-                                          SourceText<Utf8Unit>& srcBuf);
+template bool ScriptSource::assignSource(JSContext* cx,
+                                         const ReadOnlyCompileOptions& options,
+                                         SourceText<char16_t>& srcBuf);
+template bool ScriptSource::assignSource(JSContext* cx,
+                                         const ReadOnlyCompileOptions& options,
+                                         SourceText<Utf8Unit>& srcBuf);
 
 void ScriptSource::trace(JSTracer* trc) {
 #ifdef JS_BUILD_BINAST
   if (binASTMetadata_) {
     binASTMetadata_->trace(trc);
   }
 #else
   MOZ_ASSERT(!binASTMetadata_);
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -696,20 +696,22 @@ class ScriptSource {
 
  public:
   // Attempt to load usable source for |ss| -- source text on which substring
   // operations and the like can be performed.  On success return true and set
   // |*loaded| to indicate whether usable source could be loaded; otherwise
   // return false.
   static bool loadSource(JSContext* cx, ScriptSource* ss, bool* loaded);
 
+  // Assign source data from |srcBuf| to this recently-created |ScriptSource|.
   template <typename Unit>
-  MOZ_MUST_USE bool setSourceCopy(JSContext* cx, JS::SourceText<Unit>& srcBuf);
-
-  void setSourceRetrievable() { sourceRetrievable_ = true; }
+  MOZ_MUST_USE bool assignSource(JSContext* cx,
+                                 const JS::ReadOnlyCompileOptions& options,
+                                 JS::SourceText<Unit>& srcBuf);
+
   bool sourceRetrievable() const { return sourceRetrievable_; }
   bool hasSourceText() const {
     return hasUncompressedSource() || hasCompressedSource();
   }
   bool hasBinASTSource() const { return data.is<BinAST>(); }
 
   void setBinASTSourceMetadata(frontend::BinASTSourceMetadata* metadata) {
     MOZ_ASSERT(hasBinASTSource());