2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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
|
|
|
|
|
|
|
#ifndef nsJSUtils_h__
|
|
|
|
#define nsJSUtils_h__
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-06-01 23:39:06 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
class nsIDOMEventListener;
|
|
|
|
class nsIScriptContext;
|
|
|
|
class nsIScriptGlobalObject;
|
2008-03-06 14:52:58 -08:00
|
|
|
class nsIPrincipal;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsJSUtils
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static JSBool 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-11-09 07:43:57 -08:00
|
|
|
static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-09 07:43:57 -08:00
|
|
|
static nsIScriptContext *GetStaticScriptContext(JSObject* aObj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static nsIScriptGlobalObject *GetDynamicScriptGlobal(JSContext *aContext);
|
|
|
|
|
|
|
|
static nsIScriptContext *GetDynamicScriptContext(JSContext *aContext);
|
2010-12-20 08:21:58 -08:00
|
|
|
|
|
|
|
/**
|
2011-08-24 13:44:35 -07:00
|
|
|
* Retrieve the inner window ID based on the given JSContext.
|
2010-12-20 08:21:58 -08:00
|
|
|
*
|
|
|
|
* @param JSContext aContext
|
2011-08-24 13:44:35 -07:00
|
|
|
* The JSContext from which you want to find the inner window ID.
|
2010-12-20 08:21:58 -08:00
|
|
|
*
|
2012-08-22 08:56:38 -07:00
|
|
|
* @returns uint64_t the inner window ID.
|
2010-12-20 08:21:58 -08:00
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext);
|
2012-11-09 07:43:57 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report a pending exception on aContext, if any. Note that this
|
|
|
|
* can be called when the context has a JS stack. If that's the
|
|
|
|
* case, the stack will be set aside before reporting the exception.
|
|
|
|
*/
|
|
|
|
static void ReportPendingException(JSContext *aContext);
|
2013-01-16 18:50:25 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class nsDependentJSString : public nsDependentString
|
|
|
|
{
|
|
|
|
public:
|
2010-12-03 00:24:17 -08:00
|
|
|
/**
|
|
|
|
* In the case of string ids, getting the string's chars is infallible, so
|
|
|
|
* the dependent string can be constructed directly.
|
|
|
|
*/
|
|
|
|
explicit nsDependentJSString(jsid id)
|
|
|
|
: nsDependentString(JS_GetInternedStringChars(JSID_TO_STRING(id)),
|
|
|
|
JS_GetStringLength(JSID_TO_STRING(id)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-01 23:39:06 -07:00
|
|
|
/**
|
|
|
|
* Ditto for flat strings.
|
|
|
|
*/
|
|
|
|
explicit nsDependentJSString(JSFlatString* fstr)
|
|
|
|
: nsDependentString(JS_GetFlatStringChars(fstr),
|
|
|
|
JS_GetStringLength(JS_FORGET_STRING_FLATNESS(fstr)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-03 00:24:17 -08:00
|
|
|
/**
|
|
|
|
* For all other strings, the nsDependentJSString object should be default
|
|
|
|
* constructed, which leaves it empty (this->IsEmpty()), and initialized with
|
2012-06-01 23:39:06 -07:00
|
|
|
* one of the init() methods below.
|
2010-12-03 00:24:17 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
nsDependentJSString()
|
2010-07-14 23:19:36 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-12-03 00:24:17 -08:00
|
|
|
JSBool init(JSContext* aContext, JSString* str)
|
2010-07-14 23:19:36 -07:00
|
|
|
{
|
2010-12-03 00:24:17 -08:00
|
|
|
size_t length;
|
|
|
|
const jschar* chars = JS_GetStringCharsZAndLength(aContext, str, &length);
|
|
|
|
if (!chars)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
NS_ASSERTION(IsEmpty(), "init() on initialized string");
|
|
|
|
nsDependentString* base = this;
|
|
|
|
new(base) nsDependentString(chars, length);
|
|
|
|
return JS_TRUE;
|
2010-07-14 23:19:36 -07:00
|
|
|
}
|
|
|
|
|
2010-12-03 00:24:17 -08:00
|
|
|
JSBool init(JSContext* aContext, const jsval &v)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-12-03 00:24:17 -08:00
|
|
|
return init(aContext, JSVAL_TO_STRING(v));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-06-01 23:39:06 -07:00
|
|
|
void init(JSFlatString* fstr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsEmpty(), "init() on initialized string");
|
|
|
|
new(this) nsDependentJSString(fstr);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsDependentJSString()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsJSUtils_h__ */
|