Script.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Antlr4.Runtime;
  7. using MoonSharp.Interpreter.CoreLib;
  8. using MoonSharp.Interpreter.Debugging;
  9. using MoonSharp.Interpreter.Diagnostics;
  10. using MoonSharp.Interpreter.Execution.VM;
  11. using MoonSharp.Interpreter.Interop;
  12. using MoonSharp.Interpreter.Loaders;
  13. using MoonSharp.Interpreter.Tree;
  14. using MoonSharp.Interpreter.Tree.Statements;
  15. namespace MoonSharp.Interpreter
  16. {
  17. /// <summary>
  18. /// This class implements a Moon# scripting session. Multiple Script objects can coexist in the same program but cannot share
  19. /// data among themselves unless some mechanism is put in place.
  20. /// </summary>
  21. public class Script
  22. {
  23. Processor m_MainRoutine = null;
  24. List<Processor> m_Coroutines = new List<Processor>();
  25. ByteCode m_ByteCode;
  26. List<SourceCode> m_Sources = new List<SourceCode>();
  27. Table m_GlobalTable;
  28. IDebugger m_Debugger;
  29. IScriptLoader m_ScriptLoader = DefaultScriptLoader;
  30. Table[] m_TypeMetatables = new Table[(int)DataType.MaxMetaTypes];
  31. static Script()
  32. {
  33. DefaultScriptLoader = new ClassicLuaScriptLoader();
  34. }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="Script"/> class.
  37. /// </summary>
  38. public Script()
  39. : this(CoreModules.Preset_Default)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Script"/> class.
  44. /// </summary>
  45. /// <param name="coreModules">The core modules to be pre-registered in the default global table.</param>
  46. public Script(CoreModules coreModules)
  47. {
  48. DebugPrint = s => { Console.WriteLine(s); };
  49. m_ByteCode = new ByteCode();
  50. m_GlobalTable = new Table(this).RegisterCoreModules(coreModules);
  51. m_MainRoutine = new Processor(this, m_GlobalTable, m_ByteCode);
  52. ReseedRandomGenerator(DateTime.Now.Millisecond);
  53. }
  54. /// <summary>
  55. /// Gets or sets the script loader to use. A script loader wraps all code loading from files, so that access
  56. /// to the filesystem can be completely overridden.
  57. /// </summary>
  58. /// <value>
  59. /// The current script loader.
  60. /// </value>
  61. public IScriptLoader ScriptLoader
  62. {
  63. get { return m_ScriptLoader; }
  64. set { m_ScriptLoader = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the script loader which will be used as the value of the
  68. /// ScriptLoader property for all newly created scripts.
  69. /// </summary>
  70. public static IScriptLoader DefaultScriptLoader { get; set; }
  71. /// <summary>
  72. /// Gets the default global table for this script. Unless a different table is intentionally passed (or setfenv has been used)
  73. /// execution uses this table.
  74. /// </summary>
  75. public Table Globals
  76. {
  77. get { return m_GlobalTable; }
  78. }
  79. /// <summary>
  80. /// Loads a string containing a Lua/Moon# function.
  81. /// </summary>
  82. /// <param name="code">The code.</param>
  83. /// <param name="globalTable">The global table to bind to this chunk.</param>
  84. /// <param name="funcFriendlyName">Name of the function used to report errors, etc.</param>
  85. /// <returns>
  86. /// A DynValue containing a function which will execute the loaded code.
  87. /// </returns>
  88. public DynValue LoadFunction(string code, Table globalTable = null, string funcFriendlyName = null)
  89. {
  90. string chunkName = string.Format("<string:{0:X4}>", m_Sources.Count);
  91. SourceCode source = new SourceCode(funcFriendlyName ?? chunkName, code);
  92. m_Sources.Add(source);
  93. int address = Loader.LoadFunctionFromICharStream(new AntlrInputStream(code),
  94. m_ByteCode,
  95. funcFriendlyName ?? chunkName,
  96. m_Sources.Count - 1,
  97. globalTable ?? m_GlobalTable);
  98. if (m_Debugger != null)
  99. m_Debugger.SetSourceCode(m_ByteCode, null);
  100. return MakeClosure(address);
  101. }
  102. /// <summary>
  103. /// Loads a string containing a Lua/Moon# script.
  104. /// </summary>
  105. /// <param name="code">The code.</param>
  106. /// <param name="globalTable">The global table to bind to this chunk.</param>
  107. /// <param name="codeFriendlyName">Name of the code - used to report errors, etc.</param>
  108. /// <returns>
  109. /// A DynValue containing a function which will execute the loaded code.
  110. /// </returns>
  111. public DynValue LoadString(string code, Table globalTable = null, string codeFriendlyName = null)
  112. {
  113. string chunkName = string.Format("<string:{0:X4}>", m_Sources.Count);
  114. SourceCode source = new SourceCode(codeFriendlyName ?? chunkName, code);
  115. m_Sources.Add(source);
  116. int address = Loader.LoadChunkFromICharStream(new AntlrInputStream(code),
  117. m_ByteCode,
  118. codeFriendlyName ?? chunkName,
  119. m_Sources.Count - 1,
  120. globalTable ?? m_GlobalTable);
  121. if (m_Debugger != null)
  122. m_Debugger.SetSourceCode(m_ByteCode, null);
  123. return MakeClosure(address);
  124. }
  125. /// <summary>
  126. /// Loads a string containing a Lua/Moon# script.
  127. /// </summary>
  128. /// <param name="filename">The code.</param>
  129. /// <param name="globalContext">The global table to bind to this chunk.</param>
  130. /// <returns>A DynValue containing a function which will execute the loaded code.</returns>
  131. public DynValue LoadFile(string filename, Table globalContext = null)
  132. {
  133. filename = m_ScriptLoader.ResolveFileName(filename, globalContext ?? m_GlobalTable);
  134. return LoadResolvedFile(filename, globalContext);
  135. }
  136. private DynValue LoadResolvedFile(string filename, Table globalContext)
  137. {
  138. if (m_ScriptLoader.HasCustomFileLoading())
  139. {
  140. return LoadString(m_ScriptLoader.LoadFile(filename, globalContext ?? m_GlobalTable), globalContext, filename);
  141. }
  142. else
  143. {
  144. return LoadString(File.ReadAllText(filename), globalContext, filename);
  145. }
  146. }
  147. /// <summary>
  148. /// Loads and executes a string containing a Lua/Moon# script.
  149. /// </summary>
  150. /// <param name="code">The code.</param>
  151. /// <param name="globalContext">The global context.</param>
  152. /// <returns>
  153. /// A DynValue containing the result of the processing of the loaded chunk.
  154. /// </returns>
  155. public DynValue DoString(string code, Table globalContext = null)
  156. {
  157. DynValue func = LoadString(code, globalContext);
  158. return Call(func);
  159. }
  160. /// <summary>
  161. /// Loads and executes a file containing a Lua/Moon# script.
  162. /// </summary>
  163. /// <param name="filename">The filename.</param>
  164. /// <param name="globalContext">The global context.</param>
  165. /// <returns>
  166. /// A DynValue containing the result of the processing of the loaded chunk.
  167. /// </returns>
  168. public DynValue DoFile(string filename, Table globalContext = null)
  169. {
  170. DynValue func = LoadFile(filename, globalContext);
  171. return Call(func);
  172. }
  173. /// <summary>
  174. /// Runs the specified file with all possible defaults for quick experimenting.
  175. /// </summary>
  176. /// <param name="filename">The filename.</param>
  177. /// A DynValue containing the result of the processing of the executed script.
  178. public static DynValue RunFile(string filename)
  179. {
  180. Script S = new Script();
  181. return S.DoFile(filename);
  182. }
  183. /// <summary>
  184. /// Runs the specified code with all possible defaults for quick experimenting.
  185. /// </summary>
  186. /// <param name="code">The Lua/Moon# code.</param>
  187. /// A DynValue containing the result of the processing of the executed script.
  188. public static DynValue RunString(string code)
  189. {
  190. Script S = new Script();
  191. return S.DoString(code);
  192. }
  193. /// <summary>
  194. /// Creates a closure from a bytecode address.
  195. /// </summary>
  196. /// <param name="address">The address.</param>
  197. /// <returns></returns>
  198. private DynValue MakeClosure(int address)
  199. {
  200. Closure c = new Closure(this, address,
  201. new SymbolRef[0],
  202. new DynValue[0]);
  203. return DynValue.NewClosure(c);
  204. }
  205. /// <summary>
  206. /// Calls the specified function.
  207. /// </summary>
  208. /// <param name="function">The Lua/Moon# function to be called - callbacks are not supported.</param>
  209. /// <returns></returns>
  210. public DynValue Call(DynValue function)
  211. {
  212. return m_MainRoutine.Call(function, new DynValue[0]);
  213. }
  214. /// <summary>
  215. /// Calls the specified function.
  216. /// </summary>
  217. /// <param name="function">The Lua/Moon# function to be called - callbacks are not supported.</param>
  218. /// <param name="args">The arguments to pass to the function.</param>
  219. /// <returns></returns>
  220. public DynValue Call(DynValue function, params DynValue[] args)
  221. {
  222. return m_MainRoutine.Call(function, args);
  223. }
  224. /// <summary>
  225. /// Calls the specified function.
  226. /// </summary>
  227. /// <param name="function">The Lua/Moon# function to be called - callbacks are not supported.</param>
  228. /// <param name="args">The arguments to pass to the function.</param>
  229. /// <returns></returns>
  230. public DynValue Call(DynValue function, params object[] args)
  231. {
  232. return m_MainRoutine.Call(function, args.Select(v => DynValue.FromObject(this, v)).ToArray());
  233. }
  234. /// <summary>
  235. /// Calls the specified function.
  236. /// </summary>
  237. /// <param name="function">The Lua/Moon# function to be called - callbacks are not supported.</param>
  238. /// <returns></returns>
  239. public DynValue Call(object function)
  240. {
  241. return Call(DynValue.FromObject(this, function));
  242. }
  243. /// <summary>
  244. /// Calls the specified function.
  245. /// </summary>
  246. /// <param name="function">The Lua/Moon# function to be called - callbacks are not supported.</param>
  247. /// <param name="args">The arguments to pass to the function.</param>
  248. /// <returns></returns>
  249. public DynValue Call(object function, params object[] args)
  250. {
  251. return Call(DynValue.FromObject(this, function), args);
  252. }
  253. /// <summary>
  254. /// Gets the main chunk function.
  255. /// </summary>
  256. /// <returns>A DynValue containing a function which executes the first chunk that has been loaded.</returns>
  257. public DynValue GetMainChunk()
  258. {
  259. return MakeClosure(0);
  260. }
  261. /// <summary>
  262. /// Attaches a debugger.
  263. /// </summary>
  264. /// <param name="debugger">The debugger object.</param>
  265. public void AttachDebugger(IDebugger debugger)
  266. {
  267. m_Debugger = debugger;
  268. m_MainRoutine.AttachDebugger(debugger);
  269. foreach (var C in m_Coroutines)
  270. C.AttachDebugger(debugger);
  271. m_Debugger.SetSourceCode(m_ByteCode, null);
  272. }
  273. /// <summary>
  274. /// Loads a module as per the "require" Lua function. http://www.lua.org/pil/8.1.html
  275. /// </summary>
  276. /// <param name="modname">The module name</param>
  277. /// <param name="globalContext">The global context.</param>
  278. /// <returns></returns>
  279. /// <exception cref="ScriptRuntimeException">Raised if module is not found</exception>
  280. public DynValue RequireModule(string modname, Table globalContext = null)
  281. {
  282. Table globals = globalContext ?? m_GlobalTable;
  283. string filename = m_ScriptLoader.ResolveModuleName(modname, globals);
  284. if (filename == null)
  285. throw new ScriptRuntimeException("module '{0}' not found", modname);
  286. DynValue func = LoadResolvedFile(filename, globalContext);
  287. return func;
  288. }
  289. /// <summary>
  290. /// Gets the random generator associated with this Script
  291. /// </summary>
  292. public Random RandomGenerator { get; private set; }
  293. /// <summary>
  294. /// Reseeds the random generator.
  295. /// </summary>
  296. /// <param name="seed">The seed.</param>
  297. public void ReseedRandomGenerator(double seed)
  298. {
  299. RandomGenerator = new Random();
  300. }
  301. /// <summary>
  302. /// Gets a type metatable.
  303. /// </summary>
  304. /// <param name="type">The type.</param>
  305. /// <returns></returns>
  306. public Table GetTypeMetatable(DataType type)
  307. {
  308. int t = (int)type;
  309. if (t >= 0 && t < m_TypeMetatables.Length)
  310. return m_TypeMetatables[t];
  311. return null;
  312. }
  313. /// <summary>
  314. /// Sets a type metatable.
  315. /// </summary>
  316. /// <param name="type">The type. Must be Nil, Boolean, Number, String or Function</param>
  317. /// <param name="metatable">The metatable.</param>
  318. /// <exception cref="System.ArgumentException">Specified type not supported : + type.ToString()</exception>
  319. public void SetTypeMetatable(DataType type, Table metatable)
  320. {
  321. int t = (int)type;
  322. if (t >= 0 && t < m_TypeMetatables.Length)
  323. m_TypeMetatables[t] = metatable;
  324. else
  325. throw new ArgumentException("Specified type not supported : " + type.ToString());
  326. }
  327. /// <summary>
  328. /// Gets or sets the debug print handler
  329. /// </summary>
  330. public Action<string> DebugPrint { get; set; }
  331. /// <summary>
  332. /// Warms up the parser/lexer structures so that Moon# operations start faster.
  333. /// </summary>
  334. public static void WarmUp()
  335. {
  336. Script s = new Script(CoreModules.Basic);
  337. s.LoadString("return 1;");
  338. }
  339. }
  340. }