lua.html 2.7 KB

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