Bug 1544882 - Reformulate ScriptSource::loadSource into Variant-matching code. r=arai
authorJeff Walden <jwalden@mit.edu>
Mon, 15 Apr 2019 15:35:44 -0700
changeset 530254 14248be0eba939ce2317e887e205386ad3214774
parent 530253 8509f60299dfdb085a8a64ea45522bacb82b16c5
child 530255 762b78b66248061140984b2a2876a329c6479389
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [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 - Reformulate ScriptSource::loadSource into Variant-matching code. r=arai Differential Revision: https://phabricator.services.mozilla.com/D27769
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -1647,43 +1647,77 @@ void ScriptSourceObject::setPrivate(JSRu
   // private data.
   JS::AutoSuppressGCAnalysis nogc;
   Value prevValue = getReservedSlot(PRIVATE_SLOT);
   rt->releaseScriptPrivate(prevValue);
   setReservedSlot(PRIVATE_SLOT, value);
   rt->addRefScriptPrivate(value);
 }
 
+class ScriptSource::LoadSourceMatcher {
+  JSContext* const cx_;
+  ScriptSource* const ss_;
+  bool* const loaded_;
+
+ public:
+  explicit LoadSourceMatcher(JSContext* cx, ScriptSource* ss, bool* loaded)
+      : cx_(cx), ss_(ss), loaded_(loaded) {}
+
+  template <typename Unit>
+  bool operator()(const Compressed<Unit>&) const {
+    return sourceAlreadyLoaded();
+  }
+
+  template <typename Unit>
+  bool operator()(const Uncompressed<Unit>&) const {
+    return sourceAlreadyLoaded();
+  }
+
+  bool operator()(const Missing&) const { return tryLoadingSource(); }
+
+  bool operator()(const BinAST&) const { return tryLoadingSource(); }
+
+ private:
+  bool sourceAlreadyLoaded() const {
+    *loaded_ = true;
+    return true;
+  }
+
+  bool tryLoadingSource() const {
+    // Establish the default outcome first.
+    *loaded_ = false;
+
+    if (!cx_->runtime()->sourceHook.ref() || !ss_->sourceRetrievable()) {
+      return true;
+    }
+
+    char16_t* src = nullptr;
+    size_t length;
+    if (!cx_->runtime()->sourceHook->load(cx_, ss_->filename(), &src,
+                                          &length)) {
+      return false;
+    }
+    if (!src) {
+      return true;
+    }
+
+    // XXX On-demand source is currently only UTF-16.  Perhaps it should be
+    //     changed to UTF-8, or UTF-8 be allowed in addition to UTF-16?
+    if (!ss_->setSource(cx_, EntryUnits<char16_t>(src), length)) {
+      return false;
+    }
+
+    *loaded_ = true;
+    return true;
+  }
+};
+
 /* static */
 bool ScriptSource::loadSource(JSContext* cx, ScriptSource* ss, bool* loaded) {
-  *loaded = ss->hasSourceText();
-  if (*loaded) {
-    return true;
-  }
-
-  if (!cx->runtime()->sourceHook.ref() || !ss->sourceRetrievable()) {
-    return true;
-  }
-  char16_t* src = nullptr;
-  size_t length;
-  if (!cx->runtime()->sourceHook->load(cx, ss->filename(), &src, &length)) {
-    return false;
-  }
-  if (!src) {
-    return true;
-  }
-
-  // XXX On-demand source is currently only UTF-16.  Perhaps it should be
-  //     changed to UTF-8, or UTF-8 be allowed in addition to UTF-16?
-  if (!ss->setSource(cx, EntryUnits<char16_t>(src), length)) {
-    return false;
-  }
-
-  *loaded = true;
-  return true;
+  return ss->data.match(LoadSourceMatcher(cx, ss, loaded));
 }
 
 /* static */
 JSFlatString* JSScript::sourceData(JSContext* cx, HandleScript script) {
   MOZ_ASSERT(script->scriptSource()->hasSourceText());
   return script->scriptSource()->substring(cx, script->sourceStart(),
                                            script->sourceEnd());
 }
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -686,16 +686,20 @@ class ScriptSource {
       const mozilla::Maybe<uint32_t>& parameterListEnd = mozilla::Nothing());
 
   /**
    * The minimum script length (in code units) necessary for a script to be
    * eligible to be compressed.
    */
   static constexpr size_t MinimumCompressibleLength = 256;
 
+ private:
+  class LoadSourceMatcher;
+
+ 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);
 
   template <typename Unit>
   MOZ_MUST_USE bool setSourceCopy(JSContext* cx, JS::SourceText<Unit>& srcBuf);