1 |
|
2 |
|
3 |
|
4 | exports.NativeModule = NativeModule
|
5 | exports.globalTimeouts = globalTimeouts
|
6 |
|
7 | var ContextifyScript = process.binding('contextify').ContextifyScript;
|
8 | function runInThisContext(code, options) {
|
9 | var script = new ContextifyScript(code, options);
|
10 | return script.runInThisContext();
|
11 | }
|
12 |
|
13 | function 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 |
|
23 | function NativeModule(id) {
|
24 | this.filename = id + '.js';
|
25 | this.id = id;
|
26 | this.exports = {};
|
27 | this.loaded = false;
|
28 | }
|
29 |
|
30 | NativeModule._source = process.binding('natives');
|
31 | NativeModule._cache = {};
|
32 |
|
33 | NativeModule.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 |
|
57 | NativeModule.getCached = function(id) {
|
58 | return NativeModule._cache[id];
|
59 | };
|
60 |
|
61 | NativeModule.exists = function(id) {
|
62 | return NativeModule._source.hasOwnProperty(id);
|
63 | };
|
64 |
|
65 | const EXPOSE_INTERNALS = process.execArgv.some(function(arg) {
|
66 | return arg.match(/^--expose[-_]internals$/);
|
67 | });
|
68 |
|
69 | if (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 |
|
86 | NativeModule.getSource = function(id) {
|
87 | return NativeModule._source[id];
|
88 | };
|
89 |
|
90 | NativeModule.wrap = function(script) {
|
91 | return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
|
92 | };
|
93 |
|
94 | NativeModule.wrapper = [
|
95 | '(function (exports, require, module, __filename, __dirname) { ',
|
96 | '\n});'
|
97 | ];
|
98 |
|
99 | NativeModule.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 |
|
113 | NativeModule.prototype.cache = function() {
|
114 | NativeModule._cache[this.id] = this;
|
115 | };
|