| 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| |
| 18 // - GetNative(nativeName): returns the named native object | 19 // - GetNative(nativeName): returns the named native object |
| 19 (function(bootstrap) { | 20 (function(bootstrap) { |
| 20 | 21 |
| 22 function wrap(source) { |
| 23 return "(function(require, requireNative, exports) {" + source + |
| 24 "\n})"; |
| 25 } |
| 26 |
| 21 function ModuleLoader() { | 27 function ModuleLoader() { |
| 22 this.cache_ = {}; | 28 this.cache_ = {}; |
| 23 }; | 29 }; |
| 24 | 30 |
| 25 ModuleLoader.prototype.run = function(id) { | 31 ModuleLoader.prototype.run = function(id) { |
| 26 var source = bootstrap.GetSource(id); | 32 var source = bootstrap.GetSource(id); |
| 27 if (!source) { | 33 if (!source) { |
| 28 return null; | 34 return null; |
| 29 } | 35 } |
| 36 var wrappedSource = wrap(source); |
| 37 var f = bootstrap.Run(wrappedSource); |
| 30 var exports = {}; | 38 var exports = {}; |
| 31 var f = new Function("require", "requireNative", "exports", source); | |
| 32 f(require, requireNative, exports); | 39 f(require, requireNative, exports); |
| 33 return exports; | 40 return exports; |
| 34 }; | 41 }; |
| 35 | 42 |
| 36 ModuleLoader.prototype.load = function(id) { | 43 ModuleLoader.prototype.load = function(id) { |
| 37 var exports = this.cache_[id]; | 44 var exports = this.cache_[id]; |
| 38 if (exports) { | 45 if (exports) { |
| 39 return exports; | 46 return exports; |
| 40 } | 47 } |
| 41 exports = this.run(id); | 48 exports = this.run(id); |
| 42 this.cache_[id] = exports; | 49 this.cache_[id] = exports; |
| 43 return exports; | 50 return exports; |
| 44 }; | 51 }; |
| 45 | 52 |
| 46 var moduleLoader = new ModuleLoader(); | 53 var moduleLoader = new ModuleLoader(); |
| 47 | 54 |
| 48 function requireNative(nativeName) { | 55 function requireNative(nativeName) { |
| 49 var result = bootstrap.GetNative(nativeName); | 56 return bootstrap.GetNative(nativeName); |
| 50 if (!result) { | |
| 51 throw "No such native object: '" + nativeName + "'"; | |
| 52 } | |
| 53 return result; | |
| 54 } | 57 } |
| 55 | 58 |
| 56 function require(moduleId) { | 59 function require(moduleId) { |
| 57 return moduleLoader.load(moduleId); | 60 return moduleLoader.load(moduleId); |
| 58 } | 61 } |
| 59 | 62 |
| 60 return require; | 63 return require; |
| 61 }); | 64 }); |
| OLD | NEW |