contexts.h 27.9 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 16 17 18 19 20 21 22 23


enum ContextLookupFlags {
  FOLLOW_CONTEXT_CHAIN = 1,
  FOLLOW_PROTOTYPE_CHAIN = 2,

  DONT_FOLLOW_CHAINS = 0,
  FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
};


24 25
// ES5 10.2 defines lexical environments with mutable and immutable bindings.
// Immutable bindings have two states, initialized and uninitialized, and
26 27 28 29 30 31 32 33 34 35 36 37 38
// 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
// the BindingFlag MUTABLE_IS_INITIALIZED.
//
// There are two possibilities for immutable bindings
//  * 'const' declared variables. They are initialized when evaluating the
//    corresponding declaration statement. They need to be checked for being
//    initialized and thus get the flag IMMUTABLE_CHECK_INITIALIZED.
//  * The function name of a named function literal. The binding is immediately
//    initialized when entering the function and thus does not need to be
//    checked. it gets the BindingFlag IMMUTABLE_IS_INITIALIZED.
// Accessing an uninitialized binding produces the undefined value.
39 40
//
// The harmony proposal for block scoped bindings also introduces the
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// 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
//    initialized and thus get the flag MUTABLE_CHECK_INITIALIZED.
//  * A 'var' declared variable. It is initialized immediately upon creation
//    and thus doesn't need to be checked. It gets the flag
//    MUTABLE_IS_INITIALIZED.
//  * Catch bound variables, function parameters and variables introduced by
//    function declarations are initialized immediately and do not need to be
//    checked. Thus they get the flag MUTABLE_IS_INITIALIZED.
// Immutable bindings in harmony mode get the _HARMONY flag variants. Accessing
// an uninitialized binding produces a reference error.
//
// In V8 uninitialized bindings are set to the hole value upon creation and set
// to a different value upon initialization.
56 57 58 59 60
enum BindingFlags {
  MUTABLE_IS_INITIALIZED,
  MUTABLE_CHECK_INITIALIZED,
  IMMUTABLE_IS_INITIALIZED,
  IMMUTABLE_CHECK_INITIALIZED,
61 62
  IMMUTABLE_IS_INITIALIZED_HARMONY,
  IMMUTABLE_CHECK_INITIALIZED_HARMONY,
63 64 65 66
  MISSING_BINDING
};


67 68 69 70 71 72 73 74 75
// 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.

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define NATIVE_CONTEXT_FIELDS(V)                                               \
  V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object)                         \
  V(SECURITY_TOKEN_INDEX, Object, security_token)                              \
  V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function)                      \
  V(NUMBER_FUNCTION_INDEX, JSFunction, number_function)                        \
  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(OBJECT_FUNCTION_INDEX, JSFunction, object_function)                        \
  V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function)        \
  V(ARRAY_FUNCTION_INDEX, JSFunction, array_function)                          \
  V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps)                                \
  V(DATE_FUNCTION_INDEX, JSFunction, date_function)                            \
  V(JSON_OBJECT_INDEX, JSObject, json_object)                                  \
  V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function)                        \
  V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype)        \
  V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype)          \
  V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun)                        \
  V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun)                            \
  V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun)                            \
  V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun)              \
  V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun)                            \
  V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun)                          \
  V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun)                            \
  V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun)                              \
101
  V(TO_LENGTH_FUN_INDEX, JSFunction, to_length_fun)                            \
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun)                        \
  V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun)                      \
  V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun)                        \
  V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun)                          \
  V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun)                      \
  V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun)                        \
  V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun)                      \
  V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun)                        \
  V(FLOAT32_ARRAY_FUN_INDEX, JSFunction, float32_array_fun)                    \
  V(FLOAT64_ARRAY_FUN_INDEX, JSFunction, float64_array_fun)                    \
  V(UINT8_CLAMPED_ARRAY_FUN_INDEX, JSFunction, uint8_clamped_array_fun)        \
  V(INT8_ARRAY_EXTERNAL_MAP_INDEX, Map, int8_array_external_map)               \
  V(UINT8_ARRAY_EXTERNAL_MAP_INDEX, Map, uint8_array_external_map)             \
  V(INT16_ARRAY_EXTERNAL_MAP_INDEX, Map, int16_array_external_map)             \
  V(UINT16_ARRAY_EXTERNAL_MAP_INDEX, Map, uint16_array_external_map)           \
  V(INT32_ARRAY_EXTERNAL_MAP_INDEX, Map, int32_array_external_map)             \
  V(UINT32_ARRAY_EXTERNAL_MAP_INDEX, Map, uint32_array_external_map)           \
  V(FLOAT32_ARRAY_EXTERNAL_MAP_INDEX, Map, float32_array_external_map)         \
  V(FLOAT64_ARRAY_EXTERNAL_MAP_INDEX, Map, float64_array_external_map)         \
  V(UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX, Map,                               \
    uint8_clamped_array_external_map)                                          \
  V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun)                            \
  V(SLOPPY_FUNCTION_MAP_INDEX, Map, sloppy_function_map)                       \
  V(SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX, Map,                    \
    sloppy_function_with_readonly_prototype_map)                               \
  V(STRICT_FUNCTION_MAP_INDEX, Map, strict_function_map)                       \
128
  V(STRONG_FUNCTION_MAP_INDEX, Map, strong_function_map)                       \
129 130 131 132
  V(SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
    sloppy_function_without_prototype_map)                                     \
  V(STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map,                          \
    strict_function_without_prototype_map)                                     \
133
  V(STRONG_CONSTRUCTOR_MAP_INDEX, Map, strong_constructor_map)                 \
134 135 136 137 138 139 140 141 142
  V(BOUND_FUNCTION_MAP_INDEX, Map, bound_function_map)                         \
  V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)                           \
  V(SLOPPY_ARGUMENTS_MAP_INDEX, Map, sloppy_arguments_map)                     \
  V(ALIASED_ARGUMENTS_MAP_INDEX, Map, aliased_arguments_map)                   \
  V(STRICT_ARGUMENTS_MAP_INDEX, Map, strict_arguments_map)                     \
  V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners)                      \
  V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun)                      \
  V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun)          \
  V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun)                  \
143
  V(FUNCTION_CACHE_INDEX, ObjectHashTable, function_cache)                     \
144
  V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches)      \
145
  V(NORMALIZED_MAP_CACHE_INDEX, Object, normalized_map_cache)                  \
146 147 148 149 150 151 152 153 154 155 156 157
  V(RUNTIME_CONTEXT_INDEX, Context, runtime_context)                           \
  V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate)    \
  V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction,                            \
    call_as_constructor_delegate)                                              \
  V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function)                        \
  V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function)    \
  V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function)  \
  V(MAP_CACHE_INDEX, Object, map_cache)                                        \
  V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data)                            \
  V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings)    \
  V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object,                     \
    error_message_for_code_gen_from_strings)                                   \
158
  V(PROMISE_STATUS_INDEX, Symbol, promise_status)                              \
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  V(PROMISE_CREATE_INDEX, JSFunction, promise_create)                          \
  V(PROMISE_RESOLVE_INDEX, JSFunction, promise_resolve)                        \
  V(PROMISE_REJECT_INDEX, JSFunction, promise_reject)                          \
  V(PROMISE_CHAIN_INDEX, JSFunction, promise_chain)                            \
  V(PROMISE_CATCH_INDEX, JSFunction, promise_catch)                            \
  V(PROMISE_THEN_INDEX, JSFunction, promise_then)                              \
  V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction,                         \
    to_complete_property_descriptor)                                           \
  V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap)                      \
  V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap)                      \
  V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap)                      \
  V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate)                        \
  V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change)        \
  V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice)      \
  V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, observers_begin_perform_splice)  \
  V(OBSERVERS_END_SPLICE_INDEX, JSFunction, observers_end_perform_splice)      \
  V(NATIVE_OBJECT_OBSERVE_INDEX, JSFunction, native_object_observe)            \
  V(NATIVE_OBJECT_GET_NOTIFIER_INDEX, JSFunction, native_object_get_notifier)  \
  V(NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE, JSFunction,                         \
    native_object_notifier_perform_change)                                     \
  V(SLOPPY_GENERATOR_FUNCTION_MAP_INDEX, Map, sloppy_generator_function_map)   \
  V(STRICT_GENERATOR_FUNCTION_MAP_INDEX, Map, strict_generator_function_map)   \
181
  V(STRONG_GENERATOR_FUNCTION_MAP_INDEX, Map, strong_generator_function_map)   \
182 183 184 185
  V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, generator_object_prototype_map) \
  V(ITERATOR_RESULT_MAP_INDEX, Map, iterator_result_map)                       \
  V(MAP_ITERATOR_MAP_INDEX, Map, map_iterator_map)                             \
  V(SET_ITERATOR_MAP_INDEX, Map, set_iterator_map)                             \
186
  V(ARRAY_VALUES_ITERATOR_INDEX, JSFunction, array_values_iterator)            \
187
  V(SCRIPT_CONTEXT_TABLE_INDEX, ScriptContextTable, script_context_table)
188 189


190 191
// A table of all script contexts. Every loaded top-level script with top-level
// lexical declarations contributes its ScriptContext into this table.
192 193
//
// The table is a fixed array, its first slot is the current used count and
194 195
// the subsequent slots 1..used contain ScriptContexts.
class ScriptContextTable : public FixedArray {
196 197
 public:
  // Conversions.
198 199 200
  static ScriptContextTable* cast(Object* context) {
    DCHECK(context->IsScriptContextTable());
    return reinterpret_cast<ScriptContextTable*>(context);
201 202 203 204 205 206 207 208 209 210 211 212 213 214
  }

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

  int used() const { return Smi::cast(get(kUsedSlot))->value(); }

  void set_used(int used) { set(kUsedSlot, Smi::FromInt(used)); }

215
  static Handle<Context> GetContext(Handle<ScriptContextTable> table, int i) {
216 217 218 219
    DCHECK(i < table->used());
    return Handle<Context>::cast(FixedArray::get(table, i + 1));
  }

220
  // Lookup a variable `name` in a ScriptContextTable.
221 222 223 224
  // 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
225
  static bool Lookup(Handle<ScriptContextTable> table, Handle<String> name,
226 227 228
                     LookupResult* result);

  MUST_USE_RESULT
229 230
  static Handle<ScriptContextTable> Extend(Handle<ScriptContextTable> table,
                                           Handle<Context> script_context);
231

232 233 234 235
  static int GetContextOffset(int context_index) {
    return kFirstContextOffset + context_index * kPointerSize;
  }

236 237
 private:
  static const int kUsedSlot = 0;
238 239
  static const int kFirstContextOffset =
      FixedArray::kHeaderSize + (kUsedSlot + 1) * kPointerSize;
240

241
  DISALLOW_IMPLICIT_CONSTRUCTORS(ScriptContextTable);
242
};
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

// 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.
//
// [ extension ]  A pointer to an extension JSObject, or NULL. Used to
//                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.
272 273
//                For global and block contexts, contains the respective
//                ScopeInfo.
274
//                For module contexts, points back to the respective JSModule.
275
//
276
// [ global_object ]  A pointer to the global object. Provided for quick
277 278 279 280 281 282
//                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).
283 284 285 286
// The native context contains additional slots for fast access to native
// properties.
//
// Finally, with Harmony scoping, the JSFunction representing a top level
287 288 289
// script will have the ScriptContext rather than a FunctionContext.
// Script contexts from all top-level scripts are gathered in
// ScriptContextTable.
290 291 292 293 294

class Context: public FixedArray {
 public:
  // Conversions.
  static Context* cast(Object* context) {
295
    DCHECK(context->IsContext());
296 297 298 299 300 301 302 303
    return reinterpret_cast<Context*>(context);
  }

  // The default context slot layout; indices are FixedArray slot indices.
  enum {
    // These slots are in all contexts.
    CLOSURE_INDEX,
    PREVIOUS_INDEX,
304 305
    // The extension slot is used for either the global object (in global
    // contexts), eval extension object (function contexts), subject of with
306
    // (with contexts), or the variable name (catch contexts), the serialized
307
    // scope info (block contexts), or the module instance (module contexts).
308
    EXTENSION_INDEX,
309
    GLOBAL_OBJECT_INDEX,
310 311
    MIN_CONTEXT_SLOTS,

312 313 314
    // This slot holds the thrown value in catch contexts.
    THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,

315
    // These slots are only in native contexts.
316 317
    GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
    SECURITY_TOKEN_INDEX,
318 319 320
    SLOPPY_ARGUMENTS_MAP_INDEX,
    ALIASED_ARGUMENTS_MAP_INDEX,
    STRICT_ARGUMENTS_MAP_INDEX,
321
    REGEXP_RESULT_MAP_INDEX,
322
    SLOPPY_FUNCTION_MAP_INDEX,
323
    SLOPPY_FUNCTION_WITH_READONLY_PROTOTYPE_MAP_INDEX,
324
    STRICT_FUNCTION_MAP_INDEX,
325
    STRONG_FUNCTION_MAP_INDEX,
326 327
    SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
    STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
328
    STRONG_CONSTRUCTOR_MAP_INDEX,
329
    BOUND_FUNCTION_MAP_INDEX,
330
    INITIAL_OBJECT_PROTOTYPE_INDEX,
331
    INITIAL_ARRAY_PROTOTYPE_INDEX,
332 333 334
    BOOLEAN_FUNCTION_INDEX,
    NUMBER_FUNCTION_INDEX,
    STRING_FUNCTION_INDEX,
335
    STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
336
    SYMBOL_FUNCTION_INDEX,
337
    OBJECT_FUNCTION_INDEX,
338
    INTERNAL_ARRAY_FUNCTION_INDEX,
339
    ARRAY_FUNCTION_INDEX,
340
    JS_ARRAY_MAPS_INDEX,
341
    DATE_FUNCTION_INDEX,
342
    JSON_OBJECT_INDEX,
343 344 345 346 347 348 349 350 351 352
    REGEXP_FUNCTION_INDEX,
    CREATE_DATE_FUN_INDEX,
    TO_NUMBER_FUN_INDEX,
    TO_STRING_FUN_INDEX,
    TO_DETAIL_STRING_FUN_INDEX,
    TO_OBJECT_FUN_INDEX,
    TO_INTEGER_FUN_INDEX,
    TO_UINT32_FUN_INDEX,
    TO_INT32_FUN_INDEX,
    TO_BOOLEAN_FUN_INDEX,
353
    GLOBAL_EVAL_FUN_INDEX,
354
    ARRAY_BUFFER_FUN_INDEX,
355 356 357 358 359 360
    UINT8_ARRAY_FUN_INDEX,
    INT8_ARRAY_FUN_INDEX,
    UINT16_ARRAY_FUN_INDEX,
    INT16_ARRAY_FUN_INDEX,
    UINT32_ARRAY_FUN_INDEX,
    INT32_ARRAY_FUN_INDEX,
361 362 363
    FLOAT32_ARRAY_FUN_INDEX,
    FLOAT64_ARRAY_FUN_INDEX,
    UINT8_CLAMPED_ARRAY_FUN_INDEX,
364 365 366 367 368 369 370 371 372
    INT8_ARRAY_EXTERNAL_MAP_INDEX,
    UINT8_ARRAY_EXTERNAL_MAP_INDEX,
    INT16_ARRAY_EXTERNAL_MAP_INDEX,
    UINT16_ARRAY_EXTERNAL_MAP_INDEX,
    INT32_ARRAY_EXTERNAL_MAP_INDEX,
    UINT32_ARRAY_EXTERNAL_MAP_INDEX,
    FLOAT32_ARRAY_EXTERNAL_MAP_INDEX,
    FLOAT64_ARRAY_EXTERNAL_MAP_INDEX,
    UINT8_CLAMPED_ARRAY_EXTERNAL_MAP_INDEX,
373
    DATA_VIEW_FUN_INDEX,
374 375 376 377 378
    MESSAGE_LISTENERS_INDEX,
    MAKE_MESSAGE_FUN_INDEX,
    GET_STACK_TRACE_LINE_INDEX,
    CONFIGURE_GLOBAL_INDEX,
    FUNCTION_CACHE_INDEX,
379
    JSFUNCTION_RESULT_CACHES_INDEX,
380
    NORMALIZED_MAP_CACHE_INDEX,
381 382
    RUNTIME_CONTEXT_INDEX,
    CALL_AS_FUNCTION_DELEGATE_INDEX,
383
    CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
384
    SCRIPT_FUNCTION_INDEX,
385
    OPAQUE_REFERENCE_FUNCTION_INDEX,
386 387
    CONTEXT_EXTENSION_FUNCTION_INDEX,
    OUT_OF_MEMORY_INDEX,
388
    EMBEDDER_DATA_INDEX,
389
    ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
390
    ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
rossberg@chromium.org's avatar
rossberg@chromium.org committed
391
    RUN_MICROTASKS_INDEX,
392
    ENQUEUE_MICROTASK_INDEX,
393
    PROMISE_STATUS_INDEX,
394 395 396 397 398
    PROMISE_CREATE_INDEX,
    PROMISE_RESOLVE_INDEX,
    PROMISE_REJECT_INDEX,
    PROMISE_CHAIN_INDEX,
    PROMISE_CATCH_INDEX,
399
    PROMISE_THEN_INDEX,
400
    TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
401
    DERIVED_HAS_TRAP_INDEX,
402
    DERIVED_GET_TRAP_INDEX,
403
    DERIVED_SET_TRAP_INDEX,
404 405
    PROXY_ENUMERATE_INDEX,
    OBSERVERS_NOTIFY_CHANGE_INDEX,
406 407 408
    OBSERVERS_ENQUEUE_SPLICE_INDEX,
    OBSERVERS_BEGIN_SPLICE_INDEX,
    OBSERVERS_END_SPLICE_INDEX,
409 410 411
    NATIVE_OBJECT_OBSERVE_INDEX,
    NATIVE_OBJECT_GET_NOTIFIER_INDEX,
    NATIVE_OBJECT_NOTIFIER_PERFORM_CHANGE,
412 413
    SLOPPY_GENERATOR_FUNCTION_MAP_INDEX,
    STRICT_GENERATOR_FUNCTION_MAP_INDEX,
414
    STRONG_GENERATOR_FUNCTION_MAP_INDEX,
415
    GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
416 417 418
    ITERATOR_RESULT_MAP_INDEX,
    MAP_ITERATOR_MAP_INDEX,
    SET_ITERATOR_MAP_INDEX,
419
    ARRAY_VALUES_ITERATOR_INDEX,
420
    SCRIPT_CONTEXT_TABLE_INDEX,
421
    MAP_CACHE_INDEX,
422
    TO_LENGTH_FUN_INDEX,
423 424 425

    // Properties from here are treated as weak references by the full GC.
    // Scavenge treats them as strong references.
426
    OPTIMIZED_FUNCTIONS_LIST,  // Weak.
427 428 429
    OPTIMIZED_CODE_LIST,       // Weak.
    DEOPTIMIZED_CODE_LIST,     // Weak.
    NEXT_CONTEXT_LINK,         // Weak.
430 431

    // Total number of slots.
432
    NATIVE_CONTEXT_SLOTS,
433
    FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
434 435 436 437 438 439 440
  };

  // Direct slot access.
  JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
  void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }

  Context* previous() {
441
    Object* result = unchecked_previous();
442
    DCHECK(IsBootstrappingOrValidParentContext(result, this));
443
    return reinterpret_cast<Context*>(result);
444 445 446
  }
  void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }

447 448 449
  bool has_extension() { return extension() != NULL; }
  Object* extension() { return get(EXTENSION_INDEX); }
  void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
450

451 452 453
  JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
  void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }

454 455 456 457
  // Get the context where var declarations will be hoisted to, which
  // may be the context itself.
  Context* declaration_context();

458 459
  GlobalObject* global_object() {
    Object* result = get(GLOBAL_OBJECT_INDEX);
460
    DCHECK(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
461
    return reinterpret_cast<GlobalObject*>(result);
462
  }
463 464 465
  void set_global_object(GlobalObject* object) {
    set(GLOBAL_OBJECT_INDEX, object);
  }
466

467 468 469 470
  // Returns a JSGlobalProxy object or null.
  JSObject* global_proxy();
  void set_global_proxy(JSObject* global);

471 472 473
  // The builtins object.
  JSBuiltinsObject* builtins();

474 475
  // Get the script context by traversing the context chain.
  Context* script_context();
476

477 478
  // Compute the native context by traversing the context chain.
  Context* native_context();
479

480
  // Predicates for context types.  IsNativeContext is also defined on Object
481
  // because we frequently have to know if arbitrary objects are natives
482
  // contexts.
483 484 485 486
  bool IsNativeContext() {
    Map* map = this->map();
    return map == map->GetHeap()->native_context_map();
  }
487 488 489 490 491 492 493 494
  bool IsFunctionContext() {
    Map* map = this->map();
    return map == map->GetHeap()->function_context_map();
  }
  bool IsCatchContext() {
    Map* map = this->map();
    return map == map->GetHeap()->catch_context_map();
  }
495 496 497 498
  bool IsWithContext() {
    Map* map = this->map();
    return map == map->GetHeap()->with_context_map();
  }
499 500 501 502
  bool IsBlockContext() {
    Map* map = this->map();
    return map == map->GetHeap()->block_context_map();
  }
503 504 505 506
  bool IsModuleContext() {
    Map* map = this->map();
    return map == map->GetHeap()->module_context_map();
  }
507
  bool IsScriptContext() {
508
    Map* map = this->map();
509
    return map == map->GetHeap()->script_context_map();
510
  }
511

512 513 514 515 516
  bool HasSameSecurityTokenAs(Context* that) {
    return this->global_object()->native_context()->security_token() ==
        that->global_object()->native_context()->security_token();
  }

517
  // A native context holds a list of all functions with optimized code.
518 519
  void AddOptimizedFunction(JSFunction* function);
  void RemoveOptimizedFunction(JSFunction* function);
520
  void SetOptimizedFunctionsListHead(Object* head);
521
  Object* OptimizedFunctionsListHead();
522 523 524 525 526 527 528 529

  // 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();
530

531 532
  Handle<Object> ErrorMessageForCodeGenerationFromStrings();

533
#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
534
  void  set_##name(type* value) {                         \
535
    DCHECK(IsNativeContext());                            \
536 537
    set(index, value);                                    \
  }                                                       \
538
  bool is_##name(type* value) {                           \
539
    DCHECK(IsNativeContext());                            \
540 541
    return type::cast(get(index)) == value;               \
  }                                                       \
542
  type* name() {                                          \
543
    DCHECK(IsNativeContext());                            \
544 545
    return type::cast(get(index));                        \
  }
546 547
  NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
#undef NATIVE_CONTEXT_FIELD_ACCESSORS
548

549
  // Lookup the slot called name, starting with the current context.
550
  // There are three possibilities:
551
  //
552 553 554 555
  // 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).
556
  //
557 558 559 560 561
  // 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.
562
  //
563 564 565
  // 3) result.is_null():
  //    There was no binding found, *index is always -1 and *attributes is
  //    always ABSENT.
566 567
  Handle<Object> Lookup(Handle<String> name,
                        ContextLookupFlags flags,
568
                        int* index,
569 570
                        PropertyAttributes* attributes,
                        BindingFlags* binding_flags);
571 572 573 574 575

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

577
  static int FunctionMapIndex(LanguageMode language_mode, FunctionKind kind) {
578
    if (IsGeneratorFunction(kind)) {
579 580
      return is_strong(language_mode) ? STRONG_GENERATOR_FUNCTION_MAP_INDEX :
             is_strict(language_mode) ? STRICT_GENERATOR_FUNCTION_MAP_INDEX
581
                                      : SLOPPY_GENERATOR_FUNCTION_MAP_INDEX;
582 583
    }

584 585 586 587 588 589
    if (IsConstructor(kind)) {
      return is_strong(language_mode) ? STRONG_CONSTRUCTOR_MAP_INDEX :
             is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
                                      : SLOPPY_FUNCTION_MAP_INDEX;
    }

590 591
    if (IsArrowFunction(kind) || IsConciseMethod(kind) ||
        IsAccessorFunction(kind)) {
592 593 594 595
      return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
             is_strict(language_mode) ?
                 STRICT_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX :
                 SLOPPY_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX;
596 597
    }

598 599
    return is_strong(language_mode) ? STRONG_FUNCTION_MAP_INDEX :
           is_strict(language_mode) ? STRICT_FUNCTION_MAP_INDEX
600
                                    : SLOPPY_FUNCTION_MAP_INDEX;
601 602
  }

603
  static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
604 605 606 607 608 609 610 611 612 613

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

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

614 615 616 617 618 619
 private:
  // Unchecked access to the slots.
  Object* unchecked_previous() { return get(PREVIOUS_INDEX); }

#ifdef DEBUG
  // Bootstrapping-aware type checks.
620
  static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
621
  static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
622
#endif
623

624 625
  STATIC_ASSERT(kHeaderSize == Internals::kContextHeaderSize);
  STATIC_ASSERT(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
626 627 628 629 630
};

} }  // namespace v8::internal

#endif  // V8_CONTEXTS_H_