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/. */
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"
19 #include "jsfriendapi.h"
22 class nsIScriptContext
;
23 class nsIScriptGlobalObject
;
35 static bool GetCallingLocation(JSContext
* aContext
, nsACString
& aFilename
,
37 static bool GetCallingLocation(JSContext
* aContext
, nsAString
& aFilename
,
40 static nsIScriptGlobalObject
*GetStaticScriptGlobal(JSObject
* aObj
);
42 static nsIScriptContext
*GetStaticScriptContext(JSObject
* aObj
);
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
);
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
,
66 const char** aArgArray
,
67 const nsAString
& aBody
,
68 JSObject
** aFunctionObject
);
70 struct MOZ_STACK_CLASS EvaluateOptions
{
73 JS::AutoObjectVector scopeChain
;
75 explicit EvaluateOptions(JSContext
* cx
)
76 : coerceToString(false)
77 , reportUncaught(true)
81 EvaluateOptions
& setCoerceToString(bool aCoerce
) {
82 coerceToString
= aCoerce
;
86 EvaluateOptions
& setReportUncaught(bool aReport
) {
87 reportUncaught
= aReport
;
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
);
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
{
142 explicit AutoDontReportUncaught(JSContext
* aContext
) : mContext(aContext
) {
143 MOZ_ASSERT(aContext
);
144 mWasSet
= JS::ContextOptionsRef(mContext
).dontReportUncaught();
146 JS::ContextOptionsRef(mContext
).setDontReportUncaught(true);
149 ~AutoDontReportUncaught() {
151 JS::ContextOptionsRef(mContext
).setDontReportUncaught(false);
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
);
167 return js::CopyStringChars(cx
, dest
.BeginWriting(), s
, len
);
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");
177 js::CopyFlatStringChars(dest
.BeginWriting(), s
, len
);
180 class nsAutoJSString
: public nsAutoString
185 * nsAutoJSString should be default constructed, which leaves it empty
186 * (this->IsEmpty()), and initialized with one of the init() methods below.
190 bool init(JSContext
* aContext
, JSString
* str
)
192 return AssignJSString(aContext
, *this, str
);
195 bool init(JSContext
* aContext
, const JS::Value
&v
)
198 return init(aContext
, v
.toString());
201 // Stringify, making sure not to run script.
202 JS::Rooted
<JSString
*> str(aContext
);
204 str
= JS_NewStringCopyZ(aContext
, "[Object]");
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
);
224 #endif /* nsJSUtils_h__ */