File size: 2,787 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!doctype html>
<title>Lua interpreter</title>

<script src="../build/libv86.js"></script>
<script>
"use strict";

window.onload = function()
{
    var emulator = new V86({
        wasm_path: "../build/v86.wasm",
        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: {
            url: "../bios/seabios.bin",
        },
        vga_bios: {
            url: "../bios/vgabios.bin",
        },
        bzimage: {
            url: "../images/buildroot-bzimage68.bin",
        },
        autostart: true,
        disable_keyboard: true,
        disable_mouse: true,
    });

    var data = "";
    var do_output = false;

    emulator.add_listener("serial0-output-byte", function(byte)
    {
        var char = String.fromCharCode(byte);
        if(char !== "\r")
        {
            data += char;
        }

        if(do_output)
        {
            document.getElementById("result").textContent += char;
        }

        if(data.endsWith("~% "))
        {
            console.log("Now ready");
            document.getElementById("status").textContent = "Ready.\n";
            document.getElementById("run").disabled = false;
            do_output = false;
        }
    });

    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;

        do_output = true;
    };
};

// https://gist.github.com/creationix/2502704
// Implement bash string escaping.
function bashEscape(arg)
{
    arg = arg.replace(/\t+/g, "");
    return "'" + arg.replace(/'+/g, function (val) {
        return "'" + val.replace(/'/g, "\\'") + "'";
    }) + "'";
}
</script>

<textarea id=source rows=20 cols=80>
k = 1
x = 0

while k &lt; 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>