forked from jsdoc/jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhino-shim.js
191 lines (169 loc) · 5.13 KB
/
rhino-shim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*global java, Packages, version */
/*eslint-disable strict */
/**
* @overview A minimal emulation of the standard features of Node.js necessary
* to get JSDoc to run.
*/
// Set the JS version that the Rhino interpreter will use.
version(180);
/**
* Emulate DOM timeout/interval functions.
* @see https://developer.mozilla.org/en-US/docs/DOM/window#Methods
*/
(function() {
'use strict';
var timerPool = new java.util.concurrent.ScheduledThreadPoolExecutor(1);
var timers = {};
var timerCount = 1;
var timerUnits = java.util.concurrent.TimeUnit.MILLISECONDS;
var queue = {};
var queueActive = false;
function getCallback(fn) {
return new java.lang.Runnable({
run: Packages.org.mozilla.javascript.Context.call(fn)
});
}
global.setTimeout = function setTimeout(fn, delay) {
var timerId = timerCount++;
var callback = getCallback(fn);
timers[timerId] = timerPool.schedule(callback, delay, timerUnits);
return timerId;
};
global.clearTimeout = function clearTimeout(timerId) {
if (timers[timerId]) {
timerPool.remove(timers[timerId]);
delete timers[timerId];
}
};
global.setInterval = function setInterval(fn, delay) {
var timerId = timerCount++;
var callback = getCallback(fn);
timers[timerId] = timerPool.scheduleAtFixedRate(callback, delay, delay, timerUnits);
return timerId;
};
global.clearInterval = global.clearTimeout;
// adapted from https://github.com/alexgorbatchev/node-browser-builtins
// MIT license
global.setImmediate = (function() {
function drain() {
var key;
var keys = Object.keys(queue);
queueActive = false;
for (var i = 0, l = keys.length; i < l; i++) {
key = keys[i];
var fn = queue[key];
delete queue[key];
fn();
}
}
return function setImmediate(fn) {
var timerId = timerCount++;
queue[timerId] = fn;
if (!queueActive) {
queueActive = true;
global.setTimeout(drain, 0);
}
return timerId;
};
})();
global.clearImmediate = function clearImmediate(id) {
delete queue[id];
};
})();
/**
* Emulate Node.js console functions.
* @see http://nodejs.org/api/stdio.html
*/
global.console = (function() {
function println(stream, args) {
java.lang.System[stream].println( require('util').format.apply(this, args) );
}
return {
error: function error() {
println('err', arguments);
},
info: function info() {
println('out', arguments);
},
log: function log() {
println('out', arguments);
},
trace: function trace(label) {
// this puts some extra junk at the top of the stack trace, but it's close enough
var e = new java.lang.Exception(label || 'Trace');
e.printStackTrace();
},
warn: function warn() {
println('err', arguments);
}
};
})();
/**
* Emulate Node.js process functions.
* @see http://nodejs.org/api/process.html
*/
global.process = {
// not quite right, but close enough
argv: ['java', 'jsdoc.js']
.concat( Array.prototype.slice.call(arguments, 0) ),
// this depends on a hack in our version of Rhino
cwd: function cwd() {
var f = new java.io.File( java.lang.System.getProperty('user.dir') );
return String( f.getAbsolutePath() );
},
env: (function() {
var javaEnv = java.lang.System.getenv();
var key;
var keys = javaEnv.keySet().toArray();
var result = {};
for (var i = 0, l = keys.length; i < l; i++) {
key = keys[i];
result[key + ''] = javaEnv.get(key) + '';
}
return result;
})(),
exit: function exit(n) {
n = n || 0;
java.lang.System.exit(n);
},
nextTick: function nextTick(callback) {
setTimeout(callback, 0);
},
platform: (function() {
if ( String(java.lang.System.getProperty('os.name')).match(/^[Ww]in/) ) {
return 'win32';
}
else {
// not necessarily accurate, but good enough
return 'linux';
}
})(),
stderr: {
// Java can't reliably find the terminal width across platforms, so we hard-code a
// reasonable value
columns: 80,
write: function write(str) {
java.lang.System.err.print(str);
}
},
stdout: {
// Java can't reliably find the terminal width across platforms, so we hard-code a
// reasonable value
columns: 80,
write: function write(str) {
java.lang.System.out.print(str);
}
}
};
/**
* Emulate other Node.js globals.
* @see http://nodejs.org/docs/latest/api/globals.html
*/
Object.defineProperties(global, {
'__dirname': {
get: function() {
return global.process.cwd();
},
enumerable: true
}
});