Nested modules clarification

David Herman dherman at mozilla.com
Fri Jul 20 23:09:09 PDT 2012


On Jul 20, 2012, at 2:01 PM, ๏̯͡๏ Jasvir Nagra wrote:

> 1. What is part of the fresh module scope?  Sam has clarified that it closes over lexically scoped variables and Dave has clarified that no variables are closed over in the external case.  What else is included?  Can an external module utter window?  In a browser, do you expect it to be able to utter document, getComputedStyle, location?  Can modules side-effect these globals or is that verboten and the only expected way for well-behaved modules to return values is via export?

Internal modules (i.e., modules declared via `module x { ... }`) inherit the entire lexical environment.

External modules inherit only the global scope of their loader, i.e., the global object. In the browser, that means external modules loaded via the standard loader have full access to window, document, etc. in scope.

An external file can dynamically modify the window object however it wants, but it's in strict mode so unable to assign to unbound variables.

Moreover, with static variable checking of all code inside of modules, even if code dynamically adds a new property to the window, that variable is not available at compile time and rejected as unbound. For example, this is an early error:

    <script>module m { window.foo = 12; console.log(foo) }</script>

> 2. Given the semantic hazard possible in inlining, is it worth tweaking the proposal to address it or is the expectation that to really do code consolidation into a single file from separate modules, modules require something more sophisticated that a braindead inliner?

Erik's approach isn't clever, it's necessary. The braindead inliner you are talking about would wreak havoc if more than one part of the program binds the external file as a local module. You'd end up duplicating that module's contents in multiple places in the program! The point of having a single instance of each external module is to avoid multiply instantiating it even when it's used in multiple places. So an inliner would have to respect that.

Dave



More information about the es-discuss mailing list