diff options
author | Valerio Virgillito | 2012-02-02 00:11:51 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-02-02 00:11:51 -0800 |
commit | 5a305ec44b2bd5198a248357439aae8ebbd75fee (patch) | |
tree | ec3b756beab7105841353ef79d4e8059b2b6a91c /node_modules/montage/require/node.js | |
parent | dbe9c7705860fb3551321e23e7eab763f622241c (diff) | |
download | ninja-5a305ec44b2bd5198a248357439aae8ebbd75fee.tar.gz |
upgrading to Montage v0.6
Signed-off-by: Valerio Virgillito <valerio@motorola.com>
Diffstat (limited to 'node_modules/montage/require/node.js')
-rw-r--r-- | node_modules/montage/require/node.js | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/node_modules/montage/require/node.js b/node_modules/montage/require/node.js new file mode 100644 index 00000000..568410cf --- /dev/null +++ b/node_modules/montage/require/node.js | |||
@@ -0,0 +1,101 @@ | |||
1 | |||
2 | var Require = require("./require"); | ||
3 | var URL = require("../core/url"); | ||
4 | var Promise = require("../core/promise").Promise; | ||
5 | var FS = require("fs"); | ||
6 | |||
7 | var globalEval = eval; | ||
8 | |||
9 | Require.getLocation = function () { | ||
10 | return URL.resolve("file:///", process.cwd() + "/"); | ||
11 | }; | ||
12 | |||
13 | var urlToPath = function (url) { | ||
14 | var parsed = URL.parse(url); | ||
15 | return parsed.path; | ||
16 | }; | ||
17 | |||
18 | Require.read = function (url) { | ||
19 | var deferred = Promise.defer(); | ||
20 | var path = urlToPath(url); | ||
21 | FS.readFile(path, "utf-8", function (error, text) { | ||
22 | if (error) { | ||
23 | deferred.reject(new Error(error)); | ||
24 | } else { | ||
25 | deferred.resolve(text); | ||
26 | } | ||
27 | }); | ||
28 | return deferred.promise; | ||
29 | }; | ||
30 | |||
31 | // Compiles module text into a function. | ||
32 | // Can be overriden by the platform to make the engine aware of the source path. Uses sourceURL hack by default. | ||
33 | Require.Compiler = function (config) { | ||
34 | config.scope = config.scope || {}; | ||
35 | var names = ["require", "exports", "module"]; | ||
36 | var scopeNames = Object.keys(config.scope); | ||
37 | names.push.apply(names, scopeNames); | ||
38 | return function(module) { | ||
39 | if (module.factory) | ||
40 | return module; | ||
41 | if (!module.factory && module.text !== void 0) { | ||
42 | var factory = globalEval( | ||
43 | "(function(" + names.join(",") + "){" + | ||
44 | module.text + | ||
45 | "\n//*/\n})\n//@ sourceURL=" + module.location | ||
46 | ); | ||
47 | module.factory = function (require, exports, module) { | ||
48 | Array.prototype.push.apply(arguments, scopeNames.map(function (name) { | ||
49 | return config.scope[name]; | ||
50 | })); | ||
51 | return factory.apply(this, arguments); | ||
52 | }; | ||
53 | // new Function will have its body reevaluated at every call, hence using eval instead | ||
54 | // https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope | ||
55 | //module.factory = new Function("require", "exports", "module", module.text + "\n//*/\n//@ sourceURL="+module.path); | ||
56 | } | ||
57 | return module; | ||
58 | }; | ||
59 | }; | ||
60 | |||
61 | Require.DefaultLoaderConstructor = function(config) { | ||
62 | return Require.MappingsLoader( | ||
63 | config, | ||
64 | Require.ExtensionsLoader( | ||
65 | config, | ||
66 | Require.PathsLoader( | ||
67 | config, | ||
68 | Require.CachingLoader( | ||
69 | config, | ||
70 | Require.Loader( | ||
71 | config, | ||
72 | Require.NodeLoader(config) | ||
73 | ) | ||
74 | ) | ||
75 | ) | ||
76 | ) | ||
77 | ); | ||
78 | }; | ||
79 | |||
80 | Require.NodeLoader = function (config) { | ||
81 | return function (url, module) { | ||
82 | var id = url.slice(config.location.length); | ||
83 | return { | ||
84 | type: "native", | ||
85 | exports: require(id), | ||
86 | location: url | ||
87 | } | ||
88 | }; | ||
89 | } | ||
90 | |||
91 | Require.main = function () { | ||
92 | var require = Require.Sandbox(); | ||
93 | require.async(process.argv[2]).end(); | ||
94 | }; | ||
95 | |||
96 | Require.overlays = ["node", "server", "montage"]; | ||
97 | |||
98 | if (require.main === module) { | ||
99 | Require.main(); | ||
100 | } | ||
101 | |||