1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/base/nsJSUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,202 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsJSUtils_h__ 1.10 +#define nsJSUtils_h__ 1.11 + 1.12 +/** 1.13 + * This is not a generated file. It contains common utility functions 1.14 + * invoked from the JavaScript code generated from IDL interfaces. 1.15 + * The goal of the utility functions is to cut down on the size of 1.16 + * the generated code itself. 1.17 + */ 1.18 + 1.19 +#include "mozilla/Assertions.h" 1.20 + 1.21 +#include "jsapi.h" 1.22 +#include "nsString.h" 1.23 + 1.24 +class nsIScriptContext; 1.25 +class nsIScriptGlobalObject; 1.26 + 1.27 +class nsJSUtils 1.28 +{ 1.29 +public: 1.30 + static bool GetCallingLocation(JSContext* aContext, const char* *aFilename, 1.31 + uint32_t* aLineno); 1.32 + 1.33 + static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj); 1.34 + 1.35 + static nsIScriptContext *GetStaticScriptContext(JSObject* aObj); 1.36 + 1.37 + static nsIScriptGlobalObject *GetDynamicScriptGlobal(JSContext *aContext); 1.38 + 1.39 + static nsIScriptContext *GetDynamicScriptContext(JSContext *aContext); 1.40 + 1.41 + /** 1.42 + * Retrieve the inner window ID based on the given JSContext. 1.43 + * 1.44 + * @param JSContext aContext 1.45 + * The JSContext from which you want to find the inner window ID. 1.46 + * 1.47 + * @returns uint64_t the inner window ID. 1.48 + */ 1.49 + static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext); 1.50 + 1.51 + /** 1.52 + * Report a pending exception on aContext, if any. Note that this 1.53 + * can be called when the context has a JS stack. If that's the 1.54 + * case, the stack will be set aside before reporting the exception. 1.55 + */ 1.56 + static void ReportPendingException(JSContext *aContext); 1.57 + 1.58 + static nsresult CompileFunction(JSContext* aCx, 1.59 + JS::Handle<JSObject*> aTarget, 1.60 + JS::CompileOptions& aOptions, 1.61 + const nsACString& aName, 1.62 + uint32_t aArgCount, 1.63 + const char** aArgArray, 1.64 + const nsAString& aBody, 1.65 + JSObject** aFunctionObject); 1.66 + 1.67 + struct EvaluateOptions { 1.68 + bool coerceToString; 1.69 + bool reportUncaught; 1.70 + bool needResult; 1.71 + 1.72 + explicit EvaluateOptions() : coerceToString(false) 1.73 + , reportUncaught(true) 1.74 + , needResult(true) 1.75 + {} 1.76 + 1.77 + EvaluateOptions& setCoerceToString(bool aCoerce) { 1.78 + coerceToString = aCoerce; 1.79 + return *this; 1.80 + } 1.81 + 1.82 + EvaluateOptions& setReportUncaught(bool aReport) { 1.83 + reportUncaught = aReport; 1.84 + return *this; 1.85 + } 1.86 + 1.87 + EvaluateOptions& setNeedResult(bool aNeedResult) { 1.88 + needResult = aNeedResult; 1.89 + return *this; 1.90 + } 1.91 + }; 1.92 + 1.93 + static nsresult EvaluateString(JSContext* aCx, 1.94 + const nsAString& aScript, 1.95 + JS::Handle<JSObject*> aScopeObject, 1.96 + JS::CompileOptions &aCompileOptions, 1.97 + const EvaluateOptions& aEvaluateOptions, 1.98 + JS::MutableHandle<JS::Value> aRetValue, 1.99 + void **aOffThreadToken = nullptr); 1.100 + 1.101 + static nsresult EvaluateString(JSContext* aCx, 1.102 + JS::SourceBufferHolder& aSrcBuf, 1.103 + JS::Handle<JSObject*> aScopeObject, 1.104 + JS::CompileOptions &aCompileOptions, 1.105 + const EvaluateOptions& aEvaluateOptions, 1.106 + JS::MutableHandle<JS::Value> aRetValue, 1.107 + void **aOffThreadToken = nullptr); 1.108 + 1.109 + 1.110 + static nsresult EvaluateString(JSContext* aCx, 1.111 + const nsAString& aScript, 1.112 + JS::Handle<JSObject*> aScopeObject, 1.113 + JS::CompileOptions &aCompileOptions, 1.114 + void **aOffThreadToken = nullptr); 1.115 + 1.116 + static nsresult EvaluateString(JSContext* aCx, 1.117 + JS::SourceBufferHolder& aSrcBuf, 1.118 + JS::Handle<JSObject*> aScopeObject, 1.119 + JS::CompileOptions &aCompileOptions, 1.120 + void **aOffThreadToken = nullptr); 1.121 + 1.122 +}; 1.123 + 1.124 +class MOZ_STACK_CLASS AutoDontReportUncaught { 1.125 + JSContext* mContext; 1.126 + bool mWasSet; 1.127 + 1.128 +public: 1.129 + AutoDontReportUncaught(JSContext* aContext) : mContext(aContext) { 1.130 + MOZ_ASSERT(aContext); 1.131 + mWasSet = JS::ContextOptionsRef(mContext).dontReportUncaught(); 1.132 + if (!mWasSet) { 1.133 + JS::ContextOptionsRef(mContext).setDontReportUncaught(true); 1.134 + } 1.135 + } 1.136 + ~AutoDontReportUncaught() { 1.137 + if (!mWasSet) { 1.138 + JS::ContextOptionsRef(mContext).setDontReportUncaught(false); 1.139 + } 1.140 + } 1.141 +}; 1.142 + 1.143 + 1.144 +class nsDependentJSString : public nsDependentString 1.145 +{ 1.146 +public: 1.147 + /** 1.148 + * In the case of string ids, getting the string's chars is infallible, so 1.149 + * the dependent string can be constructed directly. 1.150 + */ 1.151 + explicit nsDependentJSString(JS::Handle<jsid> id) 1.152 + : nsDependentString(JS_GetInternedStringChars(JSID_TO_STRING(id)), 1.153 + JS_GetStringLength(JSID_TO_STRING(id))) 1.154 + { 1.155 + } 1.156 + 1.157 + /** 1.158 + * Ditto for flat strings. 1.159 + */ 1.160 + explicit nsDependentJSString(JSFlatString* fstr) 1.161 + : nsDependentString(JS_GetFlatStringChars(fstr), 1.162 + JS_GetStringLength(JS_FORGET_STRING_FLATNESS(fstr))) 1.163 + { 1.164 + } 1.165 + 1.166 + /** 1.167 + * For all other strings, the nsDependentJSString object should be default 1.168 + * constructed, which leaves it empty (this->IsEmpty()), and initialized with 1.169 + * one of the init() methods below. 1.170 + */ 1.171 + 1.172 + nsDependentJSString() 1.173 + { 1.174 + } 1.175 + 1.176 + bool init(JSContext* aContext, JSString* str) 1.177 + { 1.178 + size_t length; 1.179 + const jschar* chars = JS_GetStringCharsZAndLength(aContext, str, &length); 1.180 + if (!chars) 1.181 + return false; 1.182 + 1.183 + NS_ASSERTION(IsEmpty(), "init() on initialized string"); 1.184 + nsDependentString* base = this; 1.185 + new(base) nsDependentString(chars, length); 1.186 + return true; 1.187 + } 1.188 + 1.189 + bool init(JSContext* aContext, const JS::Value &v) 1.190 + { 1.191 + return init(aContext, JSVAL_TO_STRING(v)); 1.192 + } 1.193 + 1.194 + void init(JSFlatString* fstr) 1.195 + { 1.196 + MOZ_ASSERT(IsEmpty(), "init() on initialized string"); 1.197 + new(this) nsDependentJSString(fstr); 1.198 + } 1.199 + 1.200 + ~nsDependentJSString() 1.201 + { 1.202 + } 1.203 +}; 1.204 + 1.205 +#endif /* nsJSUtils_h__ */