Backout Bug 810987.

This commit is contained in:
Kyle Huey 2012-11-13 10:27:37 -08:00
parent a43ac37b08
commit d77f59d91d
4 changed files with 41 additions and 70 deletions

View File

@ -431,7 +431,6 @@ mozJSComponentLoader::ReallyInit()
mModules.Init(32); mModules.Init(32);
mImports.Init(32); mImports.Init(32);
mInProgressImports.Init(32); mInProgressImports.Init(32);
mThisObjects.Init(32);
nsCOMPtr<nsIObserverService> obsSvc = nsCOMPtr<nsIObserverService> obsSvc =
do_GetService(kObserverServiceContractID, &rv); do_GetService(kObserverServiceContractID, &rv);
@ -579,52 +578,6 @@ mozJSComponentLoader::LoadModule(FileLocation &aFile)
return entry.forget(); return entry.forget();
} }
nsresult
mozJSComponentLoader::FindTargetObject(JSContext* aCx,
JSObject** aTargetObject)
{
JSObject* targetObject = nullptr;
*aTargetObject = nullptr;
if (mReuseLoaderGlobal) {
JSScript* script =
js::GetOutermostEnclosingFunctionOfScriptedCaller(aCx);
if (script) {
targetObject = mThisObjects.Get(script);
}
}
// The above could fail, even if mReuseLoaderGlobal, if the scripted
// caller is not a component/JSM (it could be a DOM scope, for
// instance).
if (!targetObject) {
// Our targetObject is the caller's global object. Let's get it.
nsresult rv;
nsCOMPtr<nsIXPConnect> xpc =
do_GetService(kXPConnectServiceContractID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAXPCNativeCallContext *cc = nullptr;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIXPConnectWrappedNative> wn;
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
NS_ENSURE_SUCCESS(rv, rv);
wn->GetJSObject(&targetObject);
if (!targetObject) {
NS_ERROR("null calling object");
return NS_ERROR_FAILURE;
}
targetObject = JS_GetGlobalForObject(aCx, targetObject);
}
*aTargetObject = targetObject;
return NS_OK;
}
// Some stack based classes for cleaning up on early return // Some stack based classes for cleaning up on early return
#ifdef HAVE_PR_MEMMAP #ifdef HAVE_PR_MEMMAP
class FileAutoCloser class FileAutoCloser
@ -1024,14 +977,6 @@ mozJSComponentLoader::ObjectForLocation(nsIFile *aComponentFile,
// See bug 384168. // See bug 384168.
*aObject = obj; *aObject = obj;
JSScript* tableScript = script;
if (!tableScript) {
tableScript = JS_GetFunctionScript(cx, function);
MOZ_ASSERT(tableScript);
}
mThisObjects.Put(tableScript, obj);
uint32_t oldopts = JS_GetOptions(cx); uint32_t oldopts = JS_GetOptions(cx);
JS_SetOptions(cx, oldopts | JS_SetOptions(cx, oldopts |
(exception ? JSOPTION_DONT_REPORT_UNCAUGHT : 0)); (exception ? JSOPTION_DONT_REPORT_UNCAUGHT : 0));
@ -1095,7 +1040,6 @@ mozJSComponentLoader::UnloadModules()
mInProgressImports.Clear(); mInProgressImports.Clear();
mImports.Clear(); mImports.Clear();
mThisObjects.Clear();
mModules.Enumerate(ClearModules, NULL); mModules.Enumerate(ClearModules, NULL);
@ -1145,8 +1089,28 @@ mozJSComponentLoader::Import(const nsACString& registryLocation,
PromiseFlatCString(registryLocation).get()); PromiseFlatCString(registryLocation).get());
} }
} else { } else {
nsresult rv = FindTargetObject(cx, &targetObject); // Our targetObject is the caller's global object. Find it by
// walking the calling object's parent chain.
nsresult rv;
nsCOMPtr<nsIXPConnect> xpc =
do_GetService(kXPConnectServiceContractID, &rv);
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
nsAXPCNativeCallContext *cc = nullptr;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIXPConnectWrappedNative> wn;
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
NS_ENSURE_SUCCESS(rv, rv);
wn->GetJSObject(&targetObject);
if (!targetObject) {
NS_ERROR("null calling object");
return NS_ERROR_FAILURE;
}
targetObject = JS_GetGlobalForObject(cx, targetObject);
} }
Maybe<JSAutoCompartment> ac; Maybe<JSAutoCompartment> ac;

View File

@ -50,10 +50,6 @@ class mozJSComponentLoader : public mozilla::ModuleLoader,
// ModuleLoader // ModuleLoader
const mozilla::Module* LoadModule(mozilla::FileLocation &aFile); const mozilla::Module* LoadModule(mozilla::FileLocation &aFile);
nsresult FindTargetObject(JSContext* aCx, JSObject** aTargetObject);
static mozJSComponentLoader* Get() { return sSelf; }
protected: protected:
static mozJSComponentLoader* sSelf; static mozJSComponentLoader* sSelf;
@ -140,7 +136,6 @@ class mozJSComponentLoader : public mozilla::ModuleLoader,
nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports; nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports;
nsDataHashtable<nsCStringHashKey, ModuleEntry*> mInProgressImports; nsDataHashtable<nsCStringHashKey, ModuleEntry*> mInProgressImports;
nsDataHashtable<nsPtrHashKey<JSScript>, JSObject*> mThisObjects;
bool mInitialized; bool mInitialized;
bool mReuseLoaderGlobal; bool mReuseLoaderGlobal;

View File

@ -6,7 +6,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozJSSubScriptLoader.h" #include "mozJSSubScriptLoader.h"
#include "mozJSComponentLoader.h"
#include "mozJSLoaderUtils.h" #include "mozJSLoaderUtils.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
@ -180,10 +179,23 @@ mozJSSubScriptLoader::LoadSubScript(const nsAString& url,
if (!targetObj) { if (!targetObj) {
// If the user didn't provide an object to eval onto, find one. // If the user didn't provide an object to eval onto, find the global
mozJSComponentLoader* loader = mozJSComponentLoader::Get(); // object by walking the parent chain of the calling object.
rv = loader->FindTargetObject(cx, &targetObj); nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(xpc, NS_ERROR_FAILURE);
nsAXPCNativeCallContext *cc = nullptr;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
nsCOMPtr<nsIXPConnectWrappedNative> wn;
rv = cc->GetCalleeWrapper(getter_AddRefs(wn));
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
rv = wn->GetJSObject(&targetObj);
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE);
targetObj = JS_GetGlobalForObject(cx, targetObj);
} }
// Remember an object out of the calling compartment so that we // Remember an object out of the calling compartment so that we

View File

@ -7,17 +7,17 @@
const Cc = Components.classes; const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
this.EXPORTED_SYMBOLS = ['ContentPrefInstance']; var EXPORTED_SYMBOLS = ['ContentPrefInstance'];
// This is a wrapper for nsIContentPrefService that alleviates the need to pass // This is a wrapper for nsIContentPrefService that alleviates the need to pass
// an nsILoadContext argument to every method. Pass the context to the constructor // an nsILoadContext argument to every method. Pass the context to the constructor
// instead and continue on your way in blissful ignorance. // instead and continue on your way in blissful ignorance.
this.ContentPrefInstance = function ContentPrefInstance(aContext) { function ContentPrefInstance(aContext) {
this._contentPrefSvc = Cc["@mozilla.org/content-pref/service;1"]. this._contentPrefSvc = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService); getService(Ci.nsIContentPrefService);
this._context = aContext; this._context = aContext;
}; }
ContentPrefInstance.prototype = { ContentPrefInstance.prototype = {
getPref: function ContentPrefInstance_init(aName, aGroup, aCallback) { getPref: function ContentPrefInstance_init(aName, aGroup, aCallback) {