Skip to content

Commit f39fdf2

Browse files
felixgery
authored andcommitted
Isolate native module system again
See: nodejs/node-v0.x-archive@2e5dfaf#commitcomment-239719
1 parent c3b0d13 commit f39fdf2

File tree

1 file changed

+96
-62
lines changed

1 file changed

+96
-62
lines changed

src/node.js

Lines changed: 96 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -76,67 +76,103 @@
7676
process._needTickCallback();
7777
};
7878

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';
8684
this.id = id;
8785
this.exports = {};
88-
this.parent = parent;
89-
90-
this.filename = null;
9186
this.loaded = false;
92-
this.exited = false;
93-
this.children = [];
94-
};
87+
}
9588

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 = {};
10391

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) {
10793
if (id == 'module') {
10894
return Module;
10995
}
11096

111-
if (Module._internalCache[id]) {
112-
return Module._internalCache[id].exports;
97+
var cached = NativeModule.getCached(id);
98+
if (cached) {
99+
return cached.exports;
113100
}
114101

115-
if (!natives[id]) {
102+
if (!NativeModule.exists(id)) {
116103
throw new Error('No such native module ' + id);
117104
}
118105

119-
var filename = id + '.js';
106+
var nativeModule = new NativeModule(id);
120107

121-
var fn = runInThisContext(Module.wrap(natives[id]), filename, true);
108+
nativeModule.compile();
109+
nativeModule.cache();
122110

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;
128112
};
129113

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];
132128
};
133129

134-
Module.wrapper = [
130+
NativeModule.wrapper = [
135131
'(function (exports, require, module, __filename, __dirname) { ',
136132
'\n});'
137133
];
138134

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');
140176

141177
Module._debug = function() {};
142178
if (process.env.NODE_DEBUG && /module/.test(process.env.NODE_DEBUG)) {
@@ -159,7 +195,7 @@
159195
// -> a.<ext>
160196
// -> a/index.<ext>
161197
Module._findPath = function(request, paths) {
162-
var fs = Module._requireNative('fs');
198+
var fs = NativeModule.require('fs');
163199
var exts = Object.keys(Module._extensions);
164200

165201
if (request.charAt(0) === '/') {
@@ -201,7 +237,7 @@
201237
}
202238

203239
Module._resolveLookupPaths = function(request, parent) {
204-
if (natives[request]) {
240+
if (NativeModule.exists(request)) {
205241
return [request, []];
206242
}
207243

@@ -250,19 +286,17 @@
250286
return cachedModule.exports;
251287
}
252288

253-
// With natives id === request
254-
// We deal with these first
255-
if (natives[id]) {
289+
if (NativeModule.exists(id)) {
256290
// REPL is a special case, because it needs the real require.
257291
if (id == 'repl') {
258292
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;
261295
return replModule.exports;
262296
}
263297

264298
debug('load native module ' + request);
265-
return Module._requireNative(id);
299+
return NativeModule.require(id);
266300
}
267301

268302
var module = new Module(id, parent);
@@ -272,7 +306,7 @@
272306
};
273307

274308
Module._resolveFilename = function(request, parent) {
275-
if (natives[request]) {
309+
if (NativeModule.exists(request)) {
276310
return [request, request];
277311
}
278312

@@ -375,7 +409,7 @@
375409

376410
// Native extension for .js
377411
Module._extensions['.js'] = function(module, filename) {
378-
var content = Module._requireNative('fs').readFileSync(filename, 'utf8');
412+
var content = NativeModule.require('fs').readFileSync(filename, 'utf8');
379413
module._compile(content, filename);
380414
};
381415

@@ -424,7 +458,7 @@
424458

425459
// Load events module in order to access prototype elements on process like
426460
// process.addListener.
427-
var events = Module._requireNative('events');
461+
var events = NativeModule.require('events');
428462

429463
// Signal Handlers
430464
(function() {
@@ -471,22 +505,22 @@
471505

472506

473507
global.setTimeout = function() {
474-
var t = Module._requireNative('timers');
508+
var t = NativeModule.require('timers');
475509
return t.setTimeout.apply(this, arguments);
476510
};
477511

478512
global.setInterval = function() {
479-
var t = Module._requireNative('timers');
513+
var t = NativeModule.require('timers');
480514
return t.setInterval.apply(this, arguments);
481515
};
482516

483517
global.clearTimeout = function() {
484-
var t = Module._requireNative('timers');
518+
var t = NativeModule.require('timers');
485519
return t.clearTimeout.apply(this, arguments);
486520
};
487521

488522
global.clearInterval = function() {
489-
var t = Module._requireNative('timers');
523+
var t = NativeModule.require('timers');
490524
return t.clearInterval.apply(this, arguments);
491525
};
492526

@@ -498,8 +532,8 @@
498532
if (stdout) return stdout;
499533

500534
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'),
503537
fd = binding.stdoutFD;
504538

505539
if (binding.isStdoutBlocking()) {
@@ -521,8 +555,8 @@
521555
if (stdin) return stdin;
522556

523557
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'),
526560
fd = binding.openStdin();
527561

528562
if (binding.isStdinBlocking()) {
@@ -544,11 +578,11 @@
544578

545579
// Lazy load console object
546580
global.__defineGetter__('console', function() {
547-
return Module._requireNative('console');
581+
return NativeModule.require('console');
548582
});
549583

550584

551-
global.Buffer = Module._requireNative('buffer').Buffer;
585+
global.Buffer = NativeModule.require('buffer').Buffer;
552586

553587
process.exit = function(code) {
554588
process.emit('exit', code || 0);
@@ -563,7 +597,7 @@
563597

564598

565599
var cwd = process.cwd();
566-
var path = Module._requireNative('path');
600+
var path = NativeModule.require('path');
567601
var isWindows = process.platform === 'win32';
568602

569603
// Make process.argv[0] and process.argv[1] into full paths, but only
@@ -579,17 +613,17 @@
579613
// To allow people to extend Node in different ways, this hook allows
580614
// one to drop a file lib/_third_party_main.js into the build directory
581615
// 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')) {
583617
process.nextTick(function () {
584-
Module._requireNative('_third_party_main');
618+
NativeModule.require('_third_party_main');
585619
});
586620
return;
587621
}
588622

589623
if (process.argv[1]) {
590624
if (process.argv[1] == 'debug') {
591625
// Start the debugger agent
592-
var d = Module._requireNative('_debugger');
626+
var d = NativeModule.require('_debugger');
593627
d.start();
594628
return;
595629
}

0 commit comments

Comments
 (0)