forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator.ts
More file actions
335 lines (303 loc) · 12.1 KB
/
simulator.ts
File metadata and controls
335 lines (303 loc) · 12.1 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/// <reference path="../../built/pxtsim.d.ts" />
/// <reference path="../../localtypings/pxtparts.d.ts" />
import * as core from "./core";
import * as coretsx from "./coretsx";
import U = pxt.U
interface SimulatorConfig {
// return true if a visible breakpoint was found
orphanException(brk: pxsim.DebuggerBreakpointMessage): void;
highlightStatement(stmt: pxtc.LocationInfo, brk?: pxsim.DebuggerBreakpointMessage): boolean;
restartSimulator(): void;
onStateChanged(state: pxsim.SimulatorState): void;
editor: string;
}
export const FAST_TRACE_INTERVAL = 100;
export const SLOW_TRACE_INTERVAL = 500;
export let driver: pxsim.SimulatorDriver;
let config: SimulatorConfig;
let lastCompileResult: pxtc.CompileResult;
let displayedModals: pxt.Map<boolean> = {};
export let simTranslations: pxt.Map<string>;
export function setTranslations(translations: pxt.Map<string>) {
simTranslations = translations;
}
export function init(root: HTMLElement, cfg: SimulatorConfig) {
if (!root) return;
pxsim.U.clear(root);
const simulatorsDiv = document.createElement('div');
simulatorsDiv.id = 'simulators';
simulatorsDiv.className = 'simulator';
root.appendChild(simulatorsDiv);
const debuggerDiv = document.createElement('div');
debuggerDiv.id = 'debugger';
debuggerDiv.className = 'ui item landscape only';
root.appendChild(debuggerDiv);
let options: pxsim.SimulatorDriverOptions = {
restart: () => cfg.restartSimulator(),
revealElement: (el) => {
if (pxt.options.light || driver.isLoanedSimulator(el)) return;
// Play enter animation
const animation = pxt.appTarget.appTheme.simAnimationEnter || 'fly right in';
el.style.animationDuration = '500ms';
const animationClasses = `${animation} visible transition animating`;
pxsim.U.addClass(el, animationClasses);
Promise.resolve().delay(500).then(() => {
pxsim.U.removeClass(el, animationClasses);
el.style.animationDuration = '';
if (pxt.BrowserUtils.isEdge() && coretsx.dialogIsShowing()) {
// Workaround for a Microsoft Edge bug where when a dialog is open and the simulator is
// revealed it somehow breaks the page render. See https://github.com/Microsoft/pxt/pull/4707
// for more details
document.body.style.display = "none";
requestAnimationFrame(() => {
document.body.style.display = "block";
});
}
})
},
removeElement: (el, completeHandler) => {
if (pxt.appTarget.simulator.headless) {
pxsim.U.addClass(el, 'simHeadless');
if (completeHandler) completeHandler();
}
else {
if (pxt.options.light) {
if (completeHandler) completeHandler();
pxsim.U.remove(el);
return;
}
// Play exit animation
const animation = pxt.appTarget.appTheme.simAnimationExit || 'fly right out';
el.style.animationDuration = '500ms';
const animationClasses = `${animation} visible transition animating`;
pxsim.U.addClass(el, animationClasses);
Promise.resolve().delay(500).then(() => {
pxsim.U.removeClass(el, `animating`);
el.style.animationDuration = '';
if (completeHandler) completeHandler();
pxsim.U.remove(el);
})
}
},
unhideElement: (el) => {
pxsim.U.removeClass(el, "simHeadless");
},
onDebuggerBreakpoint: function (brk) {
// walk stack until breakpoint is found
// and can be highlighted
let highlighted = false;
if (config) {
let frameid = 0;
let brkid = brk.breakpointId;
while (!highlighted) {
// try highlight current statement
if (brkid) {
const brkInfo = lastCompileResult.breakpoints[brkid];
highlighted = config.highlightStatement(brkInfo, brk);
}
// try next frame
if (!highlighted) {
frameid++;
const frame = brk.stackframes ? brk.stackframes[frameid] : undefined;
// no more frames, done
if (!frame) break;
brkid = frame.breakpointId;
}
}
}
// no exception and no highlighting, keep going
if (!brk.exceptionMessage && config && !highlighted) {
// keep going until breakpoint is hit
driver.resume(pxsim.SimulatorDebuggerCommand.StepInto);
return;
}
// we had an expected but could not find a block
if (!highlighted && brk.exceptionMessage) {
pxt.debug(`runtime error: ${brk.exceptionMessage}`);
pxt.debug(brk.exceptionStack);
if (config) config.orphanException(brk);
}
postSimEditorEvent("stopped", brk.exceptionMessage);
},
onTraceMessage: function (msg) {
let brkInfo = lastCompileResult.breakpoints[msg.breakpointId]
if (config) config.highlightStatement(brkInfo)
},
onDebuggerWarning: function (wrn) {
for (let id of wrn.breakpointIds) {
let brkInfo = lastCompileResult.breakpoints[id]
if (brkInfo) {
if (!U.startsWith("pxt_modules/", brkInfo.fileName)) {
if (config) config.highlightStatement(brkInfo)
break
}
}
}
},
onDebuggerResume: function () {
postSimEditorEvent("resumed");
if (config) config.highlightStatement(null)
},
onStateChanged: function (state) {
if (state === pxsim.SimulatorState.Stopped) {
postSimEditorEvent("stopped");
} else if (state === pxsim.SimulatorState.Running) {
this.onDebuggerResume();
}
cfg.onStateChanged(state);
},
onSimulatorCommand: (msg: pxsim.SimulatorCommandMessage): void => {
switch (msg.command) {
case "restart":
cfg.restartSimulator();
break;
case "reload":
stop(true);
cfg.restartSimulator();
break;
case "modal":
stop();
if (!pxt.shell.isSandboxMode() && (!msg.displayOnceId || !displayedModals[msg.displayOnceId])) {
const modalOpts: core.ConfirmOptions = {
header: msg.header,
body: msg.body,
size: "large",
copyable: msg.copyable,
disagreeLbl: lf("Close"),
modalContext: msg.modalContext
};
const trustedSimUrls = pxt.appTarget.simulator.trustedUrls;
const hasTrustedLink = msg.linkButtonHref && trustedSimUrls && trustedSimUrls.indexOf(msg.linkButtonHref) !== -1;
if (hasTrustedLink) {
modalOpts.agreeLbl = msg.linkButtonLabel;
} else {
modalOpts.hideAgree = true;
}
displayedModals[msg.displayOnceId] = true;
core.confirmAsync(modalOpts)
.then((selection) => {
if (hasTrustedLink && selection == 1) {
window.open(msg.linkButtonHref, '_blank');
}
})
.done();
}
break;
}
},
onTopLevelCodeEnd: () => {
postSimEditorEvent("toplevelfinished");
},
stoppedClass: pxt.appTarget.simulator && pxt.appTarget.simulator.stoppedClass,
invalidatedClass: pxt.appTarget.simulator && pxt.appTarget.simulator.invalidatedClass,
autoRun: pxt.appTarget.simulator && (pxt.options.light
? !!pxt.appTarget.simulator.autoRunLight
: !!pxt.appTarget.simulator.autoRun)
};
driver = new pxsim.SimulatorDriver(document.getElementById('simulators'), options);
config = cfg
}
function postSimEditorEvent(subtype: string, exception?: string) {
if (pxt.appTarget.appTheme.allowParentController && pxt.BrowserUtils.isIFrame()) {
pxt.editor.postHostMessageAsync({
type: "pxthost",
action: "simevent",
subtype: subtype as any,
exception: exception
} as pxt.editor.EditorSimulatorStoppedEvent);
}
}
let tutorialMode: boolean = false;
export function setState(editor: string, tutMode?: boolean) {
if (config && config.editor != editor) {
config.editor = editor;
config.highlightStatement(null)
}
tutorialMode = tutMode;
}
export function setDirty() { // running outdated code
driver.setDirty();
}
export function setStarting() {
driver.setStarting();
}
export function run(pkg: pxt.MainPackage, debug: boolean,
res: pxtc.CompileResult, mute?: boolean,
highContrast?: boolean, light?: boolean,
clickTrigger?: boolean) {
const js = res.outfiles[pxtc.BINARY_JS]
const boardDefinition = pxt.appTarget.simulator.boardDefinition;
const parts = pxtc.computeUsedParts(res, true);
const fnArgs = res.usedArguments;
lastCompileResult = res;
const opts: pxsim.SimulatorRunOptions = {
boardDefinition: boardDefinition,
mute,
parts,
debug,
fnArgs,
highContrast,
light,
aspectRatio: parts.length ? pxt.appTarget.simulator.partsAspectRatio : pxt.appTarget.simulator.aspectRatio,
partDefinitions: pkg.computePartDefinitions(parts),
cdnUrl: pxt.webConfig.commitCdnUrl,
localizedStrings: simTranslations,
refCountingDebug: pxt.options.debug,
version: pkg.version(),
clickTrigger: clickTrigger
}
//if (pxt.options.debug)
// pxt.debug(JSON.stringify(opts, null, 2))
postSimEditorEvent("started");
driver.run(js, opts);
}
export function mute(mute: boolean) {
if (!driver) return;
driver.mute(mute);
}
export function stop(unload?: boolean, starting?: boolean) {
if (!driver) return;
driver.stop(unload, starting);
}
export function suspend() {
if (!driver) return;
driver.suspend();
}
export function hide(completeHandler?: () => void) {
if (!driver) return;
driver.hide(completeHandler);
}
export function unhide() {
if (!driver) return;
driver.unhide();
}
export function setTraceInterval(intervalMs: number) {
if (!driver) return;
driver.setTraceInterval(intervalMs);
}
export function proxy(message: pxsim.SimulatorCustomMessage) {
if (!driver) return;
driver.postMessage(message);
}
export function dbgPauseResume() {
if (driver.state == pxsim.SimulatorState.Paused) {
driver.resume(pxsim.SimulatorDebuggerCommand.Resume);
} else if (driver.state == pxsim.SimulatorState.Running) {
driver.resume(pxsim.SimulatorDebuggerCommand.Pause);
}
}
export function dbgStepOver() {
if (driver.state == pxsim.SimulatorState.Paused) {
driver.resume(pxsim.SimulatorDebuggerCommand.StepOver);
}
}
export function dbgStepInto() {
if (driver.state == pxsim.SimulatorState.Paused) {
driver.resume(pxsim.SimulatorDebuggerCommand.StepInto);
}
}
export function dbgStepOut() {
if (driver.state == pxsim.SimulatorState.Paused) {
driver.resume(pxsim.SimulatorDebuggerCommand.StepOut);
}
}