1 var natives = process.binding('natives')
2 var module = require('module')
3 var normalRequire = require
8 // fallback for 0.x support
9 var runInThisContext, ContextifyScript, Script
10 /*istanbul ignore next*/
12 ContextifyScript = process.binding('contextify').ContextifyScript;
13 runInThisContext = function runInThisContext(code, options) {
14 var script = new ContextifyScript(code, options);
15 return script.runInThisContext();
18 Script = process.binding('evals').NodeScript;
19 runInThisContext = Script.runInThisContext;
23 '(function (exports, require, module, __filename, __dirname) { ',
28 // Basically the same functionality as node's (buried deep)
29 // NativeModule class, but without caching, or internal/ blocking,
30 // or a class, since that's not really necessary. I assume that if
31 // you're loading something with this module, it's because you WANT
32 // a separate copy. However, to preserve semantics, any require()
33 // calls made throughout the internal module load IS cached.
34 function req (id, whitelist) {
35 var cache = Object.create(null)
37 if (Array.isArray(whitelist)) {
38 // a whitelist of things to pull from the "actual" native modules
39 whitelist.forEach(function (id) {
49 return req_(id, cache)
52 function req_ (id, cache) {
53 // Buffer is special, because it's a type rather than a "normal"
54 // class, and many things depend on `Buffer.isBuffer` working.
55 if (id === 'buffer') {
56 return require('buffer')
59 // native_module isn't actually a natives binding.
61 if (id === 'native_module') {
64 wrap: function (script) {
65 return wrap[0] + script + wrap[1]
76 source = wrap[0] + source + wrap[1]
78 var cachingRequire = function require (id) {
80 return cache[id].exports
82 return req_(id, cache)
94 /* istanbul ignore else */
95 if (ContextifyScript) {
96 fn = runInThisContext(source, {
97 filename: nm.filename,
102 fn = runInThisContext(source, nm.filename, true);
104 fn(nm.exports, cachingRequire, nm, nm.filename)