Bug 1702278 - Make CompileOptions a member of JSExecutionContext r=tcampbell

This simplified the use of JSExecutionContext, and make future patches
in this stack simpler.

Differential Revision: https://phabricator.services.mozilla.com/D110458
This commit is contained in:
Matthew Gaudet
2021-04-20 15:31:13 +00:00
parent 137349ecda
commit 3292860aa8
5 changed files with 34 additions and 40 deletions

View File

@@ -48,7 +48,8 @@ static nsresult EvaluationExceptionToNSResult(JSContext* aCx) {
}
JSExecutionContext::JSExecutionContext(JSContext* aCx,
JS::Handle<JSObject*> aGlobal)
JS::Handle<JSObject*> aGlobal,
JS::CompileOptions& aCompileOptions)
:
#ifdef MOZ_GECKO_PROFILER
mAutoProfilerLabel("JSExecutionContext",
@@ -59,6 +60,7 @@ JSExecutionContext::JSExecutionContext(JSContext* aCx,
mRealm(aCx, aGlobal),
mRetValue(aCx),
mScript(aCx),
mCompileOptions(aCompileOptions),
mRv(NS_OK),
mSkip(false),
mCoerceToString(false),
@@ -107,8 +109,7 @@ nsresult JSExecutionContext::JoinCompile(JS::OffThreadToken** aOffThreadToken) {
}
template <typename Unit>
nsresult JSExecutionContext::InternalCompile(
JS::CompileOptions& aCompileOptions, JS::SourceText<Unit>& aSrcBuf) {
nsresult JSExecutionContext::InternalCompile(JS::SourceText<Unit>& aSrcBuf) {
if (mSkip) {
return mRv;
}
@@ -116,16 +117,16 @@ nsresult JSExecutionContext::InternalCompile(
MOZ_ASSERT(aSrcBuf.get());
MOZ_ASSERT(mRetValue.isUndefined());
#ifdef DEBUG
mWantsReturnValue = !aCompileOptions.noScriptRval;
mWantsReturnValue = !mCompileOptions.noScriptRval;
#endif
MOZ_ASSERT(!mScript);
if (mEncodeBytecode) {
mScript =
JS::CompileAndStartIncrementalEncoding(mCx, aCompileOptions, aSrcBuf);
JS::CompileAndStartIncrementalEncoding(mCx, mCompileOptions, aSrcBuf);
} else {
mScript = JS::Compile(mCx, aCompileOptions, aSrcBuf);
mScript = JS::Compile(mCx, mCompileOptions, aSrcBuf);
}
if (!mScript) {
@@ -137,18 +138,15 @@ nsresult JSExecutionContext::InternalCompile(
return NS_OK;
}
nsresult JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<char16_t>& aSrcBuf) {
return InternalCompile(aCompileOptions, aSrcBuf);
nsresult JSExecutionContext::Compile(JS::SourceText<char16_t>& aSrcBuf) {
return InternalCompile(aSrcBuf);
}
nsresult JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<Utf8Unit>& aSrcBuf) {
return InternalCompile(aCompileOptions, aSrcBuf);
nsresult JSExecutionContext::Compile(JS::SourceText<Utf8Unit>& aSrcBuf) {
return InternalCompile(aSrcBuf);
}
nsresult JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
const nsAString& aScript) {
nsresult JSExecutionContext::Compile(const nsAString& aScript) {
if (mSkip) {
return mRv;
}
@@ -162,11 +160,10 @@ nsresult JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
return mRv;
}
return Compile(aCompileOptions, srcBuf);
return Compile(srcBuf);
}
nsresult JSExecutionContext::Decode(JS::CompileOptions& aCompileOptions,
mozilla::Vector<uint8_t>& aBytecodeBuf,
nsresult JSExecutionContext::Decode(mozilla::Vector<uint8_t>& aBytecodeBuf,
size_t aBytecodeIndex) {
if (mSkip) {
return mRv;
@@ -174,7 +171,7 @@ nsresult JSExecutionContext::Decode(JS::CompileOptions& aCompileOptions,
MOZ_ASSERT(!mWantsReturnValue);
JS::TranscodeResult tr = JS::DecodeScriptMaybeStencil(
mCx, aCompileOptions, aBytecodeBuf, &mScript, aBytecodeIndex);
mCx, mCompileOptions, aBytecodeBuf, &mScript, aBytecodeIndex);
// These errors are external parameters which should be handled before the
// decoding phase, and which are the only reasons why you might want to
// fallback on decoding failures.

View File

@@ -44,6 +44,9 @@ class MOZ_STACK_CLASS JSExecutionContext final {
// The compiled script.
JS::Rooted<JSScript*> mScript;
// The compilation options applied throughout
JS::CompileOptions& mCompileOptions;
// returned value forwarded when we have to interupt the execution eagerly
// with mSkip.
nsresult mRv;
@@ -68,13 +71,13 @@ class MOZ_STACK_CLASS JSExecutionContext final {
private:
// Compile a script contained in a SourceText.
template <typename Unit>
nsresult InternalCompile(JS::CompileOptions& aCompileOptions,
JS::SourceText<Unit>& aSrcBuf);
nsresult InternalCompile(JS::SourceText<Unit>& aSrcBuf);
public:
// Enter compartment in which the code would be executed. The JSContext
// must come from an AutoEntryScript.
JSExecutionContext(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
JSExecutionContext(JSContext* aCx, JS::Handle<JSObject*> aGlobal,
JS::CompileOptions& aCompileOptions);
JSExecutionContext(const JSExecutionContext&) = delete;
JSExecutionContext(JSExecutionContext&&) = delete;
@@ -110,18 +113,14 @@ class MOZ_STACK_CLASS JSExecutionContext final {
[[nodiscard]] nsresult JoinCompile(JS::OffThreadToken** aOffThreadToken);
// Compile a script contained in a SourceText.
nsresult Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<char16_t>& aSrcBuf);
nsresult Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<mozilla::Utf8Unit>& aSrcBuf);
nsresult Compile(JS::SourceText<char16_t>& aSrcBuf);
nsresult Compile(JS::SourceText<mozilla::Utf8Unit>& aSrcBuf);
// Compile a script contained in a string.
nsresult Compile(JS::CompileOptions& aCompileOptions,
const nsAString& aScript);
nsresult Compile(const nsAString& aScript);
// Decode a script contained in a buffer.
nsresult Decode(JS::CompileOptions& aCompileOptions,
mozilla::Vector<uint8_t>& aBytecodeBuf,
nsresult Decode(mozilla::Vector<uint8_t>& aBytecodeBuf,
size_t aBytecodeIndex);
// After getting a notification that an off-thread decoding terminated, this

View File

@@ -6046,8 +6046,8 @@ bool WindowScriptTimeoutHandler::Call(const char* aExecutionReason) {
options.setIntroductionType("domTimer");
JS::Rooted<JSObject*> global(aes.cx(), mGlobal->GetGlobalJSObject());
{
JSExecutionContext exec(aes.cx(), global);
nsresult rv = exec.Compile(options, mExpr);
JSExecutionContext exec(aes.cx(), global, options);
nsresult rv = exec.Compile(mExpr);
JS::Rooted<JSScript*> script(aes.cx(), exec.MaybeGetScript());
if (script) {

View File

@@ -303,9 +303,9 @@ nsresult nsJSThunk::EvaluateScript(
options.setFileAndLine(mURL.get(), 1);
options.setIntroductionType("javascriptURL");
{
JSExecutionContext exec(cx, globalJSObject);
JSExecutionContext exec(cx, globalJSObject, options);
exec.SetCoerceToString(true);
exec.Compile(options, NS_ConvertUTF8toUTF16(script));
exec.Compile(NS_ConvertUTF8toUTF16(script));
rv = exec.ExecScript(&v);
}

View File

@@ -3248,7 +3248,7 @@ nsresult ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) {
if (NS_SUCCEEDED(rv)) {
if (aRequest->IsBytecode()) {
TRACE_FOR_TEST(aRequest->GetScriptElement(), "scriptloader_execute");
JSExecutionContext exec(cx, global);
JSExecutionContext exec(cx, global, options);
if (aRequest->mOffThreadToken) {
LOG(("ScriptLoadRequest (%p): Decode Bytecode & Join and Execute",
aRequest));
@@ -3260,7 +3260,7 @@ nsresult ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) {
MarkerInnerWindowIdFromDocShell(docShell),
profilerLabelString);
rv = exec.Decode(options, aRequest->mScriptBytecode,
rv = exec.Decode(aRequest->mScriptBytecode,
aRequest->mBytecodeOffset);
}
@@ -3280,7 +3280,7 @@ nsresult ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) {
bool encodeBytecode = ShouldCacheBytecode(aRequest);
{
JSExecutionContext exec(cx, global);
JSExecutionContext exec(cx, global, options);
exec.SetEncodeBytecode(encodeBytecode);
TRACE_FOR_TEST(aRequest->GetScriptElement(),
"scriptloader_execute");
@@ -3306,10 +3306,8 @@ nsresult ScriptLoader::EvaluateScript(ScriptLoadRequest* aRequest) {
rv =
maybeSource.constructed<SourceText<char16_t>>()
? exec.Compile(options,
maybeSource.ref<SourceText<char16_t>>())
: exec.Compile(options,
maybeSource.ref<SourceText<Utf8Unit>>());
? exec.Compile(maybeSource.ref<SourceText<char16_t>>())
: exec.Compile(maybeSource.ref<SourceText<Utf8Unit>>());
}
}