Bug 824864 - Hoist the guts of CompileFunction into nsJSUtils. r=bz

This commit is contained in:
Bobby Holley 2013-01-16 18:50:25 -08:00
parent d668366180
commit baf74b5ce0
3 changed files with 54 additions and 27 deletions

View File

@ -1811,39 +1811,17 @@ nsJSContext::CompileFunction(JSObject* aTarget,
return NS_ERROR_ILLEGAL_VALUE;
}
xpc_UnmarkGrayObject(aTarget);
nsIScriptGlobalObject *global = GetGlobalObject();
nsCOMPtr<nsIPrincipal> principal;
if (global) {
// XXXbe why the two-step QI? speed up via a new GetGlobalObjectData func?
nsCOMPtr<nsIScriptObjectPrincipal> globalData = do_QueryInterface(global);
if (globalData) {
principal = globalData->GetPrincipal();
if (!principal)
return NS_ERROR_FAILURE;
}
}
JSAutoRequest ar(mContext);
JSAutoCompartment ac(mContext, aTarget);
js::RootedObject target(mContext, aShared ? NULL : aTarget);
XPCAutoRequest ar(mContext);
JS::CompileOptions options(mContext);
options.setPrincipals(nsJSPrincipals::get(principal))
.setVersion(JSVersion(aVersion))
options.setVersion(JSVersion(aVersion))
.setFileAndLine(aURL, aLineNo)
.setUserBit(aIsXBL);
JSFunction* fun = JS::CompileFunction(mContext, target,
options, PromiseFlatCString(aName).get(),
aArgCount, aArgArray,
PromiseFlatString(aBody).get(), aBody.Length());
if (!fun)
return NS_ERROR_FAILURE;
*aFunctionObject = JS_GetFunctionObject(fun);
return NS_OK;
return nsJSUtils::CompileFunction(mContext, target, options, aName, aArgCount,
aArgArray, aBody, aFunctionObject);
}
nsresult

View File

@ -25,6 +25,9 @@
#include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
#include "nsContentUtils.h"
#include "nsJSPrincipals.h"
#include "mozilla/dom/BindingUtils.h"
JSBool
@ -144,3 +147,39 @@ nsJSUtils::ReportPendingException(JSContext *aContext)
}
}
}
nsresult
nsJSUtils::CompileFunction(JSContext* aCx,
JS::HandleObject aTarget,
JS::CompileOptions& aOptions,
const nsACString& aName,
uint32_t aArgCount,
const char** aArgArray,
const nsAString& aBody,
JSObject** aFunctionObject)
{
MOZ_ASSERT(js::GetEnterCompartmentDepth(aCx) > 0);
MOZ_ASSERT(!aTarget || js::IsObjectInContextCompartment(aTarget, aCx));
MOZ_ASSERT_IF(aOptions.versionSet, aOptions.version != JSVERSION_UNKNOWN);
// Since aTarget and aCx are same-compartment, there should be no distinction
// between the object principal and the cx principal.
// However, aTarget may be null in the wacky aShared case. So use the cx.
JSPrincipals *p = JS_GetCompartmentPrincipals(js::GetContextCompartment(aCx));
aOptions.setPrincipals(p);
// Do the junk Gecko is supposed to do before calling into JSAPI.
xpc_UnmarkGrayObject(aTarget);
// Compile.
JSFunction* fun = JS::CompileFunction(aCx, aTarget, aOptions,
PromiseFlatCString(aName).get(),
aArgCount, aArgArray,
PromiseFlatString(aBody).get(),
aBody.Length());
if (!fun)
return NS_ERROR_FAILURE;
*aFunctionObject = JS_GetFunctionObject(fun);
return NS_OK;
}

View File

@ -54,6 +54,16 @@ public:
* case, the stack will be set aside before reporting the exception.
*/
static void ReportPendingException(JSContext *aContext);
static nsresult CompileFunction(JSContext* aCx,
JS::HandleObject aTarget,
JS::CompileOptions& aOptions,
const nsACString& aName,
uint32_t aArgCount,
const char** aArgArray,
const nsAString& aBody,
JSObject** aFunctionObject);
};