2011-04-07 14:42:37 +00:00
|
|
|
// Copyright 2011 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/ast.h"
|
|
|
|
#include "src/scopes.h"
|
|
|
|
#include "src/variables.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 Variable.
|
|
|
|
|
2011-10-11 08:41:19 +00:00
|
|
|
const char* Variable::Mode2String(VariableMode mode) {
|
2008-07-03 15:10:15 +00:00
|
|
|
switch (mode) {
|
|
|
|
case VAR: return "VAR";
|
2014-03-11 14:41:22 +00:00
|
|
|
case CONST_LEGACY: return "CONST_LEGACY";
|
2011-08-16 14:24:12 +00:00
|
|
|
case LET: return "LET";
|
2014-03-11 14:41:22 +00:00
|
|
|
case CONST: return "CONST";
|
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
|
|
|
case MODULE: return "MODULE";
|
2008-07-03 15:10:15 +00:00
|
|
|
case DYNAMIC: return "DYNAMIC";
|
2009-02-18 15:55:24 +00:00
|
|
|
case DYNAMIC_GLOBAL: return "DYNAMIC_GLOBAL";
|
|
|
|
case DYNAMIC_LOCAL: return "DYNAMIC_LOCAL";
|
2008-07-03 15:10:15 +00:00
|
|
|
case INTERNAL: return "INTERNAL";
|
|
|
|
case TEMPORARY: return "TEMPORARY";
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
Variable::Variable(Scope* scope, const AstRawString* name, VariableMode mode,
|
|
|
|
bool is_valid_ref, Kind kind,
|
2012-03-08 13:03:07 +00:00
|
|
|
InitializationFlag initialization_flag,
|
2014-07-30 13:54:45 +00:00
|
|
|
MaybeAssignedFlag maybe_assigned_flag, Interface* interface)
|
|
|
|
: scope_(scope),
|
|
|
|
name_(name),
|
|
|
|
mode_(mode),
|
|
|
|
kind_(kind),
|
|
|
|
location_(UNALLOCATED),
|
|
|
|
index_(-1),
|
|
|
|
initializer_position_(RelocInfo::kNoPosition),
|
|
|
|
local_if_not_shadowed_(NULL),
|
|
|
|
is_valid_ref_(is_valid_ref),
|
|
|
|
force_context_allocation_(false),
|
|
|
|
is_used_(false),
|
|
|
|
initialization_flag_(initialization_flag),
|
|
|
|
maybe_assigned_(maybe_assigned_flag),
|
|
|
|
interface_(interface) {
|
2011-11-03 11:59:51 +00:00
|
|
|
// Var declared variables never need initialization.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!(mode == VAR && initialization_flag == kNeedsInitialization));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-28 11:25:08 +00:00
|
|
|
bool Variable::IsGlobalObjectProperty() const {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Temporaries are never global, they must always be allocated in the
|
|
|
|
// activation frame.
|
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
|
|
|
return (IsDynamicVariableMode(mode_) ||
|
|
|
|
(IsDeclaredVariableMode(mode_) && !IsLexicalVariableMode(mode_)))
|
2014-11-12 11:34:09 +00:00
|
|
|
&& scope_ != NULL && scope_->is_script_scope();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 14:50:19 +00:00
|
|
|
|
|
|
|
int Variable::CompareIndex(Variable* const* v, Variable* const* w) {
|
|
|
|
int x = (*v)->index();
|
|
|
|
int y = (*w)->index();
|
|
|
|
// Consider sorting them according to type as well?
|
|
|
|
return x - y;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|