• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006-2008 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
4 // met:
5 //
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.
15 //
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.
27 
28 #include "v8.h"
29 
30 #include "bootstrapper.h"
31 #include "debug.h"
32 #include "scopeinfo.h"
33 
34 namespace v8 {
35 namespace internal {
36 
builtins()37 JSBuiltinsObject* Context::builtins() {
38   GlobalObject* object = global();
39   if (object->IsJSGlobalObject()) {
40     return JSGlobalObject::cast(object)->builtins();
41   } else {
42     ASSERT(object->IsJSBuiltinsObject());
43     return JSBuiltinsObject::cast(object);
44   }
45 }
46 
47 
global_context()48 Context* Context::global_context() {
49   // Fast case: the global object for this context has been set.  In
50   // that case, the global object has a direct pointer to the global
51   // context.
52   if (global()->IsGlobalObject()) {
53     return global()->global_context();
54   }
55 
56   // During bootstrapping, the global object might not be set and we
57   // have to search the context chain to find the global context.
58   ASSERT(Isolate::Current()->bootstrapper()->IsActive());
59   Context* current = this;
60   while (!current->IsGlobalContext()) {
61     JSFunction* closure = JSFunction::cast(current->closure());
62     current = Context::cast(closure->context());
63   }
64   return current;
65 }
66 
67 
global_proxy()68 JSObject* Context::global_proxy() {
69   return global_context()->global_proxy_object();
70 }
71 
set_global_proxy(JSObject * object)72 void Context::set_global_proxy(JSObject* object) {
73   global_context()->set_global_proxy_object(object);
74 }
75 
76 
Lookup(Handle<String> name,ContextLookupFlags flags,int * index_,PropertyAttributes * attributes)77 Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags,
78                                int* index_, PropertyAttributes* attributes) {
79   Isolate* isolate = GetIsolate();
80   Handle<Context> context(this, isolate);
81 
82   bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
83   *index_ = -1;
84   *attributes = ABSENT;
85 
86   if (FLAG_trace_contexts) {
87     PrintF("Context::Lookup(");
88     name->ShortPrint();
89     PrintF(")\n");
90   }
91 
92   do {
93     if (FLAG_trace_contexts) {
94       PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
95       if (context->IsGlobalContext()) PrintF(" (global context)");
96       PrintF("\n");
97     }
98 
99     // check extension/with object
100     if (context->has_extension()) {
101       Handle<JSObject> extension = Handle<JSObject>(context->extension(),
102                                                     isolate);
103       // Context extension objects needs to behave as if they have no
104       // prototype.  So even if we want to follow prototype chains, we
105       // need to only do a local lookup for context extension objects.
106       if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
107           extension->IsJSContextExtensionObject()) {
108         *attributes = extension->GetLocalPropertyAttribute(*name);
109       } else {
110         *attributes = extension->GetPropertyAttribute(*name);
111       }
112       if (*attributes != ABSENT) {
113         // property found
114         if (FLAG_trace_contexts) {
115           PrintF("=> found property in context object %p\n",
116                  reinterpret_cast<void*>(*extension));
117         }
118         return extension;
119       }
120     }
121 
122     if (context->is_function_context()) {
123       // we have context-local slots
124 
125       // check non-parameter locals in context
126       Handle<SerializedScopeInfo> scope_info(
127           context->closure()->shared()->scope_info(), isolate);
128       Variable::Mode mode;
129       int index = scope_info->ContextSlotIndex(*name, &mode);
130       ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
131       if (index >= 0) {
132         // slot found
133         if (FLAG_trace_contexts) {
134           PrintF("=> found local in context slot %d (mode = %d)\n",
135                  index, mode);
136         }
137         *index_ = index;
138         // Note: Fixed context slots are statically allocated by the compiler.
139         // Statically allocated variables always have a statically known mode,
140         // which is the mode with which they were declared when added to the
141         // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
142         // declared variables that were introduced through declaration nodes)
143         // must not appear here.
144         switch (mode) {
145           case Variable::INTERNAL:  // fall through
146           case Variable::VAR: *attributes = NONE; break;
147           case Variable::CONST: *attributes = READ_ONLY; break;
148           case Variable::DYNAMIC: UNREACHABLE(); break;
149           case Variable::DYNAMIC_GLOBAL: UNREACHABLE(); break;
150           case Variable::DYNAMIC_LOCAL: UNREACHABLE(); break;
151           case Variable::TEMPORARY: UNREACHABLE(); break;
152         }
153         return context;
154       }
155 
156       // check parameter locals in context
157       int param_index = scope_info->ParameterIndex(*name);
158       if (param_index >= 0) {
159         // slot found.
160         int index = scope_info->ContextSlotIndex(
161             isolate->heap()->arguments_shadow_symbol(), NULL);
162         ASSERT(index >= 0);  // arguments must exist and be in the heap context
163         Handle<JSObject> arguments(JSObject::cast(context->get(index)),
164                                    isolate);
165         ASSERT(arguments->HasLocalProperty(isolate->heap()->length_symbol()));
166         if (FLAG_trace_contexts) {
167           PrintF("=> found parameter %d in arguments object\n", param_index);
168         }
169         *index_ = param_index;
170         *attributes = NONE;
171         return arguments;
172       }
173 
174       // check intermediate context (holding only the function name variable)
175       if (follow_context_chain) {
176         int index = scope_info->FunctionContextSlotIndex(*name);
177         if (index >= 0) {
178           // slot found
179           if (FLAG_trace_contexts) {
180             PrintF("=> found intermediate function in context slot %d\n",
181                    index);
182           }
183           *index_ = index;
184           *attributes = READ_ONLY;
185           return context;
186         }
187       }
188     }
189 
190     // proceed with enclosing context
191     if (context->IsGlobalContext()) {
192       follow_context_chain = false;
193     } else if (context->is_function_context()) {
194       context = Handle<Context>(Context::cast(context->closure()->context()),
195                                 isolate);
196     } else {
197       context = Handle<Context>(context->previous(), isolate);
198     }
199   } while (follow_context_chain);
200 
201   // slot not found
202   if (FLAG_trace_contexts) {
203     PrintF("=> no property/slot found\n");
204   }
205   return Handle<Object>::null();
206 }
207 
208 
GlobalIfNotShadowedByEval(Handle<String> name)209 bool Context::GlobalIfNotShadowedByEval(Handle<String> name) {
210   Context* context = this;
211 
212   // Check that there is no local with the given name in contexts
213   // before the global context and check that there are no context
214   // extension objects (conservative check for with statements).
215   while (!context->IsGlobalContext()) {
216     // Check if the context is a potentially a with context.
217     if (context->has_extension()) return false;
218 
219     // Not a with context so it must be a function context.
220     ASSERT(context->is_function_context());
221 
222     // Check non-parameter locals.
223     Handle<SerializedScopeInfo> scope_info(
224         context->closure()->shared()->scope_info());
225     Variable::Mode mode;
226     int index = scope_info->ContextSlotIndex(*name, &mode);
227     ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
228     if (index >= 0) return false;
229 
230     // Check parameter locals.
231     int param_index = scope_info->ParameterIndex(*name);
232     if (param_index >= 0) return false;
233 
234     // Check context only holding the function name variable.
235     index = scope_info->FunctionContextSlotIndex(*name);
236     if (index >= 0) return false;
237     context = Context::cast(context->closure()->context());
238   }
239 
240   // No local or potential with statement found so the variable is
241   // global unless it is shadowed by an eval-introduced variable.
242   return true;
243 }
244 
245 
AddOptimizedFunction(JSFunction * function)246 void Context::AddOptimizedFunction(JSFunction* function) {
247   ASSERT(IsGlobalContext());
248 #ifdef DEBUG
249   Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
250   while (!element->IsUndefined()) {
251     CHECK(element != function);
252     element = JSFunction::cast(element)->next_function_link();
253   }
254 
255   CHECK(function->next_function_link()->IsUndefined());
256 
257   // Check that the context belongs to the weak global contexts list.
258   bool found = false;
259   Object* context = GetHeap()->global_contexts_list();
260   while (!context->IsUndefined()) {
261     if (context == this) {
262       found = true;
263       break;
264     }
265     context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
266   }
267   CHECK(found);
268 #endif
269   function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST));
270   set(OPTIMIZED_FUNCTIONS_LIST, function);
271 }
272 
273 
RemoveOptimizedFunction(JSFunction * function)274 void Context::RemoveOptimizedFunction(JSFunction* function) {
275   ASSERT(IsGlobalContext());
276   Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
277   JSFunction* prev = NULL;
278   while (!element->IsUndefined()) {
279     JSFunction* element_function = JSFunction::cast(element);
280     ASSERT(element_function->next_function_link()->IsUndefined() ||
281            element_function->next_function_link()->IsJSFunction());
282     if (element_function == function) {
283       if (prev == NULL) {
284         set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link());
285       } else {
286         prev->set_next_function_link(element_function->next_function_link());
287       }
288       element_function->set_next_function_link(GetHeap()->undefined_value());
289       return;
290     }
291     prev = element_function;
292     element = element_function->next_function_link();
293   }
294   UNREACHABLE();
295 }
296 
297 
OptimizedFunctionsListHead()298 Object* Context::OptimizedFunctionsListHead() {
299   ASSERT(IsGlobalContext());
300   return get(OPTIMIZED_FUNCTIONS_LIST);
301 }
302 
303 
ClearOptimizedFunctions()304 void Context::ClearOptimizedFunctions() {
305   set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value());
306 }
307 
308 
309 #ifdef DEBUG
IsBootstrappingOrContext(Object * object)310 bool Context::IsBootstrappingOrContext(Object* object) {
311   // During bootstrapping we allow all objects to pass as
312   // contexts. This is necessary to fix circular dependencies.
313   return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext();
314 }
315 
316 
IsBootstrappingOrGlobalObject(Object * object)317 bool Context::IsBootstrappingOrGlobalObject(Object* object) {
318   // During bootstrapping we allow all objects to pass as global
319   // objects. This is necessary to fix circular dependencies.
320   Isolate* isolate = Isolate::Current();
321   return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
322       isolate->bootstrapper()->IsActive() ||
323       object->IsGlobalObject();
324 }
325 #endif
326 
327 } }  // namespace v8::internal
328