0

I have a big object defined in the global scope called global. I would like to dynamically find all the referenced properties under my variable global. That is, all the properties that were accessed during the execution of the code.

I want to do static code analysis to extract all the referenced properties under my variable. I can search for these patterns: global.PROPERTY_NAME AND global[PROPERTY_NAM]. However, what about the complicated cases like these ones

var tmp="PROPERTY_NAME";
global[tmp]

OR

var tmp=global;
tmp.PROPERTY_NAME

and the other ones?

I don't want to get all the variable's properties. I only want a list of the referenced ONES!! the properties that were referenced in my source code only

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
nullException
  • 1,112
  • 4
  • 17
  • 29

2 Answers2

3

After your edit:

What you're looking for is JavaScript Proxy objects. Here is a tutorial on how to do this using them.

Proxy objects let you wrap an object and execute a method whenever its properties are accessed. Unfortunately as it currently stands they are not widely supported.

This is currently only way in JavaScript to accomplish this without changing your original global object.

You can turn them on in Chrome by enabling experimental JavaScript in the about:flags tab.

Before your edit:

The feature you're looking for is called reflection, JavaScript supports it well and natively

Here is some code that iterates through an object and gets its properties

for(var prop in global){
    if(global.hasOwnProperty(prop)){ //this is to only get its properties and not its prototype's
        alert(prop+" => "+global[prop]);
    }
}

This is fairly cross-browser. More modern browsers allow you to do this in simpler ways like Object.keys(global) which returns an array containing all its enumerable properties, or Object.getOwnPropertyNames(global) which returns both enumerable and not-enumerable properties.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I dont want to get all the variable properties. I only want a list of the USED ONES!! the properties that were mentioned in my code only – nullException Mar 01 '13 at 14:39
  • 4
    @nullException First, there is no need to be rude, I'm helping you out of my own free time and for no return. Second, I'm not sure what you mean by 'USED ONES!!', do you mean referenced ones? Ones that were assigned into? Ones that were referenced? Ones that are functions and were called? – Benjamin Gruenbaum Mar 01 '13 at 14:41
  • Thanks for your help. a list of the referenced properties – nullException Mar 01 '13 at 14:43
1

Due to the dynamic nature of JavaScript you won't achieve that with static code analysis. Think about cases like this:

var prop = document.getElementById('prop').value;
global[prop];

Impossible. The alternative, dynamic analysis, would mean that you modify your global object to log access to its properties, then run the code. This is easily possible in JavaScript but it won't help you either because how would you assure that you have covered every possible access? Especially in a 5 MB JavaScript, there are most likely edge cases that you will oversee.

So, if you can't narrow down your requirement, it won't be possible.

Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
  • It actually is possible, just not supported cross-browser. See my answer and http://www.kevinwestern.org/blog/2012/11/07/ecmascript-6-a-quick-look-at-proxies/ for example. – Benjamin Gruenbaum Mar 01 '13 at 14:56
  • Proxies do exactly what I described and have exactly the limitations that I mentioned. – Fabian Schmengler Mar 01 '13 at 14:59
  • "dynamic analysis, would mean that you modify your global object" - It does _not_ require changing to global object using ECMAScript 6 proxies, just proxying it, that's the point, you don't have to cover edge cases. I'm really excited about proxies, they offer a lot of power :) – Benjamin Gruenbaum Mar 01 '13 at 15:00
  • OK you don't have to modify the object but that's not the problem. The problem is how to run the code to cover every possible access to the ojbect. – Fabian Schmengler Mar 01 '13 at 15:13
  • It's actually pretty awesome :) If you look at the first example "logging access" here: http://www.kevinwestern.org/blog/2012/11/07/ecmascript-6-a-quick-look-at-proxies/ it's a matter of 13 lines of code. I've previousloy asked this question http://stackoverflow.com/questions/11144589/override-undefined-call-behaviour-in-javascript which is another place where proxies might be awesome :) – Benjamin Gruenbaum Mar 01 '13 at 15:19