EnvironmentChain.h (2325B)
1 /* -*- Mode: C++; tab-width: 8; 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/. */ 5 6 #ifndef js_EnvironmentChain_h 7 #define js_EnvironmentChain_h 8 9 #include "mozilla/Attributes.h" // MOZ_RAII 10 11 #include <stddef.h> // size_t 12 13 #include "jstypes.h" // JS_PUBLIC_API 14 15 #include "js/GCVector.h" // JS::RootedVector 16 17 struct JS_PUBLIC_API JSContext; 18 class JS_PUBLIC_API JSObject; 19 20 namespace JS { 21 22 enum class SupportUnscopables : bool { No = false, Yes = true }; 23 24 /** 25 * JS::EnvironmentChain stores a list of objects to put on the environment 26 * chain. 27 * 28 * Internally the engine will create a non-syntactic 'with' environment for each 29 * of these objects. Note that 'with' environments aren't optimized well so you 30 * should use this class only if you really have to. 31 * 32 * The SupportUnscopables enum class controls whether these non-syntactic 'with' 33 * environments support Symbol.unscopables similar to syntactic 'with' 34 * statements in JS. 35 * 36 * Passing SupportUnscopables::No is better for performance because it lets us 37 * skip the Symbol.unscopables property lookup. Some Web APIs require supporting 38 * Symbol.unscopables though. In Firefox, SupportUnscopables::Yes is used for 39 * event handlers. 40 */ 41 class MOZ_RAII JS_PUBLIC_API EnvironmentChain { 42 JS::RootedObjectVector chain_; 43 SupportUnscopables supportUnscopables_; 44 45 public: 46 EnvironmentChain(JSContext* cx, SupportUnscopables supportUnscopables) 47 : chain_(cx), supportUnscopables_(supportUnscopables) {} 48 49 EnvironmentChain(const EnvironmentChain&) = delete; 50 void operator=(const EnvironmentChain&) = delete; 51 52 [[nodiscard]] bool append(JSObject* obj) { return chain_.append(obj); } 53 bool empty() const { return chain_.empty(); } 54 size_t length() const { return chain_.length(); } 55 56 RootedObjectVector& chain() { return chain_; } 57 const RootedObjectVector& chain() const { return chain_; } 58 59 void setSupportUnscopables(SupportUnscopables supportUnscopables) { 60 supportUnscopables_ = supportUnscopables; 61 } 62 SupportUnscopables supportUnscopables() const { return supportUnscopables_; } 63 }; 64 65 } // namespace JS 66 67 #endif /* js_EnvironmentChain_h */