| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file should define a function that returns a require() method that can | 5 // This file should define a function that returns a require() method that can |
| 6 // be used to load JS modules. | 6 // be used to load JS modules. |
| 7 // | 7 // |
| 8 // A module works the same as modules in node.js - JS script that populates | 8 // A module works the same as modules in node.js - JS script that populates |
| 9 // an exports object, which is what require()ers get returned. The JS in a | 9 // an exports object, which is what require()ers get returned. The JS in a |
| 10 // module will be run at most once as the exports result is cached. | 10 // module will be run at most once as the exports result is cached. |
| 11 // | 11 // |
| 12 // Modules have access to the require() function which can be used to load | 12 // Modules have access to the require() function which can be used to load |
| 13 // other JS dependencies, and requireNative() which returns objects that | 13 // other JS dependencies, and requireNative() which returns objects that |
| 14 // define native handlers. | 14 // define native handlers. |
| 15 // | 15 // |
| 16 // |bootstrap| contains the following fields: | 16 // |bootstrap| contains the following fields: |
| 17 // - GetSource(module): returns the source code for |module| | 17 // - GetSource(module): returns the source code for |module| |
| 18 // - Run(source): runs the string os JS |source| | |
| 19 // - GetNative(nativeName): returns the named native object | 18 // - GetNative(nativeName): returns the named native object |
| 20 (function(bootstrap) { | 19 (function(bootstrap) { |
| 21 | 20 |
| 22 function wrap(source) { | |
| 23 return "(function(require, requireNative, exports) {" + source + | |
| 24 "\n})"; | |
| 25 } | |
| 26 | |
| 27 function ModuleLoader() { | 21 function ModuleLoader() { |
| 28 this.cache_ = {}; | 22 this.cache_ = {}; |
| 29 }; | 23 }; |
| 30 | 24 |
| 31 ModuleLoader.prototype.run = function(id) { | 25 ModuleLoader.prototype.run = function(id) { |
| 32 var source = bootstrap.GetSource(id); | 26 var source = bootstrap.GetSource(id); |
| 33 if (!source) { | 27 if (!source) { |
| 34 return null; | 28 return null; |
| 35 } | 29 } |
| 36 var wrappedSource = wrap(source); | |
| 37 var f = bootstrap.Run(wrappedSource); | |
| 38 var exports = {}; | 30 var exports = {}; |
| 31 var f = new Function("require", "requireNative", "exports", source); |
| 39 f(require, requireNative, exports); | 32 f(require, requireNative, exports); |
| 40 return exports; | 33 return exports; |
| 41 }; | 34 }; |
| 42 | 35 |
| 43 ModuleLoader.prototype.load = function(id) { | 36 ModuleLoader.prototype.load = function(id) { |
| 44 var exports = this.cache_[id]; | 37 var exports = this.cache_[id]; |
| 45 if (exports) { | 38 if (exports) { |
| 46 return exports; | 39 return exports; |
| 47 } | 40 } |
| 48 exports = this.run(id); | 41 exports = this.run(id); |
| 49 this.cache_[id] = exports; | 42 this.cache_[id] = exports; |
| 50 return exports; | 43 return exports; |
| 51 }; | 44 }; |
| 52 | 45 |
| 53 var moduleLoader = new ModuleLoader(); | 46 var moduleLoader = new ModuleLoader(); |
| 54 | 47 |
| 55 function requireNative(nativeName) { | 48 function requireNative(nativeName) { |
| 56 return bootstrap.GetNative(nativeName); | 49 var result = bootstrap.GetNative(nativeName); |
| 50 if (!result) { |
| 51 throw "No such native object: '" + nativeName + "'"; |
| 52 } |
| 53 return result; |
| 57 } | 54 } |
| 58 | 55 |
| 59 function require(moduleId) { | 56 function require(moduleId) { |
| 60 return moduleLoader.load(moduleId); | 57 return moduleLoader.load(moduleId); |
| 61 } | 58 } |
| 62 | 59 |
| 63 return require; | 60 return require; |
| 64 }); | 61 }); |
| OLD | NEW |