V8 Project
v8::internal::ScopeIterator Class Reference
+ Collaboration diagram for v8::internal::ScopeIterator:

Public Types

enum  ScopeType {
  ScopeTypeGlobal = 0 , ScopeTypeLocal , ScopeTypeWith , ScopeTypeClosure ,
  ScopeTypeCatch , ScopeTypeBlock , ScopeTypeModule
}
 

Public Member Functions

 ScopeIterator (Isolate *isolate, JavaScriptFrame *frame, int inlined_jsframe_index, bool ignore_nested_scopes=false)
 
 ScopeIterator (Isolate *isolate, Handle< JSFunction > function)
 
bool Done ()
 
bool Failed ()
 
void Next ()
 
ScopeType Type ()
 
MaybeHandle< JSObjectScopeObject ()
 
bool SetVariableValue (Handle< String > variable_name, Handle< Object > new_value)
 
Handle< ScopeInfoCurrentScopeInfo ()
 
Handle< ContextCurrentContext ()
 

Private Member Functions

void RetrieveScopeChain (Scope *scope, Handle< SharedFunctionInfo > shared_info)
 
 DISALLOW_IMPLICIT_CONSTRUCTORS (ScopeIterator)
 

Private Attributes

Isolateisolate_
 
JavaScriptFrameframe_
 
int inlined_jsframe_index_
 
Handle< JSFunctionfunction_
 
Handle< Contextcontext_
 
List< Handle< ScopeInfo > > nested_scope_chain_
 
bool failed_
 

Detailed Description

Definition at line 6482 of file runtime.cc.

Member Enumeration Documentation

◆ ScopeType

Enumerator
ScopeTypeGlobal 
ScopeTypeLocal 
ScopeTypeWith 
ScopeTypeClosure 
ScopeTypeCatch 
ScopeTypeBlock 
ScopeTypeModule 

Definition at line 6484 of file runtime.cc.

Constructor & Destructor Documentation

◆ ScopeIterator() [1/2]

v8::internal::ScopeIterator::ScopeIterator ( Isolate isolate,
JavaScriptFrame frame,
int  inlined_jsframe_index,
bool  ignore_nested_scopes = false 
)
inline

Definition at line 6494 of file runtime.cc.

6496  : isolate_(isolate),
6497  frame_(frame),
6498  inlined_jsframe_index_(inlined_jsframe_index),
6499  function_(frame->function()),
6500  context_(Context::cast(frame->context())),
6502  failed_(false) {
6503  // Catch the case when the debugger stops in an internal function.
6504  Handle<SharedFunctionInfo> shared_info(function_->shared());
6505  Handle<ScopeInfo> scope_info(shared_info->scope_info());
6506  if (shared_info->script() == isolate->heap()->undefined_value()) {
6507  while (context_->closure() == *function_) {
6508  context_ = Handle<Context>(context_->previous(), isolate_);
6509  }
6510  return;
6511  }
6512 
6513  // Get the debug info (create it if it does not exist).
6514  if (!isolate->debug()->EnsureDebugInfo(shared_info, function_)) {
6515  // Return if ensuring debug info failed.
6516  return;
6517  }
6518 
6519  // Currently it takes too much time to find nested scopes due to script
6520  // parsing. Sometimes we want to run the ScopeIterator as fast as possible
6521  // (for example, while collecting async call stacks on every
6522  // addEventListener call), even if we drop some nested scopes.
6523  // Later we may optimize getting the nested scopes (cache the result?)
6524  // and include nested scopes into the "fast" iteration case as well.
6525  if (!ignore_nested_scopes) {
6526  Handle<DebugInfo> debug_info = Debug::GetDebugInfo(shared_info);
6527 
6528  // Find the break point where execution has stopped.
6529  BreakLocationIterator break_location_iterator(debug_info,
6531  // pc points to the instruction after the current one, possibly a break
6532  // location as well. So the "- 1" to exclude it from the search.
6533  break_location_iterator.FindBreakLocationFromAddress(frame->pc() - 1);
6534 
6535  // Within the return sequence at the moment it is not possible to
6536  // get a source position which is consistent with the current scope chain.
6537  // Thus all nested with, catch and block contexts are skipped and we only
6538  // provide the function scope.
6539  ignore_nested_scopes = break_location_iterator.IsExit();
6540  }
6541 
6542  if (ignore_nested_scopes) {
6543  if (scope_info->HasContext()) {
6544  context_ = Handle<Context>(context_->declaration_context(), isolate_);
6545  } else {
6546  while (context_->closure() == *function_) {
6547  context_ = Handle<Context>(context_->previous(), isolate_);
6548  }
6549  }
6550  if (scope_info->scope_type() == FUNCTION_SCOPE) {
6551  nested_scope_chain_.Add(scope_info);
6552  }
6553  } else {
6554  // Reparse the code and analyze the scopes.
6555  Handle<Script> script(Script::cast(shared_info->script()));
6556  Scope* scope = NULL;
6557 
6558  // Check whether we are in global, eval or function code.
6559  Handle<ScopeInfo> scope_info(shared_info->scope_info());
6560  if (scope_info->scope_type() != FUNCTION_SCOPE) {
6561  // Global or eval code.
6562  CompilationInfoWithZone info(script);
6563  if (scope_info->scope_type() == GLOBAL_SCOPE) {
6564  info.MarkAsGlobal();
6565  } else {
6566  DCHECK(scope_info->scope_type() == EVAL_SCOPE);
6567  info.MarkAsEval();
6568  info.SetContext(Handle<Context>(function_->context()));
6569  }
6570  if (Parser::Parse(&info) && Scope::Analyze(&info)) {
6571  scope = info.function()->scope();
6572  }
6573  RetrieveScopeChain(scope, shared_info);
6574  } else {
6575  // Function code
6576  CompilationInfoWithZone info(shared_info);
6577  if (Parser::Parse(&info) && Scope::Analyze(&info)) {
6578  scope = info.function()->scope();
6579  }
6580  RetrieveScopeChain(scope, shared_info);
6581  }
6582  }
6583  }
static Context * cast(Object *context)
Definition: contexts.h:255
static Handle< DebugInfo > GetDebugInfo(Handle< SharedFunctionInfo > shared)
Definition: debug.cc:1069
void RetrieveScopeChain(Scope *scope, Handle< SharedFunctionInfo > shared_info)
Definition: runtime.cc:6817
JavaScriptFrame * frame_
Definition: runtime.cc:6810
Handle< Context > context_
Definition: runtime.cc:6813
List< Handle< ScopeInfo > > nested_scope_chain_
Definition: runtime.cc:6814
Handle< JSFunction > function_
Definition: runtime.cc:6812
static bool Analyze(CompilationInfo *info)
Definition: scopes.cc:260
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
@ ALL_BREAK_LOCATIONS
Definition: debug.h:53
@ FUNCTION_SCOPE
Definition: globals.h:647
@ GLOBAL_SCOPE
Definition: globals.h:649

References v8::internal::ALL_BREAK_LOCATIONS, v8::internal::Scope::Analyze(), context_, DCHECK, v8::internal::Isolate::debug(), v8::internal::Debug::EnsureDebugInfo(), v8::internal::EVAL_SCOPE, v8::internal::BreakLocationIterator::FindBreakLocationFromAddress(), v8::internal::CompilationInfo::function(), function_, v8::internal::FUNCTION_SCOPE, v8::internal::Debug::GetDebugInfo(), v8::internal::GLOBAL_SCOPE, v8::internal::Isolate::heap(), v8::internal::BreakLocationIterator::IsExit(), isolate_, v8::internal::CompilationInfo::MarkAsEval(), v8::internal::CompilationInfo::MarkAsGlobal(), nested_scope_chain_, NULL, v8::internal::Parser::Parse(), RetrieveScopeChain(), and v8::internal::CompilationInfo::SetContext().

+ Here is the call graph for this function:

◆ ScopeIterator() [2/2]

v8::internal::ScopeIterator::ScopeIterator ( Isolate isolate,
Handle< JSFunction function 
)
inline

Definition at line 6585 of file runtime.cc.

6586  : isolate_(isolate),
6587  frame_(NULL),
6589  function_(function),
6590  context_(function->context()),
6591  failed_(false) {
6592  if (function->IsBuiltin()) {
6593  context_ = Handle<Context>();
6594  }
6595  }

References context_.

Member Function Documentation

◆ CurrentContext()

Handle<Context> v8::internal::ScopeIterator::CurrentContext ( )
inline

Definition at line 6742 of file runtime.cc.

6742  {
6743  DCHECK(!failed_);
6744  if (Type() == ScopeTypeGlobal || nested_scope_chain_.is_empty()) {
6745  return context_;
6746  } else if (nested_scope_chain_.last()->HasContext()) {
6747  return context_;
6748  } else {
6749  return Handle<Context>();
6750  }
6751  }

References context_, DCHECK, failed_, nested_scope_chain_, ScopeTypeGlobal, and Type().

Referenced by ScopeObject(), and SetVariableValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ CurrentScopeInfo()

Handle<ScopeInfo> v8::internal::ScopeIterator::CurrentScopeInfo ( )
inline

Definition at line 6728 of file runtime.cc.

6728  {
6729  DCHECK(!failed_);
6730  if (!nested_scope_chain_.is_empty()) {
6731  return nested_scope_chain_.last();
6732  } else if (context_->IsBlockContext()) {
6733  return Handle<ScopeInfo>(ScopeInfo::cast(context_->extension()));
6734  } else if (context_->IsFunctionContext()) {
6735  return Handle<ScopeInfo>(context_->closure()->shared()->scope_info());
6736  }
6737  return Handle<ScopeInfo>::null();
6738  }
static Handle< T > null()
Definition: handles.h:123

References context_, DCHECK, failed_, nested_scope_chain_, and v8::internal::Handle< T >::null().

+ Here is the call graph for this function:

◆ DISALLOW_IMPLICIT_CONSTRUCTORS()

v8::internal::ScopeIterator::DISALLOW_IMPLICIT_CONSTRUCTORS ( ScopeIterator  )
private

◆ Done()

bool v8::internal::ScopeIterator::Done ( )
inline

Definition at line 6598 of file runtime.cc.

6598  {
6599  DCHECK(!failed_);
6600  return context_.is_null();
6601  }

References context_, DCHECK, and failed_.

Referenced by v8::internal::RUNTIME_FUNCTION(), and v8::internal::SetScopeVariableValue().

+ Here is the caller graph for this function:

◆ Failed()

bool v8::internal::ScopeIterator::Failed ( )
inline

Definition at line 6603 of file runtime.cc.

6603 { return failed_; }

References failed_.

◆ Next()

void v8::internal::ScopeIterator::Next ( )
inline

Definition at line 6606 of file runtime.cc.

6606  {
6607  DCHECK(!failed_);
6608  ScopeType scope_type = Type();
6609  if (scope_type == ScopeTypeGlobal) {
6610  // The global scope is always the last in the chain.
6611  DCHECK(context_->IsNativeContext());
6612  context_ = Handle<Context>();
6613  return;
6614  }
6615  if (nested_scope_chain_.is_empty()) {
6616  context_ = Handle<Context>(context_->previous(), isolate_);
6617  } else {
6618  if (nested_scope_chain_.last()->HasContext()) {
6619  DCHECK(context_->previous() != NULL);
6620  context_ = Handle<Context>(context_->previous(), isolate_);
6621  }
6622  nested_scope_chain_.RemoveLast();
6623  }
6624  }

References context_, DCHECK, failed_, isolate_, nested_scope_chain_, NULL, ScopeTypeGlobal, and Type().

Referenced by v8::internal::RUNTIME_FUNCTION().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RetrieveScopeChain()

void v8::internal::ScopeIterator::RetrieveScopeChain ( Scope scope,
Handle< SharedFunctionInfo shared_info 
)
inlineprivate

Definition at line 6817 of file runtime.cc.

6818  {
6819  if (scope != NULL) {
6820  int source_position = shared_info->code()->SourcePosition(frame_->pc());
6821  scope->GetNestedScopeChain(&nested_scope_chain_, source_position);
6822  } else {
6823  // A failed reparse indicates that the preparser has diverged from the
6824  // parser or that the preparse data given to the initial parse has been
6825  // faulty. We fail in debug mode but in release mode we only provide the
6826  // information we get from the context chain but nothing about
6827  // completely stack allocated scopes or stack allocated locals.
6828  // Or it could be due to stack overflow.
6830  failed_ = true;
6831  }
6832  }
bool has_pending_exception()
Definition: isolate.h:581

References DCHECK, failed_, frame_, v8::internal::Scope::GetNestedScopeChain(), v8::internal::Isolate::has_pending_exception(), isolate_, nested_scope_chain_, and NULL.

Referenced by ScopeIterator().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ScopeObject()

MaybeHandle<JSObject> v8::internal::ScopeIterator::ScopeObject ( )
inline

Definition at line 6675 of file runtime.cc.

6675  {
6676  DCHECK(!failed_);
6677  switch (Type()) {
6679  return Handle<JSObject>(CurrentContext()->global_object());
6681  // Materialize the content of the local scope into a JSObject.
6682  DCHECK(nested_scope_chain_.length() == 1);
6685  // Return the with object.
6686  return Handle<JSObject>(JSObject::cast(CurrentContext()->extension()));
6690  // Materialize the content of the closure scope into a JSObject.
6696  }
6697  UNREACHABLE();
6698  return Handle<JSObject>();
6699  }
Handle< Context > CurrentContext()
Definition: runtime.cc:6742
#define UNREACHABLE()
Definition: logging.h:30
static MUST_USE_RESULT MaybeHandle< JSObject > MaterializeModuleScope(Isolate *isolate, Handle< Context > context)
Definition: runtime.cc:6458
static MUST_USE_RESULT MaybeHandle< JSObject > MaterializeCatchScope(Isolate *isolate, Handle< Context > context)
Definition: runtime.cc:6406
static MUST_USE_RESULT MaybeHandle< JSObject > MaterializeBlockScope(Isolate *isolate, Handle< Context > context)
Definition: runtime.cc:6436
static MUST_USE_RESULT MaybeHandle< JSObject > MaterializeLocalScope(Isolate *isolate, JavaScriptFrame *frame, int inlined_jsframe_index)
Definition: runtime.cc:6219
static MUST_USE_RESULT MaybeHandle< JSObject > MaterializeClosure(Isolate *isolate, Handle< Context > context)
Definition: runtime.cc:6326

References CurrentContext(), DCHECK, failed_, frame_, inlined_jsframe_index_, isolate_, v8::internal::MaterializeBlockScope(), v8::internal::MaterializeCatchScope(), v8::internal::MaterializeClosure(), v8::internal::MaterializeLocalScope(), v8::internal::MaterializeModuleScope(), nested_scope_chain_, ScopeTypeBlock, ScopeTypeCatch, ScopeTypeClosure, ScopeTypeGlobal, ScopeTypeLocal, ScopeTypeModule, ScopeTypeWith, Type(), and UNREACHABLE.

Referenced by v8::internal::MaterializeScopeDetails().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SetVariableValue()

bool v8::internal::ScopeIterator::SetVariableValue ( Handle< String variable_name,
Handle< Object new_value 
)
inline

Definition at line 6701 of file runtime.cc.

6702  {
6703  DCHECK(!failed_);
6704  switch (Type()) {
6706  break;
6709  variable_name, new_value);
6711  break;
6713  return SetCatchVariableValue(isolate_, CurrentContext(), variable_name,
6714  new_value);
6717  variable_name, new_value);
6719  // TODO(2399): should we implement it?
6720  break;
6722  // TODO(2399): should we implement it?
6723  break;
6724  }
6725  return false;
6726  }
static bool SetClosureVariableValue(Isolate *isolate, Handle< Context > context, Handle< String > variable_name, Handle< Object > new_value)
Definition: runtime.cc:6372
static bool SetLocalVariableValue(Isolate *isolate, JavaScriptFrame *frame, int inlined_jsframe_index, Handle< String > variable_name, Handle< Object > new_value)
Definition: runtime.cc:6258
static bool SetCatchVariableValue(Isolate *isolate, Handle< Context > context, Handle< String > variable_name, Handle< Object > new_value)
Definition: runtime.cc:6421

References CurrentContext(), DCHECK, failed_, frame_, inlined_jsframe_index_, isolate_, ScopeTypeBlock, ScopeTypeCatch, ScopeTypeClosure, ScopeTypeGlobal, ScopeTypeLocal, ScopeTypeModule, ScopeTypeWith, v8::internal::SetCatchVariableValue(), v8::internal::SetClosureVariableValue(), v8::internal::SetLocalVariableValue(), and Type().

Referenced by v8::internal::SetScopeVariableValue().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Type()

ScopeType v8::internal::ScopeIterator::Type ( )
inline

Definition at line 6627 of file runtime.cc.

6627  {
6628  DCHECK(!failed_);
6629  if (!nested_scope_chain_.is_empty()) {
6630  Handle<ScopeInfo> scope_info = nested_scope_chain_.last();
6631  switch (scope_info->scope_type()) {
6632  case FUNCTION_SCOPE:
6633  DCHECK(context_->IsFunctionContext() || !scope_info->HasContext());
6634  return ScopeTypeLocal;
6635  case MODULE_SCOPE:
6636  DCHECK(context_->IsModuleContext());
6637  return ScopeTypeModule;
6638  case GLOBAL_SCOPE:
6639  DCHECK(context_->IsNativeContext());
6640  return ScopeTypeGlobal;
6641  case WITH_SCOPE:
6642  DCHECK(context_->IsWithContext());
6643  return ScopeTypeWith;
6644  case CATCH_SCOPE:
6645  DCHECK(context_->IsCatchContext());
6646  return ScopeTypeCatch;
6647  case BLOCK_SCOPE:
6648  DCHECK(!scope_info->HasContext() || context_->IsBlockContext());
6649  return ScopeTypeBlock;
6650  case EVAL_SCOPE:
6651  UNREACHABLE();
6652  }
6653  }
6654  if (context_->IsNativeContext()) {
6655  DCHECK(context_->global_object()->IsGlobalObject());
6656  return ScopeTypeGlobal;
6657  }
6658  if (context_->IsFunctionContext()) {
6659  return ScopeTypeClosure;
6660  }
6661  if (context_->IsCatchContext()) {
6662  return ScopeTypeCatch;
6663  }
6664  if (context_->IsBlockContext()) {
6665  return ScopeTypeBlock;
6666  }
6667  if (context_->IsModuleContext()) {
6668  return ScopeTypeModule;
6669  }
6670  DCHECK(context_->IsWithContext());
6671  return ScopeTypeWith;
6672  }
@ MODULE_SCOPE
Definition: globals.h:648

References v8::internal::BLOCK_SCOPE, v8::internal::CATCH_SCOPE, context_, DCHECK, v8::internal::EVAL_SCOPE, failed_, v8::internal::FUNCTION_SCOPE, v8::internal::GLOBAL_SCOPE, v8::internal::MODULE_SCOPE, nested_scope_chain_, ScopeTypeBlock, ScopeTypeCatch, ScopeTypeClosure, ScopeTypeGlobal, ScopeTypeLocal, ScopeTypeModule, ScopeTypeWith, UNREACHABLE, and v8::internal::WITH_SCOPE.

Referenced by CurrentContext(), v8::internal::MaterializeScopeDetails(), Next(), ScopeObject(), and SetVariableValue().

+ Here is the caller graph for this function:

Member Data Documentation

◆ context_

Handle<Context> v8::internal::ScopeIterator::context_
private

Definition at line 6813 of file runtime.cc.

Referenced by CurrentContext(), CurrentScopeInfo(), Done(), Next(), ScopeIterator(), and Type().

◆ failed_

bool v8::internal::ScopeIterator::failed_
private

◆ frame_

JavaScriptFrame* v8::internal::ScopeIterator::frame_
private

Definition at line 6810 of file runtime.cc.

Referenced by RetrieveScopeChain(), ScopeObject(), and SetVariableValue().

◆ function_

Handle<JSFunction> v8::internal::ScopeIterator::function_
private

Definition at line 6812 of file runtime.cc.

Referenced by ScopeIterator().

◆ inlined_jsframe_index_

int v8::internal::ScopeIterator::inlined_jsframe_index_
private

Definition at line 6811 of file runtime.cc.

Referenced by ScopeObject(), and SetVariableValue().

◆ isolate_

Isolate* v8::internal::ScopeIterator::isolate_
private

Definition at line 6809 of file runtime.cc.

Referenced by Next(), RetrieveScopeChain(), ScopeIterator(), ScopeObject(), and SetVariableValue().

◆ nested_scope_chain_

List<Handle<ScopeInfo> > v8::internal::ScopeIterator::nested_scope_chain_
private

The documentation for this class was generated from the following file: