Initial commit
[yaffs-website] / node_modules / natives / index.js
1 var natives = process.binding('natives')
2 var module = require('module')
3 var normalRequire = require
4 exports.source = src
5 exports.require = req
6 var vm = require('vm')
7
8 // fallback for 0.x support
9 var runInThisContext, ContextifyScript, Script
10 /*istanbul ignore next*/
11 try {
12   ContextifyScript = process.binding('contextify').ContextifyScript;
13   runInThisContext = function runInThisContext(code, options) {
14     var script = new ContextifyScript(code, options);
15     return script.runInThisContext();
16   }
17 } catch (er) {
18   Script = process.binding('evals').NodeScript;
19   runInThisContext = Script.runInThisContext;
20 }
21
22 var wrap = [
23   '(function (exports, require, module, __filename, __dirname) { ',
24   '\n});'
25 ];
26
27
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)
36
37   if (Array.isArray(whitelist)) {
38     // a whitelist of things to pull from the "actual" native modules
39     whitelist.forEach(function (id) {
40       cache[id] = {
41         loading: false,
42         loaded: true,
43         filename: id + '.js',
44         exports: require(id)
45       }
46     })
47   }
48
49   return req_(id, cache)
50 }
51
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')
57   }
58
59   // native_module isn't actually a natives binding.
60   // weird, right?
61   if (id === 'native_module') {
62     return {
63       getSource: src,
64       wrap: function (script) {
65         return wrap[0] + script + wrap[1]
66       },
67       wrapper: wrap,
68       _cache: cache
69     }
70   }
71
72   var source = src(id)
73   if (!source) {
74     return undefined
75   }
76   source = wrap[0] + source + wrap[1]
77
78   var cachingRequire = function require (id) {
79     if (cache[id]) {
80       return cache[id].exports
81     }
82     return req_(id, cache)
83   }
84
85   var nm = {
86     exports: {},
87     loading: true,
88     loaded: false,
89     filename: id + '.js'
90   }
91   cache[id] = nm
92   var fn
93   try {
94     /* istanbul ignore else */
95     if (ContextifyScript) {
96       fn = runInThisContext(source, {
97         filename: nm.filename,
98         lineOffset: 0,
99         displayErrors: true
100       });
101     } else {
102       fn = runInThisContext(source, nm.filename, true);
103     }
104     fn(nm.exports, cachingRequire, nm, nm.filename)
105     nm.loaded = true
106   } finally {
107     nm.loading = false
108   }
109
110   return nm.exports
111 }
112
113 function src (id) {
114   return natives[id]
115 }