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