diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 1c3fb82e3fa1..3c39779198c5 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -391,14 +391,15 @@ class SourceBufferHolder final length_(dataLength), ownsChars_(ownership == GiveOwnership) { - // Ensure that null buffers properly return an unowned, empty, - // null-terminated string. - static const char16_t NullChar_ = 0; - if (!get()) { - data_ = &NullChar_; - length_ = 0; - ownsChars_ = false; - } + fixEmptyBuffer(); + } + + SourceBufferHolder(UniqueTwoByteChars&& data, size_t dataLength) + : data_(data.release()), + length_(dataLength), + ownsChars_(true) + { + fixEmptyBuffer(); } SourceBufferHolder(SourceBufferHolder&& other) @@ -448,6 +449,17 @@ class SourceBufferHolder final SourceBufferHolder(SourceBufferHolder&) = delete; SourceBufferHolder& operator=(SourceBufferHolder&) = delete; + void fixEmptyBuffer() { + // Ensure that null buffers properly return an unowned, empty, + // null-terminated string. + static const char16_t NullChar_ = 0; + if (!get()) { + data_ = &NullChar_; + length_ = 0; + ownsChars_ = false; + } + } + const char16_t* data_; size_t length_; bool ownsChars_; diff --git a/js/xpconnect/loader/ChromeScriptLoader.cpp b/js/xpconnect/loader/ChromeScriptLoader.cpp index faeb90acb892..5f2f26e4f276 100644 --- a/js/xpconnect/loader/ChromeScriptLoader.cpp +++ b/js/xpconnect/loader/ChromeScriptLoader.cpp @@ -131,8 +131,7 @@ AsyncScriptCompiler::StartCompile(JSContext* aCx) { Rooted global(aCx, mGlobalObject->GetGlobalJSObject()); - JS::SourceBufferHolder srcBuf(mScriptText.get(), mScriptLength, - JS::SourceBufferHolder::NoOwnership); + JS::SourceBufferHolder srcBuf(std::move(mScriptText), mScriptLength); if (JS::CanCompileOffThread(aCx, mOptions, mScriptLength)) { if (!JS::CompileOffThread(aCx, mOptions, srcBuf, OffThreadScriptLoaderCallback,