blob: 520f3dde2444e4d87e5817ddb6f3e54c27edbcc1 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
34namespace v8 {
35namespace internal {
36
37JSBuiltinsObject* 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
48Context* 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 }
Leon Clarkee46be812010-01-19 14:06:41 +000055
Steve Blocka7e24c12009-10-30 11:49:00 +000056 // During bootstrapping, the global object might not be set and we
57 // have to search the context chain to find the global context.
Steve Block44f0eee2011-05-26 01:26:41 +010058 ASSERT(Isolate::Current()->bootstrapper()->IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +000059 Context* current = this;
60 while (!current->IsGlobalContext()) {
Leon Clarkee46be812010-01-19 14:06:41 +000061 JSFunction* closure = JSFunction::cast(current->closure());
62 current = Context::cast(closure->context());
Steve Blocka7e24c12009-10-30 11:49:00 +000063 }
64 return current;
65}
66
67
68JSObject* Context::global_proxy() {
69 return global_context()->global_proxy_object();
70}
71
72void Context::set_global_proxy(JSObject* object) {
73 global_context()->set_global_proxy_object(object);
74}
75
76
77Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags,
78 int* index_, PropertyAttributes* attributes) {
Steve Block44f0eee2011-05-26 01:26:41 +010079 Isolate* isolate = GetIsolate();
80 Handle<Context> context(this, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +000081
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) {
Ben Murdochf87a2032010-10-22 12:50:53 +010094 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
Steve Blocka7e24c12009-10-30 11:49:00 +000095 if (context->IsGlobalContext()) PrintF(" (global context)");
96 PrintF("\n");
97 }
98
99 // check extension/with object
100 if (context->has_extension()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100101 Handle<JSObject> extension = Handle<JSObject>(context->extension(),
102 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000103 // 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) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100115 PrintF("=> found property in context object %p\n",
116 reinterpret_cast<void*>(*extension));
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 }
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
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100126 Handle<SerializedScopeInfo> scope_info(
Steve Block44f0eee2011-05-26 01:26:41 +0100127 context->closure()->shared()->scope_info(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 Variable::Mode mode;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100129 int index = scope_info->ContextSlotIndex(*name, &mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 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
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100157 int param_index = scope_info->ParameterIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000158 if (param_index >= 0) {
159 // slot found.
Steve Block44f0eee2011-05-26 01:26:41 +0100160 int index = scope_info->ContextSlotIndex(
161 isolate->heap()->arguments_shadow_symbol(), NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 ASSERT(index >= 0); // arguments must exist and be in the heap context
Steve Block44f0eee2011-05-26 01:26:41 +0100163 Handle<JSObject> arguments(JSObject::cast(context->get(index)),
164 isolate);
165 ASSERT(arguments->HasLocalProperty(isolate->heap()->length_symbol()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000166 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) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100176 int index = scope_info->FunctionContextSlotIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 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()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100194 context = Handle<Context>(Context::cast(context->closure()->context()),
195 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000196 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100197 context = Handle<Context>(context->previous(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 }
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
209bool 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.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100223 Handle<SerializedScopeInfo> scope_info(
224 context->closure()->shared()->scope_info());
Steve Blocka7e24c12009-10-30 11:49:00 +0000225 Variable::Mode mode;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100226 int index = scope_info->ContextSlotIndex(*name, &mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000227 ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
228 if (index >= 0) return false;
229
230 // Check parameter locals.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100231 int param_index = scope_info->ParameterIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 if (param_index >= 0) return false;
233
234 // Check context only holding the function name variable.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100235 index = scope_info->FunctionContextSlotIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000236 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
Ben Murdochb0fe1622011-05-05 13:52:32 +0100246void 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;
Steve Block44f0eee2011-05-26 01:26:41 +0100259 Object* context = GetHeap()->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100260 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
274void 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 }
Steve Block44f0eee2011-05-26 01:26:41 +0100288 element_function->set_next_function_link(GetHeap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100289 return;
290 }
291 prev = element_function;
292 element = element_function->next_function_link();
293 }
294 UNREACHABLE();
295}
296
297
298Object* Context::OptimizedFunctionsListHead() {
299 ASSERT(IsGlobalContext());
300 return get(OPTIMIZED_FUNCTIONS_LIST);
301}
302
303
304void Context::ClearOptimizedFunctions() {
Steve Block44f0eee2011-05-26 01:26:41 +0100305 set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100306}
307
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309#ifdef DEBUG
310bool Context::IsBootstrappingOrContext(Object* object) {
311 // During bootstrapping we allow all objects to pass as
312 // contexts. This is necessary to fix circular dependencies.
Steve Block44f0eee2011-05-26 01:26:41 +0100313 return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000314}
315
316
317bool Context::IsBootstrappingOrGlobalObject(Object* object) {
318 // During bootstrapping we allow all objects to pass as global
319 // objects. This is necessary to fix circular dependencies.
Steve Block44f0eee2011-05-26 01:26:41 +0100320 Isolate* isolate = Isolate::Current();
321 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
322 isolate->bootstrapper()->IsActive() ||
323 object->IsGlobalObject();
Steve Blocka7e24c12009-10-30 11:49:00 +0000324}
325#endif
326
327} } // namespace v8::internal