Script.java 8.7 KB

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