[V8] Introduce a QML compilation mode
[profile/ivi/qtjsbackend.git] / src / 3rdparty / v8 / src / contexts.cc
1 // Copyright 2011 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
37 Context* Context::declaration_context() {
38   Context* current = this;
39   while (!current->IsFunctionContext() && !current->IsGlobalContext()) {
40     current = current->previous();
41     ASSERT(current->closure() == closure());
42   }
43   return current;
44 }
45
46
47 JSBuiltinsObject* Context::builtins() {
48   GlobalObject* object = global();
49   if (object->IsJSGlobalObject()) {
50     return JSGlobalObject::cast(object)->builtins();
51   } else {
52     ASSERT(object->IsJSBuiltinsObject());
53     return JSBuiltinsObject::cast(object);
54   }
55 }
56
57
58 Context* Context::global_context() {
59   // Fast case: the global object for this context has been set.  In
60   // that case, the global object has a direct pointer to the global
61   // context.
62   if (global()->IsGlobalObject()) {
63     return global()->global_context();
64   }
65
66   // During bootstrapping, the global object might not be set and we
67   // have to search the context chain to find the global context.
68   ASSERT(Isolate::Current()->bootstrapper()->IsActive());
69   Context* current = this;
70   while (!current->IsGlobalContext()) {
71     JSFunction* closure = JSFunction::cast(current->closure());
72     current = Context::cast(closure->context());
73   }
74   return current;
75 }
76
77
78 JSObject* Context::global_proxy() {
79   return global_context()->global_proxy_object();
80 }
81
82 void Context::set_global_proxy(JSObject* object) {
83   global_context()->set_global_proxy_object(object);
84 }
85
86
87 Handle<Object> Context::Lookup(Handle<String> name,
88                                ContextLookupFlags flags,
89                                int* index,
90                                PropertyAttributes* attributes,
91                                BindingFlags* binding_flags) {
92   Isolate* isolate = GetIsolate();
93   Handle<Context> context(this, isolate);
94
95   bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
96   *index = -1;
97   *attributes = ABSENT;
98   *binding_flags = MISSING_BINDING;
99
100   if (FLAG_trace_contexts) {
101     PrintF("Context::Lookup(");
102     name->ShortPrint();
103     PrintF(")\n");
104   }
105
106   Handle<JSObject> qml_global;
107   Handle<JSObject> qml_global_global;
108
109   do {
110     if (FLAG_trace_contexts) {
111       PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
112       if (context->IsGlobalContext()) PrintF(" (global context)");
113       PrintF("\n");
114     }
115
116     if (qml_global.is_null() && !context->qml_global()->IsUndefined()) {
117       qml_global = Handle<JSObject>(context->qml_global(), isolate);
118       qml_global_global = Handle<JSObject>(context->global(), isolate);
119     }
120
121     // 1. Check global objects, subjects of with, and extension objects.
122     if (context->IsGlobalContext() ||
123         context->IsWithContext() ||
124         (context->IsFunctionContext() && context->has_extension())) {
125       Handle<JSObject> object(JSObject::cast(context->extension()), isolate);
126       // Context extension objects needs to behave as if they have no
127       // prototype.  So even if we want to follow prototype chains, we need
128       // to only do a local lookup for context extension objects.
129       if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
130           object->IsJSContextExtensionObject()) {
131         *attributes = object->GetLocalPropertyAttribute(*name);
132       } else {
133         *attributes = object->GetPropertyAttribute(*name);
134       }
135       if (*attributes != ABSENT) {
136         if (FLAG_trace_contexts) {
137           PrintF("=> found property in context object %p\n",
138                  reinterpret_cast<void*>(*object));
139         }
140         return object;
141       }
142     }
143
144     // 2. Check the context proper if it has slots.
145     if (context->IsFunctionContext() || context->IsBlockContext()) {
146       // Use serialized scope information of functions and blocks to search
147       // for the context index.
148       Handle<ScopeInfo> scope_info;
149       if (context->IsFunctionContext()) {
150         scope_info = Handle<ScopeInfo>(
151             context->closure()->shared()->scope_info(), isolate);
152       } else {
153         scope_info = Handle<ScopeInfo>(
154             ScopeInfo::cast(context->extension()), isolate);
155       }
156       VariableMode mode;
157       InitializationFlag init_flag;
158       int slot_index = scope_info->ContextSlotIndex(*name, &mode, &init_flag);
159       ASSERT(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
160       if (slot_index >= 0) {
161         if (FLAG_trace_contexts) {
162           PrintF("=> found local in context slot %d (mode = %d)\n",
163                  slot_index, mode);
164         }
165         *index = slot_index;
166         // Note: Fixed context slots are statically allocated by the compiler.
167         // Statically allocated variables always have a statically known mode,
168         // which is the mode with which they were declared when added to the
169         // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
170         // declared variables that were introduced through declaration nodes)
171         // must not appear here.
172         switch (mode) {
173           case INTERNAL:  // Fall through.
174           case VAR:
175             *attributes = NONE;
176             *binding_flags = MUTABLE_IS_INITIALIZED;
177             break;
178           case LET:
179             *attributes = NONE;
180             *binding_flags = (init_flag == kNeedsInitialization)
181                 ? MUTABLE_CHECK_INITIALIZED : MUTABLE_IS_INITIALIZED;
182             break;
183           case CONST:
184             *attributes = READ_ONLY;
185             *binding_flags = (init_flag == kNeedsInitialization)
186                 ? IMMUTABLE_CHECK_INITIALIZED : IMMUTABLE_IS_INITIALIZED;
187             break;
188           case CONST_HARMONY:
189             *attributes = READ_ONLY;
190             *binding_flags = (init_flag == kNeedsInitialization)
191                 ? IMMUTABLE_CHECK_INITIALIZED_HARMONY :
192                 IMMUTABLE_IS_INITIALIZED_HARMONY;
193             break;
194           case DYNAMIC:
195           case DYNAMIC_GLOBAL:
196           case DYNAMIC_LOCAL:
197           case TEMPORARY:
198             UNREACHABLE();
199             break;
200         }
201         return context;
202       }
203
204       // Check the slot corresponding to the intermediate context holding
205       // only the function name variable.
206       if (follow_context_chain && context->IsFunctionContext()) {
207         VariableMode mode;
208         int function_index = scope_info->FunctionContextSlotIndex(*name, &mode);
209         if (function_index >= 0) {
210           if (FLAG_trace_contexts) {
211             PrintF("=> found intermediate function in context slot %d\n",
212                    function_index);
213           }
214           *index = function_index;
215           *attributes = READ_ONLY;
216           ASSERT(mode == CONST || mode == CONST_HARMONY);
217           *binding_flags = (mode == CONST)
218               ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY;
219           return context;
220         }
221       }
222
223     } else if (context->IsCatchContext()) {
224       // Catch contexts have the variable name in the extension slot.
225       if (name->Equals(String::cast(context->extension()))) {
226         if (FLAG_trace_contexts) {
227           PrintF("=> found in catch context\n");
228         }
229         *index = Context::THROWN_OBJECT_INDEX;
230         *attributes = NONE;
231         *binding_flags = MUTABLE_IS_INITIALIZED;
232         return context;
233       }
234     }
235
236     // 3. Prepare to continue with the previous (next outermost) context.
237     if (context->IsGlobalContext()) {
238       follow_context_chain = false;
239     } else {
240       context = Handle<Context>(context->previous(), isolate);
241     }
242   } while (follow_context_chain);
243
244   if (!qml_global.is_null()) {
245     if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0) {
246       *attributes = qml_global_global->GetLocalPropertyAttribute(*name);
247     } else {
248       *attributes = qml_global_global->GetPropertyAttribute(*name);
249     }
250
251     if (*attributes != ABSENT) {
252       *attributes = ABSENT;
253     } else {
254       if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0) {
255         *attributes = qml_global->GetLocalPropertyAttribute(*name);
256       } else {
257         *attributes = qml_global->GetPropertyAttribute(*name);
258       }
259
260       if (*attributes != ABSENT) {
261         // property found
262         if (FLAG_trace_contexts) {
263           PrintF("=> found property in qml global object %p\n",
264                  reinterpret_cast<void*>(*qml_global));
265         }
266         return qml_global;
267       }
268     }
269   }
270
271   if (FLAG_trace_contexts) {
272     PrintF("=> no property/slot found\n");
273   }
274   return Handle<Object>::null();
275 }
276
277
278 void Context::AddOptimizedFunction(JSFunction* function) {
279   ASSERT(IsGlobalContext());
280 #ifdef DEBUG
281   Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
282   while (!element->IsUndefined()) {
283     CHECK(element != function);
284     element = JSFunction::cast(element)->next_function_link();
285   }
286
287   CHECK(function->next_function_link()->IsUndefined());
288
289   // Check that the context belongs to the weak global contexts list.
290   bool found = false;
291   Object* context = GetHeap()->global_contexts_list();
292   while (!context->IsUndefined()) {
293     if (context == this) {
294       found = true;
295       break;
296     }
297     context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
298   }
299   CHECK(found);
300 #endif
301   function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST));
302   set(OPTIMIZED_FUNCTIONS_LIST, function);
303 }
304
305
306 void Context::RemoveOptimizedFunction(JSFunction* function) {
307   ASSERT(IsGlobalContext());
308   Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
309   JSFunction* prev = NULL;
310   while (!element->IsUndefined()) {
311     JSFunction* element_function = JSFunction::cast(element);
312     ASSERT(element_function->next_function_link()->IsUndefined() ||
313            element_function->next_function_link()->IsJSFunction());
314     if (element_function == function) {
315       if (prev == NULL) {
316         set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link());
317       } else {
318         prev->set_next_function_link(element_function->next_function_link());
319       }
320       element_function->set_next_function_link(GetHeap()->undefined_value());
321       return;
322     }
323     prev = element_function;
324     element = element_function->next_function_link();
325   }
326   UNREACHABLE();
327 }
328
329
330 Object* Context::OptimizedFunctionsListHead() {
331   ASSERT(IsGlobalContext());
332   return get(OPTIMIZED_FUNCTIONS_LIST);
333 }
334
335
336 void Context::ClearOptimizedFunctions() {
337   set(OPTIMIZED_FUNCTIONS_LIST, GetHeap()->undefined_value());
338 }
339
340
341 #ifdef DEBUG
342 bool Context::IsBootstrappingOrContext(Object* object) {
343   // During bootstrapping we allow all objects to pass as
344   // contexts. This is necessary to fix circular dependencies.
345   return Isolate::Current()->bootstrapper()->IsActive() || object->IsContext();
346 }
347
348
349 bool Context::IsBootstrappingOrGlobalObject(Object* object) {
350   // During bootstrapping we allow all objects to pass as global
351   // objects. This is necessary to fix circular dependencies.
352   Isolate* isolate = Isolate::Current();
353   return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
354       isolate->bootstrapper()->IsActive() ||
355       object->IsGlobalObject();
356 }
357 #endif
358
359 } }  // namespace v8::internal