Bug 1557791 - Introduce |{un,}compressedData<Unit>()| functions that expose |const CompressedData<Unit>&|. r=arai
authorJeff Walden <jwalden@mit.edu>
Thu, 06 Jun 2019 14:09:42 -0700
changeset 478270 3048fe4c98f02d2e9ffd6aaf4ec49125b8ab7a1d
parent 478269 53f1e230255dd7419fdc6b934d5d15faa276df73
child 478271 20b8bd4bcb727de80bac55efeeadbcbfa12b9de2
push id5
push uservporof@mozilla.com
push dateWed, 12 Jun 2019 10:24:37 +0000
reviewersarai
bugs1557791
milestone69.0a1
Bug 1557791 - Introduce |{un,}compressedData<Unit>()| functions that expose |const CompressedData<Unit>&|. r=arai Differential Revision: https://phabricator.services.mozilla.com/D34202
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -1836,17 +1836,17 @@ size_t UncompressedSourceCache::sizeOfEx
   }
   return n;
 }
 
 template <typename Unit>
 const Unit* ScriptSource::chunkUnits(
     JSContext* cx, UncompressedSourceCache::AutoHoldEntry& holder,
     size_t chunk) {
-  const CompressedData<Unit>& c = data.as<Compressed<Unit>>();
+  const CompressedData<Unit>& c = *compressedData<Unit>();
 
   ScriptSourceChunk ssc(this, chunk);
   if (const Unit* decompressed =
           cx->caches().uncompressedSourceCache.lookup<Unit>(ssc, holder)) {
     return decompressed;
   }
 
   size_t totalLengthInBytes = length() * sizeof(Unit);
@@ -1909,17 +1909,17 @@ ScriptSource::PinnedUnits<Unit>::~Pinned
 template <typename Unit>
 const Unit* ScriptSource::units(JSContext* cx,
                                 UncompressedSourceCache::AutoHoldEntry& holder,
                                 size_t begin, size_t len) {
   MOZ_ASSERT(begin <= length());
   MOZ_ASSERT(begin + len <= length());
 
   if (isUncompressed<Unit>()) {
-    const Unit* units = data.as<Uncompressed<Unit>>().units();
+    const Unit* units = uncompressedData<Unit>()->units();
     if (!units) {
       return nullptr;
     }
     return units + begin;
   }
 
   if (data.is<Missing>()) {
     MOZ_CRASH("ScriptSource::units() on ScriptSource with missing source");
@@ -2341,18 +2341,17 @@ void SourceCompressionTask::workEncoding
   // size of the string, first.
   size_t inputBytes = source->length() * sizeof(Unit);
   size_t firstSize = inputBytes / 2;
   UniqueChars compressed(js_pod_malloc<char>(firstSize));
   if (!compressed) {
     return;
   }
 
-  const Unit* chars =
-      source->data.as<ScriptSource::Uncompressed<Unit>>().units();
+  const Unit* chars = source->uncompressedData<Unit>()->units();
   Compressor comp(reinterpret_cast<const unsigned char*>(chars), inputBytes);
   if (!comp.init()) {
     return;
   }
 
   comp.setOutput(reinterpret_cast<unsigned char*>(compressed.get()), firstSize);
   bool cont = true;
   bool reallocated = false;
@@ -2588,17 +2587,18 @@ struct UnretrievableSourceEncoder {
   ScriptSource* const source_;
   const uint32_t uncompressedLength_;
 
   UnretrievableSourceEncoder(XDRState<XDR_ENCODE>* xdr, ScriptSource* source,
                              uint32_t uncompressedLength)
       : xdr_(xdr), source_(source), uncompressedLength_(uncompressedLength) {}
 
   XDRResult encode() {
-    Unit* sourceUnits = const_cast<Unit*>(source_->uncompressedData<Unit>());
+    Unit* sourceUnits =
+        const_cast<Unit*>(source_->uncompressedData<Unit>()->units());
 
     return xdr_->codeChars(sourceUnits, uncompressedLength_);
   }
 };
 
 namespace js {
 
 template <>
@@ -2641,17 +2641,17 @@ XDRResult ScriptSource::codeUncompressed
     if (mode == XDR_DECODE) {
       ss->data = SourceType(Retrievable<Unit>());
     }
     return Ok();
   }
 
   uint32_t uncompressedLength;
   if (mode == XDR_ENCODE) {
-    uncompressedLength = ss->data.as<Uncompressed<Unit>>().length();
+    uncompressedLength = ss->uncompressedData<Unit>()->length();
   }
   MOZ_TRY(xdr->codeUint32(&uncompressedLength));
 
   return ss->xdrUnretrievableUncompressedSource(xdr, sizeof(Unit),
                                                 uncompressedLength);
 }
 
 template <typename Unit, XDRMode mode>
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -779,52 +779,54 @@ class ScriptSource {
   frontend::BinASTSourceMetadata* binASTSourceMetadata() const {
     MOZ_ASSERT(hasBinASTSource());
     return data.as<BinAST>().metadata.get();
   }
 
  private:
   template <typename Unit>
   struct UncompressedDataMatcher {
-    const Unit* operator()(const Uncompressed<Unit>& u) { return u.units(); }
+    const UncompressedData<Unit>* operator()(const Uncompressed<Unit>& u) {
+      return &u;
+    }
 
     template <typename T>
-    const Unit* operator()(const T&) {
+    const UncompressedData<Unit>* operator()(const T&) {
       MOZ_CRASH(
           "attempting to access uncompressed data in a ScriptSource not "
           "containing it");
       return nullptr;
     }
   };
 
  public:
   template <typename Unit>
-  const Unit* uncompressedData() {
+  const UncompressedData<Unit>* uncompressedData() {
     return data.match(UncompressedDataMatcher<Unit>());
   }
 
  private:
   template <typename Unit>
   struct CompressedDataMatcher {
-    char* operator()(const Compressed<Unit>& c) {
-      return const_cast<char*>(c.raw.chars());
+    const CompressedData<Unit>* operator()(const Compressed<Unit>& c) {
+      return &c;
     }
 
     template <typename T>
-    char* operator()(const T&) {
+    const CompressedData<Unit>* operator()(const T&) {
       MOZ_CRASH(
           "attempting to access compressed data in a ScriptSource not "
           "containing it");
       return nullptr;
     }
   };
 
  public:
   template <typename Unit>
-  char* compressedData() {
+  const CompressedData<Unit>* compressedData() {
     return data.match(CompressedDataMatcher<Unit>());
   }
 
  private:
   struct BinASTDataMatcher {
     void* operator()(const BinAST& b) {
       return const_cast<char*>(b.string.chars());
     }