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