2015-01-12 06:16:01 +01:00
|
|
|
<!doctype html>
|
|
|
|
<title>Lua interpreter</title>
|
|
|
|
|
2015-09-12 00:35:44 +02:00
|
|
|
<script src="../build/libv86.js"></script>
|
2015-01-12 06:16:01 +01:00
|
|
|
<script>
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
window.onload = function()
|
|
|
|
{
|
|
|
|
var emulator = new V86Starter({
|
2021-01-01 02:14:30 +01:00
|
|
|
wasm_path: "../build/v86.wasm",
|
2015-01-12 06:16:01 +01:00
|
|
|
memory_size: 32 * 1024 * 1024,
|
|
|
|
vga_memory_size: 2 * 1024 * 1024,
|
|
|
|
|
|
|
|
// Uncomment to see what's going on
|
|
|
|
//screen_container: document.getElementById("screen_container"),
|
|
|
|
|
|
|
|
bios: {
|
2015-09-12 00:35:44 +02:00
|
|
|
url: "../bios/seabios.bin",
|
2015-01-12 06:16:01 +01:00
|
|
|
},
|
|
|
|
vga_bios: {
|
2015-09-12 00:35:44 +02:00
|
|
|
url: "../bios/vgabios.bin",
|
2015-01-12 06:16:01 +01:00
|
|
|
},
|
2021-01-02 03:10:47 +01:00
|
|
|
bzimage: {
|
|
|
|
url: "../images/buildroot-bzimage.bin",
|
2015-01-12 06:16:01 +01:00
|
|
|
},
|
|
|
|
autostart: true,
|
|
|
|
disable_keyboard: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
var data = "";
|
|
|
|
|
|
|
|
emulator.add_listener("serial0-output-char", function(char)
|
|
|
|
{
|
|
|
|
if(char !== "\r")
|
|
|
|
{
|
|
|
|
data += char;
|
|
|
|
}
|
|
|
|
|
2021-01-02 03:10:47 +01:00
|
|
|
if(data.endsWith("~% "))
|
2015-01-12 06:16:01 +01:00
|
|
|
{
|
|
|
|
console.log("Now ready");
|
|
|
|
document.getElementById("status").textContent = "Ready.\n";
|
|
|
|
document.getElementById("run").disabled = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
emulator.add_listener("serial0-output-line", function(line)
|
|
|
|
{
|
|
|
|
// filter noise
|
2015-09-12 00:35:44 +02:00
|
|
|
if(!line.startsWith("/root% lua -e") &&
|
2015-01-12 06:16:01 +01:00
|
|
|
!line.startsWith("> ") &&
|
2015-09-12 00:35:44 +02:00
|
|
|
line.indexOf("Welcome to Buildroot") === -1 &&
|
2015-01-12 06:16:01 +01:00
|
|
|
line.indexOf("login:") === -1 &&
|
|
|
|
line.trim() !== "")
|
|
|
|
{
|
|
|
|
document.getElementById("result").textContent += line;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById("source").onkeydown = function(e)
|
|
|
|
{
|
|
|
|
if(e.which == 13 && e.ctrlKey)
|
|
|
|
{
|
|
|
|
document.getElementById("run").onclick();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.getElementById("run").onclick = function()
|
|
|
|
{
|
|
|
|
var code = document.getElementById("source").value;
|
|
|
|
|
|
|
|
emulator.serial0_send("lua -e " + bashEscape(code) + "\n");
|
|
|
|
|
|
|
|
document.getElementById("result").textContent = "";
|
|
|
|
document.getElementById("status").textContent = "Running ...\n";
|
|
|
|
this.disabled = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// https://gist.github.com/creationix/2502704
|
|
|
|
// Implement bash string escaping.
|
2016-01-01 17:50:13 +01:00
|
|
|
function bashEscape(arg)
|
2015-01-12 06:16:01 +01:00
|
|
|
{
|
2019-11-12 17:33:55 +01:00
|
|
|
arg = arg.replace(/\t+/g, "");
|
2015-01-12 06:16:01 +01:00
|
|
|
return "'" + arg.replace(/'+/g, function (val) {
|
|
|
|
return "'" + val.replace(/'/g, "\\'") + "'";
|
|
|
|
}) + "'";
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<textarea id=source rows=20 cols=80>
|
|
|
|
k = 1
|
|
|
|
x = 0
|
|
|
|
|
|
|
|
while k < 1000 do
|
|
|
|
x = x + 1 / (k * k)
|
|
|
|
k = k + 2
|
|
|
|
end
|
|
|
|
|
|
|
|
print(math.sqrt(x*8))
|
|
|
|
|
|
|
|
function factorial(n)
|
|
|
|
if n == 0 then
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
return n * factorial(n - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print("factorial(10):", factorial(10))
|
|
|
|
</textarea>
|
|
|
|
<button disabled id=run>run (ctrl-enter)</button>
|
|
|
|
<br>
|
|
|
|
<hr>
|
|
|
|
<pre id=status>Wait for boot ...</pre>
|
|
|
|
<pre id=result></pre>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
|
|
|
|
<div id="screen_container">
|
|
|
|
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
|
|
|
|
<canvas style="display: none"></canvas>
|
|
|
|
</div>
|