Created
February 3, 2017 16:33
-
-
Save ghaiklor/5a9d914347644cab3aa7ea416b8de8cb to your computer and use it in GitHub Desktop.
Module.prototype._compile() sources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run the file contents in the correct scope or sandbox. Expose | |
// the correct helper variables (require, module, exports) to | |
// the file. | |
// Returns exception, if any. | |
Module.prototype._compile = function(content, filename) { | |
// Remove shebang | |
var contLen = content.length; | |
if (contLen >= 2) { | |
if (content.charCodeAt(0) === 35/*#*/ && | |
content.charCodeAt(1) === 33/*!*/) { | |
if (contLen === 2) { | |
// Exact match | |
content = ''; | |
} else { | |
// Find end of shebang line and slice it off | |
var i = 2; | |
for (; i < contLen; ++i) { | |
var code = content.charCodeAt(i); | |
if (code === 10/*\n*/ || code === 13/*\r*/) | |
break; | |
} | |
if (i === contLen) | |
content = ''; | |
else { | |
// Note that this actually includes the newline character(s) in the | |
// new output. This duplicates the behavior of the regular expression | |
// that was previously used to replace the shebang line | |
content = content.slice(i); | |
} | |
} | |
} | |
} | |
// create wrapper function | |
var wrapper = Module.wrap(content); | |
var compiledWrapper = vm.runInThisContext(wrapper, { | |
filename: filename, | |
lineOffset: 0, | |
displayErrors: true | |
}); | |
if (process._debugWaitConnect && process._eval == null) { | |
if (!resolvedArgv) { | |
// we enter the repl if we're not given a filename argument. | |
if (process.argv[1]) { | |
resolvedArgv = Module._resolveFilename(process.argv[1], null); | |
} else { | |
resolvedArgv = 'repl'; | |
} | |
} | |
// Set breakpoint on module start | |
if (filename === resolvedArgv) { | |
delete process._debugWaitConnect; | |
const Debug = vm.runInDebugContext('Debug'); | |
Debug.setBreakPoint(compiledWrapper, 0, 0); | |
} | |
} | |
var dirname = path.dirname(filename); | |
var require = internalModule.makeRequireFunction.call(this); | |
var args = [this.exports, require, this, filename, dirname]; | |
var depth = internalModule.requireDepth; | |
if (depth === 0) stat.cache = new Map(); | |
var result = compiledWrapper.apply(this.exports, args); | |
if (depth === 0) stat.cache = null; | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment