blob: 57490a118f5d9663ab55d6bb95a0ed4890ff3688 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/bootstrapper.h"
8#include "src/debug.h"
9#include "src/scopeinfo.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000010
11namespace v8 {
12namespace internal {
13
Emily Bernierd0a1eb72015-03-24 16:35:39 -040014
15Handle<ScriptContextTable> ScriptContextTable::Extend(
16 Handle<ScriptContextTable> table, Handle<Context> script_context) {
17 Handle<ScriptContextTable> result;
18 int used = table->used();
19 int length = table->length();
20 CHECK(used >= 0 && length > 0 && used < length);
21 if (used + 1 == length) {
22 CHECK(length < Smi::kMaxValue / 2);
23 result = Handle<ScriptContextTable>::cast(
24 FixedArray::CopySize(table, length * 2));
25 } else {
26 result = table;
27 }
28 result->set_used(used + 1);
29
30 DCHECK(script_context->IsScriptContext());
31 result->set(used + 1, *script_context);
32 return result;
33}
34
35
36bool ScriptContextTable::Lookup(Handle<ScriptContextTable> table,
37 Handle<String> name, LookupResult* result) {
38 for (int i = 0; i < table->used(); i++) {
39 Handle<Context> context = GetContext(table, i);
40 DCHECK(context->IsScriptContext());
41 Handle<ScopeInfo> scope_info(ScopeInfo::cast(context->extension()));
42 int slot_index = ScopeInfo::ContextSlotIndex(
43 scope_info, name, &result->mode, &result->init_flag,
44 &result->maybe_assigned_flag);
45
46 if (slot_index >= 0) {
47 result->context_index = i;
48 result->slot_index = slot_index;
49 return true;
50 }
51 }
52 return false;
53}
54
55
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000056Context* Context::declaration_context() {
57 Context* current = this;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040058 while (!current->IsFunctionContext() && !current->IsNativeContext() &&
59 !current->IsScriptContext()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000060 current = current->previous();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 DCHECK(current->closure() == closure());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000062 }
63 return current;
64}
65
66
Steve Blocka7e24c12009-10-30 11:49:00 +000067JSBuiltinsObject* Context::builtins() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 GlobalObject* object = global_object();
Steve Blocka7e24c12009-10-30 11:49:00 +000069 if (object->IsJSGlobalObject()) {
70 return JSGlobalObject::cast(object)->builtins();
71 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072 DCHECK(object->IsJSBuiltinsObject());
Steve Blocka7e24c12009-10-30 11:49:00 +000073 return JSBuiltinsObject::cast(object);
74 }
75}
76
77
Emily Bernierd0a1eb72015-03-24 16:35:39 -040078Context* Context::script_context() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 Context* current = this;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040080 while (!current->IsScriptContext()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 current = current->previous();
82 }
83 return current;
84}
85
86
87Context* Context::native_context() {
Steve Blocka7e24c12009-10-30 11:49:00 +000088 // Fast case: the global object for this context has been set. In
89 // that case, the global object has a direct pointer to the global
90 // context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091 if (global_object()->IsGlobalObject()) {
92 return global_object()->native_context();
Steve Blocka7e24c12009-10-30 11:49:00 +000093 }
Leon Clarkee46be812010-01-19 14:06:41 +000094
Steve Blocka7e24c12009-10-30 11:49:00 +000095 // During bootstrapping, the global object might not be set and we
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 // have to search the context chain to find the native context.
97 DCHECK(this->GetIsolate()->bootstrapper()->IsActive());
Steve Blocka7e24c12009-10-30 11:49:00 +000098 Context* current = this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 while (!current->IsNativeContext()) {
Leon Clarkee46be812010-01-19 14:06:41 +0000100 JSFunction* closure = JSFunction::cast(current->closure());
101 current = Context::cast(closure->context());
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 }
103 return current;
104}
105
106
107JSObject* Context::global_proxy() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 return native_context()->global_proxy_object();
Steve Blocka7e24c12009-10-30 11:49:00 +0000109}
110
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111
Steve Blocka7e24c12009-10-30 11:49:00 +0000112void Context::set_global_proxy(JSObject* object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 native_context()->set_global_proxy_object(object);
114}
115
116
117/**
118 * Lookups a property in an object environment, taking the unscopables into
119 * account. This is used For HasBinding spec algorithms for ObjectEnvironment.
120 */
121static Maybe<PropertyAttributes> UnscopableLookup(LookupIterator* it) {
122 Isolate* isolate = it->isolate();
123
124 Maybe<PropertyAttributes> attrs = JSReceiver::GetPropertyAttributes(it);
125 DCHECK(attrs.has_value || isolate->has_pending_exception());
126 if (!attrs.has_value || attrs.value == ABSENT) return attrs;
127
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400128 Handle<Symbol> unscopables_symbol = isolate->factory()->unscopables_symbol();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 Handle<Object> receiver = it->GetReceiver();
130 Handle<Object> unscopables;
131 MaybeHandle<Object> maybe_unscopables =
132 Object::GetProperty(receiver, unscopables_symbol);
133 if (!maybe_unscopables.ToHandle(&unscopables)) {
134 return Maybe<PropertyAttributes>();
135 }
136 if (!unscopables->IsSpecObject()) return attrs;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400137 Handle<Object> blacklist;
138 MaybeHandle<Object> maybe_blacklist =
139 Object::GetProperty(unscopables, it->name());
140 if (!maybe_blacklist.ToHandle(&blacklist)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141 DCHECK(isolate->has_pending_exception());
142 return Maybe<PropertyAttributes>();
143 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400144 if (!blacklist->IsUndefined()) return maybe(ABSENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 return attrs;
Steve Blocka7e24c12009-10-30 11:49:00 +0000146}
147
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400148static void GetAttributesAndBindingFlags(VariableMode mode,
149 InitializationFlag init_flag,
150 PropertyAttributes* attributes,
151 BindingFlags* binding_flags) {
152 switch (mode) {
153 case INTERNAL: // Fall through.
154 case VAR:
155 *attributes = NONE;
156 *binding_flags = MUTABLE_IS_INITIALIZED;
157 break;
158 case LET:
159 *attributes = NONE;
160 *binding_flags = (init_flag == kNeedsInitialization)
161 ? MUTABLE_CHECK_INITIALIZED
162 : MUTABLE_IS_INITIALIZED;
163 break;
164 case CONST_LEGACY:
165 *attributes = READ_ONLY;
166 *binding_flags = (init_flag == kNeedsInitialization)
167 ? IMMUTABLE_CHECK_INITIALIZED
168 : IMMUTABLE_IS_INITIALIZED;
169 break;
170 case CONST:
171 *attributes = READ_ONLY;
172 *binding_flags = (init_flag == kNeedsInitialization)
173 ? IMMUTABLE_CHECK_INITIALIZED_HARMONY
174 : IMMUTABLE_IS_INITIALIZED_HARMONY;
175 break;
176 case MODULE:
177 *attributes = READ_ONLY;
178 *binding_flags = IMMUTABLE_IS_INITIALIZED_HARMONY;
179 break;
180 case DYNAMIC:
181 case DYNAMIC_GLOBAL:
182 case DYNAMIC_LOCAL:
183 case TEMPORARY:
184 // Note: Fixed context slots are statically allocated by the compiler.
185 // Statically allocated variables always have a statically known mode,
186 // which is the mode with which they were declared when added to the
187 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
188 // declared variables that were introduced through declaration nodes)
189 // must not appear here.
190 UNREACHABLE();
191 break;
192 }
193}
194
Steve Blocka7e24c12009-10-30 11:49:00 +0000195
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000196Handle<Object> Context::Lookup(Handle<String> name,
197 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100198 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000199 PropertyAttributes* attributes,
200 BindingFlags* binding_flags) {
Steve Block44f0eee2011-05-26 01:26:41 +0100201 Isolate* isolate = GetIsolate();
202 Handle<Context> context(this, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000203
204 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100205 *index = -1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000206 *attributes = ABSENT;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000207 *binding_flags = MISSING_BINDING;
Steve Blocka7e24c12009-10-30 11:49:00 +0000208
209 if (FLAG_trace_contexts) {
210 PrintF("Context::Lookup(");
211 name->ShortPrint();
212 PrintF(")\n");
213 }
214
215 do {
216 if (FLAG_trace_contexts) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100217 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400218 if (context->IsScriptContext()) PrintF(" (script context)");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 if (context->IsNativeContext()) PrintF(" (native context)");
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 PrintF("\n");
221 }
222
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400223
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100224 // 1. Check global objects, subjects of with, and extension objects.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000225 if (context->IsNativeContext() ||
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100226 context->IsWithContext() ||
227 (context->IsFunctionContext() && context->has_extension())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228 Handle<JSReceiver> object(
229 JSReceiver::cast(context->extension()), isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400230
231 if (context->IsNativeContext()) {
232 if (FLAG_trace_contexts) {
233 PrintF(" - trying other script contexts\n");
234 }
235 // Try other script contexts.
236 Handle<ScriptContextTable> script_contexts(
237 context->global_object()->native_context()->script_context_table());
238 ScriptContextTable::LookupResult r;
239 if (ScriptContextTable::Lookup(script_contexts, name, &r)) {
240 if (FLAG_trace_contexts) {
241 Handle<Context> c = ScriptContextTable::GetContext(script_contexts,
242 r.context_index);
243 PrintF("=> found property in script context %d: %p\n",
244 r.context_index, reinterpret_cast<void*>(*c));
245 }
246 *index = r.slot_index;
247 GetAttributesAndBindingFlags(r.mode, r.init_flag, attributes,
248 binding_flags);
249 return ScriptContextTable::GetContext(script_contexts,
250 r.context_index);
251 }
252 }
253
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100254 // Context extension objects needs to behave as if they have no
255 // prototype. So even if we want to follow prototype chains, we need
256 // to only do a local lookup for context extension objects.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 Maybe<PropertyAttributes> maybe;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100258 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
259 object->IsJSContextExtensionObject()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 maybe = JSReceiver::GetOwnPropertyAttributes(object, name);
261 } else if (context->IsWithContext()) {
262 LookupIterator it(object, name);
263 maybe = UnscopableLookup(&it);
Ben Murdoch85b71792012-04-11 18:30:58 +0100264 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 maybe = JSReceiver::GetPropertyAttributes(object, name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100266 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267
268 if (!maybe.has_value) return Handle<Object>();
269 DCHECK(!isolate->has_pending_exception());
270 *attributes = maybe.value;
271
272 if (maybe.value != ABSENT) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100273 if (FLAG_trace_contexts) {
274 PrintF("=> found property in context object %p\n",
275 reinterpret_cast<void*>(*object));
Ben Murdoch85b71792012-04-11 18:30:58 +0100276 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100277 return object;
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 }
279 }
280
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100281 // 2. Check the context proper if it has slots.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400282 if (context->IsFunctionContext() || context->IsBlockContext() ||
283 (FLAG_harmony_scoping && context->IsScriptContext())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100284 // Use serialized scope information of functions and blocks to search
285 // for the context index.
286 Handle<ScopeInfo> scope_info;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000287 if (context->IsFunctionContext()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100288 scope_info = Handle<ScopeInfo>(
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000289 context->closure()->shared()->scope_info(), isolate);
290 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100291 scope_info = Handle<ScopeInfo>(
292 ScopeInfo::cast(context->extension()), isolate);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000293 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100294 VariableMode mode;
295 InitializationFlag init_flag;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 // TODO(sigurds) Figure out whether maybe_assigned_flag should
297 // be used to compute binding_flags.
298 MaybeAssignedFlag maybe_assigned_flag;
299 int slot_index = ScopeInfo::ContextSlotIndex(
300 scope_info, name, &mode, &init_flag, &maybe_assigned_flag);
301 DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100302 if (slot_index >= 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 if (FLAG_trace_contexts) {
304 PrintF("=> found local in context slot %d (mode = %d)\n",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100305 slot_index, mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000306 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100307 *index = slot_index;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400308 GetAttributesAndBindingFlags(mode, init_flag, attributes,
309 binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000310 return context;
311 }
312
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000313 // Check the slot corresponding to the intermediate context holding
314 // only the function name variable.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100315 if (follow_context_chain && context->IsFunctionContext()) {
316 VariableMode mode;
317 int function_index = scope_info->FunctionContextSlotIndex(*name, &mode);
318 if (function_index >= 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 if (FLAG_trace_contexts) {
320 PrintF("=> found intermediate function in context slot %d\n",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100321 function_index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100323 *index = function_index;
Steve Blocka7e24c12009-10-30 11:49:00 +0000324 *attributes = READ_ONLY;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 DCHECK(mode == CONST_LEGACY || mode == CONST);
326 *binding_flags = (mode == CONST_LEGACY)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100327 ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY;
Steve Blocka7e24c12009-10-30 11:49:00 +0000328 return context;
329 }
330 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100331
332 } else if (context->IsCatchContext()) {
333 // Catch contexts have the variable name in the extension slot.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 if (String::Equals(name, handle(String::cast(context->extension())))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100335 if (FLAG_trace_contexts) {
336 PrintF("=> found in catch context\n");
337 }
338 *index = Context::THROWN_OBJECT_INDEX;
339 *attributes = NONE;
340 *binding_flags = MUTABLE_IS_INITIALIZED;
341 return context;
342 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 }
344
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100345 // 3. Prepare to continue with the previous (next outermost) context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 if (context->IsNativeContext()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 follow_context_chain = false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000348 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100349 context = Handle<Context>(context->previous(), isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 }
351 } while (follow_context_chain);
352
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 if (FLAG_trace_contexts) {
354 PrintF("=> no property/slot found\n");
355 }
356 return Handle<Object>::null();
357}
358
359
Ben Murdochb0fe1622011-05-05 13:52:32 +0100360void Context::AddOptimizedFunction(JSFunction* function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000361 DCHECK(IsNativeContext());
362#ifdef ENABLE_SLOW_DCHECKS
363 if (FLAG_enable_slow_asserts) {
364 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
365 while (!element->IsUndefined()) {
366 CHECK(element != function);
367 element = JSFunction::cast(element)->next_function_link();
368 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100369 }
370
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 // Check that the context belongs to the weak native contexts list.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100372 bool found = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000373 Object* context = GetHeap()->native_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100374 while (!context->IsUndefined()) {
375 if (context == this) {
376 found = true;
377 break;
378 }
379 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
380 }
381 CHECK(found);
382#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383
384 // If the function link field is already used then the function was
385 // enqueued as a code flushing candidate and we remove it now.
386 if (!function->next_function_link()->IsUndefined()) {
387 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
388 flusher->EvictCandidate(function);
389 }
390
391 DCHECK(function->next_function_link()->IsUndefined());
392
Ben Murdochb0fe1622011-05-05 13:52:32 +0100393 function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST));
394 set(OPTIMIZED_FUNCTIONS_LIST, function);
395}
396
397
398void Context::RemoveOptimizedFunction(JSFunction* function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 DCHECK(IsNativeContext());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100400 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
401 JSFunction* prev = NULL;
402 while (!element->IsUndefined()) {
403 JSFunction* element_function = JSFunction::cast(element);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 DCHECK(element_function->next_function_link()->IsUndefined() ||
Ben Murdochb0fe1622011-05-05 13:52:32 +0100405 element_function->next_function_link()->IsJSFunction());
406 if (element_function == function) {
407 if (prev == NULL) {
408 set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link());
409 } else {
410 prev->set_next_function_link(element_function->next_function_link());
411 }
Steve Block44f0eee2011-05-26 01:26:41 +0100412 element_function->set_next_function_link(GetHeap()->undefined_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100413 return;
414 }
415 prev = element_function;
416 element = element_function->next_function_link();
417 }
418 UNREACHABLE();
419}
420
421
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000422void Context::SetOptimizedFunctionsListHead(Object* head) {
423 DCHECK(IsNativeContext());
424 set(OPTIMIZED_FUNCTIONS_LIST, head);
425}
426
427
Ben Murdochb0fe1622011-05-05 13:52:32 +0100428Object* Context::OptimizedFunctionsListHead() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 DCHECK(IsNativeContext());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100430 return get(OPTIMIZED_FUNCTIONS_LIST);
431}
432
433
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434void Context::AddOptimizedCode(Code* code) {
435 DCHECK(IsNativeContext());
436 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
437 DCHECK(code->next_code_link()->IsUndefined());
438 code->set_next_code_link(get(OPTIMIZED_CODE_LIST));
439 set(OPTIMIZED_CODE_LIST, code);
440}
441
442
443void Context::SetOptimizedCodeListHead(Object* head) {
444 DCHECK(IsNativeContext());
445 set(OPTIMIZED_CODE_LIST, head);
446}
447
448
449Object* Context::OptimizedCodeListHead() {
450 DCHECK(IsNativeContext());
451 return get(OPTIMIZED_CODE_LIST);
452}
453
454
455void Context::SetDeoptimizedCodeListHead(Object* head) {
456 DCHECK(IsNativeContext());
457 set(DEOPTIMIZED_CODE_LIST, head);
458}
459
460
461Object* Context::DeoptimizedCodeListHead() {
462 DCHECK(IsNativeContext());
463 return get(DEOPTIMIZED_CODE_LIST);
464}
465
466
467Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() {
468 Isolate* isolate = GetIsolate();
469 Handle<Object> result(error_message_for_code_gen_from_strings(), isolate);
470 if (!result->IsUndefined()) return result;
471 return isolate->factory()->NewStringFromStaticChars(
472 "Code generation from strings disallowed for this context");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100473}
474
475
Steve Blocka7e24c12009-10-30 11:49:00 +0000476#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477bool Context::IsBootstrappingOrValidParentContext(
478 Object* object, Context* child) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 // During bootstrapping we allow all objects to pass as
480 // contexts. This is necessary to fix circular dependencies.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 if (child->GetIsolate()->bootstrapper()->IsActive()) return true;
482 if (!object->IsContext()) return false;
483 Context* context = Context::cast(object);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400484 return context->IsNativeContext() || context->IsScriptContext() ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000485 context->IsModuleContext() || !child->IsModuleContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000486}
487
488
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489bool Context::IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 // During bootstrapping we allow all objects to pass as global
491 // objects. This is necessary to fix circular dependencies.
Steve Block44f0eee2011-05-26 01:26:41 +0100492 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
493 isolate->bootstrapper()->IsActive() ||
494 object->IsGlobalObject();
Steve Blocka7e24c12009-10-30 11:49:00 +0000495}
496#endif
497
498} } // namespace v8::internal