Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsJSUtils.h
blobd42f1fd8fa7076678ee0a78161b3740b23b6f4a8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsJSUtils_h__
7 #define nsJSUtils_h__
9 /**
10 * This is not a generated file. It contains common utility functions
11 * invoked from the JavaScript code generated from IDL interfaces.
12 * The goal of the utility functions is to cut down on the size of
13 * the generated code itself.
16 #include "mozilla/Assertions.h"
18 #include "jsapi.h"
19 #include "jsfriendapi.h"
20 #include "nsString.h"
22 class nsIScriptContext;
23 class nsIScriptGlobalObject;
25 namespace mozilla {
26 namespace dom {
27 class AutoJSAPI;
28 class Element;
32 class nsJSUtils
34 public:
35 static bool GetCallingLocation(JSContext* aContext, nsACString& aFilename,
36 uint32_t* aLineno);
37 static bool GetCallingLocation(JSContext* aContext, nsAString& aFilename,
38 uint32_t* aLineno);
40 static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj);
42 static nsIScriptContext *GetStaticScriptContext(JSObject* aObj);
44 /**
45 * Retrieve the inner window ID based on the given JSContext.
47 * @param JSContext aContext
48 * The JSContext from which you want to find the inner window ID.
50 * @returns uint64_t the inner window ID.
52 static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext);
54 /**
55 * Report a pending exception on aContext, if any. Note that this
56 * can be called when the context has a JS stack. If that's the
57 * case, the stack will be set aside before reporting the exception.
59 static void ReportPendingException(JSContext *aContext);
61 static nsresult CompileFunction(mozilla::dom::AutoJSAPI& jsapi,
62 JS::AutoObjectVector& aScopeChain,
63 JS::CompileOptions& aOptions,
64 const nsACString& aName,
65 uint32_t aArgCount,
66 const char** aArgArray,
67 const nsAString& aBody,
68 JSObject** aFunctionObject);
70 struct MOZ_STACK_CLASS EvaluateOptions {
71 bool coerceToString;
72 bool reportUncaught;
73 JS::AutoObjectVector scopeChain;
75 explicit EvaluateOptions(JSContext* cx)
76 : coerceToString(false)
77 , reportUncaught(true)
78 , scopeChain(cx)
81 EvaluateOptions& setCoerceToString(bool aCoerce) {
82 coerceToString = aCoerce;
83 return *this;
86 EvaluateOptions& setReportUncaught(bool aReport) {
87 reportUncaught = aReport;
88 return *this;
92 // aEvaluationGlobal is the global to evaluate in. The return value
93 // will then be wrapped back into the compartment aCx is in when
94 // this function is called.
95 static nsresult EvaluateString(JSContext* aCx,
96 const nsAString& aScript,
97 JS::Handle<JSObject*> aEvaluationGlobal,
98 JS::CompileOptions &aCompileOptions,
99 const EvaluateOptions& aEvaluateOptions,
100 JS::MutableHandle<JS::Value> aRetValue);
102 static nsresult EvaluateString(JSContext* aCx,
103 JS::SourceBufferHolder& aSrcBuf,
104 JS::Handle<JSObject*> aEvaluationGlobal,
105 JS::CompileOptions &aCompileOptions,
106 const EvaluateOptions& aEvaluateOptions,
107 JS::MutableHandle<JS::Value> aRetValue);
110 static nsresult EvaluateString(JSContext* aCx,
111 const nsAString& aScript,
112 JS::Handle<JSObject*> aEvaluationGlobal,
113 JS::CompileOptions &aCompileOptions);
115 static nsresult EvaluateString(JSContext* aCx,
116 JS::SourceBufferHolder& aSrcBuf,
117 JS::Handle<JSObject*> aEvaluationGlobal,
118 JS::CompileOptions &aCompileOptions,
119 void **aOffThreadToken);
121 // Returns false if an exception got thrown on aCx. Passing a null
122 // aElement is allowed; that wil produce an empty aScopeChain.
123 static bool GetScopeChainForElement(JSContext* aCx,
124 mozilla::dom::Element* aElement,
125 JS::AutoObjectVector& aScopeChain);
126 private:
127 // Implementation for our EvaluateString bits
128 static nsresult EvaluateString(JSContext* aCx,
129 JS::SourceBufferHolder& aSrcBuf,
130 JS::Handle<JSObject*> aEvaluationGlobal,
131 JS::CompileOptions& aCompileOptions,
132 const EvaluateOptions& aEvaluateOptions,
133 JS::MutableHandle<JS::Value> aRetValue,
134 void **aOffThreadToken);
137 class MOZ_STACK_CLASS AutoDontReportUncaught {
138 JSContext* mContext;
139 bool mWasSet;
141 public:
142 explicit AutoDontReportUncaught(JSContext* aContext) : mContext(aContext) {
143 MOZ_ASSERT(aContext);
144 mWasSet = JS::ContextOptionsRef(mContext).dontReportUncaught();
145 if (!mWasSet) {
146 JS::ContextOptionsRef(mContext).setDontReportUncaught(true);
149 ~AutoDontReportUncaught() {
150 if (!mWasSet) {
151 JS::ContextOptionsRef(mContext).setDontReportUncaught(false);
156 template<typename T>
157 inline bool
158 AssignJSString(JSContext *cx, T &dest, JSString *s)
160 size_t len = js::GetStringLength(s);
161 static_assert(js::MaxStringLength < (1 << 28),
162 "Shouldn't overflow here or in SetCapacity");
163 if (MOZ_UNLIKELY(!dest.SetLength(len, mozilla::fallible_t()))) {
164 JS_ReportOutOfMemory(cx);
165 return false;
167 return js::CopyStringChars(cx, dest.BeginWriting(), s, len);
170 inline void
171 AssignJSFlatString(nsAString &dest, JSFlatString *s)
173 size_t len = js::GetFlatStringLength(s);
174 static_assert(js::MaxStringLength < (1 << 28),
175 "Shouldn't overflow here or in SetCapacity");
176 dest.SetLength(len);
177 js::CopyFlatStringChars(dest.BeginWriting(), s, len);
180 class nsAutoJSString : public nsAutoString
182 public:
185 * nsAutoJSString should be default constructed, which leaves it empty
186 * (this->IsEmpty()), and initialized with one of the init() methods below.
188 nsAutoJSString() {}
190 bool init(JSContext* aContext, JSString* str)
192 return AssignJSString(aContext, *this, str);
195 bool init(JSContext* aContext, const JS::Value &v)
197 if (v.isString()) {
198 return init(aContext, v.toString());
201 // Stringify, making sure not to run script.
202 JS::Rooted<JSString*> str(aContext);
203 if (v.isObject()) {
204 str = JS_NewStringCopyZ(aContext, "[Object]");
205 } else {
206 JS::Rooted<JS::Value> rootedVal(aContext, v);
207 str = JS::ToString(aContext, rootedVal);
210 return str && init(aContext, str);
213 bool init(JSContext* aContext, jsid id)
215 JS::Rooted<JS::Value> v(aContext);
216 return JS_IdToValue(aContext, id, &v) && init(aContext, v);
219 bool init(const JS::Value &v);
221 ~nsAutoJSString() {}
224 #endif /* nsJSUtils_h__ */