v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
contexts.h
Go to the documentation of this file.
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_CONTEXTS_H_
29 #define V8_CONTEXTS_H_
30 
31 #include "heap.h"
32 #include "objects.h"
33 
34 namespace v8 {
35 namespace internal {
36 
37 
41 
44 };
45 
46 
47 // ES5 10.2 defines lexical environments with mutable and immutable bindings.
48 // Immutable bindings have two states, initialized and uninitialized, and
49 // their state is changed by the InitializeImmutableBinding method. The
50 // BindingFlags enum represents information if a binding has definitely been
51 // initialized. A mutable binding does not need to be checked and thus has
52 // the BindingFlag MUTABLE_IS_INITIALIZED.
53 //
54 // There are two possibilities for immutable bindings
55 // * 'const' declared variables. They are initialized when evaluating the
56 // corresponding declaration statement. They need to be checked for being
57 // initialized and thus get the flag IMMUTABLE_CHECK_INITIALIZED.
58 // * The function name of a named function literal. The binding is immediately
59 // initialized when entering the function and thus does not need to be
60 // checked. it gets the BindingFlag IMMUTABLE_IS_INITIALIZED.
61 // Accessing an uninitialized binding produces the undefined value.
62 //
63 // The harmony proposal for block scoped bindings also introduces the
64 // uninitialized state for mutable bindings.
65 // * A 'let' declared variable. They are initialized when evaluating the
66 // corresponding declaration statement. They need to be checked for being
67 // initialized and thus get the flag MUTABLE_CHECK_INITIALIZED.
68 // * A 'var' declared variable. It is initialized immediately upon creation
69 // and thus doesn't need to be checked. It gets the flag
70 // MUTABLE_IS_INITIALIZED.
71 // * Catch bound variables, function parameters and variables introduced by
72 // function declarations are initialized immediately and do not need to be
73 // checked. Thus they get the flag MUTABLE_IS_INITIALIZED.
74 // Immutable bindings in harmony mode get the _HARMONY flag variants. Accessing
75 // an uninitialized binding produces a reference error.
76 //
77 // In V8 uninitialized bindings are set to the hole value upon creation and set
78 // to a different value upon initialization.
87 };
88 
89 
90 // Heap-allocated activation contexts.
91 //
92 // Contexts are implemented as FixedArray objects; the Context
93 // class is a convenience interface casted on a FixedArray object.
94 //
95 // Note: Context must have no virtual functions and Context objects
96 // must always be allocated via Heap::AllocateContext() or
97 // Factory::NewContext.
98 
99 #define NATIVE_CONTEXT_FIELDS(V) \
100  V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
101  V(SECURITY_TOKEN_INDEX, Object, security_token) \
102  V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
103  V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
104  V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
105  V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
106  V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
107  V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
108  V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
109  V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps) \
110  V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
111  V(JSON_OBJECT_INDEX, JSObject, json_object) \
112  V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
113  V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
114  V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
115  V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
116  V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
117  V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
118  V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
119  V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
120  V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
121  V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
122  V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
123  V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
124  V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
125  V(FUNCTION_MAP_INDEX, Map, function_map) \
126  V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
127  V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
128  V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
129  strict_mode_function_without_prototype_map) \
130  V(FUNCTION_INSTANCE_MAP_INDEX, Map, function_instance_map) \
131  V(STRICT_MODE_FUNCTION_INSTANCE_MAP_INDEX, Map, \
132  strict_mode_function_instance_map) \
133  V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
134  V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
135  V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
136  aliased_arguments_boilerplate) \
137  V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
138  strict_mode_arguments_boilerplate) \
139  V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
140  V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
141  V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
142  V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
143  V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
144  V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
145  V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
146  V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
147  V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
148  V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
149  call_as_constructor_delegate) \
150  V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
151  V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
152  V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
153  V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
154  V(MAP_CACHE_INDEX, Object, map_cache) \
155  V(CONTEXT_DATA_INDEX, Object, data) \
156  V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
157  V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
158  error_message_for_code_gen_from_strings) \
159  V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
160  to_complete_property_descriptor) \
161  V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
162  V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
163  V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
164  V(PROXY_ENUMERATE, JSFunction, proxy_enumerate) \
165  V(RANDOM_SEED_INDEX, ByteArray, random_seed)
166 
167 // JSFunctions are pairs (context, function code), sometimes also called
168 // closures. A Context object is used to represent function contexts and
169 // dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
170 //
171 // At runtime, the contexts build a stack in parallel to the execution
172 // stack, with the top-most context being the current context. All contexts
173 // have the following slots:
174 //
175 // [ closure ] This is the current function. It is the same for all
176 // contexts inside a function. It provides access to the
177 // incoming context (i.e., the outer context, which may
178 // or may not become the current function's context), and
179 // it provides access to the functions code and thus it's
180 // scope information, which in turn contains the names of
181 // statically allocated context slots. The names are needed
182 // for dynamic lookups in the presence of 'with' or 'eval'.
183 //
184 // [ previous ] A pointer to the previous context. It is NULL for
185 // function contexts, and non-NULL for 'with' contexts.
186 // Used to implement the 'with' statement.
187 //
188 // [ extension ] A pointer to an extension JSObject, or NULL. Used to
189 // implement 'with' statements and dynamic declarations
190 // (through 'eval'). The object in a 'with' statement is
191 // stored in the extension slot of a 'with' context.
192 // Dynamically declared variables/functions are also added
193 // to lazily allocated extension object. Context::Lookup
194 // searches the extension object for properties.
195 // For global and block contexts, contains the respective
196 // ScopeInfo.
197 // For module contexts, points back to the respective JSModule.
198 //
199 // [ global_object ] A pointer to the global object. Provided for quick
200 // access to the global object from inside the code (since
201 // we always have a context pointer).
202 //
203 // In addition, function contexts may have statically allocated context slots
204 // to store local variables/functions that are accessed from inner functions
205 // (via static context addresses) or through 'eval' (dynamic context lookups).
206 // Finally, the native context contains additional slots for fast access to
207 // native properties.
208 
209 class Context: public FixedArray {
210  public:
211  // Conversions.
212  static Context* cast(Object* context) {
213  ASSERT(context->IsContext());
214  return reinterpret_cast<Context*>(context);
215  }
216 
217  // The default context slot layout; indices are FixedArray slot indices.
218  enum {
219  // These slots are in all contexts.
222  // The extension slot is used for either the global object (in global
223  // contexts), eval extension object (function contexts), subject of with
224  // (with contexts), or the variable name (catch contexts), the serialized
225  // scope info (block contexts), or the module instance (module contexts).
229 
230  // This slot holds the thrown value in catch contexts.
232 
233  // These slots are only in native contexts.
293 
294  // Properties from here are treated as weak references by the full GC.
295  // Scavenge treats them as strong references.
297  MAP_CACHE_INDEX, // Weak.
299 
300  // Total number of slots.
302 
304  };
305 
306  // Direct slot access.
309 
311  Object* result = unchecked_previous();
312  ASSERT(IsBootstrappingOrValidParentContext(result, this));
313  return reinterpret_cast<Context*>(result);
314  }
315  void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
316 
317  bool has_extension() { return extension() != NULL; }
318  Object* extension() { return get(EXTENSION_INDEX); }
319  void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
320 
323 
324  // Get the context where var declarations will be hoisted to, which
325  // may be the context itself.
327 
329  Object* result = get(GLOBAL_OBJECT_INDEX);
330  ASSERT(IsBootstrappingOrGlobalObject(result));
331  return reinterpret_cast<GlobalObject*>(result);
332  }
334  set(GLOBAL_OBJECT_INDEX, object);
335  }
336 
337  // Returns a JSGlobalProxy object or null.
339  void set_global_proxy(JSObject* global);
340 
341  // The builtins object.
343 
344  // Compute the native context by traversing the context chain.
346 
347  // Predicates for context types. IsNativeContext is defined on Object
348  // because we frequently have to know if arbitrary objects are natives
349  // contexts.
351  Map* map = this->map();
352  return map == map->GetHeap()->function_context_map();
353  }
354  bool IsCatchContext() {
355  Map* map = this->map();
356  return map == map->GetHeap()->catch_context_map();
357  }
358  bool IsWithContext() {
359  Map* map = this->map();
360  return map == map->GetHeap()->with_context_map();
361  }
362  bool IsBlockContext() {
363  Map* map = this->map();
364  return map == map->GetHeap()->block_context_map();
365  }
367  Map* map = this->map();
368  return map == map->GetHeap()->module_context_map();
369  }
371  Map* map = this->map();
372  return map == map->GetHeap()->global_context_map();
373  }
374 
375  // Tells whether the native context is marked with out of memory.
376  inline bool has_out_of_memory();
377 
378  // Mark the native context with out of memory.
379  inline void mark_out_of_memory();
380 
381  // A native context hold a list of all functions which have been optimized.
382  void AddOptimizedFunction(JSFunction* function);
383  void RemoveOptimizedFunction(JSFunction* function);
386 
388 
389 #define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
390  void set_##name(type* value) { \
391  ASSERT(IsNativeContext()); \
392  set(index, value); \
393  } \
394  type* name() { \
395  ASSERT(IsNativeContext()); \
396  return type::cast(get(index)); \
397  }
399 #undef NATIVE_CONTEXT_FIELD_ACCESSORS
400 
401  // Lookup the slot called name, starting with the current context.
402  // There are three possibilities:
403  //
404  // 1) result->IsContext():
405  // The binding was found in a context. *index is always the
406  // non-negative slot index. *attributes is NONE for var and let
407  // declarations, READ_ONLY for const declarations (never ABSENT).
408  //
409  // 2) result->IsJSObject():
410  // The binding was found as a named property in a context extension
411  // object (i.e., was introduced via eval), as a property on the subject
412  // of with, or as a property of the global object. *index is -1 and
413  // *attributes is not ABSENT.
414  //
415  // 3) result.is_null():
416  // There was no binding found, *index is always -1 and *attributes is
417  // always ABSENT.
418  Handle<Object> Lookup(Handle<String> name,
420  int* index,
421  PropertyAttributes* attributes,
422  BindingFlags* binding_flags);
423 
424  // Code generation support.
425  static int SlotOffset(int index) {
426  return kHeaderSize + index * kPointerSize - kHeapObjectTag;
427  }
428 
430 
431  // GC support.
432  typedef FixedBodyDescriptor<
434 
435  typedef FixedBodyDescriptor<
436  kHeaderSize,
439 
440  private:
441  // Unchecked access to the slots.
442  Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
443 
444 #ifdef DEBUG
445  // Bootstrapping-aware type checks.
446  static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
447  static bool IsBootstrappingOrGlobalObject(Object* object);
448 #endif
449 };
450 
451 } } // namespace v8::internal
452 
453 #endif // V8_CONTEXTS_H_
bool has_out_of_memory()
Definition: isolate.h:1440
#define NATIVE_CONTEXT_FIELDS(V)
Definition: contexts.h:99
static int SlotOffset(int index)
Definition: contexts.h:425
void set(int index, Object *value)
Definition: objects-inl.h:1757
JSModule * module()
Definition: contexts.h:321
Context * previous()
Definition: contexts.h:310
JSBuiltinsObject * builtins()
Definition: contexts.cc:47
void set_global_proxy(JSObject *global)
Definition: contexts.cc:82
void mark_out_of_memory()
Definition: isolate.h:1446
#define ASSERT(condition)
Definition: checks.h:270
static Context * cast(Object *context)
Definition: contexts.h:212
void set_module(JSModule *module)
Definition: contexts.h:322
PropertyAttributes
void set_closure(JSFunction *closure)
Definition: contexts.h:308
void ClearOptimizedFunctions()
Definition: contexts.cc:303
GlobalObject * global_object()
Definition: contexts.h:328
Handle< Object > Lookup(Handle< String > name, ContextLookupFlags flags, int *index, PropertyAttributes *attributes, BindingFlags *binding_flags)
Definition: contexts.cc:87
void set_global_object(GlobalObject *object)
Definition: contexts.h:333
Context * native_context()
Definition: contexts.cc:58
JSObject * global_proxy()
Definition: contexts.cc:78
const int kPointerSize
Definition: globals.h:220
const int kHeapObjectTag
Definition: v8.h:4009
static const int kSize
Definition: contexts.h:429
Object * OptimizedFunctionsListHead()
Definition: contexts.cc:297
void RemoveOptimizedFunction(JSFunction *function)
Definition: contexts.cc:273
FixedBodyDescriptor< kHeaderSize, kSize, kSize > ScavengeBodyDescriptor
Definition: contexts.h:433
ContextLookupFlags
Definition: contexts.h:38
static const int kHeaderSize
Definition: objects.h:2296
#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name)
Definition: contexts.h:389
void set_extension(Object *object)
Definition: contexts.h:319
bool IsFunctionContext()
Definition: contexts.h:350
Object * extension()
Definition: contexts.h:318
void set_previous(Context *context)
Definition: contexts.h:315
void AddOptimizedFunction(JSFunction *function)
Definition: contexts.cc:243
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
Handle< Object > ErrorMessageForCodeGenerationFromStrings()
Definition: contexts.cc:308
JSFunction * closure()
Definition: contexts.h:307
Context * declaration_context()
Definition: contexts.cc:37
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if expose natives in global object expose gc extension number of stack frames to capture disable builtin natives files print a stack trace if an assertion failure occurs use random jit cookie to mask large constants trace lazy optimization use adaptive optimizations prepare for turning on always opt minimum length for automatic enable preparsing maximum number of optimization attempts before giving up cache prototype transitions automatically set the debug break flag when debugger commands are in the queue always cause a debug break before aborting maximum length of function source code printed in a stack trace max size of the new max size of the old max size of executable always perform global GCs print one trace line following each garbage collection do not print trace line after scavenger collection print more details following each garbage collection print amount of external allocated memory after each time it is adjusted flush code that we expect not to use again before full gc do incremental marking steps track object counts and memory usage use caching Perform compaction on every full GC Never perform compaction on full GC testing only Compact code space on full incremental collections Default seed for initializing random allows verbose printing trace parsing and preparsing Check icache flushes in ARM and MIPS simulator Stack alingment in bytes in print stack trace when throwing exceptions randomize hashes to avoid predictable hash Fixed seed to use to hash property activate a timer that switches between V8 threads testing_bool_flag float flag Seed used for threading test randomness A filename with extra code to be included in the Print usage including flags
Definition: flags.cc:495
static JSModule * cast(Object *obj)
Definition: objects-inl.h:4590
FixedBodyDescriptor< kHeaderSize, kHeaderSize+FIRST_WEAK_SLOT *kPointerSize, kSize > MarkCompactBodyDescriptor
Definition: contexts.h:438
static JSFunction * cast(Object *obj)