-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
181 lines (148 loc) · 4.91 KB
/
index.ts
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
import { BrowserVm } from "./browserVm";
import CodeMirror from "codemirror";
import "codemirror/mode/ruby/ruby";
import "codemirror/lib/codemirror.css";
import "codemirror/theme/rubyblue.css";
import "codemirror/addon/edit/matchbrackets";
import "codemirror/addon/edit/closebrackets";
import "./style.css";
import LZString from "lz-string"
let browserVm:BrowserVm;
let outputBuffer:string[] = [];
(window as any).frameBuffer = new Uint32Array(100*100);
let frameBuffer:Uint32Array = (window as any).frameBuffer;
const codeEditor = CodeMirror.fromTextArea(
document.getElementById("input") as HTMLTextAreaElement,
{
theme: 'rubyblue',
mode: "text/x-ruby",
indentUnit: 2,
matchBrackets: true,
autoCloseBrackets: true
}
);
codeEditor.setOption("extraKeys", {
"Ctrl-Enter": function(cm) {
runRubyScriptsInHtml()
}
});
const outputTextArea:HTMLTextAreaElement = <HTMLTextAreaElement>document.getElementById("output");
const printToOutput = function (line: string):void {
outputBuffer.push(line);
outputTextArea.value = outputBuffer.join("");
}
const main = async () => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('q')
if (code !== null) {
if (code === "") {
codeEditor.setValue("")
} else {
codeEditor.setValue(LZString.decompressFromEncodedURIComponent(code))
}
}
const q2 = urlParams.get('q2')
if (q2 !== null) {
if (q2 !== "") {
codeEditor.setValue(decodeURIComponent(q2))
}
}
browserVm = new BrowserVm()
await browserVm.createVm(printToOutput)
document.getElementById("run").onclick = runRubyScriptsInHtml;
document.getElementById("clear").onclick = selectAllScripts;
document.getElementById("files").onclick = listFiles;
codeEditor.focus();
runRubyScriptsInHtml();
};
export const runRubyScriptsInHtml = function () {
outputBuffer = [];
try {
// Rewrite URL
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
urlParams.set("q", LZString.compressToEncodedURIComponent(codeEditor.getValue()));
urlParams.delete("q2");
history.replaceState('', '', "?" + urlParams.toString());
// Clear framebuffer
frameBuffer.set(new Uint8Array(100*100));
// Run eval
const src = `
require "js"
def set_pixel(x, y, color)
JS::eval("frameBuffer[#{y} * 100 + #{x}] = #{color}")
end
`
const result = browserVm.vm.eval(src + "\n" + codeEditor.getValue());
if (outputBuffer.length == 0) {
outputTextArea.value = result.toString()
}
const canvasdiv = document.getElementById("canvasdiv");
const isPixelWritten = frameBuffer.findIndex((e) => e !== 0) != -1
if (isPixelWritten) {
canvasdiv.style.display = "block";
writeToCanvas();
} else {
canvasdiv.style.display = "none";
}
} catch (error) {
outputTextArea.value = error;
}
listFiles();
};
export const selectAllScripts = function () {
codeEditor.focus();
codeEditor.execCommand("selectAll");
};
const listFiles = function () {
const files = <HTMLTextAreaElement>document.getElementById("files");
const script = `def filetree(path)
str = "#{path}\n"
Dir.entries(".").each do |file|
if File.directory?(file)
str += "├─ #{file}/\n"
else
str += "├─ #{file}\n"
end
end
str
end
filetree(File.expand_path(Dir.pwd))
`;
files.value = browserVm.vm.eval(script).toString()
};
// quick and dirty image data scaling
// see: https://stackoverflow.com/questions/3448347/how-to-scale-an-imagedata-in-html-canvas
const scaleImageData = (imageData: any, scale: any, ctx: any) => {
const scaled = ctx.createImageData(imageData.width * scale, imageData.height * scale);
const subLine = ctx.createImageData(scale, 1).data;
for (let row = 0; row < imageData.height; row++) {
for (let col = 0; col < imageData.width; col++) {
const sourcePixel = imageData.data.subarray((row * imageData.width + col) * 4, (row * imageData.width + col) * 4 + 4);
for (let x = 0; x < scale; x++)
subLine.set(sourcePixel, x * 4);
for (let y = 0; y < scale; y++) {
const destRow = row * scale + y;
const destCol = col * scale;
scaled.data.set(subLine, (destRow * scaled.width + destCol) * 4);
}
}
}
return scaled;
};
function writeToCanvas() {
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const context = canvas.getContext("2d");
const imgData = context.createImageData(100, 100);
for (let i = 0; i < 100 * 100; i++) {
const c:number = frameBuffer[i];
imgData.data[i * 4] = (c >> 24) & 0xff;
imgData.data[i * 4 + 1] = (c >> 16) & 0xff;
imgData.data[i * 4 + 2] = (c >> 8) & 0xff;
imgData.data[i * 4 + 3] = c & 0xff;
}
const data = scaleImageData(imgData, 3, context);
context.putImageData(data, 0, 0);
}
main();