2001-09-25 22:43:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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"
|
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
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
class nsJSUtils
|
|
|
|
{
|
1998-12-22 22:03:20 +00:00
|
|
|
public:
|
2013-08-08 22:53:04 +00:00
|
|
|
static bool GetCallingLocation(JSContext* aContext, const char* *aFilename,
|
|
|
|
uint32_t* aLineno);
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-17 02:50:25 +00:00
|
|
|
|
|
|
|
static nsresult CompileFunction(JSContext* aCx,
|
2013-11-11 08:04:41 +00:00
|
|
|
JS::Handle<JSObject*> aTarget,
|
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);
|
|
|
|
|
2013-08-08 23:51:35 +00:00
|
|
|
struct EvaluateOptions {
|
|
|
|
bool coerceToString;
|
2013-08-08 23:51:35 +00:00
|
|
|
bool reportUncaught;
|
2014-04-01 10:34:39 +00:00
|
|
|
bool needResult;
|
2013-08-08 23:51:35 +00:00
|
|
|
|
|
|
|
explicit EvaluateOptions() : coerceToString(false)
|
2013-08-08 23:51:35 +00:00
|
|
|
, reportUncaught(true)
|
2014-04-01 10:34:39 +00:00
|
|
|
, needResult(true)
|
2013-08-08 23:51:35 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
EvaluateOptions& setCoerceToString(bool aCoerce) {
|
|
|
|
coerceToString = aCoerce;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-08-08 23:51:35 +00:00
|
|
|
|
|
|
|
EvaluateOptions& setReportUncaught(bool aReport) {
|
|
|
|
reportUncaught = aReport;
|
|
|
|
return *this;
|
|
|
|
}
|
2014-04-01 10:34:39 +00:00
|
|
|
|
|
|
|
EvaluateOptions& setNeedResult(bool aNeedResult) {
|
|
|
|
needResult = aNeedResult;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-08-08 23:51:35 +00:00
|
|
|
};
|
|
|
|
|
2013-08-08 23:51:34 +00:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
const nsAString& aScript,
|
|
|
|
JS::Handle<JSObject*> aScopeObject,
|
2013-08-08 23:51:35 +00:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
2014-04-01 10:34:39 +00:00
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
|
|
|
JS::MutableHandle<JS::Value> aRetValue,
|
|
|
|
void **aOffThreadToken = nullptr);
|
|
|
|
|
2014-04-25 14:11:56 +00:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
|
|
|
JS::Handle<JSObject*> aScopeObject,
|
|
|
|
JS::CompileOptions &aCompileOptions,
|
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
|
|
|
JS::MutableHandle<JS::Value> aRetValue,
|
|
|
|
void **aOffThreadToken = nullptr);
|
|
|
|
|
2014-04-01 10:34:39 +00:00
|
|
|
|
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
const nsAString& aScript,
|
|
|
|
JS::Handle<JSObject*> aScopeObject,
|
|
|
|
JS::CompileOptions &aCompileOptions,
|
2013-09-11 23:42:09 +00:00
|
|
|
void **aOffThreadToken = nullptr);
|
2013-08-08 23:51:34 +00:00
|
|
|
|
2014-04-25 14:11:56 +00:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
|
|
|
JS::Handle<JSObject*> aScopeObject,
|
|
|
|
JS::CompileOptions &aCompileOptions,
|
|
|
|
void **aOffThreadToken = nullptr);
|
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
};
|
|
|
|
|
2013-11-19 21:53:00 +00:00
|
|
|
class MOZ_STACK_CLASS AutoDontReportUncaught {
|
|
|
|
JSContext* mContext;
|
|
|
|
bool mWasSet;
|
|
|
|
|
|
|
|
public:
|
2014-07-28 17:17:51 +00:00
|
|
|
explicit AutoDontReportUncaught(JSContext* aContext) : mContext(aContext) {
|
2013-11-19 21:53:00 +00:00
|
|
|
MOZ_ASSERT(aContext);
|
|
|
|
mWasSet = JS::ContextOptionsRef(mContext).dontReportUncaught();
|
|
|
|
if (!mWasSet) {
|
|
|
|
JS::ContextOptionsRef(mContext).setDontReportUncaught(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~AutoDontReportUncaught() {
|
|
|
|
if (!mWasSet) {
|
|
|
|
JS::ContextOptionsRef(mContext).setDontReportUncaught(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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");
|
2014-07-19 13:23:19 +00:00
|
|
|
if (MOZ_UNLIKELY(!dest.SetLength(len, mozilla::fallible_t()))) {
|
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
|
|
|
}
|
|
|
|
|
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__ */
|