michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsJSUtils_h__ michael@0: #define nsJSUtils_h__ michael@0: michael@0: /** michael@0: * This is not a generated file. It contains common utility functions michael@0: * invoked from the JavaScript code generated from IDL interfaces. michael@0: * The goal of the utility functions is to cut down on the size of michael@0: * the generated code itself. michael@0: */ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: michael@0: #include "jsapi.h" michael@0: #include "nsString.h" michael@0: michael@0: class nsIScriptContext; michael@0: class nsIScriptGlobalObject; michael@0: michael@0: class nsJSUtils michael@0: { michael@0: public: michael@0: static bool GetCallingLocation(JSContext* aContext, const char* *aFilename, michael@0: uint32_t* aLineno); michael@0: michael@0: static nsIScriptGlobalObject *GetStaticScriptGlobal(JSObject* aObj); michael@0: michael@0: static nsIScriptContext *GetStaticScriptContext(JSObject* aObj); michael@0: michael@0: static nsIScriptGlobalObject *GetDynamicScriptGlobal(JSContext *aContext); michael@0: michael@0: static nsIScriptContext *GetDynamicScriptContext(JSContext *aContext); michael@0: michael@0: /** michael@0: * Retrieve the inner window ID based on the given JSContext. michael@0: * michael@0: * @param JSContext aContext michael@0: * The JSContext from which you want to find the inner window ID. michael@0: * michael@0: * @returns uint64_t the inner window ID. michael@0: */ michael@0: static uint64_t GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext); michael@0: michael@0: /** michael@0: * Report a pending exception on aContext, if any. Note that this michael@0: * can be called when the context has a JS stack. If that's the michael@0: * case, the stack will be set aside before reporting the exception. michael@0: */ michael@0: static void ReportPendingException(JSContext *aContext); michael@0: michael@0: static nsresult CompileFunction(JSContext* aCx, michael@0: JS::Handle aTarget, michael@0: JS::CompileOptions& aOptions, michael@0: const nsACString& aName, michael@0: uint32_t aArgCount, michael@0: const char** aArgArray, michael@0: const nsAString& aBody, michael@0: JSObject** aFunctionObject); michael@0: michael@0: struct EvaluateOptions { michael@0: bool coerceToString; michael@0: bool reportUncaught; michael@0: bool needResult; michael@0: michael@0: explicit EvaluateOptions() : coerceToString(false) michael@0: , reportUncaught(true) michael@0: , needResult(true) michael@0: {} michael@0: michael@0: EvaluateOptions& setCoerceToString(bool aCoerce) { michael@0: coerceToString = aCoerce; michael@0: return *this; michael@0: } michael@0: michael@0: EvaluateOptions& setReportUncaught(bool aReport) { michael@0: reportUncaught = aReport; michael@0: return *this; michael@0: } michael@0: michael@0: EvaluateOptions& setNeedResult(bool aNeedResult) { michael@0: needResult = aNeedResult; michael@0: return *this; michael@0: } michael@0: }; michael@0: michael@0: static nsresult EvaluateString(JSContext* aCx, michael@0: const nsAString& aScript, michael@0: JS::Handle aScopeObject, michael@0: JS::CompileOptions &aCompileOptions, michael@0: const EvaluateOptions& aEvaluateOptions, michael@0: JS::MutableHandle aRetValue, michael@0: void **aOffThreadToken = nullptr); michael@0: michael@0: static nsresult EvaluateString(JSContext* aCx, michael@0: JS::SourceBufferHolder& aSrcBuf, michael@0: JS::Handle aScopeObject, michael@0: JS::CompileOptions &aCompileOptions, michael@0: const EvaluateOptions& aEvaluateOptions, michael@0: JS::MutableHandle aRetValue, michael@0: void **aOffThreadToken = nullptr); michael@0: michael@0: michael@0: static nsresult EvaluateString(JSContext* aCx, michael@0: const nsAString& aScript, michael@0: JS::Handle aScopeObject, michael@0: JS::CompileOptions &aCompileOptions, michael@0: void **aOffThreadToken = nullptr); michael@0: michael@0: static nsresult EvaluateString(JSContext* aCx, michael@0: JS::SourceBufferHolder& aSrcBuf, michael@0: JS::Handle aScopeObject, michael@0: JS::CompileOptions &aCompileOptions, michael@0: void **aOffThreadToken = nullptr); michael@0: michael@0: }; michael@0: michael@0: class MOZ_STACK_CLASS AutoDontReportUncaught { michael@0: JSContext* mContext; michael@0: bool mWasSet; michael@0: michael@0: public: michael@0: AutoDontReportUncaught(JSContext* aContext) : mContext(aContext) { michael@0: MOZ_ASSERT(aContext); michael@0: mWasSet = JS::ContextOptionsRef(mContext).dontReportUncaught(); michael@0: if (!mWasSet) { michael@0: JS::ContextOptionsRef(mContext).setDontReportUncaught(true); michael@0: } michael@0: } michael@0: ~AutoDontReportUncaught() { michael@0: if (!mWasSet) { michael@0: JS::ContextOptionsRef(mContext).setDontReportUncaught(false); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: michael@0: class nsDependentJSString : public nsDependentString michael@0: { michael@0: public: michael@0: /** michael@0: * In the case of string ids, getting the string's chars is infallible, so michael@0: * the dependent string can be constructed directly. michael@0: */ michael@0: explicit nsDependentJSString(JS::Handle id) michael@0: : nsDependentString(JS_GetInternedStringChars(JSID_TO_STRING(id)), michael@0: JS_GetStringLength(JSID_TO_STRING(id))) michael@0: { michael@0: } michael@0: michael@0: /** michael@0: * Ditto for flat strings. michael@0: */ michael@0: explicit nsDependentJSString(JSFlatString* fstr) michael@0: : nsDependentString(JS_GetFlatStringChars(fstr), michael@0: JS_GetStringLength(JS_FORGET_STRING_FLATNESS(fstr))) michael@0: { michael@0: } michael@0: michael@0: /** michael@0: * For all other strings, the nsDependentJSString object should be default michael@0: * constructed, which leaves it empty (this->IsEmpty()), and initialized with michael@0: * one of the init() methods below. michael@0: */ michael@0: michael@0: nsDependentJSString() michael@0: { michael@0: } michael@0: michael@0: bool init(JSContext* aContext, JSString* str) michael@0: { michael@0: size_t length; michael@0: const jschar* chars = JS_GetStringCharsZAndLength(aContext, str, &length); michael@0: if (!chars) michael@0: return false; michael@0: michael@0: NS_ASSERTION(IsEmpty(), "init() on initialized string"); michael@0: nsDependentString* base = this; michael@0: new(base) nsDependentString(chars, length); michael@0: return true; michael@0: } michael@0: michael@0: bool init(JSContext* aContext, const JS::Value &v) michael@0: { michael@0: return init(aContext, JSVAL_TO_STRING(v)); michael@0: } michael@0: michael@0: void init(JSFlatString* fstr) michael@0: { michael@0: MOZ_ASSERT(IsEmpty(), "init() on initialized string"); michael@0: new(this) nsDependentJSString(fstr); michael@0: } michael@0: michael@0: ~nsDependentJSString() michael@0: { michael@0: } michael@0: }; michael@0: michael@0: #endif /* nsJSUtils_h__ */