{"id":9735,"date":"2013-09-19T10:06:00","date_gmt":"2013-09-19T10:06:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2013\/09\/19\/just-my-code-for-javascript-windows-store-apps-in-visual-studio-2013\/"},"modified":"2022-08-02T00:00:42","modified_gmt":"2022-08-02T08:00:42","slug":"just-my-code-for-javascript-windows-store-apps-in-visual-studio-2013-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/just-my-code-for-javascript-windows-store-apps-in-visual-studio-2013-2\/","title":{"rendered":"Just My Code for JavaScript Windows Store Apps in Visual Studio 2013"},"content":{"rendered":"<p>If you\u2019ve ever debugged a JavaScript Windows Store app there\u2019s a very good chance that you accidentally ended up in either \u201cFunction code [dynamic]\u201d or \u201cbase.js\u201d without knowing how you got there and with no clear way to get back out. To help address this, we\u2019ve introduced a feature called \u201cJust My Code\u201d (JMC) for JavaScript in Windows Store apps which is now available in Visual Studio 2013.<\/p>\n<p>Those who have debugged C# or Visual Basic code in Visual Studio have likely already benefitted from this debugger feature that helps you focus on your code and abstracts the implementation details of the underlying framework. To better understand how JMC works we\u2019ll walk through a very simple example, then discuss some of the finer points of how it behaves, and finally I\u2019ll show you how you can customize the behavior.<\/p>\n<h2>Why Just My Code<\/h2>\n<p>To start, I\u2019ll demonstrate the behavior when Just My Code is disabled. To this I\u2019ll create a new JavaScript Windows Store Blank app called App1. Then inside the activated event handler I\u2019ll add the following code snippet to try to open a file from the pictures folder (also adding the \u201cPictures Folder\u201d capability to the app\u2019s manifest)<\/p>\n<pre style=\"font-family: Consolas\">\/\/ TODO: This application has been newly launched. Initialize\n\/\/ your application here.                \nWindows.Storage.KnownFolders.picturesLibrary.getFileAsync(\"doesNotExist.png\").then(\n    function (file) {  \/\/handle the success case\n        return file.name + \" was opened successfully\";;\n    },\n    function (e) {  \/\/handle the error case\n        return e.message;\n    }).done(function (message) {   \/\/ run after either success or error\n        var result = message;\n    });\n<\/pre>\n<p>Next I put breakpoints inside the success and error functions, and when I start debugging I\u2019ll hit the breakpoint in the error function.<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/5621.image_thumb_3F4D50EC.png\" alt=\"image\" width=\"697\" height=\"177\" border=\"0\" \/><\/p>\n<p>Now what I want do to is step to the \u201cvar result = message;\u201d line in the .done() function. So I press F10 (Step Over) twice and all of the sudden I\u2019m in code I don\u2019t recognize in a document titled \u201cFunction code [dynamic]\u201d<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/3808.image_thumb_3AD6D025.png\" alt=\"image\" width=\"710\" height=\"245\" border=\"0\" \/><\/p>\n<p>In an effort to get back where I came from and just put a breakpoint on the line I intended to get to, I look at the Call Stack window. Here I see an incredibly long call stack with code that is mostly framework code.<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/5141.image_thumb_2F4112E6.png\" alt=\"image\" width=\"358\" height=\"626\" border=\"0\" \/><\/p>\n<h2>Just My Code at Work<\/h2>\n<p>Now let\u2019s try the same scenario only this time with Just My Code enabled (which it is by default in VS2013). I\u2019ll start debugging again and I hit the same breakpoint in the error handling function and press F10 twice, this takes me out of the error handling function instead of into \u201cFunction code [dynamic]\u201d<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/8228.image_thumb_55A2F631.png\" alt=\"image\" width=\"587\" height=\"135\" border=\"0\" \/><\/p>\n<p>A single press of F11 (Step Into) then lands me in the .done() function as I would expect<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/4431.image_thumb_3830A525.png\" alt=\"image\" width=\"597\" height=\"137\" border=\"0\" \/><\/p>\n<p>Also when I look at the Call Stack window I can see that all of the frames in \u201cFunction code\u201d and base.js have been collapsed into the \u201c[External Code]\u201d meta-frame so I only see frames for code that I control.<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/4846.image_thumb_1EC8A1EB.png\" alt=\"image\" width=\"369\" height=\"256\" border=\"0\" \/><\/p>\n<h2>Just My Code Details<\/h2>\n<p>As we saw in the simple example above, Just My Code helps developers focus on their code while debugging. Now let\u2019s dig into some of the details of how exactly Just My Code will behave when debugging.<\/p>\n<h2>What is My Code?<\/h2>\n<p>The first important thing to understand about Just My Code is what exactly is considered my code (also referred to as \u201cuser code\u201d). When JMC is enabled, code is classified into three categories:<\/p>\n<ol>\n<li>**My Code: **Code that you own and control****<\/li>\n<li>**Libraries: **Is intended for libraries that you use regularly and your application relies on to function correctly (for example WinJS or jQuery). ****<\/li>\n<li>**Unrelated: **Is intended for code that that may be running in your application, but you don\u2019t own and your application doesn\u2019t directly rely on it to function correctly (e.g. an advertising SDK that displays ads)****<\/li>\n<\/ol>\n<p>By default in Windows Store apps, the following code is considered to be \u201cLibrary\u201d code:<\/p>\n<ul>\n<li>Any file that is in a framework reference, for example files from the WinJS library<\/li>\n<li>Any file that matches \u201c*.min.js\u201d as part of the name<\/li>\n<li>Code created by passing a string into the \u201cFunction() constructor()<\/li>\n<\/ul>\n<p>Finally in Windows Store apps, any code that is loaded into your app from an HTTP or HTTPS URI is considered \u201cUnrelated\u201d. All other code is considered user code. Later we\u2019ll look at how to configure this behavior.<\/p>\n<h2>Debugging Behavior Details<\/h2>\n<p>In the example above, we saw how JMC affects debugger stepping by keeping it in my code and the call stack filtering. Now we\u2019ll discuss how JMC will affect other debugging behaviors.<\/p>\n<h3>Stepping<\/h3>\n<p>Just My Code has the following effects on stepping behavior:<\/p>\n<ul>\n<li>\u201cStep Into\u201d will be treated as \u201cStep Over\u201d if tried on a function that isn\u2019t my code<\/li>\n<li>If a step begins in non-user code, this could occur for example if you set and hit a breakpoint in Library code, then stepping temporarily behaves as if JMC is not enabled. As soon as you are back in user code, stepping will behave like JMC is enabled once again<\/li>\n<li>\n<p>When a step results in leaving the current execution context (e.g. doing a step on the last line of an event handler) the next time the debugger stops is the next time a line of use code executes (i.e. if a callback executes in Library code the debugger will not stop).<\/p>\n<\/li>\n<li>\n<p>The exception to this as described above is if the step was performed in non-user code then the debugger will stop on the next line of code executed regardless of its JMC classification<\/p>\n<\/li>\n<li>\n<p>\u201cStep Out\u201d will stop on the next line of user code. If no user code is encountered then this becomes equivalent to a continue operation.<\/p>\n<\/li>\n<\/ul>\n<h3>Exceptions<\/h3>\n<p>Just My Code also affects how the debugger will behave when an exception is thrown.<\/p>\n<ul>\n<li>\n<p>If an unhandled exception occurs in:<\/p>\n<\/li>\n<li>\n<p>User or Library code the debugger will break<\/p>\n<\/li>\n<li>\n<p>Unrelated code and:<\/p>\n<\/li>\n<li>\n<p>User or Library code is on the call stack, the debugger will break<\/p>\n<\/li>\n<li>\n<p>No User or library code is on the call stack. The debugger will not break.<\/p>\n<\/li>\n<li>\n<p>If <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/d14azbfh(v=vs.120).aspx\">first chance exceptions are enabled<\/a>, and an exception is thrown in:<\/p>\n<\/li>\n<li>Library or Unrelated code and handled the debugger will not break (e.g. jQuery will throw and handle exceptions when doing browser detection logic)<\/li>\n<li>Library or Unrelated code but not handled before propagating back to user code, the debugger will break.<\/li>\n<\/ul>\n<p>To see this in practice let\u2019s look at an example. For the purpose of this example I placed the following code in a file called \u201cnotMyLibrary.min.js\u201d so it will be considered library code by default.<\/p>\n<pre style=\"font-family: Consolas\">var NotMyLibrary = function () { }\nNotMyLibrary.appendElementToBody = function (element){\n    document.body.appendChild(element);\n}\n<\/pre>\n<p>Now in in my default.js file I\u2019ll call<\/p>\n<pre style=\"font-family: Consolas\">NotMyLibrary.appendElementToBody(document.body);<\/pre>\n<p>When I start debugging, the debugger breaks in default.js where I made this call but this time it highlights the line green instead of yellow. The green indicates that it has taken me to the last place in my code that is on the call stack which caused the exception even though the exception was not thrown in my code. The Call Stack window shows me the same thing. I can see that external code is on the call stack above my code, but the green arrow points to my code that called into the external code that resulted in the unhandled exception.<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/2308.image_thumb_1332E4AC.png\" alt=\"image\" width=\"660\" height=\"299\" border=\"0\" \/><\/p>\n<h3>Breakpoints<\/h3>\n<p>Finally breakpoint behavior is as follows:<\/p>\n<ul>\n<li>Breakpoints that have been set in any code will always be hit regardless of the JMC classification of that code<\/li>\n<li>\n<p>If the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ie\/0bwt76sk(v=vs.94).aspx\"><em>debugger<\/em><\/a> keyword is encountered in:<\/p>\n<\/li>\n<li>\n<p>Library code. The debugger stops in that library code.<\/p>\n<\/li>\n<li>Unrelated code The debugger will not stop<\/li>\n<\/ul>\n<h3>Call Stack<\/h3>\n<p>The example in the beginning of the post showed how Just My Code collapses non-user code on the call stack into [External Code]. If I want to temporarily see the full call stack without the need to disable Just My Code, I can right click in the call stack window and choose \u201cShow External Code\u201d<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/7587.image_thumb_55A586E2.png\" alt=\"image\" width=\"558\" height=\"295\" border=\"0\" \/><\/p>\n<ul>\n<li>This setting remains set on the call stack window until I manually change it<\/li>\n<li>It won\u2019t disable Just My Code, meaning none of the stepping, exception, or breakpoint behaviors will be affected.<\/li>\n<\/ul>\n<h2>Turning Just My Code On or Off<\/h2>\n<p>The good news is that JMC is on by default, but it can be turned on or off by going to Tools -> Options -> Debugging and checking or unchecking the \u201cEnable Just My Code\u201d checkbox (this checkbox governs the behavior for .NET and C++ Just My Code as well).<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/6014.image_thumb_007DEAF5.png\" alt=\"image\" width=\"647\" height=\"379\" border=\"0\" \/><\/p>\n<h2>Customizing Just My Code<\/h2>\n<p>You may have noticed that when describing what is considered my code, I used the term \u201cby default\u201d several times. This is because we have included the ability to customize what is considered my code and what is not. This can be done by adding a file named \u201cmycode.json\u201d to the root of the project.<\/p>\n<p><img decoding=\"async\" style=\"border-width: 0px\" title=\"image\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2013\/09\/0652.image_thumb_274C0135.png\" alt=\"image\" width=\"381\" height=\"384\" border=\"0\" \/><\/p>\n<p>This .json file has three sections, (MyCode, Libraries, and Unrelated) that allow you to override the default behavior by manually categorizing code for that project. If you are interested the default file is located at <span style=\"font-family: Consolas\">%ProgramFiles(X86)%Microsoft Visual Studio 12.0JavaScriptJustMyCodemycode.default.wwa.json<\/span> and has the following contents:<\/p>\n<pre style=\"font-family: Consolas\">{\n    \"Eval\" : \"MyCode\",\n    \"Function\" : \"Library\",\n    \"ScriptBlock\" : \"Unrelated\",\n    \"MyCode\" : [\n    ],\n    \"Libraries\" : [\n        \"*.min.js\"\n    ],\n    \"Unrelated\" : [\n    ]\n}\n<\/pre>\n<p>In the above file, the top three properties &#8220;Eval&#8221;, &#8220;Function&#8221;, and &#8220;ScriptBlock&#8221; are how you want that type of code to be classified, where the options are &#8220;MyCode&#8221;, &#8220;Library&#8221;, or &#8220;Unrelated&#8221;<\/p>\n<ul>\n<li><strong>Eval<\/strong> is code created\u00a0using &#8220;eval()&#8221;<\/li>\n<li><strong>Function<\/strong> represents dynamic functions<\/li>\n<li>**ScriptBlock **is code defined in <Script> blocks<\/li>\n<\/ul>\n<p>The second three categories of &#8220;MyCode&#8221;, &#8220;Libraries&#8221;, and &#8220;Unrelated&#8221; are where you list the names of files you want to be included in that category.\u00a0 Notice that wild card characters are supported; so\u00a0the file above any file name that matches *.min.js will be considered Library code (matching is not case sensitive).\u00a0<\/p>\n<p>It\u2019s also worth noting that the presence of a mycode.json file does not automatically override the default JMC behaviors. For example, if I want <em>.min.js files to be considered my code I need to add \u2018<\/em>.min.js\u2019 to the \u201cMyCode\u201d section of mycode.json to override the default behavior.<\/p>\n<pre style=\"font-family: Consolas\">\"MyCode\": [\n    '*.min.js'\n    ],\n<\/pre>\n<h3>In Closing<\/h3>\n<p>In this post I walked through the newly added Just My Code feature for Windows Store apps in Visual Studio 2013. Please try the feature out and let us know how it works for you. If you have any questions or comments please let us know by either commenting below or posting in our <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/vsdebug\/threads\">MSDN forum<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve ever debugged a JavaScript Windows Store app there\u2019s a very good chance that you accidentally ended up in either \u201cFunction code [dynamic]\u201d or \u201cbase.js\u201d without knowing how you got there and with no clear way to get back out. To help address this, we\u2019ve introduced a feature called \u201cJust My Code\u201d (JMC) for [&hellip;]<\/p>\n","protected":false},"author":68,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,225],"tags":[],"class_list":["post-9735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-git"],"acf":[],"blog_post_summary":"<p>If you\u2019ve ever debugged a JavaScript Windows Store app there\u2019s a very good chance that you accidentally ended up in either \u201cFunction code [dynamic]\u201d or \u201cbase.js\u201d without knowing how you got there and with no clear way to get back out. To help address this, we\u2019ve introduced a feature called \u201cJust My Code\u201d (JMC) for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9735","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=9735"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9735\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=9735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=9735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=9735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}