|
76 | 76 | process._needTickCallback(); |
77 | 77 | }; |
78 | 78 |
|
79 | | - // This contains the source code for the files in lib/ |
80 | | - // Like, natives.fs is the contents of lib/fs.js |
81 | | - var natives = process.binding('natives'); |
82 | | - |
83 | | - // Module System |
84 | | - var Module = (function() { |
85 | | - function Module(id, parent) { |
| 79 | + // Native modules don't need a full require function. So we can bootstrap |
| 80 | + // most of the system with this mini module system. |
| 81 | + var NativeModule = (function() { |
| 82 | + function NativeModule(id) { |
| 83 | + this.filename = id + '.js'; |
86 | 84 | this.id = id; |
87 | 85 | this.exports = {}; |
88 | | - this.parent = parent; |
89 | | - |
90 | | - this.filename = null; |
91 | 86 | this.loaded = false; |
92 | | - this.exited = false; |
93 | | - this.children = []; |
94 | | - }; |
| 87 | + } |
95 | 88 |
|
96 | | - // Set the environ variable NODE_MODULE_CONTEXTS=1 to make node load all |
97 | | - // modules in thier own context. |
98 | | - Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0); |
99 | | - Module._internalCache = {}; |
100 | | - Module._cache = {}; |
101 | | - Module._extensions = {}; |
102 | | - Module._paths = []; |
| 89 | + NativeModule._source = process.binding('natives'); |
| 90 | + NativeModule._cache = {}; |
103 | 91 |
|
104 | | - // Native modules don't need a full require function. So we can bootstrap |
105 | | - // most of the system with this mini-require. |
106 | | - Module._requireNative = function(id) { |
| 92 | + NativeModule.require = function(id) { |
107 | 93 | if (id == 'module') { |
108 | 94 | return Module; |
109 | 95 | } |
110 | 96 |
|
111 | | - if (Module._internalCache[id]) { |
112 | | - return Module._internalCache[id].exports; |
| 97 | + var cached = NativeModule.getCached(id); |
| 98 | + if (cached) { |
| 99 | + return cached.exports; |
113 | 100 | } |
114 | 101 |
|
115 | | - if (!natives[id]) { |
| 102 | + if (!NativeModule.exists(id)) { |
116 | 103 | throw new Error('No such native module ' + id); |
117 | 104 | } |
118 | 105 |
|
119 | | - var filename = id + '.js'; |
| 106 | + var nativeModule = new NativeModule(id); |
120 | 107 |
|
121 | | - var fn = runInThisContext(Module.wrap(natives[id]), filename, true); |
| 108 | + nativeModule.compile(); |
| 109 | + nativeModule.cache(); |
122 | 110 |
|
123 | | - var m = {id: id, exports: {}}; |
124 | | - fn(m.exports, Module._requireNative, m, filename); |
125 | | - m.loaded = true; |
126 | | - Module._internalCache[id] = m; |
127 | | - return m.exports; |
| 111 | + return nativeModule.exports; |
128 | 112 | }; |
129 | 113 |
|
130 | | - Module.wrap = function(script) { |
131 | | - return Module.wrapper[0] + script + Module.wrapper[1]; |
| 114 | + NativeModule.getCached = function(id) { |
| 115 | + return NativeModule._cache[id]; |
| 116 | + } |
| 117 | + |
| 118 | + NativeModule.exists = function(id) { |
| 119 | + return (id in NativeModule._source); |
| 120 | + } |
| 121 | + |
| 122 | + NativeModule.getSource = function(id) { |
| 123 | + return NativeModule._source[id]; |
| 124 | + } |
| 125 | + |
| 126 | + NativeModule.wrap = function(script) { |
| 127 | + return NativeModule.wrapper[0] + script + NativeModule.wrapper[1]; |
132 | 128 | }; |
133 | 129 |
|
134 | | - Module.wrapper = [ |
| 130 | + NativeModule.wrapper = [ |
135 | 131 | '(function (exports, require, module, __filename, __dirname) { ', |
136 | 132 | '\n});' |
137 | 133 | ]; |
138 | 134 |
|
139 | | - var path = Module._requireNative('path'); |
| 135 | + NativeModule.prototype.compile = function() { |
| 136 | + var source = NativeModule.getSource(this.id); |
| 137 | + source = NativeModule.wrap(source); |
| 138 | + |
| 139 | + var fn = runInThisContext(source, this.filename, true); |
| 140 | + fn(this.exports, NativeModule.require, this, this.filename); |
| 141 | + |
| 142 | + this.loaded = true; |
| 143 | + }; |
| 144 | + |
| 145 | + NativeModule.prototype.cache = function() { |
| 146 | + NativeModule._cache[this.id] = this; |
| 147 | + }; |
| 148 | + |
| 149 | + return NativeModule; |
| 150 | + })(); |
| 151 | + |
| 152 | + // Module System |
| 153 | + var Module = (function() { |
| 154 | + function Module(id, parent) { |
| 155 | + this.id = id; |
| 156 | + this.exports = {}; |
| 157 | + this.parent = parent; |
| 158 | + |
| 159 | + this.filename = null; |
| 160 | + this.loaded = false; |
| 161 | + this.exited = false; |
| 162 | + this.children = []; |
| 163 | + }; |
| 164 | + |
| 165 | + // Set the environ variable NODE_MODULE_CONTEXTS=1 to make node load all |
| 166 | + // modules in thier own context. |
| 167 | + Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0); |
| 168 | + Module._cache = {}; |
| 169 | + Module._extensions = {}; |
| 170 | + Module._paths = []; |
| 171 | + |
| 172 | + Module.wrapper = NativeModule.wrapper; |
| 173 | + Module.wrap = NativeModule.wrap; |
| 174 | + |
| 175 | + var path = NativeModule.require('path'); |
140 | 176 |
|
141 | 177 | Module._debug = function() {}; |
142 | 178 | if (process.env.NODE_DEBUG && /module/.test(process.env.NODE_DEBUG)) { |
|
159 | 195 | // -> a.<ext> |
160 | 196 | // -> a/index.<ext> |
161 | 197 | Module._findPath = function(request, paths) { |
162 | | - var fs = Module._requireNative('fs'); |
| 198 | + var fs = NativeModule.require('fs'); |
163 | 199 | var exts = Object.keys(Module._extensions); |
164 | 200 |
|
165 | 201 | if (request.charAt(0) === '/') { |
|
201 | 237 | } |
202 | 238 |
|
203 | 239 | Module._resolveLookupPaths = function(request, parent) { |
204 | | - if (natives[request]) { |
| 240 | + if (NativeModule.exists(request)) { |
205 | 241 | return [request, []]; |
206 | 242 | } |
207 | 243 |
|
|
250 | 286 | return cachedModule.exports; |
251 | 287 | } |
252 | 288 |
|
253 | | - // With natives id === request |
254 | | - // We deal with these first |
255 | | - if (natives[id]) { |
| 289 | + if (NativeModule.exists(id)) { |
256 | 290 | // REPL is a special case, because it needs the real require. |
257 | 291 | if (id == 'repl') { |
258 | 292 | var replModule = new Module('repl'); |
259 | | - replModule._compile(natives.repl, 'repl.js'); |
260 | | - Module._internalCache.repl = replModule; |
| 293 | + replModule._compile(NativeModule.getSource('repl'), 'repl.js'); |
| 294 | + NativeModule._cache.repl = replModule; |
261 | 295 | return replModule.exports; |
262 | 296 | } |
263 | 297 |
|
264 | 298 | debug('load native module ' + request); |
265 | | - return Module._requireNative(id); |
| 299 | + return NativeModule.require(id); |
266 | 300 | } |
267 | 301 |
|
268 | 302 | var module = new Module(id, parent); |
|
272 | 306 | }; |
273 | 307 |
|
274 | 308 | Module._resolveFilename = function(request, parent) { |
275 | | - if (natives[request]) { |
| 309 | + if (NativeModule.exists(request)) { |
276 | 310 | return [request, request]; |
277 | 311 | } |
278 | 312 |
|
|
375 | 409 |
|
376 | 410 | // Native extension for .js |
377 | 411 | Module._extensions['.js'] = function(module, filename) { |
378 | | - var content = Module._requireNative('fs').readFileSync(filename, 'utf8'); |
| 412 | + var content = NativeModule.require('fs').readFileSync(filename, 'utf8'); |
379 | 413 | module._compile(content, filename); |
380 | 414 | }; |
381 | 415 |
|
|
424 | 458 |
|
425 | 459 | // Load events module in order to access prototype elements on process like |
426 | 460 | // process.addListener. |
427 | | - var events = Module._requireNative('events'); |
| 461 | + var events = NativeModule.require('events'); |
428 | 462 |
|
429 | 463 | // Signal Handlers |
430 | 464 | (function() { |
|
471 | 505 |
|
472 | 506 |
|
473 | 507 | global.setTimeout = function() { |
474 | | - var t = Module._requireNative('timers'); |
| 508 | + var t = NativeModule.require('timers'); |
475 | 509 | return t.setTimeout.apply(this, arguments); |
476 | 510 | }; |
477 | 511 |
|
478 | 512 | global.setInterval = function() { |
479 | | - var t = Module._requireNative('timers'); |
| 513 | + var t = NativeModule.require('timers'); |
480 | 514 | return t.setInterval.apply(this, arguments); |
481 | 515 | }; |
482 | 516 |
|
483 | 517 | global.clearTimeout = function() { |
484 | | - var t = Module._requireNative('timers'); |
| 518 | + var t = NativeModule.require('timers'); |
485 | 519 | return t.clearTimeout.apply(this, arguments); |
486 | 520 | }; |
487 | 521 |
|
488 | 522 | global.clearInterval = function() { |
489 | | - var t = Module._requireNative('timers'); |
| 523 | + var t = NativeModule.require('timers'); |
490 | 524 | return t.clearInterval.apply(this, arguments); |
491 | 525 | }; |
492 | 526 |
|
|
498 | 532 | if (stdout) return stdout; |
499 | 533 |
|
500 | 534 | var binding = process.binding('stdio'), |
501 | | - net = Module._requireNative('net'), |
502 | | - fs = Module._requireNative('fs'), |
| 535 | + net = NativeModule.require('net'), |
| 536 | + fs = NativeModule.require('fs'), |
503 | 537 | fd = binding.stdoutFD; |
504 | 538 |
|
505 | 539 | if (binding.isStdoutBlocking()) { |
|
521 | 555 | if (stdin) return stdin; |
522 | 556 |
|
523 | 557 | var binding = process.binding('stdio'), |
524 | | - net = Module._requireNative('net'), |
525 | | - fs = Module._requireNative('fs'), |
| 558 | + net = NativeModule.require('net'), |
| 559 | + fs = NativeModule.require('fs'), |
526 | 560 | fd = binding.openStdin(); |
527 | 561 |
|
528 | 562 | if (binding.isStdinBlocking()) { |
|
544 | 578 |
|
545 | 579 | // Lazy load console object |
546 | 580 | global.__defineGetter__('console', function() { |
547 | | - return Module._requireNative('console'); |
| 581 | + return NativeModule.require('console'); |
548 | 582 | }); |
549 | 583 |
|
550 | 584 |
|
551 | | - global.Buffer = Module._requireNative('buffer').Buffer; |
| 585 | + global.Buffer = NativeModule.require('buffer').Buffer; |
552 | 586 |
|
553 | 587 | process.exit = function(code) { |
554 | 588 | process.emit('exit', code || 0); |
|
563 | 597 |
|
564 | 598 |
|
565 | 599 | var cwd = process.cwd(); |
566 | | - var path = Module._requireNative('path'); |
| 600 | + var path = NativeModule.require('path'); |
567 | 601 | var isWindows = process.platform === 'win32'; |
568 | 602 |
|
569 | 603 | // Make process.argv[0] and process.argv[1] into full paths, but only |
|
579 | 613 | // To allow people to extend Node in different ways, this hook allows |
580 | 614 | // one to drop a file lib/_third_party_main.js into the build directory |
581 | 615 | // which will be executed instead of Node's normal loading. |
582 | | - if (process.binding('natives')['_third_party_main']) { |
| 616 | + if (NativeModule.exists('_third_party_main')) { |
583 | 617 | process.nextTick(function () { |
584 | | - Module._requireNative('_third_party_main'); |
| 618 | + NativeModule.require('_third_party_main'); |
585 | 619 | }); |
586 | 620 | return; |
587 | 621 | } |
588 | 622 |
|
589 | 623 | if (process.argv[1]) { |
590 | 624 | if (process.argv[1] == 'debug') { |
591 | 625 | // Start the debugger agent |
592 | | - var d = Module._requireNative('_debugger'); |
| 626 | + var d = NativeModule.require('_debugger'); |
593 | 627 | d.start(); |
594 | 628 | return; |
595 | 629 | } |
|
0 commit comments