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#include "src/objects/module-inl.h"
12
13namespace v8 {
14namespace internal {
15
16
17Handle<ScriptContextTable> ScriptContextTable::Extend(
18 Handle<ScriptContextTable> table, Handle<Context> script_context) {
19 Handle<ScriptContextTable> result;
20 int used = table->used();
21 int length = table->length();
22 CHECK(used >= 0 && length > 0 && used < length);
23 if (used + kFirstContextSlotIndex == length) {
24 CHECK(length < Smi::kMaxValue / 2);
25 Isolate* isolate = script_context->GetIsolate();
26 Handle<FixedArray> copy =
27 isolate->factory()->CopyFixedArrayAndGrow(table, length);
28 copy->set_map(ReadOnlyRoots(isolate).script_context_table_map());
29 result = Handle<ScriptContextTable>::cast(copy);
30 } else {
31 result = table;
32 }
33 result->set_used(used + 1);
34
35 DCHECK(script_context->IsScriptContext());
36 result->set(used + kFirstContextSlotIndex, *script_context);
37 return result;
38}
39
40bool ScriptContextTable::Lookup(Isolate* isolate, ScriptContextTable table,
41 String name, LookupResult* result) {
42 DisallowHeapAllocation no_gc;
43 for (int i = 0; i < table->used(); i++) {
44 Context context = table->get_context(i);
45 DCHECK(context->IsScriptContext());
46 int slot_index = ScopeInfo::ContextSlotIndex(
47 context->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
59bool Context::is_declaration_context() {
60 if (IsFunctionContext() || IsNativeContext() || IsScriptContext() ||
61 IsModuleContext()) {
62 return true;
63 }
64 if (IsEvalContext()) {
65 return scope_info()->language_mode() == LanguageMode::kStrict;
66 }
67 if (!IsBlockContext()) return false;
68 return scope_info()->is_declaration_scope();
69}
70
71Context Context::declaration_context() {
72 Context current = *this;
73 while (!current->is_declaration_context()) {
74 current = current->previous();
75 }
76 return current;
77}
78
79Context Context::closure_context() {
80 Context current = *this;
81 while (!current->IsFunctionContext() && !current->IsScriptContext() &&
82 !current->IsModuleContext() && !current->IsNativeContext() &&
83 !current->IsEvalContext()) {
84 current = current->previous();
85 }
86 return current;
87}
88
89JSObject Context::extension_object() {
90 DCHECK(IsNativeContext() || IsFunctionContext() || IsBlockContext() ||
91 IsEvalContext() || IsCatchContext());
92 HeapObject object = extension();
93 if (object->IsTheHole()) return JSObject();
94 DCHECK(object->IsJSContextExtensionObject() ||
95 (IsNativeContext() && object->IsJSGlobalObject()));
96 return JSObject::cast(object);
97}
98
99JSReceiver Context::extension_receiver() {
100 DCHECK(IsNativeContext() || IsWithContext() || IsEvalContext() ||
101 IsFunctionContext() || IsBlockContext());
102 return IsWithContext() ? JSReceiver::cast(extension()) : extension_object();
103}
104
105ScopeInfo Context::scope_info() {
106 return ScopeInfo::cast(get(SCOPE_INFO_INDEX));
107}
108
109Module Context::module() {
110 Context current = *this;
111 while (!current->IsModuleContext()) {
112 current = current->previous();
113 }
114 return Module::cast(current->extension());
115}
116
117JSGlobalObject Context::global_object() {
118 return JSGlobalObject::cast(native_context()->extension());
119}
120
121Context Context::script_context() {
122 Context current = *this;
123 while (!current->IsScriptContext()) {
124 current = current->previous();
125 }
126 return current;
127}
128
129JSGlobalProxy Context::global_proxy() {
130 return native_context()->global_proxy_object();
131}
132
133void Context::set_global_proxy(JSGlobalProxy object) {
134 native_context()->set_global_proxy_object(object);
135}
136
137/**
138 * Lookups a property in an object environment, taking the unscopables into
139 * account. This is used For HasBinding spec algorithms for ObjectEnvironment.
140 */
141static Maybe<bool> UnscopableLookup(LookupIterator* it) {
142 Isolate* isolate = it->isolate();
143
144 Maybe<bool> found = JSReceiver::HasProperty(it);
145 if (found.IsNothing() || !found.FromJust()) return found;
146
147 Handle<Object> unscopables;
148 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
149 isolate, unscopables,
150 JSReceiver::GetProperty(isolate,
151 Handle<JSReceiver>::cast(it->GetReceiver()),
152 isolate->factory()->unscopables_symbol()),
153 Nothing<bool>());
154 if (!unscopables->IsJSReceiver()) return Just(true);
155 Handle<Object> blacklist;
156 ASSIGN_RETURN_ON_EXCEPTION_VALUE(
157 isolate, blacklist,
158 JSReceiver::GetProperty(isolate, Handle<JSReceiver>::cast(unscopables),
159 it->name()),
160 Nothing<bool>());
161 return Just(!blacklist->BooleanValue(isolate));
162}
163
164static PropertyAttributes GetAttributesForMode(VariableMode mode) {
165 DCHECK(IsDeclaredVariableMode(mode));
166 return mode == VariableMode::kConst ? READ_ONLY : NONE;
167}
168
169// static
170Handle<Object> Context::Lookup(Handle<Context> context, Handle<String> name,
171 ContextLookupFlags flags, int* index,
172 PropertyAttributes* attributes,
173 InitializationFlag* init_flag,
174 VariableMode* variable_mode,
175 bool* is_sloppy_function_name) {
176 Isolate* isolate = context->GetIsolate();
177
178 bool follow_context_chain = (flags & FOLLOW_CONTEXT_CHAIN) != 0;
179 bool failed_whitelist = false;
180 *index = kNotFound;
181 *attributes = ABSENT;
182 *init_flag = kCreatedInitialized;
183 *variable_mode = VariableMode::kVar;
184 if (is_sloppy_function_name != nullptr) {
185 *is_sloppy_function_name = false;
186 }
187
188 if (FLAG_trace_contexts) {
189 PrintF("Context::Lookup(");
190 name->ShortPrint();
191 PrintF(")\n");
192 }
193
194 do {
195 if (FLAG_trace_contexts) {
196 PrintF(" - looking in context %p",
197 reinterpret_cast<void*>(context->ptr()));
198 if (context->IsScriptContext()) PrintF(" (script context)");
199 if (context->IsNativeContext()) PrintF(" (native context)");
200 PrintF("\n");
201 }
202
203 // 1. Check global objects, subjects of with, and extension objects.
204 DCHECK_IMPLIES(context->IsEvalContext(),
205 context->extension()->IsTheHole(isolate));
206 if ((context->IsNativeContext() || context->IsWithContext() ||
207 context->IsFunctionContext() || context->IsBlockContext()) &&
208 !context->extension_receiver().is_null()) {
209 Handle<JSReceiver> object(context->extension_receiver(), isolate);
210
211 if (context->IsNativeContext()) {
212 DisallowHeapAllocation no_gc;
213 if (FLAG_trace_contexts) {
214 PrintF(" - trying other script contexts\n");
215 }
216 // Try other script contexts.
217 ScriptContextTable script_contexts =
218 context->global_object()->native_context()->script_context_table();
219 ScriptContextTable::LookupResult r;
220 if (ScriptContextTable::Lookup(isolate, script_contexts, *name, &r)) {
221 Context context = script_contexts->get_context(r.context_index);
222 if (FLAG_trace_contexts) {
223 PrintF("=> found property in script context %d: %p\n",
224 r.context_index, reinterpret_cast<void*>(context->ptr()));
225 }
226 *index = r.slot_index;
227 *variable_mode = r.mode;
228 *init_flag = r.init_flag;
229 *attributes = GetAttributesForMode(r.mode);
230 return handle(context, isolate);
231 }
232 }
233
234 // Context extension objects needs to behave as if they have no
235 // prototype. So even if we want to follow prototype chains, we need
236 // to only do a local lookup for context extension objects.
237 Maybe<PropertyAttributes> maybe = Nothing<PropertyAttributes>();
238 if ((flags & FOLLOW_PROTOTYPE_CHAIN) == 0 ||
239 object->IsJSContextExtensionObject()) {
240 maybe = JSReceiver::GetOwnPropertyAttributes(object, name);
241 } else if (context->IsWithContext()) {
242 // A with context will never bind "this", but debug-eval may look into
243 // a with context when resolving "this". Other synthetic variables such
244 // as new.target may be resolved as VariableMode::kDynamicLocal due to
245 // bug v8:5405 , skipping them here serves as a workaround until a more
246 // thorough fix can be applied.
247 // TODO(v8:5405): Replace this check with a DCHECK when resolution of
248 // of synthetic variables does not go through this code path.
249 if (ScopeInfo::VariableIsSynthetic(*name)) {
250 maybe = Just(ABSENT);
251 } else {
252 LookupIterator it(object, name, object);
253 Maybe<bool> found = UnscopableLookup(&it);
254 if (found.IsNothing()) {
255 maybe = Nothing<PropertyAttributes>();
256 } else {
257 // Luckily, consumers of |maybe| only care whether the property
258 // was absent or not, so we can return a dummy |NONE| value
259 // for its attributes when it was present.
260 maybe = Just(found.FromJust() ? NONE : ABSENT);
261 }
262 }
263 } else {
264 maybe = JSReceiver::GetPropertyAttributes(object, name);
265 }
266
267 if (maybe.IsNothing()) return Handle<Object>();
268 DCHECK(!isolate->has_pending_exception());
269 *attributes = maybe.FromJust();
270
271 if (maybe.FromJust() != ABSENT) {
272 if (FLAG_trace_contexts) {
273 PrintF("=> found property in context object %p\n",
274 reinterpret_cast<void*>(object->ptr()));
275 }
276 return object;
277 }
278 }
279
280 // 2. Check the context proper if it has slots.
281 if (context->IsFunctionContext() || context->IsBlockContext() ||
282 context->IsScriptContext() || context->IsEvalContext() ||
283 context->IsModuleContext() || context->IsCatchContext()) {
284 DisallowHeapAllocation no_gc;
285 // Use serialized scope information of functions and blocks to search
286 // for the context index.
287 ScopeInfo scope_info = context->scope_info();
288 VariableMode mode;
289 InitializationFlag flag;
290 MaybeAssignedFlag maybe_assigned_flag;
291 int slot_index = ScopeInfo::ContextSlotIndex(scope_info, *name, &mode,
292 &flag, &maybe_assigned_flag);
293 DCHECK(slot_index < 0 || slot_index >= MIN_CONTEXT_SLOTS);
294 if (slot_index >= 0) {
295 if (FLAG_trace_contexts) {
296 PrintF("=> found local in context slot %d (mode = %hhu)\n",
297 slot_index, static_cast<uint8_t>(mode));
298 }
299 *index = slot_index;
300 *variable_mode = mode;
301 *init_flag = flag;
302 *attributes = GetAttributesForMode(mode);
303 return context;
304 }
305
306 // Check the slot corresponding to the intermediate context holding
307 // only the function name variable. It's conceptually (and spec-wise)
308 // in an outer scope of the function's declaration scope.
309 if (follow_context_chain && context->IsFunctionContext()) {
310 int function_index = scope_info->FunctionContextSlotIndex(*name);
311 if (function_index >= 0) {
312 if (FLAG_trace_contexts) {
313 PrintF("=> found intermediate function in context slot %d\n",
314 function_index);
315 }
316 *index = function_index;
317 *attributes = READ_ONLY;
318 *init_flag = kCreatedInitialized;
319 *variable_mode = VariableMode::kConst;
320 if (is_sloppy_function_name != nullptr &&
321 is_sloppy(scope_info->language_mode())) {
322 *is_sloppy_function_name = true;
323 }
324 return context;
325 }
326 }
327
328 // Lookup variable in module imports and exports.
329 if (context->IsModuleContext()) {
330 VariableMode mode;
331 InitializationFlag flag;
332 MaybeAssignedFlag maybe_assigned_flag;
333 int cell_index =
334 scope_info->ModuleIndex(*name, &mode, &flag, &maybe_assigned_flag);
335 if (cell_index != 0) {
336 if (FLAG_trace_contexts) {
337 PrintF("=> found in module imports or exports\n");
338 }
339 *index = cell_index;
340 *variable_mode = mode;
341 *init_flag = flag;
342 *attributes = ModuleDescriptor::GetCellIndexKind(cell_index) ==
343 ModuleDescriptor::kExport
344 ? GetAttributesForMode(mode)
345 : READ_ONLY;
346 return handle(context->module(), isolate);
347 }
348 }
349 } else if (context->IsDebugEvaluateContext()) {
350 // Check materialized locals.
351 Object ext = context->get(EXTENSION_INDEX);
352 if (ext->IsJSReceiver()) {
353 Handle<JSReceiver> extension(JSReceiver::cast(ext), isolate);
354 LookupIterator it(extension, name, extension);
355 Maybe<bool> found = JSReceiver::HasProperty(&it);
356 if (found.FromMaybe(false)) {
357 *attributes = NONE;
358 return extension;
359 }
360 }
361 // Check the original context, but do not follow its context chain.
362 Object obj = context->get(WRAPPED_CONTEXT_INDEX);
363 if (obj->IsContext()) {
364 Handle<Context> context(Context::cast(obj), isolate);
365 Handle<Object> result =
366 Context::Lookup(context, name, DONT_FOLLOW_CHAINS, index,
367 attributes, init_flag, variable_mode);
368 if (!result.is_null()) return result;
369 }
370 // Check whitelist. Names that do not pass whitelist shall only resolve
371 // to with, script or native contexts up the context chain.
372 obj = context->get(WHITE_LIST_INDEX);
373 if (obj->IsStringSet()) {
374 failed_whitelist =
375 failed_whitelist || !StringSet::cast(obj)->Has(isolate, name);
376 }
377 }
378
379 // 3. Prepare to continue with the previous (next outermost) context.
380 if (context->IsNativeContext()) break;
381
382 do {
383 context = Handle<Context>(context->previous(), isolate);
384 // If we come across a whitelist context, and the name is not
385 // whitelisted, then only consider with, script, module or native
386 // contexts.
387 } while (failed_whitelist && !context->IsScriptContext() &&
388 !context->IsNativeContext() && !context->IsWithContext() &&
389 !context->IsModuleContext());
390 } while (follow_context_chain);
391
392 if (FLAG_trace_contexts) {
393 PrintF("=> no property/slot found\n");
394 }
395 return Handle<Object>::null();
396}
397
398void Context::AddOptimizedCode(Code code) {
399 DCHECK(IsNativeContext());
400 DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
401 DCHECK(code->next_code_link()->IsUndefined());
402 code->set_next_code_link(get(OPTIMIZED_CODE_LIST));
403 set(OPTIMIZED_CODE_LIST, code, UPDATE_WEAK_WRITE_BARRIER);
404}
405
406void Context::SetOptimizedCodeListHead(Object head) {
407 DCHECK(IsNativeContext());
408 set(OPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
409}
410
411Object Context::OptimizedCodeListHead() {
412 DCHECK(IsNativeContext());
413 return get(OPTIMIZED_CODE_LIST);
414}
415
416void Context::SetDeoptimizedCodeListHead(Object head) {
417 DCHECK(IsNativeContext());
418 set(DEOPTIMIZED_CODE_LIST, head, UPDATE_WEAK_WRITE_BARRIER);
419}
420
421Object Context::DeoptimizedCodeListHead() {
422 DCHECK(IsNativeContext());
423 return get(DEOPTIMIZED_CODE_LIST);
424}
425
426Handle<Object> Context::ErrorMessageForCodeGenerationFromStrings() {
427 Isolate* isolate = GetIsolate();
428 Handle<Object> result(error_message_for_code_gen_from_strings(), isolate);
429 if (!result->IsUndefined(isolate)) return result;
430 return isolate->factory()->NewStringFromStaticChars(
431 "Code generation from strings disallowed for this context");
432}
433
434#define COMPARE_NAME(index, type, name) \
435 if (string->IsOneByteEqualTo(StaticCharVector(#name))) return index;
436
437int Context::IntrinsicIndexForName(Handle<String> string) {
438 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(COMPARE_NAME);
439 return kNotFound;
440}
441
442#undef COMPARE_NAME
443
444#define COMPARE_NAME(index, type, name) \
445 if (strncmp(string, #name, length) == 0) return index;
446
447int Context::IntrinsicIndexForName(const unsigned char* unsigned_string,
448 int length) {
449 const char* string = reinterpret_cast<const char*>(unsigned_string);
450 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(COMPARE_NAME);
451 return kNotFound;
452}
453
454#undef COMPARE_NAME
455
456#ifdef DEBUG
457
458bool Context::IsBootstrappingOrNativeContext(Isolate* isolate, Object object) {
459 // During bootstrapping we allow all objects to pass as global
460 // objects. This is necessary to fix circular dependencies.
461 return isolate->heap()->gc_state() != Heap::NOT_IN_GC ||
462 isolate->bootstrapper()->IsActive() || object->IsNativeContext();
463}
464
465bool Context::IsBootstrappingOrValidParentContext(Object object,
466 Context child) {
467 // During bootstrapping we allow all objects to pass as
468 // contexts. This is necessary to fix circular dependencies.
469 if (child->GetIsolate()->bootstrapper()->IsActive()) return true;
470 if (!object->IsContext()) return false;
471 Context context = Context::cast(object);
472 return context->IsNativeContext() || context->IsScriptContext() ||
473 context->IsModuleContext() || !child->IsModuleContext();
474}
475
476#endif
477
478void Context::ResetErrorsThrown() {
479 DCHECK(IsNativeContext());
480 set_errors_thrown(Smi::FromInt(0));
481}
482
483void Context::IncrementErrorsThrown() {
484 DCHECK(IsNativeContext());
485
486 int previous_value = errors_thrown()->value();
487 set_errors_thrown(Smi::FromInt(previous_value + 1));
488}
489
490int Context::GetErrorsThrown() { return errors_thrown()->value(); }
491
492STATIC_ASSERT(Context::MIN_CONTEXT_SLOTS == 4);
493STATIC_ASSERT(NativeContext::kScopeInfoOffset ==
494 Context::OffsetOfElementAt(NativeContext::SCOPE_INFO_INDEX));
495STATIC_ASSERT(NativeContext::kPreviousOffset ==
496 Context::OffsetOfElementAt(NativeContext::PREVIOUS_INDEX));
497STATIC_ASSERT(NativeContext::kExtensionOffset ==
498 Context::OffsetOfElementAt(NativeContext::EXTENSION_INDEX));
499STATIC_ASSERT(NativeContext::kNativeContextOffset ==
500 Context::OffsetOfElementAt(NativeContext::NATIVE_CONTEXT_INDEX));
501
502STATIC_ASSERT(NativeContext::kStartOfStrongFieldsOffset ==
503 Context::OffsetOfElementAt(-1));
504STATIC_ASSERT(NativeContext::kStartOfWeakFieldsOffset ==
505 Context::OffsetOfElementAt(NativeContext::FIRST_WEAK_SLOT));
506STATIC_ASSERT(NativeContext::kMicrotaskQueueOffset ==
507 Context::SizeFor(NativeContext::NATIVE_CONTEXT_SLOTS));
508STATIC_ASSERT(NativeContext::kSize ==
509 (Context::SizeFor(NativeContext::NATIVE_CONTEXT_SLOTS) +
510 kSystemPointerSize));
511
512} // namespace internal
513} // namespace v8
514