V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
contexts-inl.h
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 
10 #include "src/heap/heap-write-barrier.h"
11 #include "src/objects-inl.h"
12 #include "src/objects/fixed-array-inl.h"
13 #include "src/objects/map-inl.h"
14 #include "src/objects/regexp-match-info.h"
15 #include "src/objects/scope-info.h"
16 #include "src/objects/shared-function-info.h"
17 
18 // Has to be the last include (doesn't have include guards):
19 #include "src/objects/object-macros.h"
20 
21 namespace v8 {
22 namespace internal {
23 
24 OBJECT_CONSTRUCTORS_IMPL(ScriptContextTable, FixedArray)
25 CAST_ACCESSOR2(ScriptContextTable)
26 
27 int ScriptContextTable::used() const { return Smi::ToInt(get(kUsedSlotIndex)); }
28 
29 void ScriptContextTable::set_used(int used) {
30  set(kUsedSlotIndex, Smi::FromInt(used));
31 }
32 
33 // static
34 Handle<Context> ScriptContextTable::GetContext(Isolate* isolate,
35  Handle<ScriptContextTable> table,
36  int i) {
37  DCHECK(i < table->used());
38  return Handle<Context>::cast(
39  FixedArray::get(*table, i + kFirstContextSlotIndex, isolate));
40 }
41 
42 OBJECT_CONSTRUCTORS_IMPL(Context, HeapObjectPtr)
43 NEVER_READ_ONLY_SPACE_IMPL(Context)
44 CAST_ACCESSOR2(Context)
45 SMI_ACCESSORS(Context, length, kLengthOffset)
46 
47 CAST_ACCESSOR2(NativeContext)
48 
49 Object* Context::get(int index) const {
50  DCHECK_LT(static_cast<unsigned>(index),
51  static_cast<unsigned>(this->length()));
52  return RELAXED_READ_FIELD(this, OffsetOfElementAt(index));
53 }
54 
55 void Context::set(int index, Object* value) {
56  DCHECK_LT(static_cast<unsigned>(index),
57  static_cast<unsigned>(this->length()));
58  int offset = OffsetOfElementAt(index);
59  RELAXED_WRITE_FIELD(this, offset, value);
60  WRITE_BARRIER(this, offset, value);
61 }
62 
63 void Context::set(int index, Object* value, WriteBarrierMode mode) {
64  DCHECK_LT(static_cast<unsigned>(index),
65  static_cast<unsigned>(this->length()));
66  int offset = OffsetOfElementAt(index);
67  RELAXED_WRITE_FIELD(this, offset, value);
68  CONDITIONAL_WRITE_BARRIER(this, offset, value, mode);
69 }
70 
71 void Context::set_scope_info(ScopeInfo scope_info) {
72  set(SCOPE_INFO_INDEX, scope_info);
73 }
74 
75 Context Context::previous() {
76  Object* result = get(PREVIOUS_INDEX);
77  DCHECK(IsBootstrappingOrValidParentContext(result, *this));
78  return Context::unchecked_cast(result);
79 }
80 void Context::set_previous(Context context) { set(PREVIOUS_INDEX, context); }
81 
82 Object* Context::next_context_link() { return get(Context::NEXT_CONTEXT_LINK); }
83 
84 bool Context::has_extension() { return !extension()->IsTheHole(); }
85 HeapObject* Context::extension() {
86  return HeapObject::cast(get(EXTENSION_INDEX));
87 }
88 void Context::set_extension(HeapObject* object) {
89  set(EXTENSION_INDEX, object);
90 }
91 
92 NativeContext Context::native_context() const {
93  Object* result = get(NATIVE_CONTEXT_INDEX);
94  DCHECK(IsBootstrappingOrNativeContext(this->GetIsolate(), result));
95  return NativeContext::unchecked_cast(result);
96 }
97 
98 void Context::set_native_context(NativeContext context) {
99  set(NATIVE_CONTEXT_INDEX, context);
100 }
101 
102 bool Context::IsFunctionContext() const {
103  return map()->instance_type() == FUNCTION_CONTEXT_TYPE;
104 }
105 
106 bool Context::IsCatchContext() const {
107  return map()->instance_type() == CATCH_CONTEXT_TYPE;
108 }
109 
110 bool Context::IsWithContext() const {
111  return map()->instance_type() == WITH_CONTEXT_TYPE;
112 }
113 
114 bool Context::IsDebugEvaluateContext() const {
115  return map()->instance_type() == DEBUG_EVALUATE_CONTEXT_TYPE;
116 }
117 
118 bool Context::IsAwaitContext() const {
119  return map()->instance_type() == AWAIT_CONTEXT_TYPE;
120 }
121 
122 bool Context::IsBlockContext() const {
123  return map()->instance_type() == BLOCK_CONTEXT_TYPE;
124 }
125 
126 bool Context::IsModuleContext() const {
127  return map()->instance_type() == MODULE_CONTEXT_TYPE;
128 }
129 
130 bool Context::IsEvalContext() const {
131  return map()->instance_type() == EVAL_CONTEXT_TYPE;
132 }
133 
134 bool Context::IsScriptContext() const {
135  return map()->instance_type() == SCRIPT_CONTEXT_TYPE;
136 }
137 
138 bool Context::HasSameSecurityTokenAs(Context that) const {
139  return this->native_context()->security_token() ==
140  that->native_context()->security_token();
141 }
142 
143 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
144  void Context::set_##name(type##ArgType value) { \
145  DCHECK(IsNativeContext()); \
146  set(index, value); \
147  } \
148  bool Context::is_##name(type##ArgType value) const { \
149  DCHECK(IsNativeContext()); \
150  return type::cast(get(index)) == value; \
151  } \
152  type##ArgType Context::name() const { \
153  DCHECK(IsNativeContext()); \
154  return type::cast(get(index)); \
155  }
156 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
157 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
158 
159 #define CHECK_FOLLOWS2(v1, v2) STATIC_ASSERT((v1 + 1) == (v2))
160 #define CHECK_FOLLOWS4(v1, v2, v3, v4) \
161  CHECK_FOLLOWS2(v1, v2); \
162  CHECK_FOLLOWS2(v2, v3); \
163  CHECK_FOLLOWS2(v3, v4)
164 
165 int Context::FunctionMapIndex(LanguageMode language_mode, FunctionKind kind,
166  bool has_prototype_slot, bool has_shared_name,
167  bool needs_home_object) {
168  if (IsClassConstructor(kind)) {
169  // Like the strict function map, but with no 'name' accessor. 'name'
170  // needs to be the last property and it is added during instantiation,
171  // in case a static property with the same name exists"
172  return CLASS_FUNCTION_MAP_INDEX;
173  }
174 
175  int base = 0;
176  if (IsGeneratorFunction(kind)) {
177  CHECK_FOLLOWS4(GENERATOR_FUNCTION_MAP_INDEX,
178  GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX,
179  GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
180  GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
181  CHECK_FOLLOWS4(
182  ASYNC_GENERATOR_FUNCTION_MAP_INDEX,
183  ASYNC_GENERATOR_FUNCTION_WITH_NAME_MAP_INDEX,
184  ASYNC_GENERATOR_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
185  ASYNC_GENERATOR_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
186 
187  base = IsAsyncFunction(kind) ? ASYNC_GENERATOR_FUNCTION_MAP_INDEX
188  : GENERATOR_FUNCTION_MAP_INDEX;
189 
190  } else if (IsAsyncFunction(kind)) {
191  CHECK_FOLLOWS4(ASYNC_FUNCTION_MAP_INDEX, ASYNC_FUNCTION_WITH_NAME_MAP_INDEX,
192  ASYNC_FUNCTION_WITH_HOME_OBJECT_MAP_INDEX,
193  ASYNC_FUNCTION_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
194 
195  base = ASYNC_FUNCTION_MAP_INDEX;
196 
197  } else if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
198  IsAccessorFunction(kind)) {
199  DCHECK_IMPLIES(IsArrowFunction(kind), !needs_home_object);
200  CHECK_FOLLOWS4(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
201  METHOD_WITH_NAME_MAP_INDEX,
202  METHOD_WITH_HOME_OBJECT_MAP_INDEX,
203  METHOD_WITH_NAME_AND_HOME_OBJECT_MAP_INDEX);
204 
205  base = STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
206 
207  } else {
208  DCHECK(!needs_home_object);
209  CHECK_FOLLOWS2(SLOPPY_FUNCTION_MAP_INDEX,
210  SLOPPY_FUNCTION_WITH_NAME_MAP_INDEX);
211  CHECK_FOLLOWS2(STRICT_FUNCTION_MAP_INDEX,
212  STRICT_FUNCTION_WITH_NAME_MAP_INDEX);
213 
214  base = is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
215  : SLOPPY_FUNCTION_MAP_INDEX;
216  }
217  int offset = static_cast<int>(!has_shared_name) |
218  (static_cast<int>(needs_home_object) << 1);
219  DCHECK_EQ(0, offset & ~3);
220 
221  return base + offset;
222 }
223 
224 #undef CHECK_FOLLOWS2
225 #undef CHECK_FOLLOWS4
226 
227 Map Context::GetInitialJSArrayMap(ElementsKind kind) const {
228  DCHECK(IsNativeContext());
229  if (!IsFastElementsKind(kind)) return Map();
230  DisallowHeapAllocation no_gc;
231  Object* const initial_js_array_map = get(Context::ArrayMapIndex(kind));
232  DCHECK(!initial_js_array_map->IsUndefined());
233  return Map::cast(initial_js_array_map);
234 }
235 
236 MicrotaskQueue* NativeContext::microtask_queue() const {
237  return reinterpret_cast<MicrotaskQueue*>(
238  READ_INTPTR_FIELD(this, kMicrotaskQueueOffset));
239 }
240 
241 void NativeContext::set_microtask_queue(MicrotaskQueue* microtask_queue) {
242  WRITE_INTPTR_FIELD(this, kMicrotaskQueueOffset,
243  reinterpret_cast<intptr_t>(microtask_queue));
244 }
245 
246 OBJECT_CONSTRUCTORS_IMPL(NativeContext, Context)
247 
248 } // namespace internal
249 } // namespace v8
250 
251 #include "src/objects/object-macros-undef.h"
252 
253 #endif // V8_CONTEXTS_INL_H_
Definition: libplatform.h:13