Bug 1724236 - Get encodeBytecode and coerceToString from function arguments instead of members r=arai

Differential Revision: https://phabricator.services.mozilla.com/D218443
This commit is contained in:
Bryan Thrall
2024-10-30 13:41:40 +00:00
parent b6fd76662b
commit 2a63e2261f
4 changed files with 44 additions and 54 deletions

View File

@@ -77,9 +77,7 @@ JSExecutionContext::JSExecutionContext(
mScript(aCx),
mDebuggerPrivateValue(aCx, aDebuggerPrivateValue),
mDebuggerIntroductionScript(aCx, aDebuggerIntroductionScript),
mSkip(false),
mCoerceToString(false),
mEncodeBytecode(false)
mSkip(false)
#ifdef DEBUG
,
mWantsReturnValue(false),
@@ -101,7 +99,8 @@ JSExecutionContext::JSExecutionContext(
void JSExecutionContext::JoinOffThread(JS::CompileOptions& aCompileOptions,
ScriptLoadContext* aContext,
ErrorResult& aRv) {
ErrorResult& aRv,
bool aEncodeBytecode /* = false */) {
MOZ_ASSERT(!mSkip);
MOZ_ASSERT(!mWantsReturnValue);
@@ -125,12 +124,13 @@ void JSExecutionContext::JoinOffThread(JS::CompileOptions& aCompileOptions,
bool unused;
InstantiateStencil(aCompileOptions, std::move(stencil), unused, aRv,
&storage);
aEncodeBytecode, &storage);
}
template <typename Unit>
void JSExecutionContext::InternalCompile(JS::CompileOptions& aCompileOptions,
JS::SourceText<Unit>& aSrcBuf,
bool aEncodeBytecode,
ErrorResult& aRv) {
MOZ_ASSERT(!mSkip);
@@ -158,23 +158,27 @@ void JSExecutionContext::InternalCompile(JS::CompileOptions& aCompileOptions,
}
bool unused;
InstantiateStencil(aCompileOptions, std::move(stencil), unused, aRv);
InstantiateStencil(aCompileOptions, std::move(stencil), unused, aRv,
aEncodeBytecode);
}
void JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<char16_t>& aSrcBuf,
ErrorResult& aRv) {
InternalCompile(aCompileOptions, aSrcBuf, aRv);
ErrorResult& aRv,
bool aEncodeBytecode /*= false */) {
InternalCompile(aCompileOptions, aSrcBuf, aEncodeBytecode, aRv);
}
void JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<Utf8Unit>& aSrcBuf,
ErrorResult& aRv) {
InternalCompile(aCompileOptions, aSrcBuf, aRv);
ErrorResult& aRv,
bool aEncodeBytecode /*= false */) {
InternalCompile(aCompileOptions, aSrcBuf, aEncodeBytecode, aRv);
}
void JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
const nsAString& aScript, ErrorResult& aRv) {
const nsAString& aScript, ErrorResult& aRv,
bool aEncodeBytecode /*= false */) {
MOZ_ASSERT(!mSkip);
const nsPromiseFlatString& flatScript = PromiseFlatString(aScript);
@@ -186,7 +190,7 @@ void JSExecutionContext::Compile(JS::CompileOptions& aCompileOptions,
return;
}
Compile(aCompileOptions, srcBuf, aRv);
Compile(aCompileOptions, srcBuf, aRv, aEncodeBytecode);
}
void JSExecutionContext::Decode(JS::CompileOptions& aCompileOptions,
@@ -227,7 +231,7 @@ void JSExecutionContext::Decode(JS::CompileOptions& aCompileOptions,
void JSExecutionContext::InstantiateStencil(
JS::CompileOptions& aCompileOptions, RefPtr<JS::Stencil>&& aStencil,
bool& incrementalEncodingAlreadyStarted, ErrorResult& aRv,
JS::InstantiationStorage* aStorage) {
bool aEncodeBytecode /* = false */, JS::InstantiationStorage* aStorage) {
JS::InstantiateOptions instantiateOptions(aCompileOptions);
JS::Rooted<JSScript*> script(
mCx, JS::InstantiateGlobalStencil(mCx, instantiateOptions, aStencil,
@@ -238,7 +242,7 @@ void JSExecutionContext::InstantiateStencil(
return;
}
if (mEncodeBytecode) {
if (aEncodeBytecode) {
if (!JS::StartIncrementalEncoding(mCx, std::move(aStencil),
incrementalEncodingAlreadyStarted)) {
mSkip = true;
@@ -297,7 +301,8 @@ static bool IsPromiseValue(JSContext* aCx, JS::Handle<JS::Value> aValue) {
}
void JSExecutionContext::ExecScript(JS::MutableHandle<JS::Value> aRetValue,
ErrorResult& aRv) {
ErrorResult& aRv,
bool aCoerceToString /* = false */) {
MOZ_ASSERT(!mSkip);
MOZ_ASSERT(mScript);
@@ -312,7 +317,7 @@ void JSExecutionContext::ExecScript(JS::MutableHandle<JS::Value> aRetValue,
#ifdef DEBUG
mWantsReturnValue = false;
#endif
if (mCoerceToString && IsPromiseValue(mCx, aRetValue)) {
if (aCoerceToString && IsPromiseValue(mCx, aRetValue)) {
// We're a javascript: url and we should treat Promise return values as
// undefined.
//
@@ -321,7 +326,7 @@ void JSExecutionContext::ExecScript(JS::MutableHandle<JS::Value> aRetValue,
aRetValue.setUndefined();
}
if (mCoerceToString && !aRetValue.isUndefined()) {
if (aCoerceToString && !aRetValue.isUndefined()) {
JSString* str = JS::ToString(mCx, aRetValue);
if (!str) {
// ToString can be a function call, so an exception can be raised while

View File

@@ -63,12 +63,6 @@ class MOZ_STACK_CLASS JSExecutionContext final {
// result is carried by mRv.
bool mSkip;
// Should the result be serialized before being returned.
bool mCoerceToString;
// Encode the bytecode before it is being executed.
bool mEncodeBytecode;
bool mKeepStencil = false;
#ifdef DEBUG
@@ -82,7 +76,8 @@ class MOZ_STACK_CLASS JSExecutionContext final {
// Compile a script contained in a SourceText.
template <typename Unit>
void InternalCompile(JS::CompileOptions& aCompileOptions,
JS::SourceText<Unit>& aSrcBuf, ErrorResult& aRv);
JS::SourceText<Unit>& aSrcBuf, bool aEncodeBytecode,
ErrorResult& aRv);
public:
// Enter compartment in which the code would be executed. The JSContext
@@ -111,37 +106,24 @@ class MOZ_STACK_CLASS JSExecutionContext final {
void SetKeepStencil() { mKeepStencil = true; }
already_AddRefed<JS::Stencil> StealStencil() { return mStencil.forget(); }
// The returned value would be converted to a string if the
// |aCoerceToString| is flag set.
JSExecutionContext& SetCoerceToString(bool aCoerceToString) {
mCoerceToString = aCoerceToString;
return *this;
}
// When set, this flag records and encodes the bytecode as soon as it is
// being compiled, and before it is being executed. The bytecode can then be
// requested by using |JS::FinishIncrementalEncoding| with the mutable
// handle |aScript| argument of |CompileAndExec| or |JoinAndExec|.
JSExecutionContext& SetEncodeBytecode(bool aEncodeBytecode) {
mEncodeBytecode = aEncodeBytecode;
return *this;
}
// After getting a notification that an off-thread compile/decode finished,
// this function will take the result of the off-thread operation and move it
// to the main thread.
void JoinOffThread(JS::CompileOptions& aCompileOptions,
ScriptLoadContext* aContext, ErrorResult& aRv);
ScriptLoadContext* aContext, ErrorResult& aRv,
bool aEncodeBytecode = false);
// Compile a script contained in a SourceText.
void Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<char16_t>& aSrcBuf, ErrorResult& aRv);
JS::SourceText<char16_t>& aSrcBuf, ErrorResult& aRv,
bool aEncodeBytecode = false);
void Compile(JS::CompileOptions& aCompileOptions,
JS::SourceText<mozilla::Utf8Unit>& aSrcBuf, ErrorResult& aRv);
JS::SourceText<mozilla::Utf8Unit>& aSrcBuf, ErrorResult& aRv,
bool aEncodeBytecode = false);
// Compile a script contained in a string.
void Compile(JS::CompileOptions& aCompileOptions, const nsAString& aScript,
ErrorResult& aRv);
ErrorResult& aRv, bool aEncodeBytecode = false);
// Decode a script contained in a buffer.
void Decode(JS::CompileOptions& aCompileOptions,
@@ -152,7 +134,7 @@ class MOZ_STACK_CLASS JSExecutionContext final {
void InstantiateStencil(JS::CompileOptions& aCompileOptions,
RefPtr<JS::Stencil>&& aStencil,
bool& incrementalEncodingAlreadyStarted,
ErrorResult& aRv,
ErrorResult& aRv, bool aEncodeBytecode = false,
JS::InstantiationStorage* aStorage = nullptr);
// Get a successfully compiled script.
@@ -174,7 +156,11 @@ class MOZ_STACK_CLASS JSExecutionContext final {
// compartment given as argument to the JSExecutionContext constructor. If the
// caller is in a different compartment, then the out-param value should be
// wrapped by calling |JS_WrapValue|.
void ExecScript(JS::MutableHandle<JS::Value> aRetValue, ErrorResult& aRv);
//
// The returned value will be converted to a string if the
// |aCoerceToString| is flag set.
void ExecScript(JS::MutableHandle<JS::Value> aRetValue, ErrorResult& aRv,
bool aCoerceToString = false);
};
} // namespace dom
} // namespace mozilla

View File

@@ -329,10 +329,9 @@ nsresult nsJSThunk::EvaluateScript(
mozilla::ErrorResult erv;
JSExecutionContext exec(cx, globalJSObject, options, erv);
if (!erv.Failed()) {
exec.SetCoerceToString(true);
exec.Compile(options, NS_ConvertUTF8toUTF16(script), erv);
if (!erv.Failed()) {
exec.ExecScript(&v, erv);
exec.ExecScript(&v, erv, /* aCoerceToString */ true);
}
}
rv = mozilla::dom::EvaluationExceptionToNSResult(erv);

View File

@@ -2751,7 +2751,7 @@ void ScriptLoader::InstantiateClassicScriptFromMaybeEncodedSource(
MOZ_ASSERT(aRequest->IsSource());
CalculateBytecodeCacheFlag(aRequest);
aExec.SetEncodeBytecode(aRequest->PassedConditionForBytecodeEncoding());
bool encodeBytecode = aRequest->PassedConditionForBytecodeEncoding();
if (aRequest->GetScriptLoadContext()->mCompileOrDecodeTask) {
// Off-main-thread parsing.
@@ -2760,7 +2760,8 @@ void ScriptLoader::InstantiateClassicScriptFromMaybeEncodedSource(
"Execute",
aRequest));
MOZ_ASSERT(aRequest->IsTextSource());
aExec.JoinOffThread(aCompileOptions, aRequest->GetScriptLoadContext(), aRv);
aExec.JoinOffThread(aCompileOptions, aRequest->GetScriptLoadContext(), aRv,
encodeBytecode);
} else {
// Main thread parsing (inline and small scripts)
LOG(("ScriptLoadRequest (%p): Compile And Exec", aRequest));
@@ -2774,7 +2775,7 @@ void ScriptLoader::InstantiateClassicScriptFromMaybeEncodedSource(
profilerLabelString);
auto compile = [&](auto& source) {
aExec.Compile(aCompileOptions, source, aRv);
aExec.Compile(aCompileOptions, source, aRv, encodeBytecode);
};
MOZ_ASSERT(!maybeSource.empty());
@@ -2795,11 +2796,10 @@ void ScriptLoader::InstantiateClassicScriptFromCachedStencil(
return;
}
aExec.SetEncodeBytecode(true);
bool incrementalEncodingAlreadyStarted = false;
aExec.InstantiateStencil(aCompileOptions, std::move(stencil),
incrementalEncodingAlreadyStarted, aRv);
incrementalEncodingAlreadyStarted, aRv,
/* aEncodeBytecode */ true);
if (incrementalEncodingAlreadyStarted) {
aRequest->MarkSkippedBytecodeEncoding();
}