@ -2,6 +2,13 @@
// Module
// Module
// Set the environ variable NODE_MODULE_CONTEXT=1 to make node load all
// modules in thier own context.
var contextLoad = false ;
if ( parseInt ( process . env [ "NODE_MODULE_CONTEXTS" ] ) > 0 ) contextLoad = true ;
var Script ;
var internalModuleCache = { } ;
var internalModuleCache = { } ;
var extensionCache = { } ;
var extensionCache = { } ;
@ -44,7 +51,9 @@ function loadNative (id) {
m . loaded = true ;
m . loaded = true ;
return m ;
return m ;
}
}
exports . requireNative = requireNative ;
exports . requireNative = requireNative ;
function requireNative ( id ) {
function requireNative ( id ) {
if ( internalModuleCache [ id ] ) return internalModuleCache [ id ] . exports ;
if ( internalModuleCache [ id ] ) return internalModuleCache [ id ] . exports ;
if ( ! natives [ id ] ) throw new Error ( 'No such native module ' + id ) ;
if ( ! natives [ id ] ) throw new Error ( 'No such native module ' + id ) ;
@ -374,18 +383,49 @@ Module.prototype._compile = function (content, filename) {
require . main = process . mainModule ;
require . main = process . mainModule ;
require . registerExtension = registerExtension ;
require . registerExtension = registerExtension ;
var dirname = path . dirname ( filename ) ;
if ( contextLoad ) {
if ( ! Script ) Script = Script = process . binding ( 'evals' ) . Script ;
if ( self . id !== "." ) {
debug ( 'load submodule' ) ;
// not root module
var sandbox = { } ;
for ( var k in global ) {
sandbox [ k ] = global [ k ] ;
}
sandbox . require = require ;
sandbox . exports = self . exports ;
sandbox . __ filename = filename ;
sandbox . __ dirname = dirname ;
sandbox . module = self ;
Script . runInNewContext ( content , sandbox , filename ) ;
if ( 'string' === typeof content ) {
} else {
// create wrapper function
debug ( 'load root module' ) ;
var wrapper = "(function (exports, require, module, __filename, __dirname) { "
// root module
+ content
global . require = require ;
+ "\n});" ;
global . exports = self . exports ;
global . __ filename = filename ;
global . __ dirname = dirname ;
global . module = self ;
Script . runInThisContext ( content , filename ) ;
}
var compiledWrapper = process . compile ( wrapper , filename ) ;
var dirName = path . dirname ( filename ) ;
compiledWrapper . apply ( self . exports , [ self . exports , require , self , filename , dirName ] ) ;
} else {
} else {
self . exports = content ;
if ( 'string' === typeof content ) {
// create wrapper function
var wrapper = "(function (exports, require, module, __filename, __dirname) { "
+ content
+ "\n});" ;
var compiledWrapper = process . compile ( wrapper , filename ) ;
compiledWrapper . apply ( self . exports , [ self . exports , require , self , filename , dirname ] ) ;
} else {
self . exports = content ;
}
}
}
} ;
} ;