blob: 67a9fea8b896d2be7115b7fb56ad724cc02c4e0b [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 Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/contexts.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007#include "src/ast/scopeinfo.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/bootstrapper.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009#include "src/debug/debug.h"
10#include "src/isolate-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000011
12namespace v8 {
13namespace internal {
14
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015
16Handle<ScriptContextTable> ScriptContextTable::Extend(
17 Handle<ScriptContextTable> table, Handle<Context> script_context) {
18 Handle<ScriptContextTable> result;
19 int used = table->used();
20 int length = table->length();
21 CHECK(used >= 0 && length > 0 && used < length);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022 if (used + kFirstContextSlot == length) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023 CHECK(length < Smi::kMaxValue / 2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024 Isolate* isolate = table->GetIsolate();
25 Handle<FixedArray> copy =
26 isolate->factory()->CopyFixedArrayAndGrow(table, length);
27 copy->set_map(isolate->heap()->script_context_table_map());
28 result = Handle<ScriptContextTable>::cast(copy);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040029 } else {
30 result = table;
31 }
32 result->set_used(used + 1);
33
34 DCHECK(script_context->IsScriptContext());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 result->set(used + kFirstContextSlot, *script_context);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040036 return result;
37}
38
39
40bool ScriptContextTable::Lookup(Handle<ScriptContextTable> table,
41 Handle<String> name, LookupResult* result) {
42 for (int i = 0; i < table->used(); i++) {
43 Handle<Context> context = GetContext(table, i);
44 DCHECK(context->IsScriptContext());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045 Handle<ScopeInfo> scope_info(context->scope_info());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040046 int slot_index = ScopeInfo::ContextSlotIndex(
47 scope_info, name, &result->mode, &result->init_flag,
48 &result->maybe_assigned_flag);
49
50 if (slot_index >= 0) {
51 result->context_index = i;
52 result->slot_index = slot_index;
53 return true;
54 }
55 }
56 return false;
57}
58
59
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060bool Context::is_declaration_context() {
61 if (IsFunctionContext() || IsNativeContext() || IsScriptContext()) {
62 return true;
63 }
64 if (!IsBlockContext()) return false;
65 Object* ext = extension();
66 // If we have the special extension, we immediately know it must be a
67 // declaration scope. That's just a small performance shortcut.
68 return ext->IsSloppyBlockWithEvalContextExtension()
69 || ScopeInfo::cast(ext)->is_declaration_scope();
70}
71
72
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000073Context* Context::declaration_context() {
74 Context* current = this;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 while (!current->is_declaration_context()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000076 current = current->previous();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 DCHECK(current->closure() == closure());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000078 }
79 return current;
80}
81
Ben Murdoch097c5b22016-05-18 11:27:45 +010082Context* Context::closure_context() {
83 Context* current = this;
84 while (!current->IsFunctionContext() && !current->IsScriptContext() &&
85 !current->IsNativeContext()) {
86 current = current->previous();
87 DCHECK(current->closure() == closure());
88 }
89 return current;
90}
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000091
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092JSObject* Context::extension_object() {
93 DCHECK(IsNativeContext() || IsFunctionContext() || IsBlockContext());
94 HeapObject* object = extension();
95 if (object->IsTheHole()) return nullptr;
96 if (IsBlockContext()) {
97 if (!object->IsSloppyBlockWithEvalContextExtension()) return nullptr;
98 object = SloppyBlockWithEvalContextExtension::cast(object)->extension();
Steve Blocka7e24c12009-10-30 11:49:00 +000099 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100 DCHECK(object->IsJSContextExtensionObject() ||
101 (IsNativeContext() && object->IsJSGlobalObject()));
102 return JSObject::cast(object);
103}
104
105
106JSReceiver* Context::extension_receiver() {
107 DCHECK(IsNativeContext() || IsWithContext() ||
108 IsFunctionContext() || IsBlockContext());
109 return IsWithContext() ? JSReceiver::cast(extension()) : extension_object();
110}
111
112
113ScopeInfo* Context::scope_info() {
114 DCHECK(IsModuleContext() || IsScriptContext() || IsBlockContext());
115 HeapObject* object = extension();
116 if (object->IsSloppyBlockWithEvalContextExtension()) {
117 DCHECK(IsBlockContext());
118 object = SloppyBlockWithEvalContextExtension::cast(object)->scope_info();
119 }
120 return ScopeInfo::cast(object);
121}
122
123
124String* Context::catch_name() {
125 DCHECK(IsCatchContext());
126 return String::cast(extension());
127}
128
129
130JSGlobalObject* Context::global_object() {
131 return JSGlobalObject::cast(native_context()->extension());
Steve Blocka7e24c12009-10-30 11:49:00 +0000132}
133
134
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400135Context* Context::script_context() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 Context* current = this;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400137 while (!current->IsScriptContext()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 current = current->previous();
139 }
140 return current;
141}
142
143
Steve Blocka7e24c12009-10-30 11:49:00 +0000144JSObject* Context::global_proxy() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 return native_context()->global_proxy_object();
Steve Blocka7e24c12009-10-30 11:49:00 +0000146}
147
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148
Steve Blocka7e24c12009-10-30 11:49:00 +0000149void Context::set_global_proxy(JSObject* object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 native_context()->set_global_proxy_object(object);
151}
152
153
154/**
155 * Lookups a property in an object environment, taking the unscopables into
156 * account. This is used For HasBinding spec algorithms for ObjectEnvironment.
157 */
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158static Maybe<bool> UnscopableLookup(LookupIterator* it) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 Isolate* isolate = it->isolate();
160
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 Maybe<bool> found = JSReceiver::HasProperty(it);
162 if (!found.IsJust() || !found.FromJust()) return found;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 Handle<Object> unscopables;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000165 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
166 isolate, unscopables,
Ben Murdochda12d292016-06-02 14:46:10 +0100167 JSReceiver::GetProperty(Handle<JSReceiver>::cast(it->GetReceiver()),
168 isolate->factory()->unscopables_symbol()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169 Nothing<bool>());
170 if (!unscopables->IsJSReceiver()) return Just(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400171 Handle<Object> blacklist;
Ben Murdochda12d292016-06-02 14:46:10 +0100172 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
173 isolate, blacklist,
174 JSReceiver::GetProperty(Handle<JSReceiver>::cast(unscopables),
175 it->name()),
176 Nothing<bool>());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177 return Just(!blacklist->BooleanValue());
Steve Blocka7e24c12009-10-30 11:49:00 +0000178}
179
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400180static void GetAttributesAndBindingFlags(VariableMode mode,
181 InitializationFlag init_flag,
182 PropertyAttributes* attributes,
183 BindingFlags* binding_flags) {
184 switch (mode) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400185 case VAR:
186 *attributes = NONE;
187 *binding_flags = MUTABLE_IS_INITIALIZED;
188 break;
189 case LET:
190 *attributes = NONE;
191 *binding_flags = (init_flag == kNeedsInitialization)
192 ? MUTABLE_CHECK_INITIALIZED
193 : MUTABLE_IS_INITIALIZED;
194 break;
195 case CONST_LEGACY:
196 *attributes = READ_ONLY;
197 *binding_flags = (init_flag == kNeedsInitialization)
198 ? IMMUTABLE_CHECK_INITIALIZED
199 : IMMUTABLE_IS_INITIALIZED;
200 break;
201 case CONST:
202 *attributes = READ_ONLY;
203 *binding_flags = (init_flag == kNeedsInitialization)
204 ? IMMUTABLE_CHECK_INITIALIZED_HARMONY
205 : IMMUTABLE_IS_INITIALIZED_HARMONY;
206 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000207 case IMPORT:
208 // TODO(ES6)
209 UNREACHABLE();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400210 break;
211 case DYNAMIC:
212 case DYNAMIC_GLOBAL:
213 case DYNAMIC_LOCAL:
214 case TEMPORARY:
215 // Note: Fixed context slots are statically allocated by the compiler.
216 // Statically allocated variables always have a statically known mode,
217 // which is the mode with which they were declared when added to the
218 // scope. Thus, the DYNAMIC mode (which corresponds to dynamically
219 // declared variables that were introduced through declaration nodes)
220 // must not appear here.
221 UNREACHABLE();
222 break;
223 }
224}
225
Steve Blocka7e24c12009-10-30 11:49:00 +0000226
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000227Handle<Object> Context::Lookup(Handle<String> name,
228 ContextLookupFlags flags,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100229 int* index,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000230 PropertyAttributes* attributes,
231 BindingFlags* binding_flags) {
Steve Block44f0eee2011-05-26 01:26:41 +0100232 Isolate* isolate = GetIsolate();
233 Handle<Context> context(this, isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000234
235 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
Ben Murdochda12d292016-06-02 14:46:10 +0100236 bool failed_whitelist = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000237 *index = kNotFound;
Steve Blocka7e24c12009-10-30 11:49:00 +0000238 *attributes = ABSENT;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000239 *binding_flags = MISSING_BINDING;
Steve Blocka7e24c12009-10-30 11:49:00 +0000240
241 if (FLAG_trace_contexts) {
242 PrintF("Context::Lookup(");
243 name->ShortPrint();
244 PrintF(")\n");
245 }
246
247 do {
248 if (FLAG_trace_contexts) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100249 PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400250 if (context->IsScriptContext()) PrintF(" (script context)");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 if (context->IsNativeContext()) PrintF(" (native context)");
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 PrintF("\n");
253 }
254
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100255 // 1. Check global objects, subjects of with, and extension objects.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000256 if ((context->IsNativeContext() ||
257 (context->IsWithContext() && ((flags & SKIP_WITH_CONTEXT) == 0)) ||
258 context->IsFunctionContext() || context->IsBlockContext()) &&
259 context->extension_receiver() != nullptr) {
260 Handle<JSReceiver> object(context->extension_receiver());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400261
262 if (context->IsNativeContext()) {
263 if (FLAG_trace_contexts) {
264 PrintF(" - trying other script contexts\n");
265 }
266 // Try other script contexts.
267 Handle<ScriptContextTable> script_contexts(
268 context->global_object()->native_context()->script_context_table());
269 ScriptContextTable::LookupResult r;
270 if (ScriptContextTable::Lookup(script_contexts, name, &r)) {
271 if (FLAG_trace_contexts) {
272 Handle<Context> c = ScriptContextTable::GetContext(script_contexts,
273 r.context_index);
274 PrintF("=> found property in script context %d: %p\n",
275 r.context_index, reinterpret_cast<void*>(*c));
276 }
277 *index = r.slot_index;
278 GetAttributesAndBindingFlags(r.mode, r.init_flag, attributes,
279 binding_flags);
280 return ScriptContextTable::GetContext(script_contexts,
281 r.context_index);
282 }
283 }
284
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100285 // Context extension objects needs to behave as if they have no
286 // prototype. So even if we want to follow prototype chains, we need
287 // to only do a local lookup for context extension objects.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000288 Maybe<PropertyAttributes> maybe = Nothing<PropertyAttributes>();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100289 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
290 object->IsJSContextExtensionObject()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 maybe = JSReceiver::GetOwnPropertyAttributes(object, name);
292 } else if (context->IsWithContext()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 // A with context will never bind "this".
294 if (name->Equals(*isolate->factory()->this_string())) {
295 maybe = Just(ABSENT);
296 } else {
Ben Murdochda12d292016-06-02 14:46:10 +0100297 LookupIterator it(object, name, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 Maybe<bool> found = UnscopableLookup(&it);
299 if (found.IsNothing()) {
300 maybe = Nothing<PropertyAttributes>();
301 } else {
302 // Luckily, consumers of |maybe| only care whether the property
303 // was absent or not, so we can return a dummy |NONE| value
304 // for its attributes when it was present.
305 maybe = Just(found.FromJust() ? NONE : ABSENT);
306 }
307 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100308 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 maybe = JSReceiver::GetPropertyAttributes(object, name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100310 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000312 if (!maybe.IsJust()) return Handle<Object>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 DCHECK(!isolate->has_pending_exception());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314 *attributes = maybe.FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000316 if (maybe.FromJust() != ABSENT) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100317 if (FLAG_trace_contexts) {
318 PrintF("=> found property in context object %p\n",
319 reinterpret_cast<void*>(*object));
Ben Murdoch85b71792012-04-11 18:30:58 +0100320 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100321 return object;
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 }
323 }
324
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100325 // 2. Check the context proper if it has slots.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400326 if (context->IsFunctionContext() || context->IsBlockContext() ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000327 context->IsScriptContext()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100328 // Use serialized scope information of functions and blocks to search
329 // for the context index.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 Handle<ScopeInfo> scope_info(context->IsFunctionContext()
331 ? context->closure()->shared()->scope_info()
332 : context->scope_info());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100333 VariableMode mode;
334 InitializationFlag init_flag;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335 // TODO(sigurds) Figure out whether maybe_assigned_flag should
336 // be used to compute binding_flags.
337 MaybeAssignedFlag maybe_assigned_flag;
338 int slot_index = ScopeInfo::ContextSlotIndex(
339 scope_info, name, &mode, &init_flag, &maybe_assigned_flag);
340 DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100341 if (slot_index >= 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000342 if (FLAG_trace_contexts) {
343 PrintF("=> found local in context slot %d (mode = %d)\n",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100344 slot_index, mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100346 *index = slot_index;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400347 GetAttributesAndBindingFlags(mode, init_flag, attributes,
348 binding_flags);
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 return context;
350 }
351
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000352 // Check the slot corresponding to the intermediate context holding
353 // only the function name variable.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100354 if (follow_context_chain && context->IsFunctionContext()) {
355 VariableMode mode;
356 int function_index = scope_info->FunctionContextSlotIndex(*name, &mode);
357 if (function_index >= 0) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 if (FLAG_trace_contexts) {
359 PrintF("=> found intermediate function in context slot %d\n",
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100360 function_index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100362 *index = function_index;
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 *attributes = READ_ONLY;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000364 DCHECK(mode == CONST_LEGACY || mode == CONST);
365 *binding_flags = (mode == CONST_LEGACY)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100366 ? IMMUTABLE_IS_INITIALIZED : IMMUTABLE_IS_INITIALIZED_HARMONY;
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 return context;
368 }
369 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100370
371 } else if (context->IsCatchContext()) {
372 // Catch contexts have the variable name in the extension slot.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 if (String::Equals(name, handle(context->catch_name()))) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100374 if (FLAG_trace_contexts) {
375 PrintF("=> found in catch context\n");
376 }
377 *index = Context::THROWN_OBJECT_INDEX;
378 *attributes = NONE;
379 *binding_flags = MUTABLE_IS_INITIALIZED;
380 return context;
381 }
Ben Murdochda12d292016-06-02 14:46:10 +0100382 } else if (context->IsDebugEvaluateContext()) {
383 // Check materialized locals.
384 Object* obj = context->get(EXTENSION_INDEX);
385 if (obj->IsJSReceiver()) {
386 Handle<JSReceiver> extension(JSReceiver::cast(obj));
387 LookupIterator it(extension, name, extension);
388 Maybe<bool> found = JSReceiver::HasProperty(&it);
389 if (found.FromMaybe(false)) {
390 *attributes = NONE;
391 return extension;
392 }
393 }
394 // Check the original context, but do not follow its context chain.
395 obj = context->get(WRAPPED_CONTEXT_INDEX);
396 if (obj->IsContext()) {
397 Handle<Object> result = Context::cast(obj)->Lookup(
398 name, DONT_FOLLOW_CHAINS, index, attributes, binding_flags);
399 if (!result.is_null()) return result;
400 }
401 // Check whitelist. Names that do not pass whitelist shall only resolve
402 // to with, script or native contexts up the context chain.
403 obj = context->get(WHITE_LIST_INDEX);
404 if (obj->IsStringSet()) {
405 failed_whitelist = failed_whitelist || !StringSet::cast(obj)->Has(name);
406 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000407 }
408
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100409 // 3. Prepare to continue with the previous (next outermost) context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000410 if (context->IsNativeContext() ||
411 ((flags & STOP_AT_DECLARATION_SCOPE) != 0 &&
412 context->is_declaration_context())) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 follow_context_chain = false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000414 } else {
Ben Murdochda12d292016-06-02 14:46:10 +0100415 do {
416 context = Handle<Context>(context->previous(), isolate);
417 // If we come across a whitelist context, and the name is not
418 // whitelisted, then only consider with, script or native contexts.
419 } while (failed_whitelist && !context->IsScriptContext() &&
420 !context->IsNativeContext() && !context->IsWithContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000421 }
422 } while (follow_context_chain);
423
Steve Blocka7e24c12009-10-30 11:49:00 +0000424 if (FLAG_trace_contexts) {
425 PrintF("=> no property/slot found\n");
426 }
427 return Handle<Object>::null();
428}
429
430
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000431void Context::InitializeGlobalSlots() {
432 DCHECK(IsScriptContext());
433 DisallowHeapAllocation no_gc;
434
435 ScopeInfo* scope_info = this->scope_info();
436
437 int context_globals = scope_info->ContextGlobalCount();
438 if (context_globals > 0) {
439 PropertyCell* empty_cell = GetHeap()->empty_property_cell();
440
441 int context_locals = scope_info->ContextLocalCount();
442 int index = Context::MIN_CONTEXT_SLOTS + context_locals;
443 for (int i = 0; i < context_globals; i++) {
444 set(index++, empty_cell);
445 }
446 }
447}
448
449
Ben Murdochb0fe1622011-05-05 13:52:32 +0100450void Context::AddOptimizedFunction(JSFunction* function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 DCHECK(IsNativeContext());
452#ifdef ENABLE_SLOW_DCHECKS
453 if (FLAG_enable_slow_asserts) {
454 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
455 while (!element->IsUndefined()) {
456 CHECK(element != function);
457 element = JSFunction::cast(element)->next_function_link();
458 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100459 }
460
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000461 // Check that the context belongs to the weak native contexts list.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100462 bool found = false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000463 Object* context = GetHeap()->native_contexts_list();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100464 while (!context->IsUndefined()) {
465 if (context == this) {
466 found = true;
467 break;
468 }
469 context = Context::cast(context)->get(Context::NEXT_CONTEXT_LINK);
470 }
471 CHECK(found);
472#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473
474 // If the function link field is already used then the function was
475 // enqueued as a code flushing candidate and we remove it now.
476 if (!function->next_function_link()->IsUndefined()) {
477 CodeFlusher* flusher = GetHeap()->mark_compact_collector()->code_flusher();
478 flusher->EvictCandidate(function);
479 }
480
481 DCHECK(function->next_function_link()->IsUndefined());
482
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000483 function->set_next_function_link(get(OPTIMIZED_FUNCTIONS_LIST),
484 UPDATE_WEAK_WRITE_BARRIER);
485 set(OPTIMIZED_FUNCTIONS_LIST, function, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100486}
487
488
489void Context::RemoveOptimizedFunction(JSFunction* function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 DCHECK(IsNativeContext());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100491 Object* element = get(OPTIMIZED_FUNCTIONS_LIST);
492 JSFunction* prev = NULL;
493 while (!element->IsUndefined()) {
494 JSFunction* element_function = JSFunction::cast(element);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495 DCHECK(element_function->next_function_link()->IsUndefined() ||
Ben Murdochb0fe1622011-05-05 13:52:32 +0100496 element_function->next_function_link()->IsJSFunction());
497 if (element_function == function) {
498 if (prev == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000499 set(OPTIMIZED_FUNCTIONS_LIST, element_function->next_function_link(),
500 UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100501 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 prev->set_next_function_link(element_function->next_function_link(),
503 UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100504 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000505 element_function->set_next_function_link(GetHeap()->undefined_value(),
506 UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100507 return;
508 }
509 prev = element_function;
510 element = element_function->next_function_link();
511 }
512 UNREACHABLE();
513}
514
515
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516void Context::SetOptimizedFunctionsListHead(Object* head) {
517 DCHECK(IsNativeContext());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000518 set(OPTIMIZED_FUNCTIONS_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519}
520
521
Ben Murdochb0fe1622011-05-05 13:52:32 +0100522Object* Context::OptimizedFunctionsListHead() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523 DCHECK(IsNativeContext());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100524 return get(OPTIMIZED_FUNCTIONS_LIST);
525}
526
527
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528void Context::AddOptimizedCode(Code* code) {
529 DCHECK(IsNativeContext());
530 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
531 DCHECK(code->next_code_link()->IsUndefined());
532 code->set_next_code_link(get(OPTIMIZED_CODE_LIST));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000533 set(OPTIMIZED_CODE_LIST, code, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534}
535
536
537void Context::SetOptimizedCodeListHead(Object* head) {
538 DCHECK(IsNativeContext());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000539 set(OPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540}
541
542
543Object* Context::OptimizedCodeListHead() {
544 DCHECK(IsNativeContext());
545 return get(OPTIMIZED_CODE_LIST);
546}
547
548
549void Context::SetDeoptimizedCodeListHead(Object* head) {
550 DCHECK(IsNativeContext());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551 set(DEOPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552}
553
554
555Object* Context::DeoptimizedCodeListHead() {
556 DCHECK(IsNativeContext());
557 return get(DEOPTIMIZED_CODE_LIST);
558}
559
560
561Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() {
562 Isolate* isolate = GetIsolate();
563 Handle<Object> result(error_message_for_code_gen_from_strings(), isolate);
564 if (!result->IsUndefined()) return result;
565 return isolate->factory()->NewStringFromStaticChars(
566 "Code generation from strings disallowed for this context");
Ben Murdochb0fe1622011-05-05 13:52:32 +0100567}
568
569
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000570#define COMPARE_NAME(index, type, name) \
571 if (string->IsOneByteEqualTo(STATIC_CHAR_VECTOR(#name))) return index;
572
573int Context::ImportedFieldIndexForName(Handle<String> string) {
574 NATIVE_CONTEXT_IMPORTED_FIELDS(COMPARE_NAME)
575 return kNotFound;
576}
577
578
579int Context::IntrinsicIndexForName(Handle<String> string) {
580 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(COMPARE_NAME);
581 return kNotFound;
582}
583
584#undef COMPARE_NAME
585
586
Steve Blocka7e24c12009-10-30 11:49:00 +0000587#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000588
589bool Context::IsBootstrappingOrNativeContext(Isolate* isolate, Object* object) {
590 // During bootstrapping we allow all objects to pass as global
591 // objects. This is necessary to fix circular dependencies.
592 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
593 isolate->bootstrapper()->IsActive() || object->IsNativeContext();
594}
595
596
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597bool Context::IsBootstrappingOrValidParentContext(
598 Object* object, Context* child) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000599 // During bootstrapping we allow all objects to pass as
600 // contexts. This is necessary to fix circular dependencies.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 if (child->GetIsolate()->bootstrapper()->IsActive()) return true;
602 if (!object->IsContext()) return false;
603 Context* context = Context::cast(object);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400604 return context->IsNativeContext() || context->IsScriptContext() ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000605 context->IsModuleContext() || !child->IsModuleContext();
Steve Blocka7e24c12009-10-30 11:49:00 +0000606}
607
Steve Blocka7e24c12009-10-30 11:49:00 +0000608#endif
609
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000610
611void Context::IncrementErrorsThrown() {
612 DCHECK(IsNativeContext());
613
614 int previous_value = errors_thrown()->value();
615 set_errors_thrown(Smi::FromInt(previous_value + 1));
616}
617
618
619int Context::GetErrorsThrown() { return errors_thrown()->value(); }
620
621} // namespace internal
622} // namespace v8