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