lua.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. bzimage: {
  21. url: "../images/buildroot-bzimage.bin",
  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("~% "))
  34. {
  35. console.log("Now ready");
  36. document.getElementById("status").textContent = "Ready.\n";
  37. document.getElementById("run").disabled = false;
  38. }
  39. });
  40. emulator.add_listener("serial0-output-line", function(line)
  41. {
  42. // filter noise
  43. if(!line.startsWith("/root% lua -e") &&
  44. !line.startsWith("> ") &&
  45. line.indexOf("Welcome to Buildroot") === -1 &&
  46. line.indexOf("login:") === -1 &&
  47. line.trim() !== "")
  48. {
  49. document.getElementById("result").textContent += line;
  50. }
  51. });
  52. document.getElementById("source").onkeydown = function(e)
  53. {
  54. if(e.which == 13 && e.ctrlKey)
  55. {
  56. document.getElementById("run").onclick();
  57. }
  58. };
  59. document.getElementById("run").onclick = function()
  60. {
  61. var code = document.getElementById("source").value;
  62. emulator.serial0_send("lua -e " + bashEscape(code) + "\n");
  63. document.getElementById("result").textContent = "";
  64. document.getElementById("status").textContent = "Running ...\n";
  65. this.disabled = true;
  66. };
  67. };
  68. // https://gist.github.com/creationix/2502704
  69. // Implement bash string escaping.
  70. function bashEscape(arg)
  71. {
  72. arg = arg.replace(/\t+/g, "");
  73. return "'" + arg.replace(/'+/g, function (val) {
  74. return "'" + val.replace(/'/g, "\\'") + "'";
  75. }) + "'";
  76. }
  77. </script>
  78. <textarea id=source rows=20 cols=80>
  79. k = 1
  80. x = 0
  81. while k &lt; 1000 do
  82. x = x + 1 / (k * k)
  83. k = k + 2
  84. end
  85. print(math.sqrt(x*8))
  86. function factorial(n)
  87. if n == 0 then
  88. return 1
  89. else
  90. return n * factorial(n - 1)
  91. end
  92. end
  93. print("factorial(10):", factorial(10))
  94. </textarea>
  95. <button disabled id=run>run (ctrl-enter)</button>
  96. <br>
  97. <hr>
  98. <pre id=status>Wait for boot ...</pre>
  99. <pre id=result></pre>
  100. <hr>
  101. <div id="screen_container">
  102. <div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
  103. <canvas style="display: none"></canvas>
  104. </div>