• 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   // During bootstrapping, the global object might not be set and we
56   // have to search the context chain to find the global context.
57   Context* current = this;
58   while (!current->IsGlobalContext()) {
59     current = Context::cast(JSFunction::cast(current->closure())->context());
60   }
61   return current;
62 }
63 
64 
global_proxy()65 JSObject* Context::global_proxy() {
66   return global_context()->global_proxy_object();
67 }
68 
set_global_proxy(JSObject * object)69 void Context::set_global_proxy(JSObject* object) {
70   global_context()->set_global_proxy_object(object);
71 }
72 
73 
Lookup(Handle<String> name,ContextLookupFlags flags,int * index_,PropertyAttributes * attributes)74 Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags,
75                                int* index_, PropertyAttributes* attributes) {
76   Handle<Context> context(this);
77 
78   bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
79   *index_ = -1;
80   *attributes = ABSENT;
81 
82   if (FLAG_trace_contexts) {
83     PrintF("Context::Lookup(");
84     name->ShortPrint();
85     PrintF(")\n");
86   }
87 
88   do {
89     if (FLAG_trace_contexts) {
90       PrintF(" - looking in context %p", *context);
91       if (context->IsGlobalContext()) PrintF(" (global context)");
92       PrintF("\n");
93     }
94 
95     // check extension/with object
96     if (context->has_extension()) {
97       Handle<JSObject> extension = Handle<JSObject>(context->extension());
98       // Context extension objects needs to behave as if they have no
99       // prototype.  So even if we want to follow prototype chains, we
100       // need to only do a local lookup for context extension objects.
101       if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
102           extension->IsJSContextExtensionObject()) {
103         *attributes = extension->GetLocalPropertyAttribute(*name);
104       } else {
105         *attributes = extension->GetPropertyAttribute(*name);
106       }
107       if (*attributes != ABSENT) {
108         // property found
109         if (FLAG_trace_contexts) {
110           PrintF("=> found property in context object %p\n", *extension);
111         }
112         return extension;
113       }
114     }
115 
116     if (context->is_function_context()) {
117       // we have context-local slots
118 
119       // check non-parameter locals in context
120       Handle<Code> code(context->closure()->code());
121       Variable::Mode mode;
122       int index = ScopeInfo<>::ContextSlotIndex(*code, *name, &mode);
123       ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
124       if (index >= 0) {
125         // slot found
126         if (FLAG_trace_contexts) {
127           PrintF("=> found local in context slot %d (mode = %d)\n",
128                  index, mode);
129         }
130         *index_ = index;
131         // Note: Fixed context slots are statically allocated by the compiler.
132         // Statically allocated variables always have a statically known mode,
133         // which is the mode with which they were declared when added to the
134         // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
135         // declared variables that were introduced through declaration nodes)
136         // must not appear here.
137         switch (mode) {
138           case Variable::INTERNAL:  // fall through
139           case Variable::VAR: *attributes = NONE; break;
140           case Variable::CONST: *attributes = READ_ONLY; break;
141           case Variable::DYNAMIC: UNREACHABLE(); break;
142           case Variable::DYNAMIC_GLOBAL: UNREACHABLE(); break;
143           case Variable::DYNAMIC_LOCAL: UNREACHABLE(); break;
144           case Variable::TEMPORARY: UNREACHABLE(); break;
145         }
146         return context;
147       }
148 
149       // check parameter locals in context
150       int param_index = ScopeInfo<>::ParameterIndex(*code, *name);
151       if (param_index >= 0) {
152         // slot found.
153         int index =
154             ScopeInfo<>::ContextSlotIndex(*code,
155                                           Heap::arguments_shadow_symbol(),
156                                           NULL);
157         ASSERT(index >= 0);  // arguments must exist and be in the heap context
158         Handle<JSObject> arguments(JSObject::cast(context->get(index)));
159         ASSERT(arguments->HasLocalProperty(Heap::length_symbol()));
160         if (FLAG_trace_contexts) {
161           PrintF("=> found parameter %d in arguments object\n", param_index);
162         }
163         *index_ = param_index;
164         *attributes = NONE;
165         return arguments;
166       }
167 
168       // check intermediate context (holding only the function name variable)
169       if (follow_context_chain) {
170         int index = ScopeInfo<>::FunctionContextSlotIndex(*code, *name);
171         if (index >= 0) {
172           // slot found
173           if (FLAG_trace_contexts) {
174             PrintF("=> found intermediate function in context slot %d\n",
175                    index);
176           }
177           *index_ = index;
178           *attributes = READ_ONLY;
179           return context;
180         }
181       }
182     }
183 
184     // proceed with enclosing context
185     if (context->IsGlobalContext()) {
186       follow_context_chain = false;
187     } else if (context->is_function_context()) {
188       context = Handle<Context>(Context::cast(context->closure()->context()));
189     } else {
190       context = Handle<Context>(context->previous());
191     }
192   } while (follow_context_chain);
193 
194   // slot not found
195   if (FLAG_trace_contexts) {
196     PrintF("=> no property/slot found\n");
197   }
198   return Handle<Object>::null();
199 }
200 
201 
GlobalIfNotShadowedByEval(Handle<String> name)202 bool Context::GlobalIfNotShadowedByEval(Handle<String> name) {
203   Context* context = this;
204 
205   // Check that there is no local with the given name in contexts
206   // before the global context and check that there are no context
207   // extension objects (conservative check for with statements).
208   while (!context->IsGlobalContext()) {
209     // Check if the context is a potentially a with context.
210     if (context->has_extension()) return false;
211 
212     // Not a with context so it must be a function context.
213     ASSERT(context->is_function_context());
214 
215     // Check non-parameter locals.
216     Handle<Code> code(context->closure()->code());
217     Variable::Mode mode;
218     int index = ScopeInfo<>::ContextSlotIndex(*code, *name, &mode);
219     ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
220     if (index >= 0) return false;
221 
222     // Check parameter locals.
223     int param_index = ScopeInfo<>::ParameterIndex(*code, *name);
224     if (param_index >= 0) return false;
225 
226     // Check context only holding the function name variable.
227     index = ScopeInfo<>::FunctionContextSlotIndex(*code, *name);
228     if (index >= 0) return false;
229     context = Context::cast(context->closure()->context());
230   }
231 
232   // No local or potential with statement found so the variable is
233   // global unless it is shadowed by an eval-introduced variable.
234   return true;
235 }
236 
237 
238 #ifdef DEBUG
IsBootstrappingOrContext(Object * object)239 bool Context::IsBootstrappingOrContext(Object* object) {
240   // During bootstrapping we allow all objects to pass as
241   // contexts. This is necessary to fix circular dependencies.
242   return Bootstrapper::IsActive() || object->IsContext();
243 }
244 
245 
IsBootstrappingOrGlobalObject(Object * object)246 bool Context::IsBootstrappingOrGlobalObject(Object* object) {
247   // During bootstrapping we allow all objects to pass as global
248   // objects. This is necessary to fix circular dependencies.
249   return Bootstrapper::IsActive() || object->IsGlobalObject();
250 }
251 #endif
252 
253 } }  // namespace v8::internal
254