Bug 1047509 - Part 8: Add an AutoJSAPI into mozJSComponentLoader::ImportInto. r=bholley

This commit is contained in:
Bob Owen 2014-08-08 13:52:16 +01:00
parent 09d975cd3e
commit d2c8747c63

View File

@ -42,7 +42,6 @@
#include "xpcprivate.h" #include "xpcprivate.h"
#include "xpcpublic.h" #include "xpcpublic.h"
#include "nsContentUtils.h" #include "nsContentUtils.h"
#include "nsCxPusher.h"
#include "WrapperFactory.h" #include "WrapperFactory.h"
#include "mozilla/AddonPathService.h" #include "mozilla/AddonPathService.h"
@ -240,12 +239,8 @@ public:
void reportErrorAfterPop(char *buf); void reportErrorAfterPop(char *buf);
operator JSContext*() const {return mContext;}
private: private:
JSContext* mContext; JSContext* mContext;
nsCxPusher mPusher;
char* mBuf; char* mBuf;
// prevent copying and assignment // prevent copying and assignment
@ -1289,28 +1284,37 @@ mozJSComponentLoader::ImportInto(const nsACString &aLocation,
vp.set(mod->obj); vp.set(mod->obj);
if (targetObj) { if (targetObj) {
JSCLContextHelper cxhelper(mContext); // cxhelper must be created before jsapi, so that jsapi is detroyed and
JSAutoCompartment ac(mContext, mod->obj); // pops any context it has pushed before we report to the caller context.
JSCLContextHelper cxhelper(callercx);
RootedValue symbols(mContext); // Even though we are calling JS_SetPropertyById on targetObj, we want
RootedObject modObj(mContext, mod->obj); // to ensure that we never run script here, so we use an AutoJSAPI and
if (!JS_GetProperty(mContext, modObj, // not an AutoEntryScript.
dom::AutoJSAPI jsapi;
jsapi.Init();
JSContext* cx = jsapi.cx();
JSAutoCompartment ac(cx, mod->obj);
RootedValue symbols(cx);
RootedObject modObj(cx, mod->obj);
if (!JS_GetProperty(cx, modObj,
"EXPORTED_SYMBOLS", &symbols)) { "EXPORTED_SYMBOLS", &symbols)) {
return ReportOnCaller(cxhelper, ERROR_NOT_PRESENT, return ReportOnCaller(cxhelper, ERROR_NOT_PRESENT,
PromiseFlatCString(aLocation).get()); PromiseFlatCString(aLocation).get());
} }
if (!JS_IsArrayObject(mContext, symbols)) { if (!JS_IsArrayObject(cx, symbols)) {
return ReportOnCaller(cxhelper, ERROR_NOT_AN_ARRAY, return ReportOnCaller(cxhelper, ERROR_NOT_AN_ARRAY,
PromiseFlatCString(aLocation).get()); PromiseFlatCString(aLocation).get());
} }
RootedObject symbolsObj(mContext, &symbols.toObject()); RootedObject symbolsObj(cx, &symbols.toObject());
// Iterate over symbols array, installing symbols on targetObj: // Iterate over symbols array, installing symbols on targetObj:
uint32_t symbolCount = 0; uint32_t symbolCount = 0;
if (!JS_GetArrayLength(mContext, symbolsObj, &symbolCount)) { if (!JS_GetArrayLength(cx, symbolsObj, &symbolCount)) {
return ReportOnCaller(cxhelper, ERROR_GETTING_ARRAY_LENGTH, return ReportOnCaller(cxhelper, ERROR_GETTING_ARRAY_LENGTH,
PromiseFlatCString(aLocation).get()); PromiseFlatCString(aLocation).get());
} }
@ -1319,19 +1323,19 @@ mozJSComponentLoader::ImportInto(const nsACString &aLocation,
nsAutoCString logBuffer; nsAutoCString logBuffer;
#endif #endif
RootedValue value(mContext); RootedValue value(cx);
RootedId symbolId(mContext); RootedId symbolId(cx);
for (uint32_t i = 0; i < symbolCount; ++i) { for (uint32_t i = 0; i < symbolCount; ++i) {
if (!JS_GetElement(mContext, symbolsObj, i, &value) || if (!JS_GetElement(cx, symbolsObj, i, &value) ||
!value.isString() || !value.isString() ||
!JS_ValueToId(mContext, value, &symbolId)) { !JS_ValueToId(cx, value, &symbolId)) {
return ReportOnCaller(cxhelper, ERROR_ARRAY_ELEMENT, return ReportOnCaller(cxhelper, ERROR_ARRAY_ELEMENT,
PromiseFlatCString(aLocation).get(), i); PromiseFlatCString(aLocation).get(), i);
} }
RootedObject modObj(mContext, mod->obj); RootedObject modObj(cx, mod->obj);
if (!JS_GetPropertyById(mContext, modObj, symbolId, &value)) { if (!JS_GetPropertyById(cx, modObj, symbolId, &value)) {
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId)); JSAutoByteString bytes(cx, JSID_TO_STRING(symbolId));
if (!bytes) if (!bytes)
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
return ReportOnCaller(cxhelper, ERROR_GETTING_SYMBOL, return ReportOnCaller(cxhelper, ERROR_GETTING_SYMBOL,
@ -1339,11 +1343,11 @@ mozJSComponentLoader::ImportInto(const nsACString &aLocation,
bytes.ptr()); bytes.ptr());
} }
JSAutoCompartment target_ac(mContext, targetObj); JSAutoCompartment target_ac(cx, targetObj);
if (!JS_WrapValue(mContext, &value) || if (!JS_WrapValue(cx, &value) ||
!JS_SetPropertyById(mContext, targetObj, symbolId, value)) { !JS_SetPropertyById(cx, targetObj, symbolId, value)) {
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId)); JSAutoByteString bytes(cx, JSID_TO_STRING(symbolId));
if (!bytes) if (!bytes)
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
return ReportOnCaller(cxhelper, ERROR_SETTING_SYMBOL, return ReportOnCaller(cxhelper, ERROR_SETTING_SYMBOL,
@ -1354,7 +1358,7 @@ mozJSComponentLoader::ImportInto(const nsACString &aLocation,
if (i == 0) { if (i == 0) {
logBuffer.AssignLiteral("Installing symbols [ "); logBuffer.AssignLiteral("Installing symbols [ ");
} }
JSAutoByteString bytes(mContext, JSID_TO_STRING(symbolId)); JSAutoByteString bytes(cx, JSID_TO_STRING(symbolId));
if (!!bytes) if (!!bytes)
logBuffer.Append(bytes.ptr()); logBuffer.Append(bytes.ptr());
logBuffer.Append(' '); logBuffer.Append(' ');
@ -1441,20 +1445,12 @@ JSCLContextHelper::JSCLContextHelper(JSContext* aCx)
: mContext(aCx) : mContext(aCx)
, mBuf(nullptr) , mBuf(nullptr)
{ {
mPusher.Push(mContext);
JS_BeginRequest(mContext);
} }
JSCLContextHelper::~JSCLContextHelper() JSCLContextHelper::~JSCLContextHelper()
{ {
JS_EndRequest(mContext);
mPusher.Pop();
JSContext *restoredCx = nsContentUtils::GetCurrentJSContext();
if (restoredCx && mBuf) {
JS_ReportError(restoredCx, mBuf);
}
if (mBuf) { if (mBuf) {
JS_ReportError(mContext, mBuf);
JS_smprintf_free(mBuf); JS_smprintf_free(mBuf);
} }
} }