Bug 901362 - Refactor nsJSUtils::EvaluateString to take an EvaluateOptions structure. r=gabor

This commit is contained in:
Bobby Holley 2013-08-08 16:51:35 -07:00
Родитель d7fb701374
Коммит a4f15f4503
3 изменённых файлов: 26 добавлений и 11 удалений

Просмотреть файл

@ -1237,7 +1237,7 @@ nsJSContext::GetCCRefcnt()
nsresult
nsJSContext::EvaluateString(const nsAString& aScript,
JS::Handle<JSObject*> 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

Просмотреть файл

@ -198,13 +198,14 @@ nsresult
nsJSUtils::EvaluateString(JSContext* aCx,
const nsAString& aScript,
JS::Handle<JSObject*> 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();

Просмотреть файл

@ -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<JSObject*> aScopeObject,
JS::CompileOptions &aOptions,
bool aCoerceToString,
JS::CompileOptions &aCompileOptions,
EvaluateOptions& aEvaluateOptions,
JS::Value* aRetValue);
};