Merge inbound to m-c on a CLOSED TREE.
[gecko.git] / dom / base / nsJSUtils.h
blobe7c63ca25d6892cf3bba3d1cd05279d84441d5d1
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 "nsString.h"
21 class nsIScriptContext;
22 class nsIScriptGlobalObject;
24 class nsJSUtils
26 public:
27 static bool GetCallingLocation(JSContext* aContext, const char* *aFilename,
28 uint32_t* aLineno);
30 static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj);
32 static nsIScriptContext *GetStaticScriptContext(JSObject* aObj);
34 static nsIScriptGlobalObject *GetDynamicScriptGlobal(JSContext *aContext);
36 static nsIScriptContext *GetDynamicScriptContext(JSContext *aContext);
38 /**
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);
48 /**
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,
59 uint32_t aArgCount,
60 const char** aArgArray,
61 const nsAString& aBody,
62 JSObject** aFunctionObject);
64 struct EvaluateOptions {
65 bool coerceToString;
66 bool reportUncaught;
68 explicit EvaluateOptions() : coerceToString(false)
69 , reportUncaught(true)
72 EvaluateOptions& setCoerceToString(bool aCoerce) {
73 coerceToString = aCoerce;
74 return *this;
77 EvaluateOptions& setReportUncaught(bool aReport) {
78 reportUncaught = aReport;
79 return *this;
83 static nsresult EvaluateString(JSContext* aCx,
84 const nsAString& aScript,
85 JS::Handle<JSObject*> aScopeObject,
86 JS::CompileOptions &aCompileOptions,
87 EvaluateOptions& aEvaluateOptions,
88 JS::Value* aRetValue,
89 void **aOffThreadToken = nullptr);
94 class nsDependentJSString : public nsDependentString
96 public:
97 /**
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)
128 size_t length;
129 const jschar* chars = JS_GetStringCharsZAndLength(aContext, str, &length);
130 if (!chars)
131 return false;
133 NS_ASSERTION(IsEmpty(), "init() on initialized string");
134 nsDependentString* base = this;
135 new(base) nsDependentString(chars, length);
136 return true;
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__ */