blob: 189c215e63904b76aea1dd9493397c2f44f21d2e [file] [log] [blame]
danno@chromium.orgfa458e42012-02-01 10:48:36 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000031#include "heap.h"
32#include "objects.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37
38enum ContextLookupFlags {
39 FOLLOW_CONTEXT_CHAIN = 1,
40 FOLLOW_PROTOTYPE_CHAIN = 2,
41
42 DONT_FOLLOW_CHAINS = 0,
43 FOLLOW_CHAINS = FOLLOW_CONTEXT_CHAIN | FOLLOW_PROTOTYPE_CHAIN
44};
45
46
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000047// ES5 10.2 defines lexical environments with mutable and immutable bindings.
48// Immutable bindings have two states, initialized and uninitialized, and
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000049// 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.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000062//
63// The harmony proposal for block scoped bindings also introduces the
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000064// 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.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000079enum BindingFlags {
80 MUTABLE_IS_INITIALIZED,
81 MUTABLE_CHECK_INITIALIZED,
82 IMMUTABLE_IS_INITIALIZED,
83 IMMUTABLE_CHECK_INITIALIZED,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000084 IMMUTABLE_IS_INITIALIZED_HARMONY,
85 IMMUTABLE_CHECK_INITIALIZED_HARMONY,
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +000086 MISSING_BINDING
87};
88
89
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090// 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
yangguo@chromium.org46839fb2012-08-28 09:06:19 +000099#define NATIVE_CONTEXT_FIELDS(V) \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000100 V(GLOBAL_PROXY_INDEX, JSObject, global_proxy_object) \
101 V(SECURITY_TOKEN_INDEX, Object, security_token) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 V(BOOLEAN_FUNCTION_INDEX, JSFunction, boolean_function) \
103 V(NUMBER_FUNCTION_INDEX, JSFunction, number_function) \
104 V(STRING_FUNCTION_INDEX, JSFunction, string_function) \
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000105 V(STRING_FUNCTION_PROTOTYPE_MAP_INDEX, Map, string_function_prototype_map) \
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000106 V(SYMBOL_FUNCTION_INDEX, JSFunction, symbol_function) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 V(OBJECT_FUNCTION_INDEX, JSFunction, object_function) \
svenpanne@chromium.org3c93e772012-01-02 09:26:59 +0000108 V(INTERNAL_ARRAY_FUNCTION_INDEX, JSFunction, internal_array_function) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 V(ARRAY_FUNCTION_INDEX, JSFunction, array_function) \
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000110 V(JS_ARRAY_MAPS_INDEX, Object, js_array_maps) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111 V(DATE_FUNCTION_INDEX, JSFunction, date_function) \
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000112 V(JSON_OBJECT_INDEX, JSObject, json_object) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113 V(REGEXP_FUNCTION_INDEX, JSFunction, regexp_function) \
114 V(INITIAL_OBJECT_PROTOTYPE_INDEX, JSObject, initial_object_prototype) \
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000115 V(INITIAL_ARRAY_PROTOTYPE_INDEX, JSObject, initial_array_prototype) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116 V(CREATE_DATE_FUN_INDEX, JSFunction, create_date_fun) \
117 V(TO_NUMBER_FUN_INDEX, JSFunction, to_number_fun) \
118 V(TO_STRING_FUN_INDEX, JSFunction, to_string_fun) \
119 V(TO_DETAIL_STRING_FUN_INDEX, JSFunction, to_detail_string_fun) \
120 V(TO_OBJECT_FUN_INDEX, JSFunction, to_object_fun) \
121 V(TO_INTEGER_FUN_INDEX, JSFunction, to_integer_fun) \
122 V(TO_UINT32_FUN_INDEX, JSFunction, to_uint32_fun) \
123 V(TO_INT32_FUN_INDEX, JSFunction, to_int32_fun) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000124 V(GLOBAL_EVAL_FUN_INDEX, JSFunction, global_eval_fun) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 V(INSTANTIATE_FUN_INDEX, JSFunction, instantiate_fun) \
126 V(CONFIGURE_INSTANCE_FUN_INDEX, JSFunction, configure_instance_fun) \
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000127 V(ARRAY_BUFFER_FUN_INDEX, JSFunction, array_buffer_fun) \
danno@chromium.orgf005df62013-04-30 16:36:45 +0000128 V(UINT8_ARRAY_FUN_INDEX, JSFunction, uint8_array_fun) \
129 V(INT8_ARRAY_FUN_INDEX, JSFunction, int8_array_fun) \
130 V(UINT16_ARRAY_FUN_INDEX, JSFunction, uint16_array_fun) \
131 V(INT16_ARRAY_FUN_INDEX, JSFunction, int16_array_fun) \
132 V(UINT32_ARRAY_FUN_INDEX, JSFunction, uint32_array_fun) \
133 V(INT32_ARRAY_FUN_INDEX, JSFunction, int32_array_fun) \
134 V(FLOAT_ARRAY_FUN_INDEX, JSFunction, float_array_fun) \
135 V(DOUBLE_ARRAY_FUN_INDEX, JSFunction, double_array_fun) \
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000136 V(UINT8C_ARRAY_FUN_INDEX, JSFunction, uint8c_array_fun) \
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000137 V(DATA_VIEW_FUN_INDEX, JSFunction, data_view_fun) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 V(FUNCTION_MAP_INDEX, Map, function_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 V(STRICT_MODE_FUNCTION_MAP_INDEX, Map, strict_mode_function_map) \
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000140 V(FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, function_without_prototype_map) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000141 V(STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX, Map, \
142 strict_mode_function_without_prototype_map) \
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000143 V(REGEXP_RESULT_MAP_INDEX, Map, regexp_result_map)\
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 V(ARGUMENTS_BOILERPLATE_INDEX, JSObject, arguments_boilerplate) \
whesse@chromium.org7b260152011-06-20 15:33:18 +0000145 V(ALIASED_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
146 aliased_arguments_boilerplate) \
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000147 V(STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX, JSObject, \
148 strict_mode_arguments_boilerplate) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 V(MESSAGE_LISTENERS_INDEX, JSObject, message_listeners) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150 V(MAKE_MESSAGE_FUN_INDEX, JSFunction, make_message_fun) \
151 V(GET_STACK_TRACE_LINE_INDEX, JSFunction, get_stack_trace_line_fun) \
152 V(CONFIGURE_GLOBAL_INDEX, JSFunction, configure_global_fun) \
153 V(FUNCTION_CACHE_INDEX, JSObject, function_cache) \
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000154 V(JSFUNCTION_RESULT_CACHES_INDEX, FixedArray, jsfunction_result_caches) \
ricow@chromium.org65fae842010-08-25 15:26:24 +0000155 V(NORMALIZED_MAP_CACHE_INDEX, NormalizedMapCache, normalized_map_cache) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 V(RUNTIME_CONTEXT_INDEX, Context, runtime_context) \
157 V(CALL_AS_FUNCTION_DELEGATE_INDEX, JSFunction, call_as_function_delegate) \
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000158 V(CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, JSFunction, \
159 call_as_constructor_delegate) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 V(SCRIPT_FUNCTION_INDEX, JSFunction, script_function) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000161 V(OPAQUE_REFERENCE_FUNCTION_INDEX, JSFunction, opaque_reference_function) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 V(CONTEXT_EXTENSION_FUNCTION_INDEX, JSFunction, context_extension_function) \
ager@chromium.org236ad962008-09-25 09:45:57 +0000163 V(OUT_OF_MEMORY_INDEX, Object, out_of_memory) \
ager@chromium.org9085a012009-05-11 19:22:57 +0000164 V(MAP_CACHE_INDEX, Object, map_cache) \
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000165 V(EMBEDDER_DATA_INDEX, FixedArray, embedder_data) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000166 V(ALLOW_CODE_GEN_FROM_STRINGS_INDEX, Object, allow_code_gen_from_strings) \
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000167 V(ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX, Object, \
168 error_message_for_code_gen_from_strings) \
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000169 V(TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX, JSFunction, \
170 to_complete_property_descriptor) \
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000171 V(DERIVED_HAS_TRAP_INDEX, JSFunction, derived_has_trap) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000172 V(DERIVED_GET_TRAP_INDEX, JSFunction, derived_get_trap) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000173 V(DERIVED_SET_TRAP_INDEX, JSFunction, derived_set_trap) \
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000174 V(PROXY_ENUMERATE_INDEX, JSFunction, proxy_enumerate) \
175 V(OBSERVERS_NOTIFY_CHANGE_INDEX, JSFunction, observers_notify_change) \
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000176 V(OBSERVERS_ENQUEUE_SPLICE_INDEX, JSFunction, observers_enqueue_splice) \
177 V(OBSERVERS_BEGIN_SPLICE_INDEX, JSFunction, \
178 observers_begin_perform_splice) \
179 V(OBSERVERS_END_SPLICE_INDEX, JSFunction, \
180 observers_end_perform_splice) \
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000181 V(OBSERVERS_DELIVER_CHANGES_INDEX, JSFunction, observers_deliver_changes) \
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000182 V(GENERATOR_FUNCTION_MAP_INDEX, Map, generator_function_map) \
183 V(STRICT_MODE_GENERATOR_FUNCTION_MAP_INDEX, Map, \
184 strict_mode_generator_function_map) \
185 V(GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX, Map, \
186 generator_object_prototype_map) \
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000187 V(GENERATOR_RESULT_MAP_INDEX, Map, generator_result_map) \
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000188 V(RANDOM_SEED_INDEX, ByteArray, random_seed)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189
190// JSFunctions are pairs (context, function code), sometimes also called
191// closures. A Context object is used to represent function contexts and
192// dynamically pushed 'with' contexts (or 'scopes' in ECMA-262 speak).
193//
194// At runtime, the contexts build a stack in parallel to the execution
195// stack, with the top-most context being the current context. All contexts
196// have the following slots:
197//
198// [ closure ] This is the current function. It is the same for all
199// contexts inside a function. It provides access to the
200// incoming context (i.e., the outer context, which may
201// or may not become the current function's context), and
202// it provides access to the functions code and thus it's
203// scope information, which in turn contains the names of
204// statically allocated context slots. The names are needed
205// for dynamic lookups in the presence of 'with' or 'eval'.
206//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207// [ previous ] A pointer to the previous context. It is NULL for
208// function contexts, and non-NULL for 'with' contexts.
209// Used to implement the 'with' statement.
210//
211// [ extension ] A pointer to an extension JSObject, or NULL. Used to
212// implement 'with' statements and dynamic declarations
213// (through 'eval'). The object in a 'with' statement is
214// stored in the extension slot of a 'with' context.
215// Dynamically declared variables/functions are also added
216// to lazily allocated extension object. Context::Lookup
217// searches the extension object for properties.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000218// For global and block contexts, contains the respective
219// ScopeInfo.
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000220// For module contexts, points back to the respective JSModule.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221//
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000222// [ global_object ] A pointer to the global object. Provided for quick
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223// access to the global object from inside the code (since
224// we always have a context pointer).
225//
226// In addition, function contexts may have statically allocated context slots
227// to store local variables/functions that are accessed from inner functions
228// (via static context addresses) or through 'eval' (dynamic context lookups).
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000229// Finally, the native context contains additional slots for fast access to
230// native properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231
232class Context: public FixedArray {
233 public:
234 // Conversions.
235 static Context* cast(Object* context) {
236 ASSERT(context->IsContext());
237 return reinterpret_cast<Context*>(context);
238 }
239
240 // The default context slot layout; indices are FixedArray slot indices.
241 enum {
242 // These slots are in all contexts.
243 CLOSURE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 PREVIOUS_INDEX,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000245 // The extension slot is used for either the global object (in global
246 // contexts), eval extension object (function contexts), subject of with
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000247 // (with contexts), or the variable name (catch contexts), the serialized
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000248 // scope info (block contexts), or the module instance (module contexts).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 EXTENSION_INDEX,
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000250 GLOBAL_OBJECT_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 MIN_CONTEXT_SLOTS,
252
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000253 // This slot holds the thrown value in catch contexts.
254 THROWN_OBJECT_INDEX = MIN_CONTEXT_SLOTS,
255
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000256 // These slots are only in native contexts.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000257 GLOBAL_PROXY_INDEX = MIN_CONTEXT_SLOTS,
258 SECURITY_TOKEN_INDEX,
259 ARGUMENTS_BOILERPLATE_INDEX,
whesse@chromium.org7b260152011-06-20 15:33:18 +0000260 ALIASED_ARGUMENTS_BOILERPLATE_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000261 STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX,
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000262 REGEXP_RESULT_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 FUNCTION_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000264 STRICT_MODE_FUNCTION_MAP_INDEX,
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000265 FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000266 STRICT_MODE_FUNCTION_WITHOUT_PROTOTYPE_MAP_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 INITIAL_OBJECT_PROTOTYPE_INDEX,
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000268 INITIAL_ARRAY_PROTOTYPE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 BOOLEAN_FUNCTION_INDEX,
270 NUMBER_FUNCTION_INDEX,
271 STRING_FUNCTION_INDEX,
ager@chromium.orgea4f62e2010-08-16 16:28:43 +0000272 STRING_FUNCTION_PROTOTYPE_MAP_INDEX,
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000273 SYMBOL_FUNCTION_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 OBJECT_FUNCTION_INDEX,
svenpanne@chromium.org3c93e772012-01-02 09:26:59 +0000275 INTERNAL_ARRAY_FUNCTION_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 ARRAY_FUNCTION_INDEX,
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +0000277 JS_ARRAY_MAPS_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 DATE_FUNCTION_INDEX,
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000279 JSON_OBJECT_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 REGEXP_FUNCTION_INDEX,
281 CREATE_DATE_FUN_INDEX,
282 TO_NUMBER_FUN_INDEX,
283 TO_STRING_FUN_INDEX,
284 TO_DETAIL_STRING_FUN_INDEX,
285 TO_OBJECT_FUN_INDEX,
286 TO_INTEGER_FUN_INDEX,
287 TO_UINT32_FUN_INDEX,
288 TO_INT32_FUN_INDEX,
289 TO_BOOLEAN_FUN_INDEX,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000290 GLOBAL_EVAL_FUN_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 INSTANTIATE_FUN_INDEX,
292 CONFIGURE_INSTANCE_FUN_INDEX,
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000293 ARRAY_BUFFER_FUN_INDEX,
danno@chromium.orgf005df62013-04-30 16:36:45 +0000294 UINT8_ARRAY_FUN_INDEX,
295 INT8_ARRAY_FUN_INDEX,
296 UINT16_ARRAY_FUN_INDEX,
297 INT16_ARRAY_FUN_INDEX,
298 UINT32_ARRAY_FUN_INDEX,
299 INT32_ARRAY_FUN_INDEX,
300 FLOAT_ARRAY_FUN_INDEX,
301 DOUBLE_ARRAY_FUN_INDEX,
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000302 UINT8C_ARRAY_FUN_INDEX,
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000303 DATA_VIEW_FUN_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 MESSAGE_LISTENERS_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 MAKE_MESSAGE_FUN_INDEX,
306 GET_STACK_TRACE_LINE_INDEX,
307 CONFIGURE_GLOBAL_INDEX,
308 FUNCTION_CACHE_INDEX,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000309 JSFUNCTION_RESULT_CACHES_INDEX,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000310 NORMALIZED_MAP_CACHE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 RUNTIME_CONTEXT_INDEX,
312 CALL_AS_FUNCTION_DELEGATE_INDEX,
sgjesse@chromium.org05521fc2009-05-21 07:37:44 +0000313 CALL_AS_CONSTRUCTOR_DELEGATE_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 SCRIPT_FUNCTION_INDEX,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000315 OPAQUE_REFERENCE_FUNCTION_INDEX,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 CONTEXT_EXTENSION_FUNCTION_INDEX,
317 OUT_OF_MEMORY_INDEX,
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000318 EMBEDDER_DATA_INDEX,
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +0000319 ALLOW_CODE_GEN_FROM_STRINGS_INDEX,
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000320 ERROR_MESSAGE_FOR_CODE_GEN_FROM_STRINGS_INDEX,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000321 TO_COMPLETE_PROPERTY_DESCRIPTOR_INDEX,
rossberg@chromium.org717967f2011-07-20 13:44:42 +0000322 DERIVED_HAS_TRAP_INDEX,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000323 DERIVED_GET_TRAP_INDEX,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000324 DERIVED_SET_TRAP_INDEX,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000325 PROXY_ENUMERATE_INDEX,
326 OBSERVERS_NOTIFY_CHANGE_INDEX,
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000327 OBSERVERS_ENQUEUE_SPLICE_INDEX,
328 OBSERVERS_BEGIN_SPLICE_INDEX,
329 OBSERVERS_END_SPLICE_INDEX,
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +0000330 OBSERVERS_DELIVER_CHANGES_INDEX,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000331 GENERATOR_FUNCTION_MAP_INDEX,
332 STRICT_MODE_GENERATOR_FUNCTION_MAP_INDEX,
333 GENERATOR_OBJECT_PROTOTYPE_MAP_INDEX,
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000334 GENERATOR_RESULT_MAP_INDEX,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000335 RANDOM_SEED_INDEX,
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000336
337 // Properties from here are treated as weak references by the full GC.
338 // Scavenge treats them as strong references.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000339 OPTIMIZED_FUNCTIONS_LIST, // Weak.
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000340 OPTIMIZED_CODE_LIST, // Weak.
341 DEOPTIMIZED_CODE_LIST, // Weak.
342 MAP_CACHE_INDEX, // Weak.
343 NEXT_CONTEXT_LINK, // Weak.
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000344
345 // Total number of slots.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000346 NATIVE_CONTEXT_SLOTS,
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000347
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000348 FIRST_WEAK_SLOT = OPTIMIZED_FUNCTIONS_LIST
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000349 };
350
351 // Direct slot access.
352 JSFunction* closure() { return JSFunction::cast(get(CLOSURE_INDEX)); }
353 void set_closure(JSFunction* closure) { set(CLOSURE_INDEX, closure); }
354
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000355 Context* previous() {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000356 Object* result = unchecked_previous();
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000357 ASSERT(IsBootstrappingOrValidParentContext(result, this));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000358 return reinterpret_cast<Context*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 }
360 void set_previous(Context* context) { set(PREVIOUS_INDEX, context); }
361
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000362 bool has_extension() { return extension() != NULL; }
363 Object* extension() { return get(EXTENSION_INDEX); }
364 void set_extension(Object* object) { set(EXTENSION_INDEX, object); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000365
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000366 JSModule* module() { return JSModule::cast(get(EXTENSION_INDEX)); }
367 void set_module(JSModule* module) { set(EXTENSION_INDEX, module); }
368
vegorov@chromium.org3cf47312011-06-29 13:20:01 +0000369 // Get the context where var declarations will be hoisted to, which
370 // may be the context itself.
371 Context* declaration_context();
372
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000373 GlobalObject* global_object() {
374 Object* result = get(GLOBAL_OBJECT_INDEX);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000375 ASSERT(IsBootstrappingOrGlobalObject(this->GetIsolate(), result));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000376 return reinterpret_cast<GlobalObject*>(result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 }
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000378 void set_global_object(GlobalObject* object) {
379 set(GLOBAL_OBJECT_INDEX, object);
380 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000382 // Returns a JSGlobalProxy object or null.
383 JSObject* global_proxy();
384 void set_global_proxy(JSObject* global);
385
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000386 // The builtins object.
387 JSBuiltinsObject* builtins();
388
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000389 // Get the innermost global context by traversing the context chain.
390 Context* global_context();
391
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000392 // Compute the native context by traversing the context chain.
393 Context* native_context();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000395 // Predicates for context types. IsNativeContext is also defined on Object
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000396 // because we frequently have to know if arbitrary objects are natives
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000397 // contexts.
rossberg@chromium.orgcddc71f2012-12-07 12:40:13 +0000398 bool IsNativeContext() {
399 Map* map = this->map();
400 return map == map->GetHeap()->native_context_map();
401 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000402 bool IsFunctionContext() {
403 Map* map = this->map();
404 return map == map->GetHeap()->function_context_map();
405 }
406 bool IsCatchContext() {
407 Map* map = this->map();
408 return map == map->GetHeap()->catch_context_map();
409 }
410 bool IsWithContext() {
411 Map* map = this->map();
412 return map == map->GetHeap()->with_context_map();
413 }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +0000414 bool IsBlockContext() {
415 Map* map = this->map();
416 return map == map->GetHeap()->block_context_map();
417 }
jkummerow@chromium.orgf7a58842012-02-21 10:08:21 +0000418 bool IsModuleContext() {
419 Map* map = this->map();
420 return map == map->GetHeap()->module_context_map();
421 }
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000422 bool IsGlobalContext() {
423 Map* map = this->map();
424 return map == map->GetHeap()->global_context_map();
425 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000426
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000427 // Tells whether the native context is marked with out of memory.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000428 inline bool has_out_of_memory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000429
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000430 // Mark the native context with out of memory.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000431 inline void mark_out_of_memory();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000433 // A native context holds a list of all functions with optimized code.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000434 void AddOptimizedFunction(JSFunction* function);
435 void RemoveOptimizedFunction(JSFunction* function);
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000436 void SetOptimizedFunctionsListHead(Object* head);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000437 Object* OptimizedFunctionsListHead();
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +0000438
439 // The native context also stores a list of all optimized code and a
440 // list of all deoptimized code, which are needed by the deoptimizer.
441 void AddOptimizedCode(Code* code);
442 void SetOptimizedCodeListHead(Object* head);
443 Object* OptimizedCodeListHead();
444 void SetDeoptimizedCodeListHead(Object* head);
445 Object* DeoptimizedCodeListHead();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000446
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000447 Handle<Object> ErrorMessageForCodeGenerationFromStrings();
448
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000449#define NATIVE_CONTEXT_FIELD_ACCESSORS(index, type, name) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000450 void set_##name(type* value) { \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000451 ASSERT(IsNativeContext()); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000452 set(index, value); \
453 } \
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000454 bool is_##name(type* value) { \
455 ASSERT(IsNativeContext()); \
456 return type::cast(get(index)) == value; \
457 } \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000458 type* name() { \
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000459 ASSERT(IsNativeContext()); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 return type::cast(get(index)); \
461 }
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000462 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSORS)
463#undef NATIVE_CONTEXT_FIELD_ACCESSORS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000464
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000465 // Lookup the slot called name, starting with the current context.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000466 // There are three possibilities:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467 //
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000468 // 1) result->IsContext():
469 // The binding was found in a context. *index is always the
470 // non-negative slot index. *attributes is NONE for var and let
471 // declarations, READ_ONLY for const declarations (never ABSENT).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 //
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000473 // 2) result->IsJSObject():
474 // The binding was found as a named property in a context extension
475 // object (i.e., was introduced via eval), as a property on the subject
476 // of with, or as a property of the global object. *index is -1 and
477 // *attributes is not ABSENT.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478 //
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000479 // 3) result.is_null():
480 // There was no binding found, *index is always -1 and *attributes is
481 // always ABSENT.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000482 Handle<Object> Lookup(Handle<String> name,
483 ContextLookupFlags flags,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000484 int* index,
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +0000485 PropertyAttributes* attributes,
486 BindingFlags* binding_flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
488 // Code generation support.
489 static int SlotOffset(int index) {
490 return kHeaderSize + index * kPointerSize - kHeapObjectTag;
491 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000492
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000493 static int FunctionMapIndex(LanguageMode language_mode, bool is_generator) {
494 return is_generator
495 ? (language_mode == CLASSIC_MODE
496 ? GENERATOR_FUNCTION_MAP_INDEX
497 : STRICT_MODE_GENERATOR_FUNCTION_MAP_INDEX)
498 : (language_mode == CLASSIC_MODE
499 ? FUNCTION_MAP_INDEX
500 : STRICT_MODE_FUNCTION_MAP_INDEX);
501 }
502
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000503 static const int kSize = kHeaderSize + NATIVE_CONTEXT_SLOTS * kPointerSize;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000504
505 // GC support.
506 typedef FixedBodyDescriptor<
507 kHeaderSize, kSize, kSize> ScavengeBodyDescriptor;
508
509 typedef FixedBodyDescriptor<
510 kHeaderSize,
511 kHeaderSize + FIRST_WEAK_SLOT * kPointerSize,
512 kSize> MarkCompactBodyDescriptor;
513
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000514 private:
515 // Unchecked access to the slots.
516 Object* unchecked_previous() { return get(PREVIOUS_INDEX); }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000517
518#ifdef DEBUG
519 // Bootstrapping-aware type checks.
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000520 static bool IsBootstrappingOrValidParentContext(Object* object, Context* kid);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000521 static bool IsBootstrappingOrGlobalObject(Isolate* isolate, Object* object);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000522#endif
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000523
524 STATIC_CHECK(kHeaderSize == Internals::kContextHeaderSize);
525 STATIC_CHECK(EMBEDDER_DATA_INDEX == Internals::kContextEmbedderDataIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526};
527
528} } // namespace v8::internal
529
530#endif // V8_CONTEXTS_H_