contexts.h 28.5 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_CONTEXTS_H_
#define V8_CONTEXTS_H_

8
#include "src/heap/heap.h"
9
#include "src/objects.h"
10

11 12
namespace v8 {
namespace internal {
13 14 15


enum ContextLookupFlags {
16 17 18 19
  FOLLOW_CONTEXT_CHAIN = 1 << 0,
  FOLLOW_PROTOTYPE_CHAIN = 1 << 1,
  STOP_AT_DECLARATION_SCOPE = 1 << 2,
  SKIP_WITH_CONTEXT = 1 << 3,
20 21

  DONT_FOLLOW_CHAINS = 0,
22 23 24
  FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN,
  LEXICAL_TEST =
      FOLLOW_CONTEXT_CHAIN | STOP_AT_DECLARATION_SCOPE | SKIP_WITH_CONTEXT,
25 26 27
};


28 29
// ES5 10.2 defines lexical environments with mutable and immutable bindings.
// Immutable bindings have two states, initialized and uninitialized, and
30 31 32
// their state is changed by the InitializeImmutableBinding method. The
// BindingFlags enum represents information if a binding has definitely been
// 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 37
//  * The function name of a named function literal. The binding is immediately
//    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 42 43
// uninitialized state for mutable bindings.
//  * A 'let' declared variable. They are initialized when evaluating the
//    corresponding declaration statement. They need to be checked for being
44
//    initialized and thus get the flag BINDING_CHECK_INITIALIZED.
45 46
//  * A 'var' declared variable. It is initialized immediately upon creation
//    and thus doesn't need to be checked. It gets the flag
47
//    BINDING_IS_INITIALIZED.
48 49
//  * Catch bound variables, function parameters and variables introduced by
//    function declarations are initialized immediately and do not need to be
50 51
//    checked. Thus they get the flag BINDING_IS_INITIALIZED.
// Accessing an uninitialized binding produces a reference error.
52 53 54
//
// In V8 uninitialized bindings are set to the hole value upon creation and set
// to a different value upon initialization.
55
enum BindingFlags {
56 57
  BINDING_IS_INITIALIZED,
  BINDING_CHECK_INITIALIZED,
58 59 60
  MISSING_BINDING
};

61 62 63 64 65 66 67 68 69
// Heap-allocated activation contexts.
//
// Contexts are implemented as FixedArray objects; the Context
// class is a convenience interface casted on a FixedArray object.
//
// Note: Context must have no virtual functions and Context objects
// must always be allocated via Heap::AllocateContext() or
// Factory::NewContext.

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#define NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                           \
  V(IS_ARRAYLIKE, JSFunction, is_arraylike)                             \
  V(GET_TEMPLATE_CALL_SITE_INDEX, JSFunction, get_template_call_site)   \
  V(MAKE_RANGE_ERROR_INDEX, JSFunction, make_range_error)               \
  V(MAKE_TYPE_ERROR_INDEX, JSFunction, make_type_error)                 \
  V(OBJECT_DEFINE_PROPERTIES, JSFunction, object_define_properties)     \
  V(OBJECT_DEFINE_PROPERTY, JSFunction, object_define_property)         \
  V(OBJECT_FREEZE, JSFunction, object_freeze)                           \
  V(OBJECT_GET_PROTOTYPE_OF, JSFunction, object_get_prototype_of)       \
  V(OBJECT_IS_EXTENSIBLE, JSFunction, object_is_extensible)             \
  V(OBJECT_IS_FROZEN, JSFunction, object_is_frozen)                     \
  V(OBJECT_IS_SEALED, JSFunction, object_is_sealed)                     \
  V(OBJECT_KEYS, JSFunction, object_keys)                               \
  V(REFLECT_APPLY_INDEX, JSFunction, reflect_apply)                     \
  V(REFLECT_CONSTRUCT_INDEX, JSFunction, reflect_construct)             \
  V(REFLECT_DEFINE_PROPERTY_INDEX, JSFunction, reflect_define_property) \
  V(REFLECT_DELETE_PROPERTY_INDEX, JSFunction, reflect_delete_property) \
  V(SPREAD_ARGUMENTS_INDEX, JSFunction, spread_arguments)               \
  V(SPREAD_ITERABLE_INDEX, JSFunction, spread_iterable)                 \
  V(ORDINARY_HAS_INSTANCE_INDEX, JSFunction, ordinary_has_instance)     \
  V(MATH_FLOOR, JSFunction, math_floor)                                 \
91
  V(MATH_SQRT, JSFunction, math_sqrt)
92

93
#define NATIVE_CONTEXT_IMPORTED_FIELDS(V)                                     \
94 95 96 97 98 99 100
  V(ARRAY_CONCAT_INDEX, JSFunction, array_concat)                             \
  V(ARRAY_POP_INDEX, JSFunction, array_pop)                                   \
  V(ARRAY_PUSH_INDEX, JSFunction, array_push)                                 \
  V(ARRAY_SHIFT_INDEX, JSFunction, array_shift)                               \
  V(ARRAY_SPLICE_INDEX, JSFunction, array_splice)                             \
  V(ARRAY_SLICE_INDEX, JSFunction, array_slice)                               \
  V(ARRAY_UNSHIFT_INDEX, JSFunction, array_unshift)                           \
101
  V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)           \
102
  V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                     \
103 104 105 106 107 108 109 110 111 112
  V(ERROR_FUNCTION_INDEX, JSFunction, error_function)                         \
  V(EVAL_ERROR_FUNCTION_INDEX, JSFunction, eval_error_function)               \
  V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)         \
  V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                       \
  V(JSON_SERIALIZE_ADAPTER_INDEX, JSFunction, json_serialize_adapter)         \
  V(MAKE_ERROR_FUNCTION_INDEX, JSFunction, make_error_function)               \
  V(MAP_DELETE_METHOD_INDEX, JSFunction, map_delete)                          \
  V(MAP_GET_METHOD_INDEX, JSFunction, map_get)                                \
  V(MAP_HAS_METHOD_INDEX, JSFunction, map_has)                                \
  V(MAP_SET_METHOD_INDEX, JSFunction, map_set)                                \
113
  V(MATH_POW_METHOD_INDEX, JSFunction, math_pow)                              \
114 115 116
  V(MESSAGE_GET_COLUMN_NUMBER_INDEX, JSFunction, message_get_column_number)   \
  V(MESSAGE_GET_LINE_NUMBER_INDEX, JSFunction, message_get_line_number)       \
  V(MESSAGE_GET_SOURCE_LINE_INDEX, JSFunction, message_get_source_line)       \
117 118
  V(NO_SIDE_EFFECTS_TO_STRING_FUN_INDEX, JSFunction,                          \
    no_side_effects_to_string_fun)                                            \
119 120
  V(OBJECT_VALUE_OF, JSFunction, object_value_of)                             \
  V(OBJECT_TO_STRING, JSFunction, object_to_string)                           \
121 122 123
  V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                           \
  V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                           \
  V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                         \
124
  V(PROMISE_FUNCTION_INDEX, JSFunction, promise_function)                     \
125 126 127 128 129 130 131 132 133 134 135 136 137
  V(PROMISE_HAS_USER_DEFINED_REJECT_HANDLER_INDEX, JSFunction,                \
    promise_has_user_defined_reject_handler)                                  \
  V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                         \
  V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                       \
  V(PROMISE_THEN_INDEX, JSFunction, promise_then)                             \
  V(RANGE_ERROR_FUNCTION_INDEX, JSFunction, range_error_function)             \
  V(REFERENCE_ERROR_FUNCTION_INDEX, JSFunction, reference_error_function)     \
  V(SET_ADD_METHOD_INDEX, JSFunction, set_add)                                \
  V(SET_DELETE_METHOD_INDEX, JSFunction, set_delete)                          \
  V(SET_HAS_METHOD_INDEX, JSFunction, set_has)                                \
  V(STACK_OVERFLOW_BOILERPLATE_INDEX, JSObject, stack_overflow_boilerplate)   \
  V(SYNTAX_ERROR_FUNCTION_INDEX, JSFunction, syntax_error_function)           \
  V(TYPE_ERROR_FUNCTION_INDEX, JSFunction, type_error_function)               \
138
  V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function)
139

140 141
#define NATIVE_CONTEXT_FIELDS(V)                                               \
  V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
142
  V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
143
  /* Below is alpha-sorted */                                                  \
144 145
  V(ACCESSOR_PROPERTY_DESCRIPTOR_MAP_INDEX, Map,                               \
    accessor_property_descriptor_map)                                          \
146
  V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
147
  V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
148
  V(ARRAY_BUFFER_MAP_INDEX, Map, array_buffer_map)                             \
149 150 151 152 153
  V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
  V(BOOL16X8_FUNCTION_INDEX, JSFunction, bool16x8_function)                    \
  V(BOOL32X4_FUNCTION_INDEX, JSFunction, bool32x4_function)                    \
  V(BOOL8X16_FUNCTION_INDEX, JSFunction, bool8x16_function)                    \
  V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
154 155 156 157
  V(BOUND_FUNCTION_WITH_CONSTRUCTOR_MAP_INDEX, Map,                            \
    bound_function_with_constructor_map)                                       \
  V(BOUND_FUNCTION_WITHOUT_CONSTRUCTOR_MAP_INDEX, Map,                         \
    bound_function_without_constructor_map)                                    \
158 159
  V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
    call_as_constructor_delegate)                                              \
160
  V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
161
  V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
162
  V(DATA_PROPERTY_DESCRIPTOR_MAP_INDEX, Map, data_property_descriptor_map)     \
163
  V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
164
  V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
165 166
  V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
    error_message_for_code_gen_from_strings)                                   \
167
  V(ERRORS_THROWN_INDEX, Smi, errors_thrown)                                   \
168
  V(EXTRAS_EXPORTS_OBJECT_INDEX, JSObject, extras_binding_object)              \
169
  V(EXTRAS_UTILS_OBJECT_INDEX, JSObject, extras_utils_object)                  \
170 171 172 173
  V(FAST_ALIASED_ARGUMENTS_MAP_INDEX, Map, fast_aliased_arguments_map)         \
  V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
  V(FLOAT32X4_FUNCTION_INDEX, JSFunction, float32x4_function)                  \
  V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
174
  V(TEMPLATE_INSTANTIATIONS_CACHE_INDEX, UnseededNumberDictionary,             \
175
    template_instantiations_cache)                                             \
176 177 178
  V(FUNCTION_FUNCTION_INDEX, JSFunction, function_function)                    \
  V(GENERATOR_FUNCTION_FUNCTION_INDEX, JSFunction,                             \
    generator_function_function)                                               \
179
  V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
180 181 182 183 184 185 186 187 188
  V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
  V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
  V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
  V(INT16X8_FUNCTION_INDEX, JSFunction, int16x8_function)                      \
  V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
  V(INT32X4_FUNCTION_INDEX, JSFunction, int32x4_function)                      \
  V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
  V(INT8X16_FUNCTION_INDEX, JSFunction, int8x16_function)                      \
  V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
189
  V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
190 191 192 193 194 195 196 197 198 199 200
  V(JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX, Map,                                 \
    js_array_fast_smi_elements_map_index)                                      \
  V(JS_ARRAY_FAST_HOLEY_SMI_ELEMENTS_MAP_INDEX, Map,                           \
    js_array_fast_holey_smi_elements_map_index)                                \
  V(JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, Map, js_array_fast_elements_map_index)   \
  V(JS_ARRAY_FAST_HOLEY_ELEMENTS_MAP_INDEX, Map,                               \
    js_array_fast_holey_elements_map_index)                                    \
  V(JS_ARRAY_FAST_DOUBLE_ELEMENTS_MAP_INDEX, Map,                              \
    js_array_fast_double_elements_map_index)                                   \
  V(JS_ARRAY_FAST_HOLEY_DOUBLE_ELEMENTS_MAP_INDEX, Map,                        \
    js_array_fast_holey_double_elements_map_index)                             \
201
  V(JS_MAP_FUN_INDEX, JSFunction, js_map_fun)                                  \
202
  V(JS_MAP_MAP_INDEX, Map, js_map_map)                                         \
203
  V(JS_SET_FUN_INDEX, JSFunction, js_set_fun)                                  \
204
  V(JS_SET_MAP_INDEX, Map, js_set_map)                                         \
205 206
  V(JS_WEAK_MAP_FUN_INDEX, JSFunction, js_weak_map_fun)                        \
  V(JS_WEAK_SET_FUN_INDEX, JSFunction, js_weak_set_fun)                        \
207
  V(MAP_CACHE_INDEX, Object, map_cache)                                        \
208
  V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
209
  V(STRING_ITERATOR_MAP_INDEX, Map, string_iterator_map)                       \
210
  V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
211
  V(NATIVES_UTILS_OBJECT_INDEX, Object, natives_utils_object)                  \
212 213 214
  V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
  V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
  V(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
215
  V(OBJECT_FUNCTION_PROTOTYPE_MAP_INDEX, Map, object_function_prototype_map)   \
216
  V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
217 218
  V(PROXY_CALLABLE_MAP_INDEX, Map, proxy_callable_map)                         \
  V(PROXY_CONSTRUCTOR_MAP_INDEX, Map, proxy_constructor_map)                   \
219
  V(PROXY_FUNCTION_INDEX, JSFunction, proxy_function)                          \
220 221
  V(PROXY_FUNCTION_MAP_INDEX, Map, proxy_function_map)                         \
  V(PROXY_MAP_INDEX, Map, proxy_map)                                           \
222 223 224 225 226
  V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
  V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
  V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)      \
  V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
  V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
227
  V(SELF_WEAK_CELL_INDEX, WeakCell, self_weak_cell)                            \
228 229 230 231 232 233 234 235
  V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
  V(SHARED_ARRAY_BUFFER_FUN_INDEX, JSFunction, shared_array_buffer_fun)        \
  V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
  V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
  V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
    sloppy_function_without_prototype_map)                                     \
  V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
    sloppy_function_with_readonly_prototype_map)                               \
236
  V(WASM_FUNCTION_MAP_INDEX, Map, wasm_function_map)                           \
237 238 239 240 241 242 243 244 245 246 247
  V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
  V(SLOW_ALIASED_ARGUMENTS_MAP_INDEX, Map, slow_aliased_arguments_map)         \
  V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
  V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
  V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
    strict_function_without_prototype_map)                                     \
  V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
  V(STRING_FUNCTION_INDEX, JSFunction, string_function)                        \
  V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map)   \
  V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function)                        \
  V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
248
  V(UINT16X8_FUNCTION_INDEX, JSFunction, uint16x8_function)                    \
249
  V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
250
  V(UINT32X4_FUNCTION_INDEX, JSFunction, uint32x4_function)                    \
251 252
  V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
  V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
253
  V(UINT8X16_FUNCTION_INDEX, JSFunction, uint8x16_function)                    \
254 255
  NATIVE_CONTEXT_INTRINSIC_FUNCTIONS(V)                                        \
  NATIVE_CONTEXT_IMPORTED_FIELDS(V)
256

257 258
// A table of all script contexts. Every loaded top-level script with top-level
// lexical declarations contributes its ScriptContext into this table.
259 260
//
// The table is a fixed array, its first slot is the current used count and
261 262
// the subsequent slots 1..used contain ScriptContexts.
class ScriptContextTable : public FixedArray {
263 264
 public:
  // Conversions.
265
  static inline ScriptContextTable* cast(Object* context);
266 267 268 269 270 271 272 273 274

  struct LookupResult {
    int context_index;
    int slot_index;
    VariableMode mode;
    InitializationFlag init_flag;
    MaybeAssignedFlag maybe_assigned_flag;
  };

275 276
  inline int used() const;
  inline void set_used(int used);
277

278 279
  static inline Handle<Context> GetContext(Handle<ScriptContextTable> table,
                                           int i);
280

281
  // Lookup a variable `name` in a ScriptContextTable.
282 283 284 285
  // If it returns true, the variable is found and `result` contains
  // valid information about its location.
  // If it returns false, `result` is untouched.
  MUST_USE_RESULT
286
  static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
287 288 289
                     LookupResult* result);

  MUST_USE_RESULT
290 291
  static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
                                           Handle<Context> script_context);
292

293 294 295 296
  static int GetContextOffset(int context_index) {
    return kFirstContextOffset + context_index * kPointerSize;
  }

297 298
 private:
  static const int kUsedSlot = 0;
299
  static const int kFirstContextSlot = kUsedSlot + 1;
300
  static const int kFirstContextOffset =
301
      FixedArray::kHeaderSize + kFirstContextSlot * kPointerSize;
302

303
  DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
304
};
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

// JSFunctions are pairs (context, function code), sometimes also called
// closures. A Context object is used to represent function contexts and
// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
//
// At runtime, the contexts build a stack in parallel to the execution
// stack, with the top-most context being the current context. All contexts
// have the following slots:
//
// [ closure   ]  This is the current function. It is the same for all
//                contexts inside a function. It provides access to the
//                incoming context (i.e., the outer context, which may
//                or may not become the current function's context), and
//                it provides access to the functions code and thus it's
//                scope information, which in turn contains the names of
//                statically allocated context slots. The names are needed
//                for dynamic lookups in the presence of 'with' or 'eval'.
//
// [ previous  ]  A pointer to the previous context. It is NULL for
//                function contexts, and non-NULL for 'with' contexts.
//                Used to implement the 'with' statement.
//
327
// [ extension ]  A pointer to an extension JSObject, or "the hole". Used to
328 329 330 331 332 333
//                implement 'with' statements and dynamic declarations
//                (through 'eval'). The object in a 'with' statement is
//                stored in the extension slot of a 'with' context.
//                Dynamically declared variables/functions are also added
//                to lazily allocated extension object. Context::Lookup
//                searches the extension object for properties.
334 335 336 337 338
//                For script and block contexts, contains the respective
//                ScopeInfo. For block contexts representing sloppy declaration
//                block scopes, it may also be a struct being a
//                SloppyBlockWithEvalContextExtension, pairing the ScopeInfo
//                with an extension object.
339
//                For module contexts, points back to the respective JSModule.
340
//
341
// [ global_object ]  A pointer to the global object. Provided for quick
342 343 344 345 346 347
//                access to the global object from inside the code (since
//                we always have a context pointer).
//
// In addition, function contexts may have statically allocated context slots
// to store local variables/functions that are accessed from inner functions
// (via static context addresses) or through 'eval' (dynamic context lookups).
348 349 350 351
// The native context contains additional slots for fast access to native
// properties.
//
// Finally, with Harmony scoping, the JSFunction representing a top level
352 353 354
// script will have the ScriptContext rather than a FunctionContext.
// Script contexts from all top-level scripts are gathered in
// ScriptContextTable.
355 356 357 358

class Context: public FixedArray {
 public:
  // Conversions.
359
  static inline Context* cast(Object* context);
360 361 362 363 364 365

  // The default context slot layout; indices are FixedArray slot indices.
  enum {
    // These slots are in all contexts.
    CLOSURE_INDEX,
    PREVIOUS_INDEX,
366
    // The extension slot is used for either the global object (in native
367
    // contexts), eval extension object (function contexts), subject of with
368
    // (with contexts), or the variable name (catch contexts), the serialized
369
    // scope info (block contexts), or the module instance (module contexts).
370
    EXTENSION_INDEX,
371
    NATIVE_CONTEXT_INDEX,
372

373
    // These slots are only in native contexts.
374 375 376
#define NATIVE_CONTEXT_SLOT(index, type, name) index,
    NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_SLOT)
#undef NATIVE_CONTEXT_SLOT
377 378 379

    // Properties from here are treated as weak references by the full GC.
    // Scavenge treats them as strong references.
380
    OPTIMIZED_FUNCTIONS_LIST,  // Weak.
381 382 383
    OPTIMIZED_CODE_LIST,       // Weak.
    DEOPTIMIZED_CODE_LIST,     // Weak.
    NEXT_CONTEXT_LINK,         // Weak.
384 385

    // Total number of slots.
386
    NATIVE_CONTEXT_SLOTS,
387
    FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST,
388
    FIRST_JS_ARRAY_MAP_SLOT = JS_ARRAY_FAST_SMI_ELEMENTS_MAP_INDEX,
389 390 391 392

    MIN_CONTEXT_SLOTS = GLOBAL_PROXY_INDEX,
    // This slot holds the thrown value in catch contexts.
    THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
393 394 395 396

    // These slots hold values in debug evaluate contexts.
    WRAPPED_CONTEXT_INDEX = MIN_CONTEXT_SLOTS,
    WHITE_LIST_INDEX = MIN_CONTEXT_SLOTS + 1
397 398
  };

399 400 401
  void IncrementErrorsThrown();
  int GetErrorsThrown();

402
  // Direct slot access.
403 404
  inline JSFunction* closure();
  inline void set_closure(JSFunction* closure);
405

406 407
  inline Context* previous();
  inline void set_previous(Context* context);
408

409
  inline bool has_extension();
410 411
  inline HeapObject* extension();
  inline void set_extension(HeapObject* object);
412 413 414 415
  JSObject* extension_object();
  JSReceiver* extension_receiver();
  ScopeInfo* scope_info();
  String* catch_name();
416

417 418
  inline JSModule* module();
  inline void set_module(JSModule* module);
419

420 421 422
  // Get the context where var declarations will be hoisted to, which
  // may be the context itself.
  Context* declaration_context();
423
  bool is_declaration_context();
424

425 426 427
  // Get the next closure's context on the context chain.
  Context* closure_context();

428 429 430 431
  // Returns a JSGlobalProxy object or null.
  JSObject* global_proxy();
  void set_global_proxy(JSObject* global);

432 433 434
  // Get the JSGlobalObject object.
  JSGlobalObject* global_object();

435 436
  // Get the script context by traversing the context chain.
  Context* script_context();
437

438 439 440
  // Compute the native context.
  inline Context* native_context();
  inline void set_native_context(Context* context);
441

442
  // Predicates for context types.  IsNativeContext is also defined on Object
443
  // because we frequently have to know if arbitrary objects are natives
444
  // contexts.
445 446 447 448
  inline bool IsNativeContext();
  inline bool IsFunctionContext();
  inline bool IsCatchContext();
  inline bool IsWithContext();
449
  inline bool IsDebugEvaluateContext();
450 451 452
  inline bool IsBlockContext();
  inline bool IsModuleContext();
  inline bool IsScriptContext();
453

454
  inline bool HasSameSecurityTokenAs(Context* that);
455

456 457 458
  // Initializes global variable bindings in given script context.
  void InitializeGlobalSlots();

459
  // A native context holds a list of all functions with optimized code.
460 461
  void AddOptimizedFunction(JSFunction* function);
  void RemoveOptimizedFunction(JSFunction* function);
462
  void SetOptimizedFunctionsListHead(Object* head);
463
  Object* OptimizedFunctionsListHead();
464 465 466 467 468 469 470 471

  // The native context also stores a list of all optimized code and a
  // list of all deoptimized code, which are needed by the deoptimizer.
  void AddOptimizedCode(Code* code);
  void SetOptimizedCodeListHead(Object* head);
  Object* OptimizedCodeListHead();
  void SetDeoptimizedCodeListHead(Object* head);
  Object* DeoptimizedCodeListHead();
472

473 474
  Handle<Object> ErrorMessageForCodeGenerationFromStrings();

475 476 477
  static int ImportedFieldIndexForName(Handle<String> name);
  static int IntrinsicIndexForName(Handle<String> name);

478
#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
479 480 481
  inline void set_##name(type* value);                    \
  inline bool is_##name(type* value);                     \
  inline type* name();
482 483
  NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
#undef NATIVE_CONTEXT_FIELD_ACCESSORS
484

485
  // Lookup the slot called name, starting with the current context.
486
  // There are three possibilities:
487
  //
488 489 490 491
  // 1) result->IsContext():
  //    The binding was found in a context.  *index is always the
  //    non-negative slot index.  *attributes is NONE for var and let
  //    declarations, READ_ONLY for const declarations (never ABSENT).
492
  //
493 494 495 496 497
  // 2) result->IsJSObject():
  //    The binding was found as a named property in a context extension
  //    object (i.e., was introduced via eval), as a property on the subject
  //    of with, or as a property of the global object.  *index is -1 and
  //    *attributes is not ABSENT.
498
  //
499 500 501
  // 3) result.is_null():
  //    There was no binding found, *index is always -1 and *attributes is
  //    always ABSENT.
502 503
  Handle<Object> Lookup(Handle<String> name,
                        ContextLookupFlags flags,
504
                        int* index,
505 506
                        PropertyAttributes* attributes,
                        BindingFlags* binding_flags);
507 508 509 510 511

  // Code generation support.
  static int SlotOffset(int index) {
    return kHeaderSize + index * kPointerSize - kHeapObjectTag;
  }
512

513
  static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
514
    if (IsGeneratorFunction(kind)) {
515
      return is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
516
                                      : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
517 518
    }

519
    if (IsClassConstructor(kind)) {
520
      // Use strict function map (no own "caller" / "arguments")
521
      return STRICT_FUNCTION_MAP_INDEX;
522 523
    }

524 525
    if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
        IsAccessorFunction(kind)) {
526
      return STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
527 528
    }

529
    return is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
530
                                    : SLOPPY_FUNCTION_MAP_INDEX;
531 532
  }

533
  static int ArrayMapIndex(ElementsKind elements_kind) {
534
    DCHECK(IsFastElementsKind(elements_kind));
535
    return elements_kind + FIRST_JS_ARRAY_MAP_SLOT;
536 537
  }

538
  static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
539
  static const int kNotFound = -1;
540 541 542 543 544 545 546 547 548 549

  // GC support.
  typedef FixedBodyDescriptor<
      kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;

  typedef FixedBodyDescriptor<
      kHeaderSize,
      kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
      kSize> MarkCompactBodyDescriptor;

550 551 552
 private:
#ifdef DEBUG
  // Bootstrapping-aware type checks.
553
  static bool IsBootstrappingOrNativeContext(Isolate* isolate, Object* object);
554
  static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
555
#endif
556

557 558
  STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
  STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
559 560
};

561 562
}  // namespace internal
}  // namespace v8
563 564

#endif  // V8_CONTEXTS_H_