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
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());
36  return Handle<Context>::cast(
37      FixedArray::get(*table, i + kFirstContextSlot, table->GetIsolate()));
38}
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
59Object* Context::next_context_link() { return get(Context::NEXT_CONTEXT_LINK); }
60
61bool Context::has_extension() { return !extension()->IsTheHole(GetIsolate()); }
62HeapObject* Context::extension() {
63  return HeapObject::cast(get(EXTENSION_INDEX));
64}
65void Context::set_extension(HeapObject* object) {
66  set(EXTENSION_INDEX, object);
67}
68
69
70JSModule* Context::module() { return JSModule::cast(get(EXTENSION_INDEX)); }
71void Context::set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
72
73
74Context* 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
81void Context::set_native_context(Context* context) {
82  set(NATIVE_CONTEXT_INDEX, context);
83}
84
85
86bool Context::IsNativeContext() {
87  Map* map = this->map();
88  return map == map->GetHeap()->native_context_map();
89}
90
91
92bool Context::IsFunctionContext() {
93  Map* map = this->map();
94  return map == map->GetHeap()->function_context_map();
95}
96
97
98bool Context::IsCatchContext() {
99  Map* map = this->map();
100  return map == map->GetHeap()->catch_context_map();
101}
102
103
104bool Context::IsWithContext() {
105  Map* map = this->map();
106  return map == map->GetHeap()->with_context_map();
107}
108
109bool Context::IsDebugEvaluateContext() {
110  Map* map = this->map();
111  return map == map->GetHeap()->debug_evaluate_context_map();
112}
113
114bool Context::IsBlockContext() {
115  Map* map = this->map();
116  return map == map->GetHeap()->block_context_map();
117}
118
119
120bool Context::IsModuleContext() {
121  Map* map = this->map();
122  return map == map->GetHeap()->module_context_map();
123}
124
125
126bool Context::IsScriptContext() {
127  Map* map = this->map();
128  return map == map->GetHeap()->script_context_map();
129}
130
131
132bool 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  }
151NATIVE_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