index.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Blockly Demo: JS Interpreter</title>
  6. <script src="acorn_interpreter.js"></script>
  7. <script src="../../blockly_compressed.js"></script>
  8. <script src="../../blocks_compressed.js"></script>
  9. <script src="../../javascript_compressed.js"></script>
  10. <script src="../../msg/js/en.js"></script>
  11. <style>
  12. body {
  13. background-color: #fff;
  14. font-family: sans-serif;
  15. }
  16. h1 {
  17. font-weight: normal;
  18. font-size: 140%;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <h1><a href="https://developers.google.com/blockly/">Blockly</a> &gt;
  24. <a href="../index.html">Demos</a> &gt; JS Interpreter</h1>
  25. <p>This is a simple demo of executing code with a sandboxed JavaScript interpreter.</p>
  26. <p>&rarr; More info on <a href="https://developers.google.com/blockly/guides/configure-blockly/web/running-javascript#js_interpreter">JS Interpreter</a>&hellip;</p>
  27. <p>
  28. <button onclick="parseCode()">Parse JavaScript</button>
  29. <button onclick="stepCode()" id="stepButton" disabled="disabled">Step JavaScript</button>
  30. </p>
  31. <div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
  32. <xml id="toolbox" style="display: none">
  33. <category name="Logic">
  34. <block type="controls_if"></block>
  35. <block type="logic_compare"></block>
  36. <block type="logic_operation"></block>
  37. <block type="logic_negate"></block>
  38. <block type="logic_boolean"></block>
  39. </category>
  40. <category name="Loops">
  41. <block type="controls_repeat_ext">
  42. <value name="TIMES">
  43. <block type="math_number">
  44. <field name="NUM">10</field>
  45. </block>
  46. </value>
  47. </block>
  48. <block type="controls_whileUntil"></block>
  49. </category>
  50. <category name="Math">
  51. <block type="math_number"></block>
  52. <block type="math_arithmetic"></block>
  53. <block type="math_single"></block>
  54. </category>
  55. <category name="Text">
  56. <block type="text"></block>
  57. <block type="text_length"></block>
  58. <block type="text_print"></block>
  59. <block type="text_prompt_ext">
  60. <value name="TEXT">
  61. <block type="text"></block>
  62. </value>
  63. </block>
  64. </category>
  65. <category name="Variables" custom="VARIABLE"></category>
  66. <category name="Functions" custom="PROCEDURE"></category>
  67. </xml>
  68. <xml id="startBlocks" style="display: none">
  69. <block type="variables_set" inline="true" x="20" y="20">
  70. <field name="VAR">n</field>
  71. <value name="VALUE">
  72. <block type="math_number">
  73. <field name="NUM">1</field>
  74. </block>
  75. </value>
  76. <next>
  77. <block type="controls_repeat_ext" inline="true">
  78. <value name="TIMES">
  79. <block type="math_number">
  80. <field name="NUM">4</field>
  81. </block>
  82. </value>
  83. <statement name="DO">
  84. <block type="variables_set" inline="true">
  85. <field name="VAR">n</field>
  86. <value name="VALUE">
  87. <block type="math_arithmetic" inline="true">
  88. <field name="OP">MULTIPLY</field>
  89. <value name="A">
  90. <block type="variables_get">
  91. <field name="VAR">n</field>
  92. </block>
  93. </value>
  94. <value name="B">
  95. <block type="math_number">
  96. <field name="NUM">2</field>
  97. </block>
  98. </value>
  99. </block>
  100. </value>
  101. </block>
  102. </statement>
  103. <next>
  104. <block type="text_print" inline="false">
  105. <value name="TEXT">
  106. <block type="variables_get">
  107. <field name="VAR">n</field>
  108. </block>
  109. </value>
  110. </block>
  111. </next>
  112. </block>
  113. </next>
  114. </block>
  115. </xml>
  116. <script>
  117. var workspace = Blockly.inject('blocklyDiv',
  118. {media: '../../media/',
  119. toolbox: document.getElementById('toolbox')});
  120. Blockly.Xml.domToWorkspace(document.getElementById('startBlocks'),
  121. workspace);
  122. var myInterpreter = null;
  123. function initApi(interpreter, scope) {
  124. // Add an API function for the alert() block.
  125. var wrapper = function(text) {
  126. text = text ? text.toString() : '';
  127. return interpreter.createPrimitive(alert(text));
  128. };
  129. interpreter.setProperty(scope, 'alert',
  130. interpreter.createNativeFunction(wrapper));
  131. // Add an API function for the prompt() block.
  132. var wrapper = function(text) {
  133. text = text ? text.toString() : '';
  134. return interpreter.createPrimitive(prompt(text));
  135. };
  136. interpreter.setProperty(scope, 'prompt',
  137. interpreter.createNativeFunction(wrapper));
  138. // Add an API function for highlighting blocks.
  139. var wrapper = function(id) {
  140. id = id ? id.toString() : '';
  141. return interpreter.createPrimitive(highlightBlock(id));
  142. };
  143. interpreter.setProperty(scope, 'highlightBlock',
  144. interpreter.createNativeFunction(wrapper));
  145. }
  146. var highlightPause = false;
  147. function highlightBlock(id) {
  148. workspace.highlightBlock(id);
  149. highlightPause = true;
  150. }
  151. function parseCode() {
  152. // Generate JavaScript code and parse it.
  153. Blockly.JavaScript.STATEMENT_PREFIX = 'highlightBlock(%1);\n';
  154. Blockly.JavaScript.addReservedWords('highlightBlock');
  155. var code = Blockly.JavaScript.workspaceToCode(workspace);
  156. myInterpreter = new Interpreter(code, initApi);
  157. alert('Ready to execute this code:\n\n' + code);
  158. document.getElementById('stepButton').disabled = '';
  159. highlightPause = false;
  160. workspace.highlightBlock(null);
  161. }
  162. function stepCode() {
  163. try {
  164. var ok = myInterpreter.step();
  165. } finally {
  166. if (!ok) {
  167. // Program complete, no more code to execute.
  168. document.getElementById('stepButton').disabled = 'disabled';
  169. workspace.highlightBlock(null);
  170. return;
  171. }
  172. }
  173. if (highlightPause) {
  174. // A block has been highlighted. Pause execution here.
  175. highlightPause = false;
  176. } else {
  177. // Keep executing until a highlight statement is reached.
  178. stepCode();
  179. }
  180. }
  181. </script>
  182. </body>
  183. </html>