Line data Source code
1 : // Copyright 2011 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/contexts.h"
6 :
7 : #include "src/ast/modules.h"
8 : #include "src/bootstrapper.h"
9 : #include "src/debug/debug.h"
10 : #include "src/isolate-inl.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 :
16 11154 : Handle<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 11154 : CHECK(used >= 0 && length > 0 && used < length);
22 11154 : if (used + kFirstContextSlot == length) {
23 8887 : CHECK(length < Smi::kMaxValue / 2);
24 : Isolate* isolate = table->GetIsolate();
25 : Handle<FixedArray> copy =
26 8887 : isolate->factory()->CopyFixedArrayAndGrow(table, length);
27 17774 : copy->set_map(isolate->heap()->script_context_table_map());
28 : result = Handle<ScriptContextTable>::cast(copy);
29 : } else {
30 : result = table;
31 : }
32 : result->set_used(used + 1);
33 :
34 : DCHECK(script_context->IsScriptContext());
35 11154 : result->set(used + kFirstContextSlot, *script_context);
36 11154 : return result;
37 : }
38 :
39 :
40 8918495 : bool ScriptContextTable::Lookup(Handle<ScriptContextTable> table,
41 : Handle<String> name, LookupResult* result) {
42 38219906 : for (int i = 0; i < table->used(); i++) {
43 10252881 : Handle<Context> context = GetContext(table, i);
44 : DCHECK(context->IsScriptContext());
45 10252881 : Handle<ScopeInfo> scope_info(context->scope_info());
46 : int slot_index = ScopeInfo::ContextSlotIndex(
47 : scope_info, name, &result->mode, &result->init_flag,
48 10252881 : &result->maybe_assigned_flag);
49 :
50 10252882 : if (slot_index >= 0) {
51 61424 : result->context_index = i;
52 61424 : result->slot_index = slot_index;
53 61424 : return true;
54 : }
55 : }
56 : return false;
57 : }
58 :
59 :
60 720393 : bool Context::is_declaration_context() {
61 723412 : if (IsFunctionContext() || IsNativeContext() || IsScriptContext() ||
62 3019 : IsModuleContext()) {
63 : return true;
64 : }
65 3019 : if (IsEvalContext())
66 234 : return closure()->shared()->language_mode() == LanguageMode::kStrict;
67 2785 : if (!IsBlockContext()) return false;
68 : Object* ext = extension();
69 : // If we have the special extension, we immediately know it must be a
70 : // declaration scope. That's just a small performance shortcut.
71 4140 : return ext->IsContextExtension() ||
72 4140 : ScopeInfo::cast(ext)->is_declaration_scope();
73 : }
74 :
75 :
76 430640 : Context* Context::declaration_context() {
77 : Context* current = this;
78 861835 : while (!current->is_declaration_context()) {
79 : current = current->previous();
80 : }
81 430640 : return current;
82 : }
83 :
84 10285 : Context* Context::closure_context() {
85 : Context* current = this;
86 35489 : while (!current->IsFunctionContext() && !current->IsScriptContext() &&
87 14909 : !current->IsModuleContext() && !current->IsNativeContext() &&
88 1156 : !current->IsEvalContext()) {
89 : current = current->previous();
90 : DCHECK(current->closure() == closure());
91 : }
92 10285 : return current;
93 : }
94 :
95 11069450 : JSObject* Context::extension_object() {
96 : DCHECK(IsNativeContext() || IsFunctionContext() || IsBlockContext() ||
97 : IsEvalContext());
98 : HeapObject* object = extension();
99 11069450 : if (object->IsTheHole(GetIsolate())) return nullptr;
100 6883481 : if (IsBlockContext()) {
101 22382 : if (!object->IsContextExtension()) return nullptr;
102 : object = JSObject::cast(ContextExtension::cast(object)->extension());
103 : }
104 : DCHECK(object->IsJSContextExtensionObject() ||
105 : (IsNativeContext() && object->IsJSGlobalObject()));
106 6863979 : return JSObject::cast(object);
107 : }
108 :
109 15633432 : JSReceiver* Context::extension_receiver() {
110 : DCHECK(IsNativeContext() || IsWithContext() || IsEvalContext() ||
111 : IsFunctionContext() || IsBlockContext());
112 15633432 : return IsWithContext() ? JSReceiver::cast(
113 : ContextExtension::cast(extension())->extension())
114 26415368 : : extension_object();
115 : }
116 :
117 16244273 : ScopeInfo* Context::scope_info() {
118 : DCHECK(!IsNativeContext());
119 16244273 : if (IsFunctionContext() || IsModuleContext() || IsEvalContext()) {
120 5333642 : return closure()->shared()->scope_info();
121 : }
122 : HeapObject* object = extension();
123 10910630 : if (object->IsContextExtension()) {
124 : DCHECK(IsBlockContext() || IsCatchContext() || IsWithContext() ||
125 : IsDebugEvaluateContext());
126 : object = ContextExtension::cast(object)->scope_info();
127 : }
128 10910630 : return ScopeInfo::cast(object);
129 : }
130 :
131 5243 : Module* Context::module() {
132 : Context* current = this;
133 10486 : while (!current->IsModuleContext()) {
134 : current = current->previous();
135 : }
136 5243 : return Module::cast(current->extension());
137 : }
138 :
139 1125 : String* Context::catch_name() {
140 : DCHECK(IsCatchContext());
141 1125 : return String::cast(ContextExtension::cast(extension())->extension());
142 : }
143 :
144 :
145 13936569 : JSGlobalObject* Context::global_object() {
146 13936569 : return JSGlobalObject::cast(native_context()->extension());
147 : }
148 :
149 :
150 0 : Context* Context::script_context() {
151 : Context* current = this;
152 0 : while (!current->IsScriptContext()) {
153 : current = current->previous();
154 : }
155 0 : return current;
156 : }
157 :
158 :
159 2523144 : JSObject* Context::global_proxy() {
160 2523144 : return native_context()->global_proxy_object();
161 : }
162 :
163 :
164 83572 : void Context::set_global_proxy(JSObject* object) {
165 : native_context()->set_global_proxy_object(object);
166 83572 : }
167 :
168 :
169 : /**
170 : * Lookups a property in an object environment, taking the unscopables into
171 : * account. This is used For HasBinding spec algorithms for ObjectEnvironment.
172 : */
173 2422977 : static Maybe<bool> UnscopableLookup(LookupIterator* it) {
174 : Isolate* isolate = it->isolate();
175 :
176 2422977 : Maybe<bool> found = JSReceiver::HasProperty(it);
177 4845918 : if (!found.IsJust() || !found.FromJust()) return found;
178 :
179 : Handle<Object> unscopables;
180 939678 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
181 : isolate, unscopables,
182 : JSReceiver::GetProperty(Handle<JSReceiver>::cast(it->GetReceiver()),
183 : isolate->factory()->unscopables_symbol()),
184 : Nothing<bool>());
185 469809 : if (!unscopables->IsJSReceiver()) return Just(true);
186 : Handle<Object> blacklist;
187 248088 : ASSIGN_RETURN_ON_EXCEPTION_VALUE(
188 : isolate, blacklist,
189 : JSReceiver::GetProperty(Handle<JSReceiver>::cast(unscopables),
190 : it->name()),
191 : Nothing<bool>());
192 124035 : return Just(!blacklist->BooleanValue());
193 : }
194 :
195 : static PropertyAttributes GetAttributesForMode(VariableMode mode) {
196 : DCHECK(IsDeclaredVariableMode(mode));
197 1864042 : return mode == CONST ? READ_ONLY : NONE;
198 : }
199 :
200 5534673 : Handle<Object> Context::Lookup(Handle<String> name, ContextLookupFlags flags,
201 : int* index, PropertyAttributes* attributes,
202 : InitializationFlag* init_flag,
203 : VariableMode* variable_mode,
204 : bool* is_sloppy_function_name) {
205 : Isolate* isolate = GetIsolate();
206 : Handle<Context> context(this, isolate);
207 :
208 5534673 : bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
209 : bool failed_whitelist = false;
210 5534673 : *index = kNotFound;
211 5534673 : *attributes = ABSENT;
212 5534673 : *init_flag = kCreatedInitialized;
213 5534673 : *variable_mode = VAR;
214 5534673 : if (is_sloppy_function_name != nullptr) {
215 2314373 : *is_sloppy_function_name = false;
216 : }
217 :
218 : if (FLAG_trace_contexts) {
219 : PrintF("Context::Lookup(");
220 : name->ShortPrint();
221 : PrintF(")\n");
222 : }
223 :
224 6357011 : do {
225 : if (FLAG_trace_contexts) {
226 : PrintF(" - looking in context %p", reinterpret_cast<void*>(*context));
227 : if (context->IsScriptContext()) PrintF(" (script context)");
228 : if (context->IsNativeContext()) PrintF(" (native context)");
229 : PrintF("\n");
230 : }
231 :
232 : // 1. Check global objects, subjects of with, and extension objects.
233 : DCHECK_IMPLIES(context->IsEvalContext(),
234 : context->extension()->IsTheHole(isolate));
235 28422995 : if ((context->IsNativeContext() ||
236 15603173 : (context->IsWithContext() && ((flags & SKIP_WITH_CONTEXT) == 0)) ||
237 26020315 : context->IsFunctionContext() || context->IsBlockContext()) &&
238 9906650 : context->extension_receiver() != nullptr) {
239 5721856 : Handle<JSReceiver> object(context->extension_receiver());
240 :
241 5721856 : if (context->IsNativeContext()) {
242 : if (FLAG_trace_contexts) {
243 : PrintF(" - trying other script contexts\n");
244 : }
245 : // Try other script contexts.
246 : Handle<ScriptContextTable> script_contexts(
247 2509270 : context->global_object()->native_context()->script_context_table());
248 : ScriptContextTable::LookupResult r;
249 2509270 : if (ScriptContextTable::Lookup(script_contexts, name, &r)) {
250 : if (FLAG_trace_contexts) {
251 : Handle<Context> c = ScriptContextTable::GetContext(script_contexts,
252 : r.context_index);
253 : PrintF("=> found property in script context %d: %p\n",
254 : r.context_index, reinterpret_cast<void*>(*c));
255 : }
256 79 : *index = r.slot_index;
257 79 : *variable_mode = r.mode;
258 79 : *init_flag = r.init_flag;
259 79 : *attributes = GetAttributesForMode(r.mode);
260 : return ScriptContextTable::GetContext(script_contexts,
261 79 : r.context_index);
262 : }
263 : }
264 :
265 : // Context extension objects needs to behave as if they have no
266 : // prototype. So even if we want to follow prototype chains, we need
267 : // to only do a local lookup for context extension objects.
268 : Maybe<PropertyAttributes> maybe = Nothing<PropertyAttributes>();
269 10643274 : if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
270 : object->IsJSContextExtensionObject()) {
271 1048828 : maybe = JSReceiver::GetOwnPropertyAttributes(object, name);
272 4672949 : } else if (context->IsWithContext()) {
273 : // A with context will never bind "this", but debug-eval may look into
274 : // a with context when resolving "this". Other synthetic variables such
275 : // as new.target may be resolved as DYNAMIC_LOCAL due to bug v8:5405 ,
276 : // skipping them here serves as a workaround until a more thorough
277 : // fix can be applied.
278 : // TODO(v8:5405): Replace this check with a DCHECK when resolution of
279 : // of synthetic variables does not go through this code path.
280 2423122 : if (ScopeInfo::VariableIsSynthetic(*name)) {
281 : maybe = Just(ABSENT);
282 : } else {
283 2422977 : LookupIterator it(object, name, object);
284 2422977 : Maybe<bool> found = UnscopableLookup(&it);
285 2422977 : if (found.IsNothing()) {
286 : maybe = Nothing<PropertyAttributes>();
287 : } else {
288 : // Luckily, consumers of |maybe| only care whether the property
289 : // was absent or not, so we can return a dummy |NONE| value
290 : // for its attributes when it was present.
291 2422902 : maybe = Just(found.FromJust() ? NONE : ABSENT);
292 : }
293 : }
294 : } else {
295 2249827 : maybe = JSReceiver::GetPropertyAttributes(object, name);
296 : }
297 :
298 5721777 : if (!maybe.IsJust()) return Handle<Object>();
299 : DCHECK(!isolate->has_pending_exception());
300 5721702 : *attributes = maybe.FromJust();
301 :
302 5721702 : if (maybe.FromJust() != ABSENT) {
303 : if (FLAG_trace_contexts) {
304 : PrintF("=> found property in context object %p\n",
305 : reinterpret_cast<void*>(*object));
306 : }
307 2082768 : return object;
308 : }
309 : }
310 :
311 : // 2. Check the context proper if it has slots.
312 23320266 : if (context->IsFunctionContext() || context->IsBlockContext() ||
313 18230932 : context->IsScriptContext() || context->IsEvalContext() ||
314 3290146 : context->IsModuleContext()) {
315 : // Use serialized scope information of functions and blocks to search
316 : // for the context index.
317 4938177 : Handle<ScopeInfo> scope_info(context->scope_info());
318 : VariableMode mode;
319 : InitializationFlag flag;
320 : MaybeAssignedFlag maybe_assigned_flag;
321 : int slot_index = ScopeInfo::ContextSlotIndex(scope_info, name, &mode,
322 4938177 : &flag, &maybe_assigned_flag);
323 : DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
324 4938177 : if (slot_index >= 0) {
325 : if (FLAG_trace_contexts) {
326 : PrintF("=> found local in context slot %d (mode = %d)\n",
327 : slot_index, mode);
328 : }
329 1863773 : *index = slot_index;
330 1863773 : *variable_mode = mode;
331 1863773 : *init_flag = flag;
332 1863773 : *attributes = GetAttributesForMode(mode);
333 1863773 : return context;
334 : }
335 :
336 : // Check the slot corresponding to the intermediate context holding
337 : // only the function name variable. It's conceptually (and spec-wise)
338 : // in an outer scope of the function's declaration scope.
339 5571015 : if (follow_context_chain && (flags & STOP_AT_DECLARATION_SCOPE) == 0 &&
340 2496611 : context->IsFunctionContext()) {
341 2373117 : int function_index = scope_info->FunctionContextSlotIndex(*name);
342 2373117 : if (function_index >= 0) {
343 : if (FLAG_trace_contexts) {
344 : PrintF("=> found intermediate function in context slot %d\n",
345 : function_index);
346 : }
347 176 : *index = function_index;
348 176 : *attributes = READ_ONLY;
349 176 : *init_flag = kCreatedInitialized;
350 176 : *variable_mode = CONST;
351 255 : if (is_sloppy_function_name != nullptr &&
352 79 : is_sloppy(scope_info->language_mode())) {
353 79 : *is_sloppy_function_name = true;
354 : }
355 176 : return context;
356 : }
357 : }
358 :
359 : // Lookup variable in module imports and exports.
360 3074228 : if (context->IsModuleContext()) {
361 : VariableMode mode;
362 : InitializationFlag flag;
363 : MaybeAssignedFlag maybe_assigned_flag;
364 : int cell_index =
365 265 : scope_info->ModuleIndex(name, &mode, &flag, &maybe_assigned_flag);
366 265 : if (cell_index != 0) {
367 : if (FLAG_trace_contexts) {
368 : PrintF("=> found in module imports or exports\n");
369 : }
370 230 : *index = cell_index;
371 230 : *variable_mode = mode;
372 230 : *init_flag = flag;
373 230 : *attributes = ModuleDescriptor::GetCellIndexKind(cell_index) ==
374 : ModuleDescriptor::kExport
375 190 : ? GetAttributesForMode(mode)
376 460 : : READ_ONLY;
377 460 : return handle(context->module(), isolate);
378 : }
379 : }
380 3289656 : } else if (context->IsCatchContext()) {
381 : // Catch contexts have the variable name in the extension slot.
382 260881 : if (String::Equals(name, handle(context->catch_name()))) {
383 : if (FLAG_trace_contexts) {
384 : PrintF("=> found in catch context\n");
385 : }
386 58 : *index = Context::THROWN_OBJECT_INDEX;
387 58 : *attributes = NONE;
388 58 : *init_flag = kCreatedInitialized;
389 58 : *variable_mode = VAR;
390 58 : return context;
391 : }
392 3028775 : } else if (context->IsDebugEvaluateContext()) {
393 : // Check materialized locals.
394 : Object* ext = context->get(EXTENSION_INDEX);
395 10099 : if (ext->IsContextExtension()) {
396 : Object* obj = ContextExtension::cast(ext)->extension();
397 10099 : if (obj->IsJSReceiver()) {
398 : Handle<JSReceiver> extension(JSReceiver::cast(obj));
399 9587 : LookupIterator it(extension, name, extension);
400 9587 : Maybe<bool> found = JSReceiver::HasProperty(&it);
401 9587 : if (found.FromMaybe(false)) {
402 5928 : *attributes = NONE;
403 5928 : return extension;
404 : }
405 : }
406 : }
407 : // Check the original context, but do not follow its context chain.
408 : Object* obj = context->get(WRAPPED_CONTEXT_INDEX);
409 4171 : if (obj->IsContext()) {
410 : Handle<Object> result =
411 : Context::cast(obj)->Lookup(name, DONT_FOLLOW_CHAINS, index,
412 901 : attributes, init_flag, variable_mode);
413 901 : if (!result.is_null()) return result;
414 : }
415 : // Check whitelist. Names that do not pass whitelist shall only resolve
416 : // to with, script or native contexts up the context chain.
417 : obj = context->get(WHITE_LIST_INDEX);
418 3514 : if (obj->IsStringSet()) {
419 3228 : failed_whitelist = failed_whitelist || !StringSet::cast(obj)->Has(name);
420 : }
421 : }
422 :
423 : // 3. Prepare to continue with the previous (next outermost) context.
424 18067749 : if (context->IsNativeContext() ||
425 5642925 : ((flags & STOP_AT_DECLARATION_SCOPE) != 0 &&
426 289198 : context->is_declaration_context())) {
427 : follow_context_chain = false;
428 : } else {
429 5065161 : do {
430 : context = Handle<Context>(context->previous(), isolate);
431 : // If we come across a whitelist context, and the name is not
432 : // whitelisted, then only consider with, script, module or native
433 : // contexts.
434 5817 : } while (failed_whitelist && !context->IsScriptContext() &&
435 5068399 : !context->IsNativeContext() && !context->IsWithContext() &&
436 230 : !context->IsModuleContext());
437 : }
438 : } while (follow_context_chain);
439 :
440 : if (FLAG_trace_contexts) {
441 : PrintF("=> no property/slot found\n");
442 : }
443 : return Handle<Object>::null();
444 : }
445 :
446 :
447 439768 : void Context::AddOptimizedCode(Code* code) {
448 : DCHECK(IsNativeContext());
449 : DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
450 : DCHECK(code->next_code_link()->IsUndefined(GetIsolate()));
451 439768 : code->set_next_code_link(get(OPTIMIZED_CODE_LIST));
452 439768 : set(OPTIMIZED_CODE_LIST, code, UPDATE_WEAK_WRITE_BARRIER);
453 439768 : }
454 :
455 :
456 249776 : void Context::SetOptimizedCodeListHead(Object* head) {
457 : DCHECK(IsNativeContext());
458 249776 : set(OPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
459 249776 : }
460 :
461 :
462 108902 : Object* Context::OptimizedCodeListHead() {
463 : DCHECK(IsNativeContext());
464 108902 : return get(OPTIMIZED_CODE_LIST);
465 : }
466 :
467 :
468 253425 : void Context::SetDeoptimizedCodeListHead(Object* head) {
469 : DCHECK(IsNativeContext());
470 253425 : set(DEOPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
471 253425 : }
472 :
473 :
474 400350 : Object* Context::DeoptimizedCodeListHead() {
475 : DCHECK(IsNativeContext());
476 400350 : return get(DEOPTIMIZED_CODE_LIST);
477 : }
478 :
479 :
480 41 : Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() {
481 : Isolate* isolate = GetIsolate();
482 : Handle<Object> result(error_message_for_code_gen_from_strings(), isolate);
483 41 : if (!result->IsUndefined(isolate)) return result;
484 : return isolate->factory()->NewStringFromStaticChars(
485 36 : "Code generation from strings disallowed for this context");
486 : }
487 :
488 :
489 : #define COMPARE_NAME(index, type, name) \
490 : if (string->IsOneByteEqualTo(STATIC_CHAR_VECTOR(#name))) return index;
491 :
492 732 : int Context::ImportedFieldIndexForName(Handle<String> string) {
493 8479 : NATIVE_CONTEXT_IMPORTED_FIELDS(COMPARE_NAME)
494 122 : return kNotFound;
495 : }
496 :
497 :
498 122 : int Context::IntrinsicIndexForName(Handle<String> string) {
499 3111 : NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(COMPARE_NAME);
500 0 : return kNotFound;
501 : }
502 :
503 : #undef COMPARE_NAME
504 :
505 : #define COMPARE_NAME(index, type, name) \
506 : if (strncmp(string, #name, length) == 0) return index;
507 :
508 140746 : int Context::IntrinsicIndexForName(const unsigned char* unsigned_string,
509 : int length) {
510 : const char* string = reinterpret_cast<const char*>(unsigned_string);
511 140746 : NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(COMPARE_NAME);
512 50 : return kNotFound;
513 : }
514 :
515 : #undef COMPARE_NAME
516 :
517 : #ifdef DEBUG
518 :
519 : bool Context::IsBootstrappingOrNativeContext(Isolate* isolate, Object* object) {
520 : // During bootstrapping we allow all objects to pass as global
521 : // objects. This is necessary to fix circular dependencies.
522 : return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
523 : isolate->bootstrapper()->IsActive() || object->IsNativeContext();
524 : }
525 :
526 :
527 : bool Context::IsBootstrappingOrValidParentContext(
528 : Object* object, Context* child) {
529 : // During bootstrapping we allow all objects to pass as
530 : // contexts. This is necessary to fix circular dependencies.
531 : if (child->GetIsolate()->bootstrapper()->IsActive()) return true;
532 : if (!object->IsContext()) return false;
533 : Context* context = Context::cast(object);
534 : return context->IsNativeContext() || context->IsScriptContext() ||
535 : context->IsModuleContext() || !child->IsModuleContext();
536 : }
537 :
538 : #endif
539 :
540 82760 : void Context::ResetErrorsThrown() {
541 : DCHECK(IsNativeContext());
542 : set_errors_thrown(Smi::FromInt(0));
543 82760 : }
544 :
545 956423 : void Context::IncrementErrorsThrown() {
546 : DCHECK(IsNativeContext());
547 :
548 : int previous_value = errors_thrown()->value();
549 956423 : set_errors_thrown(Smi::FromInt(previous_value + 1));
550 956423 : }
551 :
552 :
553 110 : int Context::GetErrorsThrown() { return errors_thrown()->value(); }
554 :
555 : } // namespace internal
556 : } // namespace v8
|