Script.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package me.hammerle.snuviscript.code;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Stack;
  6. import me.hammerle.snuviscript.exceptions.CodeTooLongException;
  7. import me.hammerle.snuviscript.variable.LocalVariable;
  8. import me.hammerle.snuviscript.variable.Variable;
  9. public final class Script
  10. {
  11. protected final String name;
  12. protected final int id;
  13. protected ISnuviLogger logger;
  14. protected ISnuviScheduler scheduler;
  15. protected int currentLine;
  16. protected Instruction[] code;
  17. // waiting scripts stop executing and run again on an event
  18. protected boolean isWaiting;
  19. // holded scripts do not receive events
  20. protected boolean isHolded;
  21. // not valid means the script is waiting for its termination
  22. protected boolean isValid;
  23. // states if event broadcasts should be received, otherwise only direct event calls work
  24. protected boolean receiveEventBroadcast;
  25. // stores the used cpuTime, schedules the script if too high
  26. protected long cpuTime;
  27. protected int catchLine;
  28. protected String currentFunction;
  29. protected boolean ifState;
  30. protected final HashMap<String, Integer> labels;
  31. protected final Stack<Integer> returnStack;
  32. protected HashMap<String, Variable> vars;
  33. protected final Stack<HashMap<String, Variable>> localVars;
  34. protected final HashSet<String> events;
  35. protected Object returnValue;
  36. protected final boolean subScript;
  37. protected final String[] subScriptInput;
  38. protected final HashMap<String, Script> subScripts;
  39. protected boolean printStackTrace;
  40. private final Runnable onStart;
  41. private final Runnable onTerm;
  42. public Script(ISnuviLogger logger, ISnuviScheduler scheduler, List<String> code, String name,
  43. int id, Runnable onStart, Runnable onTerm, boolean receiveEventBroadcast)
  44. {
  45. this.logger = logger;
  46. this.scheduler = scheduler;
  47. this.subScriptInput = null;
  48. this.subScripts = new HashMap<>();
  49. this.labels = new HashMap<>();
  50. this.returnStack = new Stack<>();
  51. this.localVars = new Stack<>();
  52. this.events = new HashSet<>();
  53. this.subScript = false;
  54. this.currentLine = 0;
  55. this.isWaiting = false;
  56. this.isHolded = false;
  57. this.isValid = true;
  58. this.receiveEventBroadcast = receiveEventBroadcast;
  59. this.cpuTime = 0;
  60. this.catchLine = -1;
  61. this.currentFunction = null;
  62. this.ifState = true;
  63. this.printStackTrace = false;
  64. this.name = name;
  65. this.id = id;
  66. this.onStart = onStart;
  67. this.onTerm = onTerm;
  68. this.code = Compiler.compile(this, code, labels, subScript, 0);
  69. }
  70. public Script(List<String> code, String[] subScriptInput, Script sc, int lineOffset)
  71. {
  72. this.logger = sc.logger;
  73. this.scheduler = sc.scheduler;
  74. this.subScriptInput = subScriptInput;
  75. this.subScripts = sc.subScripts;
  76. this.labels = new HashMap<>();
  77. this.returnStack = new Stack<>();
  78. this.localVars = sc.localVars;
  79. this.events = sc.events;
  80. this.subScript = true;
  81. this.currentLine = 0;
  82. this.isWaiting = sc.isWaiting;
  83. this.isHolded = sc.isHolded;
  84. this.isValid = sc.isValid;
  85. this.receiveEventBroadcast = sc.receiveEventBroadcast;
  86. this.catchLine = -1;
  87. this.printStackTrace = false;
  88. this.name = sc.name;
  89. this.id = sc.id;
  90. this.onStart = sc.onStart;
  91. this.onTerm = sc.onTerm;
  92. this.code = Compiler.compile(this, code, labels, subScript, lineOffset);
  93. }
  94. public HashMap<String, Variable> getLocalVars()
  95. {
  96. return localVars.peek();
  97. }
  98. // -------------------------------------------------------------------------
  99. // flow handling
  100. // -------------------------------------------------------------------------
  101. public Object run()
  102. {
  103. if(isHolded)
  104. {
  105. return returnValue;
  106. }
  107. int length = code.length;
  108. returnValue = null;
  109. isWaiting = false;
  110. cpuTime = 0;
  111. long time;
  112. while(currentLine < length && !isWaiting)
  113. {
  114. time = System.nanoTime();
  115. try
  116. {
  117. //System.out.println("EXECUTE: " + code[currentLine]);
  118. code[currentLine].execute(this);
  119. currentLine++;
  120. }
  121. catch(Exception ex)
  122. {
  123. if(printStackTrace)
  124. {
  125. ex.printStackTrace();
  126. }
  127. if(catchLine != -1)
  128. {
  129. currentLine = catchLine + 1; // + 1 because currentLine++ isn't happening
  130. catchLine = -1;
  131. setVar("error", ex.getClass().getSimpleName());
  132. continue;
  133. }
  134. logger.print(ex.getLocalizedMessage(), ex, currentFunction, name, this, code[currentLine].getRealLine() + 1);
  135. ex.printStackTrace();
  136. return returnValue;
  137. }
  138. time = System.nanoTime() - time;
  139. cpuTime += time;
  140. if(cpuTime > 10_000_000)
  141. {
  142. if(subScript)
  143. {
  144. throw new CodeTooLongException();
  145. }
  146. isWaiting = true;
  147. isHolded = true;
  148. scheduler.scheduleTask(() ->
  149. {
  150. if(isValid)
  151. {
  152. isHolded = false;
  153. run();
  154. }
  155. }, 1);
  156. return Void.TYPE;
  157. }
  158. }
  159. return returnValue;
  160. }
  161. public void end()
  162. {
  163. currentLine = code.length;
  164. }
  165. // -------------------------------------------------------------------------
  166. // general stuff
  167. // -------------------------------------------------------------------------
  168. public String getName()
  169. {
  170. return name;
  171. }
  172. public int getId()
  173. {
  174. return id;
  175. }
  176. public ISnuviLogger getLogger()
  177. {
  178. return logger;
  179. }
  180. public Variable getVar(String name)
  181. {
  182. HashMap<String, Variable> map;
  183. if(subScript)
  184. {
  185. map = localVars.peek();
  186. }
  187. else
  188. {
  189. map = vars;
  190. }
  191. return map.get(name);
  192. }
  193. public void setVar(String name, Object value)
  194. {
  195. HashMap<String, Variable> map;
  196. if(subScript)
  197. {
  198. map = localVars.peek();
  199. Variable var = map.get(name);
  200. if(var == null)
  201. {
  202. var = new LocalVariable(name);
  203. map.put(name, var);
  204. }
  205. var.set(this, value);
  206. }
  207. else
  208. {
  209. map = vars;
  210. Variable var = map.get(name);
  211. if(var == null)
  212. {
  213. var = new Variable(name);
  214. map.put(name, var);
  215. }
  216. var.set(this, value);
  217. }
  218. }
  219. // -------------------------------------------------------------------------
  220. // event handling
  221. // -------------------------------------------------------------------------
  222. public boolean isEventLoaded(String s)
  223. {
  224. return events.contains(s);
  225. }
  226. // -------------------------------------------------------------------------
  227. // onStart onTerm
  228. // -------------------------------------------------------------------------
  229. public void onStart()
  230. {
  231. if(onStart != null)
  232. {
  233. onStart.run();
  234. }
  235. }
  236. public void onTerm()
  237. {
  238. if(onTerm != null)
  239. {
  240. onTerm.run();
  241. }
  242. }
  243. }