2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2008-01-29 18:11:48 -08:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is not a generated file. It contains common utility functions
|
|
|
|
* invoked from the JavaScript code generated from IDL interfaces.
|
|
|
|
* The goal of the utility functions is to cut down on the size of
|
|
|
|
* the generated code itself.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsdbgapi.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2008-02-12 19:52:43 -08:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
|
|
|
|
|
2013-01-16 18:50:25 -08:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
|
2012-05-02 21:35:38 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2012-03-30 21:42:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
|
|
|
nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t* aLineno)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
JSScript* script = nullptr;
|
2012-04-16 12:30:00 -07:00
|
|
|
unsigned lineno = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-16 12:30:00 -07:00
|
|
|
if (!JS_DescribeScriptedCaller(aContext, &script, &lineno)) {
|
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-16 12:30:00 -07:00
|
|
|
*aFilename = ::JS_GetScriptFilename(aContext, script);
|
|
|
|
*aLineno = lineno;
|
|
|
|
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptGlobalObject *
|
2012-11-09 07:43:57 -08:00
|
|
|
nsJSUtils::GetStaticScriptGlobal(JSObject* aObj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSClass* clazz;
|
|
|
|
JSObject* glob = aObj; // starting point for search
|
|
|
|
|
|
|
|
if (!glob)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
glob = js::GetGlobalForObjectCrossCompartment(glob);
|
2011-09-28 17:57:27 -07:00
|
|
|
NS_ABORT_IF_FALSE(glob, "Infallible returns null");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-03 16:54:57 -08:00
|
|
|
clazz = JS_GetClass(glob);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-30 21:42:20 -07:00
|
|
|
// Whenever we end up with globals that are JSCLASS_IS_DOMJSCLASS
|
|
|
|
// and have an nsISupports DOM object, we will need to modify this
|
|
|
|
// check here.
|
|
|
|
MOZ_ASSERT(!(clazz->flags & JSCLASS_IS_DOMJSCLASS));
|
|
|
|
nsISupports* supports;
|
|
|
|
if (!(clazz->flags & JSCLASS_HAS_PRIVATE) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
!(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
|
2012-02-05 12:07:23 -08:00
|
|
|
!(supports = (nsISupports*)::JS_GetPrivate(glob))) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-12-21 17:41:23 -08:00
|
|
|
// We might either have a window directly (e.g. if the global is a
|
|
|
|
// sandbox whose script object principal pointer is a window), or an
|
|
|
|
// XPCWrappedNative for a window. We could also have other
|
|
|
|
// sandbox-related script object principals, but we can't do much
|
|
|
|
// about those short of trying to walk the proto chain of |glob|
|
|
|
|
// looking for a window or something.
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(supports));
|
|
|
|
if (!sgo) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
|
2013-03-21 18:50:44 -07:00
|
|
|
if (!wrapper) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-12-21 17:41:23 -08:00
|
|
|
sgo = do_QueryWrappedNative(wrapper);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We're returning a pointer to something that's about to be
|
|
|
|
// released, but that's ok here.
|
|
|
|
return sgo;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *
|
2012-11-09 07:43:57 -08:00
|
|
|
nsJSUtils::GetStaticScriptContext(JSObject* aObj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-09 07:43:57 -08:00
|
|
|
nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nativeGlobal)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-23 10:13:29 -07:00
|
|
|
return nativeGlobal->GetScriptContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptGlobalObject *
|
|
|
|
nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
|
|
|
|
{
|
|
|
|
nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
|
|
|
|
if (!scriptCX)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return scriptCX->GetGlobalObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIScriptContext *
|
|
|
|
nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
|
|
|
|
{
|
|
|
|
return GetScriptContextFromJSContext(aContext);
|
|
|
|
}
|
2010-12-20 08:21:58 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t
|
2011-08-24 13:44:35 -07:00
|
|
|
nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext)
|
2010-12-20 08:21:58 -08:00
|
|
|
{
|
|
|
|
if (!aContext)
|
|
|
|
return 0;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t innerWindowID = 0;
|
2011-02-19 20:46:44 -08:00
|
|
|
|
|
|
|
JSObject *jsGlobal = JS_GetGlobalForScopeChain(aContext);
|
|
|
|
if (jsGlobal) {
|
2012-11-09 07:43:57 -08:00
|
|
|
nsIScriptGlobalObject *scriptGlobal = GetStaticScriptGlobal(jsGlobal);
|
2011-02-19 20:46:44 -08:00
|
|
|
if (scriptGlobal) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(scriptGlobal);
|
|
|
|
if (win)
|
2011-08-24 13:44:35 -07:00
|
|
|
innerWindowID = win->WindowID();
|
2011-02-19 20:46:44 -08:00
|
|
|
}
|
|
|
|
}
|
2010-12-20 08:21:58 -08:00
|
|
|
|
2011-08-24 13:44:35 -07:00
|
|
|
return innerWindowID;
|
2010-12-20 08:21:58 -08:00
|
|
|
}
|
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
void
|
|
|
|
nsJSUtils::ReportPendingException(JSContext *aContext)
|
|
|
|
{
|
|
|
|
if (JS_IsExceptionPending(aContext)) {
|
|
|
|
bool saved = JS_SaveFrameChain(aContext);
|
|
|
|
JS_ReportPendingException(aContext);
|
|
|
|
if (saved) {
|
|
|
|
JS_RestoreFrameChain(aContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 18:50:25 -08:00
|
|
|
|
|
|
|
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);
|
2013-01-16 18:50:27 -08:00
|
|
|
MOZ_ASSERT_IF(aTarget, js::IsObjectInContextCompartment(aTarget, aCx));
|
2013-01-16 18:50:25 -08:00
|
|
|
MOZ_ASSERT_IF(aOptions.versionSet, aOptions.version != JSVERSION_UNKNOWN);
|
2013-01-22 22:12:50 -08:00
|
|
|
mozilla::DebugOnly<nsIScriptContext*> ctx = GetScriptContextFromJSContext(aCx);
|
|
|
|
MOZ_ASSERT_IF(ctx, ctx->IsContextInitialized());
|
2013-01-16 18:50:25 -08:00
|
|
|
|
|
|
|
// 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.
|
2013-01-16 18:50:27 -08:00
|
|
|
JSPrincipals* p = JS_GetCompartmentPrincipals(js::GetContextCompartment(aCx));
|
2013-01-16 18:50:25 -08:00
|
|
|
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());
|
2013-01-22 22:12:50 -08:00
|
|
|
if (!fun) {
|
|
|
|
ReportPendingException(aCx);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-01-16 18:50:25 -08:00
|
|
|
|
|
|
|
*aFunctionObject = JS_GetFunctionObject(fun);
|
|
|
|
return NS_OK;
|
|
|
|
}
|