Bug 1498320 - Implement ScriptSource::setSourceCopy to work for both UTF-8 and UTF-16 source text. r=tcampbell
authorJeff Walden <jwalden@mit.edu>
Thu, 01 Nov 2018 17:34:55 -0700
changeset 446510 ecf813f9f9ea656b973f79c25352e7f2727f6c0f
parent 446509 9d7d25507cacca3488f4efd3e4034d3a45d405eb
child 446511 ba4bdabbdd529c9d29357073b66b36ccf23caf74
push id35042
push useraiakab@mozilla.com
push dateThu, 15 Nov 2018 09:54:38 +0000
treeherdermozilla-central@dca9c72df68b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstcampbell
bugs1498320
milestone65.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 1498320 - Implement ScriptSource::setSourceCopy to work for both UTF-8 and UTF-16 source text. r=tcampbell
js/src/vm/JSScript.cpp
js/src/vm/JSScript.h
--- a/js/src/vm/JSScript.cpp
+++ b/js/src/vm/JSScript.cpp
@@ -1996,37 +1996,41 @@ ScriptSource::setCompressedSource(JSCont
         ReportOutOfMemory(cx);
         return false;
     }
 
     setCompressedSource<Unit>(std::move(*deduped), sourceLength);
     return true;
 }
 
+template<typename Unit>
 bool
-ScriptSource::setSourceCopy(JSContext* cx, SourceText<char16_t>& srcBuf)
+ScriptSource::setSourceCopy(JSContext* cx, SourceText<Unit>& srcBuf)
 {
     MOZ_ASSERT(!hasSourceText());
 
     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()
-               ? UniqueTwoByteChars(srcBuf.takeChars())
+               ? UniquePtr<CharT[], JS::FreePolicy>(srcBuf.takeChars())
                : DuplicateString(srcBuf.get(), srcBuf.length());
     });
     if (!deduped) {
         ReportOutOfMemory(cx);
         return false;
     }
 
-    setSource<char16_t>(std::move(*deduped));
+    setSource<Unit>(std::move(*deduped));
     return true;
 }
 
+template bool ScriptSource::setSourceCopy(JSContext* cx, SourceText<char16_t>& srcBuf);
+
 void
 ScriptSource::trace(JSTracer* trc)
 {
 #ifdef JS_BUILD_BINAST
     if (binASTMetadata_) {
         binASTMetadata_->trace(trc);
     }
 #else
--- a/js/src/vm/JSScript.h
+++ b/js/src/vm/JSScript.h
@@ -666,17 +666,20 @@ class ScriptSource
         MOZ_ASSERT(refs != 0);
         if (--refs == 0) {
             js_delete(this);
         }
     }
     MOZ_MUST_USE bool initFromOptions(JSContext* cx,
                                       const JS::ReadOnlyCompileOptions& options,
                                       const mozilla::Maybe<uint32_t>& parameterListEnd = mozilla::Nothing());
-    MOZ_MUST_USE bool setSourceCopy(JSContext* cx, JS::SourceText<char16_t>& srcBuf);
+
+    template<typename Unit>
+    MOZ_MUST_USE bool setSourceCopy(JSContext* cx, JS::SourceText<Unit>& srcBuf);
+
     void setSourceRetrievable() { sourceRetrievable_ = true; }
     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());
         binASTMetadata_.reset(metadata);