-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptEventLoop.java
More file actions
222 lines (174 loc) · 6.72 KB
/
ScriptEventLoop.java
File metadata and controls
222 lines (174 loc) · 6.72 KB
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
import jdk.nashorn.api.scripting.AbstractJSObject;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.runtime.Undefined;
import javax.script.Bindings;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ScriptEventLoop {
private static final Logger LOGGER = Logger.getLogger(ScriptEventLoop.class.getName());
private final Bindings bindings;
private int jobCount = 0;
private ScheduledExecutorService executor;
private Map<Integer, Object> gTasks = new ConcurrentHashMap<>();
private List<TimeoutFunction> gQueuedFunctions = new ArrayList<>();
private boolean gInLoop = false;
public ScriptEventLoop(Bindings b) {
bindings = b;
loadIntoBindings();
}
private void loadIntoBindings() {
bindings.put("setTimeout", new SetTimeoutFunction());
bindings.put("setImmediate", new SetImmediateFunction());
bindings.put("setInterval", new SetIntervalFunction());
ClearFunction clear = new ClearFunction();
bindings.put("clearTimeout", clear);
bindings.put("clearImmediate", clear);
bindings.put("clearInterval", clear);
}
public void runLoop() {
gInLoop = true;
try {
for (TimeoutFunction function : gQueuedFunctions)
giveToExecutor(function);
gQueuedFunctions.clear();
while (!gTasks.isEmpty()) {
for (Iterator<Map.Entry<Integer, Object>> iter = gTasks.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<Integer, Object> entry = iter.next();
Object task = entry.getValue();
if (task instanceof ScheduledFuture && ((ScheduledFuture) task).isDone())
iter.remove();
}
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception during runLoop", e);
}
getExecutor().shutdown();
if (!getExecutor().isShutdown())
LOGGER.log(Level.SEVERE, "Finished running loop but executor did not shut down");
executor = null;
gInLoop = false;
}
private synchronized ScheduledExecutorService getExecutor() {
if (executor == null)
executor = Executors.newSingleThreadScheduledExecutor();
return executor;
}
private void schedule(TimeoutFunction function) {
if (gInLoop)
giveToExecutor(function);
else
gQueuedFunctions.add(function);
}
private void giveToExecutor(TimeoutFunction function) {
long startDelay = System.currentTimeMillis() - function.gShouldStart;
if (function.gInterval == -1)
gTasks.put(function.gTaskId, getExecutor().schedule(function, startDelay > 0 ? startDelay : 0, TimeUnit.MILLISECONDS));
else
gTasks.put(function.gTaskId, getExecutor().scheduleAtFixedRate(function, startDelay, function.gInterval, TimeUnit.MILLISECONDS));
}
private void cancel(Integer taskId) {
Object task = gTasks.remove(taskId);
if (task instanceof ScheduledFuture)
((ScheduledFuture) task).cancel(false);
}
private void checkArgs(Object ... args) {
if (args.length < 2)
throw new RuntimeException("Should get 2 args");
if (!(args[0] instanceof ScriptObjectMirror))
throw new RuntimeException("Did not get ScriptObjectMirror as first arg");
if (!((ScriptObjectMirror) args[0]).isFunction())
throw new RuntimeException("First arg was not a function");
if (!((args[1] instanceof Integer)))
throw new RuntimeException("Second arg was not an Integer");
}
private Object setTimeout(Object ... args) {
checkArgs(args);
int funcId = jobCount++;
TimeoutFunction function = new TimeoutFunction(funcId, (ScriptObjectMirror) args[0], System.currentTimeMillis() + (Integer) args[1], Arrays.copyOfRange(args, 2, args.length));
gTasks.put(funcId, function);
schedule(function);
return funcId;
}
private Object setInterval(Object ... args) {
checkArgs(args);
int funcId = jobCount++;
Integer delay = (Integer) args[1];
TimeoutFunction function = new TimeoutFunction(funcId, (ScriptObjectMirror) args[0], System.currentTimeMillis() + delay, Arrays.copyOfRange(args, 2, args.length));
function.setInterval(delay);
gTasks.put(funcId, function);
schedule(function);
return funcId;
}
class SetTimeoutFunction extends AbstractJSObject {
@Override
public boolean isFunction() {
return true;
}
@Override
public Object call(Object thiz, Object ... args) {
return setTimeout(args);
}
}
class SetIntervalFunction extends AbstractJSObject {
@Override
public boolean isFunction() {
return true;
}
@Override
public Object call(Object thiz, Object ... args) {
return setInterval(args);
}
}
class ClearFunction extends AbstractJSObject {
@Override
public boolean isFunction() {
return true;
}
@Override
public Object call(Object thiz, Object ... args) {
if (args.length < 1 || !(args[0] instanceof Integer))
return Undefined.getUndefined();
cancel((Integer) args[0]);
return Undefined.getUndefined();
}
}
class SetImmediateFunction extends AbstractJSObject {
@Override
public boolean isFunction() {
return true;
}
@Override
public Object call(Object thiz, Object ... args) {
List<Object> newArgs = new ArrayList<>();
if (args.length < 1)
return Undefined.getUndefined();
newArgs.add(args[0]);
newArgs.add(0);
for (int i = 1; i < args.length; i++)
newArgs.add(args[i]);
return setTimeout(newArgs.toArray());
}
}
class TimeoutFunction implements Runnable {
int gTaskId = -1;
ScriptObjectMirror gFunction;
Object[] gArgs;
Long gShouldStart;
Integer gInterval = -1;
TimeoutFunction (int taskId, ScriptObjectMirror function, Long shouldStart, Object[] args) {
gTaskId = taskId;
gFunction = function;
gArgs = args;
gShouldStart = shouldStart;
}
public void setInterval(Integer interval) {
gInterval = interval;
}
@Override
public void run() {
gFunction.call(gFunction, gArgs);
}
}
}