<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9</title>
    <atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/c4f.Shreyans-Kothari/Posts/RSS"></atom:link>
    <itunes:summary></itunes:summary>
    <itunes:author>Microsoft</itunes:author>
    <itunes:subtitle></itunes:subtitle>
    <image>
      <url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
      <title>Channel 9</title>
      <link>http://channel9.msdn.com/Niners/c4f.Shreyans-Kothari/Posts</link>
    </image>
    <itunes:image href=""></itunes:image>
    <itunes:category text="Technology"></itunes:category>
    <description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
    <link>http://channel9.msdn.com/Niners/c4f.Shreyans-Kothari/Posts</link>
    <language>en</language>
    <pubDate>Sun, 26 May 2013 06:29:52 GMT</pubDate>
    <lastBuildDate>Sun, 26 May 2013 06:29:52 GMT</lastBuildDate>
    <generator>Rev9</generator>
    <c9:totalResults>1</c9:totalResults>
    <c9:pageCount>1</c9:pageCount>
    <c9:pageSize>25</c9:pageSize>
  <item>
      <title>JavaScript Reflection</title>
      <description><![CDATA[<span id="c4fmetadata">
<table cellspacing="0" cellpadding="1" width="100%" border="0">
<tbody>
<tr class="entry_overview">
<td width="50"></td>
<td><span class="entry_description">I was writing an application for creating a manifest for JavaScript. I was unable to do it using DOM; instead I wrote my own application for parsing JavaScript files. It is little tough to peek into JavaScript objects, initial
 I could only get the members and fields using for-in. but that was not enough, what if someone stored a file locally and I have to write code to execute it, I would be able to get only the members, just executing members without passing the required input
 parameter if any will not work. The code below will parse the file and give me the options as to what input is required and if something is returned what it could possibly be returning except primitive types. Knowing these things in advance makes it easier
 to execute the code. Well it worked for me; let's see how it works for you. I know many people are looking for something like this.
</span></td>
</tr>
<tr>
<td colspan="2">
<div class="entry_author">Shreyans Kothari</div>
<div class="entry_company"><a href="http://shreyanskothari.spaces.live.com/">My Space</a></div>
<br>
<div class="entry_details"><b>Difficulty: </b><span class="entry_details_input">Intermediate</span></div>
<div class="entry_details"><b>Time Required:</b> <span class="entry_details_input">
1-3 hours</span></div>
<div class="entry_details"><b>Cost: </b><span class="entry_details_input">Free</span></div>
<div class="entry_details"><b>Software: </b><span class="entry_details_input"></span></div>
<div class="entry_details"><b>Hardware: </b><span class="entry_details_input"></span></div>
<div class="entry_details"><b>Download: </b><a href="http://channel9.msdn.com/ShowPost.aspx?PostID=329478">Download</a>
<ul>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
</span>
<p>The code might not be perfect or might not do everything for you, since I wrote to cater my needs. But the basic utilities are implemented, you can add thing according to your needs. I am using the code from my project, therefore you will not see lot of
 other code that the project refers to, but you can input you JavaScript file to see the results or use the sample file that's provided in the project.
<br>
<strong>Some assumptions</strong> that I made; it will only look for 'prototype' and 'this' keywords in the JS code.
</p>
<p><strong>The core code:</strong> <br>
This code finds all the objects, their members and inputs for each member:<br>
</p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>      x = content.IndexOf(<span class="str">&quot;.prototype.&quot;</span>, x);</pre>
<pre><span class="lnum">   2:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   3:  </span>      <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; x != -1; i&#43;&#43;)</pre>
<pre><span class="lnum">   4:  </span>      {</pre>
<pre class="alt"><span class="lnum">   5:  </span>         <span class="kwrd">int</span> j = 0;</pre>
<pre><span class="lnum">   6:  </span>         <span class="kwrd">int</span> k = 0;</pre>
<pre class="alt"><span class="lnum">   7:  </span>&nbsp;</pre>
<pre><span class="lnum">   8:  </span>         <span class="kwrd">for</span> (j = x - 1; (<span class="str">' '</span> != content[j] &amp;&amp; <span class="str">'\n'</span> != content[j]); j--) { }</pre>
<pre class="alt"><span class="lnum">   9:  </span>         <span class="kwrd">for</span> (k = x &#43; 10; (<span class="str">' '</span> != content[k] &amp;&amp; <span class="str">'='</span> != content[k]); k&#43;&#43;) { }</pre>
<pre><span class="lnum">  10:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  11:  </span>         <span class="kwrd">int</span> y = content.IndexOf(<span class="str">&quot;function(&quot;</span>, k);</pre>
<pre><span class="lnum">  12:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  13:  </span>         <span class="kwrd">if</span> (y == -1)</pre>
<pre><span class="lnum">  14:  </span>            <span class="kwrd">break</span>;</pre>
<pre class="alt"><span class="lnum">  15:  </span>&nbsp;</pre>
<pre><span class="lnum">  16:  </span>         <span class="kwrd">int</span> z = content.IndexOf(<span class="str">')'</span>, y);</pre>
<pre class="alt"><span class="lnum">  17:  </span>         <span class="kwrd">string</span>[] inputs = content.Substring(y &#43; 8 &#43; 1, z - (y &#43; 8) - 1).Split(chArr);</pre>
<pre><span class="lnum">  18:  </span>         <span class="kwrd">string</span> className = content.Substring(j &#43; 1, x - j - 1);</pre>
<pre class="alt"><span class="lnum">  19:  </span>         <span class="kwrd">string</span> member = content.Substring(x &#43; 10 &#43; 1, k - (x &#43; 10) - 1);</pre>
<pre><span class="lnum">  20:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  21:  </span>         JSObject jsObj = <span class="kwrd">null</span>;</pre>
<pre><span class="lnum">  22:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  23:  </span>         <span class="kwrd">if</span> (!jsObjs.TryGetValue(className, <span class="kwrd">out</span> jsObj))</pre>
<pre><span class="lnum">  24:  </span>         {</pre>
<pre class="alt"><span class="lnum">  25:  </span>            jsObj = <span class="kwrd">new</span> JSObject(className);</pre>
<pre><span class="lnum">  26:  </span>            jsObjs.Add(jsObj.Name, jsObj);</pre>
<pre class="alt"><span class="lnum">  27:  </span>         }</pre>
<pre><span class="lnum">  28:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  29:  </span>         JSOperation operation = jsObj.addOperation(member);</pre>
<pre><span class="lnum">  30:  </span>         </pre>
<pre class="alt"><span class="lnum">  31:  </span>         <span class="kwrd">for</span> (j = 0; j &lt; inputs.Length; j&#43;&#43;)</pre>
<pre><span class="lnum">  32:  </span>         {</pre>
<pre class="alt"><span class="lnum">  33:  </span>            <span class="kwrd">if</span> (inputs[j] != <span class="kwrd">string</span>.Empty)</pre>
<pre><span class="lnum">  34:  </span>               operation.addInput(inputs[j].Trim());</pre>
<pre class="alt"><span class="lnum">  35:  </span>         }</pre>
<pre><span class="lnum">  36:  </span>         </pre>
<pre class="alt"><span class="lnum">  37:  </span>         x = content.IndexOf(<span class="str">&quot;.prototype.&quot;</span>, x &#43; 1);</pre>
<pre><span class="lnum">  38:  </span>      }</pre>
</div>
<p></p>
<p></p>
<p>Below code finds out all the methods that could be potential outputs for any of the operation, if the operation is not returning primitive data type then most likely it could be one of these types. The problem with associating the output to the operation
 is to find out what the return type is, since JavaScript supports loosely typed data, no one can be sure of the output data type unless the code is executed. therefore this code at least looks up all the non-primitive data types that could be one of the output
 types. </p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>      x = content.IndexOf(<span class="str">&quot;this.&quot;</span>, 0);</pre>
<pre><span class="lnum">   2:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   3:  </span>      <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; x != -1; i&#43;&#43;)</pre>
<pre><span class="lnum">   4:  </span>      {</pre>
<pre class="alt"><span class="lnum">   5:  </span>          <span class="kwrd">int</span> j = 0;</pre>
<pre><span class="lnum">   6:  </span>          <span class="kwrd">int</span> k = 0;</pre>
<pre class="alt"><span class="lnum">   7:  </span>          <span class="kwrd">string</span> strObj = <span class="kwrd">string</span>.Empty;</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span>          <span class="kwrd">for</span> (j = x - 1; <span class="str">'{'</span> != content[j]; j--) { }</pre>
<pre><span class="lnum">  10:  </span>          </pre>
<pre class="alt"><span class="lnum">  11:  </span>          <span class="kwrd">for</span> (k = j - 1; ; k--)</pre>
<pre><span class="lnum">  12:  </span>          {</pre>
<pre class="alt"><span class="lnum">  13:  </span>              <span class="kwrd">string</span> fun = content.Substring(k, 8);</pre>
<pre><span class="lnum">  14:  </span>              </pre>
<pre class="alt"><span class="lnum">  15:  </span>              <span class="kwrd">if</span> (fun == <span class="str">&quot;function&quot;</span>)</pre>
<pre><span class="lnum">  16:  </span>                  <span class="kwrd">break</span>;</pre>
<pre class="alt"><span class="lnum">  17:  </span>          }</pre>
<pre><span class="lnum">  18:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  19:  </span>          <span class="kwrd">int</span> y = 0;</pre>
<pre><span class="lnum">  20:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  21:  </span>          <span class="kwrd">for</span> (y = k &#43; 8; content[y] != <span class="str">'('</span>; y&#43;&#43;) { }</pre>
<pre><span class="lnum">  22:  </span>                </pre>
<pre class="alt"><span class="lnum">  23:  </span>          <span class="kwrd">if</span> ((k &#43; 8) &lt; y)</pre>
<pre><span class="lnum">  24:  </span>          {              </pre>
<pre class="alt"><span class="lnum">  25:  </span>              <span class="kwrd">string</span> objName = content.Substring(k &#43; 9, y - (k &#43; 9));</pre>
<pre><span class="lnum">  26:  </span>              JSOutputFunction obj = <span class="kwrd">new</span> JSOutputFunction();</pre>
<pre class="alt"><span class="lnum">  27:  </span>              obj.ObjName = objName;</pre>
<pre><span class="lnum">  28:  </span>              <span class="kwrd">int</span> cnt = 1;</pre>
<pre class="alt"><span class="lnum">  29:  </span>                    </pre>
<pre><span class="lnum">  30:  </span>              <span class="kwrd">for</span> (k = j &#43; 1; cnt != 0; k&#43;&#43;)                    </pre>
<pre class="alt"><span class="lnum">  31:  </span>              {</pre>
<pre><span class="lnum">  32:  </span>                  <span class="kwrd">if</span> (content[k] == <span class="str">'{'</span>)</pre>
<pre class="alt"><span class="lnum">  33:  </span>                      cnt&#43;&#43;;</pre>
<pre><span class="lnum">  34:  </span>                        </pre>
<pre class="alt"><span class="lnum">  35:  </span>                  <span class="kwrd">if</span> (content[k] == <span class="str">'}'</span>)</pre>
<pre><span class="lnum">  36:  </span>                      cnt--;</pre>
<pre class="alt"><span class="lnum">  37:  </span>              }</pre>
<pre><span class="lnum">  38:  </span>                    </pre>
<pre class="alt"><span class="lnum">  39:  </span>              strObj = content.Substring(j, k - j);</pre>
<pre><span class="lnum">  40:  </span>              k = strObj.IndexOf(<span class="str">&quot;this.&quot;</span>, 0);</pre>
<pre class="alt"><span class="lnum">  41:  </span>                    </pre>
<pre><span class="lnum">  42:  </span>              <span class="kwrd">for</span> (; k != -1; )</pre>
<pre class="alt"><span class="lnum">  43:  </span>              {</pre>
<pre><span class="lnum">  44:  </span>                  y = strObj.IndexOfAny(ch, k &#43; 1);</pre>
<pre class="alt"><span class="lnum">  45:  </span>                  <span class="kwrd">string</span> fieldName = strObj.Substring(k &#43; 5, y - k - 5);</pre>
<pre><span class="lnum">  46:  </span>                        </pre>
<pre class="alt"><span class="lnum">  47:  </span>                  <span class="kwrd">if</span> (fieldName.Trim() != <span class="str">&quot;toString&quot;</span>)</pre>
<pre><span class="lnum">  48:  </span>                      obj.AddField(fieldName.Trim());                        </pre>
<pre class="alt"><span class="lnum">  49:  </span>              </pre>
<pre><span class="lnum">  50:  </span>                  k = strObj.IndexOf(<span class="str">&quot;this.&quot;</span>, k &#43; 1);</pre>
<pre class="alt"><span class="lnum">  51:  </span>              }</pre>
<pre><span class="lnum">  52:  </span>              outputUtil.AddOutput(obj);</pre>
<pre class="alt"><span class="lnum">  53:  </span>          }</pre>
<pre><span class="lnum">  54:  </span>          <span class="kwrd">else</span></pre>
<pre class="alt"><span class="lnum">  55:  </span>              j = x &#43; 1;          </pre>
<pre><span class="lnum">  56:  </span>          x = content.IndexOf(<span class="str">&quot;this.&quot;</span>, j &#43; strObj.Length);</pre>
<pre class="alt"><span class="lnum">  57:  </span>      }</pre>
</div>
<style type="text/css">
<!--
.csharpcode, .csharpcode 
	{font-size:small;
	color:black;
	font-family:consolas,"Courier New",courier,monospace;
	background-color:#ffffff}
.csharpcode 
	{margin:0em}
.csharpcode .rem
	{color:#008000}
.csharpcode .kwrd
	{color:#0000ff}
.csharpcode .str
	{color:#006080}
.csharpcode .op
	{color:#0000c0}
.csharpcode .preproc
	{color:#cc6633}
.csharpcode .asp
	{background-color:#ffff00}
.csharpcode .html
	{color:#800000}
.csharpcode .attr
	{color:#ff0000}
.csharpcode .alt
	{background-color:#f4f4f4;
	width:100%;
	margin:0em}
.csharpcode .lnum
	{color:#606060}
-->
</style>
<p>After running the application, it would look something like this: </p>
<p><a href="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/4073227/JSRef.jpg"><img height="220" alt="JSRef" src="http://ecn.channel9.msdn.com/o9/c4fcontent/migration/4073227/JSRef_thumb.jpg" width="536" border="0"></a>
</p>
<p>I am currently working as a Program Manager at Microsoft. </p>
<p>If I missed something or you have any questions, please email me. If you have any feedback or comments please do write I would definitely appreciate it.
</p>
<p></p>
<p>Shreyans Kothari (MCAD)<br>
Program Manager <br>
Aditi @ Microsoft <br>
<a href="mailto:a-shreyk@microsoft.com?subject=Feedback for JavaScript Reflection">Feedback/Comments</a>
</p>
<p></p>
<div class="wlWriterSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:819db524-fdf8-4932-add1-ac514cae0d2d">
C4F Tags: <a href="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx" rel="tag">
Web</a> <a href="http://blogs.msdn.com/coding4fun/archive/tags/windows/default.aspx" rel="tag">
Windows</a> <a href="http://blogs.msdn.com/coding4fun/archive/tags/utility/default.aspx" rel="tag">
Utility</a> , <a href="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx" rel="tag">
Web</a> <a href="http://blogs.msdn.com/coding4fun/archive/tags/windows/default.aspx" rel="tag">
Windows</a> <a href="http://blogs.msdn.com/coding4fun/archive/tags/utility/default.aspx" rel="tag">
Utility</a> , <a href="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx" rel="tag">
Web</a> <a href="http://blogs.msdn.com/coding4fun/archive/tags/windows/default.aspx" rel="tag">
Windows</a> <a href="http://blogs.msdn.com/coding4fun/archive/tags/utility/default.aspx" rel="tag">
Utility</a> </div>
 <img src="http://m.webtrends.com/dcs1wotjh10000w0irc493s0e_6x1g/njs.gif?dcssip=channel9.msdn.com&dcsuri=http://channel9.msdn.com/Niners/c4f.Shreyans-Kothari/Posts/RSS&WT.dl=0&WT.entryid=Entry:RSSView:975cb9856e524686a2f99e7600d30707">]]></description>
      <comments>http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection</comments>
      <itunes:summary>




I was writing an application for creating a manifest for JavaScript. I was unable to do it using DOM; instead I wrote my own application for parsing JavaScript files. It is little tough to peek into JavaScript objects, initial
 I could only get the members and fields using for-in. but that was not enough, what if someone stored a file locally and I have to write code to execute it, I would be able to get only the members, just executing members without passing the required input
 parameter if any will not work. The code below will parse the file and give me the options as to what input is required and if something is returned what it could possibly be returning except primitive types. Knowing these things in advance makes it easier
 to execute the code. Well it worked for me; let&#39;s see how it works for you. I know many people are looking for something like this.




Shreyans Kothari
My Space

Difficulty: Intermediate
Time Required: 
1-3 hours
Cost: Free
Software: 
Hardware: 
Download: Download








The code might not be perfect or might not do everything for you, since I wrote to cater my needs. But the basic utilities are implemented, you can add thing according to your needs. I am using the code from my project, therefore you will not see lot of
 other code that the project refers to, but you can input you JavaScript file to see the results or use the sample file that&#39;s provided in the project.

Some assumptions that I made; it will only look for &#39;prototype&#39; and &#39;this&#39; keywords in the JS code.
 
The core code: 
This code finds all the objects, their members and inputs for each member:
 

   1:        x = content.IndexOf(&amp;quot;.prototype.&amp;quot;, x);
   2:  &amp;nbsp;
   3:        for (int i = 0; x != -1; i&amp;#43;&amp;#43;)
   4:        {
   5:           int j = 0;
   6:           int k = 0;
   7:  &amp;nbsp;
   8:           for (j = x - 1; (&#39; &#39; != content[j] &amp;amp;&amp;amp; &#39;\n&#39; != content[j]); j--) { }
   9:           for (k = x &amp;#43; 10; (&#39; &#39; != content[k] &amp;amp;&amp;amp; &#39;=&#39; !=</itunes:summary>
      <link>http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection</link>
      <pubDate>Thu, 26 Jul 2007 22:36:11 GMT</pubDate>
      <guid isPermaLink="false">http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection</guid>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/4073227_100.jpg" height="75" width="100"></media:thumbnail>
      <media:thumbnail url="http://ecn.channel9.msdn.com/o9/c4f/images/4073227_220.jpg" height="165" width="220"></media:thumbnail>      
      <dc:creator>Shreyans Kothari</dc:creator>
      <itunes:author>Shreyans Kothari</itunes:author>
      <slash:comments>3</slash:comments>
      <wfw:commentRss>http://channel9.msdn.com/coding4fun/articles/JavaScript-Reflection/RSS</wfw:commentRss>
    </item>    
</channel>
</rss>