title: Javascript code modules mutual import [Firefox addon]
body: <p>I came across this strange behavior while using javascript code modules for a firefox addon. I'm not sure if this is a bug or a bad design doing mutual imports.</p>
  
  <p>So let's say there are 3 modules a, b and c. </p>
  
  <p>a.js</p>
  
  <pre><code>var EXPORTED_SYMBOLS = ["a"];                                                       
  Components.utils.import("resource://mymodule/c.js");                                 
  Components.utils.import("resource://mymodule/b.js");                                 
  
  var a = {                                                                           
      init: function() {                                                              
          dump("A init\n");                                                           
          b.init();                                                                   
      }                                                                               
  };     
  </code></pre>
  
  <p>b.js</p>
  
  <pre><code>var EXPORTED_SYMBOLS = ["b"];
  Components.utils.import("resource://mymodule/c.js");
  
  var b = {
      init : function() {
          try {
              dump("B init\n");
              dump(c.foo() + "\n");
          } catch (e) {
              dump("Error C init : " + e.message + "\n");
          }
      }
  };
  </code></pre>
  
  <p>c.js </p>
  
  <pre><code>var EXPORTED_SYMBOLS = ["c"];
  Components.utils.import("resource://mymodule/b.js");
  
  var c = {
      foo : function() {
          return "C Foo";
      },
  };
  </code></pre>
  
  <p>a.init() is called externally. Now with the above code, I hit an undefined for 'c' from b.</p>
  
  <pre><code>A init
  B init
  Error C init : c is undefined
  </code></pre>
  
  <p>After some troubleshooting, I realized that to correct this, </p>
  
  <ul>
  <li>I can either swap the imports inside a.js (b is imported before c)</li>
  <li>Or I can remove the mutual import ( remove the import of b from within c)
  With either of these, things go fine.</li>
  </ul>
  
  <p>In my actual code b and c represent some UI related stuff and they have mutual dependencies. I can totally get rid of a  mutual import of modules, and register a callback function for one of them. But I wish to know what is causing this behavior. As far as I understand <a href="https://developer.mozilla.org/en-US/docs/Components.utils.import" rel="nofollow">the documentation</a> do not have any strict guidelines for import among modules. I am also aware that <a href="https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Using?redirectlocale=en-US&amp;redirectslug=JavaScript_code_modules%2FUsing#Sharing_objects_using_code_modules" rel="nofollow">one module when imported multiple times will be shared</a> because of caching of modules. But somehow can't explain this. What am I doing wrong here ?</p>