blob: 344d5db5784cae76ea9c857ee38fa7103231867b [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
11namespace v8 {
12namespace internal {
13
14
15// static
16ScriptContextTable* ScriptContextTable::cast(Object* context) {
17 DCHECK(context->IsScriptContextTable());
18 return reinterpret_cast<ScriptContextTable*>(context);
19}
20
21
22int ScriptContextTable::used() const {
23 return Smi::cast(get(kUsedSlot))->value();
24}
25
26
27void ScriptContextTable::set_used(int used) {
28 set(kUsedSlot, Smi::FromInt(used));
29}
30
31
32// static
33Handle<Context> ScriptContextTable::GetContext(Handle<ScriptContextTable> table,
34 int i) {
35 DCHECK(i < table->used());
Ben Murdoch097c5b22016-05-18 11:27:45 +010036 return Handle<Context>::cast(
37 FixedArray::get(*table, i + kFirstContextSlot, table->GetIsolate()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038}
39
40
41// static
42Context* Context::cast(Object* context) {
43 DCHECK(context->IsContext());
44 return reinterpret_cast<Context*>(context);
45}
46
47
48JSFunction* Context::closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
49void Context::set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
50
51
52Context* Context::previous() {
53 Object* result = get(PREVIOUS_INDEX);
54 DCHECK(IsBootstrappingOrValidParentContext(result, this));
55 return reinterpret_cast<Context*>(result);
56}
57void Context::set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
58
59
60bool Context::has_extension() { return !extension()->IsTheHole(); }
61HeapObject* Context::extension() {
62 return HeapObject::cast(get(EXTENSION_INDEX));
63}
64void Context::set_extension(HeapObject* object) {
65 set(EXTENSION_INDEX, object);
66}
67
68
69JSModule* Context::module() { return JSModule::cast(get(EXTENSION_INDEX)); }
70void Context::set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
71
72
73Context* Context::native_context() {
74 Object* result = get(NATIVE_CONTEXT_INDEX);
75 DCHECK(IsBootstrappingOrNativeContext(this->GetIsolate(), result));
76 return reinterpret_cast<Context*>(result);
77}
78
79
80void Context::set_native_context(Context* context) {
81 set(NATIVE_CONTEXT_INDEX, context);
82}
83
84
85bool Context::IsNativeContext() {
86 Map* map = this->map();
87 return map == map->GetHeap()->native_context_map();
88}
89
90
91bool Context::IsFunctionContext() {
92 Map* map = this->map();
93 return map == map->GetHeap()->function_context_map();
94}
95
96
97bool Context::IsCatchContext() {
98 Map* map = this->map();
99 return map == map->GetHeap()->catch_context_map();
100}
101
102
103bool Context::IsWithContext() {
104 Map* map = this->map();
105 return map == map->GetHeap()->with_context_map();
106}
107
Ben Murdochda12d292016-06-02 14:46:10 +0100108bool Context::IsDebugEvaluateContext() {
109 Map* map = this->map();
110 return map == map->GetHeap()->debug_evaluate_context_map();
111}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112
113bool Context::IsBlockContext() {
114 Map* map = this->map();
115 return map == map->GetHeap()->block_context_map();
116}
117
118
119bool Context::IsModuleContext() {
120 Map* map = this->map();
121 return map == map->GetHeap()->module_context_map();
122}
123
124
125bool Context::IsScriptContext() {
126 Map* map = this->map();
127 return map == map->GetHeap()->script_context_map();
128}
129
130
131bool Context::HasSameSecurityTokenAs(Context* that) {
132 return this->native_context()->security_token() ==
133 that->native_context()->security_token();
134}
135
136
137#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
138 void Context::set_##name(type* value) { \
139 DCHECK(IsNativeContext()); \
140 set(index, value); \
141 } \
142 bool Context::is_##name(type* value) { \
143 DCHECK(IsNativeContext()); \
144 return type::cast(get(index)) == value; \
145 } \
146 type* Context::name() { \
147 DCHECK(IsNativeContext()); \
148 return type::cast(get(index)); \
149 }
150NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
151#undef NATIVE_CONTEXT_FIELD_ACCESSORS
152
153
154} // namespace internal
155} // namespace v8
156
157#endif // V8_CONTEXTS_INL_H_