Modules/1.0
From CommonJS Spec Wiki
< Modules(Redirected from Modules/SecurableModules)
STATUS: SUPERSEDED BY 1.1
- Implementations
- [[Implementations/Flusspferd|]], [[Implementations/GLUEscript|]], [[Implementations/GPSEE|]], [[Implementations/JSBuild|]], [[Implementations/Narwhal|]] (0.1), [[Implementations/Persevere|]], [[Implementations/RingoJS|]], [[Implementations/SproutCore|]], [[Implementations/node.js|]], [[Implementations/TeaJS|]], [[Implementations/CouchDB|]], [[Implementations/Smart|]], [[Implementations/Yabble|]], [[Implementations/Wakanda|]], [[Implementations/XULJet|]]
This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API. By implication, this specification defines the minimum features that a module system must provide in order to support interoperable modules.
Contents
Contract
Module Context
- In a module, there is a free variable "require", that is a function.
- The "require" function accepts a module identifier.
- "require" returns the exported API of the foreign module.
- If there is a dependency cycle, the foreign module may not have finished executing at the time it is required by one of its transitive dependencies; in this case, the object returned by "require" must contain at least the exports that the foreign module has prepared before the call to require that led to the current module's execution.
- If the requested module cannot be returned, "require" must throw an error.
- In a module, there is a free variable called "exports", that is an object that the module may add its API to as it executes.
- modules must use the "exports" object as the only means of exporting.
Module Identifiers
- A module identifier is a String of "terms" delimited by forward slashes.
- A term must be a camelCase identifier, ".", or "..".
- Module identifiers may not have file-name extensions like ".js".
- Module identifiers may be "relative" or "top-level". A module identifier is "relative" if the first term is "." or "..".
- Top-level identifiers are resolved off the conceptual module name space root.
- Relative identifiers are resolved relative to the identifier of the module in which "require" is written and called.
Unspecified
This specification leaves the following important points of interoperability unspecified:
- Whether modules are stored with a database, file system, or factory functions, or are interchangeable with link libraries.
- Whether a PATH is supported by the module loader for resolving module identifiers.
Unit Tests
- Unit Tests at Google Code by Kris Kowal
- Unit Tests Git Mirror by Ash Berlin
Sample Code
- math.js
exports.add = function() { var sum = 0, i = 0, args = arguments, l = args.length; while (i < l) { sum += args[i++]; } return sum; };
- increment.js
var add = require('math').add; exports.increment = function(val) { return add(val, 1); };
- program.js
var inc = require('increment').increment; var a = 1; inc(a); // 2
Notes
- Secure Modules
- Notes on compiling modules for browsers
- On writing modules that also work as <script>s
Amendment Proposals
Implementations
Passing all compliance tests:
- Flusspferd (Spidermonkey / C++) http://flusspferd.org/
- Ondrej Zara's v8cgi for V8 http://code.google.com/p/v8cgi/
- Tom Robinson's narwhal for Rhino http://github.com/tlrobinson/narwhal/
- Wes Garland's GPSEE for Spidermonkey http://code.google.com/p/gpsee/
- Kris Kowal's chiron for browsers http://github.com/kriskowal/chiron/
- Hannes Wallnoefer's RingoJS for Rhino at http://github.com/ringo/ringojs
- Ben Collver's Trogl for v8 at http://bencollver.googlepages.com/trogl-1.zip
- Atul Varma's Pydertron for Python/Spidermonkey at http://hg.toolness.com/pydertron/raw-file/tip/docs.html
- Daniel Friesen's MonkeyScript Lite for Rhino
- Irakli Gozalishvili's narwhal for XULRunner http://github.com/gozala/narwhal-xulrunner/
- James Brantly's Yabble for browsers http://github.com/jbrantly/yabble
- 4D's Wakanda (JavaScriptCore/SquirrelFish) http://www.wakandasoft.com
Fully implemented:
- Kris Zyp's Persevere for Rhino at http://www.persvr.org/
In development:
- nathan smith is implementing this for JScript and ASP at http://github.com/smith/interoperablejscript/tree/master
- mob is implementing this in Ejscript http://www.ejscript.org
- pmuellr posted a sample loader http://wiki.github.com/pmuellr/modjewel
Related Documents
- Proposal to ECMA TC39: Module System for ES-Harmony
- Presentation to ECMA TC39: Modules