1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "bootstrapper.h"
32 #include "scopeinfo.h"
37 Context* Context::declaration_context() {
38 Context* current = this;
39 while (!current->IsFunctionContext() && !current->IsGlobalContext()) {
40 current = current->previous();
41 ASSERT(current->closure() == closure());
47 JSBuiltinsObject* Context::builtins() {
48 GlobalObject* object = global();
49 if (object->IsJSGlobalObject()) {
50 return JSGlobalObject::cast(object)->builtins();
52 ASSERT(object->IsJSBuiltinsObject());
53 return JSBuiltinsObject::cast(object);
58 Context* Context::global_context() {
59 // Fast case: the global object for this context has been set. In
60 // that case, the global object has a direct pointer to the global
62 if (global()->IsGlobalObject()) {
63 return global()->global_context();
66 // During bootstrapping, the global object might not be set and we
67 // have to search the context chain to find the global context.
68 ASSERT(Isolate::Current()->bootstrapper()->IsActive());
69 Context* current = this;
70 while (!current->IsGlobalContext()) {
71 JSFunction* closure = JSFunction::cast(current->closure());
72 current = Context::cast(closure->context());
78 JSObject* Context::global_proxy() {
79 return global_context()->global_proxy_object();
82 void Context::set_global_proxy(JSObject* object) {
83 global_context()->set_global_proxy_object(object);
87 Handle<Object> Context::Lookup(Handle<String> name,
88 ContextLookupFlags flags,
90 PropertyAttributes* attributes,
91 BindingFlags* binding_flags) {
92 Isolate* isolate = GetIsolate();
93 Handle<Context> context(this, isolate);
95 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
98 *binding_flags = MISSING_BINDING;
100 if (FLAG_trace_contexts) {
101 PrintF("Context::Lookup(");
106 Handle<JSObject> qml_global;
107 Handle<JSObject> qml_global_global;
110 if (FLAG_trace_contexts) {
111 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
112 if (context->IsGlobalContext()) PrintF(" (global context)");
116 if (qml_global.is_null() && !context->qml_global()->IsUndefined()) {
117 qml_global = Handle<JSObject>(context->qml_global(), isolate);
118 qml_global_global = Handle<JSObject>(context->global(), isolate);
121 // 1. Check global objects, subjects of with, and extension objects.
122 if (context->IsGlobalContext() ||
123 context->IsWithContext() ||
124 (context->IsFunctionContext() && context->has_extension())) {
125 Handle<JSObject> object(JSObject::cast(context->extension()), isolate);
126 // Context extension objects needs to behave as if they have no
127 // prototype. So even if we want to follow prototype chains, we need
128 // to only do a local lookup for context extension objects.
129 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
130 object->IsJSContextExtensionObject()) {
131 *attributes = object->GetLocalPropertyAttribute(*name);
133 *attributes = object->GetPropertyAttribute(*name);
135 if (*attributes != ABSENT) {
136 if (FLAG_trace_contexts) {
137 PrintF("=> found property in context object %p\n",
138 reinterpret_cast<void*>(*object));
144 // 2. Check the context proper if it has slots.
145 if (context->IsFunctionContext() || context->IsBlockContext()) {
146 // Use serialized scope information of functions and blocks to search
147 // for the context index.
148 Handle<ScopeInfo> scope_info;
149 if (context->IsFunctionContext()) {
150 scope_info = Handle<ScopeInfo>(
151 context->closure()->shared()->scope_info(), isolate);
153 scope_info = Handle<ScopeInfo>(
154 ScopeInfo::cast(context->extension()), isolate);
157 InitializationFlag init_flag;
158 int slot_index = scope_info->ContextSlotIndex(*name, &mode, &init_flag);
159 ASSERT(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
160 if (slot_index >= 0) {
161 if (FLAG_trace_contexts) {
162 PrintF("=> found local in context slot %d (mode = %d)\n",
166 // Note: Fixed context slots are statically allocated by the compiler.
167 // Statically allocated variables always have a statically known mode,
168 // which is the mode with which they were declared when added to the
169 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
170 // declared variables that were introduced through declaration nodes)
171 // must not appear here.
173 case INTERNAL: // Fall through.
176 *binding_flags = MUTABLE_IS_INITIALIZED;
180 *binding_flags = (init_flag == kNeedsInitialization)
181 ? MUTABLE_CHECK_INITIALIZED : MUTABLE_IS_INITIALIZED;
184 *attributes = READ_ONLY;
185 *binding_flags = (init_flag == kNeedsInitialization)
186 ? IMMUTABLE_CHECK_INITIALIZED : IMMUTABLE_IS_INITIALIZED;
189 *attributes = READ_ONLY;
190 *binding_flags = (init_flag == kNeedsInitialization)
191 ? IMMUTABLE_CHECK_INITIALIZED_HARMONY :
192 IMMUTABLE_IS_INITIALIZED_HARMONY;
204 // Check the slot corresponding to the intermediate context holding
205 // only the function name variable.
206 if (follow_context_chain && context->IsFunctionContext()) {
208 int function_index = scope_info->FunctionContextSlotIndex(*name, &mode);
209 if (function_index >= 0) {
210 if (FLAG_trace_contexts) {
211 PrintF("=> found intermediate function in context slot %d\n",
214 *index = function_index;
215 *attributes = READ_ONLY;
216 ASSERT(mode == CONST || mode == CONST_HARMONY);
217 *binding_flags = (mode == CONST)
218 ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY;
223 } else if (context->IsCatchContext()) {
224 // Catch contexts have the variable name in the extension slot.
225 if (name->Equals(String::cast(context->extension()))) {
226 if (FLAG_trace_contexts) {
227 PrintF("=> found in catch context\n");
229 *index = Context::THROWN_OBJECT_INDEX;
231 *binding_flags = MUTABLE_IS_INITIALIZED;
236 // 3. Prepare to continue with the previous (next outermost) context.
237 if (context->IsGlobalContext()) {
238 follow_context_chain = false;
240 context = Handle<Context>(context->previous(), isolate);
242 } while (follow_context_chain);
244 if (!qml_global.is_null()) {
245 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0) {
246 *attributes = qml_global_global->GetLocalPropertyAttribute(*name);
248 *attributes = qml_global_global->GetPropertyAttribute(*name);
251 if (*attributes != ABSENT) {
252 *attributes = ABSENT;
254 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0) {
255 *attributes = qml_global->GetLocalPropertyAttribute(*name);
257 *attributes = qml_global->GetPropertyAttribute(*name);
260 if (*attributes != ABSENT) {
262 if (FLAG_trace_contexts) {
263 PrintF("=> found property in qml global object %p\n",
264 reinterpret_cast<void*>(*qml_global));
271 if (FLAG_trace_contexts) {
272 PrintF("=> no property/slot found\n");
274 return Handle<Object>::null();
278 void Context::AddOptimizedFunction(JSFunction* function) {
279 ASSERT(IsGlobalContext());
281 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
282 while (!element->IsUndefined()) {
283 CHECK(element != function);
284 element = JSFunction::cast(element)->next_function_link();
287 CHECK(function->next_function_link()->IsUndefined());
289 // Check that the context belongs to the weak global contexts list.
291 Object* context = GetHeap()->global_contexts_list();
292 while (!context->IsUndefined()) {
293 if (context == this) {
297 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
301 function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST));
302 set(OPTIMIZED_FUNCTIONS_LIST, function);
306 void Context::RemoveOptimizedFunction(JSFunction* function) {
307 ASSERT(IsGlobalContext());
308 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
309 JSFunction* prev = NULL;
310 while (!element->IsUndefined()) {
311 JSFunction* element_function = JSFunction::cast(element);
312 ASSERT(element_function->next_function_link()->IsUndefined() ||
313 element_function->next_function_link()->IsJSFunction());
314 if (element_function == function) {
316 set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link());
318 prev->set_next_function_link(element_function->next_function_link());
320 element_function->set_next_function_link(GetHeap()->undefined_value());
323 prev = element_function;
324 element = element_function->next_function_link();
330 Object* Context::OptimizedFunctionsListHead() {
331 ASSERT(IsGlobalContext());
332 return get(OPTIMIZED_FUNCTIONS_LIST);
336 void Context::ClearOptimizedFunctions() {
337 set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value());
342 bool Context::IsBootstrappingOrContext(Object* object) {
343 // During bootstrapping we allow all objects to pass as
344 // contexts. This is necessary to fix circular dependencies.
345 return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext();
349 bool Context::IsBootstrappingOrGlobalObject(Object* object) {
350 // During bootstrapping we allow all objects to pass as global
351 // objects. This is necessary to fix circular dependencies.
352 Isolate* isolate = Isolate::Current();
353 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
354 isolate->bootstrapper()->IsActive() ||
355 object->IsGlobalObject();
359 } } // namespace v8::internal