1 // Copyright 2011 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.
7 #include "src/bootstrapper.h"
9 #include "src/scopeinfo.h"
14 Context* Context::declaration_context() {
15 Context* current = this;
16 while (!current->IsFunctionContext() && !current->IsNativeContext()) {
17 current = current->previous();
18 DCHECK(current->closure() == closure());
24 JSBuiltinsObject* Context::builtins() {
25 GlobalObject* object = global_object();
26 if (object->IsJSGlobalObject()) {
27 return JSGlobalObject::cast(object)->builtins();
29 DCHECK(object->IsJSBuiltinsObject());
30 return JSBuiltinsObject::cast(object);
35 Context* Context::global_context() {
36 Context* current = this;
37 while (!current->IsGlobalContext()) {
38 current = current->previous();
44 Context* Context::native_context() {
45 // Fast case: the global object for this context has been set. In
46 // that case, the global object has a direct pointer to the global
48 if (global_object()->IsGlobalObject()) {
49 return global_object()->native_context();
52 // During bootstrapping, the global object might not be set and we
53 // have to search the context chain to find the native context.
54 DCHECK(this->GetIsolate()->bootstrapper()->IsActive());
55 Context* current = this;
56 while (!current->IsNativeContext()) {
57 JSFunction* closure = JSFunction::cast(current->closure());
58 current = Context::cast(closure->context());
64 JSObject* Context::global_proxy() {
65 return native_context()->global_proxy_object();
69 void Context::set_global_proxy(JSObject* object) {
70 native_context()->set_global_proxy_object(object);
75 * Lookups a property in an object environment, taking the unscopables into
76 * account. This is used For HasBinding spec algorithms for ObjectEnvironment.
78 static Maybe<PropertyAttributes> UnscopableLookup(LookupIterator* it) {
79 Isolate* isolate = it->isolate();
81 Maybe<PropertyAttributes> attrs = JSReceiver::GetPropertyAttributes(it);
82 DCHECK(attrs.has_value || isolate->has_pending_exception());
83 if (!attrs.has_value || attrs.value == ABSENT) return attrs;
85 Handle<Symbol> unscopables_symbol(
86 isolate->native_context()->unscopables_symbol(), isolate);
87 Handle<Object> receiver = it->GetReceiver();
88 Handle<Object> unscopables;
89 MaybeHandle<Object> maybe_unscopables =
90 Object::GetProperty(receiver, unscopables_symbol);
91 if (!maybe_unscopables.ToHandle(&unscopables)) {
92 return Maybe<PropertyAttributes>();
94 if (!unscopables->IsSpecObject()) return attrs;
95 Maybe<bool> blacklist = JSReceiver::HasProperty(
96 Handle<JSReceiver>::cast(unscopables), it->name());
97 if (!blacklist.has_value) {
98 DCHECK(isolate->has_pending_exception());
99 return Maybe<PropertyAttributes>();
101 if (blacklist.value) return maybe(ABSENT);
106 Handle<Object> Context::Lookup(Handle<String> name,
107 ContextLookupFlags flags,
109 PropertyAttributes* attributes,
110 BindingFlags* binding_flags) {
111 Isolate* isolate = GetIsolate();
112 Handle<Context> context(this, isolate);
114 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
116 *attributes = ABSENT;
117 *binding_flags = MISSING_BINDING;
119 if (FLAG_trace_contexts) {
120 PrintF("Context::Lookup(");
126 if (FLAG_trace_contexts) {
127 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
128 if (context->IsNativeContext()) PrintF(" (native context)");
132 // 1. Check global objects, subjects of with, and extension objects.
133 if (context->IsNativeContext() ||
134 context->IsWithContext() ||
135 (context->IsFunctionContext() && context->has_extension())) {
136 Handle<JSReceiver> object(
137 JSReceiver::cast(context->extension()), isolate);
138 // Context extension objects needs to behave as if they have no
139 // prototype. So even if we want to follow prototype chains, we need
140 // to only do a local lookup for context extension objects.
141 Maybe<PropertyAttributes> maybe;
142 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
143 object->IsJSContextExtensionObject()) {
144 maybe = JSReceiver::GetOwnPropertyAttributes(object, name);
145 } else if (context->IsWithContext()) {
146 LookupIterator it(object, name);
147 maybe = UnscopableLookup(&it);
149 maybe = JSReceiver::GetPropertyAttributes(object, name);
152 if (!maybe.has_value) return Handle<Object>();
153 DCHECK(!isolate->has_pending_exception());
154 *attributes = maybe.value;
156 if (maybe.value != ABSENT) {
157 if (FLAG_trace_contexts) {
158 PrintF("=> found property in context object %p\n",
159 reinterpret_cast<void*>(*object));
165 // 2. Check the context proper if it has slots.
166 if (context->IsFunctionContext() || context->IsBlockContext()) {
167 // Use serialized scope information of functions and blocks to search
168 // for the context index.
169 Handle<ScopeInfo> scope_info;
170 if (context->IsFunctionContext()) {
171 scope_info = Handle<ScopeInfo>(
172 context->closure()->shared()->scope_info(), isolate);
174 scope_info = Handle<ScopeInfo>(
175 ScopeInfo::cast(context->extension()), isolate);
178 InitializationFlag init_flag;
179 // TODO(sigurds) Figure out whether maybe_assigned_flag should
180 // be used to compute binding_flags.
181 MaybeAssignedFlag maybe_assigned_flag;
182 int slot_index = ScopeInfo::ContextSlotIndex(
183 scope_info, name, &mode, &init_flag, &maybe_assigned_flag);
184 DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
185 if (slot_index >= 0) {
186 if (FLAG_trace_contexts) {
187 PrintF("=> found local in context slot %d (mode = %d)\n",
191 // Note: Fixed context slots are statically allocated by the compiler.
192 // Statically allocated variables always have a statically known mode,
193 // which is the mode with which they were declared when added to the
194 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
195 // declared variables that were introduced through declaration nodes)
196 // must not appear here.
198 case INTERNAL: // Fall through.
201 *binding_flags = MUTABLE_IS_INITIALIZED;
205 *binding_flags = (init_flag == kNeedsInitialization)
206 ? MUTABLE_CHECK_INITIALIZED : MUTABLE_IS_INITIALIZED;
209 *attributes = READ_ONLY;
210 *binding_flags = (init_flag == kNeedsInitialization)
211 ? IMMUTABLE_CHECK_INITIALIZED : IMMUTABLE_IS_INITIALIZED;
214 *attributes = READ_ONLY;
215 *binding_flags = (init_flag == kNeedsInitialization)
216 ? IMMUTABLE_CHECK_INITIALIZED_HARMONY :
217 IMMUTABLE_IS_INITIALIZED_HARMONY;
220 *attributes = READ_ONLY;
221 *binding_flags = IMMUTABLE_IS_INITIALIZED_HARMONY;
233 // Check the slot corresponding to the intermediate context holding
234 // only the function name variable.
235 if (follow_context_chain && context->IsFunctionContext()) {
237 int function_index = scope_info->FunctionContextSlotIndex(*name, &mode);
238 if (function_index >= 0) {
239 if (FLAG_trace_contexts) {
240 PrintF("=> found intermediate function in context slot %d\n",
243 *index = function_index;
244 *attributes = READ_ONLY;
245 DCHECK(mode == CONST_LEGACY || mode == CONST);
246 *binding_flags = (mode == CONST_LEGACY)
247 ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY;
252 } else if (context->IsCatchContext()) {
253 // Catch contexts have the variable name in the extension slot.
254 if (String::Equals(name, handle(String::cast(context->extension())))) {
255 if (FLAG_trace_contexts) {
256 PrintF("=> found in catch context\n");
258 *index = Context::THROWN_OBJECT_INDEX;
260 *binding_flags = MUTABLE_IS_INITIALIZED;
265 // 3. Prepare to continue with the previous (next outermost) context.
266 if (context->IsNativeContext()) {
267 follow_context_chain = false;
269 context = Handle<Context>(context->previous(), isolate);
271 } while (follow_context_chain);
273 if (FLAG_trace_contexts) {
274 PrintF("=> no property/slot found\n");
276 return Handle<Object>::null();
280 void Context::AddOptimizedFunction(JSFunction* function) {
281 DCHECK(IsNativeContext());
282 #ifdef ENABLE_SLOW_DCHECKS
283 if (FLAG_enable_slow_asserts) {
284 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
285 while (!element->IsUndefined()) {
286 CHECK(element != function);
287 element = JSFunction::cast(element)->next_function_link();
291 // Check that the context belongs to the weak native contexts list.
293 Object* context = GetHeap()->native_contexts_list();
294 while (!context->IsUndefined()) {
295 if (context == this) {
299 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
304 // If the function link field is already used then the function was
305 // enqueued as a code flushing candidate and we remove it now.
306 if (!function->next_function_link()->IsUndefined()) {
307 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
308 flusher->EvictCandidate(function);
311 DCHECK(function->next_function_link()->IsUndefined());
313 function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST));
314 set(OPTIMIZED_FUNCTIONS_LIST, function);
318 void Context::RemoveOptimizedFunction(JSFunction* function) {
319 DCHECK(IsNativeContext());
320 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
321 JSFunction* prev = NULL;
322 while (!element->IsUndefined()) {
323 JSFunction* element_function = JSFunction::cast(element);
324 DCHECK(element_function->next_function_link()->IsUndefined() ||
325 element_function->next_function_link()->IsJSFunction());
326 if (element_function == function) {
328 set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link());
330 prev->set_next_function_link(element_function->next_function_link());
332 element_function->set_next_function_link(GetHeap()->undefined_value());
335 prev = element_function;
336 element = element_function->next_function_link();
342 void Context::SetOptimizedFunctionsListHead(Object* head) {
343 DCHECK(IsNativeContext());
344 set(OPTIMIZED_FUNCTIONS_LIST, head);
348 Object* Context::OptimizedFunctionsListHead() {
349 DCHECK(IsNativeContext());
350 return get(OPTIMIZED_FUNCTIONS_LIST);
354 void Context::AddOptimizedCode(Code* code) {
355 DCHECK(IsNativeContext());
356 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
357 DCHECK(code->next_code_link()->IsUndefined());
358 code->set_next_code_link(get(OPTIMIZED_CODE_LIST));
359 set(OPTIMIZED_CODE_LIST, code);
363 void Context::SetOptimizedCodeListHead(Object* head) {
364 DCHECK(IsNativeContext());
365 set(OPTIMIZED_CODE_LIST, head);
369 Object* Context::OptimizedCodeListHead() {
370 DCHECK(IsNativeContext());
371 return get(OPTIMIZED_CODE_LIST);
375 void Context::SetDeoptimizedCodeListHead(Object* head) {
376 DCHECK(IsNativeContext());
377 set(DEOPTIMIZED_CODE_LIST, head);
381 Object* Context::DeoptimizedCodeListHead() {
382 DCHECK(IsNativeContext());
383 return get(DEOPTIMIZED_CODE_LIST);
387 Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() {
388 Isolate* isolate = GetIsolate();
389 Handle<Object> result(error_message_for_code_gen_from_strings(), isolate);
390 if (!result->IsUndefined()) return result;
391 return isolate->factory()->NewStringFromStaticChars(
392 "Code generation from strings disallowed for this context");
397 bool Context::IsBootstrappingOrValidParentContext(
398 Object* object, Context* child) {
399 // During bootstrapping we allow all objects to pass as
400 // contexts. This is necessary to fix circular dependencies.
401 if (child->GetIsolate()->bootstrapper()->IsActive()) return true;
402 if (!object->IsContext()) return false;
403 Context* context = Context::cast(object);
404 return context->IsNativeContext() || context->IsGlobalContext() ||
405 context->IsModuleContext() || !child->IsModuleContext();
409 bool Context::IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object) {
410 // During bootstrapping we allow all objects to pass as global
411 // objects. This is necessary to fix circular dependencies.
412 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
413 isolate->bootstrapper()->IsActive() ||
414 object->IsGlobalObject();
418 } } // namespace v8::internal