forked from abrobston/jscc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncSupport.js
252 lines (241 loc) · 10.2 KB
/
asyncSupport.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var asyncSupportGlobalThis;
(function(root, factory) {
asyncSupportGlobalThis = root;
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.jsccasyncSupport = factory();
}
}(this, function() {
// Use this module to get true async support in Java runners that have no
// native setTimeout, setImmediate, etc.
var gThis = asyncSupportGlobalThis,
isNashorn = typeof Java === "object" && typeof Java.type === "function",
isRhino = typeof java !== "undefined" && !isNashorn;
var commonPool = null, Instant = null, millisecondsUnit = null, Thread = null, FutureTask = null, commonPoolSubmit = null,
toJsArray = function(input) {
return input;
};
if (isNashorn) {
commonPool = Java.type("java.util.concurrent.ForkJoinPool").commonPool();
Instant = Java.type("java.time.Instant");
millisecondsUnit = Java.type("java.time.temporal.ChronoUnit").MILLIS;
Thread = Java.type("java.lang.Thread");
FutureTask = Java.type("java.util.concurrent.FutureTask");
toJsArray = function(input) {
if (Array.isArray(input)) {
return input;
}
var length = input.length,
result = [];
for (var index = 0; index < length; index++) {
result.push(input[index]);
}
return result;
};
commonPoolSubmit = function(callback) {
// This is necessary because the commonPool variable may otherwise be garbage-collected,
// reminiscent of .NET P/Invoke issues...
return commonPool["submit(java.lang.Runnable)"](callback);
};
} else if (isRhino) {
commonPool = java.util.concurrent.ForkJoinPool.commonPool();
Instant = java.time.Instant;
millisecondsUnit = java.time.temporal.ChronoUnit.MILLIS;
Thread = java.lang.Thread;
FutureTask = java.util.concurrent.FutureTask;
commonPoolSubmit = commonPool.submit;
}
if (typeof gThis.setTimeout === "undefined") {
if (commonPool) {
gThis.setTimeout = function() {
try {
var jsArgs = toJsArray(arguments),
callback = jsArgs[0],
delay = Math.floor(Math.max(4, Math.min(2147483647, jsArgs[1]))),
args = jsArgs.slice(2),
fireAfterInstant = Instant.now().plusMillis(delay);
if (typeof callback !== "function") {
// Not supporting the string-eval version, at least for now
throw new Error("Non-function callbacks are not supported in this version of setTimeout. Callback parameter was of type " +
(typeof callback) + ".");
}
var forkJoinTask = commonPoolSubmit(function() {
var remainingDelay = millisecondsUnit.between(Instant.now(), fireAfterInstant);
if (remainingDelay > 0) {
Thread.sleep(remainingDelay);
}
callback.apply(null, args);
});
return forkJoinTask;
} catch (e) {
throw e;
}
};
} else {
// Just run task more-or-less immediately
gThis.setTimeout = function() {
var jsArgs = toJsArray(arguments),
callback = jsArgs[0],
args = jsArgs.slice(2);
if (typeof callback !== "function") {
throw new Error("Non-function callbacks are not supported in this version of setTimeout. Callback parameter was of type " +
(typeof callback) + ".");
}
callback.apply(null, args);
};
}
gThis.clearTimeout = function(forkJoinTask) {
if (typeof forkJoinTask !== "undefined" && forkJoinTask !== null &&
typeof forkJoinTask.cancel === "function") {
forkJoinTask.cancel(true);
}
};
}
if (typeof gThis.setImmediate === "undefined") {
gThis.setImmediate = function() {
var args = toJsArray(arguments);
args.splice(1, 0, 4);
return gThis.setTimeout.apply(null, args);
};
gThis.clearImmediate = function(forkJoinTask) {
gThis.clearTimeout(forkJoinTask);
};
}
if (typeof gThis.setInterval === "undefined") {
if (commonPool) {
var IntervalTaskToken = function(callback, delay, args) {
if (typeof callback !== "function") {
throw new Error("The callback passed to the IntervalTaskToken must be a function, but instead has type " +
(typeof callback) + ".");
}
this._callback = callback;
this._delay = Math.floor(Math.max(4, Math.min(2147483647, delay)));
this._args = args;
};
/**
* @type {!Function}
* @private
*/
IntervalTaskToken.prototype._callback = function() {
};
/**
* @type {!number}
* @private
*/
IntervalTaskToken.prototype._delay = 4;
/**
* @type {!Array}
* @private
*/
IntervalTaskToken.prototype._args = [];
/**
* @type {!boolean}
* @private
*/
IntervalTaskToken.prototype._cancelCalled = false;
/**
* @type {Object<!{ cancel: function(boolean):boolean }, *>}
* @private
*/
IntervalTaskToken.prototype._taskObject = {};
/**
* @param {boolean=} interrupt
*/
IntervalTaskToken.prototype.cancel = function(interrupt) {
var that = this;
this._cancelCalled = true;
Object.keys(this._taskObject).forEach(function(task) {
try {
task.cancel(interrupt);
} catch (e) {
// no-op
}
try {
delete that._taskObject[task];
} catch (e) {
// no-op
}
});
};
IntervalTaskToken.prototype.queueTask = function() {
var that = this,
fireAfterInstant = Instant.now().plusMillis(this._delay);
var task = commonPoolSubmit(function() {
var remainingMilliseconds = millisecondsUnit.between(Instant.now(), fireAfterInstant);
if (remainingMilliseconds > 0 && !that._cancelCalled) {
Thread.sleep(remainingMilliseconds);
}
if (!that._cancelCalled) {
var immediateTask = new FutureTask(function() {
that._callback.apply(null, that._args);
delete that._taskObject[immediateTask];
});
that._taskObject[immediateTask] = true;
immediateTask.fork();
}
if (!that._cancelCalled) {
// Don't save this one
new FutureTask(that.queueTask).fork();
}
var deleted = false;
while (!deleted) {
try {
deleted = that._cancelCalled;
delete that._taskObject[task];
deleted = true;
} catch (e) {
Thread.sleep(20);
}
}
});
this._taskObject[task] = true;
return task;
};
gThis.setInterval = function() {
var jsArgs = toJsArray(arguments),
callback = jsArgs[0],
delay = Math.floor(Math.max(4, Math.min(2147483647, jsArgs[1]))),
args = jsArgs.slice(2);
if (typeof callback !== "function") {
throw new Error("Non-function callbacks are not supported in this version of setInterval. Callback parameter was of type " +
(typeof callback) + ".");
}
return new IntervalTaskToken(callback, delay, args);
};
} else {
gThis.setInterval = function() {
var jsArgs = toJsArray(arguments),
callback = jsArgs[0],
delay = Math.floor(Math.max(4, Math.min(2147483647, jsArgs[1]))),
args = jsArgs.slice(2),
cancelCalled = false;
if (typeof callback !== "function") {
throw new Error("Non-function callbacks are not supported in this version of setInterval. Callback parameter was of type " +
(typeof callback) + ".");
}
var innerFunction = function() {
if (!cancelCalled) {
callback.apply(null, args);
gThis.setTimeout(innerFunction, delay);
}
};
gThis.setTimeout(innerFunction, delay);
return {
cancel: function() {
cancelCalled = true;
}
}
}
}
gThis.clearInterval = function(forkJoinTask) {
if (typeof forkJoinTask !== "undefined" && forkJoinTask !== null &&
typeof forkJoinTask.cancel === "function") {
forkJoinTask.cancel(true);
}
};
}
return gThis;
}));