1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_CONTEXTS_INL_H_
6 #define V8_CONTEXTS_INL_H_
7 
8 #include "src/contexts.h"
9 #include "src/objects-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 
15 // static
cast(Object * context)16 ScriptContextTable* ScriptContextTable::cast(Object* context) {
17   DCHECK(context->IsScriptContextTable());
18   return reinterpret_cast<ScriptContextTable*>(context);
19 }
20 
21 
used()22 int ScriptContextTable::used() const {
23   return Smi::cast(get(kUsedSlot))->value();
24 }
25 
26 
set_used(int used)27 void ScriptContextTable::set_used(int used) {
28   set(kUsedSlot, Smi::FromInt(used));
29 }
30 
31 
32 // static
GetContext(Handle<ScriptContextTable> table,int i)33 Handle<Context> ScriptContextTable::GetContext(Handle<ScriptContextTable> table,
34                                                int i) {
35   DCHECK(i < table->used());
36   return Handle<Context>::cast(
37       FixedArray::get(*table, i + kFirstContextSlot, table->GetIsolate()));
38 }
39 
40 
41 // static
cast(Object * context)42 Context* Context::cast(Object* context) {
43   DCHECK(context->IsContext());
44   return reinterpret_cast<Context*>(context);
45 }
46 
47 
closure()48 JSFunction* Context::closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
set_closure(JSFunction * closure)49 void Context::set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
50 
51 
previous()52 Context* Context::previous() {
53   Object* result = get(PREVIOUS_INDEX);
54   DCHECK(IsBootstrappingOrValidParentContext(result, this));
55   return reinterpret_cast<Context*>(result);
56 }
set_previous(Context * context)57 void Context::set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
58 
next_context_link()59 Object* Context::next_context_link() { return get(Context::NEXT_CONTEXT_LINK); }
60 
has_extension()61 bool Context::has_extension() { return !extension()->IsTheHole(GetIsolate()); }
extension()62 HeapObject* Context::extension() {
63   return HeapObject::cast(get(EXTENSION_INDEX));
64 }
set_extension(HeapObject * object)65 void Context::set_extension(HeapObject* object) {
66   set(EXTENSION_INDEX, object);
67 }
68 
69 
native_context()70 Context* Context::native_context() {
71   Object* result = get(NATIVE_CONTEXT_INDEX);
72   DCHECK(IsBootstrappingOrNativeContext(this->GetIsolate(), result));
73   return reinterpret_cast<Context*>(result);
74 }
75 
76 
set_native_context(Context * context)77 void Context::set_native_context(Context* context) {
78   set(NATIVE_CONTEXT_INDEX, context);
79 }
80 
81 
IsNativeContext()82 bool Context::IsNativeContext() {
83   Map* map = this->map();
84   return map == map->GetHeap()->native_context_map();
85 }
86 
87 
IsFunctionContext()88 bool Context::IsFunctionContext() {
89   Map* map = this->map();
90   return map == map->GetHeap()->function_context_map();
91 }
92 
93 
IsCatchContext()94 bool Context::IsCatchContext() {
95   Map* map = this->map();
96   return map == map->GetHeap()->catch_context_map();
97 }
98 
99 
IsWithContext()100 bool Context::IsWithContext() {
101   Map* map = this->map();
102   return map == map->GetHeap()->with_context_map();
103 }
104 
IsDebugEvaluateContext()105 bool Context::IsDebugEvaluateContext() {
106   Map* map = this->map();
107   return map == map->GetHeap()->debug_evaluate_context_map();
108 }
109 
IsBlockContext()110 bool Context::IsBlockContext() {
111   Map* map = this->map();
112   return map == map->GetHeap()->block_context_map();
113 }
114 
115 
IsModuleContext()116 bool Context::IsModuleContext() {
117   Map* map = this->map();
118   return map == map->GetHeap()->module_context_map();
119 }
120 
121 
IsScriptContext()122 bool Context::IsScriptContext() {
123   Map* map = this->map();
124   return map == map->GetHeap()->script_context_map();
125 }
126 
127 
HasSameSecurityTokenAs(Context * that)128 bool Context::HasSameSecurityTokenAs(Context* that) {
129   return this->native_context()->security_token() ==
130          that->native_context()->security_token();
131 }
132 
133 
134 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
135   void Context::set_##name(type* value) {                 \
136     DCHECK(IsNativeContext());                            \
137     set(index, value);                                    \
138   }                                                       \
139   bool Context::is_##name(type* value) {                  \
140     DCHECK(IsNativeContext());                            \
141     return type::cast(get(index)) == value;               \
142   }                                                       \
143   type* Context::name() {                                 \
144     DCHECK(IsNativeContext());                            \
145     return type::cast(get(index));                        \
146   }
147 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
148 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
149 
150 
151 }  // namespace internal
152 }  // namespace v8
153 
154 #endif  // V8_CONTEXTS_INL_H_
155