blob: f6031f1d058cc5c135b6d077b45162d37bab3ef0 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 if (FLAG_trace_contexts) {
166 PrintF("=> found parameter %d in arguments object\n", param_index);
167 }
168 *index_ = param_index;
169 *attributes = NONE;
170 return arguments;
171 }
172
173 // check intermediate context (holding only the function name variable)
174 if (follow_context_chain) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100175 int index = scope_info->FunctionContextSlotIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000176 if (index >= 0) {
177 // slot found
178 if (FLAG_trace_contexts) {
179 PrintF("=> found intermediate function in context slot %d\n",
180 index);
181 }
182 *index_ = index;
183 *attributes = READ_ONLY;
184 return context;
185 }
186 }
187 }
188
189 // proceed with enclosing context
190 if (context->IsGlobalContext()) {
191 follow_context_chain = false;
192 } else if (context->is_function_context()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100193 context = Handle<Context>(Context::cast(context->closure()->context()),
194 isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100196 context = Handle<Context>(context->previous(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000197 }
198 } while (follow_context_chain);
199
200 // slot not found
201 if (FLAG_trace_contexts) {
202 PrintF("=> no property/slot found\n");
203 }
204 return Handle<Object>::null();
205}
206
207
208bool Context::GlobalIfNotShadowedByEval(Handle<String> name) {
209 Context* context = this;
210
211 // Check that there is no local with the given name in contexts
212 // before the global context and check that there are no context
213 // extension objects (conservative check for with statements).
214 while (!context->IsGlobalContext()) {
215 // Check if the context is a potentially a with context.
216 if (context->has_extension()) return false;
217
218 // Not a with context so it must be a function context.
219 ASSERT(context->is_function_context());
220
221 // Check non-parameter locals.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100222 Handle<SerializedScopeInfo> scope_info(
223 context->closure()->shared()->scope_info());
Steve Blocka7e24c12009-10-30 11:49:00 +0000224 Variable::Mode mode;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100225 int index = scope_info->ContextSlotIndex(*name, &mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000226 ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
227 if (index >= 0) return false;
228
229 // Check parameter locals.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100230 int param_index = scope_info->ParameterIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000231 if (param_index >= 0) return false;
232
233 // Check context only holding the function name variable.
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100234 index = scope_info->FunctionContextSlotIndex(*name);
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 if (index >= 0) return false;
236 context = Context::cast(context->closure()->context());
237 }
238
239 // No local or potential with statement found so the variable is
240 // global unless it is shadowed by an eval-introduced variable.
241 return true;
242}
243
244
Ben Murdoch257744e2011-11-30 15:57:28 +0000245void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval,
246 bool* outer_scope_calls_non_strict_eval) {
247 Context* context = this;
248 while (true) {
249 Handle<SerializedScopeInfo> scope_info(
250 context->closure()->shared()->scope_info());
251 if (scope_info->CallsEval()) {
252 *outer_scope_calls_eval = true;
253 if (!scope_info->IsStrictMode()) {
254 // No need to go further since the answers will not change
255 // from here.
256 *outer_scope_calls_non_strict_eval = true;
257 return;
258 }
259 }
260 if (context->IsGlobalContext()) break;
261 context = Context::cast(context->closure()->context());
262 }
263}
264
265
Ben Murdochb0fe1622011-05-05 13:52:32 +0100266void Context::AddOptimizedFunction(JSFunction* function) {
267 ASSERT(IsGlobalContext());
268#ifdef DEBUG
269 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
270 while (!element->IsUndefined()) {
271 CHECK(element != function);
272 element = JSFunction::cast(element)->next_function_link();
273 }
274
275 CHECK(function->next_function_link()->IsUndefined());
276
277 // Check that the context belongs to the weak global contexts list.
278 bool found = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100279 Object* context = GetHeap()->global_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100280 while (!context->IsUndefined()) {
281 if (context == this) {
282 found = true;
283 break;
284 }
285 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
286 }
287 CHECK(found);
288#endif
289 function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST));
290 set(OPTIMIZED_FUNCTIONS_LIST, function);
291}
292
293
294void Context::RemoveOptimizedFunction(JSFunction* function) {
295 ASSERT(IsGlobalContext());
296 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
297 JSFunction* prev = NULL;
298 while (!element->IsUndefined()) {
299 JSFunction* element_function = JSFunction::cast(element);
300 ASSERT(element_function->next_function_link()->IsUndefined() ||
301 element_function->next_function_link()->IsJSFunction());
302 if (element_function == function) {
303 if (prev == NULL) {
304 set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link());
305 } else {
306 prev->set_next_function_link(element_function->next_function_link());
307 }
Steve Block44f0eee2011-05-26 01:26:41 +0100308 element_function->set_next_function_link(GetHeap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100309 return;
310 }
311 prev = element_function;
312 element = element_function->next_function_link();
313 }
314 UNREACHABLE();
315}
316
317
318Object* Context::OptimizedFunctionsListHead() {
319 ASSERT(IsGlobalContext());
320 return get(OPTIMIZED_FUNCTIONS_LIST);
321}
322
323
324void Context::ClearOptimizedFunctions() {
Steve Block44f0eee2011-05-26 01:26:41 +0100325 set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326}
327
328
Steve Blocka7e24c12009-10-30 11:49:00 +0000329#ifdef DEBUG
330bool Context::IsBootstrappingOrContext(Object* object) {
331 // During bootstrapping we allow all objects to pass as
332 // contexts. This is necessary to fix circular dependencies.
Steve Block44f0eee2011-05-26 01:26:41 +0100333 return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000334}
335
336
337bool Context::IsBootstrappingOrGlobalObject(Object* object) {
338 // During bootstrapping we allow all objects to pass as global
339 // objects. This is necessary to fix circular dependencies.
Steve Block44f0eee2011-05-26 01:26:41 +0100340 Isolate* isolate = Isolate::Current();
341 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
342 isolate->bootstrapper()->IsActive() ||
343 object->IsGlobalObject();
Steve Blocka7e24c12009-10-30 11:49:00 +0000344}
345#endif
346
347} } // namespace v8::internal