UNPKG

2.82 kBJavaScriptView Raw
1// lifted from node core, https://github.com/nodejs/node/blob/88307974e60346bc98c4e9f70a2b6918ccb6844f/src/node.js
2// needed to instrument timers
3
4exports.NativeModule = NativeModule
5exports.globalTimeouts = globalTimeouts
6
7var ContextifyScript = process.binding('contextify').ContextifyScript;
8function runInThisContext(code, options) {
9 var script = new ContextifyScript(code, options);
10 return script.runInThisContext();
11}
12
13function globalTimeouts () {
14 const timers = NativeModule.require('timers');
15 global.clearImmediate = timers.clearImmediate;
16 global.clearInterval = timers.clearInterval;
17 global.clearTimeout = timers.clearTimeout;
18 global.setImmediate = timers.setImmediate;
19 global.setInterval = timers.setInterval;
20 global.setTimeout = timers.setTimeout;
21};
22
23function NativeModule(id) {
24 this.filename = id + '.js';
25 this.id = id;
26 this.exports = {};
27 this.loaded = false;
28}
29
30NativeModule._source = process.binding('natives');
31NativeModule._cache = {};
32
33NativeModule.require = function(id) {
34 if (id == 'native_module') {
35 return NativeModule;
36 }
37
38 var cached = NativeModule.getCached(id);
39 if (cached) {
40 return cached.exports;
41 }
42
43 if (!NativeModule.exists(id)) {
44 throw new Error('No such native module ' + id);
45 }
46
47 process.moduleLoadList.push('NativeModule ' + id);
48
49 var nativeModule = new NativeModule(id);
50
51 nativeModule.cache();
52 nativeModule.compile();
53
54 return nativeModule.exports;
55};
56
57NativeModule.getCached = function(id) {
58 return NativeModule._cache[id];
59};
60
61NativeModule.exists = function(id) {
62 return NativeModule._source.hasOwnProperty(id);
63};
64
65const EXPOSE_INTERNALS = process.execArgv.some(function(arg) {
66 return arg.match(/^--expose[-_]internals$/);
67});
68
69if (EXPOSE_INTERNALS) {
70 NativeModule.nonInternalExists = NativeModule.exists;
71
72 NativeModule.isInternal = function(id) {
73 return false;
74 };
75} else {
76 NativeModule.nonInternalExists = function(id) {
77 return NativeModule.exists(id) && !NativeModule.isInternal(id);
78 };
79
80 NativeModule.isInternal = function(id) {
81 return id.startsWith('internal/');
82 };
83}
84
85
86NativeModule.getSource = function(id) {
87 return NativeModule._source[id];
88};
89
90NativeModule.wrap = function(script) {
91 return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
92};
93
94NativeModule.wrapper = [
95 '(function (exports, require, module, __filename, __dirname) { ',
96 '\n});'
97];
98
99NativeModule.prototype.compile = function() {
100 var source = NativeModule.getSource(this.id);
101 source = NativeModule.wrap(source);
102
103 var fn = runInThisContext(source, {
104 filename: this.filename,
105 lineOffset: 0,
106 displayErrors: true
107 });
108 fn(this.exports, NativeModule.require, this, this.filename);
109
110 this.loaded = true;
111};
112
113NativeModule.prototype.cache = function() {
114 NativeModule._cache[this.id] = this;
115};