lua.html 3.0 KB

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