Skip to content

Commit 3f11ba8

Browse files
committed
add a few comments
1 parent 9ee0f36 commit 3f11ba8

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

example/ipython.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@
3131

3232
ipythonExample = function (dom) {
3333
if (ace === undefined) {
34-
Sk.asserts.fail("No ace");
34+
throw Error("No ace");
3535
}
3636
this.editor = dom;
37+
38+
// focus the current inCell on clickGuard click event
3739
this.clickGuard = document.getElementById("clickGuard");
3840
this.clickGuard.addEventListener("click", () => {
3941
this.inCell.focus();
4042
});
43+
44+
// add a keyboard interrupt feature
4145
const keyboardInterrupt = (e) => {
4246
if (e.ctrlKey && e.key === "c") {
4347
// faile safe for keyboard interrupt
@@ -47,21 +51,21 @@
4751
this.editor.addEventListener("keydown", keyboardInterrupt);
4852
this.clickGuard.addEventListener("keydown", keyboardInterrupt);
4953

54+
// prepend browser info
5055
const infoElement = document.createElement("DIV");
5156
infoElement.innerText = info;
5257
infoElement.style.margin = "5px";
5358
this.editor.appendChild(infoElement);
5459

60+
// setup some basics
5561
this.inputs = [];
5662
this.idx = 0;
57-
this.inCell;
58-
this.outCell;
59-
this.printCell;
6063
this.newCells();
6164
this.outf = this.outf.bind(this);
6265
this.lineHeight = this.inCell.renderer.lineHeight || 16;
6366
this.pad = 15;
6467

68+
// configure the skulpt environment
6569
Sk.configure({
6670
output: this.outf,
6771
__future__: Sk.python3,
@@ -90,12 +94,12 @@
9094
ipythonExample.prototype.execute = function () {
9195
stopExecution = false;
9296
const code = this.inCell.getValue();
93-
const codeAsPyStr = new Sk.builtin.str(code);
94-
Sk.globals["_i" + this.inputs.length] = codeAsPyStr;
95-
Sk.misceval.callsimArray(Sk.globals.In.append, [Sk.globals.In, codeAsPyStr]);
97+
const codeAsPyStr = Sk.ffi.remapToPy(code);
98+
Sk.globals["_i" + this.inputs.length] = codeAsPyStr; // add the input to globals as per ipython
99+
Sk.globals.In.v.push(codeAsPyStr);
96100
this.inputs[this.inputs.length - 1] = code;
97101

98-
let compile_code = code.trimEnd() || "None";
102+
let compile_code = code.trimEnd() || "None"; // always have a last line
99103

100104
const lines = compile_code.split("\n");
101105
let last_line = lines[lines.length - 1];
@@ -110,8 +114,11 @@
110114
lines[lines.length - 1] = "_" + this.inputs.length + " = " + last_line;
111115
}
112116
compile_code = lines.join("\n");
117+
// ace editor stuff
113118
this.inCell.setReadOnly(true);
114119
this.inCell.renderer.$cursorLayer.element.style.opacity = 0;
120+
121+
// allow suspension and check the stopExecution flag in case of keyboard interrupt
115122
const executionPromise = Sk.misceval.asyncToPromise(() => Sk.importMainWithBody("ipython", false, compile_code, true), {
116123
"*": () => {
117124
if (stopExecution) {
@@ -130,7 +137,7 @@
130137

131138
const last_input = Sk.globals["_" + this.inputs.length];
132139
if (Sk.builtin.checkNone(last_input) || last_input === undefined) {
133-
delete Sk.globals["_" + this.inputs.length];
140+
delete Sk.globals["_" + this.inputs.length]; // if the last input evaluated to None remove it from globals
134141
} else {
135142
this.outCell.setValue(Sk.ffi.remapToJs(Sk.misceval.objectRepr(last_input)), -1);
136143
if (last_input !== Sk.globals.Out) {

0 commit comments

Comments
 (0)