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. |
(...skipping 15 matching lines...) Expand all Loading... | |
26 | 26 |
27 function ModuleLoader() { | 27 function ModuleLoader() { |
28 this.cache_ = {}; | 28 this.cache_ = {}; |
29 }; | 29 }; |
30 | 30 |
31 ModuleLoader.prototype.run = function(id) { | 31 ModuleLoader.prototype.run = function(id) { |
32 var source = bootstrap.GetSource(id); | 32 var source = bootstrap.GetSource(id); |
33 if (!source) { | 33 if (!source) { |
34 return null; | 34 return null; |
35 } | 35 } |
36 var wrappedSource = wrap(source); | 36 var wrappedSource = wrap(source); |
Aaron Boodman
2012/03/01 07:24:26
Unless v8 does something fancy internally, it seem
Aaron Boodman
2012/03/01 07:30:32
I feel pretty strongly that the right answer here
koz (OOO until 15th September)
2012/03/02 01:13:58
As discussed, I've reimplemented with "new Functio
| |
37 var f = bootstrap.Run(wrappedSource); | 37 var f = bootstrap.Run(wrappedSource, id); |
38 var exports = {}; | 38 var exports = {}; |
39 f(require, requireNative, exports); | 39 f(require, requireNative, exports); |
40 return exports; | 40 return exports; |
41 }; | 41 }; |
42 | 42 |
43 ModuleLoader.prototype.load = function(id) { | 43 ModuleLoader.prototype.load = function(id) { |
44 var exports = this.cache_[id]; | 44 var exports = this.cache_[id]; |
45 if (exports) { | 45 if (exports) { |
46 return exports; | 46 return exports; |
47 } | 47 } |
48 exports = this.run(id); | 48 exports = this.run(id); |
49 this.cache_[id] = exports; | 49 this.cache_[id] = exports; |
50 return exports; | 50 return exports; |
51 }; | 51 }; |
52 | 52 |
53 var moduleLoader = new ModuleLoader(); | 53 var moduleLoader = new ModuleLoader(); |
54 | 54 |
55 function requireNative(nativeName) { | 55 function requireNative(nativeName) { |
56 return bootstrap.GetNative(nativeName); | 56 var result = bootstrap.GetNative(nativeName); |
57 if (!result) { | |
58 throw "No such native object: '" + nativeName + "'"; | |
59 } | |
60 return result; | |
57 } | 61 } |
58 | 62 |
59 function require(moduleId) { | 63 function require(moduleId) { |
60 return moduleLoader.load(moduleId); | 64 return moduleLoader.load(moduleId); |
61 } | 65 } |
62 | 66 |
63 return require; | 67 return require; |
64 }); | 68 }); |
OLD | NEW |