blob: 0f3f6088d17e7e45d60e1852cfc2b90d8e8ad79f [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000030#include "bootstrapper.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031#include "debug.h"
32#include "scopeinfo.h"
33
34namespace v8 { namespace internal {
35
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036JSBuiltinsObject* Context::builtins() {
37 GlobalObject* object = global();
38 if (object->IsJSGlobalObject()) {
39 return JSGlobalObject::cast(object)->builtins();
40 } else {
41 ASSERT(object->IsJSBuiltinsObject());
42 return JSBuiltinsObject::cast(object);
43 }
44}
45
46
47Context* Context::global_context() {
48 // Fast case: the global object for this context has been set. In
49 // that case, the global object has a direct pointer to the global
50 // context.
51 if (global()->IsGlobalObject()) {
52 return global()->global_context();
53 }
54 // During bootstrapping, the global object might not be set and we
55 // have to search the context chain to find the global context.
56 Context* current = this;
57 while (!current->IsGlobalContext()) {
58 current = Context::cast(JSFunction::cast(current->closure())->context());
59 }
60 return current;
61}
62
63
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000064JSObject* Context::global_proxy() {
65 return global_context()->global_proxy_object();
66}
67
68void Context::set_global_proxy(JSObject* object) {
69 global_context()->set_global_proxy_object(object);
70}
71
72
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags,
74 int* index_, PropertyAttributes* attributes) {
75 Handle<Context> context(this);
76
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
78 *index_ = -1;
79 *attributes = ABSENT;
80
81 if (FLAG_trace_contexts) {
82 PrintF("Context::Lookup(");
83 name->ShortPrint();
84 PrintF(")\n");
85 }
86
87 do {
88 if (FLAG_trace_contexts) {
89 PrintF(" - looking in context %p", *context);
90 if (context->IsGlobalContext()) PrintF(" (global context)");
91 PrintF("\n");
92 }
93
94 // check extension/with object
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000095 if (context->has_extension()) {
96 Handle<JSObject> extension = Handle<JSObject>(context->extension());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +000098 *attributes = extension->GetLocalPropertyAttribute(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 } else {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000100 *attributes = extension->GetPropertyAttribute(*name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101 }
102 if (*attributes != ABSENT) {
103 // property found
104 if (FLAG_trace_contexts) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000105 PrintF("=> found property in context object %p\n", *extension);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000106 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000107 return extension;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 }
109 }
110
111 if (context->is_function_context()) {
112 // we have context-local slots
113
114 // check non-parameter locals in context
115 Handle<Code> code(context->closure()->code());
116 Variable::Mode mode;
117 int index = ScopeInfo<>::ContextSlotIndex(*code, *name, &mode);
118 ASSERT(index < 0 || index >= MIN_CONTEXT_SLOTS);
119 if (index >= 0) {
120 // slot found
121 if (FLAG_trace_contexts) {
122 PrintF("=> found local in context slot %d (mode = %d)\n",
123 index, mode);
124 }
125 *index_ = index;
126 // Note: Fixed context slots are statically allocated by the compiler.
127 // Statically allocated variables always have a statically known mode,
128 // which is the mode with which they were declared when added to the
129 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
130 // declared variables that were introduced through declaration nodes)
131 // must not appear here.
132 switch (mode) {
133 case Variable::INTERNAL : // fall through
134 case Variable::VAR : *attributes = NONE; break;
135 case Variable::CONST : *attributes = READ_ONLY; break;
136 case Variable::DYNAMIC : UNREACHABLE(); break;
137 case Variable::TEMPORARY: UNREACHABLE(); break;
138 }
139 return context;
140 }
141
142 // check parameter locals in context
143 int param_index = ScopeInfo<>::ParameterIndex(*code, *name);
144 if (param_index >= 0) {
145 // slot found
146 int index =
147 ScopeInfo<>::ContextSlotIndex(*code,
148 Heap::arguments_shadow_symbol(),
149 NULL);
150 ASSERT(index >= 0); // arguments must exist and be in the heap context
151 Handle<JSObject> arguments(JSObject::cast(context->get(index)));
152 ASSERT(arguments->HasLocalProperty(Heap::length_symbol()));
153 if (FLAG_trace_contexts) {
154 PrintF("=> found parameter %d in arguments object\n", param_index);
155 }
156 *index_ = param_index;
157 *attributes = NONE;
158 return arguments;
159 }
160
161 // check intermediate context (holding only the function name variable)
162 if (follow_context_chain) {
163 int index = ScopeInfo<>::FunctionContextSlotIndex(*code, *name);
164 if (index >= 0) {
165 // slot found
166 if (FLAG_trace_contexts) {
167 PrintF("=> found intermediate function in context slot %d\n",
168 index);
169 }
170 *index_ = index;
171 *attributes = READ_ONLY;
172 return context;
173 }
174 }
175 }
176
177 // proceed with enclosing context
178 if (context->IsGlobalContext()) {
179 follow_context_chain = false;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000180 } else if (context->is_function_context()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000181 context = Handle<Context>(Context::cast(context->closure()->context()));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000182 } else {
183 context = Handle<Context>(context->previous());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 }
185 } while (follow_context_chain);
186
187 // slot not found
188 if (FLAG_trace_contexts) {
189 PrintF("=> no property/slot found\n");
190 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000191 return Handle<Object>::null();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000192}
193
194
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000195#ifdef DEBUG
196bool Context::IsBootstrappingOrContext(Object* object) {
197 // During bootstrapping we allow all objects to pass as
198 // contexts. This is necessary to fix circular dependencies.
199 return Bootstrapper::IsActive() || object->IsContext();
200}
201
202
203bool Context::IsBootstrappingOrGlobalObject(Object* object) {
204 // During bootstrapping we allow all objects to pass as global
205 // objects. This is necessary to fix circular dependencies.
206 return Bootstrapper::IsActive() || object->IsGlobalObject();
207}
208#endif
209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210} } // namespace v8::internal