Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsJSUtils.h
blob18f7864fa84268f4e9d4d8ce505363a67ca80272
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 class nsJSUtils
27 public:
28 static bool GetCallingLocation(JSContext* aContext, const char* *aFilename,
29 uint32_t* aLineno);
31 static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj);
33 static nsIScriptContext *GetStaticScriptContext(JSObject* aObj);
35 /**
36 * Retrieve the inner window ID based on the given JSContext.
38 * @param JSContext aContext
39 * The JSContext from which you want to find the inner window ID.
41 * @returns uint64_t the inner window ID.
43 static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext);
45 /**
46 * Report a pending exception on aContext, if any. Note that this
47 * can be called when the context has a JS stack. If that's the
48 * case, the stack will be set aside before reporting the exception.
50 static void ReportPendingException(JSContext *aContext);
52 static nsresult CompileFunction(JSContext* aCx,
53 JS::Handle<JSObject*> aTarget,
54 JS::CompileOptions& aOptions,
55 const nsACString& aName,
56 uint32_t aArgCount,
57 const char** aArgArray,
58 const nsAString& aBody,
59 JSObject** aFunctionObject);
61 struct EvaluateOptions {
62 bool coerceToString;
63 bool reportUncaught;
64 bool needResult;
66 explicit EvaluateOptions() : coerceToString(false)
67 , reportUncaught(true)
68 , needResult(true)
71 EvaluateOptions& setCoerceToString(bool aCoerce) {
72 coerceToString = aCoerce;
73 return *this;
76 EvaluateOptions& setReportUncaught(bool aReport) {
77 reportUncaught = aReport;
78 return *this;
81 EvaluateOptions& setNeedResult(bool aNeedResult) {
82 needResult = aNeedResult;
83 return *this;
87 static nsresult EvaluateString(JSContext* aCx,
88 const nsAString& aScript,
89 JS::Handle<JSObject*> aScopeObject,
90 JS::CompileOptions &aCompileOptions,
91 const EvaluateOptions& aEvaluateOptions,
92 JS::MutableHandle<JS::Value> aRetValue,
93 void **aOffThreadToken = nullptr);
95 static nsresult EvaluateString(JSContext* aCx,
96 JS::SourceBufferHolder& aSrcBuf,
97 JS::Handle<JSObject*> aScopeObject,
98 JS::CompileOptions &aCompileOptions,
99 const EvaluateOptions& aEvaluateOptions,
100 JS::MutableHandle<JS::Value> aRetValue,
101 void **aOffThreadToken = nullptr);
104 static nsresult EvaluateString(JSContext* aCx,
105 const nsAString& aScript,
106 JS::Handle<JSObject*> aScopeObject,
107 JS::CompileOptions &aCompileOptions,
108 void **aOffThreadToken = nullptr);
110 static nsresult EvaluateString(JSContext* aCx,
111 JS::SourceBufferHolder& aSrcBuf,
112 JS::Handle<JSObject*> aScopeObject,
113 JS::CompileOptions &aCompileOptions,
114 void **aOffThreadToken = nullptr);
118 class MOZ_STACK_CLASS AutoDontReportUncaught {
119 JSContext* mContext;
120 bool mWasSet;
122 public:
123 explicit AutoDontReportUncaught(JSContext* aContext) : mContext(aContext) {
124 MOZ_ASSERT(aContext);
125 mWasSet = JS::ContextOptionsRef(mContext).dontReportUncaught();
126 if (!mWasSet) {
127 JS::ContextOptionsRef(mContext).setDontReportUncaught(true);
130 ~AutoDontReportUncaught() {
131 if (!mWasSet) {
132 JS::ContextOptionsRef(mContext).setDontReportUncaught(false);
137 template<typename T>
138 inline bool
139 AssignJSString(JSContext *cx, T &dest, JSString *s)
141 size_t len = js::GetStringLength(s);
142 static_assert(js::MaxStringLength < (1 << 28),
143 "Shouldn't overflow here or in SetCapacity");
144 if (MOZ_UNLIKELY(!dest.SetLength(len, mozilla::fallible_t()))) {
145 JS_ReportOutOfMemory(cx);
146 return false;
148 return js::CopyStringChars(cx, dest.BeginWriting(), s, len);
151 inline void
152 AssignJSFlatString(nsAString &dest, JSFlatString *s)
154 size_t len = js::GetFlatStringLength(s);
155 static_assert(js::MaxStringLength < (1 << 28),
156 "Shouldn't overflow here or in SetCapacity");
157 dest.SetLength(len);
158 js::CopyFlatStringChars(dest.BeginWriting(), s, len);
161 class nsAutoJSString : public nsAutoString
163 public:
166 * nsAutoJSString should be default constructed, which leaves it empty
167 * (this->IsEmpty()), and initialized with one of the init() methods below.
169 nsAutoJSString() {}
171 bool init(JSContext* aContext, JSString* str)
173 return AssignJSString(aContext, *this, str);
176 bool init(JSContext* aContext, const JS::Value &v)
178 if (v.isString()) {
179 return init(aContext, v.toString());
182 // Stringify, making sure not to run script.
183 JS::Rooted<JSString*> str(aContext);
184 if (v.isObject()) {
185 str = JS_NewStringCopyZ(aContext, "[Object]");
186 } else {
187 JS::Rooted<JS::Value> rootedVal(aContext, v);
188 str = JS::ToString(aContext, rootedVal);
191 return str && init(aContext, str);
194 bool init(JSContext* aContext, jsid id)
196 JS::Rooted<JS::Value> v(aContext);
197 return JS_IdToValue(aContext, id, &v) && init(aContext, v);
200 ~nsAutoJSString() {}
203 #endif /* nsJSUtils_h__ */