Doubts about WatchList and Locals

Aug 23, 2010 at 2:56 PM

Hi, I'm working to get a javascript editor with interactive debugger, and I used the DebugInformation Locals dictionary to fill a "Locals" listview in my UI. While trying to debug some script I found that, while doing a step by step execution of this script:

 var globalFoo = 'I am global!'

// constructor
function MyObject(attributeA, attributeB)
{
  var localFoo = 'I am local!'  
  this.attributeA = attributeA
  this.attributeB = attributeB
}
// creates an Object
obj = new MyObject('red', 1000)
var mickey= obj.attributeA
var donald = obj.attributeB
mickey = mickey + donald
mickey = 'hello'
var minnie = mickey + donald 

I could read the globalFoo value even if I was inside the MyObject constructor (with localFoo visible);
is this a wanted feature?

I saw that in Jint code there is this Locals filling:

public DebugInformation CreateDebugInformation(Statement statement)
{
  DebugInformation info = new DebugInformation();
  info.CurrentStatement = statement;
  info.CallStack = CallStack;
  info.Locals = new JsObject() { Prototype = JsUndefined.Instance };
  DebugMode = false;
  foreach (JsDictionaryObject scope in Scopes.ToArray())
  {
    foreach (var property in scope.GetKeys())
    {
      if (!info.Locals.HasProperty(property))
      {
        info.Locals[property] = scope[property];
      }
    }
  }
  DebugMode = true;
  return info;
}
If this was a wanted behaviour, I could evaluate the WatchList simply looking for the contained identifiers in the Locals dictionary keys, isnt'it?
But in this case, the "Locals" meaning could be misunderstood if compared to, i.e., that of visual studio.
If I decided to modify the above Locals filling only with current scope, then what do you think is the best way to evaluate a watch variable while debugging? 
Should I use a visitor? Could you give me any hint?
Thank you, Marco


Coordinator
Aug 25, 2010 at 7:07 AM

Hi,

Yes this is a wanted feature, because in Locals, this is actually all the values that are accessible at this step in the code. But you are also right : you have no way to make the difference between a local and a global variable.

The work around could be to change the way debug information are created : each variable in a specific scope should result in a new prototype of the info.Locals object.

 

JsObject locals=info.Locals
foreach (JsDictionaryObject scope in Scopes.ToArray())
  {
	
    foreach (var property in scope.GetKeys())
    {
      if (!locals.HasProperty(property))
      {
        locals[property] = scope[property];
      }
	locals.Prototype=new JsObject(){Prototype=JsUndefined.Instance};
	locals=locals.Prototype;
    }
  }

 

This should be better, and suit your needs.