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"
21 class nsIScriptContext
;
22 class nsIScriptGlobalObject
;
27 static bool GetCallingLocation(JSContext
* aContext
, const char* *aFilename
,
30 static nsIScriptGlobalObject
*GetStaticScriptGlobal(JSObject
* aObj
);
32 static nsIScriptContext
*GetStaticScriptContext(JSObject
* aObj
);
34 static nsIScriptGlobalObject
*GetDynamicScriptGlobal(JSContext
*aContext
);
36 static nsIScriptContext
*GetDynamicScriptContext(JSContext
*aContext
);
39 * Retrieve the inner window ID based on the given JSContext.
41 * @param JSContext aContext
42 * The JSContext from which you want to find the inner window ID.
44 * @returns uint64_t the inner window ID.
46 static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext
*aContext
);
49 * Report a pending exception on aContext, if any. Note that this
50 * can be called when the context has a JS stack. If that's the
51 * case, the stack will be set aside before reporting the exception.
53 static void ReportPendingException(JSContext
*aContext
);
55 static nsresult
CompileFunction(JSContext
* aCx
,
56 JS::Handle
<JSObject
*> aTarget
,
57 JS::CompileOptions
& aOptions
,
58 const nsACString
& aName
,
60 const char** aArgArray
,
61 const nsAString
& aBody
,
62 JSObject
** aFunctionObject
);
64 struct EvaluateOptions
{
68 explicit EvaluateOptions() : coerceToString(false)
69 , reportUncaught(true)
72 EvaluateOptions
& setCoerceToString(bool aCoerce
) {
73 coerceToString
= aCoerce
;
77 EvaluateOptions
& setReportUncaught(bool aReport
) {
78 reportUncaught
= aReport
;
83 static nsresult
EvaluateString(JSContext
* aCx
,
84 const nsAString
& aScript
,
85 JS::Handle
<JSObject
*> aScopeObject
,
86 JS::CompileOptions
&aCompileOptions
,
87 EvaluateOptions
& aEvaluateOptions
,
89 void **aOffThreadToken
= nullptr);
94 class nsDependentJSString
: public nsDependentString
98 * In the case of string ids, getting the string's chars is infallible, so
99 * the dependent string can be constructed directly.
101 explicit nsDependentJSString(JS::Handle
<jsid
> id
)
102 : nsDependentString(JS_GetInternedStringChars(JSID_TO_STRING(id
)),
103 JS_GetStringLength(JSID_TO_STRING(id
)))
108 * Ditto for flat strings.
110 explicit nsDependentJSString(JSFlatString
* fstr
)
111 : nsDependentString(JS_GetFlatStringChars(fstr
),
112 JS_GetStringLength(JS_FORGET_STRING_FLATNESS(fstr
)))
117 * For all other strings, the nsDependentJSString object should be default
118 * constructed, which leaves it empty (this->IsEmpty()), and initialized with
119 * one of the init() methods below.
122 nsDependentJSString()
126 bool init(JSContext
* aContext
, JSString
* str
)
129 const jschar
* chars
= JS_GetStringCharsZAndLength(aContext
, str
, &length
);
133 NS_ASSERTION(IsEmpty(), "init() on initialized string");
134 nsDependentString
* base
= this;
135 new(base
) nsDependentString(chars
, length
);
139 bool init(JSContext
* aContext
, const JS::Value
&v
)
141 return init(aContext
, JSVAL_TO_STRING(v
));
144 void init(JSFlatString
* fstr
)
146 MOZ_ASSERT(IsEmpty(), "init() on initialized string");
147 new(this) nsDependentJSString(fstr
);
150 ~nsDependentJSString()
155 #endif /* nsJSUtils_h__ */