blob: 67257ae0d71d794a9c75e92728223a498042069d [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());
36 return Handle<Context>::cast(FixedArray::get(table, i + kFirstContextSlot));
37}
38
39
40// static
41Context* Context::cast(Object* context) {
42 DCHECK(context->IsContext());
43 return reinterpret_cast<Context*>(context);
44}
45
46
47JSFunction* Context::closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
48void Context::set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
49
50
51Context* Context::previous() {
52 Object* result = get(PREVIOUS_INDEX);
53 DCHECK(IsBootstrappingOrValidParentContext(result, this));
54 return reinterpret_cast<Context*>(result);
55}
56void Context::set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
57
58
59bool Context::has_extension() { return !extension()->IsTheHole(); }
60HeapObject* Context::extension() {
61 return HeapObject::cast(get(EXTENSION_INDEX));
62}
63void Context::set_extension(HeapObject* object) {
64 set(EXTENSION_INDEX, object);
65}
66
67
68JSModule* Context::module() { return JSModule::cast(get(EXTENSION_INDEX)); }
69void Context::set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
70
71
72Context* 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
79void Context::set_native_context(Context* context) {
80 set(NATIVE_CONTEXT_INDEX, context);
81}
82
83
84bool Context::IsNativeContext() {
85 Map* map = this->map();
86 return map == map->GetHeap()->native_context_map();
87}
88
89
90bool Context::IsFunctionContext() {
91 Map* map = this->map();
92 return map == map->GetHeap()->function_context_map();
93}
94
95
96bool Context::IsCatchContext() {
97 Map* map = this->map();
98 return map == map->GetHeap()->catch_context_map();
99}
100
101
102bool Context::IsWithContext() {
103 Map* map = this->map();
104 return map == map->GetHeap()->with_context_map();
105}
106
107
108bool Context::IsBlockContext() {
109 Map* map = this->map();
110 return map == map->GetHeap()->block_context_map();
111}
112
113
114bool Context::IsModuleContext() {
115 Map* map = this->map();
116 return map == map->GetHeap()->module_context_map();
117}
118
119
120bool Context::IsScriptContext() {
121 Map* map = this->map();
122 return map == map->GetHeap()->script_context_map();
123}
124
125
126bool 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 }
145NATIVE_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_