-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
174 lines (150 loc) · 4.46 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CheerpJ MIPS Mars Transpilation</title>
<script src="https://cjrtnc.leaningtech.com/2.3/loader.js"></script>
<style>
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#cheerpjDisplay {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
</body>
<script>
// compute homepage from url
let url = window.location.href;
// const homepage = new URL(url).pathname;
// const homepage = url.substring(0, url.lastIndexOf("/") + 1);
// folder relative to toplevel
const homepage = url.substring(url.indexOf("/", 8), url.lastIndexOf("/") + 1);
console.log("url", url);
console.log("homepage", homepage);
let code = undefined;
let urlParams = null;
if (window.location.search) {
console.log("search:", window.location.search);
urlParams = new URLSearchParams(window.location.search);
}
if (window.location.hash) {
urlParams = new URLSearchParams("?" + window.location.hash.substring(1));
}
if (urlParams) {
if (urlParams.get('code')) {
code = urlParams.get('code');
}
}
cheerpjInit({ clipboardMode: "system" });
cheerpjCreateDisplay(800, 600);
const display = document.getElementById("cheerpjDisplay");
display.style.width = "100%";
display.style.height = "100%";
const jar_call = "/app" + homepage + "Mars.jar" + ":" + "/app" + homepage + "Util.jar";
function checkFileSystem(path) {
for (var i = 0; i < cheerpjFSMounts.length; i++) {
var mount = cheerpjFSMounts[i];
if (path.startsWith(mount.mountPoint)) {
return true;
}
}
return false;
}
if (code) {
console.log("load init code", code);
const file_path = "/str/" + "code.mips";
const repeat_time = 500;
function try_add() {
try {
// check needed to avoid debuggers
if (checkFileSystem(file_path)) {
cheerpjAddStringFile(file_path, code);
} else {
setTimeout(try_add, repeat_time);
}
} catch (e) {
console.log("error creating init file", e);
setTimeout(try_add, repeat_time);
}
}
try_add();
//cheerpjAddStringFile(file_path, code);
console.log("saved code to", file_path);
// cheerpjAddStringFile("/str/" + "code.mips", "li $v0, 1\nli $a0, 1\nsyscall\nli $v0, 10\nsyscall\n");
cheerpjRunMain("Mars", jar_call, "-g", file_path);
} else {
cheerpjRunMain("Mars", jar_call);
}
// we can use a /index.list file to expose files to /app/
// but this does not work with folders
function read_file(url, callback, error_callback = () => { }) {
const request = new XMLHttpRequest();
request.open("GET", url);
request.responseType = "text";
request.onload = function () {
if (request.status === 200) {
callback(request.response);
} else {
error_callback();
}
};
request.send();
}
function createDirectory(path) {
console.log("createDirectory", path)
file = cjNew("java.io.File", "/files/" + path);
cjCall(file, "mkdirs");
}
function copyFile(src, dst) {
src_str = "/app" + homepage + src;
dst_str = "/files/" + dst;
cjCall("util.Util", "copyFile", src_str, dst_str, true);
}
function processFileOperation(s) {
console.log("processFileOperation", s)
const parts = s.split(" ");
switch (parts[0]) {
case "mkdir":
createDirectory(parts[1]);
break;
case "copy":
copyFile(parts[1], parts[2]);
break;
case "create":
copyFile(parts[1], parts[1]);
break;
}
}
setTimeout(() => {
read_file("files.list",
(content) => {
const files = content.split("\n");
files.forEach((file) => {
if (file.length == 0) {
return;
}
processFileOperation(file);
});
},
() => {
console.log("error loading list");
});
}, 1000);
// general info: https://docs.leaningtech.com/cheerpj/Runtime-API.html
// file system: https://docs.leaningtech.com/cheerpj/File-System-support.html
// load prepared files /app/asm/*.asm -- but these files are not shown in the file system
// /files/ is persistent indexedDB storage
</script>
Save your own files in /files/. You can also find examples there.
<br />
The first start takes a bit of time to load all resources.
If after a minute or two nothing happend, try hard refresh (Ctrl+F5 / Ctrl+Shift+R).
</html>