From a4f15f45034c7d37a48b35811ce580543e3dfd67 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Thu, 8 Aug 2013 16:51:35 -0700 Subject: [PATCH] Bug 901362 - Refactor nsJSUtils::EvaluateString to take an EvaluateOptions structure. r=gabor --- dom/base/nsJSEnvironment.cpp | 6 ++++-- dom/base/nsJSUtils.cpp | 15 ++++++++------- dom/base/nsJSUtils.h | 16 ++++++++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index bff86bf92b32..ea607c8ec331 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -1237,7 +1237,7 @@ nsJSContext::GetCCRefcnt() nsresult nsJSContext::EvaluateString(const nsAString& aScript, JS::Handle aScopeObject, - JS::CompileOptions& aOptions, + JS::CompileOptions& aCompileOptions, bool aCoerceToString, JS::Value* aRetValue) { @@ -1249,8 +1249,10 @@ nsJSContext::EvaluateString(const nsAString& aScript, { AutoCxPusher pusher(mContext); + nsJSUtils::EvaluateOptions evalOptions; + evalOptions.setCoerceToString(aCoerceToString); rv = nsJSUtils::EvaluateString(mContext, aScript, aScopeObject, - aOptions, aCoerceToString, aRetValue); + aCompileOptions, evalOptions, aRetValue); } // ScriptEvaluated needs to come after we pop the stack diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index 65902bdae91f..913aa0e31a6f 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -198,13 +198,14 @@ nsresult nsJSUtils::EvaluateString(JSContext* aCx, const nsAString& aScript, JS::Handle aScopeObject, - JS::CompileOptions& aOptions, - bool aCoerceToString, + JS::CompileOptions& aCompileOptions, + EvaluateOptions& aEvaluateOptions, JS::Value* aRetValue) { PROFILER_LABEL("JS", "EvaluateString"); - MOZ_ASSERT_IF(aOptions.versionSet, aOptions.version != JSVERSION_UNKNOWN); - MOZ_ASSERT_IF(aCoerceToString, aRetValue); + MOZ_ASSERT_IF(aCompileOptions.versionSet, + aCompileOptions.version != JSVERSION_UNKNOWN); + MOZ_ASSERT_IF(aEvaluateOptions.coerceToString, aRetValue); MOZ_ASSERT(aCx == nsContentUtils::GetCurrentJSContext()); // Unfortunately, the JS engine actually compiles scripts with a return value @@ -219,7 +220,7 @@ nsJSUtils::EvaluateString(JSContext* aCx, nsAutoMicroTask mt; JSPrincipals* p = JS_GetCompartmentPrincipals(js::GetObjectCompartment(aScopeObject)); - aOptions.setPrincipals(p); + aCompileOptions.setPrincipals(p); bool ok = false; nsresult rv = nsContentUtils::GetSecurityManager()-> @@ -233,10 +234,10 @@ nsJSUtils::EvaluateString(JSContext* aCx, JSAutoCompartment ac(aCx, aScopeObject); JS::RootedObject rootedScope(aCx, aScopeObject); - ok = JS::Evaluate(aCx, rootedScope, aOptions, + ok = JS::Evaluate(aCx, rootedScope, aCompileOptions, PromiseFlatString(aScript).get(), aScript.Length(), aRetValue); - if (ok && aCoerceToString && !aRetValue->isUndefined()) { + if (ok && aEvaluateOptions.coerceToString && !aRetValue->isUndefined()) { JSString* str = JS_ValueToString(aCx, *aRetValue); ok = !!str; *aRetValue = ok ? JS::StringValue(str) : JS::UndefinedValue(); diff --git a/dom/base/nsJSUtils.h b/dom/base/nsJSUtils.h index 29f90fcb3f03..4f4a41e061bf 100644 --- a/dom/base/nsJSUtils.h +++ b/dom/base/nsJSUtils.h @@ -64,11 +64,23 @@ public: const nsAString& aBody, JSObject** aFunctionObject); + struct EvaluateOptions { + bool coerceToString; + + explicit EvaluateOptions() : coerceToString(false) + {} + + EvaluateOptions& setCoerceToString(bool aCoerce) { + coerceToString = aCoerce; + return *this; + } + }; + static nsresult EvaluateString(JSContext* aCx, const nsAString& aScript, JS::Handle aScopeObject, - JS::CompileOptions &aOptions, - bool aCoerceToString, + JS::CompileOptions &aCompileOptions, + EvaluateOptions& aEvaluateOptions, JS::Value* aRetValue); };