2015-05-03 22:32:37 +03: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 15:12:37 +04: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 03:33:00 +03:00
|
|
|
|
1998-12-23 01:03:20 +03:00
|
|
|
#ifndef nsJSUtils_h__
|
|
|
|
#define nsJSUtils_h__
|
|
|
|
|
1998-12-03 03:33:00 +03: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 10:39:06 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
1998-12-03 03:33:00 +03:00
|
|
|
#include "jsapi.h"
|
2014-07-05 19:30:54 +04:00
|
|
|
#include "jsfriendapi.h"
|
2014-12-18 00:50:20 +03:00
|
|
|
#include "js/Conversions.h"
|
1998-12-03 03:33:00 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
|
1999-12-18 23:29:29 +03:00
|
|
|
class nsIScriptContext;
|
|
|
|
class nsIScriptGlobalObject;
|
1999-03-29 02:19:42 +04:00
|
|
|
|
2014-09-29 17:34:21 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class AutoJSAPI;
|
2014-10-31 00:40:09 +03:00
|
|
|
class Element;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2014-09-29 17:34:21 +04:00
|
|
|
|
2004-02-10 01:48:53 +03:00
|
|
|
class nsJSUtils
|
|
|
|
{
|
1998-12-23 01:03:20 +03:00
|
|
|
public:
|
2015-01-06 00:53:27 +03:00
|
|
|
static bool GetCallingLocation(JSContext* aContext, nsACString& aFilename,
|
2015-09-02 03:56:03 +03:00
|
|
|
uint32_t* aLineno = nullptr,
|
|
|
|
uint32_t* aColumn = nullptr);
|
2015-01-06 00:53:27 +03:00
|
|
|
static bool GetCallingLocation(JSContext* aContext, nsAString& aFilename,
|
2015-09-02 03:56:03 +03: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 21:42:36 +04:00
|
|
|
|
2012-11-09 19:43:57 +04: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 21:42:36 +04:00
|
|
|
|
2012-11-09 19:43:57 +04: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 21:42:36 +04:00
|
|
|
|
2010-12-20 19:21:58 +03:00
|
|
|
/**
|
2011-08-25 00:44:35 +04:00
|
|
|
* Retrieve the inner window ID based on the given JSContext.
|
2010-12-20 19:21:58 +03:00
|
|
|
*
|
|
|
|
* @param JSContext aContext
|
2011-08-25 00:44:35 +04:00
|
|
|
* The JSContext from which you want to find the inner window ID.
|
2010-12-20 19:21:58 +03:00
|
|
|
*
|
2012-08-22 19:56:38 +04:00
|
|
|
* @returns uint64_t the inner window ID.
|
2010-12-20 19:21:58 +03:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext);
|
2012-11-09 19:43:57 +04:00
|
|
|
|
2014-09-29 17:34:21 +04:00
|
|
|
static nsresult CompileFunction(mozilla::dom::AutoJSAPI& jsapi,
|
2014-10-25 08:50:29 +04:00
|
|
|
JS::AutoObjectVector& aScopeChain,
|
2013-01-17 06:50:25 +04:00
|
|
|
JS::CompileOptions& aOptions,
|
|
|
|
const nsACString& aName,
|
|
|
|
uint32_t aArgCount,
|
|
|
|
const char** aArgArray,
|
|
|
|
const nsAString& aBody,
|
|
|
|
JSObject** aFunctionObject);
|
|
|
|
|
2014-11-13 01:04:29 +03:00
|
|
|
struct MOZ_STACK_CLASS EvaluateOptions {
|
|
|
|
JS::AutoObjectVector scopeChain;
|
2013-08-09 03:51:35 +04:00
|
|
|
|
2014-11-13 01:04:29 +03:00
|
|
|
explicit EvaluateOptions(JSContext* cx)
|
2016-05-10 05:25:40 +03:00
|
|
|
: scopeChain(cx)
|
2013-08-09 03:51:35 +04:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2014-11-13 01:04:29 +03: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 03:11:06 +03: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-09 03:51:34 +04:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
const nsAString& aScript,
|
2014-11-13 01:04:29 +03:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2013-08-09 03:51:35 +04:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
2014-04-01 14:34:39 +04:00
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
2014-11-18 19:01:08 +03:00
|
|
|
JS::MutableHandle<JS::Value> aRetValue);
|
2014-04-01 14:34:39 +04:00
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
2014-11-13 01:04:29 +03:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2014-04-25 18:11:56 +04:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
|
|
|
const EvaluateOptions& aEvaluateOptions,
|
2014-11-18 19:01:08 +03:00
|
|
|
JS::MutableHandle<JS::Value> aRetValue);
|
2014-04-25 18:11:56 +04:00
|
|
|
|
2014-04-01 14:34:39 +04:00
|
|
|
|
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
const nsAString& aScript,
|
2014-11-13 01:04:29 +03:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2014-11-18 19:01:08 +03:00
|
|
|
JS::CompileOptions &aCompileOptions);
|
2013-08-09 03:51:34 +04:00
|
|
|
|
2014-04-25 18:11:56 +04:00
|
|
|
static nsresult EvaluateString(JSContext* aCx,
|
|
|
|
JS::SourceBufferHolder& aSrcBuf,
|
2014-11-13 01:04:29 +03:00
|
|
|
JS::Handle<JSObject*> aEvaluationGlobal,
|
2014-04-25 18:11:56 +04:00
|
|
|
JS::CompileOptions &aCompileOptions,
|
2014-11-18 19:01:08 +03:00
|
|
|
void **aOffThreadToken);
|
2014-04-25 18:11:56 +04:00
|
|
|
|
2016-05-07 14:46:38 +03: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-31 00:40:09 +03: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 10:44:09 +03:00
|
|
|
|
|
|
|
static void ResetTimeZone();
|
|
|
|
|
2014-11-18 19:01:08 +03: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-10 01:48:53 +03:00
|
|
|
};
|
|
|
|
|
2014-07-05 19:30:54 +04: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 12:00:40 +03:00
|
|
|
if (MOZ_UNLIKELY(!dest.SetLength(len, mozilla::fallible))) {
|
2014-07-05 19:30:54 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-19 17:23:19 +04:00
|
|
|
return js::CopyStringChars(cx, dest.BeginWriting(), s, len);
|
2014-07-05 19:30:54 +04:00
|
|
|
}
|
2004-02-10 01:48:53 +03:00
|
|
|
|
2014-07-12 11:43:06 +04: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 17:23:19 +04:00
|
|
|
js::CopyFlatStringChars(dest.BeginWriting(), s, len);
|
2014-07-12 11:43:06 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
class nsAutoJSString : public nsAutoString
|
2004-02-10 01:48:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-06-11 07:15:56 +04:00
|
|
|
|
2010-12-03 11:24:17 +03:00
|
|
|
/**
|
2014-07-05 19:30:54 +04:00
|
|
|
* nsAutoJSString should be default constructed, which leaves it empty
|
|
|
|
* (this->IsEmpty()), and initialized with one of the init() methods below.
|
2010-12-03 11:24:17 +03:00
|
|
|
*/
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString() {}
|
2014-06-11 07:15:56 +04:00
|
|
|
|
|
|
|
bool init(JSContext* aContext, JSString* str)
|
2004-02-10 01:48:53 +03:00
|
|
|
{
|
2014-07-05 19:30:54 +04:00
|
|
|
return AssignJSString(aContext, *this, str);
|
2004-02-10 01:48:53 +03:00
|
|
|
}
|
|
|
|
|
2014-06-11 07:15:56 +04:00
|
|
|
bool init(JSContext* aContext, const JS::Value &v)
|
2012-06-02 10:39:06 +04:00
|
|
|
{
|
2014-06-11 07:15:56 +04:00
|
|
|
if (v.isString()) {
|
|
|
|
return init(aContext, v.toString());
|
|
|
|
}
|
2012-06-02 10:39:06 +04:00
|
|
|
|
2014-06-11 07:15:56 +04: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 11:24:17 +03:00
|
|
|
|
2014-06-11 07:15:56 +04:00
|
|
|
return str && init(aContext, str);
|
2010-07-15 10:19:36 +04:00
|
|
|
}
|
|
|
|
|
2014-06-11 07:15:56 +04:00
|
|
|
bool init(JSContext* aContext, jsid id)
|
2010-07-15 10:19:36 +04:00
|
|
|
{
|
2014-06-11 07:15:56 +04:00
|
|
|
JS::Rooted<JS::Value> v(aContext);
|
|
|
|
return JS_IdToValue(aContext, id, &v) && init(aContext, v);
|
2010-07-15 10:19:36 +04:00
|
|
|
}
|
|
|
|
|
2015-02-19 22:16:00 +03:00
|
|
|
bool init(const JS::Value &v);
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
~nsAutoJSString() {}
|
1998-12-23 01:03:20 +03:00
|
|
|
};
|
1998-12-03 03:33:00 +03:00
|
|
|
|
1998-12-23 01:03:20 +03:00
|
|
|
#endif /* nsJSUtils_h__ */
|