1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef vm_EnvironmentObject_inl_h
8 #define vm_EnvironmentObject_inl_h
9 
10 #include "vm/EnvironmentObject.h"
11 
12 #include "vm/JSObject-inl.h"
13 
14 namespace js {
15 
16 inline ExtensibleLexicalEnvironmentObject&
NearestEnclosingExtensibleLexicalEnvironment(JSObject * env)17 NearestEnclosingExtensibleLexicalEnvironment(JSObject* env) {
18   MOZ_ASSERT(env);
19   while (!env->is<ExtensibleLexicalEnvironmentObject>()) {
20     env = env->enclosingEnvironment();
21     MOZ_ASSERT(env);
22   }
23   return env->as<ExtensibleLexicalEnvironmentObject>();
24 }
25 
26 // Returns the innermost "qualified var object" on the environment chain.
27 // See the JSObject::isQualifiedVarObj comment for more info.
GetVariablesObject(JSObject * envChain)28 inline JSObject& GetVariablesObject(JSObject* envChain) {
29   while (!envChain->isQualifiedVarObj()) {
30     envChain = envChain->enclosingEnvironment();
31   }
32   MOZ_ASSERT(envChain);
33   return *envChain;
34 }
35 
aliasedBinding(EnvironmentCoordinate ec)36 inline const Value& EnvironmentObject::aliasedBinding(
37     EnvironmentCoordinate ec) {
38   MOZ_ASSERT(!IsExtensibleLexicalEnvironment(this));
39   MOZ_ASSERT(nonExtensibleIsFixedSlot(ec) ==
40              NativeObject::isFixedSlot(ec.slot()));
41   return getSlot(ec.slot());
42 }
43 
setAliasedBinding(JSContext * cx,uint32_t slot,const Value & v)44 inline void EnvironmentObject::setAliasedBinding(JSContext* cx, uint32_t slot,
45                                                  const Value& v) {
46   setSlot(slot, v);
47 }
48 
setAliasedBinding(JSContext * cx,EnvironmentCoordinate ec,const Value & v)49 inline void EnvironmentObject::setAliasedBinding(JSContext* cx,
50                                                  EnvironmentCoordinate ec,
51                                                  const Value& v) {
52   MOZ_ASSERT(!IsExtensibleLexicalEnvironment(this));
53   MOZ_ASSERT(nonExtensibleIsFixedSlot(ec) ==
54              NativeObject::isFixedSlot(ec.slot()));
55   setAliasedBinding(cx, ec.slot(), v);
56 }
57 
setAliasedBinding(JSContext * cx,const BindingIter & bi,const Value & v)58 inline void EnvironmentObject::setAliasedBinding(JSContext* cx,
59                                                  const BindingIter& bi,
60                                                  const Value& v) {
61   MOZ_ASSERT(bi.location().kind() == BindingLocation::Kind::Environment);
62   setAliasedBinding(cx, bi.location().slot(), v);
63 }
64 
setAliasedFormalFromArguments(const Value & argsValue,const Value & v)65 inline void CallObject::setAliasedFormalFromArguments(const Value& argsValue,
66                                                       const Value& v) {
67   setSlot(ArgumentsObject::SlotFromMagicScopeSlotValue(argsValue), v);
68 }
69 
70 } /* namespace js */
71 
enclosingEnvironment()72 inline JSObject* JSObject::enclosingEnvironment() const {
73   if (is<js::EnvironmentObject>()) {
74     return &as<js::EnvironmentObject>().enclosingEnvironment();
75   }
76 
77   if (is<js::DebugEnvironmentProxy>()) {
78     return &as<js::DebugEnvironmentProxy>().enclosingEnvironment();
79   }
80 
81   if (is<js::GlobalObject>()) {
82     return nullptr;
83   }
84 
85   MOZ_ASSERT_IF(is<JSFunction>(), as<JSFunction>().isInterpreted());
86   return &nonCCWGlobal();
87 }
88 
89 #endif /* vm_EnvironmentObject_inl_h */
90