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
module()70 JSModule* Context::module() { return JSModule::cast(get(EXTENSION_INDEX)); }
set_module(JSModule * module)71 void Context::set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
72
73
native_context()74 Context* Context::native_context() {
75 Object* result = get(NATIVE_CONTEXT_INDEX);
76 DCHECK(IsBootstrappingOrNativeContext(this->GetIsolate(), result));
77 return reinterpret_cast<Context*>(result);
78 }
79
80
set_native_context(Context * context)81 void Context::set_native_context(Context* context) {
82 set(NATIVE_CONTEXT_INDEX, context);
83 }
84
85
IsNativeContext()86 bool Context::IsNativeContext() {
87 Map* map = this->map();
88 return map == map->GetHeap()->native_context_map();
89 }
90
91
IsFunctionContext()92 bool Context::IsFunctionContext() {
93 Map* map = this->map();
94 return map == map->GetHeap()->function_context_map();
95 }
96
97
IsCatchContext()98 bool Context::IsCatchContext() {
99 Map* map = this->map();
100 return map == map->GetHeap()->catch_context_map();
101 }
102
103
IsWithContext()104 bool Context::IsWithContext() {
105 Map* map = this->map();
106 return map == map->GetHeap()->with_context_map();
107 }
108
IsDebugEvaluateContext()109 bool Context::IsDebugEvaluateContext() {
110 Map* map = this->map();
111 return map == map->GetHeap()->debug_evaluate_context_map();
112 }
113
IsBlockContext()114 bool Context::IsBlockContext() {
115 Map* map = this->map();
116 return map == map->GetHeap()->block_context_map();
117 }
118
119
IsModuleContext()120 bool Context::IsModuleContext() {
121 Map* map = this->map();
122 return map == map->GetHeap()->module_context_map();
123 }
124
125
IsScriptContext()126 bool Context::IsScriptContext() {
127 Map* map = this->map();
128 return map == map->GetHeap()->script_context_map();
129 }
130
131
HasSameSecurityTokenAs(Context * that)132 bool Context::HasSameSecurityTokenAs(Context* that) {
133 return this->native_context()->security_token() ==
134 that->native_context()->security_token();
135 }
136
137
138 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
139 void Context::set_##name(type* value) { \
140 DCHECK(IsNativeContext()); \
141 set(index, value); \
142 } \
143 bool Context::is_##name(type* value) { \
144 DCHECK(IsNativeContext()); \
145 return type::cast(get(index)) == value; \
146 } \
147 type* Context::name() { \
148 DCHECK(IsNativeContext()); \
149 return type::cast(get(index)); \
150 }
151 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
152 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
153
154
155 } // namespace internal
156 } // namespace v8
157
158 #endif // V8_CONTEXTS_INL_H_
159