dom/base/nsJSUtils.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsJSUtils_h__
michael@0 7 #define nsJSUtils_h__
michael@0 8
michael@0 9 /**
michael@0 10 * This is not a generated file. It contains common utility functions
michael@0 11 * invoked from the JavaScript code generated from IDL interfaces.
michael@0 12 * The goal of the utility functions is to cut down on the size of
michael@0 13 * the generated code itself.
michael@0 14 */
michael@0 15
michael@0 16 #include "mozilla/Assertions.h"
michael@0 17
michael@0 18 #include "jsapi.h"
michael@0 19 #include "nsString.h"
michael@0 20
michael@0 21 class nsIScriptContext;
michael@0 22 class nsIScriptGlobalObject;
michael@0 23
michael@0 24 class nsJSUtils
michael@0 25 {
michael@0 26 public:
michael@0 27 static bool GetCallingLocation(JSContext* aContext, const char* *aFilename,
michael@0 28 uint32_t* aLineno);
michael@0 29
michael@0 30 static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj);
michael@0 31
michael@0 32 static nsIScriptContext *GetStaticScriptContext(JSObject* aObj);
michael@0 33
michael@0 34 static nsIScriptGlobalObject *GetDynamicScriptGlobal(JSContext *aContext);
michael@0 35
michael@0 36 static nsIScriptContext *GetDynamicScriptContext(JSContext *aContext);
michael@0 37
michael@0 38 /**
michael@0 39 * Retrieve the inner window ID based on the given JSContext.
michael@0 40 *
michael@0 41 * @param JSContext aContext
michael@0 42 * The JSContext from which you want to find the inner window ID.
michael@0 43 *
michael@0 44 * @returns uint64_t the inner window ID.
michael@0 45 */
michael@0 46 static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext);
michael@0 47
michael@0 48 /**
michael@0 49 * Report a pending exception on aContext, if any. Note that this
michael@0 50 * can be called when the context has a JS stack. If that's the
michael@0 51 * case, the stack will be set aside before reporting the exception.
michael@0 52 */
michael@0 53 static void ReportPendingException(JSContext *aContext);
michael@0 54
michael@0 55 static nsresult CompileFunction(JSContext* aCx,
michael@0 56 JS::Handle<JSObject*> aTarget,
michael@0 57 JS::CompileOptions& aOptions,
michael@0 58 const nsACString& aName,
michael@0 59 uint32_t aArgCount,
michael@0 60 const char** aArgArray,
michael@0 61 const nsAString& aBody,
michael@0 62 JSObject** aFunctionObject);
michael@0 63
michael@0 64 struct EvaluateOptions {
michael@0 65 bool coerceToString;
michael@0 66 bool reportUncaught;
michael@0 67 bool needResult;
michael@0 68
michael@0 69 explicit EvaluateOptions() : coerceToString(false)
michael@0 70 , reportUncaught(true)
michael@0 71 , needResult(true)
michael@0 72 {}
michael@0 73
michael@0 74 EvaluateOptions& setCoerceToString(bool aCoerce) {
michael@0 75 coerceToString = aCoerce;
michael@0 76 return *this;
michael@0 77 }
michael@0 78
michael@0 79 EvaluateOptions& setReportUncaught(bool aReport) {
michael@0 80 reportUncaught = aReport;
michael@0 81 return *this;
michael@0 82 }
michael@0 83
michael@0 84 EvaluateOptions& setNeedResult(bool aNeedResult) {
michael@0 85 needResult = aNeedResult;
michael@0 86 return *this;
michael@0 87 }
michael@0 88 };
michael@0 89
michael@0 90 static nsresult EvaluateString(JSContext* aCx,
michael@0 91 const nsAString& aScript,
michael@0 92 JS::Handle<JSObject*> aScopeObject,
michael@0 93 JS::CompileOptions &aCompileOptions,
michael@0 94 const EvaluateOptions& aEvaluateOptions,
michael@0 95 JS::MutableHandle<JS::Value> aRetValue,
michael@0 96 void **aOffThreadToken = nullptr);
michael@0 97
michael@0 98 static nsresult EvaluateString(JSContext* aCx,
michael@0 99 JS::SourceBufferHolder& aSrcBuf,
michael@0 100 JS::Handle<JSObject*> aScopeObject,
michael@0 101 JS::CompileOptions &aCompileOptions,
michael@0 102 const EvaluateOptions& aEvaluateOptions,
michael@0 103 JS::MutableHandle<JS::Value> aRetValue,
michael@0 104 void **aOffThreadToken = nullptr);
michael@0 105
michael@0 106
michael@0 107 static nsresult EvaluateString(JSContext* aCx,
michael@0 108 const nsAString& aScript,
michael@0 109 JS::Handle<JSObject*> aScopeObject,
michael@0 110 JS::CompileOptions &aCompileOptions,
michael@0 111 void **aOffThreadToken = nullptr);
michael@0 112
michael@0 113 static nsresult EvaluateString(JSContext* aCx,
michael@0 114 JS::SourceBufferHolder& aSrcBuf,
michael@0 115 JS::Handle<JSObject*> aScopeObject,
michael@0 116 JS::CompileOptions &aCompileOptions,
michael@0 117 void **aOffThreadToken = nullptr);
michael@0 118
michael@0 119 };
michael@0 120
michael@0 121 class MOZ_STACK_CLASS AutoDontReportUncaught {
michael@0 122 JSContext* mContext;
michael@0 123 bool mWasSet;
michael@0 124
michael@0 125 public:
michael@0 126 AutoDontReportUncaught(JSContext* aContext) : mContext(aContext) {
michael@0 127 MOZ_ASSERT(aContext);
michael@0 128 mWasSet = JS::ContextOptionsRef(mContext).dontReportUncaught();
michael@0 129 if (!mWasSet) {
michael@0 130 JS::ContextOptionsRef(mContext).setDontReportUncaught(true);
michael@0 131 }
michael@0 132 }
michael@0 133 ~AutoDontReportUncaught() {
michael@0 134 if (!mWasSet) {
michael@0 135 JS::ContextOptionsRef(mContext).setDontReportUncaught(false);
michael@0 136 }
michael@0 137 }
michael@0 138 };
michael@0 139
michael@0 140
michael@0 141 class nsDependentJSString : public nsDependentString
michael@0 142 {
michael@0 143 public:
michael@0 144 /**
michael@0 145 * In the case of string ids, getting the string's chars is infallible, so
michael@0 146 * the dependent string can be constructed directly.
michael@0 147 */
michael@0 148 explicit nsDependentJSString(JS::Handle<jsid> id)
michael@0 149 : nsDependentString(JS_GetInternedStringChars(JSID_TO_STRING(id)),
michael@0 150 JS_GetStringLength(JSID_TO_STRING(id)))
michael@0 151 {
michael@0 152 }
michael@0 153
michael@0 154 /**
michael@0 155 * Ditto for flat strings.
michael@0 156 */
michael@0 157 explicit nsDependentJSString(JSFlatString* fstr)
michael@0 158 : nsDependentString(JS_GetFlatStringChars(fstr),
michael@0 159 JS_GetStringLength(JS_FORGET_STRING_FLATNESS(fstr)))
michael@0 160 {
michael@0 161 }
michael@0 162
michael@0 163 /**
michael@0 164 * For all other strings, the nsDependentJSString object should be default
michael@0 165 * constructed, which leaves it empty (this->IsEmpty()), and initialized with
michael@0 166 * one of the init() methods below.
michael@0 167 */
michael@0 168
michael@0 169 nsDependentJSString()
michael@0 170 {
michael@0 171 }
michael@0 172
michael@0 173 bool init(JSContext* aContext, JSString* str)
michael@0 174 {
michael@0 175 size_t length;
michael@0 176 const jschar* chars = JS_GetStringCharsZAndLength(aContext, str, &length);
michael@0 177 if (!chars)
michael@0 178 return false;
michael@0 179
michael@0 180 NS_ASSERTION(IsEmpty(), "init() on initialized string");
michael@0 181 nsDependentString* base = this;
michael@0 182 new(base) nsDependentString(chars, length);
michael@0 183 return true;
michael@0 184 }
michael@0 185
michael@0 186 bool init(JSContext* aContext, const JS::Value &v)
michael@0 187 {
michael@0 188 return init(aContext, JSVAL_TO_STRING(v));
michael@0 189 }
michael@0 190
michael@0 191 void init(JSFlatString* fstr)
michael@0 192 {
michael@0 193 MOZ_ASSERT(IsEmpty(), "init() on initialized string");
michael@0 194 new(this) nsDependentJSString(fstr);
michael@0 195 }
michael@0 196
michael@0 197 ~nsDependentJSString()
michael@0 198 {
michael@0 199 }
michael@0 200 };
michael@0 201
michael@0 202 #endif /* nsJSUtils_h__ */

mercurial