2011-05-11 11:26:11 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "scopeinfo.h"
|
|
|
|
#include "scopes.h"
|
|
|
|
|
2011-07-12 08:03:19 +00:00
|
|
|
#include "allocation-inl.h"
|
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
Handle<ScopeInfo> ScopeInfo::Create(Scope* scope, Zone* zone) {
|
2011-11-03 10:36:55 +00:00
|
|
|
// Collect stack and context locals.
|
2012-06-11 12:42:31 +00:00
|
|
|
ZoneList<Variable*> stack_locals(scope->StackLocalCount(), zone);
|
|
|
|
ZoneList<Variable*> context_locals(scope->ContextLocalCount(), zone);
|
2011-11-03 14:50:19 +00:00
|
|
|
scope->CollectStackAndContextLocals(&stack_locals, &context_locals);
|
|
|
|
const int stack_local_count = stack_locals.length();
|
|
|
|
const int context_local_count = context_locals.length();
|
|
|
|
// Make sure we allocate the correct amount.
|
|
|
|
ASSERT(scope->StackLocalCount() == stack_local_count);
|
|
|
|
ASSERT(scope->ContextLocalCount() == context_local_count);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
// Determine use and location of the function variable if it is present.
|
|
|
|
FunctionVariableInfo function_name_info;
|
|
|
|
VariableMode function_variable_mode;
|
|
|
|
if (scope->is_function_scope() && scope->function() != NULL) {
|
2012-04-16 11:48:20 +00:00
|
|
|
Variable* var = scope->function()->proxy()->var();
|
2011-11-03 10:36:55 +00:00
|
|
|
if (!var->is_used()) {
|
|
|
|
function_name_info = UNUSED;
|
|
|
|
} else if (var->IsContextSlot()) {
|
|
|
|
function_name_info = CONTEXT;
|
|
|
|
} else {
|
|
|
|
ASSERT(var->IsStackLocal());
|
|
|
|
function_name_info = STACK;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-11-03 10:36:55 +00:00
|
|
|
function_variable_mode = var->mode();
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2011-11-03 10:36:55 +00:00
|
|
|
function_name_info = NONE;
|
|
|
|
function_variable_mode = VAR;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
const bool has_function_name = function_name_info != NONE;
|
|
|
|
const int parameter_count = scope->num_parameters();
|
|
|
|
const int length = kVariablePartIndex
|
|
|
|
+ parameter_count + stack_local_count + 2 * context_local_count
|
|
|
|
+ (has_function_name ? 2 : 0);
|
|
|
|
|
|
|
|
Handle<ScopeInfo> scope_info = FACTORY->NewScopeInfo(length);
|
|
|
|
|
|
|
|
// Encode the flags.
|
|
|
|
int flags = TypeField::encode(scope->type()) |
|
|
|
|
CallsEvalField::encode(scope->calls_eval()) |
|
2011-11-24 15:17:04 +00:00
|
|
|
LanguageModeField::encode(scope->language_mode()) |
|
2011-11-03 10:36:55 +00:00
|
|
|
FunctionVariableField::encode(function_name_info) |
|
|
|
|
FunctionVariableMode::encode(function_variable_mode);
|
|
|
|
scope_info->SetFlags(flags);
|
|
|
|
scope_info->SetParameterCount(parameter_count);
|
|
|
|
scope_info->SetStackLocalCount(stack_local_count);
|
|
|
|
scope_info->SetContextLocalCount(context_local_count);
|
|
|
|
|
|
|
|
int index = kVariablePartIndex;
|
|
|
|
// Add parameters.
|
|
|
|
ASSERT(index == scope_info->ParameterEntriesIndex());
|
|
|
|
for (int i = 0; i < parameter_count; ++i) {
|
|
|
|
scope_info->set(index++, *scope->parameter(i)->name());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
// Add stack locals' names. We are assuming that the stack locals'
|
|
|
|
// slots are allocated in increasing order, so we can simply add
|
|
|
|
// them to the ScopeInfo object.
|
|
|
|
ASSERT(index == scope_info->StackLocalEntriesIndex());
|
|
|
|
for (int i = 0; i < stack_local_count; ++i) {
|
|
|
|
ASSERT(stack_locals[i]->index() == i);
|
|
|
|
scope_info->set(index++, *stack_locals[i]->name());
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
// Due to usage analysis, context-allocated locals are not necessarily in
|
|
|
|
// increasing order: Some of them may be parameters which are allocated before
|
|
|
|
// the non-parameter locals. When the non-parameter locals are sorted
|
|
|
|
// according to usage, the allocated slot indices may not be in increasing
|
|
|
|
// order with the variable list anymore. Thus, we first need to sort them by
|
|
|
|
// context slot index before adding them to the ScopeInfo object.
|
2011-11-03 14:50:19 +00:00
|
|
|
context_locals.Sort(&Variable::CompareIndex);
|
2011-11-03 10:36:55 +00:00
|
|
|
|
|
|
|
// Add context locals' names.
|
|
|
|
ASSERT(index == scope_info->ContextLocalNameEntriesIndex());
|
|
|
|
for (int i = 0; i < context_local_count; ++i) {
|
|
|
|
scope_info->set(index++, *context_locals[i]->name());
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-03 11:59:51 +00:00
|
|
|
// Add context locals' info.
|
|
|
|
ASSERT(index == scope_info->ContextLocalInfoEntriesIndex());
|
2011-11-03 10:36:55 +00:00
|
|
|
for (int i = 0; i < context_local_count; ++i) {
|
2011-11-03 11:59:51 +00:00
|
|
|
Variable* var = context_locals[i];
|
|
|
|
uint32_t value = ContextLocalMode::encode(var->mode()) |
|
|
|
|
ContextLocalInitFlag::encode(var->initialization_flag());
|
|
|
|
scope_info->set(index++, Smi::FromInt(value));
|
2011-11-03 10:36:55 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
// If present, add the function variable name and its index.
|
|
|
|
ASSERT(index == scope_info->FunctionNameEntryIndex());
|
|
|
|
if (has_function_name) {
|
2012-04-16 11:48:20 +00:00
|
|
|
int var_index = scope->function()->proxy()->var()->index();
|
|
|
|
scope_info->set(index++, *scope->function()->proxy()->name());
|
2011-11-03 10:36:55 +00:00
|
|
|
scope_info->set(index++, Smi::FromInt(var_index));
|
|
|
|
ASSERT(function_name_info != STACK ||
|
|
|
|
(var_index == scope_info->StackLocalCount() &&
|
|
|
|
var_index == scope_info->StackSlotCount() - 1));
|
|
|
|
ASSERT(function_name_info != CONTEXT ||
|
|
|
|
var_index == scope_info->ContextLength() - 1);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
ASSERT(index == scope_info->length());
|
|
|
|
ASSERT(scope->num_parameters() == scope_info->ParameterCount());
|
|
|
|
ASSERT(scope->num_stack_slots() == scope_info->StackSlotCount());
|
2012-04-16 14:43:27 +00:00
|
|
|
ASSERT(scope->num_heap_slots() == scope_info->ContextLength() ||
|
|
|
|
(scope->num_heap_slots() == kVariablePartIndex &&
|
|
|
|
scope_info->ContextLength() == 0));
|
2011-11-03 10:36:55 +00:00
|
|
|
return scope_info;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-27 14:45:59 +00:00
|
|
|
ScopeInfo* ScopeInfo::Empty(Isolate* isolate) {
|
|
|
|
return reinterpret_cast<ScopeInfo*>(isolate->heap()->empty_fixed_array());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
ScopeType ScopeInfo::Type() {
|
|
|
|
ASSERT(length() > 0);
|
|
|
|
return TypeField::decode(Flags());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
bool ScopeInfo::CallsEval() {
|
|
|
|
return length() > 0 && CallsEvalField::decode(Flags());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-24 15:17:04 +00:00
|
|
|
LanguageMode ScopeInfo::language_mode() {
|
|
|
|
return length() > 0 ? LanguageModeField::decode(Flags()) : CLASSIC_MODE;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::LocalCount() {
|
|
|
|
return StackLocalCount() + ContextLocalCount();
|
2009-02-24 13:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::StackSlotCount() {
|
|
|
|
if (length() > 0) {
|
|
|
|
bool function_name_stack_slot =
|
|
|
|
FunctionVariableField::decode(Flags()) == STACK;
|
|
|
|
return StackLocalCount() + (function_name_stack_slot ? 1 : 0);
|
|
|
|
}
|
|
|
|
return 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::ContextLength() {
|
|
|
|
if (length() > 0) {
|
|
|
|
int context_locals = ContextLocalCount();
|
|
|
|
bool function_name_context_slot =
|
|
|
|
FunctionVariableField::decode(Flags()) == CONTEXT;
|
|
|
|
bool has_context = context_locals > 0 ||
|
|
|
|
function_name_context_slot ||
|
|
|
|
Type() == WITH_SCOPE ||
|
2012-07-09 08:59:03 +00:00
|
|
|
(Type() == FUNCTION_SCOPE && CallsEval()) ||
|
|
|
|
Type() == MODULE_SCOPE;
|
2011-11-03 10:36:55 +00:00
|
|
|
if (has_context) {
|
|
|
|
return Context::MIN_CONTEXT_SLOTS + context_locals +
|
|
|
|
(function_name_context_slot ? 1 : 0);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2011-11-03 10:36:55 +00:00
|
|
|
return 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
bool ScopeInfo::HasFunctionName() {
|
|
|
|
if (length() > 0) {
|
|
|
|
return NONE != FunctionVariableField::decode(Flags());
|
2010-07-14 11:18:09 +00:00
|
|
|
} else {
|
2011-11-03 10:36:55 +00:00
|
|
|
return false;
|
2010-07-14 11:18:09 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
bool ScopeInfo::HasHeapAllocatedLocals() {
|
|
|
|
if (length() > 0) {
|
|
|
|
return ContextLocalCount() > 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
2010-07-14 11:18:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
bool ScopeInfo::HasContext() {
|
2012-07-09 08:59:03 +00:00
|
|
|
return ContextLength() > 0;
|
2011-05-11 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
String* ScopeInfo::FunctionName() {
|
|
|
|
ASSERT(HasFunctionName());
|
|
|
|
return String::cast(get(FunctionNameEntryIndex()));
|
2009-02-24 13:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
String* ScopeInfo::ParameterName(int var) {
|
|
|
|
ASSERT(0 <= var && var < ParameterCount());
|
|
|
|
int info_index = ParameterEntriesIndex() + var;
|
|
|
|
return String::cast(get(info_index));
|
2011-10-21 10:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
String* ScopeInfo::LocalName(int var) {
|
|
|
|
ASSERT(0 <= var && var < LocalCount());
|
|
|
|
ASSERT(StackLocalEntriesIndex() + StackLocalCount() ==
|
|
|
|
ContextLocalNameEntriesIndex());
|
|
|
|
int info_index = StackLocalEntriesIndex() + var;
|
|
|
|
return String::cast(get(info_index));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
String* ScopeInfo::StackLocalName(int var) {
|
|
|
|
ASSERT(0 <= var && var < StackLocalCount());
|
|
|
|
int info_index = StackLocalEntriesIndex() + var;
|
|
|
|
return String::cast(get(info_index));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
String* ScopeInfo::ContextLocalName(int var) {
|
|
|
|
ASSERT(0 <= var && var < ContextLocalCount());
|
|
|
|
int info_index = ContextLocalNameEntriesIndex() + var;
|
|
|
|
return String::cast(get(info_index));
|
2010-06-23 07:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
VariableMode ScopeInfo::ContextLocalMode(int var) {
|
|
|
|
ASSERT(0 <= var && var < ContextLocalCount());
|
2011-11-03 11:59:51 +00:00
|
|
|
int info_index = ContextLocalInfoEntriesIndex() + var;
|
|
|
|
int value = Smi::cast(get(info_index))->value();
|
|
|
|
return ContextLocalMode::decode(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InitializationFlag ScopeInfo::ContextLocalInitFlag(int var) {
|
|
|
|
ASSERT(0 <= var && var < ContextLocalCount());
|
|
|
|
int info_index = ContextLocalInfoEntriesIndex() + var;
|
|
|
|
int value = Smi::cast(get(info_index))->value();
|
|
|
|
return ContextLocalInitFlag::decode(value);
|
2011-10-21 10:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::StackSlotIndex(String* name) {
|
2013-02-28 17:03:34 +00:00
|
|
|
ASSERT(name->IsInternalizedString());
|
2010-07-14 11:18:09 +00:00
|
|
|
if (length() > 0) {
|
2011-11-03 10:36:55 +00:00
|
|
|
int start = StackLocalEntriesIndex();
|
|
|
|
int end = StackLocalEntriesIndex() + StackLocalCount();
|
|
|
|
for (int i = start; i < end; ++i) {
|
|
|
|
if (name == get(i)) {
|
|
|
|
return i - start;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
|
2011-11-03 11:59:51 +00:00
|
|
|
int ScopeInfo::ContextSlotIndex(String* name,
|
|
|
|
VariableMode* mode,
|
|
|
|
InitializationFlag* init_flag) {
|
2013-02-28 17:03:34 +00:00
|
|
|
ASSERT(name->IsInternalizedString());
|
2011-11-03 10:36:55 +00:00
|
|
|
ASSERT(mode != NULL);
|
2011-11-03 11:59:51 +00:00
|
|
|
ASSERT(init_flag != NULL);
|
2010-07-14 11:18:09 +00:00
|
|
|
if (length() > 0) {
|
2011-11-03 10:36:55 +00:00
|
|
|
ContextSlotCache* context_slot_cache = GetIsolate()->context_slot_cache();
|
2011-11-03 11:59:51 +00:00
|
|
|
int result = context_slot_cache->Lookup(this, name, mode, init_flag);
|
2011-11-03 10:36:55 +00:00
|
|
|
if (result != ContextSlotCache::kNotFound) {
|
|
|
|
ASSERT(result < ContextLength());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int start = ContextLocalNameEntriesIndex();
|
|
|
|
int end = ContextLocalNameEntriesIndex() + ContextLocalCount();
|
|
|
|
for (int i = start; i < end; ++i) {
|
|
|
|
if (name == get(i)) {
|
|
|
|
int var = i - start;
|
|
|
|
*mode = ContextLocalMode(var);
|
2011-11-03 11:59:51 +00:00
|
|
|
*init_flag = ContextLocalInitFlag(var);
|
2011-11-03 10:36:55 +00:00
|
|
|
result = Context::MIN_CONTEXT_SLOTS + var;
|
2011-11-03 11:59:51 +00:00
|
|
|
context_slot_cache->Update(this, name, *mode, *init_flag, result);
|
2011-11-03 10:36:55 +00:00
|
|
|
ASSERT(result < ContextLength());
|
2009-06-22 08:09:57 +00:00
|
|
|
return result;
|
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
|
|
|
// Cache as not found. Mode and init flag don't matter.
|
2011-11-03 11:59:51 +00:00
|
|
|
context_slot_cache->Update(this, name, INTERNAL, kNeedsInitialization, -1);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::ParameterIndex(String* name) {
|
2013-02-28 17:03:34 +00:00
|
|
|
ASSERT(name->IsInternalizedString());
|
2010-07-14 11:18:09 +00:00
|
|
|
if (length() > 0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// We must read parameters from the end since for
|
|
|
|
// multiply declared parameters the value of the
|
|
|
|
// last declaration of that parameter is used
|
|
|
|
// inside a function (and thus we need to look
|
|
|
|
// at the last index). Was bug# 1110337.
|
2011-11-03 10:36:55 +00:00
|
|
|
int start = ParameterEntriesIndex();
|
|
|
|
int end = ParameterEntriesIndex() + ParameterCount();
|
|
|
|
for (int i = end - 1; i >= start; --i) {
|
|
|
|
if (name == get(i)) {
|
|
|
|
return i - start;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::FunctionContextSlotIndex(String* name, VariableMode* mode) {
|
2013-02-28 17:03:34 +00:00
|
|
|
ASSERT(name->IsInternalizedString());
|
2011-11-03 10:36:55 +00:00
|
|
|
ASSERT(mode != NULL);
|
2010-07-14 11:18:09 +00:00
|
|
|
if (length() > 0) {
|
2011-11-03 10:36:55 +00:00
|
|
|
if (FunctionVariableField::decode(Flags()) == CONTEXT &&
|
|
|
|
FunctionName() == name) {
|
|
|
|
*mode = FunctionVariableMode::decode(Flags());
|
|
|
|
return Smi::cast(get(FunctionNameEntryIndex() + 1))->value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-25 16:46:54 +00:00
|
|
|
bool ScopeInfo::CopyContextLocalsToScopeObject(
|
|
|
|
Isolate* isolate,
|
|
|
|
Handle<Context> context,
|
|
|
|
Handle<JSObject> scope_object) {
|
|
|
|
int local_count = ContextLocalCount();
|
|
|
|
if (local_count == 0) return true;
|
|
|
|
// Fill all context locals to the context extension.
|
|
|
|
int start = ContextLocalNameEntriesIndex();
|
|
|
|
int end = start + local_count;
|
|
|
|
for (int i = start; i < end; ++i) {
|
|
|
|
int context_index = Context::MIN_CONTEXT_SLOTS + i - start;
|
|
|
|
RETURN_IF_EMPTY_HANDLE_VALUE(
|
|
|
|
isolate,
|
|
|
|
SetProperty(isolate,
|
|
|
|
scope_object,
|
|
|
|
Handle<String>(String::cast(get(i))),
|
|
|
|
Handle<Object>(context->get(context_index), isolate),
|
|
|
|
::NONE,
|
|
|
|
kNonStrictMode),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
int ScopeInfo::ParameterEntriesIndex() {
|
|
|
|
ASSERT(length() > 0);
|
|
|
|
return kVariablePartIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ScopeInfo::StackLocalEntriesIndex() {
|
|
|
|
return ParameterEntriesIndex() + ParameterCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ScopeInfo::ContextLocalNameEntriesIndex() {
|
|
|
|
return StackLocalEntriesIndex() + StackLocalCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 11:59:51 +00:00
|
|
|
int ScopeInfo::ContextLocalInfoEntriesIndex() {
|
2011-11-03 10:36:55 +00:00
|
|
|
return ContextLocalNameEntriesIndex() + ContextLocalCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ScopeInfo::FunctionNameEntryIndex() {
|
2011-11-03 11:59:51 +00:00
|
|
|
return ContextLocalInfoEntriesIndex() + ContextLocalCount();
|
2011-11-03 10:36:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 13:06:33 +00:00
|
|
|
int ContextSlotCache::Hash(Object* data, String* name) {
|
2009-06-22 08:09:57 +00:00
|
|
|
// Uses only lower 32 bits if pointers are larger.
|
|
|
|
uintptr_t addr_hash =
|
2010-07-13 13:06:33 +00:00
|
|
|
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(data)) >> 2;
|
2010-02-22 11:42:46 +00:00
|
|
|
return static_cast<int>((addr_hash ^ name->Hash()) % kLength);
|
2009-06-22 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 13:06:33 +00:00
|
|
|
int ContextSlotCache::Lookup(Object* data,
|
2009-06-22 08:09:57 +00:00
|
|
|
String* name,
|
2011-11-03 11:59:51 +00:00
|
|
|
VariableMode* mode,
|
|
|
|
InitializationFlag* init_flag) {
|
2010-07-13 13:06:33 +00:00
|
|
|
int index = Hash(data, name);
|
2009-06-22 08:09:57 +00:00
|
|
|
Key& key = keys_[index];
|
2010-07-13 13:06:33 +00:00
|
|
|
if ((key.data == data) && key.name->Equals(name)) {
|
2009-06-22 08:09:57 +00:00
|
|
|
Value result(values_[index]);
|
|
|
|
if (mode != NULL) *mode = result.mode();
|
2011-11-03 11:59:51 +00:00
|
|
|
if (init_flag != NULL) *init_flag = result.initialization_flag();
|
2009-06-22 08:09:57 +00:00
|
|
|
return result.index() + kNotFound;
|
|
|
|
}
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-13 13:06:33 +00:00
|
|
|
void ContextSlotCache::Update(Object* data,
|
2009-06-22 08:09:57 +00:00
|
|
|
String* name,
|
2011-10-11 08:41:19 +00:00
|
|
|
VariableMode mode,
|
2011-11-03 11:59:51 +00:00
|
|
|
InitializationFlag init_flag,
|
2009-06-22 08:09:57 +00:00
|
|
|
int slot_index) {
|
2013-02-28 17:03:34 +00:00
|
|
|
String* internalized_name;
|
2009-06-22 08:09:57 +00:00
|
|
|
ASSERT(slot_index > kNotFound);
|
2013-02-28 17:03:34 +00:00
|
|
|
if (HEAP->InternalizeStringIfExists(name, &internalized_name)) {
|
|
|
|
int index = Hash(data, internalized_name);
|
2009-06-22 08:09:57 +00:00
|
|
|
Key& key = keys_[index];
|
2010-07-13 13:06:33 +00:00
|
|
|
key.data = data;
|
2013-02-28 17:03:34 +00:00
|
|
|
key.name = internalized_name;
|
2009-06-22 08:09:57 +00:00
|
|
|
// Please note value only takes a uint as index.
|
2011-11-03 11:59:51 +00:00
|
|
|
values_[index] = Value(mode, init_flag, slot_index - kNotFound).raw();
|
2009-06-22 08:09:57 +00:00
|
|
|
#ifdef DEBUG
|
2011-11-03 11:59:51 +00:00
|
|
|
ValidateEntry(data, name, mode, init_flag, slot_index);
|
2009-06-22 08:09:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ContextSlotCache::Clear() {
|
2010-07-13 13:06:33 +00:00
|
|
|
for (int index = 0; index < kLength; index++) keys_[index].data = NULL;
|
2009-06-22 08:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef DEBUG
|
2009-06-22 08:09:57 +00:00
|
|
|
|
2010-07-13 13:06:33 +00:00
|
|
|
void ContextSlotCache::ValidateEntry(Object* data,
|
2009-06-22 08:09:57 +00:00
|
|
|
String* name,
|
2011-10-11 08:41:19 +00:00
|
|
|
VariableMode mode,
|
2011-11-03 11:59:51 +00:00
|
|
|
InitializationFlag init_flag,
|
2009-06-22 08:09:57 +00:00
|
|
|
int slot_index) {
|
2013-02-28 17:03:34 +00:00
|
|
|
String* internalized_name;
|
|
|
|
if (HEAP->InternalizeStringIfExists(name, &internalized_name)) {
|
2010-07-13 13:06:33 +00:00
|
|
|
int index = Hash(data, name);
|
2009-06-22 08:09:57 +00:00
|
|
|
Key& key = keys_[index];
|
2010-07-13 13:06:33 +00:00
|
|
|
ASSERT(key.data == data);
|
2009-06-22 08:09:57 +00:00
|
|
|
ASSERT(key.name->Equals(name));
|
|
|
|
Value result(values_[index]);
|
|
|
|
ASSERT(result.mode() == mode);
|
2011-11-03 11:59:51 +00:00
|
|
|
ASSERT(result.initialization_flag() == init_flag);
|
2009-06-22 08:09:57 +00:00
|
|
|
ASSERT(result.index() + kNotFound == slot_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static void PrintList(const char* list_name,
|
|
|
|
int nof_internal_slots,
|
2011-11-03 10:36:55 +00:00
|
|
|
int start,
|
|
|
|
int end,
|
|
|
|
ScopeInfo* scope_info) {
|
|
|
|
if (start < end) {
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("\n // %s\n", list_name);
|
|
|
|
if (nof_internal_slots > 0) {
|
|
|
|
PrintF(" %2d - %2d [internal slots]\n", 0 , nof_internal_slots - 1);
|
|
|
|
}
|
2011-11-03 10:36:55 +00:00
|
|
|
for (int i = nof_internal_slots; start < end; ++i, ++start) {
|
|
|
|
PrintF(" %2d ", i);
|
|
|
|
String::cast(scope_info->get(start))->ShortPrint();
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
void ScopeInfo::Print() {
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("ScopeInfo ");
|
2011-11-03 10:36:55 +00:00
|
|
|
if (HasFunctionName()) {
|
|
|
|
FunctionName()->ShortPrint();
|
|
|
|
} else {
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("/* no function name */");
|
2011-11-03 10:36:55 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("{");
|
|
|
|
|
2011-11-03 10:36:55 +00:00
|
|
|
PrintList("parameters", 0,
|
|
|
|
ParameterEntriesIndex(),
|
|
|
|
ParameterEntriesIndex() + ParameterCount(),
|
|
|
|
this);
|
|
|
|
PrintList("stack slots", 0,
|
|
|
|
StackLocalEntriesIndex(),
|
|
|
|
StackLocalEntriesIndex() + StackLocalCount(),
|
|
|
|
this);
|
|
|
|
PrintList("context slots",
|
|
|
|
Context::MIN_CONTEXT_SLOTS,
|
|
|
|
ContextLocalNameEntriesIndex(),
|
|
|
|
ContextLocalNameEntriesIndex() + ContextLocalCount(),
|
|
|
|
this);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
PrintF("}\n");
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
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
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// ModuleInfo.
|
|
|
|
|
|
|
|
Handle<ModuleInfo> ModuleInfo::Create(
|
|
|
|
Isolate* isolate, Interface* interface, Scope* scope) {
|
|
|
|
Handle<ModuleInfo> info = Allocate(isolate, interface->Length());
|
|
|
|
info->set_host_index(interface->Index());
|
|
|
|
int i = 0;
|
|
|
|
for (Interface::Iterator it = interface->iterator();
|
|
|
|
!it.done(); it.Advance(), ++i) {
|
|
|
|
Variable* var = scope->LocalLookup(it.name());
|
|
|
|
info->set_name(i, *it.name());
|
|
|
|
info->set_mode(i, var->mode());
|
|
|
|
ASSERT((var->mode() == MODULE) == (it.interface()->IsModule()));
|
|
|
|
if (var->mode() == MODULE) {
|
|
|
|
ASSERT(it.interface()->IsFrozen());
|
|
|
|
ASSERT(it.interface()->Index() >= 0);
|
|
|
|
info->set_index(i, it.interface()->Index());
|
|
|
|
} else {
|
|
|
|
ASSERT(var->index() >= 0);
|
|
|
|
info->set_index(i, var->index());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(i == info->length());
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|