lua.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <!doctype html>
  2. <title>Lua interpreter</title>
  3. <script src="../build/libv86.js"></script>
  4. <script>
  5. "use strict";
  6. window.onload = function()
  7. {
  8. var emulator = new V86Starter({
  9. memory_size: 32 * 1024 * 1024,
  10. vga_memory_size: 2 * 1024 * 1024,
  11. // Uncomment to see what's going on
  12. //screen_container: document.getElementById("screen_container"),
  13. bios: {
  14. url: "../bios/seabios.bin",
  15. },
  16. vga_bios: {
  17. url: "../bios/vgabios.bin",
  18. },
  19. cdrom: {
  20. url: "../images/linux.iso",
  21. },
  22. autostart: true,
  23. disable_keyboard: true,
  24. });
  25. var data = "";
  26. emulator.add_listener("serial0-output-char", function(char)
  27. {
  28. if(char !== "\r")
  29. {
  30. data += char;
  31. }
  32. if(data.endsWith("login: "))
  33. {
  34. console.log("Do login");
  35. emulator.serial0_send("root\n");
  36. }
  37. else if(data.endsWith("/root% "))
  38. {
  39. console.log("Now ready");
  40. document.getElementById("status").textContent = "Ready.\n";
  41. document.getElementById("run").disabled = false;
  42. }
  43. });
  44. emulator.add_listener("serial0-output-line", function(line)
  45. {
  46. // filter noise
  47. if(!line.startsWith("/root% lua -e") &&
  48. !line.startsWith("> ") &&
  49. line.indexOf("Welcome to Buildroot") === -1 &&
  50. line.indexOf("login:") === -1 &&
  51. line.trim() !== "")
  52. {
  53. document.getElementById("result").textContent += line;
  54. }
  55. });
  56. document.getElementById("source").onkeydown = function(e)
  57. {
  58. if(e.which == 13 && e.ctrlKey)
  59. {
  60. document.getElementById("run").onclick();
  61. }
  62. };
  63. document.getElementById("run").onclick = function()
  64. {
  65. var code = document.getElementById("source").value;
  66. emulator.serial0_send("lua -e " + bashEscape(code) + "\n");
  67. document.getElementById("result").textContent = "";
  68. document.getElementById("status").textContent = "Running ...\n";
  69. this.disabled = true;
  70. };
  71. };
  72. // https://gist.github.com/creationix/2502704
  73. // Implement bash string escaping.
  74. function bashEscape(arg)
  75. {
  76. return "'" + arg.replace(/'+/g, function (val) {
  77. return "'" + val.replace(/'/g, "\\'") + "'";
  78. }) + "'";
  79. }
  80. </script>
  81. <textarea id=source rows=20 cols=80>
  82. k = 1
  83. x = 0
  84. while k &lt; 1000 do
  85. x = x + 1 / (k * k)
  86. k = k + 2
  87. end
  88. print(math.sqrt(x*8))
  89. function factorial(n)
  90. if n == 0 then
  91. return 1
  92. else
  93. return n * factorial(n - 1)
  94. end
  95. end
  96. print("factorial(10):", factorial(10))
  97. </textarea>
  98. <button disabled id=run>run (ctrl-enter)</button>
  99. <br>
  100. <hr>
  101. <pre id=status>Wait for boot ...</pre>
  102. <pre id=result></pre>
  103. <hr>
  104. <div id="screen_container">
  105. <div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
  106. <canvas style="display: none"></canvas>
  107. </div>