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(FixedArray::get(table, i + kFirstContextSlot));
37 }
38
39
40 // static
cast(Object * context)41 Context* Context::cast(Object* context) {
42 DCHECK(context->IsContext());
43 return reinterpret_cast<Context*>(context);
44 }
45
46
closure()47 JSFunction* Context::closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
set_closure(JSFunction * closure)48 void Context::set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
49
50
previous()51 Context* Context::previous() {
52 Object* result = get(PREVIOUS_INDEX);
53 DCHECK(IsBootstrappingOrValidParentContext(result, this));
54 return reinterpret_cast<Context*>(result);
55 }
set_previous(Context * context)56 void Context::set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
57
58
has_extension()59 bool Context::has_extension() { return !extension()->IsTheHole(); }
extension()60 HeapObject* Context::extension() {
61 return HeapObject::cast(get(EXTENSION_INDEX));
62 }
set_extension(HeapObject * object)63 void Context::set_extension(HeapObject* object) {
64 set(EXTENSION_INDEX, object);
65 }
66
67
module()68 JSModule* Context::module() { return JSModule::cast(get(EXTENSION_INDEX)); }
set_module(JSModule * module)69 void Context::set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
70
71
native_context()72 Context* Context::native_context() {
73 Object* result = get(NATIVE_CONTEXT_INDEX);
74 DCHECK(IsBootstrappingOrNativeContext(this->GetIsolate(), result));
75 return reinterpret_cast<Context*>(result);
76 }
77
78
set_native_context(Context * context)79 void Context::set_native_context(Context* context) {
80 set(NATIVE_CONTEXT_INDEX, context);
81 }
82
83
IsNativeContext()84 bool Context::IsNativeContext() {
85 Map* map = this->map();
86 return map == map->GetHeap()->native_context_map();
87 }
88
89
IsFunctionContext()90 bool Context::IsFunctionContext() {
91 Map* map = this->map();
92 return map == map->GetHeap()->function_context_map();
93 }
94
95
IsCatchContext()96 bool Context::IsCatchContext() {
97 Map* map = this->map();
98 return map == map->GetHeap()->catch_context_map();
99 }
100
101
IsWithContext()102 bool Context::IsWithContext() {
103 Map* map = this->map();
104 return map == map->GetHeap()->with_context_map();
105 }
106
107
IsBlockContext()108 bool Context::IsBlockContext() {
109 Map* map = this->map();
110 return map == map->GetHeap()->block_context_map();
111 }
112
113
IsModuleContext()114 bool Context::IsModuleContext() {
115 Map* map = this->map();
116 return map == map->GetHeap()->module_context_map();
117 }
118
119
IsScriptContext()120 bool Context::IsScriptContext() {
121 Map* map = this->map();
122 return map == map->GetHeap()->script_context_map();
123 }
124
125
HasSameSecurityTokenAs(Context * that)126 bool Context::HasSameSecurityTokenAs(Context* that) {
127 return this->native_context()->security_token() ==
128 that->native_context()->security_token();
129 }
130
131
132 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
133 void Context::set_##name(type* value) { \
134 DCHECK(IsNativeContext()); \
135 set(index, value); \
136 } \
137 bool Context::is_##name(type* value) { \
138 DCHECK(IsNativeContext()); \
139 return type::cast(get(index)) == value; \
140 } \
141 type* Context::name() { \
142 DCHECK(IsNativeContext()); \
143 return type::cast(get(index)); \
144 }
145 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
146 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
147
148
149 } // namespace internal
150 } // namespace v8
151
152 #endif // V8_CONTEXTS_INL_H_
153