From 644beaf30bfedf45a6fdbb31a833532c672ba593 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 30 Oct 2014 17:40:09 -0400 Subject: [PATCH] Bug 1088228 part 2. Create an nsJSUtils API for building the scope chain for a given Element. r=peterv --- dom/base/nsJSUtils.cpp | 23 +++++++++++++++++++++++ dom/base/nsJSUtils.h | 6 ++++++ 2 files changed, 29 insertions(+) diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index 0132b4fc94c8..ddfb5bb084f8 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -27,6 +27,8 @@ #include "nsContentUtils.h" #include "nsGlobalWindow.h" +#include "mozilla/dom/BindingUtils.h" +#include "mozilla/dom/Element.h" #include "mozilla/dom/ScriptSettings.h" using namespace mozilla::dom; @@ -313,6 +315,27 @@ nsJSUtils::EvaluateString(JSContext* aCx, options, &unused, aOffThreadToken); } +/* static */ +bool +nsJSUtils::GetScopeChainForElement(JSContext* aCx, + mozilla::dom::Element* aElement, + JS::AutoObjectVector& aScopeChain) +{ + for (nsINode* cur = aElement; cur; cur = cur->GetScopeChainParent()) { + JS::RootedValue val(aCx); + if (!WrapNewBindingObject(aCx, cur, &val)) { + return false; + } + + if (!aScopeChain.append(&val.toObject())) { + return false; + } + } + + return true; +} + + // // nsDOMJSUtils.h // diff --git a/dom/base/nsJSUtils.h b/dom/base/nsJSUtils.h index d46a5a5b09dd..3442487229ff 100644 --- a/dom/base/nsJSUtils.h +++ b/dom/base/nsJSUtils.h @@ -25,6 +25,7 @@ class nsIScriptGlobalObject; namespace mozilla { namespace dom { class AutoJSAPI; +class Element; } } @@ -119,6 +120,11 @@ public: JS::CompileOptions &aCompileOptions, void **aOffThreadToken = nullptr); + // Returns false if an exception got thrown on aCx. Passing a null + // aElement is allowed; that wil produce an empty aScopeChain. + static bool GetScopeChainForElement(JSContext* aCx, + mozilla::dom::Element* aElement, + JS::AutoObjectVector& aScopeChain); }; class MOZ_STACK_CLASS AutoDontReportUncaught {