-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSIL.Host.js
More file actions
243 lines (185 loc) · 5.32 KB
/
Copy pathJSIL.Host.js
File metadata and controls
243 lines (185 loc) · 5.32 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"use strict";
if (typeof (JSIL) === "undefined")
throw new Error("JSIL.Core required");
JSIL.Host.isBrowser = (typeof (window) !== "undefined") && (typeof (navigator) !== "undefined");
JSIL.Host.initCallbacks = [];
JSIL.Host.runInitCallbacks = function () {
for (var i = 0; i < JSIL.Host.initCallbacks.length; i++) {
var cb = JSIL.Host.initCallbacks[i];
cb();
}
JSIL.Host.initCallbacks = null;
};
JSIL.Host.services = JSIL.CreateDictionaryObject(null);
JSIL.Host.getService = function (key, noThrow) {
var svc = JSIL.Host.services[key];
if (!svc) {
if (noThrow)
return null;
else
JSIL.RuntimeError("Service '" + key + "' not available");
}
return svc;
};
JSIL.Host.registerService = function (name, service) {
JSIL.Host.services[name] = service;
};
JSIL.Host.registerServices = function (services) {
for (var key in services) {
if (!services.hasOwnProperty(key))
continue;
JSIL.Host.registerService(key, services[key]);
}
};
// Access services using these methods instead of getService directly.
// Returns UTC time
JSIL.Host.getTime = function () {
var svc = JSIL.Host.getService("time");
return svc.getUTC();
};
// Returns elapsed ticks since some starting point (usually page load), high precision.
JSIL.Host.getTickCount = function () {
var svc = JSIL.Host.getService("time");
return svc.getTickCount();
};
// Entry point used by storage APIs to get UTC time.
JSIL.Host.getFileTime = function () {
// FIXME
return Date.now();
};
JSIL.Host.getCanvas = function (desiredWidth, desiredHeight) {
var svc = JSIL.Host.getService("canvas");
if (
(typeof (desiredWidth) !== "undefined") &&
(typeof (desiredHeight) !== "undefined")
)
return svc.get(desiredWidth, desiredHeight);
else
return svc.get();
};
JSIL.Host.createCanvas = function (desiredWidth, desiredHeight) {
var svc = JSIL.Host.getService("canvas");
return svc.create(desiredWidth, desiredHeight);
};
JSIL.Host.getHeldKeys = function () {
var svc = JSIL.Host.getService("keyboard", true);
if (!svc)
return [];
return svc.getHeldKeys();
};
JSIL.Host.getMousePosition = function () {
var svc = JSIL.Host.getService("mouse", true);
if (!svc)
return [0, 0];
return svc.getPosition();
};
JSIL.Host.getHeldMouseButtons = function () {
var svc = JSIL.Host.getService("mouse", true);
if (!svc)
return [];
return svc.getHeldButtons();
};
JSIL.Host.isPageVisible = function () {
var svc = JSIL.Host.getService("pageVisibility", true);
if (!svc)
return true;
return svc.get();
};
JSIL.Host.runLater = function (action) {
var svc = JSIL.Host.getService("runLater", true);
if (!svc)
return false;
svc.enqueue(action);
return true;
};
JSIL.Host.runLaterFlush = function () {
var svc = JSIL.Host.getService("runLater", true);
if (!svc)
return false;
if (svc.flush) {
svc.flush();
return true;
}
return false;
};
JSIL.Host.logWrite = function (text) {
var svc = JSIL.Host.getService("stdout");
svc.write(text);
};
JSIL.Host.logWriteLine = function (text) {
var svc = JSIL.Host.getService("stdout");
svc.write(text + "\n");
};
JSIL.Host.warning = function (text) {
var svc = JSIL.Host.getService("stderr");
var stack = Error().stack;
if (stack)
svc.write(text + "\n" + stack.slice(stack.indexOf("\n", 6)+1, -1));
else
svc.write(text + "\n");
}
JSIL.Host.abort = function (exception, extraInfo) {
var svc = JSIL.Host.getService("stderr");
if (extraInfo)
svc.write(extraInfo);
try {
svc.write(exception);
} catch (exc) {
}
try {
if (exception.stack)
svc.write(exception.stack);
} catch (exc) {
}
var svc = JSIL.Host.getService("error");
svc.error(exception);
};
JSIL.Host.assertionFailed = function (message) {
var svc = JSIL.Host.getService("error");
svc.error(new Error(message || "Assertion Failed"));
};
JSIL.Host.scheduleTick = function (tickCallback) {
var svc = JSIL.Host.getService("tickScheduler");
svc.schedule(tickCallback);
};
JSIL.Host.reportPerformance = function (drawDuration, updateDuration, cacheSize, isWebGL) {
var svc = JSIL.Host.getService("performanceReporter", true);
if (!svc)
return;
svc.report(drawDuration, updateDuration, cacheSize, isWebGL);
};
// Default service implementations that are environment-agnostic
// Don't use for anything that needs to be reproducible in replays!
(function () {
if (
(typeof (window) !== "undefined") &&
(typeof (window.performance) !== "undefined") &&
(typeof (window.performance.now) === "function")
)
JSIL.$GetHighResTime = window.performance.now.bind(window.performance);
else
JSIL.$GetHighResTime = Date.now.bind(Date);
})();
JSIL.Host.ES5TimeService = function () {
this.started = JSIL.$GetHighResTime();
};
JSIL.Host.ES5TimeService.prototype.getTickCount = function () {
var result = JSIL.$GetHighResTime() - this.started;
// Round it to a few digits past the decimal.
result *= 100;
result = Math.round(result);
result /= 100;
return result;
};
JSIL.Host.ES5TimeService.prototype.getUTC = function () {
return Date.now();
};
JSIL.Host.ThrowErrorService = function () {
};
JSIL.Host.ThrowErrorService.prototype.error = function (exception) {
throw exception;
};
JSIL.Host.registerServices({
time: new JSIL.Host.ES5TimeService(),
error: new JSIL.Host.ThrowErrorService()
});