From ad89a11136e7aed7775c3d1b0c8c709eed291caf Mon Sep 17 00:00:00 2001 From: Margareta Eliza Balazs Date: Tue, 17 Jul 2018 17:57:55 +0300 Subject: [PATCH] Backed out 7 changesets (bug 1475228) for causing Spidermonkey rust failures on Linux x64 debug Backed out changeset e91802969fb7 (bug 1475228) Backed out changeset 623af73419eb (bug 1475228) Backed out changeset bf96bd78dc11 (bug 1475228) Backed out changeset 104817d51d1b (bug 1475228) Backed out changeset d3829c85f650 (bug 1475228) Backed out changeset 74d10b32b3ea (bug 1475228) Backed out changeset dde64fbe2f0d (bug 1475228) --- dom/base/nsJSUtils.cpp | 5 +- dom/script/ScriptLoadRequest.cpp | 4 +- dom/script/ScriptLoadRequest.h | 25 ++--- dom/script/ScriptLoader.cpp | 80 +++++++--------- dom/script/ScriptLoader.h | 7 +- dom/workers/WorkerPrivate.cpp | 5 +- dom/xul/XULDocument.cpp | 11 ++- dom/xul/nsXULElement.cpp | 2 +- ipc/testshell/XPCShellEnvironment.cpp | 5 +- js/public/AllocPolicy.h | 34 ++++--- js/src/NamespaceImports.h | 2 +- js/src/builtin/TestingFunctions.cpp | 3 +- .../jsapi-tests/testCompileNonSyntactic.cpp | 18 ++-- js/src/jsapi-tests/testJSEvaluateScript.cpp | 3 +- js/src/jsapi-tests/testMutedErrors.cpp | 3 +- js/src/jsapi-tests/testScriptObject.cpp | 9 +- js/src/jsapi.cpp | 94 +++++++++++++++---- js/src/jsapi.h | 87 ++++++++--------- js/src/shell/js.cpp | 40 +++----- js/src/vm/HelperThreads.cpp | 22 +++-- js/src/vm/HelperThreads.h | 12 +-- js/xpconnect/loader/ChromeScriptLoader.cpp | 5 +- js/xpconnect/src/Sandbox.cpp | 5 +- 23 files changed, 256 insertions(+), 225 deletions(-) diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index d2057db0ae11..2e4ea5ed0aae 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -94,12 +94,11 @@ nsJSUtils::CompileFunction(AutoJSAPI& jsapi, // Compile. JS::Rooted fun(cx); - JS::SourceBufferHolder source(PromiseFlatString(aBody).get(), aBody.Length(), - JS::SourceBufferHolder::NoOwnership); if (!JS::CompileFunction(cx, aScopeChain, aOptions, PromiseFlatCString(aName).get(), aArgCount, aArgArray, - source, &fun)) + PromiseFlatString(aBody).get(), + aBody.Length(), &fun)) { return NS_ERROR_FAILURE; } diff --git a/dom/script/ScriptLoadRequest.cpp b/dom/script/ScriptLoadRequest.cpp index 211d97082098..58d49e6e189a 100644 --- a/dom/script/ScriptLoadRequest.cpp +++ b/dom/script/ScriptLoadRequest.cpp @@ -166,7 +166,7 @@ ScriptLoadRequest::SetTextSource() { MOZ_ASSERT(IsUnknownDataType()); mDataType = DataType::eTextSource; - mScriptData.emplace(VariantType()); + mScriptData.emplace(VariantType>()); } void @@ -175,7 +175,7 @@ ScriptLoadRequest::SetBinASTSource() #ifdef JS_BUILD_BINAST MOZ_ASSERT(IsUnknownDataType()); mDataType = DataType::eBinASTSource; - mScriptData.emplace(VariantType()); + mScriptData.emplace(VariantType>()); #else MOZ_CRASH("BinAST not supported"); #endif diff --git a/dom/script/ScriptLoadRequest.h b/dom/script/ScriptLoadRequest.h index 6b429ad51228..2466e4c0cd0e 100644 --- a/dom/script/ScriptLoadRequest.h +++ b/dom/script/ScriptLoadRequest.h @@ -171,28 +171,21 @@ public: void SetBinASTSource(); void SetBytecode(); - using ScriptTextBuffer = Vector; - using BinASTSourceBuffer = Vector; - - const ScriptTextBuffer& ScriptText() const - { + const Vector& ScriptText() const { MOZ_ASSERT(IsTextSource()); - return mScriptData->as(); + return mScriptData->as>(); } - ScriptTextBuffer& ScriptText() - { + Vector& ScriptText() { MOZ_ASSERT(IsTextSource()); - return mScriptData->as(); + return mScriptData->as>(); } - const BinASTSourceBuffer& ScriptBinASTData() const - { + const Vector& ScriptBinASTData() const { MOZ_ASSERT(IsBinASTSource()); - return mScriptData->as(); + return mScriptData->as>(); } - BinASTSourceBuffer& ScriptBinASTData() - { + Vector& ScriptBinASTData() { MOZ_ASSERT(IsBinASTSource()); - return mScriptData->as(); + return mScriptData->as>(); } enum class ScriptMode : uint8_t { @@ -259,7 +252,7 @@ public: // Holds script source data for non-inline scripts. Don't use nsString so we // can give ownership to jsapi. Holds either char16_t source text characters // or BinAST encoded bytes depending on mSourceEncoding. - Maybe> mScriptData; + Maybe, Vector>> mScriptData; // Holds the SRI serialized hash and the script bytecode for non-inline // scripts. diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 24c449d658d9..7433c688b03b 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -491,7 +491,8 @@ ScriptLoader::CreateModuleScript(ModuleLoadRequest* aRequest) rv = FillCompileOptionsForRequest(aes, aRequest, global, &options); if (NS_SUCCEEDED(rv)) { - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); + nsAutoString inlineData; + SourceBufferHolder srcBuf = GetScriptSource(aRequest, inlineData); rv = nsJSUtils::CompileModule(cx, srcBuf, global, options, &module); } } @@ -1769,16 +1770,14 @@ OffThreadScriptLoaderCallback(JS::OffThreadToken* aToken, void* aCallbackData) } nsresult -ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, - bool* aCouldCompileOut) +ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest) { MOZ_ASSERT_IF(!aRequest->IsModuleRequest(), aRequest->IsReadyToRun()); MOZ_ASSERT(!aRequest->mWasCompiledOMT); - MOZ_ASSERT(aCouldCompileOut && !*aCouldCompileOut); // Don't off-thread compile inline scripts. if (aRequest->mIsInline) { - return NS_OK; + return NS_ERROR_FAILURE; } nsCOMPtr globalObject = GetScriptGlobalObject(); @@ -1802,19 +1801,19 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, if (aRequest->IsTextSource()) { if (!JS::CanCompileOffThread(cx, options, aRequest->ScriptText().length())) { - return NS_OK; + return NS_ERROR_FAILURE; } #ifdef JS_BUILD_BINAST } else if (aRequest->IsBinASTSource()) { if (!JS::CanDecodeBinASTOffThread(cx, options, aRequest->ScriptBinASTData().length())) { - return NS_OK; + return NS_ERROR_FAILURE; } #endif } else { MOZ_ASSERT(aRequest->IsBytecode()); size_t length = aRequest->mScriptBytecode.length() - aRequest->mBytecodeOffset; if (!JS::CanDecodeOffThread(cx, options, length)) { - return NS_OK; + return NS_ERROR_FAILURE; } } @@ -1823,9 +1822,9 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, if (aRequest->IsModuleRequest()) { MOZ_ASSERT(aRequest->IsTextSource()); - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); if (!JS::CompileOffThreadModule(cx, options, - srcBuf, + aRequest->ScriptText().begin(), + aRequest->ScriptText().length(), OffThreadScriptLoaderCallback, static_cast(runnable))) { return NS_ERROR_OUT_OF_MEMORY; @@ -1851,9 +1850,9 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, #endif } else { MOZ_ASSERT(aRequest->IsTextSource()); - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); if (!JS::CompileOffThread(cx, options, - srcBuf, + aRequest->ScriptText().begin(), + aRequest->ScriptText().length(), OffThreadScriptLoaderCallback, static_cast(runnable))) { return NS_ERROR_OUT_OF_MEMORY; @@ -1866,7 +1865,6 @@ ScriptLoader::AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, // to call ScriptLoader::ProcessOffThreadRequest with the same request. aRequest->mProgress = ScriptLoadRequest::Progress::eCompiling; - *aCouldCompileOut = true; Unused << runnable.forget(); return NS_OK; } @@ -1881,42 +1879,33 @@ ScriptLoader::CompileOffThreadOrProcessRequest(ScriptLoadRequest* aRequest) NS_ASSERTION(!aRequest->InCompilingStage(), "Candidate for off-thread compile is already in compiling stage."); - bool couldCompile = false; - nsresult rv = AttemptAsyncScriptCompile(aRequest, &couldCompile); - if (NS_FAILED(rv)) { - HandleLoadError(aRequest, rv); + nsresult rv = AttemptAsyncScriptCompile(aRequest); + if (NS_SUCCEEDED(rv)) { return rv; } - if (couldCompile) { - return NS_OK; - } - return ProcessRequest(aRequest); } SourceBufferHolder -ScriptLoader::GetScriptSource(JSContext* aCx, ScriptLoadRequest* aRequest) +ScriptLoader::GetScriptSource(ScriptLoadRequest* aRequest, nsAutoString& inlineData) { // Return a SourceBufferHolder object holding the script's source text. - // Ownership of the buffer is transferred to the resulting SourceBufferHolder. + // |inlineData| is used to hold the text for inline objects. // If there's no script text, we try to get it from the element if (aRequest->mIsInline) { - nsAutoString inlineData; + // XXX This is inefficient - GetText makes multiple + // copies. aRequest->mElement->GetScriptText(inlineData); - - size_t nbytes = inlineData.Length() * sizeof(char16_t); - JS::UniqueTwoByteChars chars(static_cast(JS_malloc(aCx, nbytes))); - MOZ_RELEASE_ASSERT(chars); - memcpy(chars.get(), inlineData.get(), nbytes); - return SourceBufferHolder(std::move(chars), inlineData.Length()); + return SourceBufferHolder(inlineData.get(), + inlineData.Length(), + SourceBufferHolder::NoOwnership); } - size_t length = aRequest->ScriptText().length(); - return SourceBufferHolder(aRequest->ScriptText().extractOrCopyRawBuffer(), - length, - SourceBufferHolder::GiveOwnership); + return SourceBufferHolder(aRequest->ScriptText().begin(), + aRequest->ScriptText().length(), + SourceBufferHolder::NoOwnership); } nsresult @@ -2361,7 +2350,8 @@ ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) &script); } else { MOZ_ASSERT(aRequest->IsTextSource()); - SourceBufferHolder srcBuf = GetScriptSource(cx, aRequest); + nsAutoString inlineData; + SourceBufferHolder srcBuf = GetScriptSource(aRequest, inlineData); rv = exec.CompileAndExec(options, srcBuf, &script); } } @@ -3190,12 +3180,11 @@ ScriptLoader::PrepareLoadedRequest(ScriptLoadRequest* aRequest, channel->GetURI(getter_AddRefs(request->mBaseURL)); } + // Attempt to compile off main thread. - bool couldCompile = false; - rv = AttemptAsyncScriptCompile(request, &couldCompile); - NS_ENSURE_SUCCESS(rv, rv); - if (couldCompile) { - return NS_OK; + rv = AttemptAsyncScriptCompile(request); + if (NS_SUCCEEDED(rv)) { + return rv; } // Otherwise compile it right away and start fetching descendents. @@ -3208,15 +3197,18 @@ ScriptLoader::PrepareLoadedRequest(ScriptLoadRequest* aRequest, // If this is currently blocking the parser, attempt to compile it off-main-thread. if (aRequest == mParserBlockingRequest && NumberOfProcessors() > 1) { MOZ_ASSERT(!aRequest->IsModuleRequest()); - bool couldCompile = false; - nsresult rv = AttemptAsyncScriptCompile(aRequest, &couldCompile); - NS_ENSURE_SUCCESS(rv, rv); - if (couldCompile) { + nsresult rv = AttemptAsyncScriptCompile(aRequest); + if (rv == NS_OK) { MOZ_ASSERT(aRequest->mProgress == ScriptLoadRequest::Progress::eCompiling, "Request should be off-thread compiling now."); return NS_OK; } + // If off-thread compile errored, return the error. + if (rv != NS_ERROR_FAILURE) { + return rv; + } + // If off-thread compile was rejected, continue with regular processing. } diff --git a/dom/script/ScriptLoader.h b/dom/script/ScriptLoader.h index a710cb50574a..82d6c5120e2f 100644 --- a/dom/script/ScriptLoader.h +++ b/dom/script/ScriptLoader.h @@ -454,8 +454,7 @@ private: void ReportErrorToConsole(ScriptLoadRequest *aRequest, nsresult aResult) const; - nsresult AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest, - bool* aCouldCompileOut); + nsresult AttemptAsyncScriptCompile(ScriptLoadRequest* aRequest); nsresult ProcessRequest(ScriptLoadRequest* aRequest); nsresult CompileOffThreadOrProcessRequest(ScriptLoadRequest* aRequest); void FireScriptAvailable(nsresult aResult, @@ -505,8 +504,8 @@ private: void MaybeMoveToLoadedList(ScriptLoadRequest* aRequest); - JS::SourceBufferHolder GetScriptSource(JSContext* aCx, - ScriptLoadRequest* aRequest); + JS::SourceBufferHolder GetScriptSource(ScriptLoadRequest* aRequest, + nsAutoString& inlineData); void SetModuleFetchStarted(ModuleLoadRequest *aRequest); void SetModuleFetchFinishedAndResumeWaitingRequests(ModuleLoadRequest* aRequest, diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index f72f0f3289ec..43ddb9825871 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -4941,9 +4941,8 @@ WorkerPrivate::RunExpiredTimeouts(JSContext* aCx) JS::Rooted unused(aes.cx()); - JS::SourceBufferHolder srcBuf(script.BeginReading(), script.Length(), - JS::SourceBufferHolder::NoOwnership); - if (!JS::Evaluate(aes.cx(), options, srcBuf, &unused) && + if (!JS::Evaluate(aes.cx(), options, script.BeginReading(), + script.Length(), &unused) && !JS_IsExceptionPending(aCx)) { retval = false; break; diff --git a/dom/xul/XULDocument.cpp b/dom/xul/XULDocument.cpp index a40a5b2dbe1f..603d5da98f6d 100644 --- a/dom/xul/XULDocument.cpp +++ b/dom/xul/XULDocument.cpp @@ -2376,10 +2376,15 @@ XULDocument::OnStreamComplete(nsIStreamLoader* aLoader, rv = mCurrentScriptProto->Compile(srcBuf, uri, 1, this, this); if (NS_SUCCEEDED(rv) && !mCurrentScriptProto->HasScriptObject()) { // We will be notified via OnOffThreadCompileComplete when the - // compile finishes. The JS engine has taken ownership of the - // source buffer. - MOZ_RELEASE_ASSERT(!srcBuf.ownsChars()); + // compile finishes. Keep the contents of the compiled script + // alive until the compilation finishes. mOffThreadCompiling = true; + // If the JS engine did not take the source buffer, then take + // it back here to ensure it remains alive. + mOffThreadCompileStringBuf = srcBuf.take(); + if (mOffThreadCompileStringBuf) { + mOffThreadCompileStringLength = srcBuf.length(); + } BlockOnload(); return NS_OK; } diff --git a/dom/xul/nsXULElement.cpp b/dom/xul/nsXULElement.cpp index ac8424c73328..7272bda69693 100644 --- a/dom/xul/nsXULElement.cpp +++ b/dom/xul/nsXULElement.cpp @@ -2417,7 +2417,7 @@ nsXULPrototypeScript::Compile(JS::SourceBufferHolder& aSrcBuf, if (aOffThreadReceiver && JS::CanCompileOffThread(cx, options, aSrcBuf.length())) { if (!JS::CompileOffThread(cx, options, - aSrcBuf, + aSrcBuf.get(), aSrcBuf.length(), OffThreadScriptReceiverCallback, static_cast(aOffThreadReceiver))) { return NS_ERROR_OUT_OF_MEMORY; diff --git a/ipc/testshell/XPCShellEnvironment.cpp b/ipc/testshell/XPCShellEnvironment.cpp index 903de8718cef..cca7387f908b 100644 --- a/ipc/testshell/XPCShellEnvironment.cpp +++ b/ipc/testshell/XPCShellEnvironment.cpp @@ -487,9 +487,8 @@ XPCShellEnvironment::EvaluateString(const nsString& aString, JS::CompileOptions options(cx); options.setFileAndLine("typein", 0); JS::Rooted script(cx); - JS::SourceBufferHolder srcBuf(aString.get(), aString.Length(), - JS::SourceBufferHolder::NoOwnership); - if (!JS_CompileUCScript(cx, srcBuf, options, &script)) + if (!JS_CompileUCScript(cx, aString.get(), aString.Length(), options, + &script)) { return false; } diff --git a/js/public/AllocPolicy.h b/js/public/AllocPolicy.h index 88c019ad1f44..9a59df1f1c9b 100644 --- a/js/public/AllocPolicy.h +++ b/js/public/AllocPolicy.h @@ -26,9 +26,8 @@ enum class AllocFunction { Calloc, Realloc }; - -/* Base class allocation policies providing allocation methods. */ -class AllocPolicyBase +/* Policy for using system memory functions and doing no error reporting. */ +class SystemAllocPolicy { public: template T* maybe_pod_malloc(size_t numElems) { return js_pod_malloc(numElems); } @@ -42,12 +41,6 @@ class AllocPolicyBase return maybe_pod_realloc(p, oldSize, newSize); } void free_(void* p) { js_free(p); } -}; - -/* Policy for using system memory functions and doing no error reporting. */ -class SystemAllocPolicy : public AllocPolicyBase -{ - public: void reportAllocOverflow() const {} bool checkSimulatedOOM() const { return !js::oom::ShouldFailWithOOM(); @@ -65,7 +58,7 @@ MOZ_COLD JS_FRIEND_API(void) ReportOutOfMemory(JSContext* cx); * FIXME bug 647103 - rewrite this in terms of temporary allocation functions, * not the system ones. */ -class TempAllocPolicy : public AllocPolicyBase +class TempAllocPolicy { JSContext* const cx_; @@ -87,9 +80,24 @@ class TempAllocPolicy : public AllocPolicyBase public: MOZ_IMPLICIT TempAllocPolicy(JSContext* cx) : cx_(cx) {} + template + T* maybe_pod_malloc(size_t numElems) { + return js_pod_malloc(numElems); + } + + template + T* maybe_pod_calloc(size_t numElems) { + return js_pod_calloc(numElems); + } + + template + T* maybe_pod_realloc(T* prior, size_t oldSize, size_t newSize) { + return js_pod_realloc(prior, oldSize, newSize); + } + template T* pod_malloc(size_t numElems) { - T* p = this->maybe_pod_malloc(numElems); + T* p = maybe_pod_malloc(numElems); if (MOZ_UNLIKELY(!p)) p = onOutOfMemoryTyped(AllocFunction::Malloc, numElems); return p; @@ -97,7 +105,7 @@ class TempAllocPolicy : public AllocPolicyBase template T* pod_calloc(size_t numElems) { - T* p = this->maybe_pod_calloc(numElems); + T* p = maybe_pod_calloc(numElems); if (MOZ_UNLIKELY(!p)) p = onOutOfMemoryTyped(AllocFunction::Calloc, numElems); return p; @@ -105,7 +113,7 @@ class TempAllocPolicy : public AllocPolicyBase template T* pod_realloc(T* prior, size_t oldSize, size_t newSize) { - T* p2 = this->maybe_pod_realloc(prior, oldSize, newSize); + T* p2 = maybe_pod_realloc(prior, oldSize, newSize); if (MOZ_UNLIKELY(!p2)) p2 = onOutOfMemoryTyped(AllocFunction::Realloc, newSize, prior); return p2; diff --git a/js/src/NamespaceImports.h b/js/src/NamespaceImports.h index 60ee0c2a1357..e9d5645e9213 100644 --- a/js/src/NamespaceImports.h +++ b/js/src/NamespaceImports.h @@ -38,7 +38,7 @@ using ValueVector = JS::GCVector; using IdVector = JS::GCVector; using ScriptVector = JS::GCVector; -class SourceBufferHolder; +class MOZ_STACK_CLASS SourceBufferHolder; class HandleValueArray; diff --git a/js/src/builtin/TestingFunctions.cpp b/js/src/builtin/TestingFunctions.cpp index 79b5db696fb0..0d26f2fa3a20 100644 --- a/js/src/builtin/TestingFunctions.cpp +++ b/js/src/builtin/TestingFunctions.cpp @@ -5180,8 +5180,7 @@ js::TestingFunctionArgumentToScript(JSContext* cx, RootedScript script(cx); CompileOptions options(cx); - SourceBufferHolder source(chars, len, SourceBufferHolder::NoOwnership); - if (!JS::Compile(cx, options, source, &script)) + if (!JS::Compile(cx, options, chars, len, &script)) return nullptr; return script; } diff --git a/js/src/jsapi-tests/testCompileNonSyntactic.cpp b/js/src/jsapi-tests/testCompileNonSyntactic.cpp index 436f3ba64818..afb404896fc8 100644 --- a/js/src/jsapi-tests/testCompileNonSyntactic.cpp +++ b/js/src/jsapi-tests/testCompileNonSyntactic.cpp @@ -84,11 +84,8 @@ testCompile(bool nonSyntactic) CHECK(CompileForNonSyntacticScope(cx, options, src, length, &script)); CHECK_EQUAL(script->hasNonSyntacticScope(), true); - { - JS::SourceBufferHolder srcBuf(src_16, length, JS::SourceBufferHolder::NoOwnership); - CHECK(CompileForNonSyntacticScope(cx, options, srcBuf, &script)); - CHECK_EQUAL(script->hasNonSyntacticScope(), true); - } + CHECK(CompileForNonSyntacticScope(cx, options, src_16, length, &script)); + CHECK_EQUAL(script->hasNonSyntacticScope(), true); CHECK(Compile(cx, options, buf, &script)); @@ -97,19 +94,16 @@ testCompile(bool nonSyntactic) CHECK(Compile(cx, options, src, length, &script)); CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic); - { - JS::SourceBufferHolder srcBuf(src_16, length, JS::SourceBufferHolder::NoOwnership); - CHECK(Compile(cx, options, srcBuf, &script)); - CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic); - } + CHECK(Compile(cx, options, src_16, length, &script)); + CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic); options.forceAsync = true; OffThreadTask task; OffThreadToken* token; - SourceBufferHolder srcBuf(src_16, length, SourceBufferHolder::NoOwnership); - CHECK(CompileOffThread(cx, options, srcBuf, task.OffThreadCallback, &task)); + CHECK(CompileOffThread(cx, options, src_16, length, + task.OffThreadCallback, &task)); CHECK(token = task.waitUntilDone(cx)); CHECK(script = FinishOffThreadScript(cx, token)); CHECK_EQUAL(script->hasNonSyntacticScope(), nonSyntactic); diff --git a/js/src/jsapi-tests/testJSEvaluateScript.cpp b/js/src/jsapi-tests/testJSEvaluateScript.cpp index f3fd009a9cde..2ba22f8fd581 100644 --- a/js/src/jsapi-tests/testJSEvaluateScript.cpp +++ b/js/src/jsapi-tests/testJSEvaluateScript.cpp @@ -17,9 +17,8 @@ BEGIN_TEST(testJSEvaluateScript) JS::CompileOptions opts(cx); JS::AutoObjectVector scopeChain(cx); CHECK(scopeChain.append(obj)); - JS::SourceBufferHolder srcBuf(src, ArrayLength(src) - 1, JS::SourceBufferHolder::NoOwnership); CHECK(JS::Evaluate(cx, scopeChain, opts.setFileAndLine(__FILE__, __LINE__), - srcBuf, &retval)); + src, ArrayLength(src) - 1, &retval)); bool hasProp = true; CHECK(JS_AlreadyHasOwnProperty(cx, obj, "x", &hasProp)); diff --git a/js/src/jsapi-tests/testMutedErrors.cpp b/js/src/jsapi-tests/testMutedErrors.cpp index ffa75398db2e..3442dc018185 100644 --- a/js/src/jsapi-tests/testMutedErrors.cpp +++ b/js/src/jsapi-tests/testMutedErrors.cpp @@ -54,8 +54,7 @@ eval(const char* asciiChars, bool mutedErrors, JS::MutableHandleValue rval) options.setMutedErrors(mutedErrors) .setFileAndLine("", 0); - JS::SourceBufferHolder srcBuf(chars.get(), len, JS::SourceBufferHolder::NoOwnership); - return JS::Evaluate(cx, options, srcBuf, rval); + return JS::Evaluate(cx, options, chars.get(), len, rval); } bool diff --git a/js/src/jsapi-tests/testScriptObject.cpp b/js/src/jsapi-tests/testScriptObject.cpp index a71b2d230694..fa1853344633 100644 --- a/js/src/jsapi-tests/testScriptObject.cpp +++ b/js/src/jsapi-tests/testScriptObject.cpp @@ -72,8 +72,7 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) JS::CompileOptions options(cx); options.setFileAndLine(__FILE__, __LINE__); JS::RootedScript script(cx); - JS::SourceBufferHolder srcBuf(uc_code, code_size, JS::SourceBufferHolder::NoOwnership); - CHECK(JS_CompileUCScript(cx, srcBuf, options, &script)); + CHECK(JS_CompileUCScript(cx, uc_code, code_size, options, &script)); return tryScript(script); } END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript) @@ -83,8 +82,7 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) JS::CompileOptions options(cx); options.setFileAndLine(__FILE__, __LINE__); JS::RootedScript script(cx); - JS::SourceBufferHolder srcBuf(uc_code, 0, JS::SourceBufferHolder::NoOwnership); - CHECK(JS_CompileUCScript(cx, srcBuf, options, &script)); + CHECK(JS_CompileUCScript(cx, uc_code, 0, options, &script)); return tryScript(script); } END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScript_empty) @@ -94,8 +92,7 @@ BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipal JS::CompileOptions options(cx); options.setFileAndLine(__FILE__, __LINE__); JS::RootedScript script(cx); - JS::SourceBufferHolder srcBuf(uc_code, code_size, JS::SourceBufferHolder::NoOwnership); - CHECK(JS_CompileUCScript(cx, srcBuf, options, &script)); + CHECK(JS_CompileUCScript(cx, uc_code, code_size, options, &script)); return tryScript(script); } END_FIXTURE_TEST(ScriptObjectFixture, bug438633_JS_CompileUCScriptForPrincipals) diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index a6a4b616e4e0..f5e6a47fd9c5 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -3995,20 +3995,27 @@ Compile(JSContext* cx, const ReadOnlyCompileOptions& options, return !!script; } +static bool +Compile(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, MutableHandleScript script) +{ + SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership); + return ::Compile(cx, options, srcBuf, script); +} + static bool Compile(JSContext* cx, const ReadOnlyCompileOptions& options, const char* bytes, size_t length, MutableHandleScript script) { - char16_t* chars; + UniqueTwoByteChars chars; if (options.utf8) - chars = UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get(); + chars.reset(UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get()); else - chars = InflateString(cx, bytes, length); + chars.reset(InflateString(cx, bytes, length)); if (!chars) return false; - SourceBufferHolder source(chars, length, SourceBufferHolder::GiveOwnership); - return ::Compile(cx, options, source, script); + return ::Compile(cx, options, chars.get(), length, script); } static bool @@ -4048,6 +4055,13 @@ JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options, return ::Compile(cx, options, bytes, length, script); } +bool +JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, JS::MutableHandleScript script) +{ + return ::Compile(cx, options, chars, length, script); +} + bool JS::Compile(JSContext* cx, const ReadOnlyCompileOptions& options, FILE* file, JS::MutableHandleScript script) @@ -4080,6 +4094,16 @@ JS::CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& opt return ::Compile(cx, options, bytes, length, script); } +bool +JS::CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& optionsArg, + const char16_t* chars, size_t length, + JS::MutableHandleScript script) +{ + CompileOptions options(cx, optionsArg); + options.setNonSyntacticScope(true); + return ::Compile(cx, options, chars, length, script); +} + bool JS::CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& optionsArg, FILE* file, JS::MutableHandleScript script) @@ -4201,11 +4225,11 @@ JS::CanDecodeBinASTOffThread(JSContext* cx, const ReadOnlyCompileOptions& option JS_PUBLIC_API(bool) JS::CompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, OffThreadCompileCallback callback, void* callbackData) { - MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length())); - return StartOffThreadParseScript(cx, options, srcBuf, callback, callbackData); + MOZ_ASSERT(CanCompileOffThread(cx, options, length)); + return StartOffThreadParseScript(cx, options, chars, length, callback, callbackData); } JS_PUBLIC_API(JSScript*) @@ -4226,11 +4250,11 @@ JS::CancelOffThreadScript(JSContext* cx, JS::OffThreadToken* token) JS_PUBLIC_API(bool) JS::CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, OffThreadCompileCallback callback, void* callbackData) { - MOZ_ASSERT(CanCompileOffThread(cx, options, srcBuf.length())); - return StartOffThreadParseModule(cx, options, srcBuf, callback, callbackData); + MOZ_ASSERT(CanCompileOffThread(cx, options, length)); + return StartOffThreadParseModule(cx, options, chars, length, callback, callbackData); } JS_PUBLIC_API(JSObject*) @@ -4323,10 +4347,10 @@ JS_CompileScript(JSContext* cx, const char* ascii, size_t length, } JS_PUBLIC_API(bool) -JS_CompileUCScript(JSContext* cx, JS::SourceBufferHolder& srcBuf, +JS_CompileUCScript(JSContext* cx, const char16_t* chars, size_t length, const JS::CompileOptions& options, MutableHandleScript script) { - return ::Compile(cx, options, srcBuf, script); + return ::Compile(cx, options, chars, length, script); } JS_PUBLIC_API(bool) @@ -4543,23 +4567,33 @@ JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain, scope, fun); } +JS_PUBLIC_API(bool) +JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain, + const ReadOnlyCompileOptions& options, + const char* name, unsigned nargs, const char* const* argnames, + const char16_t* chars, size_t length, MutableHandleFunction fun) +{ + SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership); + return CompileFunction(cx, envChain, options, name, nargs, argnames, + srcBuf, fun); +} + JS_PUBLIC_API(bool) JS::CompileFunction(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& options, const char* name, unsigned nargs, const char* const* argnames, const char* bytes, size_t length, MutableHandleFunction fun) { - char16_t* chars; + UniqueTwoByteChars chars; if (options.utf8) - chars = UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get(); + chars.reset(UTF8CharsToNewTwoByteCharsZ(cx, UTF8Chars(bytes, length), &length).get()); else - chars = InflateString(cx, bytes, length); + chars.reset(InflateString(cx, bytes, length)); if (!chars) return false; - SourceBufferHolder source(chars, length, SourceBufferHolder::GiveOwnership); return CompileFunction(cx, envChain, options, name, nargs, argnames, - source, fun); + chars.get(), length, fun); } JS_PUBLIC_API(bool) @@ -4738,6 +4772,15 @@ Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions return ::Evaluate(cx, scope->kind(), env, optionsArg, srcBuf, rval); } +static bool +Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg, + const char16_t* chars, size_t length, MutableHandleValue rval) +{ + SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership); + RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment()); + return ::Evaluate(cx, ScopeKind::Global, globalLexical, optionsArg, srcBuf, rval); +} + extern JS_PUBLIC_API(bool) JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options, const char* bytes, size_t length, MutableHandleValue rval) @@ -4787,6 +4830,21 @@ JS::Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOpt return ::Evaluate(cx, envChain, optionsArg, srcBuf, rval); } +JS_PUBLIC_API(bool) +JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg, + const char16_t* chars, size_t length, MutableHandleValue rval) +{ + return ::Evaluate(cx, optionsArg, chars, length, rval); +} + +JS_PUBLIC_API(bool) +JS::Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& optionsArg, + const char16_t* chars, size_t length, MutableHandleValue rval) +{ + SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership); + return ::Evaluate(cx, envChain, optionsArg, srcBuf, rval); +} + JS_PUBLIC_API(bool) JS::Evaluate(JSContext* cx, const ReadOnlyCompileOptions& optionsArg, const char* filename, MutableHandleValue rval) diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 0cfb834fc1f0..7e44e65e236b 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -378,7 +378,7 @@ namespace JS { * JS::SourceBufferHolder srcBuf(chars, length, JS::SourceBufferHolder::GiveOwnership); * JS::Compile(cx, options, srcBuf); */ -class SourceBufferHolder final +class MOZ_STACK_CLASS SourceBufferHolder final { public: enum Ownership { @@ -391,15 +391,14 @@ class SourceBufferHolder final length_(dataLength), ownsChars_(ownership == GiveOwnership) { - fixEmptyBuffer(); - } - - SourceBufferHolder(UniqueTwoByteChars&& data, size_t dataLength) - : data_(data.release()), - length_(dataLength), - ownsChars_(true) - { - 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; + } } SourceBufferHolder(SourceBufferHolder&& other) @@ -449,17 +448,6 @@ 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_; @@ -1316,24 +1304,6 @@ JS_freeop(JSFreeOp* fop, void* p); extern JS_PUBLIC_API(void) JS_updateMallocCounter(JSContext* cx, size_t nbytes); -/* - * A replacement for MallocAllocPolicy that allocates in the JS heap and adds no - * extra behaviours. - * - * This is currently used for allocating source buffers for parsing. Since these - * are temporary and will not be freed by GC, the memory is not tracked by the - * usual accounting. - */ -class JS_PUBLIC_API(JSMallocAllocPolicy) : public js::AllocPolicyBase -{ -public: - void reportAllocOverflow() const {} - - MOZ_MUST_USE bool checkSimulatedOOM() const { - return true; - } -}; - /** * Set the size of the native stack that should not be exceed. To disable * stack size checking pass 0. @@ -3203,7 +3173,7 @@ JS_CompileScript(JSContext* cx, const char* ascii, size_t length, * |script| will always be set. On failure, it will be set to nullptr. */ extern JS_PUBLIC_API(bool) -JS_CompileUCScript(JSContext* cx, JS::SourceBufferHolder& srcBuf, +JS_CompileUCScript(JSContext* cx, const char16_t* chars, size_t length, const JS::CompileOptions& options, JS::MutableHandleScript script); @@ -3616,6 +3586,10 @@ extern JS_PUBLIC_API(bool) Compile(JSContext* cx, const ReadOnlyCompileOptions& options, const char* bytes, size_t length, JS::MutableHandleScript script); +extern JS_PUBLIC_API(bool) +Compile(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, JS::MutableHandleScript script); + extern JS_PUBLIC_API(bool) Compile(JSContext* cx, const ReadOnlyCompileOptions& options, FILE* file, JS::MutableHandleScript script); @@ -3632,6 +3606,10 @@ extern JS_PUBLIC_API(bool) CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options, const char* bytes, size_t length, JS::MutableHandleScript script); +extern JS_PUBLIC_API(bool) +CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, JS::MutableHandleScript script); + extern JS_PUBLIC_API(bool) CompileForNonSyntacticScope(JSContext* cx, const ReadOnlyCompileOptions& options, FILE* file, JS::MutableHandleScript script); @@ -3665,7 +3643,7 @@ CanDecodeOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, size_t extern JS_PUBLIC_API(bool) CompileOffThread(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, OffThreadCompileCallback callback, void* callbackData); extern JS_PUBLIC_API(JSScript*) @@ -3676,7 +3654,7 @@ CancelOffThreadScript(JSContext* cx, OffThreadToken* token); extern JS_PUBLIC_API(bool) CompileOffThreadModule(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, OffThreadCompileCallback callback, void* callbackData); extern JS_PUBLIC_API(JSObject*) @@ -3721,6 +3699,15 @@ CancelMultiOffThreadScriptsDecoder(JSContext* cx, OffThreadToken* token); * global must not be explicitly included in the scope chain. */ extern JS_PUBLIC_API(bool) +CompileFunction(JSContext* cx, AutoObjectVector& envChain, + const ReadOnlyCompileOptions& options, + const char* name, unsigned nargs, const char* const* argnames, + const char16_t* chars, size_t length, JS::MutableHandleFunction fun); + +/** + * Same as above, but taking a SourceBufferHolder for the function body. + */ +extern JS_PUBLIC_API(bool) CompileFunction(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& options, const char* name, unsigned nargs, const char* const* argnames, @@ -3837,6 +3824,22 @@ extern JS_PUBLIC_API(bool) Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& options, SourceBufferHolder& srcBuf, JS::MutableHandleValue rval); +/** + * Evaluate the given character buffer in the scope of the current global of cx. + */ +extern JS_PUBLIC_API(bool) +Evaluate(JSContext* cx, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, JS::MutableHandleValue rval); + +/** + * As above, but providing an explicit scope chain. envChain must not include + * the global object on it; that's implicit. It needs to contain the other + * objects that should end up on the script's scope chain. + */ +extern JS_PUBLIC_API(bool) +Evaluate(JSContext* cx, AutoObjectVector& envChain, const ReadOnlyCompileOptions& options, + const char16_t* chars, size_t length, JS::MutableHandleValue rval); + /** * Evaluate the given byte buffer in the scope of the current global of cx. */ diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index e951b28e8439..783916b9b7ec 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -1986,12 +1986,10 @@ Evaluate(JSContext* cx, unsigned argc, Value* vp) return false; } else { mozilla::Range chars = codeChars.twoByteRange(); - JS::SourceBufferHolder srcBuf(chars.begin().get(), chars.length(), - JS::SourceBufferHolder::NoOwnership); if (envChain.length() == 0) { - (void) JS::Compile(cx, options, srcBuf, &script); + (void) JS::Compile(cx, options, chars.begin().get(), chars.length(), &script); } else { - (void) JS::CompileForNonSyntacticScope(cx, options, srcBuf, &script); + (void) JS::CompileForNonSyntacticScope(cx, options, chars.begin().get(), chars.length(), &script); } } @@ -2190,8 +2188,8 @@ Run(JSContext* cx, unsigned argc, Value* vp) if (!chars.initTwoByte(cx, str)) return false; - JS::SourceBufferHolder srcBuf(chars.twoByteRange().begin().get(), str->length(), - JS::SourceBufferHolder::NoOwnership); + const char16_t* ucbuf = chars.twoByteRange().begin().get(); + size_t buflen = str->length(); RootedScript script(cx); int64_t startClock = PRMJ_Now(); @@ -2206,7 +2204,7 @@ Run(JSContext* cx, unsigned argc, Value* vp) .setFileAndLine(filename.ptr(), 1) .setIsRunOnce(true) .setNoScriptRval(true); - if (!JS_CompileUCScript(cx, srcBuf, options, &script)) + if (!JS_CompileUCScript(cx, ucbuf, buflen, options, &script)) return false; } @@ -3534,8 +3532,7 @@ EvalInContext(JSContext* cx, unsigned argc, Value* vp) } JS::CompileOptions opts(cx); opts.setFileAndLine(filename.get(), lineno); - JS::SourceBufferHolder srcBuf(src, srclen, JS::SourceBufferHolder::NoOwnership); - if (!JS::Evaluate(cx, opts, srcBuf, args.rval())) { + if (!JS::Evaluate(cx, opts, src, srclen, args.rval())) { return false; } } @@ -3638,9 +3635,7 @@ WorkerMain(WorkerInput* input) AutoReportException are(cx); RootedScript script(cx); - JS::SourceBufferHolder srcBuf(input->chars.get(), input->length, - JS::SourceBufferHolder::NoOwnership); - if (!JS::Compile(cx, options, srcBuf, &script)) + if (!JS::Compile(cx, options, input->chars.get(), input->length, &script)) break; RootedValue result(cx); JS_ExecuteScript(cx, script, &result); @@ -4226,10 +4221,8 @@ Compile(JSContext* cx, unsigned argc, Value* vp) .setIsRunOnce(true) .setNoScriptRval(true); RootedScript script(cx); - JS::SourceBufferHolder srcBuf(stableChars.twoByteRange().begin().get(), - scriptContents->length(), - JS::SourceBufferHolder::NoOwnership); - bool ok = JS_CompileUCScript(cx, srcBuf, options, &script); + const char16_t* chars = stableChars.twoByteRange().begin().get(); + bool ok = JS_CompileUCScript(cx, chars, scriptContents->length(), options, &script); args.rval().setUndefined(); return ok; } @@ -4726,9 +4719,7 @@ OffThreadCompileScript(JSContext* cx, unsigned argc, Value* vp) if (!job) return false; - JS::SourceBufferHolder srcBuf(job->sourceChars(), length, - JS::SourceBufferHolder::NoOwnership); - if (!JS::CompileOffThread(cx, options, srcBuf, + if (!JS::CompileOffThread(cx, options, job->sourceChars(), length, OffThreadCompileScriptCallback, job)) { job->cancel(); @@ -4814,9 +4805,7 @@ OffThreadCompileModule(JSContext* cx, unsigned argc, Value* vp) if (!job) return false; - JS::SourceBufferHolder srcBuf(job->sourceChars(), length, - JS::SourceBufferHolder::NoOwnership); - if (!JS::CompileOffThreadModule(cx, options, srcBuf, + if (!JS::CompileOffThreadModule(cx, options, job->sourceChars(), length, OffThreadCompileScriptCallback, job)) { job->cancel(); @@ -6680,16 +6669,15 @@ EntryPoints(JSContext* cx, unsigned argc, Value* vp) AutoStableStringChars stableChars(cx); if (!stableChars.initTwoByte(cx, codeString)) return false; - JS::SourceBufferHolder srcBuf(stableChars.twoByteRange().begin().get(), - codeString->length(), - JS::SourceBufferHolder::NoOwnership); + const char16_t* chars = stableChars.twoByteRange().begin().get(); + size_t length = codeString->length(); CompileOptions options(cx); options.setIntroductionType("entryPoint eval") .setFileAndLine("entryPoint eval", 1); js::shell::ShellAutoEntryMonitor sarep(cx); - if (!JS::Evaluate(cx, options, srcBuf, &dummy)) + if (!JS::Evaluate(cx, options, chars, length, &dummy)) return false; return sarep.buildResult(cx, args.rval()); } diff --git a/js/src/vm/HelperThreads.cpp b/js/src/vm/HelperThreads.cpp index 28ae523edaaa..6c837c4aca3a 100644 --- a/js/src/vm/HelperThreads.cpp +++ b/js/src/vm/HelperThreads.cpp @@ -482,21 +482,22 @@ ParseTask::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const errors.sizeOfExcludingThis(mallocSizeOf); } -ScriptParseTask::ScriptParseTask(JSContext* cx, JS::SourceBufferHolder& srcBuf, +ScriptParseTask::ScriptParseTask(JSContext* cx, const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData) : ParseTask(ParseTaskKind::Script, cx, callback, callbackData), - data(std::move(srcBuf)) + data(TwoByteChars(chars, length)) {} void ScriptParseTask::parse(JSContext* cx) { + SourceBufferHolder srcBuf(data.begin().get(), data.length(), SourceBufferHolder::NoOwnership); Rooted sourceObject(cx); ScopeKind scopeKind = options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global; JSScript* script = frontend::CompileGlobalScript(cx, alloc, scopeKind, - options, data, + options, srcBuf, /* sourceObjectOut = */ &sourceObject.get()); if (script) scripts.infallibleAppend(script); @@ -504,18 +505,19 @@ ScriptParseTask::parse(JSContext* cx) sourceObjects.infallibleAppend(sourceObject); } -ModuleParseTask::ModuleParseTask(JSContext* cx, JS::SourceBufferHolder& srcBuf, +ModuleParseTask::ModuleParseTask(JSContext* cx, const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData) : ParseTask(ParseTaskKind::Module, cx, callback, callbackData), - data(std::move(srcBuf)) + data(TwoByteChars(chars, length)) {} void ModuleParseTask::parse(JSContext* cx) { + SourceBufferHolder srcBuf(data.begin().get(), data.length(), SourceBufferHolder::NoOwnership); Rooted sourceObject(cx); - ModuleObject* module = frontend::CompileModule(cx, options, data, alloc, &sourceObject.get()); + ModuleObject* module = frontend::CompileModule(cx, options, srcBuf, alloc, &sourceObject.get()); if (module) { scripts.infallibleAppend(module->script()); if (sourceObject) @@ -823,10 +825,10 @@ StartOffThreadParseTask(JSContext* cx, ParseTask* task, const ReadOnlyCompileOpt bool js::StartOffThreadParseScript(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData) { - auto task = cx->make_unique(cx, srcBuf, callback, callbackData); + auto task = cx->make_unique(cx, chars, length, callback, callbackData); if (!task || !StartOffThreadParseTask(cx, task.get(), options)) return false; @@ -836,10 +838,10 @@ js::StartOffThreadParseScript(JSContext* cx, const ReadOnlyCompileOptions& optio bool js::StartOffThreadParseModule(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData) { - auto task = cx->make_unique(cx, srcBuf, callback, callbackData); + auto task = cx->make_unique(cx, chars, length, callback, callbackData); if (!task || !StartOffThreadParseTask(cx, task.get(), options)) return false; diff --git a/js/src/vm/HelperThreads.h b/js/src/vm/HelperThreads.h index 66f4fa7e1112..df19fd399fee 100644 --- a/js/src/vm/HelperThreads.h +++ b/js/src/vm/HelperThreads.h @@ -589,12 +589,12 @@ CancelOffThreadParses(JSRuntime* runtime); */ bool StartOffThreadParseScript(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData); bool StartOffThreadParseModule(JSContext* cx, const ReadOnlyCompileOptions& options, - JS::SourceBufferHolder& srcBuf, + const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData); bool @@ -720,18 +720,18 @@ struct ParseTask : public mozilla::LinkedListElement, public JS::OffT struct ScriptParseTask : public ParseTask { - JS::SourceBufferHolder data; + JS::TwoByteChars data; - ScriptParseTask(JSContext* cx, JS::SourceBufferHolder& srcBuf, + ScriptParseTask(JSContext* cx, const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData); void parse(JSContext* cx) override; }; struct ModuleParseTask : public ParseTask { - JS::SourceBufferHolder data; + JS::TwoByteChars data; - ModuleParseTask(JSContext* cx, JS::SourceBufferHolder& srcBuf, + ModuleParseTask(JSContext* cx, const char16_t* chars, size_t length, JS::OffThreadCompileCallback callback, void* callbackData); void parse(JSContext* cx) override; }; diff --git a/js/xpconnect/loader/ChromeScriptLoader.cpp b/js/xpconnect/loader/ChromeScriptLoader.cpp index 8ff24e53c373..6fa5cbdde586 100644 --- a/js/xpconnect/loader/ChromeScriptLoader.cpp +++ b/js/xpconnect/loader/ChromeScriptLoader.cpp @@ -131,9 +131,8 @@ AsyncScriptCompiler::StartCompile(JSContext* aCx) { Rooted global(aCx, mGlobalObject->GetGlobalJSObject()); - JS::SourceBufferHolder srcBuf(std::move(mScriptText), mScriptLength); if (JS::CanCompileOffThread(aCx, mOptions, mScriptLength)) { - if (!JS::CompileOffThread(aCx, mOptions, srcBuf, + if (!JS::CompileOffThread(aCx, mOptions, mScriptText.get(), mScriptLength, OffThreadScriptLoaderCallback, static_cast(this))) { return false; @@ -144,7 +143,7 @@ AsyncScriptCompiler::StartCompile(JSContext* aCx) } Rooted script(aCx); - if (!JS::Compile(aCx, mOptions, srcBuf, &script)) { + if (!JS::Compile(aCx, mOptions, mScriptText.get(), mScriptLength, &script)) { return false; } diff --git a/js/xpconnect/src/Sandbox.cpp b/js/xpconnect/src/Sandbox.cpp index f7ace7da75dc..0681ef5c68bc 100644 --- a/js/xpconnect/src/Sandbox.cpp +++ b/js/xpconnect/src/Sandbox.cpp @@ -1848,9 +1848,8 @@ xpc::EvalInSandbox(JSContext* cx, HandleObject sandboxArg, const nsAString& sour JS::CompileOptions options(sandcx); options.setFileAndLine(filenameBuf.get(), lineNo); MOZ_ASSERT(JS_IsGlobalObject(sandbox)); - JS::SourceBufferHolder buffer(PromiseFlatString(source).get(), source.Length(), - JS::SourceBufferHolder::NoOwnership); - ok = JS::Evaluate(sandcx, options, buffer, &v); + ok = JS::Evaluate(sandcx, options, + PromiseFlatString(source).get(), source.Length(), &v); // If the sandbox threw an exception, grab it off the context. if (aes.HasException()) {