2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
1998-12-03 00:33:00 +00:00
|
|
|
|
1998-12-22 22:03:20 +00:00
|
|
|
#ifndef nsJSUtils_h__
|
|
|
|
#define nsJSUtils_h__
|
|
|
|
|
1998-12-03 00:33:00 +00: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.
|
|
|
|
*/
|
|
|
|
|
2012-06-02 06:39:06 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
1998-12-03 00:33:00 +00:00
|
|
|
#include "jsapi.h"
|
2014-07-05 15:30:54 +00:00
|
|
|
#include "jsfriendapi.h"
|
2014-12-17 21:50:20 +00:00
|
|
|
#include "js/Conversions.h"
|
1998-12-03 00:33:00 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
|
1999-12-18 20:29:29 +00:00
|
|
|
class nsIScriptContext;
|
|
|
|
class nsIScriptGlobalObject;
|
1999-03-28 22:19:42 +00:00
|
|
|
|
2014-09-29 13:34:21 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class AutoJSAPI;
|
2014-10-30 21:40:09 +00:00
|
|
|
class Element;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2014-09-29 13:34:21 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
class nsJSUtils
|
|
|
|
{
|
1998-12-22 22:03:20 +00:00
|
|
|
public:
|
2015-01-05 21:53:27 +00:00
|
|
|
static bool GetCallingLocation(JSContext* aContext, nsACString& aFilename,
|
2015-09-02 00:56:03 +00:00
|
|
|
uint32_t* aLineno = nullptr,
|
|
|
|
uint32_t* aColumn = nullptr);
|
2015-01-05 21:53:27 +00:00
|
|
|
static bool GetCallingLocation(JSContext* aContext, nsAString& aFilename,
|
2015-09-02 00:56:03 +00:00
|
|
|
uint32_t* aLineno = nullptr,
|
|
|
|
uint32_t* aColumn = nullptr);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
|
2012-11-09 15:43:57 +00:00
|
|
|
static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
|
2012-11-09 15:43:57 +00:00
|
|
|
static nsIScriptContext *GetStaticScriptContext(JSObject* aObj);
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 17:42:36 +00:00
|
|
|
|
2010-12-20 16:21:58 +00:00
|
|
|
/**
|
2011-08-24 20:44:35 +00:00
|
|
|
* Retrieve the inner window ID based on the given JSContext.
|
2010-12-20 16:21:58 +00:00
|
|
|
*
|
|
|
|
* @param JSContext aContext
|
2011-08-24 20:44:35 +00:00
|
|
|
* The JSContext from which you want to find the inner window ID.
|
2010-12-20 16:21:58 +00:00
|
|
|
*
|
2012-08-22 15:56:38 +00:00
|
|
|
* @returns uint64_t the inner window ID.
|
2010-12-20 16:21:58 +00:00
|
|
|
*/
|
2012-08-22 15:56:38 +00:00
|
|
|
static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext);
|
2012-11-09 15:43:57 +00:00
|
|
|
|
2014-09-29 13:34:21 +00:00
|
|
|
static nsresult CompileFunction(mozilla::dom::AutoJSAPI& jsapi,
|
2014-10-25 04:50:29 +00:00
|
|
|
JS::AutoObjectVector& aScopeChain,
|
2013-01-17 02:50:25 +00:00
|
|
|
JS::CompileOptions& aOptions,
|
|
|
|
const nsACString& aName,
|
|
|
|
uint32_t aArgCount,
|
|
|
|
const char** aArgArray,
|
|
|
|
const nsAString& aBody,
|
|
|
|
JSObject** aFunctionObject);
|
|
|
|
|
2014-11-12 22:04:29 +00:00
|
|
|
struct MOZ_STACK_CLASS EvaluateOptions {
|
|
|
|
JS::AutoObjectVector scopeChain;
|
2013-08-08 23:51:35 +00:00
|
|
|
|
2014-11-12 22:04:29 +00:00
|
|
|
explicit EvaluateOptions(JSContext* cx)
|
2016-05-10 02:25:40 +00:00
|
|
|
: scopeChain(cx)
|
2013-08-08 23:51:35 +00:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2014-11-12 22:04:29 +00:00
|
|
|
// aEvaluationGlobal is the global to evaluate in. The return value
|
|
|
|
// will then be wrapped back into the compartment aCx is in when
|
2015-06-16 00:11:06 +00:00
|
|
|
// this function is called. For all the EvaluateString overloads,
|
|
|
|
// the JSContext must come from an AutoJSAPI that has had
|
|
|
|
// TakeOwnershipOfErrorReporting() called on it.
|
2013-08-08 23:51:34 +00:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
const nsAString& aScript,
|
2014-11-12 22:04:29 +00:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2013-08-08 23:51:35 +00:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
2014-04-01 10:34:39 +00:00
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
2014-11-18 16:01:08 +00:00
|
|
|
JS::MutableHandle<JS::Value> aRetValue);
|
2014-04-01 10:34:39 +00:00
|
|
|
|
2014-04-25 14:11:56 +00:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
2014-11-12 22:04:29 +00:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2014-04-25 14:11:56 +00:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
2014-11-18 16:01:08 +00:00
|
|
|
JS::MutableHandle<JS::Value> aRetValue);
|
2014-04-25 14:11:56 +00:00
|
|
|
|
2014-04-01 10:34:39 +00:00
|
|
|
|
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
const nsAString& aScript,
|
2014-11-12 22:04:29 +00:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2014-11-18 16:01:08 +00:00
|
|
|
JS::CompileOptions &aCompileOptions);
|
2013-08-08 23:51:34 +00:00
|
|
|
|
2014-04-25 14:11:56 +00:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
2014-11-12 22:04:29 +00:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2014-04-25 14:11:56 +00:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
2014-11-18 16:01:08 +00:00
|
|
|
void **aOffThreadToken);
|
2014-04-25 14:11:56 +00:00
|
|
|
|
2016-05-07 11:46:38 +00:00
|
|
|
static nsresult CompileModule(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
|
|
|
JS::CompileOptions &aCompileOptions,
|
|
|
|
JS::MutableHandle<JSObject*> aModule);
|
|
|
|
|
|
|
|
static nsresult ModuleDeclarationInstantiation(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aModule);
|
|
|
|
|
|
|
|
static nsresult ModuleEvaluation(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aModule);
|
|
|
|
|
2014-10-30 21:40:09 +00:00
|
|
|
// Returns false if an exception got thrown on aCx. Passing a null
|
|
|
|
// aElement is allowed; that wil produce an empty aScopeChain.
|
|
|
|
static bool GetScopeChainForElement(JSContext* aCx,
|
|
|
|
mozilla::dom::Element* aElement,
|
|
|
|
JS::AutoObjectVector& aScopeChain);
|
2015-06-15 07:44:09 +00:00
|
|
|
|
|
|
|
static void ResetTimeZone();
|
|
|
|
|
2014-11-18 16:01:08 +00:00
|
|
|
private:
|
|
|
|
// Implementation for our EvaluateString bits
|
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
|
|
|
JS::CompileOptions& aCompileOptions,
|
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
|
|
|
JS::MutableHandle<JS::Value> aRetValue,
|
|
|
|
void **aOffThreadToken);
|
2004-02-09 22:48:53 +00:00
|
|
|
};
|
|
|
|
|
2014-07-05 15:30:54 +00:00
|
|
|
template<typename T>
|
|
|
|
inline bool
|
|
|
|
AssignJSString(JSContext *cx, T &dest, JSString *s)
|
|
|
|
{
|
|
|
|
size_t len = js::GetStringLength(s);
|
|
|
|
static_assert(js::MaxStringLength < (1 << 28),
|
|
|
|
"Shouldn't overflow here or in SetCapacity");
|
2015-01-28 09:00:40 +00:00
|
|
|
if (MOZ_UNLIKELY(!dest.SetLength(len, mozilla::fallible))) {
|
2014-07-05 15:30:54 +00:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-19 13:23:19 +00:00
|
|
|
return js::CopyStringChars(cx, dest.BeginWriting(), s, len);
|
2014-07-05 15:30:54 +00:00
|
|
|
}
|
2004-02-09 22:48:53 +00:00
|
|
|
|
2014-07-12 07:43:06 +00:00
|
|
|
inline void
|
|
|
|
AssignJSFlatString(nsAString &dest, JSFlatString *s)
|
|
|
|
{
|
|
|
|
size_t len = js::GetFlatStringLength(s);
|
|
|
|
static_assert(js::MaxStringLength < (1 << 28),
|
|
|
|
"Shouldn't overflow here or in SetCapacity");
|
|
|
|
dest.SetLength(len);
|
2014-07-19 13:23:19 +00:00
|
|
|
js::CopyFlatStringChars(dest.BeginWriting(), s, len);
|
2014-07-12 07:43:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 15:30:54 +00:00
|
|
|
class nsAutoJSString : public nsAutoString
|
2004-02-09 22:48:53 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-06-11 03:15:56 +00:00
|
|
|
|
2010-12-03 08:24:17 +00:00
|
|
|
/**
|
2014-07-05 15:30:54 +00:00
|
|
|
* nsAutoJSString should be default constructed, which leaves it empty
|
|
|
|
* (this->IsEmpty()), and initialized with one of the init() methods below.
|
2010-12-03 08:24:17 +00:00
|
|
|
*/
|
2014-07-05 15:30:54 +00:00
|
|
|
nsAutoJSString() {}
|
2014-06-11 03:15:56 +00:00
|
|
|
|
|
|
|
bool init(JSContext* aContext, JSString* str)
|
2004-02-09 22:48:53 +00:00
|
|
|
{
|
2014-07-05 15:30:54 +00:00
|
|
|
return AssignJSString(aContext, *this, str);
|
2004-02-09 22:48:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 03:15:56 +00:00
|
|
|
bool init(JSContext* aContext, const JS::Value &v)
|
2012-06-02 06:39:06 +00:00
|
|
|
{
|
2014-06-11 03:15:56 +00:00
|
|
|
if (v.isString()) {
|
|
|
|
return init(aContext, v.toString());
|
|
|
|
}
|
2012-06-02 06:39:06 +00:00
|
|
|
|
2014-06-11 03:15:56 +00:00
|
|
|
// Stringify, making sure not to run script.
|
|
|
|
JS::Rooted<JSString*> str(aContext);
|
|
|
|
if (v.isObject()) {
|
|
|
|
str = JS_NewStringCopyZ(aContext, "[Object]");
|
|
|
|
} else {
|
|
|
|
JS::Rooted<JS::Value> rootedVal(aContext, v);
|
|
|
|
str = JS::ToString(aContext, rootedVal);
|
|
|
|
}
|
2010-12-03 08:24:17 +00:00
|
|
|
|
2014-06-11 03:15:56 +00:00
|
|
|
return str && init(aContext, str);
|
2010-07-15 06:19:36 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 03:15:56 +00:00
|
|
|
bool init(JSContext* aContext, jsid id)
|
2010-07-15 06:19:36 +00:00
|
|
|
{
|
2014-06-11 03:15:56 +00:00
|
|
|
JS::Rooted<JS::Value> v(aContext);
|
|
|
|
return JS_IdToValue(aContext, id, &v) && init(aContext, v);
|
2010-07-15 06:19:36 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 19:16:00 +00:00
|
|
|
bool init(const JS::Value &v);
|
|
|
|
|
2014-07-05 15:30:54 +00:00
|
|
|
~nsAutoJSString() {}
|
1998-12-22 22:03:20 +00:00
|
|
|
};
|
1998-12-03 00:33:00 +00:00
|
|
|
|
1998-12-22 22:03:20 +00:00
|
|
|
#endif /* nsJSUtils_h__ */
|