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
;
28 static bool GetCallingLocation(JSContext
* aContext
, const char* *aFilename
,
31 static nsIScriptGlobalObject
*GetStaticScriptGlobal(JSObject
* aObj
);
33 static nsIScriptContext
*GetStaticScriptContext(JSObject
* aObj
);
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
);
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
,
57 const char** aArgArray
,
58 const nsAString
& aBody
,
59 JSObject
** aFunctionObject
);
61 struct EvaluateOptions
{
66 explicit EvaluateOptions() : coerceToString(false)
67 , reportUncaught(true)
71 EvaluateOptions
& setCoerceToString(bool aCoerce
) {
72 coerceToString
= aCoerce
;
76 EvaluateOptions
& setReportUncaught(bool aReport
) {
77 reportUncaught
= aReport
;
81 EvaluateOptions
& setNeedResult(bool aNeedResult
) {
82 needResult
= aNeedResult
;
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
{
123 explicit AutoDontReportUncaught(JSContext
* aContext
) : mContext(aContext
) {
124 MOZ_ASSERT(aContext
);
125 mWasSet
= JS::ContextOptionsRef(mContext
).dontReportUncaught();
127 JS::ContextOptionsRef(mContext
).setDontReportUncaught(true);
130 ~AutoDontReportUncaught() {
132 JS::ContextOptionsRef(mContext
).setDontReportUncaught(false);
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
);
148 return js::CopyStringChars(cx
, dest
.BeginWriting(), s
, len
);
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");
158 js::CopyFlatStringChars(dest
.BeginWriting(), s
, len
);
161 class nsAutoJSString
: public nsAutoString
166 * nsAutoJSString should be default constructed, which leaves it empty
167 * (this->IsEmpty()), and initialized with one of the init() methods below.
171 bool init(JSContext
* aContext
, JSString
* str
)
173 return AssignJSString(aContext
, *this, str
);
176 bool init(JSContext
* aContext
, const JS::Value
&v
)
179 return init(aContext
, v
.toString());
182 // Stringify, making sure not to run script.
183 JS::Rooted
<JSString
*> str(aContext
);
185 str
= JS_NewStringCopyZ(aContext
, "[Object]");
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
);
203 #endif /* nsJSUtils_h__ */