5 #ifndef V8_CONTEXTS_INL_H_ 6 #define V8_CONTEXTS_INL_H_ 8 #include "src/contexts.h" 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" 19 #include "src/objects/object-macros.h" 24 OBJECT_CONSTRUCTORS_IMPL(ScriptContextTable, FixedArray)
25 CAST_ACCESSOR2(ScriptContextTable)
27 int ScriptContextTable::used()
const {
return Smi::ToInt(
get(kUsedSlotIndex)); }
29 void ScriptContextTable::set_used(
int used) {
30 set(kUsedSlotIndex, Smi::FromInt(used));
34 Handle<Context> ScriptContextTable::GetContext(Isolate* isolate,
35 Handle<ScriptContextTable> table,
37 DCHECK(i < table->used());
38 return Handle<Context>::cast(
39 FixedArray::get(*table,
i + kFirstContextSlotIndex, isolate));
42 OBJECT_CONSTRUCTORS_IMPL(Context, HeapObjectPtr)
43 NEVER_READ_ONLY_SPACE_IMPL(Context)
44 CAST_ACCESSOR2(Context)
45 SMI_ACCESSORS(Context, length, kLengthOffset)
47 CAST_ACCESSOR2(NativeContext)
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));
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);
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);
71 void Context::set_scope_info(ScopeInfo scope_info) {
72 set(SCOPE_INFO_INDEX, scope_info);
75 Context Context::previous() {
76 Object* result =
get(PREVIOUS_INDEX);
77 DCHECK(IsBootstrappingOrValidParentContext(result, *
this));
78 return Context::unchecked_cast(result);
80 void Context::set_previous(Context context) {
set(PREVIOUS_INDEX, context); }
82 Object* Context::next_context_link() {
return get(Context::NEXT_CONTEXT_LINK); }
84 bool Context::has_extension() {
return !extension()->IsTheHole(); }
85 HeapObject* Context::extension() {
86 return HeapObject::cast(
get(EXTENSION_INDEX));
88 void Context::set_extension(HeapObject*
object) {
89 set(EXTENSION_INDEX, object);
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);
98 void Context::set_native_context(NativeContext context) {
99 set(NATIVE_CONTEXT_INDEX, context);
102 bool Context::IsFunctionContext()
const {
103 return map()->instance_type() == FUNCTION_CONTEXT_TYPE;
106 bool Context::IsCatchContext()
const {
107 return map()->instance_type() == CATCH_CONTEXT_TYPE;
110 bool Context::IsWithContext()
const {
111 return map()->instance_type() == WITH_CONTEXT_TYPE;
114 bool Context::IsDebugEvaluateContext()
const {
115 return map()->instance_type() == DEBUG_EVALUATE_CONTEXT_TYPE;
118 bool Context::IsAwaitContext()
const {
119 return map()->instance_type() == AWAIT_CONTEXT_TYPE;
122 bool Context::IsBlockContext()
const {
123 return map()->instance_type() == BLOCK_CONTEXT_TYPE;
126 bool Context::IsModuleContext()
const {
127 return map()->instance_type() == MODULE_CONTEXT_TYPE;
130 bool Context::IsEvalContext()
const {
131 return map()->instance_type() == EVAL_CONTEXT_TYPE;
134 bool Context::IsScriptContext()
const {
135 return map()->instance_type() == SCRIPT_CONTEXT_TYPE;
138 bool Context::HasSameSecurityTokenAs(Context that)
const {
139 return this->native_context()->security_token() ==
140 that->native_context()->security_token();
143 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \ 144 void Context::set_##name(type##ArgType value) { \ 145 DCHECK(IsNativeContext()); \ 148 bool Context::is_##name(type##ArgType value) const { \ 149 DCHECK(IsNativeContext()); \ 150 return type::cast(get(index)) == value; \ 152 type##ArgType Context::name() const { \ 153 DCHECK(IsNativeContext()); \ 154 return type::cast(get(index)); \ 156 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
157 #undef NATIVE_CONTEXT_FIELD_ACCESSORS 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) 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)) {
172 return CLASS_FUNCTION_MAP_INDEX;
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);
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);
187 base = IsAsyncFunction(kind) ? ASYNC_GENERATOR_FUNCTION_MAP_INDEX
188 : GENERATOR_FUNCTION_MAP_INDEX;
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);
195 base = ASYNC_FUNCTION_MAP_INDEX;
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);
205 base = STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
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);
214 base = is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
215 : SLOPPY_FUNCTION_MAP_INDEX;
217 int offset =
static_cast<int>(!has_shared_name) |
218 (static_cast<int>(needs_home_object) << 1);
219 DCHECK_EQ(0, offset & ~3);
221 return base + offset;
224 #undef CHECK_FOLLOWS2 225 #undef CHECK_FOLLOWS4 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);
236 MicrotaskQueue* NativeContext::microtask_queue()
const {
237 return reinterpret_cast<MicrotaskQueue*
>(
238 READ_INTPTR_FIELD(
this, kMicrotaskQueueOffset));
241 void NativeContext::set_microtask_queue(MicrotaskQueue* microtask_queue) {
242 WRITE_INTPTR_FIELD(
this, kMicrotaskQueueOffset,
243 reinterpret_cast<intptr_t>(microtask_queue));
246 OBJECT_CONSTRUCTORS_IMPL(NativeContext, Context)
251 #include "src/objects/object-macros-undef.h" 253 #endif // V8_CONTEXTS_INL_H_