2012-02-08 09:56:33 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/scopes.h"
|
2010-10-04 14:30:43 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/accessors.h"
|
|
|
|
#include "src/bootstrapper.h"
|
|
|
|
#include "src/compiler.h"
|
|
|
|
#include "src/messages.h"
|
|
|
|
#include "src/scopeinfo.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Implementation of LocalsMap
|
|
|
|
//
|
|
|
|
// Note: We are storing the handle locations as key values in the hash map.
|
|
|
|
// When inserting a new variable via Declare(), we rely on the fact that
|
|
|
|
// the handle location remains alive for the duration of that variable
|
|
|
|
// use. Because a Variable holding a handle with the same location exists
|
|
|
|
// this is ensured.
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
VariableMap::VariableMap(Zone* zone)
|
2014-06-24 14:03:24 +00:00
|
|
|
: ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)),
|
2012-06-11 12:42:31 +00:00
|
|
|
zone_(zone) {}
|
2009-07-07 09:48:54 +00:00
|
|
|
VariableMap::~VariableMap() {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
Variable* VariableMap::Declare(Scope* scope, const AstRawString* name,
|
|
|
|
VariableMode mode, bool is_valid_lhs,
|
|
|
|
Variable::Kind kind,
|
|
|
|
InitializationFlag initialization_flag,
|
|
|
|
MaybeAssignedFlag maybe_assigned_flag,
|
|
|
|
Interface* interface) {
|
2014-06-24 14:03:24 +00:00
|
|
|
// AstRawStrings are unambiguous, i.e., the same string is always represented
|
|
|
|
// by the same AstRawString*.
|
|
|
|
// FIXME(marja): fix the type of Lookup.
|
|
|
|
Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(),
|
|
|
|
true, ZoneAllocationPolicy(zone()));
|
2008-07-03 15:10:15 +00:00
|
|
|
if (p->value == NULL) {
|
|
|
|
// The variable has not been declared yet -> insert it.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(p->key == name);
|
2014-07-30 13:54:45 +00:00
|
|
|
p->value = new (zone())
|
|
|
|
Variable(scope, name, mode, is_valid_lhs, kind, initialization_flag,
|
|
|
|
maybe_assigned_flag, interface);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return reinterpret_cast<Variable*>(p->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* VariableMap::Lookup(const AstRawString* name) {
|
|
|
|
Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash(),
|
|
|
|
false, ZoneAllocationPolicy(NULL));
|
2008-07-03 15:10:15 +00:00
|
|
|
if (p != NULL) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(reinterpret_cast<const AstRawString*>(p->key) == name);
|
|
|
|
DCHECK(p->value != NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
return reinterpret_cast<Variable*>(p->value);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Implementation of Scope
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Scope::Scope(Scope* outer_scope, ScopeType scope_type,
|
|
|
|
AstValueFactory* ast_value_factory, Zone* zone)
|
2013-02-27 14:45:59 +00:00
|
|
|
: isolate_(zone->isolate()),
|
2012-06-11 12:42:31 +00:00
|
|
|
inner_scopes_(4, zone),
|
|
|
|
variables_(zone),
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
internals_(4, zone),
|
2012-06-11 12:42:31 +00:00
|
|
|
temps_(4, zone),
|
|
|
|
params_(4, zone),
|
|
|
|
unresolved_(16, zone),
|
|
|
|
decls_(4, zone),
|
2012-03-08 13:03:07 +00:00
|
|
|
interface_(FLAG_harmony_modules &&
|
2013-06-06 13:28:22 +00:00
|
|
|
(scope_type == MODULE_SCOPE || scope_type == GLOBAL_SCOPE)
|
2012-06-11 12:42:31 +00:00
|
|
|
? Interface::NewModule(zone) : NULL),
|
|
|
|
already_resolved_(false),
|
2014-06-24 14:03:24 +00:00
|
|
|
ast_value_factory_(ast_value_factory),
|
2012-06-11 12:42:31 +00:00
|
|
|
zone_(zone) {
|
2013-06-06 13:28:22 +00:00
|
|
|
SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null());
|
2012-08-28 11:25:08 +00:00
|
|
|
// The outermost scope must be a global scope.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(scope_type == GLOBAL_SCOPE || outer_scope != NULL);
|
|
|
|
DCHECK(!HasIllegalRedeclaration());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-11 16:29:28 +00:00
|
|
|
Scope::Scope(Scope* inner_scope,
|
2013-06-06 13:28:22 +00:00
|
|
|
ScopeType scope_type,
|
2012-06-11 12:42:31 +00:00
|
|
|
Handle<ScopeInfo> scope_info,
|
2014-06-24 14:03:24 +00:00
|
|
|
AstValueFactory* value_factory,
|
2012-06-11 12:42:31 +00:00
|
|
|
Zone* zone)
|
2013-09-03 11:54:08 +00:00
|
|
|
: isolate_(zone->isolate()),
|
2012-06-11 12:42:31 +00:00
|
|
|
inner_scopes_(4, zone),
|
|
|
|
variables_(zone),
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
internals_(4, zone),
|
2012-06-11 12:42:31 +00:00
|
|
|
temps_(4, zone),
|
|
|
|
params_(4, zone),
|
|
|
|
unresolved_(16, zone),
|
|
|
|
decls_(4, zone),
|
2012-03-08 13:03:07 +00:00
|
|
|
interface_(NULL),
|
2012-06-11 12:42:31 +00:00
|
|
|
already_resolved_(true),
|
2014-06-24 14:03:24 +00:00
|
|
|
ast_value_factory_(value_factory),
|
2012-06-11 12:42:31 +00:00
|
|
|
zone_(zone) {
|
2013-06-06 13:28:22 +00:00
|
|
|
SetDefaults(scope_type, NULL, scope_info);
|
2011-11-03 10:36:55 +00:00
|
|
|
if (!scope_info.is_null()) {
|
|
|
|
num_heap_slots_ = scope_info_->ContextLength();
|
2011-01-17 08:11:03 +00:00
|
|
|
}
|
2012-02-02 09:35:12 +00:00
|
|
|
// Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context.
|
|
|
|
num_heap_slots_ = Max(num_heap_slots_,
|
|
|
|
static_cast<int>(Context::MIN_CONTEXT_SLOTS));
|
2011-07-01 14:05:46 +00:00
|
|
|
AddInnerScope(inner_scope);
|
|
|
|
}
|
|
|
|
|
2011-01-19 08:16:17 +00:00
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Scope::Scope(Scope* inner_scope, const AstRawString* catch_variable_name,
|
|
|
|
AstValueFactory* value_factory, Zone* zone)
|
2013-09-03 11:54:08 +00:00
|
|
|
: isolate_(zone->isolate()),
|
2012-06-11 12:42:31 +00:00
|
|
|
inner_scopes_(1, zone),
|
|
|
|
variables_(zone),
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
internals_(0, zone),
|
2012-06-11 12:42:31 +00:00
|
|
|
temps_(0, zone),
|
|
|
|
params_(0, zone),
|
|
|
|
unresolved_(0, zone),
|
|
|
|
decls_(0, zone),
|
2012-03-08 13:03:07 +00:00
|
|
|
interface_(NULL),
|
2012-06-11 12:42:31 +00:00
|
|
|
already_resolved_(true),
|
2014-06-24 14:03:24 +00:00
|
|
|
ast_value_factory_(value_factory),
|
2012-06-11 12:42:31 +00:00
|
|
|
zone_(zone) {
|
2011-11-03 10:36:55 +00:00
|
|
|
SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null());
|
2011-03-09 16:57:03 +00:00
|
|
|
AddInnerScope(inner_scope);
|
2011-07-01 14:05:46 +00:00
|
|
|
++num_var_or_const_;
|
2011-11-15 13:48:40 +00:00
|
|
|
num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
|
2011-07-01 14:05:46 +00:00
|
|
|
Variable* variable = variables_.Declare(this,
|
|
|
|
catch_variable_name,
|
2011-10-11 08:41:19 +00:00
|
|
|
VAR,
|
2011-07-01 14:05:46 +00:00
|
|
|
true, // Valid left-hand side.
|
2011-11-03 11:59:51 +00:00
|
|
|
Variable::NORMAL,
|
|
|
|
kCreatedInitialized);
|
2011-07-01 14:05:46 +00:00
|
|
|
AllocateHeapSlot(variable);
|
2011-01-17 08:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 13:28:22 +00:00
|
|
|
void Scope::SetDefaults(ScopeType scope_type,
|
2011-04-06 18:32:01 +00:00
|
|
|
Scope* outer_scope,
|
2011-11-03 10:36:55 +00:00
|
|
|
Handle<ScopeInfo> scope_info) {
|
2011-04-06 18:32:01 +00:00
|
|
|
outer_scope_ = outer_scope;
|
2013-06-06 13:28:22 +00:00
|
|
|
scope_type_ = scope_type;
|
2014-06-24 14:03:24 +00:00
|
|
|
scope_name_ = ast_value_factory_->empty_string();
|
2011-04-06 18:32:01 +00:00
|
|
|
dynamics_ = NULL;
|
|
|
|
receiver_ = NULL;
|
|
|
|
function_ = NULL;
|
|
|
|
arguments_ = NULL;
|
|
|
|
illegal_redecl_ = NULL;
|
|
|
|
scope_inside_with_ = false;
|
|
|
|
scope_contains_with_ = false;
|
|
|
|
scope_calls_eval_ = false;
|
2014-09-19 12:50:50 +00:00
|
|
|
asm_module_ = false;
|
|
|
|
asm_function_ = outer_scope != NULL && outer_scope->asm_module_;
|
2011-04-06 18:32:01 +00:00
|
|
|
// Inherit the strict mode from the parent scope.
|
2014-03-11 14:41:22 +00:00
|
|
|
strict_mode_ = outer_scope != NULL ? outer_scope->strict_mode_ : SLOPPY;
|
2014-03-11 14:39:08 +00:00
|
|
|
outer_scope_calls_sloppy_eval_ = false;
|
2011-04-06 18:32:01 +00:00
|
|
|
inner_scope_calls_eval_ = false;
|
|
|
|
force_eager_compilation_ = false;
|
2013-04-05 13:19:31 +00:00
|
|
|
force_context_allocation_ = (outer_scope != NULL && !is_function_scope())
|
|
|
|
? outer_scope->has_forced_context_allocation() : false;
|
2011-05-16 08:27:52 +00:00
|
|
|
num_var_or_const_ = 0;
|
2011-04-06 18:32:01 +00:00
|
|
|
num_stack_slots_ = 0;
|
|
|
|
num_heap_slots_ = 0;
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
num_modules_ = 0;
|
|
|
|
module_var_ = NULL,
|
2011-04-06 18:32:01 +00:00
|
|
|
scope_info_ = scope_info;
|
2011-10-21 10:26:59 +00:00
|
|
|
start_position_ = RelocInfo::kNoPosition;
|
|
|
|
end_position_ = RelocInfo::kNoPosition;
|
2011-11-15 13:48:40 +00:00
|
|
|
if (!scope_info.is_null()) {
|
|
|
|
scope_calls_eval_ = scope_info->CallsEval();
|
2014-03-11 14:41:22 +00:00
|
|
|
strict_mode_ = scope_info->strict_mode();
|
2011-11-15 13:48:40 +00:00
|
|
|
}
|
2011-04-06 18:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
Scope* Scope::DeserializeScopeChain(Context* context, Scope* global_scope,
|
|
|
|
Zone* zone) {
|
2011-07-01 14:05:46 +00:00
|
|
|
// Reconstruct the outer scope chain from a closure's context chain.
|
|
|
|
Scope* current_scope = NULL;
|
2011-03-09 16:57:03 +00:00
|
|
|
Scope* innermost_scope = NULL;
|
2011-07-01 14:05:46 +00:00
|
|
|
bool contains_with = false;
|
2012-08-17 09:03:08 +00:00
|
|
|
while (!context->IsNativeContext()) {
|
2011-07-01 14:05:46 +00:00
|
|
|
if (context->IsWithContext()) {
|
2012-06-11 12:42:31 +00:00
|
|
|
Scope* with_scope = new(zone) Scope(current_scope,
|
|
|
|
WITH_SCOPE,
|
|
|
|
Handle<ScopeInfo>::null(),
|
2014-06-24 14:03:24 +00:00
|
|
|
global_scope->ast_value_factory_,
|
2012-06-11 12:42:31 +00:00
|
|
|
zone);
|
2011-10-17 09:29:37 +00:00
|
|
|
current_scope = with_scope;
|
2011-07-01 14:05:46 +00:00
|
|
|
// All the inner scopes are inside a with.
|
|
|
|
contains_with = true;
|
|
|
|
for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
|
|
|
|
s->scope_inside_with_ = true;
|
|
|
|
}
|
2012-08-27 09:40:26 +00:00
|
|
|
} else if (context->IsGlobalContext()) {
|
|
|
|
ScopeInfo* scope_info = ScopeInfo::cast(context->extension());
|
|
|
|
current_scope = new(zone) Scope(current_scope,
|
|
|
|
GLOBAL_SCOPE,
|
|
|
|
Handle<ScopeInfo>(scope_info),
|
2014-06-24 14:03:24 +00:00
|
|
|
global_scope->ast_value_factory_,
|
2012-08-27 09:40:26 +00:00
|
|
|
zone);
|
2012-07-09 08:59:03 +00:00
|
|
|
} else if (context->IsModuleContext()) {
|
|
|
|
ScopeInfo* scope_info = ScopeInfo::cast(context->module()->scope_info());
|
|
|
|
current_scope = new(zone) Scope(current_scope,
|
|
|
|
MODULE_SCOPE,
|
|
|
|
Handle<ScopeInfo>(scope_info),
|
2014-06-24 14:03:24 +00:00
|
|
|
global_scope->ast_value_factory_,
|
2012-07-09 08:59:03 +00:00
|
|
|
zone);
|
2011-10-17 09:29:37 +00:00
|
|
|
} else if (context->IsFunctionContext()) {
|
2011-11-03 10:36:55 +00:00
|
|
|
ScopeInfo* scope_info = context->closure()->shared()->scope_info();
|
2012-06-11 12:42:31 +00:00
|
|
|
current_scope = new(zone) Scope(current_scope,
|
|
|
|
FUNCTION_SCOPE,
|
|
|
|
Handle<ScopeInfo>(scope_info),
|
2014-06-24 14:03:24 +00:00
|
|
|
global_scope->ast_value_factory_,
|
2012-06-11 12:42:31 +00:00
|
|
|
zone);
|
2014-09-19 12:50:50 +00:00
|
|
|
if (scope_info->IsAsmFunction()) current_scope->asm_function_ = true;
|
|
|
|
if (scope_info->IsAsmModule()) current_scope->asm_module_ = true;
|
2011-10-17 09:29:37 +00:00
|
|
|
} else if (context->IsBlockContext()) {
|
2011-11-03 10:36:55 +00:00
|
|
|
ScopeInfo* scope_info = ScopeInfo::cast(context->extension());
|
2012-06-11 12:42:31 +00:00
|
|
|
current_scope = new(zone) Scope(current_scope,
|
|
|
|
BLOCK_SCOPE,
|
|
|
|
Handle<ScopeInfo>(scope_info),
|
2014-06-24 14:03:24 +00:00
|
|
|
global_scope->ast_value_factory_,
|
2012-06-11 12:42:31 +00:00
|
|
|
zone);
|
2011-07-01 14:05:46 +00:00
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(context->IsCatchContext());
|
2011-10-17 09:29:37 +00:00
|
|
|
String* name = String::cast(context->extension());
|
2014-06-24 14:03:24 +00:00
|
|
|
current_scope = new (zone) Scope(
|
|
|
|
current_scope,
|
|
|
|
global_scope->ast_value_factory_->GetString(Handle<String>(name)),
|
|
|
|
global_scope->ast_value_factory_, zone);
|
2011-07-01 14:05:46 +00:00
|
|
|
}
|
2014-06-30 13:59:00 +00:00
|
|
|
if (contains_with) current_scope->RecordWithStatement();
|
2011-10-17 09:29:37 +00:00
|
|
|
if (innermost_scope == NULL) innermost_scope = current_scope;
|
2011-03-09 16:57:03 +00:00
|
|
|
|
2011-07-01 14:05:46 +00:00
|
|
|
// Forget about a with when we move to a context for a different function.
|
|
|
|
if (context->previous()->closure() != context->closure()) {
|
|
|
|
contains_with = false;
|
|
|
|
}
|
|
|
|
context = context->previous();
|
|
|
|
}
|
2011-03-09 16:57:03 +00:00
|
|
|
|
2011-07-01 14:05:46 +00:00
|
|
|
global_scope->AddInnerScope(current_scope);
|
2011-11-15 13:48:40 +00:00
|
|
|
global_scope->PropagateScopeInfo(false);
|
2011-07-01 14:05:46 +00:00
|
|
|
return (innermost_scope == NULL) ? global_scope : innermost_scope;
|
2011-03-09 16:57:03 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 08:11:03 +00:00
|
|
|
|
2010-10-04 14:30:43 +00:00
|
|
|
bool Scope::Analyze(CompilationInfo* info) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(info->function() != NULL);
|
2011-11-15 13:48:40 +00:00
|
|
|
Scope* scope = info->function()->scope();
|
|
|
|
Scope* top = scope;
|
2011-01-17 08:11:03 +00:00
|
|
|
|
2011-11-15 13:48:40 +00:00
|
|
|
// Traverse the scope tree up to the first unresolved scope or the global
|
|
|
|
// scope and start scope resolution and variable allocation from that scope.
|
|
|
|
while (!top->is_global_scope() &&
|
|
|
|
!top->outer_scope()->already_resolved()) {
|
|
|
|
top = top->outer_scope();
|
|
|
|
}
|
|
|
|
|
2012-02-08 09:56:33 +00:00
|
|
|
// Allocate the variables.
|
|
|
|
{
|
2014-08-22 11:12:29 +00:00
|
|
|
AstNodeFactory<AstNullVisitor> ast_node_factory(
|
|
|
|
info->zone(), info->ast_value_factory(), info->ast_node_id_gen());
|
2012-03-08 13:03:07 +00:00
|
|
|
if (!top->AllocateVariables(info, &ast_node_factory)) return false;
|
2012-02-08 09:56:33 +00:00
|
|
|
}
|
2010-10-04 14:30:43 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-03-18 20:35:07 +00:00
|
|
|
if (info->isolate()->bootstrapper()->IsActive()
|
2010-10-04 14:30:43 +00:00
|
|
|
? FLAG_print_builtin_scopes
|
|
|
|
: FLAG_print_scopes) {
|
2011-11-15 13:48:40 +00:00
|
|
|
scope->Print();
|
2010-10-04 14:30:43 +00:00
|
|
|
}
|
2012-03-08 13:03:07 +00:00
|
|
|
|
|
|
|
if (FLAG_harmony_modules && FLAG_print_interfaces && top->is_global_scope()) {
|
|
|
|
PrintF("global : ");
|
|
|
|
top->interface()->Print();
|
|
|
|
}
|
2010-10-04 14:30:43 +00:00
|
|
|
#endif
|
|
|
|
|
2014-02-10 21:38:17 +00:00
|
|
|
info->PrepareForCompilation(scope);
|
2011-12-05 14:43:28 +00:00
|
|
|
return true;
|
2010-10-04 14:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
void Scope::Initialize() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!already_resolved());
|
2011-01-17 08:11:03 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Add this scope as a new inner scope of the outer scope.
|
|
|
|
if (outer_scope_ != NULL) {
|
2012-06-11 12:42:31 +00:00
|
|
|
outer_scope_->inner_scopes_.Add(this, zone());
|
2011-10-17 09:29:37 +00:00
|
|
|
scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope();
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2011-10-17 09:29:37 +00:00
|
|
|
scope_inside_with_ = is_with_scope();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Declare convenience variables.
|
|
|
|
// Declare and allocate receiver (even for the global scope, and even
|
|
|
|
// if naccesses_ == 0).
|
|
|
|
// NOTE: When loading parameters in the global scope, we must take
|
|
|
|
// care not to access them as properties of the global object, but
|
|
|
|
// instead load them directly from the stack. Currently, the only
|
|
|
|
// such parameter is 'this' which is passed on the stack when
|
|
|
|
// invoking scripts
|
2011-10-17 09:29:37 +00:00
|
|
|
if (is_declaration_scope()) {
|
2011-06-30 14:37:55 +00:00
|
|
|
Variable* var =
|
2011-07-15 16:57:35 +00:00
|
|
|
variables_.Declare(this,
|
2014-06-24 14:03:24 +00:00
|
|
|
ast_value_factory_->this_string(),
|
2011-10-11 08:41:19 +00:00
|
|
|
VAR,
|
2011-07-15 16:57:35 +00:00
|
|
|
false,
|
2011-11-03 11:59:51 +00:00
|
|
|
Variable::THIS,
|
|
|
|
kCreatedInitialized);
|
2011-09-07 11:02:31 +00:00
|
|
|
var->AllocateTo(Variable::PARAMETER, -1);
|
2011-06-30 14:37:55 +00:00
|
|
|
receiver_ = var;
|
2011-10-17 09:29:37 +00:00
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(outer_scope() != NULL);
|
2011-10-17 09:29:37 +00:00
|
|
|
receiver_ = outer_scope()->receiver();
|
2011-06-30 14:37:55 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if (is_function_scope()) {
|
|
|
|
// Declare 'arguments' variable which exists in all functions.
|
2009-07-07 09:48:54 +00:00
|
|
|
// Note that it might never be accessed, in which case it won't be
|
|
|
|
// allocated during variable allocation.
|
2011-07-15 16:57:35 +00:00
|
|
|
variables_.Declare(this,
|
2014-06-24 14:03:24 +00:00
|
|
|
ast_value_factory_->arguments_string(),
|
2011-10-11 08:41:19 +00:00
|
|
|
VAR,
|
2011-07-15 16:57:35 +00:00
|
|
|
true,
|
2011-11-03 11:59:51 +00:00
|
|
|
Variable::ARGUMENTS,
|
|
|
|
kCreatedInitialized);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-02 12:43:28 +00:00
|
|
|
Scope* Scope::FinalizeBlockScope() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_block_scope());
|
|
|
|
DCHECK(internals_.is_empty());
|
|
|
|
DCHECK(temps_.is_empty());
|
|
|
|
DCHECK(params_.is_empty());
|
2011-09-02 12:43:28 +00:00
|
|
|
|
|
|
|
if (num_var_or_const() > 0) return this;
|
|
|
|
|
|
|
|
// Remove this scope from outer scope.
|
|
|
|
for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) {
|
|
|
|
if (outer_scope_->inner_scopes_[i] == this) {
|
|
|
|
outer_scope_->inner_scopes_.Remove(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reparent inner scopes.
|
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
|
|
|
outer_scope()->AddInnerScope(inner_scopes_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move unresolved variables
|
|
|
|
for (int i = 0; i < unresolved_.length(); i++) {
|
2012-06-11 12:42:31 +00:00
|
|
|
outer_scope()->unresolved_.Add(unresolved_[i], zone());
|
2011-09-02 12:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::LookupLocal(const AstRawString* name) {
|
2011-01-17 08:11:03 +00:00
|
|
|
Variable* result = variables_.Lookup(name);
|
2011-07-01 14:05:46 +00:00
|
|
|
if (result != NULL || scope_info_.is_null()) {
|
2011-01-17 08:11:03 +00:00
|
|
|
return result;
|
|
|
|
}
|
2014-06-24 14:03:24 +00:00
|
|
|
// The Scope is backed up by ScopeInfo. This means it cannot operate in a
|
|
|
|
// heap-independent mode, and all strings must be internalized immediately. So
|
|
|
|
// it's ok to get the Handle<String> here.
|
|
|
|
Handle<String> name_handle = name->string();
|
2011-07-01 14:05:46 +00:00
|
|
|
// If we have a serialized scope info, we might find the variable there.
|
2011-06-16 14:12:58 +00:00
|
|
|
// There should be no local slot with the given name.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0);
|
2011-01-19 08:16:17 +00:00
|
|
|
|
2011-01-17 08:11:03 +00:00
|
|
|
// Check context slot lookup.
|
2011-10-11 08:41:19 +00:00
|
|
|
VariableMode mode;
|
2012-04-12 19:32:29 +00:00
|
|
|
Variable::Location location = Variable::CONTEXT;
|
2011-11-03 11:59:51 +00:00
|
|
|
InitializationFlag init_flag;
|
2014-07-30 13:54:45 +00:00
|
|
|
MaybeAssignedFlag maybe_assigned_flag;
|
|
|
|
int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode,
|
|
|
|
&init_flag, &maybe_assigned_flag);
|
2011-06-16 14:12:58 +00:00
|
|
|
if (index < 0) {
|
|
|
|
// Check parameters.
|
2014-06-24 14:03:24 +00:00
|
|
|
index = scope_info_->ParameterIndex(*name_handle);
|
2011-11-15 13:48:40 +00:00
|
|
|
if (index < 0) return NULL;
|
2012-04-12 19:32:29 +00:00
|
|
|
|
|
|
|
mode = DYNAMIC;
|
|
|
|
location = Variable::LOOKUP;
|
|
|
|
init_flag = kCreatedInitialized;
|
2014-07-30 13:54:45 +00:00
|
|
|
// Be conservative and flag parameters as maybe assigned. Better information
|
|
|
|
// would require ScopeInfo to serialize the maybe_assigned bit also for
|
|
|
|
// parameters.
|
|
|
|
maybe_assigned_flag = kMaybeAssigned;
|
2011-06-16 06:37:49 +00:00
|
|
|
}
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL,
|
2014-07-30 13:54:45 +00:00
|
|
|
init_flag, maybe_assigned_flag);
|
2012-04-12 19:32:29 +00:00
|
|
|
var->AllocateTo(location, index);
|
2011-06-16 14:12:58 +00:00
|
|
|
return var;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::LookupFunctionVar(const AstRawString* name,
|
2012-02-08 09:56:33 +00:00
|
|
|
AstNodeFactory<AstNullVisitor>* factory) {
|
2014-06-24 14:03:24 +00:00
|
|
|
if (function_ != NULL && function_->proxy()->raw_name() == name) {
|
2012-04-16 11:48:20 +00:00
|
|
|
return function_->proxy()->var();
|
2011-11-15 13:48:40 +00:00
|
|
|
} else if (!scope_info_.is_null()) {
|
|
|
|
// If we are backed by a scope info, try to lookup the variable there.
|
|
|
|
VariableMode mode;
|
2014-06-24 14:03:24 +00:00
|
|
|
int index = scope_info_->FunctionContextSlotIndex(*(name->string()), &mode);
|
2011-11-15 13:48:40 +00:00
|
|
|
if (index < 0) return NULL;
|
2012-06-11 12:42:31 +00:00
|
|
|
Variable* var = new(zone()) Variable(
|
2012-04-16 11:48:20 +00:00
|
|
|
this, name, mode, true /* is valid LHS */,
|
|
|
|
Variable::NORMAL, kCreatedInitialized);
|
|
|
|
VariableProxy* proxy = factory->NewVariableProxy(var);
|
2013-10-14 09:24:58 +00:00
|
|
|
VariableDeclaration* declaration = factory->NewVariableDeclaration(
|
|
|
|
proxy, mode, this, RelocInfo::kNoPosition);
|
2012-04-16 11:48:20 +00:00
|
|
|
DeclareFunctionVar(declaration);
|
2011-11-15 13:48:40 +00:00
|
|
|
var->AllocateTo(Variable::CONTEXT, index);
|
|
|
|
return var;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::Lookup(const AstRawString* name) {
|
2008-11-27 13:55:06 +00:00
|
|
|
for (Scope* scope = this;
|
|
|
|
scope != NULL;
|
|
|
|
scope = scope->outer_scope()) {
|
2014-05-22 15:27:57 +00:00
|
|
|
Variable* var = scope->LookupLocal(name);
|
2008-11-27 13:55:06 +00:00
|
|
|
if (var != NULL) return var;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
Variable* Scope::DeclareParameter(const AstRawString* name, VariableMode mode) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!already_resolved());
|
|
|
|
DCHECK(is_function_scope());
|
2012-06-11 12:42:31 +00:00
|
|
|
Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL,
|
|
|
|
kCreatedInitialized);
|
|
|
|
params_.Add(var, zone());
|
2014-07-30 13:54:45 +00:00
|
|
|
return var;
|
2011-05-30 07:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode,
|
2012-03-08 13:03:07 +00:00
|
|
|
InitializationFlag init_flag,
|
2014-07-30 13:54:45 +00:00
|
|
|
MaybeAssignedFlag maybe_assigned_flag,
|
2012-03-08 13:03:07 +00:00
|
|
|
Interface* interface) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!already_resolved());
|
2014-03-11 14:41:22 +00:00
|
|
|
// This function handles VAR, LET, and CONST modes. DYNAMIC variables are
|
2011-05-30 07:33:12 +00:00
|
|
|
// introduces during variable allocation, INTERNAL variables are allocated
|
|
|
|
// explicitly, and TEMPORARY variables are allocated via NewTemporary().
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsDeclaredVariableMode(mode));
|
2011-05-30 07:33:12 +00:00
|
|
|
++num_var_or_const_;
|
2014-07-30 13:54:45 +00:00
|
|
|
return variables_.Declare(this, name, mode, true, Variable::NORMAL, init_flag,
|
|
|
|
maybe_assigned_flag, interface);
|
2009-07-07 09:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::DeclareDynamicGlobal(const AstRawString* name) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_global_scope());
|
2011-11-03 11:59:51 +00:00
|
|
|
return variables_.Declare(this,
|
|
|
|
name,
|
|
|
|
DYNAMIC_GLOBAL,
|
2011-09-07 11:02:31 +00:00
|
|
|
true,
|
2011-11-03 11:59:51 +00:00
|
|
|
Variable::NORMAL,
|
|
|
|
kCreatedInitialized);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::RemoveUnresolved(VariableProxy* var) {
|
|
|
|
// Most likely (always?) any variable we want to remove
|
|
|
|
// was just added before, so we search backwards.
|
|
|
|
for (int i = unresolved_.length(); i-- > 0;) {
|
|
|
|
if (unresolved_[i] == var) {
|
|
|
|
unresolved_.Remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::NewInternal(const AstRawString* name) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!already_resolved());
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
Variable* var = new(zone()) Variable(this,
|
|
|
|
name,
|
|
|
|
INTERNAL,
|
|
|
|
false,
|
|
|
|
Variable::NORMAL,
|
|
|
|
kCreatedInitialized);
|
|
|
|
internals_.Add(var, zone());
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::NewTemporary(const AstRawString* name) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!already_resolved());
|
2012-06-11 12:42:31 +00:00
|
|
|
Variable* var = new(zone()) Variable(this,
|
|
|
|
name,
|
|
|
|
TEMPORARY,
|
|
|
|
true,
|
|
|
|
Variable::NORMAL,
|
|
|
|
kCreatedInitialized);
|
|
|
|
temps_.Add(var, zone());
|
2010-12-07 11:31:57 +00:00
|
|
|
return var;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AddDeclaration(Declaration* declaration) {
|
2012-06-11 12:42:31 +00:00
|
|
|
decls_.Add(declaration, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::SetIllegalRedeclaration(Expression* expression) {
|
2011-02-07 09:55:42 +00:00
|
|
|
// Record only the first illegal redeclaration.
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!HasIllegalRedeclaration()) {
|
|
|
|
illegal_redecl_ = expression;
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(HasIllegalRedeclaration());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(HasIllegalRedeclaration());
|
2008-07-03 15:10:15 +00:00
|
|
|
illegal_redecl_->Accept(visitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 12:31:18 +00:00
|
|
|
Declaration* Scope::CheckConflictingVarDeclarations() {
|
|
|
|
int length = decls_.length();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Declaration* decl = decls_[i];
|
2011-10-11 08:41:19 +00:00
|
|
|
if (decl->mode() != VAR) continue;
|
2014-06-24 14:03:24 +00:00
|
|
|
const AstRawString* name = decl->proxy()->raw_name();
|
2011-10-17 09:29:37 +00:00
|
|
|
|
|
|
|
// Iterate through all scopes until and including the declaration scope.
|
|
|
|
Scope* previous = NULL;
|
|
|
|
Scope* current = decl->scope();
|
|
|
|
do {
|
2011-09-01 12:31:18 +00:00
|
|
|
// There is a conflict if there exists a non-VAR binding.
|
2011-10-17 09:29:37 +00:00
|
|
|
Variable* other_var = current->variables_.Lookup(name);
|
2011-10-11 08:41:19 +00:00
|
|
|
if (other_var != NULL && other_var->mode() != VAR) {
|
2011-09-01 12:31:18 +00:00
|
|
|
return decl;
|
|
|
|
}
|
2011-10-17 09:29:37 +00:00
|
|
|
previous = current;
|
|
|
|
current = current->outer_scope_;
|
|
|
|
} while (!previous->is_declaration_scope());
|
2011-09-01 12:31:18 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-23 08:15:38 +00:00
|
|
|
class VarAndOrder {
|
|
|
|
public:
|
|
|
|
VarAndOrder(Variable* var, int order) : var_(var), order_(order) { }
|
|
|
|
Variable* var() const { return var_; }
|
|
|
|
int order() const { return order_; }
|
|
|
|
static int Compare(const VarAndOrder* a, const VarAndOrder* b) {
|
|
|
|
return a->order_ - b->order_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Variable* var_;
|
|
|
|
int order_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-03 14:50:19 +00:00
|
|
|
void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
|
|
|
|
ZoneList<Variable*>* context_locals) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(stack_locals != NULL);
|
|
|
|
DCHECK(context_locals != NULL);
|
2011-11-03 14:50:19 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
// Collect internals which are always allocated on the heap.
|
|
|
|
for (int i = 0; i < internals_.length(); i++) {
|
|
|
|
Variable* var = internals_[i];
|
|
|
|
if (var->is_used()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var->IsContextSlot());
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
context_locals->Add(var, zone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-05 13:19:31 +00:00
|
|
|
// Collect temporaries which are always allocated on the stack, unless the
|
|
|
|
// context as a whole has forced context allocation.
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < temps_.length(); i++) {
|
|
|
|
Variable* var = temps_[i];
|
2010-03-08 13:01:24 +00:00
|
|
|
if (var->is_used()) {
|
2013-04-05 13:19:31 +00:00
|
|
|
if (var->IsContextSlot()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(has_forced_context_allocation());
|
2013-04-05 13:19:31 +00:00
|
|
|
context_locals->Add(var, zone());
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var->IsStackLocal());
|
2013-04-05 13:19:31 +00:00
|
|
|
stack_locals->Add(var, zone());
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-03 14:50:19 +00:00
|
|
|
|
|
|
|
// Collect declared local variables.
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
ZoneList<VarAndOrder> vars(variables_.occupancy(), zone());
|
2009-07-07 09:48:54 +00:00
|
|
|
for (VariableMap::Entry* p = variables_.Start();
|
|
|
|
p != NULL;
|
|
|
|
p = variables_.Next(p)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Variable* var = reinterpret_cast<Variable*>(p->value);
|
2010-03-08 13:01:24 +00:00
|
|
|
if (var->is_used()) {
|
2012-08-23 08:15:38 +00:00
|
|
|
vars.Add(VarAndOrder(var, p->order), zone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vars.Sort(VarAndOrder::Compare);
|
|
|
|
int var_count = vars.length();
|
|
|
|
for (int i = 0; i < var_count; i++) {
|
|
|
|
Variable* var = vars[i].var();
|
|
|
|
if (var->IsStackLocal()) {
|
|
|
|
stack_locals->Add(var, zone());
|
|
|
|
} else if (var->IsContextSlot()) {
|
|
|
|
context_locals->Add(var, zone());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-08 13:03:07 +00:00
|
|
|
bool Scope::AllocateVariables(CompilationInfo* info,
|
2012-02-08 09:56:33 +00:00
|
|
|
AstNodeFactory<AstNullVisitor>* factory) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// 1) Propagate scope information.
|
2014-03-11 14:39:08 +00:00
|
|
|
bool outer_scope_calls_sloppy_eval = false;
|
2011-11-15 13:48:40 +00:00
|
|
|
if (outer_scope_ != NULL) {
|
2014-03-11 14:39:08 +00:00
|
|
|
outer_scope_calls_sloppy_eval =
|
|
|
|
outer_scope_->outer_scope_calls_sloppy_eval() |
|
|
|
|
outer_scope_->calls_sloppy_eval();
|
2011-05-11 11:26:11 +00:00
|
|
|
}
|
2014-03-11 14:39:08 +00:00
|
|
|
PropagateScopeInfo(outer_scope_calls_sloppy_eval);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
// 2) Allocate module instances.
|
|
|
|
if (FLAG_harmony_modules && (is_global_scope() || is_module_scope())) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(num_modules_ == 0);
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
AllocateModulesRecursively(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) Resolve variables.
|
2012-03-08 13:03:07 +00:00
|
|
|
if (!ResolveVariablesRecursively(info, factory)) return false;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
// 4) Allocate variables.
|
2008-07-03 15:10:15 +00:00
|
|
|
AllocateVariablesRecursively();
|
2012-03-08 13:03:07 +00:00
|
|
|
|
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Scope::HasTrivialContext() const {
|
|
|
|
// A function scope has a trivial context if it always is the global
|
|
|
|
// context. We iteratively scan out the context chain to see if
|
|
|
|
// there is anything that makes this scope non-trivial; otherwise we
|
|
|
|
// return true.
|
|
|
|
for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) {
|
|
|
|
if (scope->is_eval_scope()) return false;
|
|
|
|
if (scope->scope_inside_with_) return false;
|
|
|
|
if (scope->num_heap_slots_ > 0) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Scope::HasTrivialOuterContext() const {
|
|
|
|
Scope* outer = outer_scope_;
|
|
|
|
if (outer == NULL) return true;
|
|
|
|
// Note that the outer context may be trivial in general, but the current
|
|
|
|
// scope may be inside a 'with' statement in which case the outer context
|
|
|
|
// for this scope is not trivial.
|
|
|
|
return !scope_inside_with_ && outer->HasTrivialContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 08:41:13 +00:00
|
|
|
bool Scope::HasLazyCompilableOuterContext() const {
|
|
|
|
Scope* outer = outer_scope_;
|
|
|
|
if (outer == NULL) return true;
|
2012-12-07 10:35:50 +00:00
|
|
|
// We have to prevent lazy compilation if this scope is inside a with scope
|
|
|
|
// and all declaration scopes between them have empty contexts. Such
|
|
|
|
// declaration scopes may become invisible during scope info deserialization.
|
2012-07-03 08:41:13 +00:00
|
|
|
outer = outer->DeclarationScope();
|
|
|
|
bool found_non_trivial_declarations = false;
|
|
|
|
for (const Scope* scope = outer; scope != NULL; scope = scope->outer_scope_) {
|
|
|
|
if (scope->is_with_scope() && !found_non_trivial_declarations) return false;
|
|
|
|
if (scope->is_declaration_scope() && scope->num_heap_slots() > 0) {
|
|
|
|
found_non_trivial_declarations = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-05-18 13:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 08:41:13 +00:00
|
|
|
bool Scope::AllowsLazyCompilation() const {
|
|
|
|
return !force_eager_compilation_ && HasLazyCompilableOuterContext();
|
2012-06-19 14:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-03 08:41:13 +00:00
|
|
|
bool Scope::AllowsLazyCompilationWithoutContext() const {
|
|
|
|
return !force_eager_compilation_ && HasTrivialOuterContext();
|
2012-05-18 13:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
int Scope::ContextChainLength(Scope* scope) {
|
|
|
|
int n = 0;
|
|
|
|
for (Scope* s = this; s != scope; s = s->outer_scope_) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(s != NULL); // scope must be in the scope chain
|
2013-04-26 11:55:22 +00:00
|
|
|
if (s->is_with_scope() || s->num_heap_slots() > 0) n++;
|
2013-04-26 13:22:11 +00:00
|
|
|
// Catch and module scopes always have heap slots.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!s->is_catch_scope() || s->num_heap_slots() > 0);
|
|
|
|
DCHECK(!s->is_module_scope() || s->num_heap_slots() > 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
Scope* Scope::GlobalScope() {
|
|
|
|
Scope* scope = this;
|
|
|
|
while (!scope->is_global_scope()) {
|
|
|
|
scope = scope->outer_scope();
|
|
|
|
}
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-04 09:34:47 +00:00
|
|
|
Scope* Scope::DeclarationScope() {
|
|
|
|
Scope* scope = this;
|
2011-10-17 09:29:37 +00:00
|
|
|
while (!scope->is_declaration_scope()) {
|
2011-07-04 09:34:47 +00:00
|
|
|
scope = scope->outer_scope();
|
|
|
|
}
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
Handle<ScopeInfo> Scope::GetScopeInfo() {
|
2011-08-11 16:29:28 +00:00
|
|
|
if (scope_info_.is_null()) {
|
2012-06-11 12:42:31 +00:00
|
|
|
scope_info_ = ScopeInfo::Create(this, zone());
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
return scope_info_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-21 10:26:59 +00:00
|
|
|
void Scope::GetNestedScopeChain(
|
2011-11-03 10:36:55 +00:00
|
|
|
List<Handle<ScopeInfo> >* chain,
|
2011-10-21 10:26:59 +00:00
|
|
|
int position) {
|
2011-11-29 08:43:14 +00:00
|
|
|
if (!is_eval_scope()) chain->Add(Handle<ScopeInfo>(GetScopeInfo()));
|
2011-10-21 10:26:59 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
|
|
|
Scope* scope = inner_scopes_[i];
|
|
|
|
int beg_pos = scope->start_position();
|
|
|
|
int end_pos = scope->end_position();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(beg_pos >= 0 && end_pos >= 0);
|
2011-11-29 08:43:14 +00:00
|
|
|
if (beg_pos <= position && position < end_pos) {
|
2011-10-21 10:26:59 +00:00
|
|
|
scope->GetNestedScopeChain(chain, position);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef DEBUG
|
2013-06-06 13:28:22 +00:00
|
|
|
static const char* Header(ScopeType scope_type) {
|
|
|
|
switch (scope_type) {
|
2011-10-21 10:26:59 +00:00
|
|
|
case EVAL_SCOPE: return "eval";
|
|
|
|
case FUNCTION_SCOPE: return "function";
|
2012-02-20 14:02:59 +00:00
|
|
|
case MODULE_SCOPE: return "module";
|
2011-10-21 10:26:59 +00:00
|
|
|
case GLOBAL_SCOPE: return "global";
|
|
|
|
case CATCH_SCOPE: return "catch";
|
|
|
|
case BLOCK_SCOPE: return "block";
|
|
|
|
case WITH_SCOPE: return "with";
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Indent(int n, const char* str) {
|
|
|
|
PrintF("%*s%s", n, "", str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
static void PrintName(const AstRawString* name) {
|
|
|
|
PrintF("%.*s", name->length(), name->raw_data());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-07 11:02:31 +00:00
|
|
|
static void PrintLocation(Variable* var) {
|
|
|
|
switch (var->location()) {
|
|
|
|
case Variable::UNALLOCATED:
|
|
|
|
break;
|
|
|
|
case Variable::PARAMETER:
|
|
|
|
PrintF("parameter[%d]", var->index());
|
|
|
|
break;
|
|
|
|
case Variable::LOCAL:
|
|
|
|
PrintF("local[%d]", var->index());
|
|
|
|
break;
|
|
|
|
case Variable::CONTEXT:
|
|
|
|
PrintF("context[%d]", var->index());
|
|
|
|
break;
|
|
|
|
case Variable::LOOKUP:
|
|
|
|
PrintF("lookup");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void PrintVar(int indent, Variable* var) {
|
|
|
|
if (var->is_used() || !var->IsUnallocated()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Indent(indent, Variable::Mode2String(var->mode()));
|
|
|
|
PrintF(" ");
|
2014-06-24 14:03:24 +00:00
|
|
|
PrintName(var->raw_name());
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("; // ");
|
2011-09-07 11:02:31 +00:00
|
|
|
PrintLocation(var);
|
2014-06-26 11:59:42 +00:00
|
|
|
bool comma = !var->IsUnallocated();
|
2011-11-03 14:33:46 +00:00
|
|
|
if (var->has_forced_context_allocation()) {
|
2014-06-26 11:59:42 +00:00
|
|
|
if (comma) PrintF(", ");
|
2011-11-03 14:33:46 +00:00
|
|
|
PrintF("forced context allocation");
|
2014-06-26 11:59:42 +00:00
|
|
|
comma = true;
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
if (var->maybe_assigned() == kMaybeAssigned) {
|
2014-06-26 11:59:42 +00:00
|
|
|
if (comma) PrintF(", ");
|
|
|
|
PrintF("maybe assigned");
|
2010-03-08 13:01:24 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-07 11:02:31 +00:00
|
|
|
static void PrintMap(int indent, VariableMap* map) {
|
2009-07-07 09:48:54 +00:00
|
|
|
for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
|
2009-05-13 16:27:40 +00:00
|
|
|
Variable* var = reinterpret_cast<Variable*>(p->value);
|
2011-09-07 11:02:31 +00:00
|
|
|
PrintVar(indent, var);
|
2009-05-13 16:27:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void Scope::Print(int n) {
|
|
|
|
int n0 = (n > 0 ? n : 0);
|
|
|
|
int n1 = n0 + 2; // indentation
|
|
|
|
|
|
|
|
// Print header.
|
2013-06-06 13:28:22 +00:00
|
|
|
Indent(n0, Header(scope_type_));
|
2014-06-24 14:03:24 +00:00
|
|
|
if (!scope_name_->IsEmpty()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF(" ");
|
|
|
|
PrintName(scope_name_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print parameters, if any.
|
|
|
|
if (is_function_scope()) {
|
|
|
|
PrintF(" (");
|
|
|
|
for (int i = 0; i < params_.length(); i++) {
|
|
|
|
if (i > 0) PrintF(", ");
|
2014-06-24 14:03:24 +00:00
|
|
|
PrintName(params_[i]->raw_name());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
PrintF(")");
|
|
|
|
}
|
|
|
|
|
2012-01-06 10:26:17 +00:00
|
|
|
PrintF(" { // (%d, %d)\n", start_position(), end_position());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Function name, if any (named function literals, only).
|
|
|
|
if (function_ != NULL) {
|
|
|
|
Indent(n1, "// (local) function name: ");
|
2014-06-24 14:03:24 +00:00
|
|
|
PrintName(function_->proxy()->raw_name());
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scope info.
|
|
|
|
if (HasTrivialOuterContext()) {
|
|
|
|
Indent(n1, "// scope has trivial outer context\n");
|
|
|
|
}
|
2014-03-11 14:41:22 +00:00
|
|
|
if (strict_mode() == STRICT) {
|
|
|
|
Indent(n1, "// strict mode scope\n");
|
2011-11-24 15:17:04 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
|
|
|
|
if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
|
|
|
|
if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
|
2014-03-11 14:39:08 +00:00
|
|
|
if (outer_scope_calls_sloppy_eval_) {
|
|
|
|
Indent(n1, "// outer scope calls 'eval' in sloppy context\n");
|
2011-05-11 11:26:11 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
|
|
|
|
if (num_stack_slots_ > 0) { Indent(n1, "// ");
|
|
|
|
PrintF("%d stack slots\n", num_stack_slots_); }
|
|
|
|
if (num_heap_slots_ > 0) { Indent(n1, "// ");
|
|
|
|
PrintF("%d heap slots\n", num_heap_slots_); }
|
|
|
|
|
|
|
|
// Print locals.
|
|
|
|
if (function_ != NULL) {
|
2013-08-23 09:25:37 +00:00
|
|
|
Indent(n1, "// function var:\n");
|
2012-04-16 11:48:20 +00:00
|
|
|
PrintVar(n1, function_->proxy()->var());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:25:37 +00:00
|
|
|
if (temps_.length() > 0) {
|
|
|
|
Indent(n1, "// temporary vars:\n");
|
|
|
|
for (int i = 0; i < temps_.length(); i++) {
|
|
|
|
PrintVar(n1, temps_[i]);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:25:37 +00:00
|
|
|
if (internals_.length() > 0) {
|
|
|
|
Indent(n1, "// internal vars:\n");
|
|
|
|
for (int i = 0; i < internals_.length(); i++) {
|
|
|
|
PrintVar(n1, internals_[i]);
|
|
|
|
}
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-23 09:25:37 +00:00
|
|
|
if (variables_.Start() != NULL) {
|
|
|
|
Indent(n1, "// local vars:\n");
|
|
|
|
PrintMap(n1, &variables_);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-14 07:12:58 +00:00
|
|
|
if (dynamics_ != NULL) {
|
2013-08-23 09:25:37 +00:00
|
|
|
Indent(n1, "// dynamic vars:\n");
|
2011-10-11 08:41:19 +00:00
|
|
|
PrintMap(n1, dynamics_->GetMap(DYNAMIC));
|
|
|
|
PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL));
|
|
|
|
PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL));
|
2009-05-14 07:12:58 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Print inner scopes (disable by providing negative n).
|
|
|
|
if (n >= 0) {
|
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
|
|
|
PrintF("\n");
|
|
|
|
inner_scopes_[i]->Print(n1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Indent(n0, "}\n");
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) {
|
|
|
|
if (dynamics_ == NULL) dynamics_ = new (zone()) DynamicScopePart(zone());
|
2009-07-07 09:48:54 +00:00
|
|
|
VariableMap* map = dynamics_->GetMap(mode);
|
2009-05-13 16:27:40 +00:00
|
|
|
Variable* var = map->Lookup(name);
|
|
|
|
if (var == NULL) {
|
|
|
|
// Declare a new non-local.
|
2011-11-03 11:59:51 +00:00
|
|
|
InitializationFlag init_flag = (mode == VAR)
|
|
|
|
? kCreatedInitialized : kNeedsInitialization;
|
|
|
|
var = map->Declare(NULL,
|
|
|
|
name,
|
|
|
|
mode,
|
|
|
|
true,
|
|
|
|
Variable::NORMAL,
|
|
|
|
init_flag);
|
2009-05-13 16:27:40 +00:00
|
|
|
// Allocate it by giving it a dynamic lookup.
|
2011-09-07 11:02:31 +00:00
|
|
|
var->AllocateTo(Variable::LOOKUP, -1);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-26 11:59:42 +00:00
|
|
|
Variable* Scope::LookupRecursive(VariableProxy* proxy,
|
2012-02-08 09:56:33 +00:00
|
|
|
BindingKind* binding_kind,
|
|
|
|
AstNodeFactory<AstNullVisitor>* factory) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(binding_kind != NULL);
|
2013-07-17 15:29:00 +00:00
|
|
|
if (already_resolved() && is_with_scope()) {
|
|
|
|
// Short-cut: if the scope is deserialized from a scope info, variable
|
|
|
|
// allocation is already fixed. We can simply return with dynamic lookup.
|
|
|
|
*binding_kind = DYNAMIC_LOOKUP;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Try to find the variable in this scope.
|
2014-06-26 11:59:42 +00:00
|
|
|
Variable* var = LookupLocal(proxy->raw_name());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
// We found a variable and we are done. (Even if there is an 'eval' in
|
|
|
|
// this scope which introduces the same variable again, the resulting
|
|
|
|
// variable remains the same.)
|
2008-07-03 15:10:15 +00:00
|
|
|
if (var != NULL) {
|
2011-10-17 09:29:37 +00:00
|
|
|
*binding_kind = BOUND;
|
|
|
|
return var;
|
2011-01-17 08:11:03 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
// We did not find a variable locally. Check against the function variable,
|
|
|
|
// if any. We can do this for all scopes, since the function variable is
|
|
|
|
// only present - if at all - for function scopes.
|
|
|
|
*binding_kind = UNBOUND;
|
2014-06-26 11:59:42 +00:00
|
|
|
var = LookupFunctionVar(proxy->raw_name(), factory);
|
2011-11-15 13:48:40 +00:00
|
|
|
if (var != NULL) {
|
2011-10-17 09:29:37 +00:00
|
|
|
*binding_kind = BOUND;
|
|
|
|
} else if (outer_scope_ != NULL) {
|
2014-06-26 11:59:42 +00:00
|
|
|
var = outer_scope_->LookupRecursive(proxy, binding_kind, factory);
|
2011-11-03 14:33:46 +00:00
|
|
|
if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) {
|
|
|
|
var->ForceContextAllocation();
|
|
|
|
}
|
2011-11-15 13:48:40 +00:00
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_global_scope());
|
2011-10-17 09:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_with_scope()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!already_resolved());
|
2011-10-17 09:29:37 +00:00
|
|
|
// The current scope is a with scope, so the variable binding can not be
|
|
|
|
// statically resolved. However, note that it was necessary to do a lookup
|
|
|
|
// in the outer scope anyway, because if a binding exists in an outer scope,
|
|
|
|
// the associated variable has to be marked as potentially being accessed
|
|
|
|
// from inside of an inner with scope (the property may not be in the 'with'
|
|
|
|
// object).
|
2014-06-26 11:59:42 +00:00
|
|
|
if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned();
|
2011-10-17 09:29:37 +00:00
|
|
|
*binding_kind = DYNAMIC_LOOKUP;
|
|
|
|
return NULL;
|
2014-03-11 14:39:08 +00:00
|
|
|
} else if (calls_sloppy_eval()) {
|
2011-10-17 09:29:37 +00:00
|
|
|
// A variable binding may have been found in an outer scope, but the current
|
2014-03-11 14:39:08 +00:00
|
|
|
// scope makes a sloppy 'eval' call, so the found variable may not be
|
2011-10-17 09:29:37 +00:00
|
|
|
// the correct one (the 'eval' may introduce a binding with the same name).
|
|
|
|
// In that case, change the lookup result to reflect this situation.
|
|
|
|
if (*binding_kind == BOUND) {
|
|
|
|
*binding_kind = BOUND_EVAL_SHADOWED;
|
|
|
|
} else if (*binding_kind == UNBOUND) {
|
|
|
|
*binding_kind = UNBOUND_EVAL_SHADOWED;
|
|
|
|
}
|
2009-02-18 15:55:24 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-08 13:03:07 +00:00
|
|
|
bool Scope::ResolveVariable(CompilationInfo* info,
|
2012-02-08 09:56:33 +00:00
|
|
|
VariableProxy* proxy,
|
|
|
|
AstNodeFactory<AstNullVisitor>* factory) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(info->global_scope()->is_global_scope());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// If the proxy is already resolved there's nothing to do
|
|
|
|
// (functions and consts may be resolved by the parser).
|
2012-03-08 13:03:07 +00:00
|
|
|
if (proxy->var() != NULL) return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Otherwise, try to resolve the variable.
|
2011-10-17 09:29:37 +00:00
|
|
|
BindingKind binding_kind;
|
2014-06-26 11:59:42 +00:00
|
|
|
Variable* var = LookupRecursive(proxy, &binding_kind, factory);
|
2011-10-17 09:29:37 +00:00
|
|
|
switch (binding_kind) {
|
|
|
|
case BOUND:
|
|
|
|
// We found a variable binding.
|
|
|
|
break;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
case BOUND_EVAL_SHADOWED:
|
2012-04-12 19:32:29 +00:00
|
|
|
// We either found a variable binding that might be shadowed by eval or
|
|
|
|
// gave up on it (e.g. by encountering a local with the same in the outer
|
|
|
|
// scope which was not promoted to a context, this can happen if we use
|
|
|
|
// debugger to evaluate arbitrary expressions at a break point).
|
2012-08-28 11:25:08 +00:00
|
|
|
if (var->IsGlobalObjectProperty()) {
|
2014-06-24 14:03:24 +00:00
|
|
|
var = NonLocal(proxy->raw_name(), DYNAMIC_GLOBAL);
|
2012-04-12 19:32:29 +00:00
|
|
|
} else if (var->is_dynamic()) {
|
2014-06-24 14:03:24 +00:00
|
|
|
var = NonLocal(proxy->raw_name(), DYNAMIC);
|
2011-10-17 09:29:37 +00:00
|
|
|
} else {
|
|
|
|
Variable* invalidated = var;
|
2014-06-24 14:03:24 +00:00
|
|
|
var = NonLocal(proxy->raw_name(), DYNAMIC_LOCAL);
|
2011-10-17 09:29:37 +00:00
|
|
|
var->set_local_if_not_shadowed(invalidated);
|
|
|
|
}
|
|
|
|
break;
|
2009-02-19 15:27:44 +00:00
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
case UNBOUND:
|
2012-08-27 09:40:26 +00:00
|
|
|
// No binding has been found. Declare a variable on the global object.
|
2014-06-24 14:03:24 +00:00
|
|
|
var = info->global_scope()->DeclareDynamicGlobal(proxy->raw_name());
|
2011-10-17 09:29:37 +00:00
|
|
|
break;
|
2009-02-24 13:11:53 +00:00
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
case UNBOUND_EVAL_SHADOWED:
|
2014-03-11 14:39:08 +00:00
|
|
|
// No binding has been found. But some scope makes a sloppy 'eval' call.
|
2014-06-24 14:03:24 +00:00
|
|
|
var = NonLocal(proxy->raw_name(), DYNAMIC_GLOBAL);
|
2011-10-17 09:29:37 +00:00
|
|
|
break;
|
2009-02-19 15:27:44 +00:00
|
|
|
|
2011-10-17 09:29:37 +00:00
|
|
|
case DYNAMIC_LOOKUP:
|
|
|
|
// The variable could not be resolved statically.
|
2014-06-24 14:03:24 +00:00
|
|
|
var = NonLocal(proxy->raw_name(), DYNAMIC);
|
2011-10-17 09:29:37 +00:00
|
|
|
break;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var != NULL);
|
2014-06-26 11:59:42 +00:00
|
|
|
if (proxy->is_assigned()) var->set_maybe_assigned();
|
2012-03-08 13:03:07 +00:00
|
|
|
|
2014-03-11 14:41:22 +00:00
|
|
|
if (FLAG_harmony_scoping && strict_mode() == STRICT &&
|
2014-06-26 11:59:42 +00:00
|
|
|
var->is_const_mode() && proxy->is_assigned()) {
|
2012-12-18 12:00:50 +00:00
|
|
|
// Assignment to const. Throw a syntax error.
|
|
|
|
MessageLocation location(
|
|
|
|
info->script(), proxy->position(), proxy->position());
|
2013-09-03 11:54:08 +00:00
|
|
|
Isolate* isolate = info->isolate();
|
2012-12-18 12:00:50 +00:00
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Handle<JSArray> array = factory->NewJSArray(0);
|
2014-09-01 09:11:44 +00:00
|
|
|
Handle<Object> error;
|
|
|
|
MaybeHandle<Object> maybe_error =
|
2012-12-18 12:00:50 +00:00
|
|
|
factory->NewSyntaxError("harmony_const_assign", array);
|
2014-09-01 09:11:44 +00:00
|
|
|
if (maybe_error.ToHandle(&error)) isolate->Throw(*error, &location);
|
2012-12-18 12:00:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-08 13:03:07 +00:00
|
|
|
if (FLAG_harmony_modules) {
|
|
|
|
bool ok;
|
|
|
|
#ifdef DEBUG
|
2014-06-24 14:03:24 +00:00
|
|
|
if (FLAG_print_interface_details) {
|
|
|
|
PrintF("# Resolve %.*s:\n", var->raw_name()->length(),
|
|
|
|
var->raw_name()->raw_data());
|
|
|
|
}
|
2012-03-08 13:03:07 +00:00
|
|
|
#endif
|
2012-06-11 12:42:31 +00:00
|
|
|
proxy->interface()->Unify(var->interface(), zone(), &ok);
|
2012-03-08 13:03:07 +00:00
|
|
|
if (!ok) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (FLAG_print_interfaces) {
|
|
|
|
PrintF("SCOPES TYPE ERROR\n");
|
|
|
|
PrintF("proxy: ");
|
|
|
|
proxy->interface()->Print();
|
|
|
|
PrintF("var: ");
|
|
|
|
var->interface()->Print();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Inconsistent use of module. Throw a syntax error.
|
|
|
|
// TODO(rossberg): generate more helpful error message.
|
2012-12-18 12:00:50 +00:00
|
|
|
MessageLocation location(
|
|
|
|
info->script(), proxy->position(), proxy->position());
|
2013-09-03 11:54:08 +00:00
|
|
|
Isolate* isolate = info->isolate();
|
2012-03-08 13:03:07 +00:00
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
Handle<JSArray> array = factory->NewJSArray(1);
|
2014-04-08 07:04:13 +00:00
|
|
|
JSObject::SetElement(array, 0, var->name(), NONE, STRICT).Assert();
|
2014-09-01 09:11:44 +00:00
|
|
|
Handle<Object> error;
|
|
|
|
MaybeHandle<Object> maybe_error =
|
2012-03-08 13:03:07 +00:00
|
|
|
factory->NewSyntaxError("module_type_error", array);
|
2014-09-01 09:11:44 +00:00
|
|
|
if (maybe_error.ToHandle(&error)) isolate->Throw(*error, &location);
|
2012-03-08 13:03:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
proxy->BindTo(var);
|
|
|
|
|
2012-03-08 13:03:07 +00:00
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-08 13:03:07 +00:00
|
|
|
bool Scope::ResolveVariablesRecursively(
|
|
|
|
CompilationInfo* info,
|
2012-02-08 09:56:33 +00:00
|
|
|
AstNodeFactory<AstNullVisitor>* factory) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(info->global_scope()->is_global_scope());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Resolve unresolved variables for this scope.
|
|
|
|
for (int i = 0; i < unresolved_.length(); i++) {
|
2012-03-08 13:03:07 +00:00
|
|
|
if (!ResolveVariable(info, unresolved_[i], factory)) return false;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve unresolved variables for inner scopes.
|
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
2012-03-08 13:03:07 +00:00
|
|
|
if (!inner_scopes_[i]->ResolveVariablesRecursively(info, factory))
|
|
|
|
return false;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2012-03-08 13:03:07 +00:00
|
|
|
|
|
|
|
return true;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-26 11:59:42 +00:00
|
|
|
void Scope::PropagateScopeInfo(bool outer_scope_calls_sloppy_eval ) {
|
2014-03-11 14:39:08 +00:00
|
|
|
if (outer_scope_calls_sloppy_eval) {
|
|
|
|
outer_scope_calls_sloppy_eval_ = true;
|
2011-05-11 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 14:39:08 +00:00
|
|
|
bool calls_sloppy_eval =
|
|
|
|
this->calls_sloppy_eval() || outer_scope_calls_sloppy_eval_;
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
2014-06-26 11:59:42 +00:00
|
|
|
Scope* inner = inner_scopes_[i];
|
|
|
|
inner->PropagateScopeInfo(calls_sloppy_eval);
|
|
|
|
if (inner->scope_calls_eval_ || inner->inner_scope_calls_eval_) {
|
2008-07-03 15:10:15 +00:00
|
|
|
inner_scope_calls_eval_ = true;
|
|
|
|
}
|
2014-06-26 11:59:42 +00:00
|
|
|
if (inner->force_eager_compilation_) {
|
2008-07-03 15:10:15 +00:00
|
|
|
force_eager_compilation_ = true;
|
|
|
|
}
|
2014-09-19 12:50:50 +00:00
|
|
|
if (asm_module_ && inner->scope_type() == FUNCTION_SCOPE) {
|
|
|
|
inner->asm_function_ = true;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Scope::MustAllocate(Variable* var) {
|
2009-07-07 09:48:54 +00:00
|
|
|
// Give var a read/write use if there is a chance it might be accessed
|
|
|
|
// via an eval() call. This is only possible if the variable has a
|
|
|
|
// visible name.
|
2014-06-24 14:03:24 +00:00
|
|
|
if ((var->is_this() || !var->raw_name()->IsEmpty()) &&
|
2011-11-03 14:33:46 +00:00
|
|
|
(var->has_forced_context_allocation() ||
|
2011-06-30 14:37:55 +00:00
|
|
|
scope_calls_eval_ ||
|
|
|
|
inner_scope_calls_eval_ ||
|
|
|
|
scope_contains_with_ ||
|
2011-08-11 16:29:28 +00:00
|
|
|
is_catch_scope() ||
|
2012-07-09 08:59:03 +00:00
|
|
|
is_block_scope() ||
|
2012-08-28 11:25:08 +00:00
|
|
|
is_module_scope() ||
|
|
|
|
is_global_scope())) {
|
2014-06-26 11:59:42 +00:00
|
|
|
var->set_is_used();
|
|
|
|
if (scope_calls_eval_ || inner_scope_calls_eval_) var->set_maybe_assigned();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-07-07 09:48:54 +00:00
|
|
|
// Global variables do not need to be allocated.
|
2012-08-28 11:25:08 +00:00
|
|
|
return !var->IsGlobalObjectProperty() && var->is_used();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Scope::MustAllocateInContext(Variable* var) {
|
2011-06-30 14:37:55 +00:00
|
|
|
// If var is accessed from an inner scope, or if there is a possibility
|
|
|
|
// that it might be accessed from the current or an inner scope (through
|
|
|
|
// an eval() call or a runtime with lookup), it must be allocated in the
|
2009-07-07 09:48:54 +00:00
|
|
|
// context.
|
2011-06-30 14:37:55 +00:00
|
|
|
//
|
2013-04-05 13:19:31 +00:00
|
|
|
// Exceptions: If the scope as a whole has forced context allocation, all
|
|
|
|
// variables will have context allocation, even temporaries. Otherwise
|
|
|
|
// temporary variables are always stack-allocated. Catch-bound variables are
|
|
|
|
// always context-allocated.
|
|
|
|
if (has_forced_context_allocation()) return true;
|
2011-10-11 08:41:19 +00:00
|
|
|
if (var->mode() == TEMPORARY) return false;
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
if (var->mode() == INTERNAL) return true;
|
2012-04-16 14:43:27 +00:00
|
|
|
if (is_catch_scope() || is_block_scope() || is_module_scope()) return true;
|
2012-08-29 09:19:53 +00:00
|
|
|
if (is_global_scope() && IsLexicalVariableMode(var->mode())) return true;
|
2011-11-03 14:33:46 +00:00
|
|
|
return var->has_forced_context_allocation() ||
|
2011-06-30 14:37:55 +00:00
|
|
|
scope_calls_eval_ ||
|
|
|
|
inner_scope_calls_eval_ ||
|
2012-08-28 11:25:08 +00:00
|
|
|
scope_contains_with_;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Scope::HasArgumentsParameter() {
|
|
|
|
for (int i = 0; i < params_.length(); i++) {
|
2011-07-15 16:57:35 +00:00
|
|
|
if (params_[i]->name().is_identical_to(
|
2013-02-28 17:03:34 +00:00
|
|
|
isolate_->factory()->arguments_string())) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return true;
|
2011-07-15 16:57:35 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AllocateStackSlot(Variable* var) {
|
2011-09-07 11:02:31 +00:00
|
|
|
var->AllocateTo(Variable::LOCAL, num_stack_slots_++);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AllocateHeapSlot(Variable* var) {
|
2011-09-07 11:02:31 +00:00
|
|
|
var->AllocateTo(Variable::CONTEXT, num_heap_slots_++);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AllocateParameterLocals() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_function_scope());
|
2014-06-24 14:03:24 +00:00
|
|
|
Variable* arguments = LookupLocal(ast_value_factory_->arguments_string());
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(arguments != NULL); // functions have 'arguments' declared implicitly
|
2011-03-07 19:23:46 +00:00
|
|
|
|
2014-03-11 14:39:08 +00:00
|
|
|
bool uses_sloppy_arguments = false;
|
2011-03-07 19:23:46 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (MustAllocate(arguments) && !HasArgumentsParameter()) {
|
|
|
|
// 'arguments' is used. Unless there is also a parameter called
|
2011-06-16 14:12:58 +00:00
|
|
|
// 'arguments', we must be conservative and allocate all parameters to
|
|
|
|
// the context assuming they will be captured by the arguments object.
|
|
|
|
// If we have a parameter named 'arguments', a (new) value is always
|
|
|
|
// assigned to it via the function invocation. Then 'arguments' denotes
|
|
|
|
// that specific parameter value and cannot be used to access the
|
|
|
|
// parameters, which is why we don't need to allocate an arguments
|
|
|
|
// object in that case.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// We are using 'arguments'. Tell the code generator that is needs to
|
|
|
|
// allocate the arguments object by setting 'arguments_'.
|
2010-09-24 07:53:59 +00:00
|
|
|
arguments_ = arguments;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-07 19:23:46 +00:00
|
|
|
// In strict mode 'arguments' does not alias formal parameters.
|
|
|
|
// Therefore in strict mode we allocate parameters as if 'arguments'
|
|
|
|
// were not used.
|
2014-03-11 14:41:22 +00:00
|
|
|
uses_sloppy_arguments = strict_mode() == SLOPPY;
|
2011-06-16 14:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The same parameter may occur multiple times in the parameters_ list.
|
|
|
|
// If it does, and if it is not copied into the context object, it must
|
|
|
|
// receive the highest parameter index for that parameter; thus iteration
|
|
|
|
// order is relevant!
|
|
|
|
for (int i = params_.length() - 1; i >= 0; --i) {
|
|
|
|
Variable* var = params_[i];
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var->scope() == this);
|
2014-04-22 11:34:16 +00:00
|
|
|
if (uses_sloppy_arguments || has_forced_context_allocation()) {
|
2011-11-03 14:33:46 +00:00
|
|
|
// Force context allocation of the parameter.
|
|
|
|
var->ForceContextAllocation();
|
2011-06-16 06:37:49 +00:00
|
|
|
}
|
|
|
|
|
2011-06-16 14:12:58 +00:00
|
|
|
if (MustAllocate(var)) {
|
|
|
|
if (MustAllocateInContext(var)) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var->IsUnallocated() || var->IsContextSlot());
|
2011-09-07 11:02:31 +00:00
|
|
|
if (var->IsUnallocated()) {
|
2011-06-16 14:12:58 +00:00
|
|
|
AllocateHeapSlot(var);
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var->IsUnallocated() || var->IsParameter());
|
2011-09-07 11:02:31 +00:00
|
|
|
if (var->IsUnallocated()) {
|
|
|
|
var->AllocateTo(Variable::PARAMETER, i);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AllocateNonParameterLocal(Variable* var) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(var->scope() == this);
|
|
|
|
DCHECK(!var->IsVariable(isolate_->factory()->dot_result_string()) ||
|
2011-09-07 11:02:31 +00:00
|
|
|
!var->IsStackLocal());
|
|
|
|
if (var->IsUnallocated() && MustAllocate(var)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (MustAllocateInContext(var)) {
|
|
|
|
AllocateHeapSlot(var);
|
|
|
|
} else {
|
|
|
|
AllocateStackSlot(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AllocateNonParameterLocals() {
|
2009-07-07 09:48:54 +00:00
|
|
|
// All variables that have no rewrite yet are non-parameter locals.
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < temps_.length(); i++) {
|
|
|
|
AllocateNonParameterLocal(temps_[i]);
|
|
|
|
}
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
for (int i = 0; i < internals_.length(); i++) {
|
|
|
|
AllocateNonParameterLocal(internals_[i]);
|
|
|
|
}
|
2012-08-23 08:15:38 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
ZoneList<VarAndOrder> vars(variables_.occupancy(), zone());
|
2009-07-07 09:48:54 +00:00
|
|
|
for (VariableMap::Entry* p = variables_.Start();
|
|
|
|
p != NULL;
|
|
|
|
p = variables_.Next(p)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Variable* var = reinterpret_cast<Variable*>(p->value);
|
2012-08-23 08:15:38 +00:00
|
|
|
vars.Add(VarAndOrder(var, p->order), zone());
|
|
|
|
}
|
|
|
|
vars.Sort(VarAndOrder::Compare);
|
|
|
|
int var_count = vars.length();
|
|
|
|
for (int i = 0; i < var_count; i++) {
|
|
|
|
AllocateNonParameterLocal(vars[i].var());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-07-07 09:48:54 +00:00
|
|
|
// For now, function_ must be allocated at the very end. If it gets
|
|
|
|
// allocated in the context, it must be the last slot in the context,
|
|
|
|
// because of the current ScopeInfo implementation (see
|
2008-07-03 15:10:15 +00:00
|
|
|
// ScopeInfo::ScopeInfo(FunctionScope* scope) constructor).
|
|
|
|
if (function_ != NULL) {
|
2012-04-16 11:48:20 +00:00
|
|
|
AllocateNonParameterLocal(function_->proxy()->var());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Scope::AllocateVariablesRecursively() {
|
|
|
|
// Allocate variables for inner scopes.
|
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
|
|
|
inner_scopes_[i]->AllocateVariablesRecursively();
|
|
|
|
}
|
|
|
|
|
2011-01-17 08:11:03 +00:00
|
|
|
// If scope is already resolved, we still need to allocate
|
|
|
|
// variables in inner scopes which might not had been resolved yet.
|
2011-07-01 14:05:46 +00:00
|
|
|
if (already_resolved()) return;
|
2011-01-17 08:11:03 +00:00
|
|
|
// The number of slots required for variables.
|
|
|
|
num_stack_slots_ = 0;
|
|
|
|
num_heap_slots_ = Context::MIN_CONTEXT_SLOTS;
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Allocate variables for this scope.
|
|
|
|
// Parameters must be allocated first, if any.
|
|
|
|
if (is_function_scope()) AllocateParameterLocals();
|
|
|
|
AllocateNonParameterLocals();
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
// Force allocation of a context for this scope if necessary. For a 'with'
|
|
|
|
// scope and for a function scope that makes an 'eval' call we need a context,
|
|
|
|
// even if no local variables were statically allocated in the scope.
|
2012-04-16 14:43:27 +00:00
|
|
|
// Likewise for modules.
|
|
|
|
bool must_have_context = is_with_scope() || is_module_scope() ||
|
2011-11-03 10:36:55 +00:00
|
|
|
(is_function_scope() && calls_eval());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// If we didn't allocate any locals in the local context, then we only
|
2011-11-03 10:36:55 +00:00
|
|
|
// need the minimal number of slots if we must have a context.
|
|
|
|
if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) {
|
2008-07-03 15:10:15 +00:00
|
|
|
num_heap_slots_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocation done.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 14:50:19 +00:00
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
void Scope::AllocateModulesRecursively(Scope* host_scope) {
|
|
|
|
if (already_resolved()) return;
|
2012-07-09 08:59:03 +00:00
|
|
|
if (is_module_scope()) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(interface_->IsFrozen());
|
|
|
|
DCHECK(module_var_ == NULL);
|
2014-06-24 14:03:24 +00:00
|
|
|
module_var_ =
|
|
|
|
host_scope->NewInternal(ast_value_factory_->dot_module_string());
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
++host_scope->num_modules_;
|
2012-07-09 08:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < inner_scopes_.length(); i++) {
|
|
|
|
Scope* inner_scope = inner_scopes_.at(i);
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
inner_scope->AllocateModulesRecursively(host_scope);
|
2012-07-09 08:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
int Scope::StackLocalCount() const {
|
|
|
|
return num_stack_slots() -
|
|
|
|
(function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0);
|
|
|
|
}
|
2012-07-09 08:59:03 +00:00
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
int Scope::ContextLocalCount() const {
|
|
|
|
if (num_heap_slots() == 0) return 0;
|
|
|
|
return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
|
|
|
|
(function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0);
|
2012-07-09 08:59:03 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|