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"
15 Handle<ScriptContextTable> ScriptContextTable::Extend(
16 Handle<ScriptContextTable> table, Handle<Context> script_context) {
17 Handle<ScriptContextTable> result;
18 int used = table->used();
19 int length = table->length();
20 CHECK(used >= 0 && length > 0 && used < length);
21 if (used + 1 == length) {
22 CHECK(length < Smi::kMaxValue / 2);
23 result = Handle<ScriptContextTable>::cast(
24 FixedArray::CopySize(table, length * 2));
28 result->set_used(used + 1);
30 DCHECK(script_context->IsScriptContext());
31 result->set(used + 1, *script_context);
36 bool ScriptContextTable::Lookup(Handle<ScriptContextTable> table,
37 Handle<String> name, LookupResult* result) {
38 for (int i = 0; i < table->used(); i++) {
39 Handle<Context> context = GetContext(table, i);
40 DCHECK(context->IsScriptContext());
41 Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension()));
42 int slot_index = ScopeInfo::ContextSlotIndex(
43 scope_info, name, &result->mode, &result->location, &result->init_flag,
44 &result->maybe_assigned_flag);
46 if (slot_index >= 0 && result->location == VariableLocation::CONTEXT) {
47 result->context_index = i;
48 result->slot_index = slot_index;
56 Context* Context::declaration_context() {
57 Context* current = this;
58 while (!current->IsFunctionContext() && !current->IsNativeContext() &&
59 !current->IsScriptContext()) {
60 current = current->previous();
61 DCHECK(current->closure() == closure());
67 JSBuiltinsObject* Context::builtins() {
68 GlobalObject* object = global_object();
69 if (object->IsJSGlobalObject()) {
70 return JSGlobalObject::cast(object)->builtins();
72 DCHECK(object->IsJSBuiltinsObject());
73 return JSBuiltinsObject::cast(object);
78 Context* Context::script_context() {
79 Context* current = this;
80 while (!current->IsScriptContext()) {
81 current = current->previous();
87 Context* Context::native_context() {
88 // Fast case: the receiver context is already a native context.
89 if (IsNativeContext()) return this;
90 // The global object has a direct pointer to the native context. If the
91 // following DCHECK fails, the native context is probably being accessed
92 // indirectly during bootstrapping. This is unsupported.
93 DCHECK(global_object()->IsGlobalObject());
94 return global_object()->native_context();
98 JSObject* Context::global_proxy() {
99 return native_context()->global_proxy_object();
103 void Context::set_global_proxy(JSObject* object) {
104 native_context()->set_global_proxy_object(object);
109 * Lookups a property in an object environment, taking the unscopables into
110 * account. This is used For HasBinding spec algorithms for ObjectEnvironment.
112 static Maybe<PropertyAttributes> UnscopableLookup(LookupIterator* it) {
113 Isolate* isolate = it->isolate();
115 Maybe<PropertyAttributes> attrs = JSReceiver::GetPropertyAttributes(it);
116 DCHECK(attrs.IsJust() || isolate->has_pending_exception());
117 if (!attrs.IsJust() || attrs.FromJust() == ABSENT) return attrs;
119 Handle<Symbol> unscopables_symbol = isolate->factory()->unscopables_symbol();
120 Handle<Object> receiver = it->GetReceiver();
121 Handle<Object> unscopables;
122 MaybeHandle<Object> maybe_unscopables =
123 Object::GetProperty(receiver, unscopables_symbol);
124 if (!maybe_unscopables.ToHandle(&unscopables)) {
125 return Nothing<PropertyAttributes>();
127 if (!unscopables->IsSpecObject()) return attrs;
128 Handle<Object> blacklist;
129 MaybeHandle<Object> maybe_blacklist =
130 Object::GetProperty(unscopables, it->name());
131 if (!maybe_blacklist.ToHandle(&blacklist)) {
132 DCHECK(isolate->has_pending_exception());
133 return Nothing<PropertyAttributes>();
135 return blacklist->BooleanValue() ? Just(ABSENT) : attrs;
138 static void GetAttributesAndBindingFlags(VariableMode mode,
139 InitializationFlag init_flag,
140 PropertyAttributes* attributes,
141 BindingFlags* binding_flags) {
145 *binding_flags = MUTABLE_IS_INITIALIZED;
149 *binding_flags = (init_flag == kNeedsInitialization)
150 ? MUTABLE_CHECK_INITIALIZED
151 : MUTABLE_IS_INITIALIZED;
154 *attributes = READ_ONLY;
155 *binding_flags = (init_flag == kNeedsInitialization)
156 ? IMMUTABLE_CHECK_INITIALIZED
157 : IMMUTABLE_IS_INITIALIZED;
160 *attributes = READ_ONLY;
161 *binding_flags = (init_flag == kNeedsInitialization)
162 ? IMMUTABLE_CHECK_INITIALIZED_HARMONY
163 : IMMUTABLE_IS_INITIALIZED_HARMONY;
173 // Note: Fixed context slots are statically allocated by the compiler.
174 // Statically allocated variables always have a statically known mode,
175 // which is the mode with which they were declared when added to the
176 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
177 // declared variables that were introduced through declaration nodes)
178 // must not appear here.
185 Handle<Object> Context::Lookup(Handle<String> name,
186 ContextLookupFlags flags,
188 PropertyAttributes* attributes,
189 BindingFlags* binding_flags) {
190 Isolate* isolate = GetIsolate();
191 Handle<Context> context(this, isolate);
193 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
195 *attributes = ABSENT;
196 *binding_flags = MISSING_BINDING;
198 if (FLAG_trace_contexts) {
199 PrintF("Context::Lookup(");
205 if (FLAG_trace_contexts) {
206 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
207 if (context->IsScriptContext()) PrintF(" (script context)");
208 if (context->IsNativeContext()) PrintF(" (native context)");
213 // 1. Check global objects, subjects of with, and extension objects.
214 if (context->IsNativeContext() ||
215 context->IsWithContext() ||
216 (context->IsFunctionContext() && context->has_extension())) {
217 Handle<JSReceiver> object(
218 JSReceiver::cast(context->extension()), isolate);
220 if (context->IsNativeContext()) {
221 if (FLAG_trace_contexts) {
222 PrintF(" - trying other script contexts\n");
224 // Try other script contexts.
225 Handle<ScriptContextTable> script_contexts(
226 context->global_object()->native_context()->script_context_table());
227 ScriptContextTable::LookupResult r;
228 if (ScriptContextTable::Lookup(script_contexts, name, &r)) {
229 if (FLAG_trace_contexts) {
230 Handle<Context> c = ScriptContextTable::GetContext(script_contexts,
232 PrintF("=> found property in script context %d: %p\n",
233 r.context_index, reinterpret_cast<void*>(*c));
235 *index = r.slot_index;
236 GetAttributesAndBindingFlags(r.mode, r.init_flag, attributes,
238 return ScriptContextTable::GetContext(script_contexts,
243 // Context extension objects needs to behave as if they have no
244 // prototype. So even if we want to follow prototype chains, we need
245 // to only do a local lookup for context extension objects.
246 Maybe<PropertyAttributes> maybe = Nothing<PropertyAttributes>();
247 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
248 object->IsJSContextExtensionObject()) {
249 maybe = JSReceiver::GetOwnPropertyAttributes(object, name);
250 } else if (context->IsWithContext()) {
251 // A with context will never bind "this".
252 if (name->Equals(*isolate->factory()->this_string())) {
253 maybe = Just(ABSENT);
255 LookupIterator it(object, name);
256 maybe = UnscopableLookup(&it);
259 maybe = JSReceiver::GetPropertyAttributes(object, name);
262 if (!maybe.IsJust()) return Handle<Object>();
263 DCHECK(!isolate->has_pending_exception());
264 *attributes = maybe.FromJust();
266 if (maybe.FromJust() != ABSENT) {
267 if (FLAG_trace_contexts) {
268 PrintF("=> found property in context object %p\n",
269 reinterpret_cast<void*>(*object));
275 // 2. Check the context proper if it has slots.
276 if (context->IsFunctionContext() || context->IsBlockContext() ||
277 context->IsScriptContext()) {
278 // Use serialized scope information of functions and blocks to search
279 // for the context index.
280 Handle<ScopeInfo> scope_info;
281 if (context->IsFunctionContext()) {
282 scope_info = Handle<ScopeInfo>(
283 context->closure()->shared()->scope_info(), isolate);
285 scope_info = Handle<ScopeInfo>(
286 ScopeInfo::cast(context->extension()), isolate);
289 VariableLocation location;
290 InitializationFlag init_flag;
291 // TODO(sigurds) Figure out whether maybe_assigned_flag should
292 // be used to compute binding_flags.
293 MaybeAssignedFlag maybe_assigned_flag;
294 int slot_index = ScopeInfo::ContextSlotIndex(
295 scope_info, name, &mode, &location, &init_flag, &maybe_assigned_flag);
296 DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
297 if (slot_index >= 0 && location == VariableLocation::CONTEXT) {
298 if (FLAG_trace_contexts) {
299 PrintF("=> found local in context slot %d (mode = %d)\n",
303 GetAttributesAndBindingFlags(mode, init_flag, attributes,
308 // Check the slot corresponding to the intermediate context holding
309 // only the function name variable.
310 if (follow_context_chain && context->IsFunctionContext()) {
312 int function_index = scope_info->FunctionContextSlotIndex(*name, &mode);
313 if (function_index >= 0) {
314 if (FLAG_trace_contexts) {
315 PrintF("=> found intermediate function in context slot %d\n",
318 *index = function_index;
319 *attributes = READ_ONLY;
320 DCHECK(mode == CONST_LEGACY || mode == CONST);
321 *binding_flags = (mode == CONST_LEGACY)
322 ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY;
327 } else if (context->IsCatchContext()) {
328 // Catch contexts have the variable name in the extension slot.
329 if (String::Equals(name, handle(String::cast(context->extension())))) {
330 if (FLAG_trace_contexts) {
331 PrintF("=> found in catch context\n");
333 *index = Context::THROWN_OBJECT_INDEX;
335 *binding_flags = MUTABLE_IS_INITIALIZED;
340 // 3. Prepare to continue with the previous (next outermost) context.
341 if (context->IsNativeContext()) {
342 follow_context_chain = false;
344 context = Handle<Context>(context->previous(), isolate);
346 } while (follow_context_chain);
348 if (FLAG_trace_contexts) {
349 PrintF("=> no property/slot found\n");
351 return Handle<Object>::null();
355 void Context::InitializeGlobalSlots() {
356 DCHECK(IsScriptContext());
357 DisallowHeapAllocation no_gc;
359 ScopeInfo* scope_info = ScopeInfo::cast(extension());
361 int context_globals = scope_info->ContextGlobalCount();
362 if (context_globals > 0) {
363 PropertyCell* empty_cell = GetHeap()->empty_property_cell();
365 int context_locals = scope_info->ContextLocalCount();
366 int index = Context::MIN_CONTEXT_SLOTS + context_locals;
367 for (int i = 0; i < context_globals; i++) {
368 set(index++, empty_cell);
374 void Context::AddOptimizedFunction(JSFunction* function) {
375 DCHECK(IsNativeContext());
376 #ifdef ENABLE_SLOW_DCHECKS
377 if (FLAG_enable_slow_asserts) {
378 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
379 while (!element->IsUndefined()) {
380 CHECK(element != function);
381 element = JSFunction::cast(element)->next_function_link();
385 // Check that the context belongs to the weak native contexts list.
387 Object* context = GetHeap()->native_contexts_list();
388 while (!context->IsUndefined()) {
389 if (context == this) {
393 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
398 // If the function link field is already used then the function was
399 // enqueued as a code flushing candidate and we remove it now.
400 if (!function->next_function_link()->IsUndefined()) {
401 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
402 flusher->EvictCandidate(function);
405 DCHECK(function->next_function_link()->IsUndefined());
407 function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST),
408 UPDATE_WEAK_WRITE_BARRIER);
409 set(OPTIMIZED_FUNCTIONS_LIST, function, UPDATE_WEAK_WRITE_BARRIER);
413 void Context::RemoveOptimizedFunction(JSFunction* function) {
414 DCHECK(IsNativeContext());
415 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
416 JSFunction* prev = NULL;
417 while (!element->IsUndefined()) {
418 JSFunction* element_function = JSFunction::cast(element);
419 DCHECK(element_function->next_function_link()->IsUndefined() ||
420 element_function->next_function_link()->IsJSFunction());
421 if (element_function == function) {
423 set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link(),
424 UPDATE_WEAK_WRITE_BARRIER);
426 prev->set_next_function_link(element_function->next_function_link(),
427 UPDATE_WEAK_WRITE_BARRIER);
429 element_function->set_next_function_link(GetHeap()->undefined_value(),
430 UPDATE_WEAK_WRITE_BARRIER);
433 prev = element_function;
434 element = element_function->next_function_link();
440 void Context::SetOptimizedFunctionsListHead(Object* head) {
441 DCHECK(IsNativeContext());
442 set(OPTIMIZED_FUNCTIONS_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
446 Object* Context::OptimizedFunctionsListHead() {
447 DCHECK(IsNativeContext());
448 return get(OPTIMIZED_FUNCTIONS_LIST);
452 void Context::AddOptimizedCode(Code* code) {
453 DCHECK(IsNativeContext());
454 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
455 DCHECK(code->next_code_link()->IsUndefined());
456 code->set_next_code_link(get(OPTIMIZED_CODE_LIST));
457 set(OPTIMIZED_CODE_LIST, code, UPDATE_WEAK_WRITE_BARRIER);
461 void Context::SetOptimizedCodeListHead(Object* head) {
462 DCHECK(IsNativeContext());
463 set(OPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
467 Object* Context::OptimizedCodeListHead() {
468 DCHECK(IsNativeContext());
469 return get(OPTIMIZED_CODE_LIST);
473 void Context::SetDeoptimizedCodeListHead(Object* head) {
474 DCHECK(IsNativeContext());
475 set(DEOPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
479 Object* Context::DeoptimizedCodeListHead() {
480 DCHECK(IsNativeContext());
481 return get(DEOPTIMIZED_CODE_LIST);
485 Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() {
486 Isolate* isolate = GetIsolate();
487 Handle<Object> result(error_message_for_code_gen_from_strings(), isolate);
488 if (!result->IsUndefined()) return result;
489 return isolate->factory()->NewStringFromStaticChars(
490 "Code generation from strings disallowed for this context");
495 bool Context::IsBootstrappingOrValidParentContext(
496 Object* object, Context* child) {
497 // During bootstrapping we allow all objects to pass as
498 // contexts. This is necessary to fix circular dependencies.
499 if (child->GetIsolate()->bootstrapper()->IsActive()) return true;
500 if (!object->IsContext()) return false;
501 Context* context = Context::cast(object);
502 return context->IsNativeContext() || context->IsScriptContext() ||
503 context->IsModuleContext() || !child->IsModuleContext();
507 bool Context::IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object) {
508 // During bootstrapping we allow all objects to pass as global
509 // objects. This is necessary to fix circular dependencies.
510 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
511 isolate->bootstrapper()->IsActive() ||
512 object->IsGlobalObject();
516 } // namespace internal