@@ -73,7 +73,9 @@ ScopeIterator::ScopeIterator(Isolate* isolate, FrameInspector* frame_inspector,
7373 }
7474 }
7575 if (scope_info->scope_type () == FUNCTION_SCOPE ) {
76- nested_scope_chain_.Add (scope_info);
76+ nested_scope_chain_.Add (ExtendedScopeInfo (scope_info,
77+ shared_info->start_position (),
78+ shared_info->end_position ()));
7779 }
7880 if (!collect_non_locals) return ;
7981 }
@@ -130,12 +132,34 @@ MUST_USE_RESULT MaybeHandle<JSObject> ScopeIterator::MaterializeScopeDetails() {
130132 Handle<JSObject> scope_object;
131133 ASSIGN_RETURN_ON_EXCEPTION (isolate_, scope_object, ScopeObject (), JSObject);
132134 details->set (kScopeDetailsObjectIndex , *scope_object);
133- if (HasContext () && CurrentContext ()->closure () != NULL ) {
134- Handle<String> closure_name = JSFunction::GetDebugName (
135- Handle<JSFunction>(CurrentContext ()->closure ()));
135+ Handle<JSFunction> js_function = HasContext ()
136+ ? handle (CurrentContext ()->closure ())
137+ : Handle<JSFunction>::null ();
138+ if (!js_function.is_null ()) {
139+ Handle<String> closure_name = JSFunction::GetDebugName (js_function);
136140 if (!closure_name.is_null () && (closure_name->length () != 0 ))
137141 details->set (kScopeDetailsNameIndex , *closure_name);
138142 }
143+ if (Type () == ScopeTypeGlobal || Type () == ScopeTypeScript) {
144+ return isolate_->factory ()->NewJSArrayWithElements (details);
145+ }
146+
147+ int start_position = 0 ;
148+ int end_position = 0 ;
149+ if (!nested_scope_chain_.is_empty ()) {
150+ js_function = GetFunction ();
151+ start_position = nested_scope_chain_.last ().start_position ;
152+ end_position = nested_scope_chain_.last ().end_position ;
153+ } else if (!js_function.is_null ()) {
154+ start_position = js_function->shared ()->start_position ();
155+ end_position = js_function->shared ()->end_position ();
156+ }
157+
158+ if (!js_function.is_null ()) {
159+ details->set (kScopeDetailsStartPositionIndex , Smi::FromInt (start_position));
160+ details->set (kScopeDetailsEndPositionIndex , Smi::FromInt (end_position));
161+ details->set (kScopeDetailsFunctionIndex , *js_function);
162+ }
139163 return isolate_->factory ()->NewJSArrayWithElements (details);
140164}
141165
@@ -155,7 +179,8 @@ void ScopeIterator::Next() {
155179 context_ = Handle<Context>(context_->previous (), isolate_);
156180 }
157181 if (!nested_scope_chain_.is_empty ()) {
158- DCHECK_EQ (nested_scope_chain_.last ()->scope_type (), SCRIPT_SCOPE );
182+ DCHECK_EQ (nested_scope_chain_.last ().scope_info ->scope_type (),
183+ SCRIPT_SCOPE );
159184 nested_scope_chain_.RemoveLast ();
160185 DCHECK (nested_scope_chain_.is_empty ());
161186 }
@@ -165,7 +190,7 @@ void ScopeIterator::Next() {
165190 if (nested_scope_chain_.is_empty ()) {
166191 context_ = Handle<Context>(context_->previous (), isolate_);
167192 } else {
168- if (nested_scope_chain_.last ()->HasContext ()) {
193+ if (nested_scope_chain_.last (). scope_info ->HasContext ()) {
169194 DCHECK (context_->previous () != NULL );
170195 context_ = Handle<Context>(context_->previous (), isolate_);
171196 }
@@ -178,7 +203,7 @@ void ScopeIterator::Next() {
178203ScopeIterator::ScopeType ScopeIterator::Type () {
179204 DCHECK (!failed_);
180205 if (!nested_scope_chain_.is_empty ()) {
181- Handle<ScopeInfo> scope_info = nested_scope_chain_.last ();
206+ Handle<ScopeInfo> scope_info = nested_scope_chain_.last (). scope_info ;
182207 switch (scope_info->scope_type ()) {
183208 case FUNCTION_SCOPE :
184209 DCHECK (context_->IsFunctionContext () || !scope_info->HasContext ());
@@ -262,7 +287,7 @@ bool ScopeIterator::HasContext() {
262287 ScopeType type = Type ();
263288 if (type == ScopeTypeBlock || type == ScopeTypeLocal) {
264289 if (!nested_scope_chain_.is_empty ()) {
265- return nested_scope_chain_.last ()->HasContext ();
290+ return nested_scope_chain_.last (). scope_info ->HasContext ();
266291 }
267292 }
268293 return true ;
@@ -298,7 +323,7 @@ bool ScopeIterator::SetVariableValue(Handle<String> variable_name,
298323Handle<ScopeInfo> ScopeIterator::CurrentScopeInfo () {
299324 DCHECK (!failed_);
300325 if (!nested_scope_chain_.is_empty ()) {
301- return nested_scope_chain_.last ();
326+ return nested_scope_chain_.last (). scope_info ;
302327 } else if (context_->IsBlockContext ()) {
303328 return Handle<ScopeInfo>(context_->scope_info ());
304329 } else if (context_->IsFunctionContext ()) {
@@ -313,7 +338,7 @@ Handle<Context> ScopeIterator::CurrentContext() {
313338 if (Type () == ScopeTypeGlobal || Type () == ScopeTypeScript ||
314339 nested_scope_chain_.is_empty ()) {
315340 return context_;
316- } else if (nested_scope_chain_.last ()->HasContext ()) {
341+ } else if (nested_scope_chain_.last (). scope_info ->HasContext ()) {
317342 return context_;
318343 } else {
319344 return Handle<Context>();
@@ -409,7 +434,7 @@ void ScopeIterator::DebugPrint() {
409434void ScopeIterator::RetrieveScopeChain (Scope* scope) {
410435 if (scope != NULL ) {
411436 int source_position = frame_inspector_->GetSourcePosition ();
412- scope-> GetNestedScopeChain (isolate_, &nested_scope_chain_ , source_position);
437+ GetNestedScopeChain (isolate_, scope , source_position);
413438 } else {
414439 // A failed reparse indicates that the preparser has diverged from the
415440 // parser or that the preparse data given to the initial parse has been
@@ -541,7 +566,7 @@ Handle<JSObject> ScopeIterator::MaterializeBlockScope() {
541566
542567 Handle<Context> context = Handle<Context>::null ();
543568 if (!nested_scope_chain_.is_empty ()) {
544- Handle<ScopeInfo> scope_info = nested_scope_chain_.last ();
569+ Handle<ScopeInfo> scope_info = nested_scope_chain_.last (). scope_info ;
545570 frame_inspector_->MaterializeStackLocals (block_scope, scope_info);
546571 if (scope_info->HasContext ()) context = CurrentContext ();
547572 } else {
@@ -815,5 +840,24 @@ bool ScopeIterator::CopyContextExtensionToScopeObject(
815840 return true ;
816841}
817842
843+ void ScopeIterator::GetNestedScopeChain (Isolate* isolate, Scope* scope,
844+ int position) {
845+ if (!scope->is_eval_scope ()) {
846+ nested_scope_chain_.Add (ExtendedScopeInfo (scope->GetScopeInfo (isolate),
847+ scope->start_position (),
848+ scope->end_position ()));
849+ }
850+ for (int i = 0 ; i < scope->inner_scopes ()->length (); i++) {
851+ Scope* inner_scope = scope->inner_scopes ()->at (i);
852+ int beg_pos = inner_scope->start_position ();
853+ int end_pos = inner_scope->end_position ();
854+ DCHECK (beg_pos >= 0 && end_pos >= 0 );
855+ if (beg_pos <= position && position < end_pos) {
856+ GetNestedScopeChain (isolate, inner_scope, position);
857+ return ;
858+ }
859+ }
860+ }
861+
818862} // namespace internal
819863} // namespace v8
0 commit comments