Script.java 8.8 KB

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