[modules] Minor refactorings in scopes and scopeinfos.
R=adamk@chromium.org BUG=v8:1569 Review-Url: https://codereview.chromium.org/2275943005 Cr-Commit-Position: refs/heads/master@{#38931}
This commit is contained in:
parent
a4a4e7fa97
commit
04d8112036
@ -14,13 +14,11 @@ namespace internal {
|
||||
|
||||
Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone,
|
||||
Scope* scope) {
|
||||
// Collect stack and context locals.
|
||||
// Collect variables.
|
||||
ZoneList<Variable*> stack_locals(scope->StackLocalCount(), zone);
|
||||
ZoneList<Variable*> context_locals(scope->ContextLocalCount(), zone);
|
||||
ZoneList<Variable*> context_globals(scope->ContextGlobalCount(), zone);
|
||||
|
||||
scope->CollectStackAndContextLocals(&stack_locals, &context_locals,
|
||||
&context_globals);
|
||||
scope->CollectVariables(&stack_locals, &context_locals, &context_globals);
|
||||
const int stack_local_count = stack_locals.length();
|
||||
const int context_local_count = context_locals.length();
|
||||
const int context_global_count = context_globals.length();
|
||||
@ -148,10 +146,9 @@ Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone,
|
||||
for (int i = 0; i < context_local_count; ++i) {
|
||||
Variable* var = context_locals[i];
|
||||
int context_index = var->index() - Context::MIN_CONTEXT_SLOTS;
|
||||
uint32_t info =
|
||||
ContextLocalMode::encode(var->mode()) |
|
||||
ContextLocalInitFlag::encode(var->initialization_flag()) |
|
||||
ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned());
|
||||
uint32_t info = VariableModeField::encode(var->mode()) |
|
||||
InitFlagField::encode(var->initialization_flag()) |
|
||||
MaybeAssignedFlagField::encode(var->maybe_assigned());
|
||||
scope_info->set(index + context_index, *var->name());
|
||||
scope_info->set(info_index + context_index, Smi::FromInt(info));
|
||||
}
|
||||
@ -166,10 +163,9 @@ Handle<ScopeInfo> ScopeInfo::Create(Isolate* isolate, Zone* zone,
|
||||
Variable* var = context_globals[i];
|
||||
scope_info->set(index + i, *var->name());
|
||||
// TODO(ishell): do we need this kind of info for globals here?
|
||||
uint32_t info =
|
||||
ContextLocalMode::encode(var->mode()) |
|
||||
ContextLocalInitFlag::encode(var->initialization_flag()) |
|
||||
ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned());
|
||||
uint32_t info = VariableModeField::encode(var->mode()) |
|
||||
InitFlagField::encode(var->initialization_flag()) |
|
||||
MaybeAssignedFlagField::encode(var->maybe_assigned());
|
||||
scope_info->set(info_index + i, Smi::FromInt(info));
|
||||
}
|
||||
|
||||
@ -252,9 +248,9 @@ Handle<ScopeInfo> ScopeInfo::CreateGlobalThisBinding(Isolate* isolate) {
|
||||
DCHECK(index == scope_info->ContextLocalNameEntriesIndex());
|
||||
scope_info->set(index++, *isolate->factory()->this_string());
|
||||
DCHECK(index == scope_info->ContextLocalInfoEntriesIndex());
|
||||
const uint32_t value = ContextLocalMode::encode(CONST) |
|
||||
ContextLocalInitFlag::encode(kCreatedInitialized) |
|
||||
ContextLocalMaybeAssignedFlag::encode(kNotAssigned);
|
||||
const uint32_t value = VariableModeField::encode(CONST) |
|
||||
InitFlagField::encode(kCreatedInitialized) |
|
||||
MaybeAssignedFlagField::encode(kNotAssigned);
|
||||
scope_info->set(index++, Smi::FromInt(value));
|
||||
|
||||
// And here we record that this scopeinfo binds a receiver.
|
||||
@ -428,7 +424,7 @@ VariableMode ScopeInfo::ContextLocalMode(int var) {
|
||||
DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount());
|
||||
int info_index = ContextLocalInfoEntriesIndex() + var;
|
||||
int value = Smi::cast(get(info_index))->value();
|
||||
return ContextLocalMode::decode(value);
|
||||
return VariableModeField::decode(value);
|
||||
}
|
||||
|
||||
|
||||
@ -436,7 +432,7 @@ InitializationFlag ScopeInfo::ContextLocalInitFlag(int var) {
|
||||
DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount());
|
||||
int info_index = ContextLocalInfoEntriesIndex() + var;
|
||||
int value = Smi::cast(get(info_index))->value();
|
||||
return ContextLocalInitFlag::decode(value);
|
||||
return InitFlagField::decode(value);
|
||||
}
|
||||
|
||||
|
||||
@ -444,7 +440,7 @@ MaybeAssignedFlag ScopeInfo::ContextLocalMaybeAssignedFlag(int var) {
|
||||
DCHECK(0 <= var && var < ContextLocalCount() + ContextGlobalCount());
|
||||
int info_index = ContextLocalInfoEntriesIndex() + var;
|
||||
int value = Smi::cast(get(info_index))->value();
|
||||
return ContextLocalMaybeAssignedFlag::decode(value);
|
||||
return MaybeAssignedFlagField::decode(value);
|
||||
}
|
||||
|
||||
bool ScopeInfo::VariableIsSynthetic(String* name) {
|
||||
@ -462,7 +458,7 @@ int ScopeInfo::StackSlotIndex(String* name) {
|
||||
if (length() > 0) {
|
||||
int first_slot_index = Smi::cast(get(StackLocalFirstSlotIndex()))->value();
|
||||
int start = StackLocalEntriesIndex();
|
||||
int end = StackLocalEntriesIndex() + StackLocalCount();
|
||||
int end = start + StackLocalCount();
|
||||
for (int i = start; i < end; ++i) {
|
||||
if (name == get(i)) {
|
||||
return i - start + first_slot_index;
|
||||
@ -478,8 +474,10 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info,
|
||||
InitializationFlag* init_flag,
|
||||
MaybeAssignedFlag* maybe_assigned_flag) {
|
||||
DCHECK(name->IsInternalizedString());
|
||||
DCHECK(mode != NULL);
|
||||
DCHECK(init_flag != NULL);
|
||||
DCHECK_NOT_NULL(mode);
|
||||
DCHECK_NOT_NULL(init_flag);
|
||||
DCHECK_NOT_NULL(maybe_assigned_flag);
|
||||
|
||||
if (scope_info->length() > 0) {
|
||||
ContextSlotCache* context_slot_cache =
|
||||
scope_info->GetIsolate()->context_slot_cache();
|
||||
@ -491,8 +489,7 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info,
|
||||
}
|
||||
|
||||
int start = scope_info->ContextLocalNameEntriesIndex();
|
||||
int end = scope_info->ContextLocalNameEntriesIndex() +
|
||||
scope_info->ContextLocalCount();
|
||||
int end = start + scope_info->ContextLocalCount();
|
||||
for (int i = start; i < end; ++i) {
|
||||
if (*name == scope_info->get(i)) {
|
||||
int var = i - start;
|
||||
@ -511,17 +508,18 @@ int ScopeInfo::ContextSlotIndex(Handle<ScopeInfo> scope_info,
|
||||
context_slot_cache->Update(scope_info, name, TEMPORARY,
|
||||
kNeedsInitialization, kNotAssigned, -1);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int ScopeInfo::ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info,
|
||||
Handle<String> name, VariableMode* mode,
|
||||
InitializationFlag* init_flag,
|
||||
MaybeAssignedFlag* maybe_assigned_flag) {
|
||||
DCHECK(name->IsInternalizedString());
|
||||
DCHECK(mode != NULL);
|
||||
DCHECK(init_flag != NULL);
|
||||
DCHECK_NOT_NULL(mode);
|
||||
DCHECK_NOT_NULL(init_flag);
|
||||
DCHECK_NOT_NULL(maybe_assigned_flag);
|
||||
if (scope_info->length() > 0) {
|
||||
// This is to ensure that ContextLocalMode() and co. queries would work.
|
||||
DCHECK_EQ(scope_info->ContextGlobalNameEntriesIndex(),
|
||||
@ -529,8 +527,7 @@ int ScopeInfo::ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info,
|
||||
scope_info->ContextLocalCount());
|
||||
int base = scope_info->ContextLocalNameEntriesIndex();
|
||||
int start = scope_info->ContextGlobalNameEntriesIndex();
|
||||
int end = scope_info->ContextGlobalNameEntriesIndex() +
|
||||
scope_info->ContextGlobalCount();
|
||||
int end = start + scope_info->ContextGlobalCount();
|
||||
for (int i = start; i < end; ++i) {
|
||||
if (*name == scope_info->get(i)) {
|
||||
int var = i - base;
|
||||
@ -564,7 +561,7 @@ int ScopeInfo::ParameterIndex(String* name) {
|
||||
// inside a function (and thus we need to look
|
||||
// at the last index). Was bug# 1110337.
|
||||
int start = ParameterEntriesIndex();
|
||||
int end = ParameterEntriesIndex() + ParameterCount();
|
||||
int end = start + ParameterCount();
|
||||
for (int i = end - 1; i >= start; --i) {
|
||||
if (name == get(i)) {
|
||||
return i - start;
|
||||
|
@ -148,9 +148,10 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope,
|
||||
asm_function_ = outer_scope_->IsAsmModule();
|
||||
}
|
||||
|
||||
ModuleScope::ModuleScope(Zone* zone, DeclarationScope* script_scope,
|
||||
ModuleScope::ModuleScope(DeclarationScope* script_scope,
|
||||
AstValueFactory* ast_value_factory)
|
||||
: DeclarationScope(zone, script_scope, MODULE_SCOPE) {
|
||||
: DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) {
|
||||
Zone* zone = ast_value_factory->zone();
|
||||
module_descriptor_ = new (zone) ModuleDescriptor(zone);
|
||||
set_language_mode(STRICT);
|
||||
DeclareThis(ast_value_factory);
|
||||
@ -617,35 +618,36 @@ Variable* Scope::LookupInScopeInfo(const AstRawString* name) {
|
||||
// There should be no local slot with the given name.
|
||||
DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0);
|
||||
|
||||
// Check context slot lookup.
|
||||
VariableMode mode;
|
||||
VariableLocation location = VariableLocation::CONTEXT;
|
||||
InitializationFlag init_flag;
|
||||
MaybeAssignedFlag maybe_assigned_flag;
|
||||
|
||||
VariableLocation location = VariableLocation::CONTEXT;
|
||||
int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode,
|
||||
&init_flag, &maybe_assigned_flag);
|
||||
if (index < 0) {
|
||||
location = VariableLocation::GLOBAL;
|
||||
index = ScopeInfo::ContextGlobalSlotIndex(scope_info_, name_handle, &mode,
|
||||
&init_flag, &maybe_assigned_flag);
|
||||
DCHECK(index < 0 || (is_script_scope() && mode == VAR));
|
||||
}
|
||||
if (index < 0) {
|
||||
// Check parameters.
|
||||
index = scope_info_->ParameterIndex(*name_handle);
|
||||
if (index < 0) return NULL;
|
||||
|
||||
mode = DYNAMIC;
|
||||
location = VariableLocation::LOOKUP;
|
||||
init_flag = kCreatedInitialized;
|
||||
// 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;
|
||||
} else {
|
||||
DCHECK(location != VariableLocation::GLOBAL ||
|
||||
(is_script_scope() && IsDeclaredVariableMode(mode) &&
|
||||
!IsLexicalVariableMode(mode)));
|
||||
index = scope_info_->ParameterIndex(*name_handle);
|
||||
if (index >= 0) {
|
||||
mode = DYNAMIC;
|
||||
init_flag = kCreatedInitialized;
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
if (index < 0 && scope_type() == MODULE_SCOPE) {
|
||||
location = VariableLocation::MODULE;
|
||||
index = -1; // TODO(neis): Find module variables in scope info.
|
||||
}
|
||||
if (index < 0) return nullptr; // Nowhere found.
|
||||
|
||||
Variable::Kind kind = Variable::NORMAL;
|
||||
if (location == VariableLocation::CONTEXT &&
|
||||
@ -822,9 +824,9 @@ Declaration* Scope::CheckLexDeclarationsConflictingWith(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
|
||||
ZoneList<Variable*>* context_locals,
|
||||
ZoneList<Variable*>* context_globals) {
|
||||
void Scope::CollectVariables(ZoneList<Variable*>* stack_locals,
|
||||
ZoneList<Variable*>* context_locals,
|
||||
ZoneList<Variable*>* context_globals) {
|
||||
// TODO(verwaest): Just pass out locals_ directly and walk it?
|
||||
DCHECK_NOT_NULL(stack_locals);
|
||||
DCHECK_NOT_NULL(context_locals);
|
||||
|
@ -340,12 +340,11 @@ class Scope: public ZoneObject {
|
||||
// ---------------------------------------------------------------------------
|
||||
// Variable allocation.
|
||||
|
||||
// Collect stack and context allocated local variables in this scope. Note
|
||||
// that the function variable - if present - is not collected and should be
|
||||
// handled separately.
|
||||
void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals,
|
||||
ZoneList<Variable*>* context_locals,
|
||||
ZoneList<Variable*>* context_globals);
|
||||
// Collect variables in this scope. Note that the function variable - if
|
||||
// present - is not collected and should be handled separately.
|
||||
void CollectVariables(ZoneList<Variable*>* stack_locals,
|
||||
ZoneList<Variable*>* context_locals,
|
||||
ZoneList<Variable*>* context_globals);
|
||||
|
||||
// Result of variable allocation.
|
||||
int num_stack_slots() const { return num_stack_slots_; }
|
||||
@ -836,7 +835,7 @@ class DeclarationScope : public Scope {
|
||||
|
||||
class ModuleScope final : public DeclarationScope {
|
||||
public:
|
||||
ModuleScope(Zone* zone, DeclarationScope* script_scope,
|
||||
ModuleScope(DeclarationScope* script_scope,
|
||||
AstValueFactory* ast_value_factory);
|
||||
|
||||
ModuleDescriptor* module() const {
|
||||
|
@ -87,8 +87,7 @@ void AstTyper::ObserveTypesAtOsrEntry(IterationStatement* stmt) {
|
||||
ZoneList<Variable*> local_vars(locals, zone());
|
||||
ZoneList<Variable*> context_vars(scope_->ContextLocalCount(), zone());
|
||||
ZoneList<Variable*> global_vars(scope_->ContextGlobalCount(), zone());
|
||||
scope_->CollectStackAndContextLocals(&local_vars, &context_vars,
|
||||
&global_vars);
|
||||
scope_->CollectVariables(&local_vars, &context_vars, &global_vars);
|
||||
for (int i = 0; i < locals; i++) {
|
||||
PrintObserved(local_vars.at(i),
|
||||
frame->GetExpression(i),
|
||||
|
@ -1904,8 +1904,7 @@ Handle<Object> LiveEditFunctionTracker::SerializeFunctionScope(Scope* scope) {
|
||||
ZoneList<Variable*> context_list(current_scope->ContextLocalCount(), zone_);
|
||||
ZoneList<Variable*> globals_list(current_scope->ContextGlobalCount(),
|
||||
zone_);
|
||||
current_scope->CollectStackAndContextLocals(&stack_list, &context_list,
|
||||
&globals_list);
|
||||
current_scope->CollectVariables(&stack_list, &context_list, &globals_list);
|
||||
for (int i = 0; i < context_list.length(); i++) {
|
||||
int context_index = context_list[i]->index() - Context::MIN_CONTEXT_SLOTS;
|
||||
int location = scope_info_length + context_index * 2;
|
||||
|
@ -4507,12 +4507,10 @@ class ScopeInfo : public FixedArray {
|
||||
class FunctionKindField
|
||||
: public BitField<FunctionKind, HasSimpleParametersField::kNext, 9> {};
|
||||
|
||||
// BitFields representing the encoded information for context locals in the
|
||||
// ContextLocalInfoEntries part.
|
||||
class ContextLocalMode: public BitField<VariableMode, 0, 3> {};
|
||||
class ContextLocalInitFlag: public BitField<InitializationFlag, 3, 1> {};
|
||||
class ContextLocalMaybeAssignedFlag
|
||||
: public BitField<MaybeAssignedFlag, 4, 1> {};
|
||||
// Properties of variables.
|
||||
class VariableModeField : public BitField<VariableMode, 0, 3> {};
|
||||
class InitFlagField : public BitField<InitializationFlag, 3, 1> {};
|
||||
class MaybeAssignedFlagField : public BitField<MaybeAssignedFlag, 4, 1> {};
|
||||
|
||||
friend class ScopeIterator;
|
||||
};
|
||||
|
@ -641,7 +641,7 @@ class ParserBase {
|
||||
}
|
||||
|
||||
ModuleScope* NewModuleScope(DeclarationScope* parent) const {
|
||||
return new (zone()) ModuleScope(zone(), parent, ast_value_factory());
|
||||
return new (zone()) ModuleScope(parent, ast_value_factory());
|
||||
}
|
||||
|
||||
DeclarationScope* NewEvalScope(Scope* parent) const {
|
||||
|
Loading…
Reference in New Issue
Block a user