1 // Copyright 2012 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 #ifndef V8_CONTEXTS_H_ 6 #define V8_CONTEXTS_H_ 7 8 #include "src/heap/heap.h" 9 #include "src/objects.h" 10 11 namespace v8 { 12 namespace internal { 13 14 15 enum ContextLookupFlags { 16 FOLLOW_CONTEXT_CHAIN = 1 << 0, 17 FOLLOW_PROTOTYPE_CHAIN = 1 << 1, 18 STOP_AT_DECLARATION_SCOPE = 1 << 2, 19 SKIP_WITH_CONTEXT = 1 << 3, 20 21 DONT_FOLLOW_CHAINS = 0, 22 FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN, 23 LEXICAL_TEST = 24 FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT, 25 }; 26 27 28 // ES5 10.2 defines lexical environments with mutable and immutable bindings. 29 // Immutable bindings have two states, initialized and uninitialized, and 30 // their state is changed by the InitializeImmutableBinding method. The 31 // BindingFlags enum represents information if a binding has definitely been 32 // initialized. A mutable binding does not need to be checked and thus has 33 // the BindingFlag BINDING_IS_INITIALIZED. 34 // 35 // There is one possibility for legacy immutable bindings: 36 // * The function name of a named function literal. The binding is immediately 37 // initialized when entering the function and thus does not need to be 38 // checked. it gets the BindingFlag BINDING_IS_INITIALIZED. 39 // 40 // The harmony proposal for block scoped bindings also introduces the 41 // uninitialized state for mutable bindings. 42 // * A 'let' declared variable. They are initialized when evaluating the 43 // corresponding declaration statement. They need to be checked for being 44 // initialized and thus get the flag BINDING_CHECK_INITIALIZED. 45 // * A 'var' declared variable. It is initialized immediately upon creation 46 // and thus doesn't need to be checked. It gets the flag 47 // BINDING_IS_INITIALIZED. 48 // * Catch bound variables, function parameters and variables introduced by 49 // function declarations are initialized immediately and do not need to be 50 // checked. Thus they get the flag BINDING_IS_INITIALIZED. 51 // Accessing an uninitialized binding produces a reference error. 52 // 53 // In V8 uninitialized bindings are set to the hole value upon creation and set 54 // to a different value upon initialization. 55 enum BindingFlags { 56 BINDING_IS_INITIALIZED, 57 BINDING_CHECK_INITIALIZED, 58 MISSING_BINDING 59 }; 60 61 // Heap-allocated activation contexts. 62 // 63 // Contexts are implemented as FixedArray objects; the Context 64 // class is a convenience interface casted on a FixedArray object. 65 // 66 // Note: Context must have no virtual functions and Context objects 67 // must always be allocated via Heap::AllocateContext() or 68 // Factory::NewContext. 69 70 #define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \ 71 V(IS_ARRAYLIKE, JSFunction, is_arraylike) \ 72 V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site) \ 73 V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error) \ 74 V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error) \ 75 V(OBJECT_DEFINE_PROPERTIES, JSFunction, object_define_properties) \ 76 V(OBJECT_DEFINE_PROPERTY, JSFunction, object_define_property) \ 77 V(OBJECT_FREEZE, JSFunction, object_freeze) \ 78 V(OBJECT_GET_PROTOTYPE_OF, JSFunction, object_get_prototype_of) \ 79 V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible) \ 80 V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen) \ 81 V(OBJECT_IS_SEALED, JSFunction, object_is_sealed) \ 82 V(OBJECT_KEYS, JSFunction, object_keys) \ 83 V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply) \ 84 V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct) \ 85 V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \ 86 V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \ 87 V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments) \ 88 V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable) \ 89 V(MATH_EXP_INDEX, JSFunction, math_exp) \ 90 V(MATH_FLOOR_INDEX, JSFunction, math_floor) \ 91 V(MATH_LOG_INDEX, JSFunction, math_log) \ 92 V(MATH_SQRT_INDEX, JSFunction, math_sqrt) 93 94 #define NATIVE_CONTEXT_IMPORTED_FIELDS(V) \ 95 V(ARRAY_CONCAT_INDEX, JSFunction, array_concat) \ 96 V(ARRAY_POP_INDEX, JSFunction, array_pop) \ 97 V(ARRAY_PUSH_INDEX, JSFunction, array_push) \ 98 V(ARRAY_SHIFT_INDEX, JSFunction, array_shift) \ 99 V(ARRAY_SPLICE_INDEX, JSFunction, array_splice) \ 100 V(ARRAY_SLICE_INDEX, JSFunction, array_slice) \ 101 V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift) \ 102 V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator) \ 103 V(ASYNC_FUNCTION_AWAIT_INDEX, JSFunction, async_function_await) \ 104 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \ 105 V(ERROR_FUNCTION_INDEX, JSFunction, error_function) \ 106 V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function) \ 107 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \ 108 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \ 109 V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function) \ 110 V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete) \ 111 V(MAP_GET_METHOD_INDEX, JSFunction, map_get) \ 112 V(MAP_HAS_METHOD_INDEX, JSFunction, map_has) \ 113 V(MAP_SET_METHOD_INDEX, JSFunction, map_set) \ 114 V(MATH_POW_METHOD_INDEX, JSFunction, math_pow) \ 115 V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number) \ 116 V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number) \ 117 V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line) \ 118 V(NO_SIDE_EFFECTS_TO_STRING_FUN_INDEX, JSFunction, \ 119 no_side_effects_to_string_fun) \ 120 V(OBJECT_VALUE_OF, JSFunction, object_value_of) \ 121 V(OBJECT_TO_STRING, JSFunction, object_to_string) \ 122 V(PROMISE_CATCH_INDEX, JSFunction, promise_catch) \ 123 V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain) \ 124 V(PROMISE_CREATE_INDEX, JSFunction, promise_create) \ 125 V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function) \ 126 V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction, \ 127 promise_has_user_defined_reject_handler) \ 128 V(PROMISE_REJECT_INDEX, JSFunction, promise_reject) \ 129 V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve) \ 130 V(PROMISE_CREATE_RESOLVED_INDEX, JSFunction, promise_create_resolved) \ 131 V(PROMISE_CREATE_REJECTED_INDEX, JSFunction, promise_create_rejected) \ 132 V(PROMISE_THEN_INDEX, JSFunction, promise_then) \ 133 V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function) \ 134 V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function) \ 135 V(SET_ADD_METHOD_INDEX, JSFunction, set_add) \ 136 V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete) \ 137 V(SET_HAS_METHOD_INDEX, JSFunction, set_has) \ 138 V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate) \ 139 V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function) \ 140 V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function) \ 141 V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function) 142 143 #define NATIVE_CONTEXT_FIELDS(V) \ 144 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \ 145 V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \ 146 /* Below is alpha-sorted */ \ 147 V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, \ 148 accessor_property_descriptor_map) \ 149 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \ 150 V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \ 151 V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map) \ 152 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \ 153 V(ASYNC_FUNCTION_FUNCTION_INDEX, JSFunction, async_function_constructor) \ 154 V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function) \ 155 V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function) \ 156 V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function) \ 157 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \ 158 V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map, \ 159 bound_function_with_constructor_map) \ 160 V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map, \ 161 bound_function_without_constructor_map) \ 162 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \ 163 call_as_constructor_delegate) \ 164 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \ 165 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \ 166 V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map) \ 167 V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \ 168 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \ 169 V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \ 170 error_message_for_code_gen_from_strings) \ 171 V(ERRORS_THROWN_INDEX, Smi, errors_thrown) \ 172 V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object) \ 173 V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object) \ 174 V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map) \ 175 V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun) \ 176 V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function) \ 177 V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun) \ 178 V(TEMPLATE_INSTANTIATIONS_CACHE_INDEX, UnseededNumberDictionary, \ 179 template_instantiations_cache) \ 180 V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function) \ 181 V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction, \ 182 generator_function_function) \ 183 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \ 184 V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \ 185 V(INITIAL_GENERATOR_PROTOTYPE_INDEX, JSObject, initial_generator_prototype) \ 186 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \ 187 V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \ 188 V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function) \ 189 V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \ 190 V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function) \ 191 V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \ 192 V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function) \ 193 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \ 194 V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map) \ 195 V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map, \ 196 js_array_fast_smi_elements_map_index) \ 197 V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map, \ 198 js_array_fast_holey_smi_elements_map_index) \ 199 V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index) \ 200 V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map, \ 201 js_array_fast_holey_elements_map_index) \ 202 V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map, \ 203 js_array_fast_double_elements_map_index) \ 204 V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map, \ 205 js_array_fast_holey_double_elements_map_index) \ 206 V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun) \ 207 V(JS_MAP_MAP_INDEX, Map, js_map_map) \ 208 V(JS_SET_FUN_INDEX, JSFunction, js_set_fun) \ 209 V(JS_SET_MAP_INDEX, Map, js_set_map) \ 210 V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun) \ 211 V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun) \ 212 V(MAP_CACHE_INDEX, Object, map_cache) \ 213 V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map) \ 214 V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map) \ 215 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \ 216 V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object) \ 217 V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache) \ 218 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \ 219 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \ 220 V(OBJECT_WITH_NULL_PROTOTYPE_MAP, Map, object_with_null_prototype_map) \ 221 V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map) \ 222 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \ 223 V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map) \ 224 V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map) \ 225 V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function) \ 226 V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map) \ 227 V(PROXY_MAP_INDEX, Map, proxy_map) \ 228 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \ 229 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map) \ 230 V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table) \ 231 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \ 232 V(SECURITY_TOKEN_INDEX, Object, security_token) \ 233 V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell) \ 234 V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map) \ 235 V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun) \ 236 V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map) \ 237 V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map) \ 238 V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \ 239 sloppy_function_without_prototype_map) \ 240 V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map, \ 241 sloppy_function_with_readonly_prototype_map) \ 242 V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map) \ 243 V(WASM_MODULE_CONSTRUCTOR_INDEX, JSFunction, wasm_module_constructor) \ 244 V(WASM_INSTANCE_CONSTRUCTOR_INDEX, JSFunction, wasm_instance_constructor) \ 245 V(WASM_MODULE_SYM_INDEX, Symbol, wasm_module_sym) \ 246 V(WASM_INSTANCE_SYM_INDEX, Symbol, wasm_instance_sym) \ 247 V(SLOPPY_ASYNC_FUNCTION_MAP_INDEX, Map, sloppy_async_function_map) \ 248 V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map) \ 249 V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map) \ 250 V(STRICT_ASYNC_FUNCTION_MAP_INDEX, Map, strict_async_function_map) \ 251 V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map) \ 252 V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map) \ 253 V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \ 254 strict_function_without_prototype_map) \ 255 V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map) \ 256 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \ 257 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \ 258 V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \ 259 V(TYPED_ARRAY_FUN_INDEX, JSFunction, typed_array_function) \ 260 V(TYPED_ARRAY_PROTOTYPE_INDEX, JSObject, typed_array_prototype) \ 261 V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \ 262 V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function) \ 263 V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \ 264 V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function) \ 265 V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \ 266 V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun) \ 267 V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function) \ 268 NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V) \ 269 NATIVE_CONTEXT_IMPORTED_FIELDS(V) 270 271 // A table of all script contexts. Every loaded top-level script with top-level 272 // lexical declarations contributes its ScriptContext into this table. 273 // 274 // The table is a fixed array, its first slot is the current used count and 275 // the subsequent slots 1..used contain ScriptContexts. 276 class ScriptContextTable : public FixedArray { 277 public: 278 // Conversions. 279 static inline ScriptContextTable* cast(Object* context); 280 281 struct LookupResult { 282 int context_index; 283 int slot_index; 284 VariableMode mode; 285 InitializationFlag init_flag; 286 MaybeAssignedFlag maybe_assigned_flag; 287 }; 288 289 inline int used() const; 290 inline void set_used(int used); 291 292 static inline Handle<Context> GetContext(Handle<ScriptContextTable> table, 293 int i); 294 295 // Lookup a variable `name` in a ScriptContextTable. 296 // If it returns true, the variable is found and `result` contains 297 // valid information about its location. 298 // If it returns false, `result` is untouched. 299 MUST_USE_RESULT 300 static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name, 301 LookupResult* result); 302 303 MUST_USE_RESULT 304 static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table, 305 Handle<Context> script_context); 306 GetContextOffset(int context_index)307 static int GetContextOffset(int context_index) { 308 return kFirstContextOffset + context_index * kPointerSize; 309 } 310 311 private: 312 static const int kUsedSlot = 0; 313 static const int kFirstContextSlot = kUsedSlot + 1; 314 static const int kFirstContextOffset = 315 FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize; 316 317 DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable); 318 }; 319 320 // JSFunctions are pairs (context, function code), sometimes also called 321 // closures. A Context object is used to represent function contexts and 322 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak). 323 // 324 // At runtime, the contexts build a stack in parallel to the execution 325 // stack, with the top-most context being the current context. All contexts 326 // have the following slots: 327 // 328 // [ closure ] This is the current function. It is the same for all 329 // contexts inside a function. It provides access to the 330 // incoming context (i.e., the outer context, which may 331 // or may not become the current function's context), and 332 // it provides access to the functions code and thus it's 333 // scope information, which in turn contains the names of 334 // statically allocated context slots. The names are needed 335 // for dynamic lookups in the presence of 'with' or 'eval'. 336 // 337 // [ previous ] A pointer to the previous context. It is NULL for 338 // function contexts, and non-NULL for 'with' contexts. 339 // Used to implement the 'with' statement. 340 // 341 // [ extension ] A pointer to an extension JSObject, or "the hole". Used to 342 // implement 'with' statements and dynamic declarations 343 // (through 'eval'). The object in a 'with' statement is 344 // stored in the extension slot of a 'with' context. 345 // Dynamically declared variables/functions are also added 346 // to lazily allocated extension object. Context::Lookup 347 // searches the extension object for properties. 348 // For script and block contexts, contains the respective 349 // ScopeInfo. For block contexts representing sloppy declaration 350 // block scopes, it may also be a struct being a 351 // SloppyBlockWithEvalContextExtension, pairing the ScopeInfo 352 // with an extension object. 353 // For module contexts, points back to the respective JSModule. 354 // 355 // [ global_object ] A pointer to the global object. Provided for quick 356 // access to the global object from inside the code (since 357 // we always have a context pointer). 358 // 359 // In addition, function contexts may have statically allocated context slots 360 // to store local variables/functions that are accessed from inner functions 361 // (via static context addresses) or through 'eval' (dynamic context lookups). 362 // The native context contains additional slots for fast access to native 363 // properties. 364 // 365 // Finally, with Harmony scoping, the JSFunction representing a top level 366 // script will have the ScriptContext rather than a FunctionContext. 367 // Script contexts from all top-level scripts are gathered in 368 // ScriptContextTable. 369 370 class Context: public FixedArray { 371 public: 372 // Conversions. 373 static inline Context* cast(Object* context); 374 375 // The default context slot layout; indices are FixedArray slot indices. 376 enum { 377 // These slots are in all contexts. 378 CLOSURE_INDEX, 379 PREVIOUS_INDEX, 380 // The extension slot is used for either the global object (in native 381 // contexts), eval extension object (function contexts), subject of with 382 // (with contexts), or the variable name (catch contexts), the serialized 383 // scope info (block contexts), or the module instance (module contexts). 384 EXTENSION_INDEX, 385 NATIVE_CONTEXT_INDEX, 386 387 // These slots are only in native contexts. 388 #define NATIVE_CONTEXT_SLOT(index, type, name) index, 389 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT) 390 #undef NATIVE_CONTEXT_SLOT 391 392 // Properties from here are treated as weak references by the full GC. 393 // Scavenge treats them as strong references. 394 OPTIMIZED_FUNCTIONS_LIST, // Weak. 395 OPTIMIZED_CODE_LIST, // Weak. 396 DEOPTIMIZED_CODE_LIST, // Weak. 397 NEXT_CONTEXT_LINK, // Weak. 398 399 // Total number of slots. 400 NATIVE_CONTEXT_SLOTS, 401 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST, 402 FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, 403 404 MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX, 405 // This slot holds the thrown value in catch contexts. 406 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS, 407 408 // These slots hold values in debug evaluate contexts. 409 WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_SLOTS, 410 WHITE_LIST_INDEX = MIN_CONTEXT_SLOTS + 1 411 }; 412 413 void IncrementErrorsThrown(); 414 int GetErrorsThrown(); 415 416 // Direct slot access. 417 inline JSFunction* closure(); 418 inline void set_closure(JSFunction* closure); 419 420 inline Context* previous(); 421 inline void set_previous(Context* context); 422 423 inline Object* next_context_link(); 424 425 inline bool has_extension(); 426 inline HeapObject* extension(); 427 inline void set_extension(HeapObject* object); 428 JSObject* extension_object(); 429 JSReceiver* extension_receiver(); 430 ScopeInfo* scope_info(); 431 String* catch_name(); 432 433 inline JSModule* module(); 434 inline void set_module(JSModule* module); 435 436 // Get the context where var declarations will be hoisted to, which 437 // may be the context itself. 438 Context* declaration_context(); 439 bool is_declaration_context(); 440 441 // Get the next closure's context on the context chain. 442 Context* closure_context(); 443 444 // Returns a JSGlobalProxy object or null. 445 JSObject* global_proxy(); 446 void set_global_proxy(JSObject* global); 447 448 // Get the JSGlobalObject object. 449 JSGlobalObject* global_object(); 450 451 // Get the script context by traversing the context chain. 452 Context* script_context(); 453 454 // Compute the native context. 455 inline Context* native_context(); 456 inline void set_native_context(Context* context); 457 458 // Predicates for context types. IsNativeContext is also defined on Object 459 // because we frequently have to know if arbitrary objects are natives 460 // contexts. 461 inline bool IsNativeContext(); 462 inline bool IsFunctionContext(); 463 inline bool IsCatchContext(); 464 inline bool IsWithContext(); 465 inline bool IsDebugEvaluateContext(); 466 inline bool IsBlockContext(); 467 inline bool IsModuleContext(); 468 inline bool IsScriptContext(); 469 470 inline bool HasSameSecurityTokenAs(Context* that); 471 472 // Initializes global variable bindings in given script context. 473 void InitializeGlobalSlots(); 474 475 // A native context holds a list of all functions with optimized code. 476 void AddOptimizedFunction(JSFunction* function); 477 void RemoveOptimizedFunction(JSFunction* function); 478 void SetOptimizedFunctionsListHead(Object* head); 479 Object* OptimizedFunctionsListHead(); 480 481 // The native context also stores a list of all optimized code and a 482 // list of all deoptimized code, which are needed by the deoptimizer. 483 void AddOptimizedCode(Code* code); 484 void SetOptimizedCodeListHead(Object* head); 485 Object* OptimizedCodeListHead(); 486 void SetDeoptimizedCodeListHead(Object* head); 487 Object* DeoptimizedCodeListHead(); 488 489 Handle<Object> ErrorMessageForCodeGenerationFromStrings(); 490 491 static int ImportedFieldIndexForName(Handle<String> name); 492 static int IntrinsicIndexForName(Handle<String> name); 493 494 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \ 495 inline void set_##name(type* value); \ 496 inline bool is_##name(type* value); \ 497 inline type* name(); 498 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS) 499 #undef NATIVE_CONTEXT_FIELD_ACCESSORS 500 501 // Lookup the slot called name, starting with the current context. 502 // There are three possibilities: 503 // 504 // 1) result->IsContext(): 505 // The binding was found in a context. *index is always the 506 // non-negative slot index. *attributes is NONE for var and let 507 // declarations, READ_ONLY for const declarations (never ABSENT). 508 // 509 // 2) result->IsJSObject(): 510 // The binding was found as a named property in a context extension 511 // object (i.e., was introduced via eval), as a property on the subject 512 // of with, or as a property of the global object. *index is -1 and 513 // *attributes is not ABSENT. 514 // 515 // 3) result.is_null(): 516 // There was no binding found, *index is always -1 and *attributes is 517 // always ABSENT. 518 Handle<Object> Lookup(Handle<String> name, 519 ContextLookupFlags flags, 520 int* index, 521 PropertyAttributes* attributes, 522 BindingFlags* binding_flags); 523 524 // Code generation support. SlotOffset(int index)525 static int SlotOffset(int index) { 526 return kHeaderSize + index * kPointerSize - kHeapObjectTag; 527 } 528 FunctionMapIndex(LanguageMode language_mode,FunctionKind kind)529 static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) { 530 if (IsGeneratorFunction(kind)) { 531 return is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX 532 : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX; 533 } 534 535 if (IsAsyncFunction(kind)) { 536 return is_strict(language_mode) ? STRICT_ASYNC_FUNCTION_MAP_INDEX 537 : SLOPPY_ASYNC_FUNCTION_MAP_INDEX; 538 } 539 540 if (IsClassConstructor(kind)) { 541 // Use strict function map (no own "caller" / "arguments") 542 return STRICT_FUNCTION_MAP_INDEX; 543 } 544 545 if (IsArrowFunction(kind) || IsConciseMethod(kind) || 546 IsAccessorFunction(kind)) { 547 return STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX; 548 } 549 550 return is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX 551 : SLOPPY_FUNCTION_MAP_INDEX; 552 } 553 ArrayMapIndex(ElementsKind elements_kind)554 static int ArrayMapIndex(ElementsKind elements_kind) { 555 DCHECK(IsFastElementsKind(elements_kind)); 556 return elements_kind + FIRST_JS_ARRAY_MAP_SLOT; 557 } 558 559 static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize; 560 static const int kNotFound = -1; 561 562 // GC support. 563 typedef FixedBodyDescriptor< 564 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor; 565 566 typedef FixedBodyDescriptor< 567 kHeaderSize, 568 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize, 569 kSize> MarkCompactBodyDescriptor; 570 571 private: 572 #ifdef DEBUG 573 // Bootstrapping-aware type checks. 574 static bool IsBootstrappingOrNativeContext(Isolate* isolate, Object* object); 575 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid); 576 #endif 577 578 STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize); 579 STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex); 580 }; 581 582 } // namespace internal 583 } // namespace v8 584 585 #endif // V8_CONTEXTS_H_ 586